]> git.saurik.com Git - apple/xnu.git/blame - tests/monotonic_core.c
xnu-4903.221.2.tar.gz
[apple/xnu.git] / tests / monotonic_core.c
CommitLineData
5ba3f43e
A
1/*
2 * Must come before including darwintest.h
3 */
4#ifdef T_NAMESPACE
5#undef T_NAMESPACE
6#endif /* defined(T_NAMESPACE) */
7
8#include <darwintest.h>
9#include <fcntl.h>
10#include <inttypes.h>
11#ifndef PRIVATE
12/*
13 * Need new CPU families.
14 */
15#define PRIVATE
16#include <mach/machine.h>
17#undef PRIVATE
18#else /* !defined(PRIVATE) */
19#include <mach/machine.h>
20#endif /* defined(PRIVATE) */
21#include <ktrace.h>
22#include <mach/mach.h>
23#include <stdint.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>
29#include <unistd.h>
30
31T_GLOBAL_META(
32 T_META_NAMESPACE("xnu.monotonic"),
33 T_META_CHECK_LEAKS(false)
34);
35
36static void
37skip_if_unsupported(void)
38{
39 int r;
40 int supported = 0;
41 size_t supported_size = sizeof(supported);
42
43 r = sysctlbyname("kern.monotonic.supported", &supported, &supported_size,
44 NULL, 0);
45 if (r < 0) {
46 T_WITH_ERRNO;
47 T_SKIP("could not find \"kern.monotonic.supported\" sysctl");
48 }
49
50 if (!supported) {
51 T_SKIP("monotonic is not supported on this platform");
52 }
53}
54
55static void
56check_fixed_counts(uint64_t counts[2][2])
57{
58 T_QUIET;
59 T_EXPECT_GT(counts[0][0], UINT64_C(0), "instructions are larger than 0");
60 T_QUIET;
61 T_EXPECT_GT(counts[0][1], UINT64_C(0), "cycles are larger than 0");
62
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");
65}
66
67T_DECL(core_fixed_thread_self, "check the current thread's fixed counters",
68 T_META_ASROOT(true))
69{
70 int err;
71 extern int thread_selfcounts(int type, void *buf, size_t nbytes);
72 uint64_t counts[2][2];
73
74 T_SETUPBEGIN;
75 skip_if_unsupported();
76 T_SETUPEND;
77
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");
82
83 check_fixed_counts(counts);
84}
85
86T_DECL(core_fixed_task, "check that task counting is working",
87 T_META_ASROOT(true))
88{
89 task_t task = mach_task_self();
90 kern_return_t kr;
91 mach_msg_type_number_t size = TASK_INSPECT_BASIC_COUNTS_COUNT;
92 uint64_t counts[2][2];
93
94 skip_if_unsupported();
95
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 ...)");
100
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 ...)");
106
107 check_fixed_counts(counts);
108}
109
110T_DECL(core_fixed_kdebug, "check that the kdebug macros for monotonic work",
111 T_META_ASROOT(true))
112{
113 __block bool saw_events = false;
114 ktrace_session_t s;
115 int r;
116 int set = 1;
117
118 T_SETUPBEGIN;
119 skip_if_unsupported();
120
121 s = ktrace_session_create();
122 T_QUIET; T_ASSERT_NOTNULL(s, "ktrace_session_create");
123
124 ktrace_events_single_paired(s,
125 KDBG_EVENTID(DBG_MONOTONIC, DBG_MT_TMPCPU, 0x3fff),
126 ^(struct trace_point *start, struct trace_point *end)
127 {
128 uint64_t counts[2][2];
129
130 saw_events = true;
131
132 counts[0][0] = start->arg1;
133 counts[0][1] = start->arg2;
134 counts[1][0] = end->arg1;
135 counts[1][1] = end->arg2;
136
137 check_fixed_counts(counts);
138 });
139
140 ktrace_set_completion_handler(s, ^{
141 T_ASSERT_TRUE(saw_events, "should see monotonic kdebug events");
142 T_END;
143 });
144 T_SETUPEND;
145
146 T_ASSERT_POSIX_ZERO(ktrace_start(s,
147 dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)), NULL);
148
149 r = sysctlbyname("kern.monotonic.kdebug_test", NULL, NULL, &set,
150 sizeof(set));
151 T_ASSERT_POSIX_SUCCESS(r,
152 "sysctlbyname(\"kern.monotonic.kdebug_test\", ...)");
153
154 ktrace_end(s, 0);
155 dispatch_main();
156}
157
158static void
159perf_sysctl_deltas(const char *sysctl_name, const char *stat_name)
160{
161 uint64_t deltas[2];
162 size_t deltas_size;
163 int r;
164
165 T_SETUPBEGIN;
166 skip_if_unsupported();
167
168 dt_stat_t instrs = dt_stat_create("instructions", "%s_instrs",
169 stat_name);
170 dt_stat_t cycles = dt_stat_create("cycles", "%s_cycles", stat_name);
171 T_SETUPEND;
172
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);
176 T_QUIET;
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]);
180 }
181
182 dt_stat_finalize(instrs);
183 dt_stat_finalize(cycles);
184}
185
186T_DECL(perf_core_fixed_cpu, "test the performance of fixed CPU counter access",
d9a64523 187 T_META_ASROOT(true), T_META_TAG_PERF)
5ba3f43e
A
188{
189 perf_sysctl_deltas("kern.monotonic.fixed_cpu_perf", "fixed_cpu_counters");
190}
191
192T_DECL(perf_core_fixed_thread, "test the performance of fixed thread counter access",
d9a64523 193 T_META_ASROOT(true), T_META_TAG_PERF)
5ba3f43e
A
194{
195 perf_sysctl_deltas("kern.monotonic.fixed_thread_perf",
196 "fixed_thread_counters");
197}
198
199T_DECL(perf_core_fixed_task, "test the performance of fixed task counter access",
d9a64523 200 T_META_ASROOT(true), T_META_TAG_PERF)
5ba3f43e
A
201{
202 perf_sysctl_deltas("kern.monotonic.fixed_task_perf", "fixed_task_counters");
203}
204
d9a64523
A
205T_DECL(perf_core_fixed_thread_self, "test the performance of thread self counts",
206 T_META_TAG_PERF)
5ba3f43e
A
207{
208 extern int thread_selfcounts(int type, void *buf, size_t nbytes);
209 uint64_t counts[2][2];
210
211 T_SETUPBEGIN;
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");
214
215 skip_if_unsupported();
216 T_SETUPEND;
217
218 while (!dt_stat_stable(instrs) || !dt_stat_stable(cycles)) {
219 int r1, r2;
220
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");
225
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]);
229
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]);
233 }
234
235 dt_stat_finalize(instrs);
236 dt_stat_finalize(cycles);
237}