4 #include <darwintest.h>
5 #include <darwintest_utils.h>
7 #include <dispatch/dispatch.h>
8 #include <kern/debug.h>
10 #include <mach-o/dyld.h>
11 #include <sys/syscall.h>
12 #include <sys/stackshot.h>
16 T_META_NAMESPACE("xnu.stackshot"),
17 T_META_CHECK_LEAKS(false),
21 #define TEST_DURATION_NS (60 * NSEC_PER_SEC)
23 #define REAP_INTERVAL 10
26 loop(__attribute__ ((unused
)) void *arg
)
31 T_HELPER_DECL(spawn_children_helper
, "spawn_children helper")
35 T_QUIET
; T_ASSERT_POSIX_ZERO(pthread_create(&pthread
, NULL
, loop
, NULL
), "pthread_create");
45 uint32_t stackshot_flags
= (STACKSHOT_SAVE_LOADINFO
| STACKSHOT_GET_GLOBAL_MEM_STATS
|
46 STACKSHOT_SAVE_IMP_DONATION_PIDS
| STACKSHOT_KCDATA_FORMAT
);
48 void *config
= stackshot_config_create();
49 T_QUIET
; T_ASSERT_NOTNULL(config
, "created stackshot config");
51 int ret
= stackshot_config_set_flags(config
, stackshot_flags
);
52 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "set flags on stackshot config");
54 int retries_remaining
= 5;
57 ret
= stackshot_capture_with_config(config
);
59 if (ret
== EBUSY
|| ret
== ETIMEDOUT
) {
60 if (retries_remaining
> 0) {
64 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
,
65 "called stackshot_capture_with_config (no retries remaining)");
68 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "called stackshot_capture_with_config");
71 ret
= stackshot_config_dealloc(config
);
72 T_QUIET
; T_EXPECT_POSIX_ZERO(ret
, "deallocated stackshot config");
75 T_DECL(stackshot_spawn_exit
, "tests taking many stackshots while children processes are spawning+exiting", T_META_TIMEOUT(120))
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
};
82 uint64_t stop_time
= clock_gettime_nsec_np(CLOCK_UPTIME_RAW
) + TEST_DURATION_NS
;
84 dispatch_queue_t stackshot_queue
= dispatch_queue_create("stackshot_queue", NULL
);
85 dispatch_async(stackshot_queue
, ^(void) {
86 int num_stackshots
= 0;
91 if ((num_stackshots
% 100) == 0) {
92 T_LOG("completed %d stackshots", num_stackshots
);
95 // Sleep between each stackshot
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");
106 int children_unreaped
= 0, status
;
107 uint64_t iterations_completed
= 0;
108 while (clock_gettime_nsec_np(CLOCK_UPTIME_RAW
) < stop_time
) {
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
);
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");
123 if ((iterations_completed
% 100) == 0) {
124 T_LOG("spawned %llu children thus far", iterations_completed
);
126 iterations_completed
++;
129 while (children_unreaped
) {
130 T_QUIET
; T_ASSERT_POSIX_SUCCESS(waitpid(-1, &status
, 0), "waitpid returned child pid");