xref: /linux/tools/perf/tests/workloads/sqrtloop.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <math.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <linux/compiler.h>
7 #include <sys/wait.h>
8 #include "../tests.h"
9 
10 static volatile sig_atomic_t done;
11 
12 static void sighandler(int sig __maybe_unused)
13 {
14 	done = 1;
15 }
16 
17 static int __sqrtloop(int sec)
18 {
19 	signal(SIGALRM, sighandler);
20 	alarm(sec);
21 
22 	while (!done)
23 		(void)sqrt(rand());
24 	return 0;
25 }
26 
27 static int sqrtloop(int argc, const char **argv)
28 {
29 	int sec = 1;
30 
31 	if (argc > 0)
32 		sec = atoi(argv[0]);
33 
34 	switch (fork()) {
35 	case 0:
36 		return __sqrtloop(sec);
37 	case -1:
38 		return -1;
39 	default:
40 		wait(NULL);
41 	}
42 	return 0;
43 }
44 
45 DEFINE_WORKLOAD(sqrtloop);
46