]>
Commit | Line | Data |
---|---|---|
d9a64523 A |
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( | |
0a7de745 A |
16 | T_META_NAMESPACE("xnu.stackshot"), |
17 | T_META_CHECK_LEAKS(false), | |
18 | T_META_ASROOT(true) | |
19 | ); | |
d9a64523 | 20 | |
cb323159 | 21 | #define TEST_DURATION_NS (60 * NSEC_PER_SEC) |
d9a64523 A |
22 | |
23 | #define REAP_INTERVAL 10 | |
24 | ||
0a7de745 A |
25 | static void* |
26 | loop(__attribute__ ((unused)) void *arg) | |
27 | { | |
d9a64523 A |
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 | ||
0a7de745 A |
37 | while (1) { |
38 | ; | |
39 | } | |
d9a64523 A |
40 | } |
41 | ||
42 | static void | |
43 | take_stackshot(void) | |
44 | { | |
f427ee49 | 45 | uint64_t stackshot_flags = (STACKSHOT_SAVE_LOADINFO | STACKSHOT_GET_GLOBAL_MEM_STATS | |
0a7de745 | 46 | STACKSHOT_SAVE_IMP_DONATION_PIDS | STACKSHOT_KCDATA_FORMAT); |
d9a64523 A |
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, | |
0a7de745 | 65 | "called stackshot_capture_with_config (no retries remaining)"); |
d9a64523 A |
66 | } |
67 | } else { | |
0a7de745 | 68 | T_QUIET; T_ASSERT_POSIX_ZERO(ret, "called stackshot_capture_with_config"); |
d9a64523 A |
69 | } |
70 | ||
71 | ret = stackshot_config_dealloc(config); | |
72 | T_QUIET; T_EXPECT_POSIX_ZERO(ret, "deallocated stackshot config"); | |
73 | } | |
74 | ||
cb323159 | 75 | T_DECL(stackshot_spawn_exit, "tests taking many stackshots while children processes are spawning+exiting", T_META_TIMEOUT(120)) |
d9a64523 A |
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 | ||
cb323159 A |
82 | uint64_t stop_time = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) + TEST_DURATION_NS; |
83 | ||
d9a64523 A |
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) { | |
0a7de745 A |
89 | take_stackshot(); |
90 | num_stackshots++; | |
91 | if ((num_stackshots % 100) == 0) { | |
92 | T_LOG("completed %d stackshots", num_stackshots); | |
d9a64523 A |
93 | } |
94 | ||
0a7de745 A |
95 | // Sleep between each stackshot |
96 | usleep(100); | |
d9a64523 A |
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"); | |
0a7de745 A |
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"); | |
d9a64523 A |
105 | |
106 | int children_unreaped = 0, status; | |
cb323159 A |
107 | uint64_t iterations_completed = 0; |
108 | while (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) < stop_time) { | |
d9a64523 A |
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 | ||
cb323159 A |
123 | if ((iterations_completed % 100) == 0) { |
124 | T_LOG("spawned %llu children thus far", iterations_completed); | |
d9a64523 | 125 | } |
cb323159 | 126 | iterations_completed++; |
d9a64523 A |
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 | } |