2 * Copyright (C) 2018, Emilio G. Cota <cota@braap.org>
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 qatomic_inc(&n_ready_threads);
60 while (!qatomic_read(&test_start)) {
64 while (!qatomic_read(&test_stop)) {
67 info->r = xorshift64star(info->r);
68 index = info->r & (range - 1);
69 qatomic_read_i64(&counts[index].i64);
75 static void run_test(void)
79 while (qatomic_read(&n_ready_threads) != n_threads) {
83 qatomic_set(&test_start, true);
84 g_usleep(duration * G_USEC_PER_SEC);
85 qatomic_set(&test_stop, true);
87 for (i = 0; i < n_threads; i++) {
88 qemu_thread_join(&threads[i]);
92 static void create_threads(void)
96 threads = g_new(QemuThread, n_threads);
97 th_info = g_new(struct thread_info, n_threads);
98 counts = g_malloc0_n(range, sizeof(*counts));
100 for (i = 0; i < n_threads; i++) {
101 struct thread_info *info = &th_info[i];
103 info->r = (i + 1) ^ time(NULL);
105 qemu_thread_create(&threads[i], NULL, thread_func, info,
106 QEMU_THREAD_JOINABLE);
110 static void pr_params(void)
112 printf("Parameters:\n");
113 printf(" # of threads: %u\n", n_threads);
114 printf(" duration: %u\n", duration);
115 printf(" ops' range: %u\n", range);
118 static void pr_stats(void)
120 unsigned long long val = 0;
124 for (i = 0; i < n_threads; i++) {
125 val += th_info[i].accesses;
127 tx = val / duration / 1e6;
129 printf("Results:\n");
130 printf("Duration: %u s\n", duration);
131 printf(" Throughput: %.2f Mops/s\n", tx);
132 printf(" Throughput/thread: %.2f Mops/s/thread\n", tx / n_threads);
135 static void parse_args(int argc, char *argv[])
140 c = getopt(argc, argv, "hd:n:r:");
146 usage_complete(argv);
149 duration = atoi(optarg);
152 n_threads = atoi(optarg);
155 range = pow2ceil(atoi(optarg));
161 int main(int argc, char *argv[])
163 parse_args(argc, argv);