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