]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/mktimer/mktimer_test.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tools / tests / mktimer / mktimer_test.c
1 /*
2 * Copyright (c) 2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <CoreFoundation/CoreFoundation.h>
30 #include <unistd.h>
31 #include <mach/mach.h>
32 #include <mach/mach_time.h>
33
34 /* These externs can be removed once the prototypes make it to the SDK */
35 extern mach_port_name_t mk_timer_create(void);
36 extern kern_return_t mk_timer_arm(mach_port_name_t name, uint64_t expire_time);
37
38 #define MK_TIMER_CRITICAL (1)
39 extern kern_return_t mk_timer_arm_leeway(mach_port_name_t name,
40 uint64_t mk_timer_flags,
41 uint64_t mk_timer_expire_time,
42 uint64_t mk_timer_leeway);
43
44 struct mach_timebase_info tbinfo;
45 double conversion;
46
47 mach_port_t timerPort;
48
49 uint64_t interval_abs = 1000000000;
50
51 uint32_t use_leeway = 0;
52 uint32_t report = 1000;
53
54 uint64_t on, lastfire = 0, totaljitter = 0, max_jitter = 0, min_jitter = ~0ULL, jiterations = 0, leeway_ns = 0, leeway_abs = 0;
55 uint64_t deadline;
56
57 void
58 cfmcb(CFMachPortRef port, void *msg, CFIndex size, void *msginfo)
59 {
60 uint64_t ctime = mach_absolute_time();
61 uint64_t jitter = 0;
62
63 if (deadline) {
64 jitter = (ctime - deadline);
65 if (jitter > max_jitter) {
66 max_jitter = jitter;
67 }
68
69 if (jitter < min_jitter) {
70 min_jitter = jitter;
71 }
72
73 totaljitter += jitter;
74 if ((++jiterations % report) == 0) {
75 printf("max_jitter: %g (ns), min_jitter: %g (ns), average_jitter: %g (ns)\n", max_jitter * conversion, min_jitter * conversion, ((double)totaljitter / (double)jiterations) * conversion);
76 max_jitter = 0; min_jitter = ~0ULL; jiterations = 0; totaljitter = 0;
77 }
78 }
79
80 deadline = mach_absolute_time() + interval_abs;
81
82 if (use_leeway) {
83 mk_timer_arm_leeway(timerPort, MK_TIMER_CRITICAL, deadline, leeway_abs);
84 } else {
85 mk_timer_arm(timerPort, deadline);
86 }
87 }
88
89 int
90 main(int argc, char **argv)
91 {
92 if (argc != 4) {
93 printf("Usage: mktimer_test <interval_ns> <use leeway trap> <leeway_ns>\n");
94 return 0;
95 }
96
97 on = strtoul(argv[1], NULL, 0);
98 use_leeway = strtoul(argv[2], NULL, 0);
99
100 mach_timebase_info(&tbinfo);
101 conversion = ((double)tbinfo.numer / (double) tbinfo.denom);
102
103 leeway_ns = strtoul(argv[3], NULL, 0);
104
105 leeway_abs = leeway_ns / conversion;
106 printf("Interval in ns: %llu, timebase conversion: %g, use leeway syscall: %d, leeway_ns: %llu\n", on, conversion, !!use_leeway, leeway_ns);
107
108 interval_abs = on / conversion;
109
110 uint64_t cID = 0;
111 CFMachPortContext context = (CFMachPortContext){
112 1,
113 (void *)cID,
114 NULL,
115 NULL,
116 NULL,
117 };
118
119 timerPort = mk_timer_create();
120 CFMachPortRef port = CFMachPortCreateWithPort(NULL, timerPort, cfmcb, &context, NULL);
121 CFRunLoopSourceRef eventSource = CFMachPortCreateRunLoopSource(NULL, port, -1);
122 CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
123 CFRelease(eventSource);
124
125 if (use_leeway) {
126 mk_timer_arm_leeway(timerPort, MK_TIMER_CRITICAL, mach_absolute_time() + interval_abs, leeway_abs);
127 } else {
128 mk_timer_arm(timerPort, mach_absolute_time() + interval_abs);
129 }
130
131 for (;;) {
132 CFRunLoopRun();
133 }
134 return 0;
135 }