2 * Copyright (c) 2010-2011 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
26 #include <mach/mach_time.h>
27 #include <libkern/OSAtomic.h>
29 #include <dispatch/dispatch.h>
32 #include "dispatch_test.h"
34 #define delay (1ull * NSEC_PER_SEC)
35 #define interval (5ull * NSEC_PER_USEC)
39 static dispatch_source_t t[N];
40 static dispatch_queue_t q;
41 static dispatch_group_t g;
43 static volatile int32_t count;
44 static mach_timebase_info_data_t tbi;
45 static uint64_t start, last;
47 #define elapsed_ms(x) (((now-(x))*tbi.numer/tbi.denom)/(1000ull*NSEC_PER_USEC))
52 fprintf(stderr, "Called back every %llu us on average\n",
53 (delay/count)/NSEC_PER_USEC);
54 test_long_less_than("Frequency", 1, ceil((double)delay/(count*interval)));
56 for (i = 0; i < N; i++) {
57 dispatch_source_cancel(t[i]);
58 dispatch_release(t[i]);
63 test_ptr("finalizer ran", cxt, cxt);
68 test_short_timer(void)
70 // Add a large number of timers with suspended target queue in front of
71 // the timer being measured <rdar://problem/7401353>
72 g = dispatch_group_create();
73 q = dispatch_queue_create("q", NULL);
75 for (i = 0; i < N; i++) {
76 t[i] = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, q);
77 dispatch_source_set_timer(t[i], DISPATCH_TIME_NOW, interval, 0);
78 dispatch_group_enter(g);
79 dispatch_source_set_registration_handler(t[i], ^{
80 dispatch_suspend(t[i]);
81 dispatch_group_leave(g);
83 dispatch_resume(t[i]);
85 // Wait for registration & configuration of all timers
86 dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
88 for (i = 0; i < N; i++) {
89 dispatch_resume(t[i]);
92 dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
93 0, 0, dispatch_get_global_queue(0, 0));
94 test_ptr_notnull("dispatch_source_create", s);
95 dispatch_source_set_timer(s, DISPATCH_TIME_NOW, interval, 0);
96 dispatch_source_set_event_handler(s, ^{
97 uint64_t now = mach_absolute_time();
99 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay),
100 dispatch_get_global_queue(0, 0), ^{
101 dispatch_source_cancel(s);
104 fprintf(stderr, "First timer callback (after %4llu ms)\n",
107 OSAtomicIncrement32(&count);
108 if (elapsed_ms(last) >= 100) {
109 fprintf(stderr, "%5d timer callbacks (after %4llu ms)\n", count,
114 dispatch_set_context(s, s);
115 dispatch_set_finalizer_f(s, test_fin);
116 fprintf(stderr, "Scheduling %llu us timer\n", interval/NSEC_PER_USEC);
117 start = last = mach_absolute_time();
124 dispatch_test_start("Dispatch Short Timer"); // <rdar://problem/7765184>
125 mach_timebase_info(&tbi);