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