xref: /xnu-10063.121.3/tests/monotonic_core.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions // Copyright (c) 2021-2022 Apple Inc.  All rights reserved.
2*2c2f96dcSApple OSS Distributions 
3*2c2f96dcSApple OSS Distributions #include <darwintest.h>
4*2c2f96dcSApple OSS Distributions #include "test_utils.h"
5*2c2f96dcSApple OSS Distributions #include <fcntl.h>
6*2c2f96dcSApple OSS Distributions #include <inttypes.h>
7*2c2f96dcSApple OSS Distributions #ifndef PRIVATE
8*2c2f96dcSApple OSS Distributions /*
9*2c2f96dcSApple OSS Distributions  * Need new CPU families.
10*2c2f96dcSApple OSS Distributions  */
11*2c2f96dcSApple OSS Distributions #define PRIVATE
12*2c2f96dcSApple OSS Distributions #include <mach/machine.h>
13*2c2f96dcSApple OSS Distributions #undef PRIVATE
14*2c2f96dcSApple OSS Distributions #else /* !defined(PRIVATE) */
15*2c2f96dcSApple OSS Distributions #include <mach/machine.h>
16*2c2f96dcSApple OSS Distributions #endif /* defined(PRIVATE) */
17*2c2f96dcSApple OSS Distributions #include <ktrace.h>
18*2c2f96dcSApple OSS Distributions #include <mach/mach.h>
19*2c2f96dcSApple OSS Distributions #include <stdint.h>
20*2c2f96dcSApple OSS Distributions #include <System/sys/guarded.h>
21*2c2f96dcSApple OSS Distributions #include <System/sys/monotonic.h>
22*2c2f96dcSApple OSS Distributions #include <sys/ioctl.h>
23*2c2f96dcSApple OSS Distributions #include <sys/kdebug.h>
24*2c2f96dcSApple OSS Distributions #include <sys/resource.h>
25*2c2f96dcSApple OSS Distributions #include <sys/resource_private.h>
26*2c2f96dcSApple OSS Distributions #include <sys/sysctl.h>
27*2c2f96dcSApple OSS Distributions #include <unistd.h>
28*2c2f96dcSApple OSS Distributions 
29*2c2f96dcSApple OSS Distributions T_GLOBAL_META(
30*2c2f96dcSApple OSS Distributions 	T_META_NAMESPACE("xnu.monotonic"),
31*2c2f96dcSApple OSS Distributions 	T_META_CHECK_LEAKS(false)
32*2c2f96dcSApple OSS Distributions 	);
33*2c2f96dcSApple OSS Distributions 
34*2c2f96dcSApple OSS Distributions static void
skip_if_unsupported(void)35*2c2f96dcSApple OSS Distributions skip_if_unsupported(void)
36*2c2f96dcSApple OSS Distributions {
37*2c2f96dcSApple OSS Distributions 	int r;
38*2c2f96dcSApple OSS Distributions 	int supported = 0;
39*2c2f96dcSApple OSS Distributions 	size_t supported_size = sizeof(supported);
40*2c2f96dcSApple OSS Distributions 
41*2c2f96dcSApple OSS Distributions 	r = sysctlbyname("kern.monotonic.supported", &supported, &supported_size,
42*2c2f96dcSApple OSS Distributions 	    NULL, 0);
43*2c2f96dcSApple OSS Distributions 	if (r < 0) {
44*2c2f96dcSApple OSS Distributions 		T_WITH_ERRNO;
45*2c2f96dcSApple OSS Distributions 		T_SKIP("could not find \"kern.monotonic.supported\" sysctl");
46*2c2f96dcSApple OSS Distributions 	}
47*2c2f96dcSApple OSS Distributions 
48*2c2f96dcSApple OSS Distributions 	if (!supported) {
49*2c2f96dcSApple OSS Distributions 		T_SKIP("monotonic is not supported on this platform");
50*2c2f96dcSApple OSS Distributions 	}
51*2c2f96dcSApple OSS Distributions }
52*2c2f96dcSApple OSS Distributions 
53*2c2f96dcSApple OSS Distributions static void
check_fixed_counts(struct thsc_cpi counts[2])54*2c2f96dcSApple OSS Distributions check_fixed_counts(struct thsc_cpi counts[2])
55*2c2f96dcSApple OSS Distributions {
56*2c2f96dcSApple OSS Distributions 	T_QUIET;
57*2c2f96dcSApple OSS Distributions 	T_EXPECT_GT(counts[0].tcpi_instructions, UINT64_C(0), "non-zero instructions");
58*2c2f96dcSApple OSS Distributions 	T_QUIET;
59*2c2f96dcSApple OSS Distributions 	T_EXPECT_GT(counts[0].tcpi_cycles, UINT64_C(0), "non-zero cycles");
60*2c2f96dcSApple OSS Distributions 
61*2c2f96dcSApple OSS Distributions 	T_EXPECT_GT(counts[1].tcpi_instructions, counts[0].tcpi_instructions,
62*2c2f96dcSApple OSS Distributions 	    "monotonically-increasing instructions");
63*2c2f96dcSApple OSS Distributions 	T_EXPECT_GT(counts[1].tcpi_cycles, counts[0].tcpi_cycles,
64*2c2f96dcSApple OSS Distributions 	    "monotonically-increasing cycles");
65*2c2f96dcSApple OSS Distributions }
66*2c2f96dcSApple OSS Distributions 
67*2c2f96dcSApple OSS Distributions T_DECL(core_fixed_task, "check that task counting is working",
68*2c2f96dcSApple OSS Distributions     XNU_T_META_SOC_SPECIFIC, T_META_ASROOT(true))
69*2c2f96dcSApple OSS Distributions {
70*2c2f96dcSApple OSS Distributions 	task_t task = mach_task_self();
71*2c2f96dcSApple OSS Distributions 	kern_return_t kr;
72*2c2f96dcSApple OSS Distributions 	mach_msg_type_number_t size = TASK_INSPECT_BASIC_COUNTS_COUNT;
73*2c2f96dcSApple OSS Distributions 	struct thsc_cpi counts[2];
74*2c2f96dcSApple OSS Distributions 
75*2c2f96dcSApple OSS Distributions 	skip_if_unsupported();
76*2c2f96dcSApple OSS Distributions 
77*2c2f96dcSApple OSS Distributions 	kr = task_inspect(task, TASK_INSPECT_BASIC_COUNTS,
78*2c2f96dcSApple OSS Distributions 	    (task_inspect_info_t)&counts[0], &size);
79*2c2f96dcSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr,
80*2c2f96dcSApple OSS Distributions 	    "task_inspect(... TASK_INSPECT_BASIC_COUNTS ...)");
81*2c2f96dcSApple OSS Distributions 
82*2c2f96dcSApple OSS Distributions 	size = TASK_INSPECT_BASIC_COUNTS_COUNT;
83*2c2f96dcSApple OSS Distributions 	kr = task_inspect(task, TASK_INSPECT_BASIC_COUNTS,
84*2c2f96dcSApple OSS Distributions 	    (task_inspect_info_t)&counts[1], &size);
85*2c2f96dcSApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr,
86*2c2f96dcSApple OSS Distributions 	    "task_inspect(... TASK_INSPECT_BASIC_COUNTS ...)");
87*2c2f96dcSApple OSS Distributions 
88*2c2f96dcSApple OSS Distributions 	check_fixed_counts(counts);
89*2c2f96dcSApple OSS Distributions }
90*2c2f96dcSApple OSS Distributions 
91*2c2f96dcSApple OSS Distributions T_DECL(core_fixed_kdebug, "check that the kdebug macros for monotonic work",
92*2c2f96dcSApple OSS Distributions     T_META_ASROOT(true))
93*2c2f96dcSApple OSS Distributions {
94*2c2f96dcSApple OSS Distributions 	__block bool saw_events = false;
95*2c2f96dcSApple OSS Distributions 	ktrace_session_t s;
96*2c2f96dcSApple OSS Distributions 	int r;
97*2c2f96dcSApple OSS Distributions 	int set = 1;
98*2c2f96dcSApple OSS Distributions 
99*2c2f96dcSApple OSS Distributions 	T_SETUPBEGIN;
100*2c2f96dcSApple OSS Distributions 	skip_if_unsupported();
101*2c2f96dcSApple OSS Distributions 
102*2c2f96dcSApple OSS Distributions 	s = ktrace_session_create();
103*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(s, "ktrace_session_create");
104*2c2f96dcSApple OSS Distributions 
105*2c2f96dcSApple OSS Distributions 	ktrace_events_single_paired(s,
106*2c2f96dcSApple OSS Distributions 	    KDBG_EVENTID(DBG_MONOTONIC, DBG_MT_TMPCPU, 0x3fff),
107*2c2f96dcSApple OSS Distributions 	    ^(struct trace_point *start, struct trace_point *end)
108*2c2f96dcSApple OSS Distributions 	{
109*2c2f96dcSApple OSS Distributions 		struct thsc_cpi counts[2];
110*2c2f96dcSApple OSS Distributions 
111*2c2f96dcSApple OSS Distributions 		saw_events = true;
112*2c2f96dcSApple OSS Distributions 
113*2c2f96dcSApple OSS Distributions 		counts[0].tcpi_instructions = start->arg1;
114*2c2f96dcSApple OSS Distributions 		counts[0].tcpi_cycles = start->arg2;
115*2c2f96dcSApple OSS Distributions 		counts[1].tcpi_instructions = end->arg1;
116*2c2f96dcSApple OSS Distributions 		counts[1].tcpi_cycles = end->arg2;
117*2c2f96dcSApple OSS Distributions 
118*2c2f96dcSApple OSS Distributions 		check_fixed_counts(counts);
119*2c2f96dcSApple OSS Distributions 	});
120*2c2f96dcSApple OSS Distributions 
121*2c2f96dcSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
122*2c2f96dcSApple OSS Distributions 		T_ASSERT_TRUE(saw_events, "should see monotonic kdebug events");
123*2c2f96dcSApple OSS Distributions 		T_END;
124*2c2f96dcSApple OSS Distributions 	});
125*2c2f96dcSApple OSS Distributions 	T_SETUPEND;
126*2c2f96dcSApple OSS Distributions 
127*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s,
128*2c2f96dcSApple OSS Distributions 	    dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)), NULL);
129*2c2f96dcSApple OSS Distributions 
130*2c2f96dcSApple OSS Distributions 	r = sysctlbyname("kern.monotonic.kdebug_test", NULL, NULL, &set,
131*2c2f96dcSApple OSS Distributions 	    sizeof(set));
132*2c2f96dcSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(r,
133*2c2f96dcSApple OSS Distributions 	    "sysctlbyname(\"kern.monotonic.kdebug_test\", ...)");
134*2c2f96dcSApple OSS Distributions 
135*2c2f96dcSApple OSS Distributions 	ktrace_end(s, 0);
136*2c2f96dcSApple OSS Distributions 	dispatch_main();
137*2c2f96dcSApple OSS Distributions }
138*2c2f96dcSApple OSS Distributions 
139*2c2f96dcSApple OSS Distributions static void *
spin_thread_self_counts(__unused void * arg)140*2c2f96dcSApple OSS Distributions spin_thread_self_counts(__unused void *arg)
141*2c2f96dcSApple OSS Distributions {
142*2c2f96dcSApple OSS Distributions 	struct thsc_cpi counts = { 0 };
143*2c2f96dcSApple OSS Distributions 	while (true) {
144*2c2f96dcSApple OSS Distributions 		(void)thread_selfcounts(THSC_CPI, &counts, sizeof(counts));
145*2c2f96dcSApple OSS Distributions 	}
146*2c2f96dcSApple OSS Distributions }
147*2c2f96dcSApple OSS Distributions 
148*2c2f96dcSApple OSS Distributions static void *
spin_task_inspect(__unused void * arg)149*2c2f96dcSApple OSS Distributions spin_task_inspect(__unused void *arg)
150*2c2f96dcSApple OSS Distributions {
151*2c2f96dcSApple OSS Distributions 	task_t task = mach_task_self();
152*2c2f96dcSApple OSS Distributions 	uint64_t counts[2] = { 0 };
153*2c2f96dcSApple OSS Distributions 	unsigned int size = 0;
154*2c2f96dcSApple OSS Distributions 	while (true) {
155*2c2f96dcSApple OSS Distributions 		size = (unsigned int)sizeof(counts);
156*2c2f96dcSApple OSS Distributions 		(void)task_inspect(task, TASK_INSPECT_BASIC_COUNTS,
157*2c2f96dcSApple OSS Distributions 		    (task_inspect_info_t)&counts[0], &size);
158*2c2f96dcSApple OSS Distributions 		/*
159*2c2f96dcSApple OSS Distributions 		 * Not realistic for a process to see count values with the high bit
160*2c2f96dcSApple OSS Distributions 		 * set, but kernel pointers will be that high.
161*2c2f96dcSApple OSS Distributions 		 */
162*2c2f96dcSApple OSS Distributions 		T_QUIET; T_ASSERT_LT(counts[0], 1ULL << 63,
163*2c2f96dcSApple OSS Distributions 		        "check for valid count entry 1");
164*2c2f96dcSApple OSS Distributions 		T_QUIET; T_ASSERT_LT(counts[1], 1ULL << 63,
165*2c2f96dcSApple OSS Distributions 		        "check for valid count entry 2");
166*2c2f96dcSApple OSS Distributions 	}
167*2c2f96dcSApple OSS Distributions }
168*2c2f96dcSApple OSS Distributions 
169*2c2f96dcSApple OSS Distributions T_DECL(core_fixed_stack_leak_race,
170*2c2f96dcSApple OSS Distributions     "ensure no stack data is leaked by TASK_INSPECT_BASIC_COUNTS")
171*2c2f96dcSApple OSS Distributions {
172*2c2f96dcSApple OSS Distributions 	T_SETUPBEGIN;
173*2c2f96dcSApple OSS Distributions 
174*2c2f96dcSApple OSS Distributions 	int ncpus = 0;
175*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname("hw.logicalcpu_max", &ncpus,
176*2c2f96dcSApple OSS Distributions 	    &(size_t){ sizeof(ncpus) }, NULL, 0), "get number of CPUs");
177*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_GT(ncpus, 0, "got non-zero number of CPUs");
178*2c2f96dcSApple OSS Distributions 	pthread_t *threads = calloc((unsigned long)ncpus, sizeof(*threads));
179*2c2f96dcSApple OSS Distributions 
180*2c2f96dcSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(threads, "allocated space for threads");
181*2c2f96dcSApple OSS Distributions 
182*2c2f96dcSApple OSS Distributions 	T_LOG("creating %d threads to attempt to race around task counts", ncpus);
183*2c2f96dcSApple OSS Distributions 	/*
184*2c2f96dcSApple OSS Distributions 	 * Have half the threads hammering thread_self_counts and the other half
185*2c2f96dcSApple OSS Distributions 	 * trying to get an error to occur inside TASK_INSPECT_BASIC_COUNTS and see
186*2c2f96dcSApple OSS Distributions 	 * uninitialized kernel memory.
187*2c2f96dcSApple OSS Distributions 	 */
188*2c2f96dcSApple OSS Distributions 	for (int i = 0; i < ncpus; i++) {
189*2c2f96dcSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(pthread_create(&threads[i], NULL,
190*2c2f96dcSApple OSS Distributions 		    i & 1 ? spin_task_inspect : spin_thread_self_counts, NULL),
191*2c2f96dcSApple OSS Distributions 		    NULL);
192*2c2f96dcSApple OSS Distributions 	}
193*2c2f96dcSApple OSS Distributions 
194*2c2f96dcSApple OSS Distributions 	T_SETUPEND;
195*2c2f96dcSApple OSS Distributions 
196*2c2f96dcSApple OSS Distributions 	sleep(10);
197*2c2f96dcSApple OSS Distributions 	T_PASS("ending test after 10 seconds");
198*2c2f96dcSApple OSS Distributions }
199*2c2f96dcSApple OSS Distributions 
200*2c2f96dcSApple OSS Distributions static void
perf_sysctl_deltas(const char * sysctl_name,const char * stat_name)201*2c2f96dcSApple OSS Distributions perf_sysctl_deltas(const char *sysctl_name, const char *stat_name)
202*2c2f96dcSApple OSS Distributions {
203*2c2f96dcSApple OSS Distributions 	uint64_t deltas[2];
204*2c2f96dcSApple OSS Distributions 	size_t deltas_size;
205*2c2f96dcSApple OSS Distributions 	int r;
206*2c2f96dcSApple OSS Distributions 
207*2c2f96dcSApple OSS Distributions 	T_SETUPBEGIN;
208*2c2f96dcSApple OSS Distributions 	skip_if_unsupported();
209*2c2f96dcSApple OSS Distributions 
210*2c2f96dcSApple OSS Distributions 	dt_stat_t instrs = dt_stat_create("instructions", "%s_instrs",
211*2c2f96dcSApple OSS Distributions 	    stat_name);
212*2c2f96dcSApple OSS Distributions 	dt_stat_t cycles = dt_stat_create("cycles", "%s_cycles", stat_name);
213*2c2f96dcSApple OSS Distributions 	T_SETUPEND;
214*2c2f96dcSApple OSS Distributions 
215*2c2f96dcSApple OSS Distributions 	while (!dt_stat_stable(instrs) || !dt_stat_stable(cycles)) {
216*2c2f96dcSApple OSS Distributions 		deltas_size = sizeof(deltas);
217*2c2f96dcSApple OSS Distributions 		r = sysctlbyname(sysctl_name, deltas, &deltas_size, NULL, 0);
218*2c2f96dcSApple OSS Distributions 		T_QUIET;
219*2c2f96dcSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(r, "sysctlbyname(\"%s\", ...)", sysctl_name);
220*2c2f96dcSApple OSS Distributions 		dt_stat_add(instrs, (double)deltas[0]);
221*2c2f96dcSApple OSS Distributions 		dt_stat_add(cycles, (double)deltas[1]);
222*2c2f96dcSApple OSS Distributions 	}
223*2c2f96dcSApple OSS Distributions 
224*2c2f96dcSApple OSS Distributions 	dt_stat_finalize(instrs);
225*2c2f96dcSApple OSS Distributions 	dt_stat_finalize(cycles);
226*2c2f96dcSApple OSS Distributions }
227*2c2f96dcSApple OSS Distributions 
228*2c2f96dcSApple OSS Distributions T_DECL(perf_core_fixed_cpu, "test the performance of fixed CPU counter access",
229*2c2f96dcSApple OSS Distributions     T_META_ASROOT(true), XNU_T_META_SOC_SPECIFIC, T_META_TAG_PERF)
230*2c2f96dcSApple OSS Distributions {
231*2c2f96dcSApple OSS Distributions 	perf_sysctl_deltas("kern.monotonic.fixed_cpu_perf", "fixed_cpu_counters");
232*2c2f96dcSApple OSS Distributions }
233*2c2f96dcSApple OSS Distributions 
234*2c2f96dcSApple OSS Distributions T_DECL(perf_core_fixed_thread, "test the performance of fixed thread counter access",
235*2c2f96dcSApple OSS Distributions     T_META_ASROOT(true), XNU_T_META_SOC_SPECIFIC, T_META_TAG_PERF)
236*2c2f96dcSApple OSS Distributions {
237*2c2f96dcSApple OSS Distributions 	perf_sysctl_deltas("kern.monotonic.fixed_thread_perf",
238*2c2f96dcSApple OSS Distributions 	    "fixed_thread_counters");
239*2c2f96dcSApple OSS Distributions }
240*2c2f96dcSApple OSS Distributions 
241*2c2f96dcSApple OSS Distributions T_DECL(perf_core_fixed_task, "test the performance of fixed task counter access",
242*2c2f96dcSApple OSS Distributions     T_META_ASROOT(true), XNU_T_META_SOC_SPECIFIC, T_META_TAG_PERF)
243*2c2f96dcSApple OSS Distributions {
244*2c2f96dcSApple OSS Distributions 	perf_sysctl_deltas("kern.monotonic.fixed_task_perf", "fixed_task_counters");
245*2c2f96dcSApple OSS Distributions }
246