libdispatch update
[platform/upstream/gcd.git] / dispatch-1.0 / testing / dispatch_timer_short.c
1 /*
2  * Copyright (c) 2010-2011 Apple Inc. All rights reserved.
3  *
4  * @APPLE_APACHE_LICENSE_HEADER_START@
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * @APPLE_APACHE_LICENSE_HEADER_END@
19  */
20
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <math.h>
26 #include <mach/mach_time.h>
27 #include <libkern/OSAtomic.h>
28
29 #include <dispatch/dispatch.h>
30
31 #include <bsdtests.h>
32 #include "dispatch_test.h"
33
34 #define delay (1ull * NSEC_PER_SEC)
35 #define interval (5ull * NSEC_PER_USEC)
36
37 #define N 100000
38
39 static dispatch_source_t t[N];
40 static dispatch_queue_t q;
41 static dispatch_group_t g;
42
43 static volatile int32_t count;
44 static mach_timebase_info_data_t tbi;
45 static uint64_t start, last;
46
47 #define elapsed_ms(x) (((now-(x))*tbi.numer/tbi.denom)/(1000ull*NSEC_PER_USEC))
48
49 void
50 test_fin(void *cxt)
51 {
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)));
55         int i;
56         for (i = 0; i < N; i++) {
57                 dispatch_source_cancel(t[i]);
58                 dispatch_release(t[i]);
59         }
60         dispatch_resume(q);
61         dispatch_release(q);
62         dispatch_release(g);
63         test_ptr("finalizer ran", cxt, cxt);
64         test_stop();
65 }
66
67 void
68 test_short_timer(void)
69 {
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);
74         int i;
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);
82                 });
83                 dispatch_resume(t[i]);
84         }
85         // Wait for registration & configuration of all timers
86         dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
87         dispatch_suspend(q);
88         for (i = 0; i < N; i++) {
89                 dispatch_resume(t[i]);
90         }
91
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();
98                 if (!count) {
99                         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay),
100                                         dispatch_get_global_queue(0, 0), ^{
101                                 dispatch_source_cancel(s);
102                                 dispatch_release(s);
103                         });
104                         fprintf(stderr, "First timer callback  (after %4llu ms)\n",
105                                         elapsed_ms(start));
106                 }
107                 OSAtomicIncrement32(&count);
108                 if (elapsed_ms(last) >= 100) {
109                         fprintf(stderr, "%5d timer callbacks (after %4llu ms)\n", count,
110                                         elapsed_ms(start));
111                         last = now;
112                 }
113         });
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();
118         dispatch_resume(s);
119 }
120
121 int
122 main(void)
123 {
124         dispatch_test_start("Dispatch Short Timer"); // <rdar://problem/7765184>
125         mach_timebase_info(&tbi);
126         test_short_timer();
127         dispatch_main();
128         return 0;
129 }