]> git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_cascade.c
libdispatch-84.5.5.tar.gz
[apple/libdispatch.git] / testing / dispatch_cascade.c
1 #include <stdio.h>
2 #include <dispatch/dispatch.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5
6 #include "dispatch_test.h"
7
8 int done = 0;
9
10 #define QUEUES 80
11 dispatch_queue_t queues[QUEUES];
12
13
14 #define BLOCKS 10000
15 union {
16 size_t index;
17 char padding[64];
18 } indices[BLOCKS];
19
20 size_t iterations = QUEUES * BLOCKS * 0.25;
21
22 void
23 histogram(void) {
24 size_t counts[QUEUES] = {};
25 size_t maxcount = 0;
26
27 size_t q;
28 for (q = 0; q < QUEUES; ++q) {
29 size_t i;
30 for (i = 0; i < BLOCKS; ++i) {
31 if (indices[i].index == q) {
32 ++counts[q];
33 }
34 }
35 }
36
37 for (q = 0; q < QUEUES; ++q) {
38 if (counts[q] > maxcount) {
39 maxcount = counts[q];
40 }
41 }
42
43 printf("maxcount = %ld\n", maxcount);
44
45 size_t x,y;
46 for (y = 20; y > 0; --y) {
47 for (x = 0; x < QUEUES; ++x) {
48 double fraction = (double)counts[x] / (double)maxcount;
49 double value = fraction * (double)20;
50 printf("%s", (value > y) ? "*" : " ");
51 }
52 printf("\n");
53 }
54 }
55
56 void
57 cascade(void* context) {
58 size_t idx, *idxptr = context;
59
60 if (done) return;
61
62 idx = *idxptr + 1;
63
64 if (idx < QUEUES) {
65 *idxptr = idx;
66 dispatch_async_f(queues[idx], context, cascade);
67 }
68
69 if (__sync_sub_and_fetch(&iterations, 1) == 0) {
70 done = 1;
71 histogram();
72 test_stop();
73 exit(0);
74 }
75 }
76
77 int
78 main(int argc __attribute__((unused)), char* argv[] __attribute__((unused))) {
79 int i;
80
81 test_start("Dispatch Cascade");
82
83 for (i = 0; i < QUEUES; ++i) {
84 queues[i] = dispatch_queue_create(NULL, NULL);
85 }
86
87 for (i = 0; i < BLOCKS; ++i) {
88 cascade(&indices[i].index);
89 }
90
91 dispatch_main();
92
93 return 0;
94 }