xref: /xnu-10002.1.13/tests/stackshot_idle_25570396.m (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions/* This program tests that kThreadIdleWorker is being set properly, so
2*1031c584SApple OSS Distributions * that idle and active threads can be appropriately identified.
3*1031c584SApple OSS Distributions */
4*1031c584SApple OSS Distributions
5*1031c584SApple OSS Distributions#include <darwintest.h>
6*1031c584SApple OSS Distributions#include <dispatch/dispatch.h>
7*1031c584SApple OSS Distributions#include <kdd.h>
8*1031c584SApple OSS Distributions#include <kern/kcdata.h>
9*1031c584SApple OSS Distributions#include <kern/debug.h>
10*1031c584SApple OSS Distributions#include <mach/mach_init.h>
11*1031c584SApple OSS Distributions#include <mach/mach_traps.h>
12*1031c584SApple OSS Distributions#include <mach/semaphore.h>
13*1031c584SApple OSS Distributions#include <mach/task.h>
14*1031c584SApple OSS Distributions#include <pthread.h>
15*1031c584SApple OSS Distributions#include <sys/stackshot.h>
16*1031c584SApple OSS Distributions#include <stdlib.h>
17*1031c584SApple OSS Distributions#include <unistd.h>
18*1031c584SApple OSS Distributions
19*1031c584SApple OSS Distributions#include <Foundation/Foundation.h>
20*1031c584SApple OSS Distributions
21*1031c584SApple OSS Distributions#define NUMRETRIES  5  // number of times to retry a stackshot
22*1031c584SApple OSS Distributions#define NUMENQUEUES 16 // number of blocking jobs to enqueue
23*1031c584SApple OSS Distributions#define NUMTHREADS  (NUMENQUEUES + 2) // total number of threads (including numenqueues)
24*1031c584SApple OSS Distributions
25*1031c584SApple OSS DistributionsT_GLOBAL_META(
26*1031c584SApple OSS Distributions        T_META_NAMESPACE("xnu.stackshot"),
27*1031c584SApple OSS Distributions        T_META_RADAR_COMPONENT_NAME("xnu"),
28*1031c584SApple OSS Distributions        T_META_RADAR_COMPONENT_VERSION("stackshot"),
29*1031c584SApple OSS Distributions        T_META_OWNER("jonathan_w_adams"),
30*1031c584SApple OSS Distributions        T_META_ASROOT(true)
31*1031c584SApple OSS Distributions);
32*1031c584SApple OSS Distributions
33*1031c584SApple OSS Distributionsvolatile static int spin_threads = 1;
34*1031c584SApple OSS Distributions
35*1031c584SApple OSS Distributionsstatic void *
36*1031c584SApple OSS Distributionstake_stackshot(uint32_t extra_flags, uint64_t since_timestamp)
37*1031c584SApple OSS Distributions{
38*1031c584SApple OSS Distributions	void * stackshot;
39*1031c584SApple OSS Distributions	int ret, retries;
40*1031c584SApple OSS Distributions	uint32_t stackshot_flags = STACKSHOT_SAVE_LOADINFO |
41*1031c584SApple OSS Distributions					STACKSHOT_GET_GLOBAL_MEM_STATS |
42*1031c584SApple OSS Distributions					STACKSHOT_SAVE_IMP_DONATION_PIDS |
43*1031c584SApple OSS Distributions					STACKSHOT_KCDATA_FORMAT;
44*1031c584SApple OSS Distributions
45*1031c584SApple OSS Distributions	if (since_timestamp != 0)
46*1031c584SApple OSS Distributions		stackshot_flags |= STACKSHOT_COLLECT_DELTA_SNAPSHOT;
47*1031c584SApple OSS Distributions
48*1031c584SApple OSS Distributions	stackshot_flags |= extra_flags;
49*1031c584SApple OSS Distributions
50*1031c584SApple OSS Distributions	stackshot = stackshot_config_create();
51*1031c584SApple OSS Distributions	T_ASSERT_NOTNULL(stackshot, "Allocating stackshot config");
52*1031c584SApple OSS Distributions
53*1031c584SApple OSS Distributions	ret = stackshot_config_set_flags(stackshot, stackshot_flags);
54*1031c584SApple OSS Distributions	T_ASSERT_POSIX_ZERO(ret, "Setting flags on stackshot config");
55*1031c584SApple OSS Distributions
56*1031c584SApple OSS Distributions	ret = stackshot_config_set_pid(stackshot, getpid());
57*1031c584SApple OSS Distributions	T_ASSERT_POSIX_ZERO(ret, "Setting target pid on stackshot config");
58*1031c584SApple OSS Distributions
59*1031c584SApple OSS Distributions	if (since_timestamp != 0) {
60*1031c584SApple OSS Distributions		ret = stackshot_config_set_delta_timestamp(stackshot, since_timestamp);
61*1031c584SApple OSS Distributions		T_ASSERT_POSIX_ZERO(ret, "Setting prev snapshot time on stackshot config");
62*1031c584SApple OSS Distributions	}
63*1031c584SApple OSS Distributions
64*1031c584SApple OSS Distributions	for (retries = NUMRETRIES; retries > 0; retries--) {
65*1031c584SApple OSS Distributions		ret = stackshot_capture_with_config(stackshot);
66*1031c584SApple OSS Distributions		T_ASSERT_TRUE(ret == 0 || ret == EBUSY || ret == ETIMEDOUT, "Attempting to take stackshot (error %d)...", ret);
67*1031c584SApple OSS Distributions		if (retries == 0 && (ret == EBUSY || ret == ETIMEDOUT))
68*1031c584SApple OSS Distributions			T_ASSERT_FAIL("Failed to take stackshot after %d retries: %s", ret, strerror(ret));
69*1031c584SApple OSS Distributions		if (ret == 0)
70*1031c584SApple OSS Distributions			break;
71*1031c584SApple OSS Distributions	}
72*1031c584SApple OSS Distributions	return stackshot;
73*1031c584SApple OSS Distributions}
74*1031c584SApple OSS Distributions
75*1031c584SApple OSS Distributionsstatic uint64_t get_stackshot_timestamp(void * stackshot)
76*1031c584SApple OSS Distributions{
77*1031c584SApple OSS Distributions	kcdata_iter_t iter;
78*1031c584SApple OSS Distributions	void * buf;
79*1031c584SApple OSS Distributions	uint64_t default_time = 0;
80*1031c584SApple OSS Distributions	uint32_t t, buflen;
81*1031c584SApple OSS Distributions
82*1031c584SApple OSS Distributions	buf = stackshot_config_get_stackshot_buffer(stackshot);
83*1031c584SApple OSS Distributions	T_ASSERT_NOTNULL(buf, "Getting stackshot buffer");
84*1031c584SApple OSS Distributions	buflen = stackshot_config_get_stackshot_size(stackshot);
85*1031c584SApple OSS Distributions
86*1031c584SApple OSS Distributions	iter = kcdata_iter(buf, buflen);
87*1031c584SApple OSS Distributions	t    = kcdata_iter_type(iter);
88*1031c584SApple OSS Distributions
89*1031c584SApple OSS Distributions	T_ASSERT_TRUE(t == KCDATA_BUFFER_BEGIN_STACKSHOT || t == KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT,
90*1031c584SApple OSS Distributions		"Making sure stackshot data begins with \"begin\" flag");
91*1031c584SApple OSS Distributions	T_ASSERT_TRUE(kcdata_iter_valid(iter = kcdata_iter_find_type(iter, KCDATA_TYPE_MACH_ABSOLUTE_TIME)),
92*1031c584SApple OSS Distributions		"Getting stackshot timestamp");
93*1031c584SApple OSS Distributions	default_time = *(uint64_t *)kcdata_iter_payload(iter);
94*1031c584SApple OSS Distributions	return default_time;
95*1031c584SApple OSS Distributions}
96*1031c584SApple OSS Distributions
97*1031c584SApple OSS Distributionsstatic void
98*1031c584SApple OSS Distributionsget_thread_statuses(void * stackshot, int * num_idles, int * num_nonidles)
99*1031c584SApple OSS Distributions{
100*1031c584SApple OSS Distributions	void *buf;
101*1031c584SApple OSS Distributions	uint32_t t, buflen;
102*1031c584SApple OSS Distributions	uint64_t thread_snap_flags;
103*1031c584SApple OSS Distributions	NSError *error = nil;
104*1031c584SApple OSS Distributions	NSMutableDictionary *parsed_container, *parsed_threads;
105*1031c584SApple OSS Distributions
106*1031c584SApple OSS Distributions	*num_idles = 0;
107*1031c584SApple OSS Distributions	*num_nonidles = 0;
108*1031c584SApple OSS Distributions
109*1031c584SApple OSS Distributions	buf = stackshot_config_get_stackshot_buffer(stackshot);
110*1031c584SApple OSS Distributions	T_ASSERT_NOTNULL(buf, "Getting stackshot buffer");
111*1031c584SApple OSS Distributions	buflen = stackshot_config_get_stackshot_size(stackshot);
112*1031c584SApple OSS Distributions
113*1031c584SApple OSS Distributions	kcdata_iter_t iter = kcdata_iter(buf, buflen);
114*1031c584SApple OSS Distributions	T_ASSERT_TRUE(kcdata_iter_type(iter) == KCDATA_BUFFER_BEGIN_STACKSHOT ||
115*1031c584SApple OSS Distributions			kcdata_iter_type(iter) == KCDATA_BUFFER_BEGIN_DELTA_STACKSHOT,
116*1031c584SApple OSS Distributions			"Checking start of stackshot buffer");
117*1031c584SApple OSS Distributions
118*1031c584SApple OSS Distributions	iter = kcdata_iter_next(iter);
119*1031c584SApple OSS Distributions	KCDATA_ITER_FOREACH(iter)
120*1031c584SApple OSS Distributions	{
121*1031c584SApple OSS Distributions		t = kcdata_iter_type(iter);
122*1031c584SApple OSS Distributions
123*1031c584SApple OSS Distributions		if (t != KCDATA_TYPE_CONTAINER_BEGIN) {
124*1031c584SApple OSS Distributions			continue;
125*1031c584SApple OSS Distributions		}
126*1031c584SApple OSS Distributions
127*1031c584SApple OSS Distributions		if (kcdata_iter_container_type(iter) != STACKSHOT_KCCONTAINER_TASK) {
128*1031c584SApple OSS Distributions			continue;
129*1031c584SApple OSS Distributions		}
130*1031c584SApple OSS Distributions
131*1031c584SApple OSS Distributions		parsed_container = parseKCDataContainer(&iter, &error);
132*1031c584SApple OSS Distributions		T_ASSERT_TRUE(parsed_container && !error, "Parsing container");
133*1031c584SApple OSS Distributions
134*1031c584SApple OSS Distributions		parsed_threads = parsed_container[@"task_snapshots"][@"thread_snapshots"];
135*1031c584SApple OSS Distributions		for (id th_key in parsed_threads) {
136*1031c584SApple OSS Distributions			/* check to see that tid matches expected idle status */
137*1031c584SApple OSS Distributions			thread_snap_flags = [parsed_threads[th_key][@"thread_snapshot"][@"ths_ss_flags"] unsignedLongLongValue];
138*1031c584SApple OSS Distributions			(thread_snap_flags & kThreadIdleWorker) ? (*num_idles)++ : (*num_nonidles)++;
139*1031c584SApple OSS Distributions		}
140*1031c584SApple OSS Distributions		[parsed_container release];
141*1031c584SApple OSS Distributions	}
142*1031c584SApple OSS Distributions
143*1031c584SApple OSS Distributions}
144*1031c584SApple OSS Distributions
145*1031c584SApple OSS Distributions/* Dispatch NUMENQUEUES jobs to a concurrent queue that immediately wait on a
146*1031c584SApple OSS Distributions * shared semaphore. This should spin up plenty of threads! */
147*1031c584SApple OSS Distributionsstatic void
148*1031c584SApple OSS Distributionswarm_up_threadpool(dispatch_queue_t q)
149*1031c584SApple OSS Distributions{
150*1031c584SApple OSS Distributions	int i;
151*1031c584SApple OSS Distributions	dispatch_semaphore_t thread_wait = dispatch_semaphore_create(0);
152*1031c584SApple OSS Distributions	T_QUIET; T_ASSERT_NOTNULL(thread_wait, "Initializing work queue semaphore");
153*1031c584SApple OSS Distributions	dispatch_semaphore_t main_wait = dispatch_semaphore_create(0);
154*1031c584SApple OSS Distributions	T_QUIET; T_ASSERT_NOTNULL(main_wait, "Initializing main thread semaphore");
155*1031c584SApple OSS Distributions
156*1031c584SApple OSS Distributions	for (i = 0; i < NUMENQUEUES; i++) {
157*1031c584SApple OSS Distributions		dispatch_async(q, ^{
158*1031c584SApple OSS Distributions			dispatch_semaphore_wait(thread_wait, DISPATCH_TIME_FOREVER);
159*1031c584SApple OSS Distributions			dispatch_semaphore_signal(main_wait);
160*1031c584SApple OSS Distributions		});
161*1031c584SApple OSS Distributions	}
162*1031c584SApple OSS Distributions
163*1031c584SApple OSS Distributions	sleep(1); // give worker threads enough time to block
164*1031c584SApple OSS Distributions
165*1031c584SApple OSS Distributions	for (i = 0; i < NUMENQUEUES; i++) {
166*1031c584SApple OSS Distributions		dispatch_semaphore_signal(thread_wait);
167*1031c584SApple OSS Distributions		dispatch_semaphore_wait(main_wait, DISPATCH_TIME_FOREVER);
168*1031c584SApple OSS Distributions	}
169*1031c584SApple OSS Distributions
170*1031c584SApple OSS Distributions	dispatch_release(thread_wait);
171*1031c584SApple OSS Distributions	dispatch_release(main_wait);
172*1031c584SApple OSS Distributions
173*1031c584SApple OSS Distributions	// Give enough time for worker threads to go idle again
174*1031c584SApple OSS Distributions	sleep(1);
175*1031c584SApple OSS Distributions}
176*1031c584SApple OSS Distributions
177*1031c584SApple OSS Distributions/* Dispatch NUMENQUEUES jobs to a concurrent queue that spin in a tight loop.
178*1031c584SApple OSS Distributions * Isn't guaranteed to occupy every worker thread, but it's enough so
179*1031c584SApple OSS Distributions * that a thread will go from idle to nonidle.
180*1031c584SApple OSS Distributions */
181*1031c584SApple OSS Distributionsstatic void
182*1031c584SApple OSS Distributionsfill_threadpool_with_spinning(dispatch_queue_t q)
183*1031c584SApple OSS Distributions{
184*1031c584SApple OSS Distributions	int i;
185*1031c584SApple OSS Distributions	for (i = 0; i < NUMENQUEUES; i++) {
186*1031c584SApple OSS Distributions		dispatch_async(q, ^{
187*1031c584SApple OSS Distributions			while(spin_threads); // should now appear as non-idle in delta shot
188*1031c584SApple OSS Distributions		});
189*1031c584SApple OSS Distributions	}
190*1031c584SApple OSS Distributions	sleep(1); // wait for jobs to enqueue
191*1031c584SApple OSS Distributions}
192*1031c584SApple OSS Distributions
193*1031c584SApple OSS Distributions/* Take stackshot, count the number of idle and nonidle threads the stackshot records.
194*1031c584SApple OSS Distributions * Where this is called, there should be NUMENQUEUES idle threads (thanks to warm_up_threadpool)
195*1031c584SApple OSS Distributions * and 2 nonidle threads (the main thread, and the spinning pthread).
196*1031c584SApple OSS Distributions */
197*1031c584SApple OSS Distributionsstatic void
198*1031c584SApple OSS Distributionstake_and_verify_initial_stackshot(uint64_t * since_time)
199*1031c584SApple OSS Distributions{
200*1031c584SApple OSS Distributions	void *stackshot;
201*1031c584SApple OSS Distributions	int num_init_idle_threads, num_init_nonidle_threads;
202*1031c584SApple OSS Distributions
203*1031c584SApple OSS Distributions	stackshot = take_stackshot(0, 0);
204*1031c584SApple OSS Distributions	*since_time = get_stackshot_timestamp(stackshot);
205*1031c584SApple OSS Distributions	get_thread_statuses(stackshot, &num_init_idle_threads, &num_init_nonidle_threads);
206*1031c584SApple OSS Distributions
207*1031c584SApple OSS Distributions	T_EXPECT_EQ(num_init_idle_threads, NUMENQUEUES,
208*1031c584SApple OSS Distributions			"Idle count of %d should match expected value of %d...",
209*1031c584SApple OSS Distributions			num_init_idle_threads, NUMENQUEUES);
210*1031c584SApple OSS Distributions	T_EXPECT_EQ(num_init_nonidle_threads, NUMTHREADS - NUMENQUEUES,
211*1031c584SApple OSS Distributions			"Non-idle count of %d should match expected value of %d...",
212*1031c584SApple OSS Distributions			num_init_nonidle_threads, NUMTHREADS - NUMENQUEUES);
213*1031c584SApple OSS Distributions	stackshot_config_dealloc(stackshot);
214*1031c584SApple OSS Distributions}
215*1031c584SApple OSS Distributions
216*1031c584SApple OSS Distributions/* Take a stackshot and a delta stackshot, measuring what changed since the previous
217*1031c584SApple OSS Distributions * stackshot. Where this is called, the blocking jobs have been cleared from the work queue,
218*1031c584SApple OSS Distributions * and the work queue has NUMENQUEUES tight-spinning jobs on it. Make sure that
219*1031c584SApple OSS Distributions * no new idle threads appear in the delta, and make sure that the delta shot isn't
220*1031c584SApple OSS Distributions * ignoring the worker threads that have become active.
221*1031c584SApple OSS Distributions */
222*1031c584SApple OSS Distributionsstatic void
223*1031c584SApple OSS Distributionstake_and_verify_delta_stackshot(uint64_t since_time)
224*1031c584SApple OSS Distributions{
225*1031c584SApple OSS Distributions	void *stackshot;
226*1031c584SApple OSS Distributions	void *delta_stackshot;
227*1031c584SApple OSS Distributions
228*1031c584SApple OSS Distributions	int num_delta_idles, num_delta_nonidles, num_curr_idles, num_curr_nonidles;
229*1031c584SApple OSS Distributions
230*1031c584SApple OSS Distributions	stackshot = take_stackshot(0, 0);
231*1031c584SApple OSS Distributions	delta_stackshot = take_stackshot(0, since_time); /* Threads should appear in delta stackshot as non-idle */
232*1031c584SApple OSS Distributions
233*1031c584SApple OSS Distributions	get_thread_statuses(stackshot, &num_curr_idles, &num_curr_nonidles);
234*1031c584SApple OSS Distributions	get_thread_statuses(delta_stackshot, &num_delta_idles, &num_delta_nonidles);
235*1031c584SApple OSS Distributions
236*1031c584SApple OSS Distributions	T_EXPECT_EQ(num_delta_idles, 0, "Making sure there are no idles in delta shot");
237*1031c584SApple OSS Distributions	T_EXPECT_EQ(num_delta_nonidles + num_curr_idles, NUMTHREADS,
238*1031c584SApple OSS Distributions			"Making sure delta shot isn't ignoring newly active threads");
239*1031c584SApple OSS Distributions	stackshot_config_dealloc(stackshot);
240*1031c584SApple OSS Distributions	stackshot_config_dealloc(delta_stackshot);
241*1031c584SApple OSS Distributions}
242*1031c584SApple OSS Distributions
243*1031c584SApple OSS Distributionsstatic void *
244*1031c584SApple OSS Distributionsspinning_non_work_queue_thread(void * ignored)
245*1031c584SApple OSS Distributions{
246*1031c584SApple OSS Distributions	(void)ignored;
247*1031c584SApple OSS Distributions	while(spin_threads);
248*1031c584SApple OSS Distributions	return NULL;
249*1031c584SApple OSS Distributions}
250*1031c584SApple OSS Distributions
251*1031c584SApple OSS DistributionsT_DECL(stackshot_idle_25570396, "Tests that stackshot can properly recognize idle and non-idle threads")
252*1031c584SApple OSS Distributions{
253*1031c584SApple OSS Distributions	int ret;
254*1031c584SApple OSS Distributions	uint64_t initial_stackshot_time;
255*1031c584SApple OSS Distributions	pthread_t spinning_thread;
256*1031c584SApple OSS Distributions	dispatch_queue_t q;
257*1031c584SApple OSS Distributions
258*1031c584SApple OSS Distributions	ret = pthread_create(&spinning_thread, NULL, spinning_non_work_queue_thread, NULL);
259*1031c584SApple OSS Distributions	T_ASSERT_POSIX_ZERO(ret, "Spinning up non-work-queue thread");
260*1031c584SApple OSS Distributions
261*1031c584SApple OSS Distributions	q = dispatch_queue_create("com.apple.kernel.test.waiting_semaphores", DISPATCH_QUEUE_CONCURRENT);
262*1031c584SApple OSS Distributions
263*1031c584SApple OSS Distributions	warm_up_threadpool(q);
264*1031c584SApple OSS Distributions	take_and_verify_initial_stackshot(&initial_stackshot_time);
265*1031c584SApple OSS Distributions
266*1031c584SApple OSS Distributions	fill_threadpool_with_spinning(q);
267*1031c584SApple OSS Distributions	take_and_verify_delta_stackshot(initial_stackshot_time);
268*1031c584SApple OSS Distributions
269*1031c584SApple OSS Distributions	spin_threads = 0; /* pthread-made thread should now exit */
270*1031c584SApple OSS Distributions	ret = pthread_join(spinning_thread, NULL);
271*1031c584SApple OSS Distributions	T_ASSERT_POSIX_ZERO(ret, "Joining on non-work-queue thread");
272*1031c584SApple OSS Distributions}
273