]>
Commit | Line | Data |
---|---|---|
82fdfcbe EC |
1 | /* |
2 | * Copyright (C) 2018, Emilio G. Cota <[email protected]> | |
3 | * | |
4 | * License: GNU GPL, version 2 or later. | |
5 | * See the COPYING file in the top-level directory. | |
6 | */ | |
7 | #include "qemu/osdep.h" | |
8 | #include "qemu/thread.h" | |
9 | #include "qemu/host-utils.h" | |
10 | #include "qemu/processor.h" | |
11 | ||
12 | struct thread_info { | |
13 | uint64_t r; | |
14 | uint64_t accesses; | |
15 | } QEMU_ALIGNED(64); | |
16 | ||
17 | struct count { | |
18 | int64_t i64; | |
19 | } QEMU_ALIGNED(64); | |
20 | ||
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; | |
30 | ||
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)"; | |
35 | ||
36 | static void usage_complete(char *argv[]) | |
37 | { | |
38 | fprintf(stderr, "Usage: %s [options]\n", argv[0]); | |
39 | fprintf(stderr, "options:\n%s\n", commands_string); | |
40 | } | |
41 | ||
42 | /* | |
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). | |
46 | */ | |
47 | static uint64_t xorshift64star(uint64_t x) | |
48 | { | |
49 | x ^= x >> 12; /* a */ | |
50 | x ^= x << 25; /* b */ | |
51 | x ^= x >> 27; /* c */ | |
52 | return x * UINT64_C(2685821657736338717); | |
53 | } | |
54 | ||
55 | static void *thread_func(void *arg) | |
56 | { | |
57 | struct thread_info *info = arg; | |
58 | ||
59 | atomic_inc(&n_ready_threads); | |
60 | while (!atomic_read(&test_start)) { | |
61 | cpu_relax(); | |
62 | } | |
63 | ||
64 | while (!atomic_read(&test_stop)) { | |
65 | unsigned int index; | |
66 | ||
67 | info->r = xorshift64star(info->r); | |
68 | index = info->r & (range - 1); | |
69 | atomic_read_i64(&counts[index].i64); | |
70 | info->accesses++; | |
71 | } | |
72 | return NULL; | |
73 | } | |
74 | ||
75 | static void run_test(void) | |
76 | { | |
77 | unsigned int remaining; | |
78 | unsigned int i; | |
79 | ||
80 | while (atomic_read(&n_ready_threads) != n_threads) { | |
81 | cpu_relax(); | |
82 | } | |
83 | atomic_set(&test_start, true); | |
84 | do { | |
85 | remaining = sleep(duration); | |
86 | } while (remaining); | |
87 | atomic_set(&test_stop, true); | |
88 | ||
89 | for (i = 0; i < n_threads; i++) { | |
90 | qemu_thread_join(&threads[i]); | |
91 | } | |
92 | } | |
93 | ||
94 | static void create_threads(void) | |
95 | { | |
96 | unsigned int i; | |
97 | ||
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)); | |
101 | ||
102 | for (i = 0; i < n_threads; i++) { | |
103 | struct thread_info *info = &th_info[i]; | |
104 | ||
105 | info->r = (i + 1) ^ time(NULL); | |
106 | info->accesses = 0; | |
107 | qemu_thread_create(&threads[i], NULL, thread_func, info, | |
108 | QEMU_THREAD_JOINABLE); | |
109 | } | |
110 | } | |
111 | ||
112 | static void pr_params(void) | |
113 | { | |
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); | |
118 | } | |
119 | ||
120 | static void pr_stats(void) | |
121 | { | |
122 | unsigned long long val = 0; | |
123 | double tx; | |
124 | int i; | |
125 | ||
126 | for (i = 0; i < n_threads; i++) { | |
127 | val += th_info[i].accesses; | |
128 | } | |
129 | tx = val / duration / 1e6; | |
130 | ||
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); | |
135 | } | |
136 | ||
137 | static void parse_args(int argc, char *argv[]) | |
138 | { | |
139 | int c; | |
140 | ||
141 | for (;;) { | |
142 | c = getopt(argc, argv, "hd:n:r:"); | |
143 | if (c < 0) { | |
144 | break; | |
145 | } | |
146 | switch (c) { | |
147 | case 'h': | |
148 | usage_complete(argv); | |
149 | exit(0); | |
150 | case 'd': | |
151 | duration = atoi(optarg); | |
152 | break; | |
153 | case 'n': | |
154 | n_threads = atoi(optarg); | |
155 | break; | |
156 | case 'r': | |
157 | range = pow2ceil(atoi(optarg)); | |
158 | break; | |
159 | } | |
160 | } | |
161 | } | |
162 | ||
163 | int main(int argc, char *argv[]) | |
164 | { | |
165 | parse_args(argc, argv); | |
166 | pr_params(); | |
167 | create_threads(); | |
168 | run_test(); | |
169 | pr_stats(); | |
170 | return 0; | |
171 | } |