2 * Must come before including darwintest.h
6 #endif /* defined(T_NAMESPACE) */
8 #include <darwintest.h>
13 * Need new CPU families.
16 #include <mach/machine.h>
18 #else /* !defined(PRIVATE) */
19 #include <mach/machine.h>
20 #endif /* defined(PRIVATE) */
22 #include <mach/mach.h>
24 #include <System/sys/guarded.h>
25 #include <System/sys/monotonic.h>
26 #include <sys/ioctl.h>
27 #include <sys/kdebug.h>
28 #include <sys/sysctl.h>
32 T_META_NAMESPACE("xnu.monotonic"),
33 T_META_CHECK_LEAKS(false)
37 skip_if_unsupported(void)
41 size_t supported_size
= sizeof(supported
);
43 r
= sysctlbyname("kern.monotonic.supported", &supported
, &supported_size
,
47 T_SKIP("could not find \"kern.monotonic.supported\" sysctl");
51 T_SKIP("monotonic is not supported on this platform");
56 check_fixed_counts(uint64_t counts
[2][2])
59 T_EXPECT_GT(counts
[0][0], UINT64_C(0), "instructions are larger than 0");
61 T_EXPECT_GT(counts
[0][1], UINT64_C(0), "cycles are larger than 0");
63 T_EXPECT_GT(counts
[1][0], counts
[0][0], "instructions increase monotonically");
64 T_EXPECT_GT(counts
[1][1], counts
[0][1], "cycles increase monotonically");
67 T_DECL(core_fixed_thread_self
, "check the current thread's fixed counters",
71 extern int thread_selfcounts(int type
, void *buf
, size_t nbytes
);
72 uint64_t counts
[2][2];
75 skip_if_unsupported();
78 err
= thread_selfcounts(1, &counts
[0], sizeof(counts
[0]));
79 T_ASSERT_POSIX_ZERO(err
, "thread_selfcounts");
80 err
= thread_selfcounts(1, &counts
[1], sizeof(counts
[1]));
81 T_ASSERT_POSIX_ZERO(err
, "thread_selfcounts");
83 check_fixed_counts(counts
);
86 T_DECL(core_fixed_task
, "check that task counting is working",
89 task_t task
= mach_task_self();
91 mach_msg_type_number_t size
= TASK_INSPECT_BASIC_COUNTS_COUNT
;
92 uint64_t counts
[2][2];
94 skip_if_unsupported();
96 kr
= task_inspect(task
, TASK_INSPECT_BASIC_COUNTS
,
97 (task_inspect_info_t
)&counts
[0], &size
);
98 T_ASSERT_MACH_SUCCESS(kr
,
99 "task_inspect(... TASK_INSPECT_BASIC_COUNTS ...)");
101 size
= TASK_INSPECT_BASIC_COUNTS_COUNT
;
102 kr
= task_inspect(task
, TASK_INSPECT_BASIC_COUNTS
,
103 (task_inspect_info_t
)&counts
[1], &size
);
104 T_ASSERT_MACH_SUCCESS(kr
,
105 "task_inspect(... TASK_INSPECT_BASIC_COUNTS ...)");
107 check_fixed_counts(counts
);
110 T_DECL(core_fixed_kdebug
, "check that the kdebug macros for monotonic work",
113 __block
bool saw_events
= false;
119 skip_if_unsupported();
121 s
= ktrace_session_create();
122 T_QUIET
; T_ASSERT_NOTNULL(s
, "ktrace_session_create");
124 ktrace_events_single_paired(s
,
125 KDBG_EVENTID(DBG_MONOTONIC
, DBG_MT_TMPCPU
, 0x3fff),
126 ^(struct trace_point
*start
, struct trace_point
*end
)
128 uint64_t counts
[2][2];
132 counts
[0][0] = start
->arg1
;
133 counts
[0][1] = start
->arg2
;
134 counts
[1][0] = end
->arg1
;
135 counts
[1][1] = end
->arg2
;
137 check_fixed_counts(counts
);
140 ktrace_set_completion_handler(s
, ^{
141 T_ASSERT_TRUE(saw_events
, "should see monotonic kdebug events");
146 T_ASSERT_POSIX_ZERO(ktrace_start(s
,
147 dispatch_get_global_queue(QOS_CLASS_USER_INITIATED
, 0)), NULL
);
149 r
= sysctlbyname("kern.monotonic.kdebug_test", NULL
, NULL
, &set
,
151 T_ASSERT_POSIX_SUCCESS(r
,
152 "sysctlbyname(\"kern.monotonic.kdebug_test\", ...)");
159 perf_sysctl_deltas(const char *sysctl_name
, const char *stat_name
)
166 skip_if_unsupported();
168 dt_stat_t instrs
= dt_stat_create("instructions", "%s_instrs",
170 dt_stat_t cycles
= dt_stat_create("cycles", "%s_cycles", stat_name
);
173 while (!dt_stat_stable(instrs
) || !dt_stat_stable(cycles
)) {
174 deltas_size
= sizeof(deltas
);
175 r
= sysctlbyname(sysctl_name
, deltas
, &deltas_size
, NULL
, 0);
177 T_ASSERT_POSIX_SUCCESS(r
, "sysctlbyname(\"%s\", ...)", sysctl_name
);
178 dt_stat_add(instrs
, (double)deltas
[0]);
179 dt_stat_add(cycles
, (double)deltas
[1]);
182 dt_stat_finalize(instrs
);
183 dt_stat_finalize(cycles
);
186 T_DECL(perf_core_fixed_cpu
, "test the performance of fixed CPU counter access",
187 T_META_ASROOT(true), T_META_TAG_PERF
)
189 perf_sysctl_deltas("kern.monotonic.fixed_cpu_perf", "fixed_cpu_counters");
192 T_DECL(perf_core_fixed_thread
, "test the performance of fixed thread counter access",
193 T_META_ASROOT(true), T_META_TAG_PERF
)
195 perf_sysctl_deltas("kern.monotonic.fixed_thread_perf",
196 "fixed_thread_counters");
199 T_DECL(perf_core_fixed_task
, "test the performance of fixed task counter access",
200 T_META_ASROOT(true), T_META_TAG_PERF
)
202 perf_sysctl_deltas("kern.monotonic.fixed_task_perf", "fixed_task_counters");
205 T_DECL(perf_core_fixed_thread_self
, "test the performance of thread self counts",
208 extern int thread_selfcounts(int type
, void *buf
, size_t nbytes
);
209 uint64_t counts
[2][2];
212 dt_stat_t instrs
= dt_stat_create("fixed_thread_self_instrs", "instructions");
213 dt_stat_t cycles
= dt_stat_create("fixed_thread_self_cycles", "cycles");
215 skip_if_unsupported();
218 while (!dt_stat_stable(instrs
) || !dt_stat_stable(cycles
)) {
221 r1
= thread_selfcounts(1, &counts
[0], sizeof(counts
[0]));
222 r2
= thread_selfcounts(1, &counts
[1], sizeof(counts
[1]));
223 T_QUIET
; T_ASSERT_POSIX_ZERO(r1
, "__thread_selfcounts");
224 T_QUIET
; T_ASSERT_POSIX_ZERO(r2
, "__thread_selfcounts");
226 T_QUIET
; T_ASSERT_GT(counts
[1][0], counts
[0][0],
227 "instructions increase monotonically");
228 dt_stat_add(instrs
, counts
[1][0] - counts
[0][0]);
230 T_QUIET
; T_ASSERT_GT(counts
[1][1], counts
[0][1],
231 "cycles increase monotonically");
232 dt_stat_add(cycles
, counts
[1][1] - counts
[0][1]);
235 dt_stat_finalize(instrs
);
236 dt_stat_finalize(cycles
);