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"
13 #include "qemu/memalign.h"
27 void (*func)(struct thread_info *);
28 struct thread_stats stats;
30 * Seed is in the range [1..UINT64_MAX], because the RNG requires
31 * a non-zero seed. To use, subtract 1 and compare against the
32 * threshold with </>=. This lets threshold = 0 never match (0% hit),
33 * and threshold = UINT64_MAX always match (100% hit).
36 bool write_op; /* writes alternate between insertions and removals */
38 } QEMU_ALIGNED(64); /* avoid false sharing among threads */
41 static QemuThread *rw_threads;
43 #define DEFAULT_RANGE (4096)
44 #define DEFAULT_QHT_N_ELEMS DEFAULT_RANGE
46 static unsigned int duration = 1;
47 static unsigned int n_rw_threads = 1;
48 static unsigned long lookup_range = DEFAULT_RANGE;
49 static unsigned long update_range = DEFAULT_RANGE;
50 static size_t init_range = DEFAULT_RANGE;
51 static size_t init_size = DEFAULT_RANGE;
52 static size_t n_ready_threads;
53 static long populate_offset;
56 static size_t resize_min;
57 static size_t resize_max;
58 static struct thread_info *rz_info;
59 static unsigned long resize_delay = 1000;
60 static double resize_rate; /* 0.0 to 1.0 */
61 static unsigned int n_rz_threads = 1;
62 static QemuThread *rz_threads;
63 static bool precompute_hash;
65 static double update_rate; /* 0.0 to 1.0 */
66 static uint64_t update_threshold;
67 static uint64_t resize_threshold;
69 static size_t qht_n_elems = DEFAULT_QHT_N_ELEMS;
72 static bool test_start;
73 static bool test_stop;
75 static struct thread_info *rw_info;
77 static const char commands_string[] =
78 " -d = duration, in seconds\n"
79 " -n = number of threads\n"
81 " -o = offset at which keys start\n"
82 " -p = precompute hashes\n"
84 " -g = set -s,-k,-K,-l,-r to the same value\n"
85 " -s = initial size hint\n"
86 " -k = initial number of keys\n"
87 " -K = initial range of keys (will be rounded up to pow2)\n"
88 " -l = lookup range of keys (will be rounded up to pow2)\n"
89 " -r = update range of keys (will be rounded up to pow2)\n"
91 " -u = update rate (0.0 to 100.0), 50/50 split of insertions/removals\n"
93 " -R = enable auto-resize\n"
94 " -S = resize rate (0.0 to 100.0)\n"
95 " -D = delay (in us) between potential resizes\n"
96 " -N = number of resize threads";
98 static void usage_complete(int argc, char *argv[])
100 fprintf(stderr, "Usage: %s [options]\n", argv[0]);
101 fprintf(stderr, "options:\n%s\n", commands_string);
105 static bool is_equal(const void *ap, const void *bp)
113 static uint32_t h(unsigned long v)
115 return qemu_xxhash2(v);
118 static uint32_t hval(unsigned long v)
123 static uint32_t (*hfunc)(unsigned long v) = h;
126 * From: https://en.wikipedia.org/wiki/Xorshift
127 * This is faster than rand_r(), and gives us a wider range (RAND_MAX is only
128 * guaranteed to be >= INT_MAX).
130 static uint64_t xorshift64star(uint64_t x)
132 x ^= x >> 12; /* a */
133 x ^= x << 25; /* b */
134 x ^= x >> 27; /* c */
135 return x * UINT64_C(2685821657736338717);
138 static void do_rz(struct thread_info *info)
140 struct thread_stats *stats = &info->stats;
141 uint64_t r = info->seed - 1;
143 if (r < resize_threshold) {
144 size_t size = info->resize_down ? resize_min : resize_max;
147 resized = qht_resize(&ht, size);
148 info->resize_down = !info->resize_down;
156 g_usleep(resize_delay);
159 static void do_rw(struct thread_info *info)
161 struct thread_stats *stats = &info->stats;
162 uint64_t r = info->seed - 1;
166 if (r >= update_threshold) {
169 p = &keys[r & (lookup_range - 1)];
171 read = qht_lookup(&ht, p, hash);
178 p = &keys[r & (update_range - 1)];
180 if (info->write_op) {
181 bool written = false;
183 if (qht_lookup(&ht, p, hash) == NULL) {
184 written = qht_insert(&ht, p, hash, NULL);
192 bool removed = false;
194 if (qht_lookup(&ht, p, hash)) {
195 removed = qht_remove(&ht, p, hash);
203 info->write_op = !info->write_op;
207 static void *thread_func(void *p)
209 struct thread_info *info = p;
211 rcu_register_thread();
213 qatomic_inc(&n_ready_threads);
214 while (!qatomic_read(&test_start)) {
219 while (!qatomic_read(&test_stop)) {
220 info->seed = xorshift64star(info->seed);
225 rcu_unregister_thread();
229 /* sets everything except info->func */
230 static void prepare_thread_info(struct thread_info *info, int i)
232 /* seed for the RNG; each thread should have a different one */
233 info->seed = (i + 1) ^ time(NULL);
234 /* the first update will be a write */
235 info->write_op = true;
236 /* the first resize will be down */
237 info->resize_down = true;
239 memset(&info->stats, 0, sizeof(info->stats));
243 th_create_n(QemuThread **threads, struct thread_info **infos, const char *name,
244 void (*func)(struct thread_info *), int offset, int n)
246 struct thread_info *info;
250 th = g_malloc(sizeof(*th) * n);
253 info = qemu_memalign(64, sizeof(*info) * n);
256 for (i = 0; i < n; i++) {
257 prepare_thread_info(&info[i], offset + i);
259 qemu_thread_create(&th[i], name, thread_func, &info[i],
260 QEMU_THREAD_JOINABLE);
264 static void create_threads(void)
266 th_create_n(&rw_threads, &rw_info, "rw", do_rw, 0, n_rw_threads);
267 th_create_n(&rz_threads, &rz_info, "rz", do_rz, n_rw_threads, n_rz_threads);
270 static void pr_params(void)
272 printf("Parameters:\n");
273 printf(" duration: %d s\n", duration);
274 printf(" # of threads: %u\n", n_rw_threads);
275 printf(" initial # of keys: %zu\n", init_size);
276 printf(" initial size hint: %zu\n", qht_n_elems);
277 printf(" auto-resize: %s\n",
278 qht_mode & QHT_MODE_AUTO_RESIZE ? "on" : "off");
280 printf(" resize_rate: %f%%\n", resize_rate * 100.0);
281 printf(" resize range: %zu-%zu\n", resize_min, resize_max);
282 printf(" # resize threads %u\n", n_rz_threads);
284 printf(" update rate: %f%%\n", update_rate * 100.0);
285 printf(" offset: %ld\n", populate_offset);
286 printf(" initial key range: %zu\n", init_range);
287 printf(" lookup range: %lu\n", lookup_range);
288 printf(" update range: %lu\n", update_range);
291 static void do_threshold(double rate, uint64_t *threshold)
294 * For 0 <= rate <= 1, scale to fit in a uint64_t.
296 * Scale by 2**64, with a special case for 1.0.
297 * The remainder of the possible values are scattered between 0
298 * and 0xfffffffffffff800 (nextafter(0x1p64, 0)).
300 * Note that we cannot simply scale by UINT64_MAX, because that
301 * value is not representable as an IEEE double value.
303 * If we scale by the next largest value, nextafter(0x1p64, 0),
304 * then the remainder of the possible values are scattered between
305 * 0 and 0xfffffffffffff000. Which leaves us with a gap between
306 * the final two inputs that is twice as large as any other.
309 *threshold = UINT64_MAX;
311 *threshold = rate * 0x1p64;
315 static void htable_init(void)
317 unsigned long n = MAX(init_range, update_range);
318 uint64_t r = time(NULL);
322 /* avoid allocating memory later by allocating all the keys now */
323 keys = g_malloc(sizeof(*keys) * n);
324 for (i = 0; i < n; i++) {
325 long val = populate_offset + i;
327 keys[i] = precompute_hash ? h(val) : hval(val);
330 /* some sanity checks */
331 g_assert_cmpuint(lookup_range, <=, n);
333 /* compute thresholds */
334 do_threshold(update_rate, &update_threshold);
335 do_threshold(resize_rate, &resize_threshold);
340 assert(resize_min < resize_max);
345 /* initialize the hash table */
346 qht_init(&ht, is_equal, qht_n_elems, qht_mode);
347 assert(init_size <= init_range);
351 fprintf(stderr, "Initialization: populating %zu items...", init_size);
352 for (i = 0; i < init_size; i++) {
357 r = xorshift64star(r);
358 p = &keys[r & (init_range - 1)];
360 if (qht_insert(&ht, p, hash, NULL)) {
366 fprintf(stderr, " populated after %zu retries\n", retries);
369 static void add_stats(struct thread_stats *s, struct thread_info *info, int n)
373 for (i = 0; i < n; i++) {
374 struct thread_stats *stats = &info[i].stats;
377 s->not_rd += stats->not_rd;
380 s->not_in += stats->not_in;
383 s->not_rm += stats->not_rm;
386 s->not_rz += stats->not_rz;
390 static void pr_stats(void)
392 struct thread_stats s = {};
395 add_stats(&s, rw_info, n_rw_threads);
396 add_stats(&s, rz_info, n_rz_threads);
398 printf("Results:\n");
401 printf(" Resizes: %zu (%.2f%% of %zu)\n",
402 s.rz, (double)s.rz / (s.rz + s.not_rz) * 100, s.rz + s.not_rz);
405 printf(" Read: %.2f M (%.2f%% of %.2fM)\n",
407 (double)s.rd / (s.rd + s.not_rd) * 100,
408 (double)(s.rd + s.not_rd) / 1e6);
409 printf(" Inserted: %.2f M (%.2f%% of %.2fM)\n",
411 (double)s.in / (s.in + s.not_in) * 100,
412 (double)(s.in + s.not_in) / 1e6);
413 printf(" Removed: %.2f M (%.2f%% of %.2fM)\n",
415 (double)s.rm / (s.rm + s.not_rm) * 100,
416 (double)(s.rm + s.not_rm) / 1e6);
418 tx = (s.rd + s.not_rd + s.in + s.not_in + s.rm + s.not_rm) / 1e6 / duration;
419 printf(" Throughput: %.2f MT/s\n", tx);
420 printf(" Throughput/thread: %.2f MT/s/thread\n", tx / n_rw_threads);
423 static void run_test(void)
427 while (qatomic_read(&n_ready_threads) != n_rw_threads + n_rz_threads) {
431 qatomic_set(&test_start, true);
432 g_usleep(duration * G_USEC_PER_SEC);
433 qatomic_set(&test_stop, true);
435 for (i = 0; i < n_rw_threads; i++) {
436 qemu_thread_join(&rw_threads[i]);
438 for (i = 0; i < n_rz_threads; i++) {
439 qemu_thread_join(&rz_threads[i]);
443 static void parse_args(int argc, char *argv[])
448 c = getopt(argc, argv, "d:D:g:k:K:l:hn:N:o:pr:Rs:S:u:");
454 duration = atoi(optarg);
457 resize_delay = atol(optarg);
460 init_range = pow2ceil(atol(optarg));
461 lookup_range = pow2ceil(atol(optarg));
462 update_range = pow2ceil(atol(optarg));
463 qht_n_elems = atol(optarg);
464 init_size = atol(optarg);
467 usage_complete(argc, argv);
470 init_size = atol(optarg);
473 init_range = pow2ceil(atol(optarg));
476 lookup_range = pow2ceil(atol(optarg));
479 n_rw_threads = atoi(optarg);
482 n_rz_threads = atoi(optarg);
485 populate_offset = atol(optarg);
488 precompute_hash = true;
492 update_range = pow2ceil(atol(optarg));
495 qht_mode |= QHT_MODE_AUTO_RESIZE;
498 qht_n_elems = atol(optarg);
501 resize_rate = atof(optarg) / 100.0;
502 if (resize_rate > 1.0) {
507 update_rate = atof(optarg) / 100.0;
508 if (update_rate > 1.0) {
516 int main(int argc, char *argv[])
518 parse_args(argc, argv);