]> Git Repo - qemu.git/blob - tests/atomic_add-bench.c
iotests: make 083 specific to raw
[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     " -p = enable sync profiler\n"
30     " -d = duration in seconds\n"
31     " -r = range (will be rounded up to pow2)";
32
33 static void usage_complete(char *argv[])
34 {
35     fprintf(stderr, "Usage: %s [options]\n", argv[0]);
36     fprintf(stderr, "options:\n%s\n", commands_string);
37 }
38
39 /*
40  * From: https://en.wikipedia.org/wiki/Xorshift
41  * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
42  * guaranteed to be >= INT_MAX).
43  */
44 static uint64_t xorshift64star(uint64_t x)
45 {
46     x ^= x >> 12; /* a */
47     x ^= x << 25; /* b */
48     x ^= x >> 27; /* c */
49     return x * UINT64_C(2685821657736338717);
50 }
51
52 static void *thread_func(void *arg)
53 {
54     struct thread_info *info = arg;
55
56     atomic_inc(&n_ready_threads);
57     while (!atomic_read(&test_start)) {
58         cpu_relax();
59     }
60
61     while (!atomic_read(&test_stop)) {
62         unsigned int index;
63
64         info->r = xorshift64star(info->r);
65         index = info->r & (range - 1);
66         if (use_mutex) {
67             qemu_mutex_lock(&counts[index].lock);
68             counts[index].val += 1;
69             qemu_mutex_unlock(&counts[index].lock);
70         } else {
71             atomic_inc(&counts[index].val);
72         }
73     }
74     return NULL;
75 }
76
77 static void run_test(void)
78 {
79     unsigned int remaining;
80     unsigned int i;
81
82     while (atomic_read(&n_ready_threads) != n_threads) {
83         cpu_relax();
84     }
85     atomic_set(&test_start, true);
86     do {
87         remaining = sleep(duration);
88     } while (remaining);
89     atomic_set(&test_stop, true);
90
91     for (i = 0; i < n_threads; i++) {
92         qemu_thread_join(&threads[i]);
93     }
94 }
95
96 static void create_threads(void)
97 {
98     unsigned int i;
99
100     threads = g_new(QemuThread, n_threads);
101     th_info = g_new(struct thread_info, n_threads);
102     counts = qemu_memalign(64, sizeof(*counts) * range);
103     memset(counts, 0, sizeof(*counts) * range);
104     for (i = 0; i < range; i++) {
105         qemu_mutex_init(&counts[i].lock);
106     }
107
108     for (i = 0; i < n_threads; i++) {
109         struct thread_info *info = &th_info[i];
110
111         info->r = (i + 1) ^ time(NULL);
112         qemu_thread_create(&threads[i], NULL, thread_func, info,
113                            QEMU_THREAD_JOINABLE);
114     }
115 }
116
117 static void pr_params(void)
118 {
119     printf("Parameters:\n");
120     printf(" # of threads:      %u\n", n_threads);
121     printf(" duration:          %u\n", duration);
122     printf(" ops' range:        %u\n", range);
123 }
124
125 static void pr_stats(void)
126 {
127     unsigned long long val = 0;
128     unsigned int i;
129     double tx;
130
131     for (i = 0; i < range; i++) {
132         val += counts[i].val;
133     }
134     tx = val / duration / 1e6;
135
136     printf("Results:\n");
137     printf("Duration:            %u s\n", duration);
138     printf(" Throughput:         %.2f Mops/s\n", tx);
139     printf(" Throughput/thread:  %.2f Mops/s/thread\n", tx / n_threads);
140 }
141
142 static void parse_args(int argc, char *argv[])
143 {
144     int c;
145
146     for (;;) {
147         c = getopt(argc, argv, "hd:n:mpr:");
148         if (c < 0) {
149             break;
150         }
151         switch (c) {
152         case 'h':
153             usage_complete(argv);
154             exit(0);
155         case 'd':
156             duration = atoi(optarg);
157             break;
158         case 'n':
159             n_threads = atoi(optarg);
160             break;
161         case 'm':
162             use_mutex = true;
163             break;
164         case 'p':
165             qsp_enable();
166             break;
167         case 'r':
168             range = pow2ceil(atoi(optarg));
169             break;
170         }
171     }
172 }
173
174 int main(int argc, char *argv[])
175 {
176     parse_args(argc, argv);
177     pr_params();
178     create_threads();
179     run_test();
180     pr_stats();
181     return 0;
182 }
This page took 0.03408 seconds and 4 git commands to generate.