Tizen 2.1 base
[platform/upstream/gcd.git] / dispatch-1.0 / testing / dispatch_priority.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 #define __DISPATCH_INDIRECT__
26 #include "src/queue_private.h"
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #if HAVE_TARGETCONDITIONALS_H
31 #include <TargetConditionals.h>
32 #endif
33
34 #include "dispatch_test.h"
35
36 int done = 0;
37
38 #define BLOCKS 128
39 #define PRIORITIES 3
40
41 #if TARGET_OS_EMBEDDED
42 #define LOOP_COUNT 2000000
43 #else
44 #define LOOP_COUNT 100000000
45 #endif
46
47 char *labels[PRIORITIES] = { "LOW", "DEFAULT", "HIGH" };
48 int priorities[PRIORITIES] = { DISPATCH_QUEUE_PRIORITY_LOW, DISPATCH_QUEUE_PRIORITY_DEFAULT, DISPATCH_QUEUE_PRIORITY_HIGH };
49
50 union {
51         size_t count;
52         char padding[64];
53 } counts[PRIORITIES];
54
55 #define ITERATIONS (size_t)(PRIORITIES * BLOCKS * 0.50)
56 size_t iterations = ITERATIONS;
57
58 void
59 histogram(void) {
60         size_t maxcount = BLOCKS;
61         size_t sc[PRIORITIES];
62         
63         size_t total = 0;
64         
65         size_t x,y;
66         for (y = 0; y < PRIORITIES; ++y) {
67                 sc[y] = counts[y].count;
68         }
69
70         for (y = 0; y < PRIORITIES; ++y) {
71                 printf("%s: %ld\n", labels[y], sc[y]);
72                 total += sc[y];
73                 
74                 double fraction = (double)sc[y] / (double)maxcount;
75                 double value = fraction * (double)80;
76                 for (x = 0; x < 80; ++x) {
77                         printf("%s", (value > x) ? "*" : " ");
78                 }
79                 printf("\n");
80         }
81         
82         test_long("blocks completed", total, ITERATIONS);
83         test_long_less_than("high priority precedence", (long)sc[0], (long)sc[2]);
84 }
85
86 void
87 cpubusy(void* context)
88 {
89         size_t *count = context;
90         size_t iterdone;
91
92         size_t idx;
93         for (idx = 0; idx < LOOP_COUNT; ++idx) {
94                 if (done) break;
95         }
96         
97         if ((iterdone = __sync_sub_and_fetch(&iterations, 1)) == 0) {
98                 __sync_add_and_fetch(&done, 1);
99                 __sync_add_and_fetch(count, 1);
100                 histogram();
101                 test_stop();
102                 exit(0);
103         } else if (iterdone > 0) {
104                 __sync_add_and_fetch(count, 1);
105         }
106 }
107
108 void
109 submit_work(dispatch_queue_t queue, void* context)
110 {
111         int i;
112
113         for (i = 0; i < BLOCKS; ++i) {
114                 dispatch_async_f(queue, context, cpubusy);
115         }
116
117 #if USE_SET_TARGET_QUEUE
118         dispatch_release(queue);
119 #endif
120 }
121
122 int
123 main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
124 {
125         dispatch_queue_t q[PRIORITIES];
126         int i;
127
128 #if USE_SET_TARGET_QUEUE
129         test_start("Dispatch Priority (Set Target Queue)");
130         for(i = 0; i < PRIORITIES; i++) {
131                 q[i] = dispatch_queue_create(labels[i], NULL);
132                 test_ptr_notnull("q[i]", q[i]);
133                 assert(q[i]);
134                 dispatch_set_target_queue(q[i], dispatch_get_global_queue(priorities[i], 0));
135                 dispatch_queue_set_width(q[i], DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS);
136         }
137 #else
138         test_start("Dispatch Priority");
139         for(i = 0; i < PRIORITIES; i++) {
140                 q[i] = dispatch_get_global_queue(priorities[i], 0);
141         }
142 #endif
143         
144         for(i = 0; i < PRIORITIES; i++) {
145                 submit_work(q[i], &counts[i].count);
146         }
147
148         dispatch_main();
149
150         return 0;
151 }