2 * Copyright (C) 2016, 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/processor.h"
9 #include "qemu/atomic.h"
12 #include "qemu/xxhash.h"
26 void (*func
)(struct thread_info
*);
27 struct thread_stats stats
;
29 * Seed is in the range [1..UINT64_MAX], because the RNG requires
30 * a non-zero seed. To use, subtract 1 and compare against the
31 * threshold with </>=. This lets threshold = 0 never match (0% hit),
32 * and threshold = UINT64_MAX always match (100% hit).
35 bool write_op
; /* writes alternate between insertions and removals */
37 } QEMU_ALIGNED(64); /* avoid false sharing among threads */
40 static QemuThread
*rw_threads
;
42 #define DEFAULT_RANGE (4096)
43 #define DEFAULT_QHT_N_ELEMS DEFAULT_RANGE
45 static unsigned int duration
= 1;
46 static unsigned int n_rw_threads
= 1;
47 static unsigned long lookup_range
= DEFAULT_RANGE
;
48 static unsigned long update_range
= DEFAULT_RANGE
;
49 static size_t init_range
= DEFAULT_RANGE
;
50 static size_t init_size
= DEFAULT_RANGE
;
51 static size_t n_ready_threads
;
52 static long populate_offset
;
55 static size_t resize_min
;
56 static size_t resize_max
;
57 static struct thread_info
*rz_info
;
58 static unsigned long resize_delay
= 1000;
59 static double resize_rate
; /* 0.0 to 1.0 */
60 static unsigned int n_rz_threads
= 1;
61 static QemuThread
*rz_threads
;
62 static bool precompute_hash
;
64 static double update_rate
; /* 0.0 to 1.0 */
65 static uint64_t update_threshold
;
66 static uint64_t resize_threshold
;
68 static size_t qht_n_elems
= DEFAULT_QHT_N_ELEMS
;
71 static bool test_start
;
72 static bool test_stop
;
74 static struct thread_info
*rw_info
;
76 static const char commands_string
[] =
77 " -d = duration, in seconds\n"
78 " -n = number of threads\n"
80 " -o = offset at which keys start\n"
81 " -p = precompute hashes\n"
83 " -g = set -s,-k,-K,-l,-r to the same value\n"
84 " -s = initial size hint\n"
85 " -k = initial number of keys\n"
86 " -K = initial range of keys (will be rounded up to pow2)\n"
87 " -l = lookup range of keys (will be rounded up to pow2)\n"
88 " -r = update range of keys (will be rounded up to pow2)\n"
90 " -u = update rate (0.0 to 100.0), 50/50 split of insertions/removals\n"
92 " -R = enable auto-resize\n"
93 " -S = resize rate (0.0 to 100.0)\n"
94 " -D = delay (in us) between potential resizes\n"
95 " -N = number of resize threads";
97 static void usage_complete(int argc
, char *argv
[])
99 fprintf(stderr
, "Usage: %s [options]\n", argv
[0]);
100 fprintf(stderr
, "options:\n%s\n", commands_string
);
104 static bool is_equal(const void *ap
, const void *bp
)
112 static uint32_t h(unsigned long v
)
114 return qemu_xxhash2(v
);
117 static uint32_t hval(unsigned long v
)
122 static uint32_t (*hfunc
)(unsigned long v
) = h
;
125 * From: https://en.wikipedia.org/wiki/Xorshift
126 * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
127 * guaranteed to be >= INT_MAX).
129 static uint64_t xorshift64star(uint64_t x
)
131 x
^= x
>> 12; /* a */
132 x
^= x
<< 25; /* b */
133 x
^= x
>> 27; /* c */
134 return x
* UINT64_C(2685821657736338717);
137 static void do_rz(struct thread_info
*info
)
139 struct thread_stats
*stats
= &info
->stats
;
140 uint64_t r
= info
->seed
- 1;
142 if (r
< resize_threshold
) {
143 size_t size
= info
->resize_down
? resize_min
: resize_max
;
146 resized
= qht_resize(&ht
, size
);
147 info
->resize_down
= !info
->resize_down
;
155 g_usleep(resize_delay
);
158 static void do_rw(struct thread_info
*info
)
160 struct thread_stats
*stats
= &info
->stats
;
161 uint64_t r
= info
->seed
- 1;
165 if (r
>= update_threshold
) {
168 p
= &keys
[r
& (lookup_range
- 1)];
170 read
= qht_lookup(&ht
, p
, hash
);
177 p
= &keys
[r
& (update_range
- 1)];
179 if (info
->write_op
) {
180 bool written
= false;
182 if (qht_lookup(&ht
, p
, hash
) == NULL
) {
183 written
= qht_insert(&ht
, p
, hash
, NULL
);
191 bool removed
= false;
193 if (qht_lookup(&ht
, p
, hash
)) {
194 removed
= qht_remove(&ht
, p
, hash
);
202 info
->write_op
= !info
->write_op
;
206 static void *thread_func(void *p
)
208 struct thread_info
*info
= p
;
210 rcu_register_thread();
212 atomic_inc(&n_ready_threads
);
213 while (!atomic_read(&test_start
)) {
218 while (!atomic_read(&test_stop
)) {
219 info
->seed
= xorshift64star(info
->seed
);
224 rcu_unregister_thread();
228 /* sets everything except info->func */
229 static void prepare_thread_info(struct thread_info
*info
, int i
)
231 /* seed for the RNG; each thread should have a different one */
232 info
->seed
= (i
+ 1) ^ time(NULL
);
233 /* the first update will be a write */
234 info
->write_op
= true;
235 /* the first resize will be down */
236 info
->resize_down
= true;
238 memset(&info
->stats
, 0, sizeof(info
->stats
));
242 th_create_n(QemuThread
**threads
, struct thread_info
**infos
, const char *name
,
243 void (*func
)(struct thread_info
*), int offset
, int n
)
245 struct thread_info
*info
;
249 th
= g_malloc(sizeof(*th
) * n
);
252 info
= qemu_memalign(64, sizeof(*info
) * n
);
255 for (i
= 0; i
< n
; i
++) {
256 prepare_thread_info(&info
[i
], offset
+ i
);
258 qemu_thread_create(&th
[i
], name
, thread_func
, &info
[i
],
259 QEMU_THREAD_JOINABLE
);
263 static void create_threads(void)
265 th_create_n(&rw_threads
, &rw_info
, "rw", do_rw
, 0, n_rw_threads
);
266 th_create_n(&rz_threads
, &rz_info
, "rz", do_rz
, n_rw_threads
, n_rz_threads
);
269 static void pr_params(void)
271 printf("Parameters:\n");
272 printf(" duration: %d s\n", duration
);
273 printf(" # of threads: %u\n", n_rw_threads
);
274 printf(" initial # of keys: %zu\n", init_size
);
275 printf(" initial size hint: %zu\n", qht_n_elems
);
276 printf(" auto-resize: %s\n",
277 qht_mode
& QHT_MODE_AUTO_RESIZE
? "on" : "off");
279 printf(" resize_rate: %f%%\n", resize_rate
* 100.0);
280 printf(" resize range: %zu-%zu\n", resize_min
, resize_max
);
281 printf(" # resize threads %u\n", n_rz_threads
);
283 printf(" update rate: %f%%\n", update_rate
* 100.0);
284 printf(" offset: %ld\n", populate_offset
);
285 printf(" initial key range: %zu\n", init_range
);
286 printf(" lookup range: %lu\n", lookup_range
);
287 printf(" update range: %lu\n", update_range
);
290 static void do_threshold(double rate
, uint64_t *threshold
)
293 * For 0 <= rate <= 1, scale to fit in a uint64_t.
295 * Scale by 2**64, with a special case for 1.0.
296 * The remainder of the possible values are scattered between 0
297 * and 0xfffffffffffff800 (nextafter(0x1p64, 0)).
299 * Note that we cannot simply scale by UINT64_MAX, because that
300 * value is not representable as an IEEE double value.
302 * If we scale by the next largest value, nextafter(0x1p64, 0),
303 * then the remainder of the possible values are scattered between
304 * 0 and 0xfffffffffffff000. Which leaves us with a gap between
305 * the final two inputs that is twice as large as any other.
308 *threshold
= UINT64_MAX
;
310 *threshold
= rate
* 0x1p
64;
314 static void htable_init(void)
316 unsigned long n
= MAX(init_range
, update_range
);
317 uint64_t r
= time(NULL
);
321 /* avoid allocating memory later by allocating all the keys now */
322 keys
= g_malloc(sizeof(*keys
) * n
);
323 for (i
= 0; i
< n
; i
++) {
324 long val
= populate_offset
+ i
;
326 keys
[i
] = precompute_hash
? h(val
) : hval(val
);
329 /* some sanity checks */
330 g_assert_cmpuint(lookup_range
, <=, n
);
332 /* compute thresholds */
333 do_threshold(update_rate
, &update_threshold
);
334 do_threshold(resize_rate
, &resize_threshold
);
339 assert(resize_min
< resize_max
);
344 /* initialize the hash table */
345 qht_init(&ht
, is_equal
, qht_n_elems
, qht_mode
);
346 assert(init_size
<= init_range
);
350 fprintf(stderr
, "Initialization: populating %zu items...", init_size
);
351 for (i
= 0; i
< init_size
; i
++) {
356 r
= xorshift64star(r
);
357 p
= &keys
[r
& (init_range
- 1)];
359 if (qht_insert(&ht
, p
, hash
, NULL
)) {
365 fprintf(stderr
, " populated after %zu retries\n", retries
);
368 static void add_stats(struct thread_stats
*s
, struct thread_info
*info
, int n
)
372 for (i
= 0; i
< n
; i
++) {
373 struct thread_stats
*stats
= &info
[i
].stats
;
376 s
->not_rd
+= stats
->not_rd
;
379 s
->not_in
+= stats
->not_in
;
382 s
->not_rm
+= stats
->not_rm
;
385 s
->not_rz
+= stats
->not_rz
;
389 static void pr_stats(void)
391 struct thread_stats s
= {};
394 add_stats(&s
, rw_info
, n_rw_threads
);
395 add_stats(&s
, rz_info
, n_rz_threads
);
397 printf("Results:\n");
400 printf(" Resizes: %zu (%.2f%% of %zu)\n",
401 s
.rz
, (double)s
.rz
/ (s
.rz
+ s
.not_rz
) * 100, s
.rz
+ s
.not_rz
);
404 printf(" Read: %.2f M (%.2f%% of %.2fM)\n",
406 (double)s
.rd
/ (s
.rd
+ s
.not_rd
) * 100,
407 (double)(s
.rd
+ s
.not_rd
) / 1e6
);
408 printf(" Inserted: %.2f M (%.2f%% of %.2fM)\n",
410 (double)s
.in
/ (s
.in
+ s
.not_in
) * 100,
411 (double)(s
.in
+ s
.not_in
) / 1e6
);
412 printf(" Removed: %.2f M (%.2f%% of %.2fM)\n",
414 (double)s
.rm
/ (s
.rm
+ s
.not_rm
) * 100,
415 (double)(s
.rm
+ s
.not_rm
) / 1e6
);
417 tx
= (s
.rd
+ s
.not_rd
+ s
.in
+ s
.not_in
+ s
.rm
+ s
.not_rm
) / 1e6
/ duration
;
418 printf(" Throughput: %.2f MT/s\n", tx
);
419 printf(" Throughput/thread: %.2f MT/s/thread\n", tx
/ n_rw_threads
);
422 static void run_test(void)
426 while (atomic_read(&n_ready_threads
) != n_rw_threads
+ n_rz_threads
) {
430 atomic_set(&test_start
, true);
431 g_usleep(duration
* G_USEC_PER_SEC
);
432 atomic_set(&test_stop
, true);
434 for (i
= 0; i
< n_rw_threads
; i
++) {
435 qemu_thread_join(&rw_threads
[i
]);
437 for (i
= 0; i
< n_rz_threads
; i
++) {
438 qemu_thread_join(&rz_threads
[i
]);
442 static void parse_args(int argc
, char *argv
[])
447 c
= getopt(argc
, argv
, "d:D:g:k:K:l:hn:N:o:pr:Rs:S:u:");
453 duration
= atoi(optarg
);
456 resize_delay
= atol(optarg
);
459 init_range
= pow2ceil(atol(optarg
));
460 lookup_range
= pow2ceil(atol(optarg
));
461 update_range
= pow2ceil(atol(optarg
));
462 qht_n_elems
= atol(optarg
);
463 init_size
= atol(optarg
);
466 usage_complete(argc
, argv
);
469 init_size
= atol(optarg
);
472 init_range
= pow2ceil(atol(optarg
));
475 lookup_range
= pow2ceil(atol(optarg
));
478 n_rw_threads
= atoi(optarg
);
481 n_rz_threads
= atoi(optarg
);
484 populate_offset
= atol(optarg
);
487 precompute_hash
= true;
491 update_range
= pow2ceil(atol(optarg
));
494 qht_mode
|= QHT_MODE_AUTO_RESIZE
;
497 qht_n_elems
= atol(optarg
);
500 resize_rate
= atof(optarg
) / 100.0;
501 if (resize_rate
> 1.0) {
506 update_rate
= atof(optarg
) / 100.0;
507 if (update_rate
> 1.0) {
515 int main(int argc
, char *argv
[])
517 parse_args(argc
, argv
);