xref: /xnu-8796.121.2/tests/microstackshot_tests.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /* Copyright (c) 2018-2021 Apple Inc.  All rights reserved. */
2*c54f35caSApple OSS Distributions 
3*c54f35caSApple OSS Distributions #include <CoreFoundation/CoreFoundation.h>
4*c54f35caSApple OSS Distributions #include <darwintest.h>
5*c54f35caSApple OSS Distributions #include <darwintest_utils.h>
6*c54f35caSApple OSS Distributions #include <dispatch/dispatch.h>
7*c54f35caSApple OSS Distributions #include <ktrace/ktrace.h>
8*c54f35caSApple OSS Distributions #include <kperf/kperf.h>
9*c54f35caSApple OSS Distributions #include <kern/debug.h>
10*c54f35caSApple OSS Distributions #include <notify.h>
11*c54f35caSApple OSS Distributions #include <sys/kdebug.h>
12*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
13*c54f35caSApple OSS Distributions #include <TargetConditionals.h>
14*c54f35caSApple OSS Distributions 
15*c54f35caSApple OSS Distributions #include "ktrace/ktrace_helpers.h"
16*c54f35caSApple OSS Distributions 
17*c54f35caSApple OSS Distributions enum telemetry_pmi {
18*c54f35caSApple OSS Distributions 	TELEMETRY_PMI_NONE,
19*c54f35caSApple OSS Distributions 	TELEMETRY_PMI_INSTRS,
20*c54f35caSApple OSS Distributions 	TELEMETRY_PMI_CYCLES,
21*c54f35caSApple OSS Distributions };
22*c54f35caSApple OSS Distributions #define TELEMETRY_CMD_PMI_SETUP 3
23*c54f35caSApple OSS Distributions 
24*c54f35caSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.stackshot.microstackshot"),
25*c54f35caSApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
26*c54f35caSApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("stackshot"),
27*c54f35caSApple OSS Distributions     T_META_OWNER("mwidmann"),
28*c54f35caSApple OSS Distributions     T_META_CHECK_LEAKS(false),
29*c54f35caSApple OSS Distributions     T_META_ASROOT(true));
30*c54f35caSApple OSS Distributions 
31*c54f35caSApple OSS Distributions extern int __telemetry(uint64_t cmd, uint64_t deadline, uint64_t interval,
32*c54f35caSApple OSS Distributions     uint64_t leeway, uint64_t arg4, uint64_t arg5);
33*c54f35caSApple OSS Distributions 
34*c54f35caSApple OSS Distributions /*
35*c54f35caSApple OSS Distributions  * Data Analytics (da) also has a microstackshot configuration -- set a PMI
36*c54f35caSApple OSS Distributions  * cycle interval of 0 to force it to disable microstackshot on PMI.
37*c54f35caSApple OSS Distributions  */
38*c54f35caSApple OSS Distributions 
39*c54f35caSApple OSS Distributions static void
set_da_microstackshot_period(CFNumberRef num)40*c54f35caSApple OSS Distributions set_da_microstackshot_period(CFNumberRef num)
41*c54f35caSApple OSS Distributions {
42*c54f35caSApple OSS Distributions 	CFPreferencesSetValue(CFSTR("microstackshotPMICycleInterval"), num,
43*c54f35caSApple OSS Distributions 	    CFSTR("com.apple.da"),
44*c54f35caSApple OSS Distributions #if TARGET_OS_IPHONE
45*c54f35caSApple OSS Distributions 	    CFSTR("mobile"),
46*c54f35caSApple OSS Distributions #else // TARGET_OS_IPHONE
47*c54f35caSApple OSS Distributions 	    CFSTR("root"),
48*c54f35caSApple OSS Distributions #endif // !TARGET_OS_IPHONE
49*c54f35caSApple OSS Distributions 	    kCFPreferencesCurrentHost);
50*c54f35caSApple OSS Distributions 
51*c54f35caSApple OSS Distributions 	notify_post("com.apple.da.tasking_changed");
52*c54f35caSApple OSS Distributions }
53*c54f35caSApple OSS Distributions 
54*c54f35caSApple OSS Distributions static void
disable_da_microstackshots(void)55*c54f35caSApple OSS Distributions disable_da_microstackshots(void)
56*c54f35caSApple OSS Distributions {
57*c54f35caSApple OSS Distributions 	int64_t zero = 0;
58*c54f35caSApple OSS Distributions 	CFNumberRef num = CFNumberCreate(NULL, kCFNumberSInt64Type, &zero);
59*c54f35caSApple OSS Distributions 	set_da_microstackshot_period(num);
60*c54f35caSApple OSS Distributions 	T_LOG("notified da of tasking change, sleeping");
61*c54f35caSApple OSS Distributions #if TARGET_OS_WATCH
62*c54f35caSApple OSS Distributions 	sleep(8);
63*c54f35caSApple OSS Distributions #else /* TARGET_OS_WATCH */
64*c54f35caSApple OSS Distributions 	sleep(3);
65*c54f35caSApple OSS Distributions #endif /* !TARGET_OS_WATCH */
66*c54f35caSApple OSS Distributions }
67*c54f35caSApple OSS Distributions 
68*c54f35caSApple OSS Distributions /*
69*c54f35caSApple OSS Distributions  * Unset the preference to allow da to reset its configuration.
70*c54f35caSApple OSS Distributions  */
71*c54f35caSApple OSS Distributions static void
reenable_da_microstackshots(void)72*c54f35caSApple OSS Distributions reenable_da_microstackshots(void)
73*c54f35caSApple OSS Distributions {
74*c54f35caSApple OSS Distributions 	set_da_microstackshot_period(NULL);
75*c54f35caSApple OSS Distributions }
76*c54f35caSApple OSS Distributions 
77*c54f35caSApple OSS Distributions /*
78*c54f35caSApple OSS Distributions  * Clean up the test's configuration and allow da to activate again.
79*c54f35caSApple OSS Distributions  */
80*c54f35caSApple OSS Distributions static void
telemetry_cleanup(void)81*c54f35caSApple OSS Distributions telemetry_cleanup(void)
82*c54f35caSApple OSS Distributions {
83*c54f35caSApple OSS Distributions 	(void)__telemetry(TELEMETRY_CMD_PMI_SETUP, TELEMETRY_PMI_NONE, 0, 0, 0, 0);
84*c54f35caSApple OSS Distributions 	reenable_da_microstackshots();
85*c54f35caSApple OSS Distributions }
86*c54f35caSApple OSS Distributions 
87*c54f35caSApple OSS Distributions /*
88*c54f35caSApple OSS Distributions  * Make sure da hasn't configured the microstackshots -- otherwise the PMI
89*c54f35caSApple OSS Distributions  * setup command will return EBUSY.
90*c54f35caSApple OSS Distributions  */
91*c54f35caSApple OSS Distributions static void
telemetry_init(void)92*c54f35caSApple OSS Distributions telemetry_init(void)
93*c54f35caSApple OSS Distributions {
94*c54f35caSApple OSS Distributions 	disable_da_microstackshots();
95*c54f35caSApple OSS Distributions 	T_LOG("installing cleanup handler");
96*c54f35caSApple OSS Distributions 	T_ATEND(telemetry_cleanup);
97*c54f35caSApple OSS Distributions }
98*c54f35caSApple OSS Distributions 
99*c54f35caSApple OSS Distributions volatile static bool spinning = true;
100*c54f35caSApple OSS Distributions 
101*c54f35caSApple OSS Distributions static void *
thread_spin(__unused void * arg)102*c54f35caSApple OSS Distributions thread_spin(__unused void *arg)
103*c54f35caSApple OSS Distributions {
104*c54f35caSApple OSS Distributions 	while (spinning) {
105*c54f35caSApple OSS Distributions 	}
106*c54f35caSApple OSS Distributions 	return NULL;
107*c54f35caSApple OSS Distributions }
108*c54f35caSApple OSS Distributions 
109*c54f35caSApple OSS Distributions static bool
query_pmi_params(unsigned int * pmi_counter,uint64_t * pmi_period)110*c54f35caSApple OSS Distributions query_pmi_params(unsigned int *pmi_counter, uint64_t *pmi_period)
111*c54f35caSApple OSS Distributions {
112*c54f35caSApple OSS Distributions 	bool pmi_support = true;
113*c54f35caSApple OSS Distributions 	size_t sysctl_size = sizeof(pmi_counter);
114*c54f35caSApple OSS Distributions 	int ret = sysctlbyname(
115*c54f35caSApple OSS Distributions 			"kern.microstackshot.pmi_sample_counter",
116*c54f35caSApple OSS Distributions 			pmi_counter, &sysctl_size, NULL, 0);
117*c54f35caSApple OSS Distributions 	if (ret == -1 && errno == ENOENT) {
118*c54f35caSApple OSS Distributions 		pmi_support = false;
119*c54f35caSApple OSS Distributions 		T_LOG("no PMI support");
120*c54f35caSApple OSS Distributions 	} else {
121*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "query PMI counter");
122*c54f35caSApple OSS Distributions 	}
123*c54f35caSApple OSS Distributions 	if (pmi_support) {
124*c54f35caSApple OSS Distributions 		sysctl_size = sizeof(*pmi_period);
125*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname(
126*c54f35caSApple OSS Distributions 				"kern.microstackshot.pmi_sample_period",
127*c54f35caSApple OSS Distributions 				pmi_period, &sysctl_size, NULL, 0),
128*c54f35caSApple OSS Distributions 				"query PMI period");
129*c54f35caSApple OSS Distributions 	}
130*c54f35caSApple OSS Distributions 	return pmi_support;
131*c54f35caSApple OSS Distributions }
132*c54f35caSApple OSS Distributions 
133*c54f35caSApple OSS Distributions #define MT_MICROSTACKSHOT KDBG_EVENTID(DBG_MONOTONIC, 2, 1)
134*c54f35caSApple OSS Distributions #define MS_RECORD MACHDBG_CODE(DBG_MACH_STACKSHOT, \
135*c54f35caSApple OSS Distributions 	        MICROSTACKSHOT_RECORD)
136*c54f35caSApple OSS Distributions #if defined(__arm64__)
137*c54f35caSApple OSS Distributions #define INSTRS_PERIOD (100ULL * 1000 * 1000)
138*c54f35caSApple OSS Distributions #else /* defined(__arm64__) */
139*c54f35caSApple OSS Distributions #define INSTRS_PERIOD (1ULL * 1000 * 1000 * 1000)
140*c54f35caSApple OSS Distributions #endif /* defined(__arm64__) */
141*c54f35caSApple OSS Distributions #define SLEEP_SECS 10
142*c54f35caSApple OSS Distributions 
143*c54f35caSApple OSS Distributions T_DECL(pmi_sampling, "attempt to configure microstackshots on PMI",
144*c54f35caSApple OSS Distributions 		T_META_REQUIRES_SYSCTL_EQ("kern.monotonic.supported", 1))
145*c54f35caSApple OSS Distributions {
146*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
147*c54f35caSApple OSS Distributions 
148*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
149*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
150*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "session create");
151*c54f35caSApple OSS Distributions 
152*c54f35caSApple OSS Distributions 	__block int pmi_events = 0;
153*c54f35caSApple OSS Distributions 	__block int microstackshot_record_events = 0;
154*c54f35caSApple OSS Distributions 	__block int pmi_records = 0;
155*c54f35caSApple OSS Distributions 	__block int io_records = 0;
156*c54f35caSApple OSS Distributions 	__block int interrupt_records = 0;
157*c54f35caSApple OSS Distributions 	__block int timer_arm_records = 0;
158*c54f35caSApple OSS Distributions 	__block int unknown_records = 0;
159*c54f35caSApple OSS Distributions 	__block int empty_records = 0;
160*c54f35caSApple OSS Distributions 
161*c54f35caSApple OSS Distributions 	ktrace_events_single(s, MT_MICROSTACKSHOT, ^(__unused struct trace_point *tp) {
162*c54f35caSApple OSS Distributions 		pmi_events++;
163*c54f35caSApple OSS Distributions 	});
164*c54f35caSApple OSS Distributions 	ktrace_events_single_paired(s, MS_RECORD,
165*c54f35caSApple OSS Distributions 	    ^(struct trace_point *start, __unused struct trace_point *end) {
166*c54f35caSApple OSS Distributions 		if (start->arg1 & kPMIRecord) {
167*c54f35caSApple OSS Distributions 		        pmi_records++;
168*c54f35caSApple OSS Distributions 		}
169*c54f35caSApple OSS Distributions 		if (start->arg1 & kIORecord) {
170*c54f35caSApple OSS Distributions 		        io_records++;
171*c54f35caSApple OSS Distributions 		}
172*c54f35caSApple OSS Distributions 		if (start->arg1 & kInterruptRecord) {
173*c54f35caSApple OSS Distributions 		        interrupt_records++;
174*c54f35caSApple OSS Distributions 		}
175*c54f35caSApple OSS Distributions 		if (start->arg1 & kTimerArmingRecord) {
176*c54f35caSApple OSS Distributions 		        timer_arm_records++;
177*c54f35caSApple OSS Distributions 		}
178*c54f35caSApple OSS Distributions 
179*c54f35caSApple OSS Distributions 		if (start->arg2 == end->arg2) {
180*c54f35caSApple OSS Distributions 			/*
181*c54f35caSApple OSS Distributions 			 * The buffer didn't grow for this record -- there was
182*c54f35caSApple OSS Distributions 			 * an error.
183*c54f35caSApple OSS Distributions 			 */
184*c54f35caSApple OSS Distributions 			empty_records++;
185*c54f35caSApple OSS Distributions 		}
186*c54f35caSApple OSS Distributions 
187*c54f35caSApple OSS Distributions 		const uint8_t any_record = kPMIRecord | kIORecord | kInterruptRecord |
188*c54f35caSApple OSS Distributions 		kTimerArmingRecord;
189*c54f35caSApple OSS Distributions 		if ((start->arg1 & any_record) == 0) {
190*c54f35caSApple OSS Distributions 		        unknown_records++;
191*c54f35caSApple OSS Distributions 		}
192*c54f35caSApple OSS Distributions 
193*c54f35caSApple OSS Distributions 		microstackshot_record_events++;
194*c54f35caSApple OSS Distributions 	});
195*c54f35caSApple OSS Distributions 
196*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
197*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
198*c54f35caSApple OSS Distributions 		T_EXPECT_GT(pmi_events, 0, "saw non-zero PMIs (%g/sec)",
199*c54f35caSApple OSS Distributions 				pmi_events / (double)SLEEP_SECS);
200*c54f35caSApple OSS Distributions 		T_EXPECT_GT(pmi_records, 0, "saw non-zero PMI record events (%g/sec)",
201*c54f35caSApple OSS Distributions 				pmi_records / (double)SLEEP_SECS);
202*c54f35caSApple OSS Distributions 		T_LOG("saw %d unknown record events", unknown_records);
203*c54f35caSApple OSS Distributions 		T_EXPECT_GT(microstackshot_record_events, 0,
204*c54f35caSApple OSS Distributions 				"saw non-zero microstackshot record events (%d -- %g/sec)",
205*c54f35caSApple OSS Distributions 				microstackshot_record_events,
206*c54f35caSApple OSS Distributions 				microstackshot_record_events / (double)SLEEP_SECS);
207*c54f35caSApple OSS Distributions 		T_EXPECT_NE(empty_records, microstackshot_record_events,
208*c54f35caSApple OSS Distributions 				"saw non-empty records (%d empty)", empty_records);
209*c54f35caSApple OSS Distributions 
210*c54f35caSApple OSS Distributions 		if (interrupt_records > 0) {
211*c54f35caSApple OSS Distributions 			T_LOG("saw %g interrupt records per second",
212*c54f35caSApple OSS Distributions 					interrupt_records / (double)SLEEP_SECS);
213*c54f35caSApple OSS Distributions 		} else {
214*c54f35caSApple OSS Distributions 			T_LOG("saw no interrupt records");
215*c54f35caSApple OSS Distributions 		}
216*c54f35caSApple OSS Distributions 		if (io_records > 0) {
217*c54f35caSApple OSS Distributions 			T_LOG("saw %g I/O records per second",
218*c54f35caSApple OSS Distributions 					io_records / (double)SLEEP_SECS);
219*c54f35caSApple OSS Distributions 		} else {
220*c54f35caSApple OSS Distributions 			T_LOG("saw no I/O records");
221*c54f35caSApple OSS Distributions 		}
222*c54f35caSApple OSS Distributions 		if (timer_arm_records > 0) {
223*c54f35caSApple OSS Distributions 			T_LOG("saw %g timer arming records per second",
224*c54f35caSApple OSS Distributions 					timer_arm_records / (double)SLEEP_SECS);
225*c54f35caSApple OSS Distributions 		} else {
226*c54f35caSApple OSS Distributions 			T_LOG("saw no timer arming records");
227*c54f35caSApple OSS Distributions 		}
228*c54f35caSApple OSS Distributions 
229*c54f35caSApple OSS Distributions 		T_END;
230*c54f35caSApple OSS Distributions 	});
231*c54f35caSApple OSS Distributions 
232*c54f35caSApple OSS Distributions 	T_SETUPEND;
233*c54f35caSApple OSS Distributions 
234*c54f35caSApple OSS Distributions 	telemetry_init();
235*c54f35caSApple OSS Distributions 
236*c54f35caSApple OSS Distributions 	/*
237*c54f35caSApple OSS Distributions 	 * Start sampling via telemetry on the instructions PMI.
238*c54f35caSApple OSS Distributions 	 */
239*c54f35caSApple OSS Distributions 	int ret = __telemetry(TELEMETRY_CMD_PMI_SETUP, TELEMETRY_PMI_INSTRS,
240*c54f35caSApple OSS Distributions 			INSTRS_PERIOD, 0, 0, 0);
241*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret,
242*c54f35caSApple OSS Distributions 			"telemetry syscall succeeded, started microstackshots");
243*c54f35caSApple OSS Distributions 
244*c54f35caSApple OSS Distributions 	unsigned int pmi_counter = 0;
245*c54f35caSApple OSS Distributions 	uint64_t pmi_period = 0;
246*c54f35caSApple OSS Distributions 	bool pmi_support = query_pmi_params(&pmi_counter, &pmi_period);
247*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(pmi_support, "PMI should be supported");
248*c54f35caSApple OSS Distributions 
249*c54f35caSApple OSS Distributions 	T_LOG("PMI counter: %u", pmi_counter);
250*c54f35caSApple OSS Distributions 	T_LOG("PMI period: %llu", pmi_period);
251*c54f35caSApple OSS Distributions #if defined(__arm64__)
252*c54f35caSApple OSS Distributions 	const unsigned int instrs_counter = 1;
253*c54f35caSApple OSS Distributions #else
254*c54f35caSApple OSS Distributions 	const unsigned int instrs_counter = 0;
255*c54f35caSApple OSS Distributions #endif // defined(__arm64__)
256*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(pmi_counter, instrs_counter,
257*c54f35caSApple OSS Distributions 			"PMI on instructions retired");
258*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(pmi_period, INSTRS_PERIOD, "PMI period is set");
259*c54f35caSApple OSS Distributions 
260*c54f35caSApple OSS Distributions 	pthread_t thread;
261*c54f35caSApple OSS Distributions 	int error = pthread_create(&thread, NULL, thread_spin, NULL);
262*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started thread to spin");
263*c54f35caSApple OSS Distributions 
264*c54f35caSApple OSS Distributions 	error = ktrace_start(s, dispatch_get_main_queue());
265*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started tracing");
266*c54f35caSApple OSS Distributions 
267*c54f35caSApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, SLEEP_SECS * NSEC_PER_SEC),
268*c54f35caSApple OSS Distributions 			dispatch_get_main_queue(), ^{
269*c54f35caSApple OSS Distributions 		spinning = false;
270*c54f35caSApple OSS Distributions 		ktrace_end(s, 0);
271*c54f35caSApple OSS Distributions 		(void)pthread_join(thread, NULL);
272*c54f35caSApple OSS Distributions 		T_LOG("ending trace session after %d seconds", SLEEP_SECS);
273*c54f35caSApple OSS Distributions 	});
274*c54f35caSApple OSS Distributions 
275*c54f35caSApple OSS Distributions 	dispatch_main();
276*c54f35caSApple OSS Distributions }
277*c54f35caSApple OSS Distributions 
278*c54f35caSApple OSS Distributions T_DECL(error_handling,
279*c54f35caSApple OSS Distributions 		"ensure that error conditions for the telemetry syscall are observed",
280*c54f35caSApple OSS Distributions 		T_META_REQUIRES_SYSCTL_EQ("kern.monotonic.supported", 1))
281*c54f35caSApple OSS Distributions {
282*c54f35caSApple OSS Distributions 	telemetry_init();
283*c54f35caSApple OSS Distributions 
284*c54f35caSApple OSS Distributions 	int ret = __telemetry(TELEMETRY_CMD_PMI_SETUP, TELEMETRY_PMI_INSTRS,
285*c54f35caSApple OSS Distributions 	    1, 0, 0, 0);
286*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(ret, -1, "telemetry shouldn't allow PMI every instruction");
287*c54f35caSApple OSS Distributions 
288*c54f35caSApple OSS Distributions 	ret = __telemetry(TELEMETRY_CMD_PMI_SETUP, TELEMETRY_PMI_INSTRS,
289*c54f35caSApple OSS Distributions 	    1000 * 1000, 0, 0, 0);
290*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(ret, -1,
291*c54f35caSApple OSS Distributions 	    "telemetry shouldn't allow PMI every million instructions");
292*c54f35caSApple OSS Distributions 
293*c54f35caSApple OSS Distributions 	ret = __telemetry(TELEMETRY_CMD_PMI_SETUP, TELEMETRY_PMI_CYCLES,
294*c54f35caSApple OSS Distributions 	    1, 0, 0, 0);
295*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(ret, -1, "telemetry shouldn't allow PMI every cycle");
296*c54f35caSApple OSS Distributions 
297*c54f35caSApple OSS Distributions 	ret = __telemetry(TELEMETRY_CMD_PMI_SETUP, TELEMETRY_PMI_CYCLES,
298*c54f35caSApple OSS Distributions 	    1000 * 1000, 0, 0, 0);
299*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(ret, -1,
300*c54f35caSApple OSS Distributions 	    "telemetry shouldn't allow PMI every million cycles");
301*c54f35caSApple OSS Distributions 
302*c54f35caSApple OSS Distributions 	ret = __telemetry(TELEMETRY_CMD_PMI_SETUP, TELEMETRY_PMI_CYCLES,
303*c54f35caSApple OSS Distributions 	    UINT64_MAX, 0, 0, 0);
304*c54f35caSApple OSS Distributions 	T_EXPECT_EQ(ret, -1, "telemetry shouldn't allow PMI every UINT64_MAX cycles");
305*c54f35caSApple OSS Distributions }
306*c54f35caSApple OSS Distributions 
307*c54f35caSApple OSS Distributions #define START_EVENT (0xfeedfad0)
308*c54f35caSApple OSS Distributions #define STOP_EVENT (0xfeedfac0)
309*c54f35caSApple OSS Distributions 
310*c54f35caSApple OSS Distributions T_DECL(excessive_sampling,
311*c54f35caSApple OSS Distributions 		"ensure that microstackshots are not being sampled too frequently",
312*c54f35caSApple OSS Distributions 		T_META_REQUIRES_SYSCTL_EQ("kern.monotonic.supported", 1))
313*c54f35caSApple OSS Distributions {
314*c54f35caSApple OSS Distributions 	unsigned int interrupt_sample_rate = 0;
315*c54f35caSApple OSS Distributions 	size_t sysctl_size = sizeof(interrupt_sample_rate);
316*c54f35caSApple OSS Distributions 	T_QUIET;
317*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctlbyname(
318*c54f35caSApple OSS Distributions 			"kern.microstackshot.interrupt_sample_rate",
319*c54f35caSApple OSS Distributions 			&interrupt_sample_rate, &sysctl_size, NULL, 0),
320*c54f35caSApple OSS Distributions 			"query interrupt sample rate");
321*c54f35caSApple OSS Distributions 	unsigned int pmi_counter = 0;
322*c54f35caSApple OSS Distributions 	uint64_t pmi_period = 0;
323*c54f35caSApple OSS Distributions 	(void)query_pmi_params(&pmi_counter, &pmi_period);
324*c54f35caSApple OSS Distributions 
325*c54f35caSApple OSS Distributions 	T_LOG("interrupt sample rate: %uHz", interrupt_sample_rate);
326*c54f35caSApple OSS Distributions 	T_LOG("PMI counter: %u", pmi_counter);
327*c54f35caSApple OSS Distributions 	T_LOG("PMI period: %llu", pmi_period);
328*c54f35caSApple OSS Distributions 
329*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
330*c54f35caSApple OSS Distributions 
331*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
332*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
333*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "session create");
334*c54f35caSApple OSS Distributions 
335*c54f35caSApple OSS Distributions 	__block int microstackshot_record_events = 0;
336*c54f35caSApple OSS Distributions 	__block int pmi_records = 0;
337*c54f35caSApple OSS Distributions 	__block int io_records = 0;
338*c54f35caSApple OSS Distributions 	__block int interrupt_records = 0;
339*c54f35caSApple OSS Distributions 	__block int timer_arm_records = 0;
340*c54f35caSApple OSS Distributions 	__block int unknown_records = 0;
341*c54f35caSApple OSS Distributions 	__block int empty_records = 0;
342*c54f35caSApple OSS Distributions 	__block uint64_t first_timestamp_ns = 0;
343*c54f35caSApple OSS Distributions 	__block uint64_t last_timestamp_ns = 0;
344*c54f35caSApple OSS Distributions 
345*c54f35caSApple OSS Distributions 	ktrace_events_single_paired(s, MS_RECORD,
346*c54f35caSApple OSS Distributions 			^(struct trace_point *start, __unused struct trace_point *end) {
347*c54f35caSApple OSS Distributions 		if (start->arg1 & kPMIRecord) {
348*c54f35caSApple OSS Distributions 			pmi_records++;
349*c54f35caSApple OSS Distributions 		}
350*c54f35caSApple OSS Distributions 		if (start->arg1 & kIORecord) {
351*c54f35caSApple OSS Distributions 			io_records++;
352*c54f35caSApple OSS Distributions 		}
353*c54f35caSApple OSS Distributions 		if (start->arg1 & kInterruptRecord) {
354*c54f35caSApple OSS Distributions 			interrupt_records++;
355*c54f35caSApple OSS Distributions 		}
356*c54f35caSApple OSS Distributions 		if (start->arg1 & kTimerArmingRecord) {
357*c54f35caSApple OSS Distributions 			timer_arm_records++;
358*c54f35caSApple OSS Distributions 		}
359*c54f35caSApple OSS Distributions 
360*c54f35caSApple OSS Distributions 		if (start->arg2 == end->arg2) {
361*c54f35caSApple OSS Distributions 			/*
362*c54f35caSApple OSS Distributions 			 * The buffer didn't grow for this record -- there was
363*c54f35caSApple OSS Distributions 			 * an error.
364*c54f35caSApple OSS Distributions 			 */
365*c54f35caSApple OSS Distributions 			empty_records++;
366*c54f35caSApple OSS Distributions 		}
367*c54f35caSApple OSS Distributions 
368*c54f35caSApple OSS Distributions 		const uint8_t any_record = kPMIRecord | kIORecord | kInterruptRecord |
369*c54f35caSApple OSS Distributions 				kTimerArmingRecord;
370*c54f35caSApple OSS Distributions 		if ((start->arg1 & any_record) == 0) {
371*c54f35caSApple OSS Distributions 			unknown_records++;
372*c54f35caSApple OSS Distributions 		}
373*c54f35caSApple OSS Distributions 
374*c54f35caSApple OSS Distributions 		microstackshot_record_events++;
375*c54f35caSApple OSS Distributions 	});
376*c54f35caSApple OSS Distributions 
377*c54f35caSApple OSS Distributions 	ktrace_events_single(s, START_EVENT, ^(struct trace_point *tp) {
378*c54f35caSApple OSS Distributions 		int error = ktrace_convert_timestamp_to_nanoseconds(s,
379*c54f35caSApple OSS Distributions 				tp->timestamp, &first_timestamp_ns);
380*c54f35caSApple OSS Distributions 		T_QUIET;
381*c54f35caSApple OSS Distributions 		T_ASSERT_POSIX_ZERO(error, "converted timestamp to nanoseconds");
382*c54f35caSApple OSS Distributions 	});
383*c54f35caSApple OSS Distributions 
384*c54f35caSApple OSS Distributions 	ktrace_events_single(s, STOP_EVENT, ^(struct trace_point *tp) {
385*c54f35caSApple OSS Distributions 		int error = ktrace_convert_timestamp_to_nanoseconds(s,
386*c54f35caSApple OSS Distributions 				tp->timestamp, &last_timestamp_ns);
387*c54f35caSApple OSS Distributions 		T_QUIET;
388*c54f35caSApple OSS Distributions 		T_ASSERT_POSIX_ZERO(error, "converted timestamp to nanoseconds");
389*c54f35caSApple OSS Distributions 		ktrace_end(s, 1);
390*c54f35caSApple OSS Distributions 	});
391*c54f35caSApple OSS Distributions 
392*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
393*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
394*c54f35caSApple OSS Distributions 
395*c54f35caSApple OSS Distributions 		uint64_t duration_ns = last_timestamp_ns - first_timestamp_ns;
396*c54f35caSApple OSS Distributions 		double duration_secs = (double)duration_ns / 1e9;
397*c54f35caSApple OSS Distributions 
398*c54f35caSApple OSS Distributions 		T_LOG("test lasted %g seconds", duration_secs);
399*c54f35caSApple OSS Distributions 
400*c54f35caSApple OSS Distributions 		T_MAYFAIL;
401*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(unknown_records, 0, "saw zero unknown record events");
402*c54f35caSApple OSS Distributions 		T_MAYFAIL;
403*c54f35caSApple OSS Distributions 		T_EXPECT_GT(microstackshot_record_events, 0,
404*c54f35caSApple OSS Distributions 				"saw non-zero microstackshot record events (%d, %gHz)",
405*c54f35caSApple OSS Distributions 				microstackshot_record_events,
406*c54f35caSApple OSS Distributions 				microstackshot_record_events / duration_secs);
407*c54f35caSApple OSS Distributions 		T_EXPECT_NE(empty_records, microstackshot_record_events,
408*c54f35caSApple OSS Distributions 				"saw non-empty records (%d empty)", empty_records);
409*c54f35caSApple OSS Distributions 
410*c54f35caSApple OSS Distributions 		double record_rate_hz = microstackshot_record_events / duration_secs;
411*c54f35caSApple OSS Distributions 		T_LOG("record rate: %gHz", record_rate_hz);
412*c54f35caSApple OSS Distributions 		T_LOG("PMI record rate: %gHz", pmi_records / duration_secs);
413*c54f35caSApple OSS Distributions 		T_LOG("interrupt record rate: %gHz",
414*c54f35caSApple OSS Distributions 				interrupt_records / duration_secs);
415*c54f35caSApple OSS Distributions 		T_LOG("I/O record rate: %gHz", io_records / duration_secs);
416*c54f35caSApple OSS Distributions 		T_LOG("timer arm record rate: %gHz",
417*c54f35caSApple OSS Distributions 				timer_arm_records / duration_secs);
418*c54f35caSApple OSS Distributions 
419*c54f35caSApple OSS Distributions 		T_EXPECT_LE(record_rate_hz, (double)(dt_ncpu() * 50),
420*c54f35caSApple OSS Distributions 				"found appropriate rate of microstackshots");
421*c54f35caSApple OSS Distributions 
422*c54f35caSApple OSS Distributions 		T_END;
423*c54f35caSApple OSS Distributions 	});
424*c54f35caSApple OSS Distributions 
425*c54f35caSApple OSS Distributions 	pthread_t thread;
426*c54f35caSApple OSS Distributions 	int error = pthread_create(&thread, NULL, thread_spin, NULL);
427*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started thread to spin");
428*c54f35caSApple OSS Distributions 
429*c54f35caSApple OSS Distributions 	T_SETUPEND;
430*c54f35caSApple OSS Distributions 
431*c54f35caSApple OSS Distributions 	error = ktrace_start(s, dispatch_get_main_queue());
432*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started tracing");
433*c54f35caSApple OSS Distributions 	kdebug_trace(START_EVENT, 0, 0, 0, 0);
434*c54f35caSApple OSS Distributions 
435*c54f35caSApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, SLEEP_SECS * NSEC_PER_SEC),
436*c54f35caSApple OSS Distributions 			dispatch_get_main_queue(), ^{
437*c54f35caSApple OSS Distributions 		spinning = false;
438*c54f35caSApple OSS Distributions 		kdebug_trace(STOP_EVENT, 0, 0, 0, 0);
439*c54f35caSApple OSS Distributions 		(void)pthread_join(thread, NULL);
440*c54f35caSApple OSS Distributions 		T_LOG("ending trace session after %d seconds", SLEEP_SECS);
441*c54f35caSApple OSS Distributions 	});
442*c54f35caSApple OSS Distributions 
443*c54f35caSApple OSS Distributions 	dispatch_main();
444*c54f35caSApple OSS Distributions }
445