4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
7 #include "qemu/osdep.h"
8 #include "qemu/thread.h"
9 #include "qemu/host-utils.h"
10 #include "qemu/processor.h"
21 static QemuThread *threads;
22 static struct thread_info *th_info;
23 static unsigned int n_threads = 1;
24 static unsigned int n_ready_threads;
25 static struct count *counts;
26 static unsigned int duration = 1;
27 static unsigned int range = 1024;
28 static bool test_start;
29 static bool test_stop;
31 static const char commands_string[] =
32 " -d = duration in seconds\n"
33 " -n = number of threads\n"
34 " -r = range (will be rounded up to pow2)";
36 static void usage_complete(char *argv[])
38 fprintf(stderr, "Usage: %s [options]\n", argv[0]);
39 fprintf(stderr, "options:\n%s\n", commands_string);
43 * From: https://en.wikipedia.org/wiki/Xorshift
44 * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
45 * guaranteed to be >= INT_MAX).
47 static uint64_t xorshift64star(uint64_t x)
52 return x * UINT64_C(2685821657736338717);
55 static void *thread_func(void *arg)
57 struct thread_info *info = arg;
59 atomic_inc(&n_ready_threads);
60 while (!atomic_read(&test_start)) {
64 while (!atomic_read(&test_stop)) {
67 info->r = xorshift64star(info->r);
68 index = info->r & (range - 1);
69 atomic_read_i64(&counts[index].i64);
75 static void run_test(void)
77 unsigned int remaining;
80 while (atomic_read(&n_ready_threads) != n_threads) {
83 atomic_set(&test_start, true);
85 remaining = sleep(duration);
87 atomic_set(&test_stop, true);
89 for (i = 0; i < n_threads; i++) {
90 qemu_thread_join(&threads[i]);
94 static void create_threads(void)
98 threads = g_new(QemuThread, n_threads);
99 th_info = g_new(struct thread_info, n_threads);
100 counts = g_malloc0_n(range, sizeof(*counts));
102 for (i = 0; i < n_threads; i++) {
103 struct thread_info *info = &th_info[i];
105 info->r = (i + 1) ^ time(NULL);
107 qemu_thread_create(&threads[i], NULL, thread_func, info,
108 QEMU_THREAD_JOINABLE);
112 static void pr_params(void)
114 printf("Parameters:\n");
115 printf(" # of threads: %u\n", n_threads);
116 printf(" duration: %u\n", duration);
117 printf(" ops' range: %u\n", range);
120 static void pr_stats(void)
122 unsigned long long val = 0;
126 for (i = 0; i < n_threads; i++) {
127 val += th_info[i].accesses;
129 tx = val / duration / 1e6;
131 printf("Results:\n");
132 printf("Duration: %u s\n", duration);
133 printf(" Throughput: %.2f Mops/s\n", tx);
134 printf(" Throughput/thread: %.2f Mops/s/thread\n", tx / n_threads);
137 static void parse_args(int argc, char *argv[])
142 c = getopt(argc, argv, "hd:n:r:");
148 usage_complete(argv);
151 duration = atoi(optarg);
154 n_threads = atoi(optarg);
157 range = pow2ceil(atoi(optarg));
163 int main(int argc, char *argv[])
165 parse_args(argc, argv);