]> git.saurik.com Git - apple/xnu.git/blob - tests/stackshot_spawn_exit_stress.c
448095598fb567db7acc7b377fbe504e02260dc3
[apple/xnu.git] / tests / stackshot_spawn_exit_stress.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <darwintest.h>
5 #include <darwintest_utils.h>
6
7 #include <dispatch/dispatch.h>
8 #include <kern/debug.h>
9 #include <libproc.h>
10 #include <mach-o/dyld.h>
11 #include <sys/syscall.h>
12 #include <sys/stackshot.h>
13 #include <spawn.h>
14
15 T_GLOBAL_META(
16 T_META_NAMESPACE("xnu.stackshot"),
17 T_META_CHECK_LEAKS(false),
18 T_META_ASROOT(true)
19 );
20
21 #if TARGET_OS_WATCH
22 #define SPAWN_ITERATIONS 1999
23 #elif TARGET_OS_IPHONE
24 #define SPAWN_ITERATIONS 4999
25 #else
26 #define SPAWN_ITERATIONS 9999
27 #endif
28
29 #define REAP_INTERVAL 10
30
31 static void*
32 loop(__attribute__ ((unused)) void *arg)
33 {
34 exit(0);
35 }
36
37 T_HELPER_DECL(spawn_children_helper, "spawn_children helper")
38 {
39 pthread_t pthread;
40
41 T_QUIET; T_ASSERT_POSIX_ZERO(pthread_create(&pthread, NULL, loop, NULL), "pthread_create");
42
43 while (1) {
44 ;
45 }
46 }
47
48 static void
49 take_stackshot(void)
50 {
51 uint32_t stackshot_flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS |
52 STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT);
53
54 void *config = stackshot_config_create();
55 T_QUIET; T_ASSERT_NOTNULL(config, "created stackshot config");
56
57 int ret = stackshot_config_set_flags(config, stackshot_flags);
58 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "set flags on stackshot config");
59
60 int retries_remaining = 5;
61
62 retry:
63 ret = stackshot_capture_with_config(config);
64
65 if (ret == EBUSY || ret == ETIMEDOUT) {
66 if (retries_remaining > 0) {
67 retries_remaining--;
68 goto retry;
69 } else {
70 T_QUIET; T_ASSERT_POSIX_ZERO(ret,
71 "called stackshot_capture_with_config (no retries remaining)");
72 }
73 } else {
74 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config");
75 }
76
77 ret = stackshot_config_dealloc(config);
78 T_QUIET; T_EXPECT_POSIX_ZERO(ret, "deallocated stackshot config");
79 }
80
81 T_DECL(stackshot_spawn_exit, "tests taking many stackshots while children processes are spawning+exiting")
82 {
83 char path[PATH_MAX];
84 uint32_t path_size = sizeof(path);
85 T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
86 char *args[] = { path, "-n", "spawn_children_helper", NULL };
87
88 dispatch_queue_t stackshot_queue = dispatch_queue_create("stackshot_queue", NULL);
89 dispatch_async(stackshot_queue, ^(void) {
90 int num_stackshots = 0;
91
92 while (1) {
93 take_stackshot();
94 num_stackshots++;
95 if ((num_stackshots % 100) == 0) {
96 T_LOG("completed %d stackshots", num_stackshots);
97 }
98
99 // Sleep between each stackshot
100 usleep(100);
101 }
102 });
103
104 // <rdar://problem/39739547> META option for T_HELPER_DECL to not output test begin on start
105 posix_spawn_file_actions_t actions;
106 T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_spawn_file_actions_init(&actions), "create spawn actions");
107 T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_spawn_file_actions_addopen(&actions, STDOUT_FILENO, "/dev/null", O_WRONLY, 0),
108 "set stdout of child to NULL");
109
110 int children_unreaped = 0, status;
111 for (int iterations_remaining = SPAWN_ITERATIONS; iterations_remaining > 0; iterations_remaining--) {
112 pid_t pid;
113
114 int sp_ret = posix_spawn(&pid, args[0], &actions, NULL, args, NULL);
115 T_QUIET; T_ASSERT_POSIX_ZERO(sp_ret, "spawned process '%s' with PID %d", args[0], pid);
116
117 children_unreaped++;
118
119 if (children_unreaped >= REAP_INTERVAL) {
120 while (children_unreaped) {
121 T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(-1, &status, 0), "waitpid returned child pid");
122 children_unreaped--;
123 }
124 }
125
126 if ((iterations_remaining % 100) == 0) {
127 T_LOG("spawned %d children thus far", (SPAWN_ITERATIONS - iterations_remaining));
128 }
129 }
130
131 while (children_unreaped) {
132 T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(-1, &status, 0), "waitpid returned child pid");
133 children_unreaped--;
134 }
135 }