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