]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/darwintests/perf_spawn_fork.c
xnu-3789.41.3.tar.gz
[apple/xnu.git] / tools / tests / darwintests / perf_spawn_fork.c
1 #include <darwintest.h>
2 #include <spawn.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6
7 #define SPAWN_MEASURE_LOOP(s) \
8 char *args[] = {"/usr/bin/true", NULL}; \
9 int err; \
10 pid_t pid; \
11 int status; \
12 while (!dt_stat_stable(s)) { \
13 T_STAT_MEASURE(s) { \
14 err = posix_spawn(&pid, args[0], NULL, NULL, args, NULL); \
15 } \
16 if (err) { \
17 T_FAIL("posix_spawn returned %d", err); \
18 } \
19 waitpid(pid, &status, 0); \
20 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { \
21 T_FAIL("Child process of posix_spawn failed to run"); \
22 } \
23 }
24
25 T_DECL(posix_spawn_platform_binary_latency, "posix_spawn platform binary latency", T_META_TYPE_PERF, T_META_CHECK_LEAKS(NO)) {
26 {
27 dt_stat_time_t s = dt_stat_time_create("time");
28 SPAWN_MEASURE_LOOP(s);
29 dt_stat_finalize(s);
30 }
31
32 {
33 dt_stat_thread_cpu_time_t s = dt_stat_thread_cpu_time_create("on-cpu time");
34 SPAWN_MEASURE_LOOP(s);
35 dt_stat_finalize(s);
36 }
37 }
38
39 #define FORK_MEASURE_LOOP(s) \
40 pid_t pid; \
41 int status; \
42 while (!dt_stat_stable(s)) { \
43 T_STAT_MEASURE(s) { \
44 pid = fork(); \
45 if (pid == 0) \
46 exit(0); \
47 else if (pid == -1) \
48 T_FAIL("fork returned -1"); \
49 } \
50 waitpid(pid, &status, 0); \
51 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { \
52 T_FAIL("forked process failed to exit properly"); \
53 } \
54 }
55
56 T_DECL(fork, "fork latency", T_META_TYPE_PERF, T_META_CHECK_LEAKS(NO)) {
57 {
58 dt_stat_time_t s = dt_stat_time_create("time");
59 FORK_MEASURE_LOOP(s);
60 dt_stat_finalize(s);
61 }
62 {
63 dt_stat_thread_cpu_time_t s = dt_stat_thread_cpu_time_create("on-cpu time");
64 FORK_MEASURE_LOOP(s);
65 dt_stat_finalize(s);
66 }
67 }