1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 #include <rte_cycles.h>
10 #include <rte_hash_crc.h>
11 #include <rte_jhash.h>
12 #include <rte_launch.h>
13 #include <rte_malloc.h>
14 #include <rte_random.h>
15 #include <rte_spinlock.h>
19 #define RTE_RWTEST_FAIL 0
21 #define TOTAL_ENTRY (16*1024*1024)
22 #define TOTAL_INSERT (15*1024*1024)
25 unsigned int core_cnt[NUM_TEST] = {2, 4, 8};
29 uint32_t single_write;
30 uint32_t read_only[NUM_TEST];
31 uint32_t write_only[NUM_TEST];
32 uint32_t read_write_r[NUM_TEST];
33 uint32_t read_write_w[NUM_TEST];
36 static struct perf htm_results, non_htm_results;
42 uint32_t rounded_tot_insert;
46 static rte_atomic64_t gcycles;
47 static rte_atomic64_t ginsertions;
49 static rte_atomic64_t gread_cycles;
50 static rte_atomic64_t gwrite_cycles;
52 static rte_atomic64_t greads;
53 static rte_atomic64_t gwrites;
56 test_hash_readwrite_worker(__attribute__((unused)) void *arg)
59 uint32_t lcore_id = rte_lcore_id();
60 uint64_t begin, cycles;
63 offset = (lcore_id - rte_get_master_lcore())
64 * tbl_rw_test_param.num_insert;
66 printf("Core #%d inserting and reading %d: %'"PRId64" - %'"PRId64"\n",
67 lcore_id, tbl_rw_test_param.num_insert,
68 offset, offset + tbl_rw_test_param.num_insert);
70 begin = rte_rdtsc_precise();
72 for (i = offset; i < offset + tbl_rw_test_param.num_insert; i++) {
74 if (rte_hash_lookup(tbl_rw_test_param.h,
75 tbl_rw_test_param.keys + i) > 0)
78 ret = rte_hash_add_key(tbl_rw_test_param.h,
79 tbl_rw_test_param.keys + i);
83 if (rte_hash_lookup(tbl_rw_test_param.h,
84 tbl_rw_test_param.keys + i) != ret)
88 cycles = rte_rdtsc_precise() - begin;
89 rte_atomic64_add(&gcycles, cycles);
90 rte_atomic64_add(&ginsertions, i - offset);
92 for (; i < offset + tbl_rw_test_param.num_insert; i++)
93 tbl_rw_test_param.keys[i] = RTE_RWTEST_FAIL;
99 init_params(int use_htm, int use_jhash)
103 uint32_t *keys = NULL;
104 uint32_t *found = NULL;
105 struct rte_hash *handle;
107 struct rte_hash_parameters hash_params = {
108 .entries = TOTAL_ENTRY,
109 .key_len = sizeof(uint32_t),
110 .hash_func_init_val = 0,
111 .socket_id = rte_socket_id(),
114 hash_params.hash_func = rte_jhash;
116 hash_params.hash_func = rte_hash_crc;
119 hash_params.extra_flag =
120 RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT |
121 RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY;
123 hash_params.extra_flag =
124 RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY;
126 hash_params.name = "tests";
128 handle = rte_hash_create(&hash_params);
129 if (handle == NULL) {
130 printf("hash creation failed");
134 tbl_rw_test_param.h = handle;
135 keys = rte_malloc(NULL, sizeof(uint32_t) * TOTAL_ENTRY, 0);
138 printf("RTE_MALLOC failed\n");
142 found = rte_zmalloc(NULL, sizeof(uint32_t) * TOTAL_ENTRY, 0);
144 printf("RTE_ZMALLOC failed\n");
148 tbl_rw_test_param.keys = keys;
149 tbl_rw_test_param.found = found;
151 for (i = 0; i < TOTAL_ENTRY; i++)
158 rte_hash_free(handle);
164 test_hash_readwrite_functional(int use_htm)
167 const void *next_key;
171 uint32_t duplicated_keys = 0;
172 uint32_t lost_keys = 0;
175 rte_atomic64_init(&gcycles);
176 rte_atomic64_clear(&gcycles);
178 rte_atomic64_init(&ginsertions);
179 rte_atomic64_clear(&ginsertions);
181 if (init_params(use_htm, use_jhash) != 0)
184 tbl_rw_test_param.num_insert =
185 TOTAL_INSERT / rte_lcore_count();
187 tbl_rw_test_param.rounded_tot_insert =
188 tbl_rw_test_param.num_insert
191 printf("++++++++Start function tests:+++++++++\n");
193 /* Fire all threads. */
194 rte_eal_mp_remote_launch(test_hash_readwrite_worker,
196 rte_eal_mp_wait_lcore();
198 while (rte_hash_iterate(tbl_rw_test_param.h, &next_key,
199 &next_data, &iter) >= 0) {
200 /* Search for the key in the list of keys added .*/
201 i = *(const uint32_t *)next_key;
202 tbl_rw_test_param.found[i]++;
205 for (i = 0; i < tbl_rw_test_param.rounded_tot_insert; i++) {
206 if (tbl_rw_test_param.keys[i] != RTE_RWTEST_FAIL) {
207 if (tbl_rw_test_param.found[i] > 1) {
211 if (tbl_rw_test_param.found[i] == 0) {
213 printf("key %d is lost\n", i);
219 if (duplicated_keys > 0) {
220 printf("%d key duplicated\n", duplicated_keys);
225 printf("%d key lost\n", lost_keys);
229 printf("No key corrupted during read-write test.\n");
231 unsigned long long int cycles_per_insertion =
232 rte_atomic64_read(&gcycles) /
233 rte_atomic64_read(&ginsertions);
235 printf("cycles per insertion and lookup: %llu\n", cycles_per_insertion);
237 rte_free(tbl_rw_test_param.found);
238 rte_free(tbl_rw_test_param.keys);
239 rte_hash_free(tbl_rw_test_param.h);
240 printf("+++++++++Complete function tests+++++++++\n");
244 rte_free(tbl_rw_test_param.found);
245 rte_free(tbl_rw_test_param.keys);
246 rte_hash_free(tbl_rw_test_param.h);
252 test_rw_reader(__attribute__((unused)) void *arg)
255 uint64_t begin, cycles;
256 uint64_t read_cnt = (uint64_t)((uintptr_t)arg);
258 begin = rte_rdtsc_precise();
259 for (i = 0; i < read_cnt; i++) {
261 rte_hash_lookup_data(tbl_rw_test_param.h,
262 tbl_rw_test_param.keys + i,
264 if (i != (uint64_t)(uintptr_t)data) {
265 printf("lookup find wrong value %"PRIu64","
267 (uint64_t)(uintptr_t)data);
272 cycles = rte_rdtsc_precise() - begin;
273 rte_atomic64_add(&gread_cycles, cycles);
274 rte_atomic64_add(&greads, i);
279 test_rw_writer(__attribute__((unused)) void *arg)
282 uint32_t lcore_id = rte_lcore_id();
283 uint64_t begin, cycles;
285 uint64_t start_coreid = (uint64_t)(uintptr_t)arg;
288 offset = TOTAL_INSERT / 2 + (lcore_id - start_coreid)
289 * tbl_rw_test_param.num_insert;
290 begin = rte_rdtsc_precise();
291 for (i = offset; i < offset + tbl_rw_test_param.num_insert; i++) {
292 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
293 tbl_rw_test_param.keys + i,
294 (void *)((uintptr_t)i));
296 printf("writer failed %"PRIu64"\n", i);
301 cycles = rte_rdtsc_precise() - begin;
302 rte_atomic64_add(&gwrite_cycles, cycles);
303 rte_atomic64_add(&gwrites, tbl_rw_test_param.num_insert);
308 test_hash_readwrite_perf(struct perf *perf_results, int use_htm,
314 uint64_t i, read_cnt;
316 const void *next_key;
321 uint32_t duplicated_keys = 0;
322 uint32_t lost_keys = 0;
324 uint64_t start = 0, end = 0;
326 rte_atomic64_init(&greads);
327 rte_atomic64_init(&gwrites);
328 rte_atomic64_clear(&gwrites);
329 rte_atomic64_clear(&greads);
331 rte_atomic64_init(&gread_cycles);
332 rte_atomic64_clear(&gread_cycles);
333 rte_atomic64_init(&gwrite_cycles);
334 rte_atomic64_clear(&gwrite_cycles);
336 if (init_params(use_htm, use_jhash) != 0)
340 * Do a readers finish faster or writers finish faster test.
341 * When readers finish faster, we timing the readers, and when writers
342 * finish faster, we timing the writers.
343 * Divided by 10 or 2 is just experimental values to vary the workload
347 printf("++++++Start perf test: reader++++++++\n");
348 read_cnt = TOTAL_INSERT / 10;
350 printf("++++++Start perf test: writer++++++++\n");
351 read_cnt = TOTAL_INSERT / 2;
354 /* We first test single thread performance */
355 start = rte_rdtsc_precise();
356 /* Insert half of the keys */
357 for (i = 0; i < TOTAL_INSERT / 2; i++) {
358 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
359 tbl_rw_test_param.keys + i,
360 (void *)((uintptr_t)i));
362 printf("Failed to insert half of keys\n");
366 end = rte_rdtsc_precise() - start;
367 perf_results->single_write = end / i;
369 start = rte_rdtsc_precise();
371 for (i = 0; i < read_cnt; i++) {
373 rte_hash_lookup_data(tbl_rw_test_param.h,
374 tbl_rw_test_param.keys + i,
376 if (i != (uint64_t)(uintptr_t)data) {
377 printf("lookup find wrong value"
378 " %"PRIu64",%"PRIu64"\n", i,
379 (uint64_t)(uintptr_t)data);
383 end = rte_rdtsc_precise() - start;
384 perf_results->single_read = end / i;
386 for (n = 0; n < NUM_TEST; n++) {
387 unsigned int tot_lcore = rte_lcore_count();
388 if (tot_lcore < core_cnt[n] * 2 + 1)
391 rte_atomic64_clear(&greads);
392 rte_atomic64_clear(&gread_cycles);
393 rte_atomic64_clear(&gwrites);
394 rte_atomic64_clear(&gwrite_cycles);
396 rte_hash_reset(tbl_rw_test_param.h);
398 tbl_rw_test_param.num_insert = TOTAL_INSERT / 2 / core_cnt[n];
399 tbl_rw_test_param.rounded_tot_insert = TOTAL_INSERT / 2 +
400 tbl_rw_test_param.num_insert *
403 for (i = 0; i < TOTAL_INSERT / 2; i++) {
404 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
405 tbl_rw_test_param.keys + i,
406 (void *)((uintptr_t)i));
408 printf("Failed to insert half of keys\n");
413 /* Then test multiple thread case but only all reads or
417 /* Test only reader cases */
418 for (i = 1; i <= core_cnt[n]; i++)
419 rte_eal_remote_launch(test_rw_reader,
420 (void *)(uintptr_t)read_cnt, i);
422 rte_eal_mp_wait_lcore();
425 /* Test only writer cases */
426 for (; i <= core_cnt[n] * 2; i++)
427 rte_eal_remote_launch(test_rw_writer,
428 (void *)((uintptr_t)start_coreid), i);
430 rte_eal_mp_wait_lcore();
433 unsigned long long int cycles_per_insertion =
434 rte_atomic64_read(&gread_cycles) /
435 rte_atomic64_read(&greads);
436 perf_results->read_only[n] = cycles_per_insertion;
437 printf("Reader only: cycles per lookup: %llu\n",
438 cycles_per_insertion);
442 unsigned long long int cycles_per_insertion =
443 rte_atomic64_read(&gwrite_cycles) /
444 rte_atomic64_read(&gwrites);
445 perf_results->write_only[n] = cycles_per_insertion;
446 printf("Writer only: cycles per writes: %llu\n",
447 cycles_per_insertion);
450 rte_atomic64_clear(&greads);
451 rte_atomic64_clear(&gread_cycles);
452 rte_atomic64_clear(&gwrites);
453 rte_atomic64_clear(&gwrite_cycles);
455 rte_hash_reset(tbl_rw_test_param.h);
457 for (i = 0; i < TOTAL_INSERT / 2; i++) {
458 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
459 tbl_rw_test_param.keys + i,
460 (void *)((uintptr_t)i));
462 printf("Failed to insert half of keys\n");
467 start_coreid = core_cnt[n] + 1;
470 for (i = core_cnt[n] + 1; i <= core_cnt[n] * 2; i++)
471 rte_eal_remote_launch(test_rw_writer,
472 (void *)((uintptr_t)start_coreid), i);
473 for (i = 1; i <= core_cnt[n]; i++)
474 rte_eal_remote_launch(test_rw_reader,
475 (void *)(uintptr_t)read_cnt, i);
477 for (i = 1; i <= core_cnt[n]; i++)
478 rte_eal_remote_launch(test_rw_reader,
479 (void *)(uintptr_t)read_cnt, i);
480 for (; i <= core_cnt[n] * 2; i++)
481 rte_eal_remote_launch(test_rw_writer,
482 (void *)((uintptr_t)start_coreid), i);
485 rte_eal_mp_wait_lcore();
487 while (rte_hash_iterate(tbl_rw_test_param.h,
488 &next_key, &next_data, &iter) >= 0) {
489 /* Search for the key in the list of keys added .*/
490 i = *(const uint32_t *)next_key;
491 tbl_rw_test_param.found[i]++;
494 for (i = 0; i < tbl_rw_test_param.rounded_tot_insert; i++) {
495 if (tbl_rw_test_param.keys[i] != RTE_RWTEST_FAIL) {
496 if (tbl_rw_test_param.found[i] > 1) {
500 if (tbl_rw_test_param.found[i] == 0) {
502 printf("key %"PRIu64" is lost\n", i);
508 if (duplicated_keys > 0) {
509 printf("%d key duplicated\n", duplicated_keys);
514 printf("%d key lost\n", lost_keys);
518 printf("No key corrupted during read-write test.\n");
521 unsigned long long int cycles_per_insertion =
522 rte_atomic64_read(&gread_cycles) /
523 rte_atomic64_read(&greads);
524 perf_results->read_write_r[n] = cycles_per_insertion;
525 printf("Read-write cycles per lookup: %llu\n",
526 cycles_per_insertion);
530 unsigned long long int cycles_per_insertion =
531 rte_atomic64_read(&gwrite_cycles) /
532 rte_atomic64_read(&gwrites);
533 perf_results->read_write_w[n] = cycles_per_insertion;
534 printf("Read-write cycles per writes: %llu\n",
535 cycles_per_insertion);
540 rte_free(tbl_rw_test_param.found);
541 rte_free(tbl_rw_test_param.keys);
542 rte_hash_free(tbl_rw_test_param.h);
546 rte_free(tbl_rw_test_param.found);
547 rte_free(tbl_rw_test_param.keys);
548 rte_hash_free(tbl_rw_test_param.h);
555 test_hash_readwrite_main(void)
558 * Variables used to choose different tests.
559 * use_htm indicates if hardware transactional memory should be used.
560 * reader_faster indicates if the reader threads should finish earlier
561 * than writer threads. This is to timing either reader threads or
562 * writer threads for performance numbers.
564 int use_htm, reader_faster;
566 if (rte_lcore_count() == 1) {
567 printf("More than one lcore is required "
568 "to do read write test\n");
572 setlocale(LC_NUMERIC, "");
574 if (rte_tm_supported()) {
575 printf("Hardware transactional memory (lock elision) "
578 printf("Test read-write with Hardware transactional memory\n");
581 if (test_hash_readwrite_functional(use_htm) < 0)
585 if (test_hash_readwrite_perf(&htm_results, use_htm,
590 if (test_hash_readwrite_perf(&htm_results, use_htm,
594 printf("Hardware transactional memory (lock elision) "
595 "is NOT supported\n");
598 printf("Test read-write without Hardware transactional memory\n");
600 if (test_hash_readwrite_functional(use_htm) < 0)
603 if (test_hash_readwrite_perf(&non_htm_results, use_htm,
607 if (test_hash_readwrite_perf(&non_htm_results, use_htm,
611 printf("Results summary:\n");
615 printf("single read: %u\n", htm_results.single_read);
616 printf("single write: %u\n", htm_results.single_write);
617 for (i = 0; i < NUM_TEST; i++) {
618 printf("core_cnt: %u\n", core_cnt[i]);
620 printf("read only: %u\n", htm_results.read_only[i]);
621 printf("write only: %u\n", htm_results.write_only[i]);
622 printf("read-write read: %u\n", htm_results.read_write_r[i]);
623 printf("read-write write: %u\n", htm_results.read_write_w[i]);
625 printf("non HTM:\n");
626 printf("read only: %u\n", non_htm_results.read_only[i]);
627 printf("write only: %u\n", non_htm_results.write_only[i]);
628 printf("read-write read: %u\n",
629 non_htm_results.read_write_r[i]);
630 printf("read-write write: %u\n",
631 non_htm_results.read_write_w[i]);
637 REGISTER_TEST_COMMAND(hash_readwrite_autotest, test_hash_readwrite_main);