Skip to contentSkip to conversation

perf bench sched pipe: add -p/--nprocs to run more than 2 workers

PATCH New Dirk Gouders 1 participant 7 messages Apr 2, 2025 – Apr 5, 2025 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 perf bench sched pipe: add -p/--nprocs to run more than 2 workers

Partly, sched-pipe.c looks as if it was designed to run more than two workers, but that possibility is still missing.

Introduce a new option -p/--nprocs to specify the number of processes/threads to run the worker function.
The worker function remains to simulate a ring structure, analogous to lmbench.

---
 tools/perf/Documentation/perf-bench.txt |  13 ++
 tools/perf/bench/sched-pipe.c           | 152 ++++++++++++++++--------
 2 files changed, 113 insertions(+), 52 deletions(-)
tools/perf/Documentation/perf-bench.txt +13 -0
index 8331bd28b10e..5c8dc99e1c57 100644
@@ -118,6 +118,16 @@ options (20 sender and receiver processes per group)
 Suite for pipe() system call.
 Based on pipe-test-1m.c by Ingo Molnar.
 
+Create a number of processes (default is 2) and a pipe for each of
+them.  Then, send tokens using the pipe ends for a specified number of
+loops (default 1,000,000).
+
+By default, theses Processes send tokens of length 4 (an int) by
+simulating a ring structure which means each process has two peers.
+It sends the tokens to one of its peers and receives them from its other
+peer (in case of 2 processes those peers are identical and just the other
+process).
+
 Options of *pipe*
 ^^^^^^^^^^^^^^^^^
 -l::
@@ -131,6 +141,9 @@ This is useful to check cgroup context switching overhead.
 Note that perf doesn't create nor delete the cgroups, so users should
 make sure that the cgroups exist and are accessible before use.
 
+-p::
+--nprocs=::
+Number of processes to use for sending tokens along the pipes.
 
 Example of *pipe*
 ^^^^^^^^^^^^^^^^^
tools/perf/bench/sched-pipe.c +100 -52

235 lines not on this page · raw message

Signature · 1 line
2.45.3
#1 lore raw

PATCH v2 2/3 perf bench sched pipe: add complete graph simulation

Currently, we have only one worker function: the simulation of a ring for token traversal.

Add another worker to simulate a complete graph (Kn) for token traversal. A new option -K/--Kn can be used to use the new worker.

Different workers could be interesting, because they seem to realize workload varieties noticeable by perf-report(1), for example:

(booted with mitigations=off, 6 processes)

Ring simulation:

Samples: 92K of event 'cycles:P', Event count (approx.): 18690208287

Overhead  Command     Shared Object         Symbol
  13.16%  sched-pipe  [kernel.kallsyms]     [k] timerqueue_add
   7.10%  sched-pipe  [kernel.kallsyms]     [k] read_hpet
   3.36%  sched-pipe  [kernel.kallsyms]     [k] _copy_from_iter
   3.23%  sched-pipe  [kernel.kallsyms]     [k] _copy_to_iter
   2.64%  sched-pipe  [kernel.kallsyms]     [k] vfs_write
   2.55%  sched-pipe  [kernel.kallsyms]     [k] vfs_read

Kn simulation:

Samples: 163K of event 'cycles:P', Event count (approx.): 100366721164

Overhead  Command     Shared Object         Symbol
   5.11%  sched-pipe  [kernel.kallsyms]     [k] _copy_from_iter
   4.90%  sched-pipe  [kernel.kallsyms]     [k] queued_spin_lock_slowpath
   3.99%  sched-pipe  [kernel.kallsyms]     [k] _copy_to_iter
   3.35%  sched-pipe  [kernel.kallsyms]     [k] timerqueue_add
   2.80%  sched-pipe  [kernel.kallsyms]     [k] check_preemption_disabled
   2.56%  sched-pipe  [kernel.kallsyms]     [k] vfs_write
   2.40%  sched-pipe  [kernel.kallsyms]     [k] vfs_read
---
 tools/perf/Documentation/perf-bench.txt |  5 ++
 tools/perf/bench/sched-pipe.c           | 62 +++++++++++++++++++++++--
 2 files changed, 63 insertions(+), 4 deletions(-)
tools/perf/Documentation/perf-bench.txt +5 -0
index 5c8dc99e1c57..c1f479de9ded 100644
@@ -130,6 +130,11 @@ process).
 
 Options of *pipe*
 ^^^^^^^^^^^^^^^^^
+-K::
+--Kn::
+Simulate a complete graph instead of a ring for sending tokens.
+Each process sends and receives tokens to/from every other process.
+
 -l::
 --loop=::
 Specify number of loops.
tools/perf/bench/sched-pipe.c +58 -4

108 lines not on this page · raw message

Signature · 1 line
2.45.3
#2 lore raw

PATCH v2 3/3 perf bench sched pipe: introduce multipliers for number of processes

In reply to Dirk Gouders

Introduce multipliers to specify the number of processes to run:

K|K|m|M: multiply leading number by 2^10 or 2^20, respectively

p|P: multiply intermediate result by number of online processors

Examples:

-p 2K = 2048
-p 10p = 10 * number of online processors
-p 1kp = 1024 * number of online processors

---
 tools/perf/Documentation/perf-bench.txt | 14 ++++++
 tools/perf/bench/sched-pipe.c           | 32 ++++++++++++++
 tools/perf/util/string.c                | 58 +++++++++++++++++++++++++
 tools/perf/util/string2.h               |  1 +
 4 files changed, 105 insertions(+)
tools/perf/Documentation/perf-bench.txt +14 -0
index c1f479de9ded..ed74532ee28d 100644
@@ -149,6 +149,20 @@ make sure that the cgroups exist and are accessible before use.
 -p::
 --nprocs=::
 Number of processes to use for sending tokens along the pipes.
+This option accepts a number follwed by optional (case insensitive)
+multipliers in this order:
+
+- k, m
++
+Multipliers 1024 and 1048576 for the leading number.
+
+- p
++
+Multiplier replaced by the number of online processors.
+
+Example:
+
+        -p 1kP means: 1024 * number of online processors
 
 Example of *pipe*
 ^^^^^^^^^^^^^^^^^
tools/perf/bench/sched-pipe.c +32 -0
index 553e5ead254f..1540cf46290a 100644
@@ -47,6 +47,8 @@ struct thread_data {
 #define LOOPS_DEFAULT 1000000
 static	unsigned int		loops = LOOPS_DEFAULT;
 
+static const char *nproc_str;	/* String that specifies a number of processes. */
+
 /* Use processes by default: */
 static bool			threaded;
 static unsigned int		nr_threads = 2;
@@ -91,6 +93,8 @@ static int parse_two_cgroups(const struct option *opt __maybe_unused,
 
 static const struct option options[] = {
 	OPT_BOOLEAN('n', "nonblocking",	&nonblocking,	"Use non-blocking operations"),
+	OPT_STRING('p', "nprocs",	&nproc_str,	"2P",
+		   "Number of processes (2P := 2 * online processors)"),
 	OPT_UINTEGER('p', "nprocs",	&nr_threads,    "Number of processes"),
 	OPT_UINTEGER('l', "loop",	&loops,		"Specify number of loops"),
 	OPT_BOOLEAN('K', "Kn",		&Kn_mode,	"Send tokens in a complete graph instead of a ring."),
@@ -279,6 +283,31 @@ static void *worker_thread_ring(void *__tdata)
 /* Ring mode is the default. */
 void * (*worker_thread)(void *) = worker_thread_ring;
 
+/*
+ * Get number of processes from the given string,
+ * e.g. "1k" => 1024 or
+ *      "8p" => 8 * number of online processors.
+ */
+static unsigned int get_nprocs(const char *np_str)
+{
+	unsigned int np;
+
+	np = (unsigned int)perf_nptou(np_str);
+
+	if (np == -1U) {
+		fprintf(stderr, "Cannot parse number of processes/threads: %s\n",
+			nproc_str);
+		exit(1);
+	}
+
+	if (np < 2) {
+		fprintf(stderr, "Two processes are the minimum requirement.\n");
+		exit(1);
+	}
+
+	return np;
+}
+
 static int create_pipes(struct thread_data *tdata)
 {
 	int __maybe_unused flags = 0;
@@ -338,6 +367,9 @@ int bench_sched_pipe(int argc, const char **argv)
 
 	argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
 
+	if (nproc_str)
+		nr_threads = get_nprocs(nproc_str);
+
 	if (Kn_mode)
 		worker_thread = worker_thread_kn;
 
tools/perf/util/string.c +58 -0

70 lines not on this page · raw message

tools/perf/util/string2.h +1 -0
index 4c8bff47cfd3..bca2c1687924 100644
@@ -12,6 +12,7 @@ extern const char *graph_dotted_line;
 extern const char *dots;
 
 s64 perf_atoll(const char *str);
+u32 perf_nptou(const char *str);
 bool strglobmatch(const char *str, const char *pat);
 bool strglobmatch_nocase(const char *str, const char *pat);
 bool strlazymatch(const char *str, const char *pat);
Signature · 1 line
2.45.3
#3 lore raw

v2 cover perf bench sched pipe: more processes, additional worker, multipliers for -p

In reply to Dirk Gouders

I'm very sorry for the missing cover letter in v1; it was me giving git send-email one wrong answer.
That cover letter explained that I want to share some changes I was experimenting with, maybe something of that will be considered useful:

1) The possibility to run more than two processes (new option -p/--nprocs).
2) An additional worker function that sends tokens in a complete graph in contrast to the original worker that simulates a ring.
3) Multipliers for -p so that a machine-specific multiplier 'p' meaning "number of online processors" can easily be specified.

I'm sending the v2 with fixes for issues that I noticed myself, so that something more correct is out in case somebody finds the time to look at this...

Regards,

Dirk

Changes since v1:

* In v1, I completely missed to finish the handling of the new option -p together with cgroups (-G). I now kept the restriction of two cgroups and just put every process with an even index in the first one, the others in the second one.

* Don't explicitely cast u32 to unsigned int (a parallel discussion in another huge series was about something different -- not such a case).

* Don't put the start of the thread_data array inside it; all workers know their indexes and can thus access all other data.

* Merge create_pipes() and create_thread_data(). The result isn't that complicated, no need to split those.

Dirk Gouders (3):
perf bench sched pipe: add -p/--nprocs to run more than 2 workers perf bench sched pipe: add complete graph simulation
perf bench sched pipe: introduce multipliers for number of processes

 tools/perf/Documentation/perf-bench.txt |  46 ++++-
 tools/perf/bench/sched-pipe.c           | 244 ++++++++++++++++++------
 tools/perf/util/string.c                |  58 ++++++
 tools/perf/util/string2.h               |   1 +
 4 files changed, 286 insertions(+), 63 deletions(-)
#4 lore raw

PATCH v2 1/3 perf bench sched pipe: add -p/--nprocs to run more than 2 workers

Partly, sched-pipe.c looks as if it was designed to run more than two workers, but that possibility is still missing.

Introduce a new option -p/--nprocs to specify the number of processes/threads to run the worker function.
The worker function remains to simulate a ring structure, analogous to lmbench.

Working with cgroups (-G) still limits the number of cgroups to 2. The processes with an even index go into the first cgroup specified, the ones with an odd index go into the second one.

---
 tools/perf/Documentation/perf-bench.txt |  27 +++-
 tools/perf/bench/sched-pipe.c           | 156 +++++++++++++++---------
 2 files changed, 121 insertions(+), 62 deletions(-)
tools/perf/Documentation/perf-bench.txt +25 -2
index 8331bd28b10e..8a651f2fe3aa 100644
@@ -118,6 +118,16 @@ options (20 sender and receiver processes per group)
 Suite for pipe() system call.
 Based on pipe-test-1m.c by Ingo Molnar.
 
+Create a number of processes (default is 2) and a pipe for each of
+them.  Then, send tokens using the pipe ends for a specified number of
+loops (default 1,000,000).
+
+By default, theses Processes send tokens of length 4 (an int) by
+simulating a ring structure which means each process has two peers.
+It sends the tokens to one of its peers and receives them from its other
+peer (in case of 2 processes those peers are identical and just the other
+process).
+
 Options of *pipe*
 ^^^^^^^^^^^^^^^^^
 -l::
@@ -126,11 +136,24 @@ Specify number of loops.
 
 -G::
 --cgroups=::
-Names of cgroups for sender and receiver, separated by a comma.
+Names of two cgroups separated by a comma.
++
+The worker processes will be added to the cgroups in an alternating
+fashion: processes with an even index go into the first one, those
+with an odd index into the second one.
++
 This is useful to check cgroup context switching overhead.
 Note that perf doesn't create nor delete the cgroups, so users should
-make sure that the cgroups exist and are accessible before use.
+make sure that the cgroups exist and are accessible before use, e.g.
+by
++
+------
+% mkdir /sys/fs/cgroup/{AAA,BBB}
+------
 
+-p::
+--nprocs=::
+Number of processes to use for sending tokens along the pipes.
 
 Example of *pipe*
 ^^^^^^^^^^^^^^^^^
tools/perf/bench/sched-pipe.c +96 -60

264 lines not on this page · raw message

Signature · 1 line
2.45.3
#5 lore raw

PATCH v2 2/3 perf bench sched pipe: add complete graph simulation

In reply to Dirk Gouders

Currently, we have only one worker function: the simulation of a ring for token traversal.

Add another worker to simulate a complete graph (Kn) for token traversal. A new option -K/--Kn can be used to use the new worker.

Those different workers could be interesting, because they produce workload varieties noticeable by perf-report(1), for example:

(booted with mitigations=off, 6 processes)

Ring simulation:

Samples: 92K of event 'cycles:P', Event count (approx.): 18690208287

Overhead  Command     Shared Object         Symbol
  13.16%  sched-pipe  [kernel.kallsyms]     [k] timerqueue_add
   7.10%  sched-pipe  [kernel.kallsyms]     [k] read_hpet
   3.36%  sched-pipe  [kernel.kallsyms]     [k] _copy_from_iter
   3.23%  sched-pipe  [kernel.kallsyms]     [k] _copy_to_iter
   2.64%  sched-pipe  [kernel.kallsyms]     [k] vfs_write
   2.55%  sched-pipe  [kernel.kallsyms]     [k] vfs_read

Kn simulation:

Samples: 163K of event 'cycles:P', Event count (approx.): 100366721164

Overhead  Command     Shared Object         Symbol
   5.11%  sched-pipe  [kernel.kallsyms]     [k] _copy_from_iter
   4.90%  sched-pipe  [kernel.kallsyms]     [k] queued_spin_lock_slowpath
   3.99%  sched-pipe  [kernel.kallsyms]     [k] _copy_to_iter
   3.35%  sched-pipe  [kernel.kallsyms]     [k] timerqueue_add
   2.80%  sched-pipe  [kernel.kallsyms]     [k] check_preemption_disabled
   2.56%  sched-pipe  [kernel.kallsyms]     [k] vfs_write
   2.40%  sched-pipe  [kernel.kallsyms]     [k] vfs_read
---
 tools/perf/Documentation/perf-bench.txt |  5 +++
 tools/perf/bench/sched-pipe.c           | 60 +++++++++++++++++++++++--
 2 files changed, 62 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-bench.txt +5 -0
index 8a651f2fe3aa..6f7df3d47821 100644
@@ -130,6 +130,11 @@ process).
 
 Options of *pipe*
 ^^^^^^^^^^^^^^^^^
+-K::
+--Kn::
+Simulate a complete graph instead of a ring for sending tokens.
+Each process sends and receives tokens to/from every other process.
+
 -l::
 --loop=::
 Specify number of loops.
tools/perf/bench/sched-pipe.c +57 -3

100 lines not on this page · raw message

Signature · 1 line
2.45.3
#6 lore raw

PATCH v2 3/3 perf bench sched pipe: introduce multipliers for number of processes

In reply to Dirk Gouders

Introduce multipliers to specify the number of processes to run:

K|K|m|M: multiply leading number by 2^10 or 2^20, respectively

p|P: multiply intermediate result by number of online processors

Examples:

-p 2K = 2048
-p 10p = 10 * number of online processors
-p 1kp = 1024 * number of online processors

---
 tools/perf/Documentation/perf-bench.txt | 14 ++++++
 tools/perf/bench/sched-pipe.c           | 36 ++++++++++++++-
 tools/perf/util/string.c                | 58 +++++++++++++++++++++++++
 tools/perf/util/string2.h               |  1 +
 4 files changed, 107 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-bench.txt +14 -0
index 6f7df3d47821..a2a44d80ee26 100644
@@ -159,6 +159,20 @@ by
 -p::
 --nprocs=::
 Number of processes to use for sending tokens along the pipes.
+This option accepts a number follwed by optional (case insensitive)
+multipliers in this order:
+
+- k, m
++
+Multipliers 1024 and 1048576 for the leading number.
+
+- p
++
+Multiplier replaced by the number of online processors.
+
+Example:
+
+        -p 1kP means: 1024 * number of online processors
 
 Example of *pipe*
 ^^^^^^^^^^^^^^^^^
tools/perf/bench/sched-pipe.c +34 -2
index 3c76e8249a9b..a1fa7ad7ed67 100644
@@ -12,7 +12,8 @@
 #include <subcmd/parse-options.h>
 #include <api/fs/fs.h>
 #include "bench.h"
-#include "util/cgroup.h"
+#include <util/cgroup.h>
+#include <util/string2.h>
 
 #include <unistd.h>
 #include <stdio.h>
@@ -45,6 +46,8 @@ struct thread_data {
 #define LOOPS_DEFAULT 1000000
 static	unsigned int		loops = LOOPS_DEFAULT;
 
+static const char *nproc_str;	/* String that specifies a number of processes. */
+
 /* Use processes by default: */
 static bool			threaded;
 static unsigned int		nr_threads = 2;
@@ -90,7 +93,8 @@ static int parse_two_cgroups(const struct option *opt __maybe_unused,
 
 static const struct option options[] = {
 	OPT_BOOLEAN('n', "nonblocking",	&nonblocking,	"Use non-blocking operations"),
-	OPT_UINTEGER('p', "nprocs",	&nr_threads,    "Number of processes"),
+	OPT_STRING('p', "nprocs",	&nproc_str,	"2P",
+		   "Number of processes (2P := 2 * online processors)"),
 	OPT_UINTEGER('l', "loop",	&loops,		"Specify number of loops"),
 	OPT_BOOLEAN('K', "Kn",		&Kn_mode,	"Send tokens in a complete graph instead of a ring."),
 	OPT_BOOLEAN('T', "threaded",	&threaded,	"Specify threads/process based task setup"),
@@ -281,6 +285,31 @@ static void *worker_thread_ring(void *__tdata)
 /* Ring mode is the default. */
 void * (*worker_thread)(void *) = worker_thread_ring;
 
+/*
+ * Get number of processes from the given string,
+ * e.g. "1k" => 1024 or
+ *      "8p" => 8 * number of online processors.
+ */
+static unsigned int get_nprocs(const char *np_str)
+{
+	unsigned int np;
+
+	np = perf_nptou(np_str);
+
+	if (np == -1U) {
+		fprintf(stderr, "Cannot parse number of processes/threads: %s\n",
+			nproc_str);
+		exit(1);
+	}
+
+	if (np < 2) {
+		fprintf(stderr, "Two processes are the minimum requirement.\n");
+		exit(1);
+	}
+
+	return np;
+}
+
 static struct thread_data *create_thread_data(void)
 {
 	struct thread_data *threads;
@@ -329,6 +358,9 @@ int bench_sched_pipe(int argc, const char **argv)
 
 	argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
 
+	if (nproc_str)
+		nr_threads = get_nprocs(nproc_str);
+
 	if (Kn_mode)
 		worker_thread = worker_thread_kn;
 
tools/perf/util/string.c +58 -0

70 lines not on this page · raw message

tools/perf/util/string2.h +1 -0
index 4c8bff47cfd3..bca2c1687924 100644
@@ -12,6 +12,7 @@ extern const char *graph_dotted_line;
 extern const char *dots;
 
 s64 perf_atoll(const char *str);
+u32 perf_nptou(const char *str);
 bool strglobmatch(const char *str, const char *pat);
 bool strglobmatch_nocase(const char *str, const char *pat);
 bool strlazymatch(const char *str, const char *pat);
Signature · 1 line
2.45.3