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