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