xref: /linux/tools/perf/tests/workloads/noploop.c (revision 473f6c8f437b049f8ec015d57cd59bb983b1d85c)
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 
11 static void sighandler(int sig __maybe_unused)
12 {
13 	done = 1;
14 }
15 
16 static int noploop(int argc, const char **argv)
17 {
18 	double sec = 1.0;
19 
20 	pthread_setname_np(pthread_self(), "perf-noploop");
21 	if (argc > 0)
22 		sec = atof(argv[0]);
23 
24 	if (!(sec > 0.0)) {
25 		fprintf(stderr, "Error: seconds (%f) must be > 0\n", sec);
26 		return 1;
27 	}
28 
29 	signal(SIGINT, sighandler);
30 	signal(SIGALRM, sighandler);
31 
32 	if (sec < 1.0) {
33 		useconds_t usecs = (useconds_t)(sec * 1000000.0);
34 
35 		ualarm(usecs > 0 ? usecs : 1, 0);
36 	} else
37 		alarm((unsigned int)sec);
38 
39 	while (!done)
40 		continue;
41 
42 	return 0;
43 }
44 
45 DEFINE_WORKLOAD(noploop);
46