libdispatch update
[platform/upstream/gcd.git] / dispatch-1.0 / testing / dispatch_cascade.c
1 /*
2  * Copyright (c) 2008-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 <stdio.h>
22 #include <dispatch/dispatch.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25
26 #include <bsdtests.h>
27 #include "dispatch_test.h"
28
29 int done = 0;
30
31 #define QUEUES 80
32 dispatch_queue_t queues[QUEUES];
33
34 #define BLOCKS 10000
35 union {
36         size_t index;
37         char padding[64];
38 } indices[BLOCKS];
39
40 size_t iterations = QUEUES * BLOCKS * 0.25;
41
42 static void
43 noop(void *ctxt __attribute__((unused)))
44 {
45         return;
46 }
47
48 static void
49 cleanup(void *ctxt __attribute__((unused)))
50 {
51         size_t q;
52         for (q = 0; q < QUEUES; ++q) {
53                 dispatch_sync_f(queues[q], NULL, noop);
54                 dispatch_release(queues[q]);
55         }
56         test_stop();
57         exit(0);
58 }
59
60 void
61 histogram(void)
62 {
63         size_t counts[QUEUES] = {};
64         size_t maxcount = 0;
65
66         size_t q;
67         for (q = 0; q < QUEUES; ++q) {
68                 size_t i;
69                 for (i = 0; i < BLOCKS; ++i) {
70                         if (indices[i].index == q) {
71                                 ++counts[q];
72                         }
73                 }
74         }
75
76         for (q = 0; q < QUEUES; ++q) {
77                 if (counts[q] > maxcount) {
78                         maxcount = counts[q];
79                 }
80         }
81
82         printf("maxcount = %ld\n", maxcount);
83
84         size_t x,y;
85         for (y = 20; y > 0; --y) {
86                 for (x = 0; x < QUEUES; ++x) {
87                         double fraction = (double)counts[x] / (double)maxcount;
88                         double value = fraction * (double)20;
89                         printf("%s", (value > y) ? "*" : " ");
90                 }
91                 printf("\n");
92         }
93 }
94
95 void
96 cascade(void* context)
97 {
98         size_t idx, *idxptr = context;
99
100         if (done) return;
101
102         idx = *idxptr + 1;
103
104         if (idx < QUEUES) {
105                 *idxptr = idx;
106                 dispatch_async_f(queues[idx], context, cascade);
107         }
108
109         if (__sync_sub_and_fetch(&iterations, 1) == 0) {
110                 done = 1;
111                 histogram();
112                 dispatch_async_f(dispatch_get_main_queue(), NULL, cleanup);
113         }
114 }
115
116 int
117 main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
118 {
119         int i;
120
121         dispatch_test_start("Dispatch Cascade");
122
123         for (i = 0; i < QUEUES; ++i) {
124                 queues[i] = dispatch_queue_create(NULL, NULL);
125         }
126
127         for (i = 0; i < BLOCKS; ++i) {
128                 cascade(&indices[i].index);
129         }
130
131         dispatch_main();
132
133         return 0;
134 }