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_RADAR_COMPONENT_NAME("xnu"),
18 T_META_RADAR_COMPONENT_VERSION("stackshot"),
19 T_META_OWNER("jonathan_w_adams"),
20 T_META_CHECK_LEAKS(false),
21 T_META_ASROOT(true)
22 );
23
24 #define TEST_DURATION_NS (60 * NSEC_PER_SEC)
25
26 #define REAP_INTERVAL 10
27
28 static void*
loop(void * arg)29 loop(__attribute__ ((unused)) void *arg)
30 {
31 exit(0);
32 }
33
34 T_HELPER_DECL(spawn_children_helper, "spawn_children helper")
35 {
36 pthread_t pthread;
37
38 T_QUIET; T_ASSERT_POSIX_ZERO(pthread_create(&pthread, NULL, loop, NULL), "pthread_create");
39
40 while (1) {
41 ;
42 }
43 }
44
45 static void
take_stackshot(void)46 take_stackshot(void)
47 {
48 uint64_t stackshot_flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS |
49 STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT);
50
51 void *config = stackshot_config_create();
52 T_QUIET; T_ASSERT_NOTNULL(config, "created stackshot config");
53
54 int ret = stackshot_config_set_flags(config, stackshot_flags);
55 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "set flags on stackshot config");
56
57 int retries_remaining = 5;
58
59 retry:
60 ret = stackshot_capture_with_config(config);
61
62 if (ret == EBUSY || ret == ETIMEDOUT) {
63 if (retries_remaining > 0) {
64 retries_remaining--;
65 goto retry;
66 } else {
67 T_QUIET; T_ASSERT_POSIX_ZERO(ret,
68 "called stackshot_capture_with_config (no retries remaining)");
69 }
70 } else {
71 T_QUIET; T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config");
72 }
73
74 ret = stackshot_config_dealloc(config);
75 T_QUIET; T_EXPECT_POSIX_ZERO(ret, "deallocated stackshot config");
76 }
77
78 T_DECL(stackshot_spawn_exit, "tests taking many stackshots while children processes are spawning+exiting", T_META_TIMEOUT(120), T_META_TAG_VM_PREFERRED)
79 {
80 char path[PATH_MAX];
81 uint32_t path_size = sizeof(path);
82 T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size), "_NSGetExecutablePath");
83 char *args[] = { path, "-n", "spawn_children_helper", NULL };
84
85 uint64_t stop_time = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) + TEST_DURATION_NS;
86
87 dispatch_queue_t stackshot_queue = dispatch_queue_create("stackshot_queue", NULL);
88 dispatch_async(stackshot_queue, ^(void) {
89 int num_stackshots = 0;
90
91 while (1) {
92 take_stackshot();
93 num_stackshots++;
94 if ((num_stackshots % 100) == 0) {
95 T_LOG("completed %d stackshots", num_stackshots);
96 }
97
98 // Sleep between each stackshot
99 usleep(100);
100 }
101 });
102
103 // <rdar://problem/39739547> META option for T_HELPER_DECL to not output test begin on start
104 posix_spawn_file_actions_t actions;
105 T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_spawn_file_actions_init(&actions), "create spawn actions");
106 T_QUIET; T_ASSERT_POSIX_SUCCESS(posix_spawn_file_actions_addopen(&actions, STDOUT_FILENO, "/dev/null", O_WRONLY, 0),
107 "set stdout of child to NULL");
108
109 int children_unreaped = 0, status;
110 uint64_t iterations_completed = 0;
111 while (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) < stop_time) {
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_completed % 100) == 0) {
127 T_LOG("spawned %llu children thus far", iterations_completed);
128 }
129 iterations_completed++;
130 }
131
132 while (children_unreaped) {
133 T_QUIET; T_ASSERT_POSIX_SUCCESS(waitpid(-1, &status, 0), "waitpid returned child pid");
134 children_unreaped--;
135 }
136 }
137