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