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)
79 unsigned int remaining;
82 while (atomic_read(&n_ready_threads) != n_threads) {
85 atomic_set(&test_start, true);
87 remaining = sleep(duration);
89 atomic_set(&test_stop, true);
91 for (i = 0; i < n_threads; i++) {
92 qemu_thread_join(&threads[i]);
96 static void create_threads(void)
100 threads = g_new(QemuThread, n_threads);
101 th_info = g_new(struct thread_info, n_threads);
102 counts = qemu_memalign(64, sizeof(*counts) * range);
103 memset(counts, 0, sizeof(*counts) * range);
104 for (i = 0; i < range; i++) {
105 qemu_mutex_init(&counts[i].lock);
108 for (i = 0; i < n_threads; i++) {
109 struct thread_info *info = &th_info[i];
111 info->r = (i + 1) ^ time(NULL);
112 qemu_thread_create(&threads[i], NULL, thread_func, info,
113 QEMU_THREAD_JOINABLE);
117 static void pr_params(void)
119 printf("Parameters:\n");
120 printf(" # of threads: %u\n", n_threads);
121 printf(" duration: %u\n", duration);
122 printf(" ops' range: %u\n", range);
125 static void pr_stats(void)
127 unsigned long long val = 0;
131 for (i = 0; i < range; i++) {
132 val += counts[i].val;
134 tx = val / duration / 1e6;
136 printf("Results:\n");
137 printf("Duration: %u s\n", duration);
138 printf(" Throughput: %.2f Mops/s\n", tx);
139 printf(" Throughput/thread: %.2f Mops/s/thread\n", tx / n_threads);
142 static void parse_args(int argc, char *argv[])
147 c = getopt(argc, argv, "hd:n:mpr:");
153 usage_complete(argv);
156 duration = atoi(optarg);
159 n_threads = atoi(optarg);
168 range = pow2ceil(atoi(optarg));
174 int main(int argc, char *argv[])
176 parse_args(argc, argv);