xref: /linux/tools/perf/tests/workloads/thloop.c (revision a1eccc574f977bd21a4ec8ac54bd73a2756bd281)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <unistd.h>
6 #include <linux/compiler.h>
7 #include "../tests.h"
8 
9 static volatile sig_atomic_t done;
10 static volatile unsigned count;
11 
12 /* We want to check this symbol in perf report */
13 noinline void test_loop(void);
14 
15 static void sighandler(int sig __maybe_unused)
16 {
17 	done = 1;
18 }
19 
20 noinline void test_loop(void)
21 {
22 	while (!done)
23 		__atomic_fetch_add(&count, 1, __ATOMIC_RELAXED);
24 }
25 
26 static void *thfunc(void *arg)
27 {
28 	void (*loop_fn)(void) = arg;
29 
30 	loop_fn();
31 	return NULL;
32 }
33 
34 static int thloop(int argc, const char **argv)
35 {
36 	int sec = 1;
37 	pthread_t th;
38 
39 	if (argc > 0)
40 		sec = atoi(argv[0]);
41 
42 	signal(SIGINT, sighandler);
43 	signal(SIGALRM, sighandler);
44 	alarm(sec);
45 
46 	pthread_create(&th, NULL, thfunc, test_loop);
47 	test_loop();
48 	pthread_join(th, NULL);
49 
50 	return 0;
51 }
52 
53 DEFINE_WORKLOAD(thloop);
54