]>
git.proxmox.com Git - mirror_qemu.git/blob - 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"
14 static QemuThread
*threads
;
15 static struct thread_info
*th_info
;
16 static unsigned int n_threads
= 1;
17 static unsigned int n_ready_threads
;
18 static struct count
*counts
;
19 static unsigned int duration
= 1;
20 static unsigned int range
= 1024;
21 static bool test_start
;
22 static bool test_stop
;
24 static 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)";
29 static void usage_complete(char *argv
[])
31 fprintf(stderr
, "Usage: %s [options]\n", argv
[0]);
32 fprintf(stderr
, "options:\n%s\n", commands_string
);
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).
40 static uint64_t xorshift64star(uint64_t x
)
45 return x
* UINT64_C(2685821657736338717);
48 static void *thread_func(void *arg
)
50 struct thread_info
*info
= arg
;
52 atomic_inc(&n_ready_threads
);
53 while (!atomic_read(&test_start
)) {
57 while (!atomic_read(&test_stop
)) {
60 info
->r
= xorshift64star(info
->r
);
61 index
= info
->r
& (range
- 1);
62 atomic_inc(&counts
[index
].val
);
67 static void run_test(void)
69 unsigned int remaining
;
72 while (atomic_read(&n_ready_threads
) != n_threads
) {
75 atomic_set(&test_start
, true);
77 remaining
= sleep(duration
);
79 atomic_set(&test_stop
, true);
81 for (i
= 0; i
< n_threads
; i
++) {
82 qemu_thread_join(&threads
[i
]);
86 static void create_threads(void)
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
);
95 for (i
= 0; i
< n_threads
; i
++) {
96 struct thread_info
*info
= &th_info
[i
];
98 info
->r
= (i
+ 1) ^ time(NULL
);
99 qemu_thread_create(&threads
[i
], NULL
, thread_func
, info
,
100 QEMU_THREAD_JOINABLE
);
104 static void pr_params(void)
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
);
112 static void pr_stats(void)
114 unsigned long long val
= 0;
118 for (i
= 0; i
< range
; i
++) {
119 val
+= counts
[i
].val
;
121 tx
= val
/ duration
/ 1e6
;
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
);
129 static void parse_args(int argc
, char *argv
[])
134 c
= getopt(argc
, argv
, "hd:n:r:");
140 usage_complete(argv
);
143 duration
= atoi(optarg
);
146 n_threads
= atoi(optarg
);
149 range
= pow2ceil(atoi(optarg
));
155 int main(int argc
, char *argv
[])
157 parse_args(argc
, argv
);