From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FCE33451DA for ; Tue, 9 Jun 2026 14:40:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781016031; cv=none; b=L5ceo6uhGfHpElPRvb7oK+LKwkYz/LjTsr+bIpM90iK3JyujYrnajhAQ33/PdWNqT6da9XFlfwXPk6MVdSEi5BhZvpoC0WWqGIs0bVt6q9TE65+/w0VVQKFpVCbIiAiFOtiDR37vFg+8bvkMtU2as4XMSL41hUoF6hWR+kR2pLg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781016031; c=relaxed/simple; bh=MpqHY1226EZDlsL7AYrF9I24rCLFkLMgGMHa7jjWFqY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GU34kVRHVa2138ilDMINiLVeKbJyyypKIRQG08rxGYEI6R2o7mHTbwsOXcBkianYTVnNiK1cik6GJ+8RI91hy8Br57rmdEZ0crHi6K4oo97K7jAfnre+IaSj1zkAi7LbFWe6P5GTJGE6l8aWwvkuecmYTDiZg2pd+QJUOaUmRB0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=wFMfn8AQ; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="wFMfn8AQ" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-490ac357c55so61492915e9.1 for ; Tue, 09 Jun 2026 07:40:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1781016028; x=1781620828; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=9nUZrv+uXShy5oUjVbWJnHpZlIUPoN1ZejbvjYmlEBM=; b=wFMfn8AQKgwPpf5kmqGxAb0QgWyosBDa1ZDJV7wREiSG4Uf3LdikSWBpt3LPJJpEgd moI0WehV1RUARu8IQjSnqHbxPAg8lxgxSf8rm8OV0UeuZSmj/qGcJA5M62Jrm4Xi+afw Qedi37QSfLty+a4elrO7pRBTgNaHmGZAwyEbvrU6cij/f67fZ4wPwMkWWJSn5w5pwffG 90gsdyOu4+LbL83t5ipiP+pjwbZ3UwLFn2RxxDPcYRUyu8l6XX5waa1uk2C/vxUPW/2r dP9mg6LqIKHEgSaP3B9uop6Vn9rLxXmgLFYGF7KSR7to5q5FIUpGmC6Dj3ZppQ4iyrUy uAAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781016028; x=1781620828; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=9nUZrv+uXShy5oUjVbWJnHpZlIUPoN1ZejbvjYmlEBM=; b=enKi7jELw365Wth4PFNrcUAEPniGM32dmVxa+dbydHb/orQhRBpqggm5wpjuIqMlJ4 34Y622bKWCxh8lc+2XbGfO6AIFFdJPWBDkhgdzgFGt873z8SCYTo1hb4ATZsz+89CuQB C65ApVCLQDTbav13sIIEA6I9z/rFCJwOJUmI24K5vlABAtuXKto5moiO0KTrXTlB3X63 9o2h5RwnkFdb2PP6XeQ60yKcwYarD2t0A7UfRWHCnHbVKfewVUQ18auEkSj1EQt4zPbr qsWzX8tYRvHGsVcnz8zGe07vFmiF5rBHN+YemyPJ8Sw0hOjhZUkDyihdy5uqtG2FIdMG AjHA== X-Forwarded-Encrypted: i=1; AFNElJ+T2B3KR0Fn0XK2DaMVGHHnS6B1A16xJR4QrPGgKWAn6JgzQs/UXAtjZQ0Tap035vCG6Zs7eU/1IyJOtDc=@vger.kernel.org X-Gm-Message-State: AOJu0YzQhQHVV2M+6lvHFTkI2jLk30jXKNQ1dUB/E4ADzCP6LUI9CoND cE+tImlicQj7OvkdduD2qd8IQIB3ZkjzKvUNWiWVh2FBkkxQkrJrSzIqboN9tIAqj9U= X-Gm-Gg: Acq92OFXCLFWpTBbob2C8QvThQvMEu9+SHIQgRpf5QUTpfQwpA71NZPjrb0BQ/pegX3 WfDAW50U/3uFLzhDpMVfe9wDRLcDj68IChbp6uJPiabbOuaAawTfZYeZvHJXKzaXWwPlnrMscvj eNXhY2j8WksJSe4XSihJNJEb/LB0VtU+/2JZqfRzzilkKyzqeoD6Rx9oIes7k5G3IIkYwEwtdaN ILxiPK+aoezQQ/JDZhV4u9mWNgCbxTz+9xT/TCAthLMryX0LxvaOu5yYo21fFoG9l1H75B8ydVE MmzSHu9KeyAYJRpOclLrAg5P7UBrmQvKsVTruiKadp+534Q7NHn4RRl9gmfDL6wnI1GizL0ZTgz ZsB8FeREvLjFzkfUT4+Idrz3F7HVgE5y/LVYOlSILP8IQwY5E+Tb3LKmN5xGVquw1fRHxIW+7HW GLNGML4D6XRng2JyvpzYygGepG5WY2B4qWsMXPjPkUzz4= X-Received: by 2002:a05:600c:35d4:b0:490:b8d3:5dcc with SMTP id 5b1f17b1804b1-490c25e4634mr350034245e9.19.1781016027564; Tue, 09 Jun 2026 07:40:27 -0700 (PDT) Received: from ho-tower-lan.lan ([185.48.77.170]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-490bc3c1149sm428411935e9.4.2026.06.09.07.40.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jun 2026 07:40:26 -0700 (PDT) From: James Clark Date: Tue, 09 Jun 2026 15:40:08 +0100 Subject: [PATCH v5 03/19] perf test: Add a workload that forces context switches Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260609-james-cs-context-tracking-fix-v5-3-d53a7d096a19@linaro.org> References: <20260609-james-cs-context-tracking-fix-v5-0-d53a7d096a19@linaro.org> In-Reply-To: <20260609-james-cs-context-tracking-fix-v5-0-d53a7d096a19@linaro.org> To: Suzuki K Poulose , Mike Leach , Leo Yan , Arnaldo Carvalho de Melo , Namhyung Kim , Jiri Olsa , Ian Rogers , Amir Ayupov , Jonathan Corbet , Shuah Khan , Paschalis Mpeis Cc: coresight@lists.linaro.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Arnaldo Carvalho de Melo , linux-doc@vger.kernel.org, James Clark X-Mailer: b4 0.14.0 This workload launches two processes that block when reading and writing to each other forcing the other process to be scheduled for each read/write pair. Signed-off-by: James Clark --- tools/perf/Documentation/perf-test.txt | 7 +- tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/tests.h | 1 + tools/perf/tests/workloads/Build | 1 + tools/perf/tests/workloads/context_switch_loop.c | 110 +++++++++++++++++++++++ 5 files changed, 117 insertions(+), 3 deletions(-) diff --git a/tools/perf/Documentation/perf-test.txt b/tools/perf/Documentation/perf-test.txt index 2f4a91f5b9dc..213eb62603eb 100644 --- a/tools/perf/Documentation/perf-test.txt +++ b/tools/perf/Documentation/perf-test.txt @@ -55,15 +55,16 @@ OPTIONS -w:: --workload=:: - Run a built-in workload, to list them use '--list-workloads', current ones include: - noploop, thloop, leafloop, sqrtloop, brstack, datasym and landlock. + Run a built-in workload, to list them use '--list-workloads', current + ones include: noploop, thloop, leafloop, sqrtloop, brstack, datasym, + context_switch_loop and landlock. Used with the shell script regression tests. Some accept an extra parameter: seconds: leafloop, noploop, sqrtloop, thloop - nrloops: brstack + nrloops: brstack, context_switch_loop The datasym and landlock workloads don't accept any. diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 86ea427eb0aa..9284f897de3c 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -163,6 +163,7 @@ static struct test_workload *workloads[] = { &workload__traploop, &workload__inlineloop, &workload__jitdump, + &workload__context_switch_loop, #ifdef HAVE_RUST_SUPPORT &workload__code_with_type, diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index bf8ff7d54727..7cd4da4e96d3 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -245,6 +245,7 @@ DECLARE_WORKLOAD(landlock); DECLARE_WORKLOAD(traploop); DECLARE_WORKLOAD(inlineloop); DECLARE_WORKLOAD(jitdump); +DECLARE_WORKLOAD(context_switch_loop); #ifdef HAVE_RUST_SUPPORT DECLARE_WORKLOAD(code_with_type); diff --git a/tools/perf/tests/workloads/Build b/tools/perf/tests/workloads/Build index 0eb6d99528eb..7134a031cb7c 100644 --- a/tools/perf/tests/workloads/Build +++ b/tools/perf/tests/workloads/Build @@ -10,6 +10,7 @@ perf-test-y += landlock.o perf-test-y += traploop.o perf-test-y += inlineloop.o perf-test-y += jitdump.o +perf-test-y += context_switch_loop.o ifeq ($(CONFIG_RUST_SUPPORT),y) perf-test-y += code_with_type.o diff --git a/tools/perf/tests/workloads/context_switch_loop.c b/tools/perf/tests/workloads/context_switch_loop.c new file mode 100644 index 000000000000..5431af6147e6 --- /dev/null +++ b/tools/perf/tests/workloads/context_switch_loop.c @@ -0,0 +1,110 @@ + +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include + +#include "../tests.h" + +static int loops = 100; +static char buf; +int context_switch_loop_work = 1234; + +#define write_block(fd) \ + do { \ + if (write(fd, &buf, 1) <= 0) \ + return 1; \ + } while (0) + +#define read_block(fd) \ + do { \ + if (read(fd, &buf, 1) <= 0) \ + return 1; \ + } while (0) + +/* Not static to avoid LTO clobbering the function name */ +int context_switch_loop_proc1(int in_fd, int out_fd); +int context_switch_loop_proc1(int in_fd, int out_fd) +{ + for (int i = 0; i < loops; i++) { + read_block(in_fd); + context_switch_loop_work += i * 3; + write_block(out_fd); + } + return 0; +} + +int context_switch_loop_proc2(int in_fd, int out_fd); +int context_switch_loop_proc2(int in_fd, int out_fd) +{ + for (int i = 0; i < loops; i++) { + write_block(out_fd); + context_switch_loop_work += i * 7; + read_block(in_fd); + } + return 0; +} + +/* + * Launches two processes that take turns to execute a multiplication N times + */ +static int context_switch_loop(int argc, const char **argv) +{ + int a_to_b[2], b_to_a[2]; + pid_t proc1_pid; + int status; + int ret; + + if (argc > 0) { + loops = atoi(argv[0]); + if (loops < 0) { + fprintf(stderr, "Invalid number of loops: %s\n", argv[0]); + return 1; + } + } + + if (pipe(a_to_b) || pipe(b_to_a)) { + perror("Pipe error"); + return 1; + } + + proc1_pid = fork(); + if (proc1_pid < 0) { + perror("Fork error"); + return 1; + } + + if (!proc1_pid) { + close(a_to_b[0]); + close(b_to_a[1]); + prctl(PR_SET_NAME, "proc1", 0, 0, 0); + ret = context_switch_loop_proc1(b_to_a[0], a_to_b[1]); + close(a_to_b[1]); + close(b_to_a[0]); + exit(ret); + } + + close(a_to_b[1]); + close(b_to_a[0]); + prctl(PR_SET_NAME, "proc2", 0, 0, 0); + ret = context_switch_loop_proc2(a_to_b[0], b_to_a[1]); + close(a_to_b[0]); + close(b_to_a[1]); + + if (ret) { + kill(proc1_pid, SIGKILL); + return ret; + } + + if (waitpid(proc1_pid, &status, 0) != proc1_pid || !WIFEXITED(status) || + WEXITSTATUS(status)) + return 1; + + return 0; +} + +DEFINE_WORKLOAD(context_switch_loop); -- 2.34.1