xref: /xnu-8796.141.3/tools/tests/execperf/run.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <stdlib.h>
2*1b191cb5SApple OSS Distributions #include <stdio.h>
3*1b191cb5SApple OSS Distributions #include <unistd.h>
4*1b191cb5SApple OSS Distributions #include <stdint.h>
5*1b191cb5SApple OSS Distributions #include <errno.h>
6*1b191cb5SApple OSS Distributions #include <err.h>
7*1b191cb5SApple OSS Distributions #include <pthread.h>
8*1b191cb5SApple OSS Distributions #include <spawn.h>
9*1b191cb5SApple OSS Distributions 
10*1b191cb5SApple OSS Distributions extern char **environ;
11*1b191cb5SApple OSS Distributions 
12*1b191cb5SApple OSS Distributions char * const *newargv;
13*1b191cb5SApple OSS Distributions 
14*1b191cb5SApple OSS Distributions void usage(void);
15*1b191cb5SApple OSS Distributions 
16*1b191cb5SApple OSS Distributions void *work(void *);
17*1b191cb5SApple OSS Distributions 
18*1b191cb5SApple OSS Distributions int
main(int argc,char * argv[])19*1b191cb5SApple OSS Distributions main(int argc, char *argv[])
20*1b191cb5SApple OSS Distributions {
21*1b191cb5SApple OSS Distributions 	int i, count, threadcount;
22*1b191cb5SApple OSS Distributions 	int ret;
23*1b191cb5SApple OSS Distributions 	pthread_t *threads;
24*1b191cb5SApple OSS Distributions 
25*1b191cb5SApple OSS Distributions 	if (argc < 4) {
26*1b191cb5SApple OSS Distributions 		usage();
27*1b191cb5SApple OSS Distributions 	}
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions 	threadcount = atoi(argv[1]);
30*1b191cb5SApple OSS Distributions 	count = atoi(argv[2]);
31*1b191cb5SApple OSS Distributions 
32*1b191cb5SApple OSS Distributions 	newargv = &argv[3];
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions 	threads = (pthread_t *)calloc(threadcount, sizeof(pthread_t));
35*1b191cb5SApple OSS Distributions 	for (i = 0; i < threadcount; i++) {
36*1b191cb5SApple OSS Distributions 		ret = pthread_create(&threads[i], NULL, work, (void *)(intptr_t)count);
37*1b191cb5SApple OSS Distributions 		if (ret) {
38*1b191cb5SApple OSS Distributions 			err(1, "pthread_create");
39*1b191cb5SApple OSS Distributions 		}
40*1b191cb5SApple OSS Distributions 	}
41*1b191cb5SApple OSS Distributions 
42*1b191cb5SApple OSS Distributions 	for (i = 0; i < threadcount; i++) {
43*1b191cb5SApple OSS Distributions 		ret = pthread_join(threads[i], NULL);
44*1b191cb5SApple OSS Distributions 		if (ret) {
45*1b191cb5SApple OSS Distributions 			err(1, "pthread_join");
46*1b191cb5SApple OSS Distributions 		}
47*1b191cb5SApple OSS Distributions 	}
48*1b191cb5SApple OSS Distributions 
49*1b191cb5SApple OSS Distributions 	return 0;
50*1b191cb5SApple OSS Distributions }
51*1b191cb5SApple OSS Distributions 
52*1b191cb5SApple OSS Distributions void
usage(void)53*1b191cb5SApple OSS Distributions usage(void)
54*1b191cb5SApple OSS Distributions {
55*1b191cb5SApple OSS Distributions 	fprintf(stderr, "Usage: %s <threadcount> <count> <program> [<arg1> [<arg2> ...]]\n",
56*1b191cb5SApple OSS Distributions 	    getprogname());
57*1b191cb5SApple OSS Distributions 	exit(1);
58*1b191cb5SApple OSS Distributions }
59*1b191cb5SApple OSS Distributions 
60*1b191cb5SApple OSS Distributions void *
work(void * arg)61*1b191cb5SApple OSS Distributions work(void *arg)
62*1b191cb5SApple OSS Distributions {
63*1b191cb5SApple OSS Distributions 	int count = (int)(intptr_t)arg;
64*1b191cb5SApple OSS Distributions 	int i;
65*1b191cb5SApple OSS Distributions 	int ret;
66*1b191cb5SApple OSS Distributions 	pid_t pid;
67*1b191cb5SApple OSS Distributions 
68*1b191cb5SApple OSS Distributions 	for (i = 0; i < count; i++) {
69*1b191cb5SApple OSS Distributions 		ret = posix_spawn(&pid, newargv[0], NULL, NULL, newargv, environ);
70*1b191cb5SApple OSS Distributions 		if (ret != 0) {
71*1b191cb5SApple OSS Distributions 			errc(1, ret, "posix_spawn(%s)", newargv[0]);
72*1b191cb5SApple OSS Distributions 		}
73*1b191cb5SApple OSS Distributions 
74*1b191cb5SApple OSS Distributions 		while (-1 == waitpid(pid, &ret, 0)) {
75*1b191cb5SApple OSS Distributions 			if (errno != EINTR) {
76*1b191cb5SApple OSS Distributions 				err(1, "waitpid(%d)", pid);
77*1b191cb5SApple OSS Distributions 			}
78*1b191cb5SApple OSS Distributions 		}
79*1b191cb5SApple OSS Distributions 
80*1b191cb5SApple OSS Distributions 		if (WIFSIGNALED(ret)) {
81*1b191cb5SApple OSS Distributions 			errx(1, "process exited with signal %d", WTERMSIG(ret));
82*1b191cb5SApple OSS Distributions 		} else if (WIFSTOPPED(ret)) {
83*1b191cb5SApple OSS Distributions 			errx(1, "process stopped with signal %d", WSTOPSIG(ret));
84*1b191cb5SApple OSS Distributions 		} else if (WIFEXITED(ret)) {
85*1b191cb5SApple OSS Distributions 			if (WEXITSTATUS(ret) != 42) {
86*1b191cb5SApple OSS Distributions 				errx(1, "process exited with unexpected exit code %d", WEXITSTATUS(ret));
87*1b191cb5SApple OSS Distributions 			}
88*1b191cb5SApple OSS Distributions 		} else {
89*1b191cb5SApple OSS Distributions 			errx(1, "unknown exit condition %x", ret);
90*1b191cb5SApple OSS Distributions 		}
91*1b191cb5SApple OSS Distributions 	}
92*1b191cb5SApple OSS Distributions 
93*1b191cb5SApple OSS Distributions 	return NULL;
94*1b191cb5SApple OSS Distributions }
95