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),
22 #define SPAWN_ITERATIONS 1999
23 #elif TARGET_OS_IPHONE
24 #define SPAWN_ITERATIONS 4999
26 #define SPAWN_ITERATIONS 9999
29 #define REAP_INTERVAL 10
32 loop(__attribute__ ((unused
)) void *arg
)
37 T_HELPER_DECL(spawn_children_helper
, "spawn_children helper")
41 T_QUIET
; T_ASSERT_POSIX_ZERO(pthread_create(&pthread
, NULL
, loop
, NULL
), "pthread_create");
51 uint32_t stackshot_flags
= (STACKSHOT_SAVE_LOADINFO
| STACKSHOT_GET_GLOBAL_MEM_STATS
|
52 STACKSHOT_SAVE_IMP_DONATION_PIDS
| STACKSHOT_KCDATA_FORMAT
);
54 void *config
= stackshot_config_create();
55 T_QUIET
; T_ASSERT_NOTNULL(config
, "created stackshot config");
57 int ret
= stackshot_config_set_flags(config
, stackshot_flags
);
58 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "set flags on stackshot config");
60 int retries_remaining
= 5;
63 ret
= stackshot_capture_with_config(config
);
65 if (ret
== EBUSY
|| ret
== ETIMEDOUT
) {
66 if (retries_remaining
> 0) {
70 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
,
71 "called stackshot_capture_with_config (no retries remaining)");
74 T_QUIET
; T_ASSERT_POSIX_ZERO(ret
, "called stackshot_capture_with_config");
77 ret
= stackshot_config_dealloc(config
);
78 T_QUIET
; T_EXPECT_POSIX_ZERO(ret
, "deallocated stackshot config");
81 T_DECL(stackshot_spawn_exit
, "tests taking many stackshots while children processes are spawning+exiting")
84 uint32_t path_size
= sizeof(path
);
85 T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path
, &path_size
), "_NSGetExecutablePath");
86 char *args
[] = { path
, "-n", "spawn_children_helper", NULL
};
88 dispatch_queue_t stackshot_queue
= dispatch_queue_create("stackshot_queue", NULL
);
89 dispatch_async(stackshot_queue
, ^(void) {
90 int num_stackshots
= 0;
95 if ((num_stackshots
% 100) == 0) {
96 T_LOG("completed %d stackshots", num_stackshots
);
99 // Sleep between each stackshot
104 // <rdar://problem/39739547> META option for T_HELPER_DECL to not output test begin on start
105 posix_spawn_file_actions_t actions
;
106 T_QUIET
; T_ASSERT_POSIX_SUCCESS(posix_spawn_file_actions_init(&actions
), "create spawn actions");
107 T_QUIET
; T_ASSERT_POSIX_SUCCESS(posix_spawn_file_actions_addopen(&actions
, STDOUT_FILENO
, "/dev/null", O_WRONLY
, 0),
108 "set stdout of child to NULL");
110 int children_unreaped
= 0, status
;
111 for (int iterations_remaining
= SPAWN_ITERATIONS
; iterations_remaining
> 0; iterations_remaining
--) {
114 int sp_ret
= posix_spawn(&pid
, args
[0], &actions
, NULL
, args
, NULL
);
115 T_QUIET
; T_ASSERT_POSIX_ZERO(sp_ret
, "spawned process '%s' with PID %d", args
[0], pid
);
119 if (children_unreaped
>= REAP_INTERVAL
) {
120 while (children_unreaped
) {
121 T_QUIET
; T_ASSERT_POSIX_SUCCESS(waitpid(-1, &status
, 0), "waitpid returned child pid");
126 if ((iterations_remaining
% 100) == 0) {
127 T_LOG("spawned %d children thus far", (SPAWN_ITERATIONS
- iterations_remaining
));
131 while (children_unreaped
) {
132 T_QUIET
; T_ASSERT_POSIX_SUCCESS(waitpid(-1, &status
, 0), "waitpid returned child pid");