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