OSDN Git Service

Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-3.0-20180703' into staging
[qmiga/qemu.git] / tests / atomic_add-bench.c
1 #include "qemu/osdep.h"
2 #include "qemu/thread.h"
3 #include "qemu/host-utils.h"
4 #include "qemu/processor.h"
5
6 struct thread_info {
7     uint64_t r;
8 } QEMU_ALIGNED(64);
9
10 struct count {
11     QemuMutex lock;
12     unsigned long val;
13 } QEMU_ALIGNED(64);
14
15 static QemuThread *threads;
16 static struct thread_info *th_info;
17 static unsigned int n_threads = 1;
18 static unsigned int n_ready_threads;
19 static struct count *counts;
20 static unsigned int duration = 1;
21 static unsigned int range = 1024;
22 static bool use_mutex;
23 static bool test_start;
24 static bool test_stop;
25
26 static const char commands_string[] =
27     " -n = number of threads\n"
28     " -m = use mutexes instead of atomic increments\n"
29     " -d = duration in seconds\n"
30     " -r = range (will be rounded up to pow2)";
31
32 static void usage_complete(char *argv[])
33 {
34     fprintf(stderr, "Usage: %s [options]\n", argv[0]);
35     fprintf(stderr, "options:\n%s\n", commands_string);
36 }
37
38 /*
39  * From: https://en.wikipedia.org/wiki/Xorshift
40  * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
41  * guaranteed to be >= INT_MAX).
42  */
43 static uint64_t xorshift64star(uint64_t x)
44 {
45     x ^= x >> 12; /* a */
46     x ^= x << 25; /* b */
47     x ^= x >> 27; /* c */
48     return x * UINT64_C(2685821657736338717);
49 }
50
51 static void *thread_func(void *arg)
52 {
53     struct thread_info *info = arg;
54
55     atomic_inc(&n_ready_threads);
56     while (!atomic_read(&test_start)) {
57         cpu_relax();
58     }
59
60     while (!atomic_read(&test_stop)) {
61         unsigned int index;
62
63         info->r = xorshift64star(info->r);
64         index = info->r & (range - 1);
65         if (use_mutex) {
66             qemu_mutex_lock(&counts[index].lock);
67             counts[index].val += 1;
68             qemu_mutex_unlock(&counts[index].lock);
69         } else {
70             atomic_inc(&counts[index].val);
71         }
72     }
73     return NULL;
74 }
75
76 static void run_test(void)
77 {
78     unsigned int remaining;
79     unsigned int i;
80
81     while (atomic_read(&n_ready_threads) != n_threads) {
82         cpu_relax();
83     }
84     atomic_set(&test_start, true);
85     do {
86         remaining = sleep(duration);
87     } while (remaining);
88     atomic_set(&test_stop, true);
89
90     for (i = 0; i < n_threads; i++) {
91         qemu_thread_join(&threads[i]);
92     }
93 }
94
95 static void create_threads(void)
96 {
97     unsigned int i;
98
99     threads = g_new(QemuThread, n_threads);
100     th_info = g_new(struct thread_info, n_threads);
101     counts = qemu_memalign(64, sizeof(*counts) * range);
102     memset(counts, 0, sizeof(*counts) * range);
103     for (i = 0; i < range; i++) {
104         qemu_mutex_init(&counts[i].lock);
105     }
106
107     for (i = 0; i < n_threads; i++) {
108         struct thread_info *info = &th_info[i];
109
110         info->r = (i + 1) ^ time(NULL);
111         qemu_thread_create(&threads[i], NULL, thread_func, info,
112                            QEMU_THREAD_JOINABLE);
113     }
114 }
115
116 static void pr_params(void)
117 {
118     printf("Parameters:\n");
119     printf(" # of threads:      %u\n", n_threads);
120     printf(" duration:          %u\n", duration);
121     printf(" ops' range:        %u\n", range);
122 }
123
124 static void pr_stats(void)
125 {
126     unsigned long long val = 0;
127     unsigned int i;
128     double tx;
129
130     for (i = 0; i < range; i++) {
131         val += counts[i].val;
132     }
133     tx = val / duration / 1e6;
134
135     printf("Results:\n");
136     printf("Duration:            %u s\n", duration);
137     printf(" Throughput:         %.2f Mops/s\n", tx);
138     printf(" Throughput/thread:  %.2f Mops/s/thread\n", tx / n_threads);
139 }
140
141 static void parse_args(int argc, char *argv[])
142 {
143     int c;
144
145     for (;;) {
146         c = getopt(argc, argv, "hd:n:mr:");
147         if (c < 0) {
148             break;
149         }
150         switch (c) {
151         case 'h':
152             usage_complete(argv);
153             exit(0);
154         case 'd':
155             duration = atoi(optarg);
156             break;
157         case 'n':
158             n_threads = atoi(optarg);
159             break;
160         case 'm':
161             use_mutex = true;
162             break;
163         case 'r':
164             range = pow2ceil(atoi(optarg));
165             break;
166         }
167     }
168 }
169
170 int main(int argc, char *argv[])
171 {
172     parse_args(argc, argv);
173     pr_params();
174     create_threads();
175     run_test();
176     pr_stats();
177     return 0;
178 }