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