]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
perf test: Add named_threads workload
authorJames Clark <james.clark@linaro.org>
Tue, 9 Jun 2026 14:40:14 +0000 (15:40 +0100)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 10 Jun 2026 21:55:49 +0000 (18:55 -0300)
Add a workload that runs X threads that run a unique function named
"named_threads_thread[x]" which performs a multiplication in a loop for
Y loops. Each thread sets its name to "thread[x]".

This can be used to test that processor trace decoding handles
concurrent threads correctly and the correct symbols and thread names
are assigned to samples.

Signed-off-by: James Clark <james.clark@linaro.org>
Cc: Amir Ayupov <aaupov@meta.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Leo Yan <leo.yan@arm.com>
Cc: Mike Leach <mike.leach@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paschalis Mpeis <Paschalis.Mpeis@arm.com>
Cc: Shuah Khan <skhan@linuxfoundation.org>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/Documentation/perf-test.txt
tools/perf/tests/builtin-test.c
tools/perf/tests/tests.h
tools/perf/tests/workloads/Build
tools/perf/tests/workloads/named_threads.c [new file with mode: 0644]

index c50a4b2d2d291e39ec4f2bf7e1120e51f0debc94..81c8525f594680d814f80e6f88bcce8d867bb350 100644 (file)
@@ -57,7 +57,7 @@ OPTIONS
 --workload=::
        Run a built-in workload, to list them use '--list-workloads', current
        ones include: noploop, thloop, leafloop, sqrtloop, brstack, datasym,
-       context_switch_loop, deterministic and landlock.
+       context_switch_loop, deterministic, named_threads and landlock.
 
        Used with the shell script regression tests.
 
@@ -66,6 +66,9 @@ OPTIONS
                seconds: leafloop, noploop, sqrtloop, thloop
                nrloops: brstack, context_switch_loop
 
+       'named_threads' accepts the number of threads and the number of loops to
+       do in each thread.
+
        The datasym, landlock and deterministic workloads don't accept any.
 
 --list-workloads::
index ef7e3f52a383e5581f607884735700f5d69a277e..afc06cec49546d29d86b94840c7021c5bf5c88e3 100644 (file)
@@ -155,6 +155,7 @@ static struct test_suite *generic_tests[] = {
 static struct test_workload *workloads[] = {
        &workload__noploop,
        &workload__thloop,
+       &workload__named_threads,
        &workload__leafloop,
        &workload__sqrtloop,
        &workload__brstack,
index bcfe9c33fc66094a92c3d511ad9a5571630df7db..7cedf05be544ad79a99e86d30dfa4f7b01ca0837 100644 (file)
@@ -237,6 +237,7 @@ struct test_workload workload__##work = {   \
 /* The list of test workloads */
 DECLARE_WORKLOAD(noploop);
 DECLARE_WORKLOAD(thloop);
+DECLARE_WORKLOAD(named_threads);
 DECLARE_WORKLOAD(leafloop);
 DECLARE_WORKLOAD(sqrtloop);
 DECLARE_WORKLOAD(brstack);
index 90f2d8aa4941f822b20c23645c806856031f8ba1..75b377934a0e62b9ac1fec245520ea0978ac957e 100644 (file)
@@ -2,6 +2,7 @@
 
 perf-test-y += noploop.o
 perf-test-y += thloop.o
+perf-test-y += named_threads.o
 perf-test-y += leafloop.o
 perf-test-y += sqrtloop.o
 perf-test-y += brstack.o
diff --git a/tools/perf/tests/workloads/named_threads.c b/tools/perf/tests/workloads/named_threads.c
new file mode 100644 (file)
index 0000000..d051d41
--- /dev/null
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <limits.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <linux/compiler.h>
+#include "../tests.h"
+
+#define MAX_THREADS 25
+
+static int iterations = 500;
+int named_threads_work = 1234;
+
+typedef void *(*thread_fn_t)(void *);
+
+#define DEFINE_THREAD(n)                                               \
+noinline void *named_threads_thread##n(void *arg __maybe_unused)       \
+{                                                                      \
+       pthread_setname_np(pthread_self(), "thread" #n);                \
+       for (int i = 0; i < iterations; i++)                            \
+               named_threads_work += 3;                                \
+                                                                       \
+       return NULL;                                                    \
+}
+
+#define THREAD_LIST(macro)     \
+       macro(1)                \
+       macro(2)                \
+       macro(3)                \
+       macro(4)                \
+       macro(5)                \
+       macro(6)                \
+       macro(7)                \
+       macro(8)                \
+       macro(9)                \
+       macro(10)               \
+       macro(11)               \
+       macro(12)               \
+       macro(13)               \
+       macro(14)               \
+       macro(15)               \
+       macro(16)               \
+       macro(17)               \
+       macro(18)               \
+       macro(19)               \
+       macro(20)               \
+       macro(21)               \
+       macro(22)               \
+       macro(23)               \
+       macro(24)               \
+       macro(25)
+
+#define DECLARE_THREAD(n) void *named_threads_thread##n(void *arg);
+
+THREAD_LIST(DECLARE_THREAD)
+THREAD_LIST(DEFINE_THREAD)
+
+#define THREAD_ENTRY(n) named_threads_thread##n,
+
+static thread_fn_t thread_fns[MAX_THREADS] = {
+       THREAD_LIST(THREAD_ENTRY)
+};
+
+/*
+ * Creates argv[0] threads that run a unique function named "thread[x]" which performs
+ * a multiplication in a loop for argv[1] loops.
+ */
+static int named_threads(int argc, const char **argv)
+{
+       pthread_t threads[MAX_THREADS];
+       int nr_threads = 1;
+       int err = 0;
+
+       if (argc > 0)
+               nr_threads = atoi(argv[0]);
+
+       if (nr_threads <= 0 || nr_threads > MAX_THREADS) {
+               fprintf(stderr, "Error: num threads must be 1 - %d\n", MAX_THREADS);
+               return 1;
+       }
+
+       if (argc > 1)
+               iterations = atoi(argv[1]);
+
+       if (iterations < 0) {
+               fprintf(stderr, "Error: iterations must be non-negative\n");
+               return 1;
+       }
+
+       for (int i = 0; i < nr_threads; i++) {
+               int ret;
+
+               ret = pthread_create(&threads[i], NULL, thread_fns[i], NULL);
+               if (ret) {
+                       fprintf(stderr, "Error: failed to create thread%d: %s\n",
+                               i + 1, strerror(ret));
+                       return 1;
+               }
+       }
+
+       for (int i = 0; i < nr_threads; i++)
+               pthread_join(threads[i], NULL);
+
+       return err;
+}
+
+DEFINE_WORKLOAD(named_threads);