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