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