1 #include "qemu/osdep.h"
2 #include "qemu/thread.h"
3 #include "qemu/host-utils.h"
4 #include "qemu/processor.h"
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;
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)";
33 static void usage_complete(char *argv[])
35 fprintf(stderr, "Usage: %s [options]\n", argv[0]);
36 fprintf(stderr, "options:\n%s\n", commands_string);
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).
44 static uint64_t xorshift64star(uint64_t x)
49 return x * UINT64_C(2685821657736338717);
52 static void *thread_func(void *arg)
54 struct thread_info *info = arg;
56 atomic_inc(&n_ready_threads);
57 while (!atomic_read(&test_start)) {
61 while (!atomic_read(&test_stop)) {
64 info->r = xorshift64star(info->r);
65 index = info->r & (range - 1);
67 qemu_mutex_lock(&counts[index].lock);
68 counts[index].val += 1;
69 qemu_mutex_unlock(&counts[index].lock);
71 atomic_inc(&counts[index].val);
77 static void run_test(void)
81 while (atomic_read(&n_ready_threads) != n_threads) {
85 atomic_set(&test_start, true);
86 g_usleep(duration * G_USEC_PER_SEC);
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 = qemu_memalign(64, sizeof(*counts) * range);
101 memset(counts, 0, sizeof(*counts) * range);
102 for (i = 0; i < range; i++) {
103 qemu_mutex_init(&counts[i].lock);
106 for (i = 0; i < n_threads; i++) {
107 struct thread_info *info = &th_info[i];
109 info->r = (i + 1) ^ time(NULL);
110 qemu_thread_create(&threads[i], NULL, thread_func, info,
111 QEMU_THREAD_JOINABLE);
115 static void pr_params(void)
117 printf("Parameters:\n");
118 printf(" # of threads: %u\n", n_threads);
119 printf(" duration: %u\n", duration);
120 printf(" ops' range: %u\n", range);
123 static void pr_stats(void)
125 unsigned long long val = 0;
129 for (i = 0; i < range; i++) {
130 val += counts[i].val;
132 tx = val / duration / 1e6;
134 printf("Results:\n");
135 printf("Duration: %u s\n", duration);
136 printf(" Throughput: %.2f Mops/s\n", tx);
137 printf(" Throughput/thread: %.2f Mops/s/thread\n", tx / n_threads);
140 static void parse_args(int argc, char *argv[])
145 c = getopt(argc, argv, "hd:n:mpr:");
151 usage_complete(argv);
154 duration = atoi(optarg);
157 n_threads = atoi(optarg);
166 range = pow2ceil(atoi(optarg));
172 int main(int argc, char *argv[])
174 parse_args(argc, argv);