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 (5*1024*1024)
22 #define TOTAL_INSERT (4.5*1024*1024)
23 #define TOTAL_INSERT_EXT (5*1024*1024)
26 unsigned int core_cnt[NUM_TEST] = {2, 4, 8};
28 unsigned int slave_core_ids[RTE_MAX_LCORE];
31 uint32_t single_write;
32 uint32_t read_only[NUM_TEST];
33 uint32_t write_only[NUM_TEST];
34 uint32_t read_write_r[NUM_TEST];
35 uint32_t read_write_w[NUM_TEST];
38 static struct perf htm_results, non_htm_results;
44 uint32_t rounded_tot_insert;
48 static rte_atomic64_t gcycles;
49 static rte_atomic64_t ginsertions;
51 static rte_atomic64_t gread_cycles;
52 static rte_atomic64_t gwrite_cycles;
54 static rte_atomic64_t greads;
55 static rte_atomic64_t gwrites;
58 test_hash_readwrite_worker(__rte_unused void *arg)
61 uint32_t lcore_id = rte_lcore_id();
62 uint64_t begin, cycles;
65 ret = rte_malloc(NULL, sizeof(int) *
66 tbl_rw_test_param.num_insert, 0);
67 for (i = 0; i < rte_lcore_count(); i++) {
68 if (slave_core_ids[i] == lcore_id)
71 offset = tbl_rw_test_param.num_insert * i;
73 printf("Core #%d inserting and reading %d: %'"PRId64" - %'"PRId64"\n",
74 lcore_id, tbl_rw_test_param.num_insert,
75 offset, offset + tbl_rw_test_param.num_insert - 1);
77 begin = rte_rdtsc_precise();
79 for (i = offset; i < offset + tbl_rw_test_param.num_insert; i++) {
81 if (rte_hash_lookup(tbl_rw_test_param.h,
82 tbl_rw_test_param.keys + i) > 0)
85 ret[i - offset] = rte_hash_add_key(tbl_rw_test_param.h,
86 tbl_rw_test_param.keys + i);
87 if (ret[i - offset] < 0)
90 /* lookup a random key */
91 uint32_t rand = rte_rand() % (i + 1 - offset);
93 if (rte_hash_lookup(tbl_rw_test_param.h,
94 tbl_rw_test_param.keys + rand) != ret[rand])
98 if (rte_hash_del_key(tbl_rw_test_param.h,
99 tbl_rw_test_param.keys + rand) != ret[rand])
102 ret[rand] = rte_hash_add_key(tbl_rw_test_param.h,
103 tbl_rw_test_param.keys + rand);
107 if (rte_hash_lookup(tbl_rw_test_param.h,
108 tbl_rw_test_param.keys + rand) != ret[rand])
112 cycles = rte_rdtsc_precise() - begin;
113 rte_atomic64_add(&gcycles, cycles);
114 rte_atomic64_add(&ginsertions, i - offset);
116 for (; i < offset + tbl_rw_test_param.num_insert; i++)
117 tbl_rw_test_param.keys[i] = RTE_RWTEST_FAIL;
124 init_params(int use_ext, int use_htm, int rw_lf, int use_jhash)
128 uint32_t *keys = NULL;
129 uint8_t *found = NULL;
130 struct rte_hash *handle;
132 struct rte_hash_parameters hash_params = {
133 .entries = TOTAL_ENTRY,
134 .key_len = sizeof(uint32_t),
135 .hash_func_init_val = 0,
136 .socket_id = rte_socket_id(),
139 hash_params.hash_func = rte_jhash;
141 hash_params.hash_func = rte_hash_crc;
143 hash_params.extra_flag = RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD;
145 hash_params.extra_flag |=
146 RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT;
148 hash_params.extra_flag |=
149 RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF;
151 hash_params.extra_flag |=
152 RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY;
155 hash_params.extra_flag |=
156 RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
158 hash_params.extra_flag &=
159 ~RTE_HASH_EXTRA_FLAGS_EXT_TABLE;
161 hash_params.name = "tests";
163 handle = rte_hash_create(&hash_params);
164 if (handle == NULL) {
165 printf("hash creation failed");
169 tbl_rw_test_param.h = handle;
170 keys = rte_malloc(NULL, sizeof(uint32_t) * TOTAL_ENTRY, 0);
173 printf("RTE_MALLOC failed\n");
177 found = rte_zmalloc(NULL, sizeof(uint8_t) * TOTAL_ENTRY, 0);
179 printf("RTE_ZMALLOC failed\n");
183 tbl_rw_test_param.keys = keys;
184 tbl_rw_test_param.found = found;
186 for (i = 0; i < TOTAL_ENTRY; i++)
193 rte_hash_free(handle);
199 test_hash_readwrite_functional(int use_htm, int use_rw_lf, int use_ext)
202 const void *next_key;
206 uint32_t duplicated_keys = 0;
207 uint32_t lost_keys = 0;
209 int slave_cnt = rte_lcore_count() - 1;
210 uint32_t tot_insert = 0;
212 rte_atomic64_init(&gcycles);
213 rte_atomic64_clear(&gcycles);
215 rte_atomic64_init(&ginsertions);
216 rte_atomic64_clear(&ginsertions);
218 if (init_params(use_ext, use_htm, use_rw_lf, use_jhash) != 0)
222 tot_insert = TOTAL_INSERT_EXT;
224 tot_insert = TOTAL_INSERT;
226 tbl_rw_test_param.num_insert =
227 tot_insert / slave_cnt;
229 tbl_rw_test_param.rounded_tot_insert =
230 tbl_rw_test_param.num_insert
233 printf("\nHTM = %d, RW-LF = %d, EXT-Table = %d\n",
234 use_htm, use_rw_lf, use_ext);
235 printf("++++++++Start function tests:+++++++++\n");
237 /* Fire all threads. */
238 rte_eal_mp_remote_launch(test_hash_readwrite_worker,
240 rte_eal_mp_wait_lcore();
242 while (rte_hash_iterate(tbl_rw_test_param.h, &next_key,
243 &next_data, &iter) >= 0) {
244 /* Search for the key in the list of keys added .*/
245 i = *(const uint32_t *)next_key;
246 tbl_rw_test_param.found[i]++;
249 for (i = 0; i < tbl_rw_test_param.rounded_tot_insert; i++) {
250 if (tbl_rw_test_param.keys[i] != RTE_RWTEST_FAIL) {
251 if (tbl_rw_test_param.found[i] > 1) {
255 if (tbl_rw_test_param.found[i] == 0) {
257 printf("key %d is lost\n", i);
263 if (duplicated_keys > 0) {
264 printf("%d key duplicated\n", duplicated_keys);
269 printf("%d key lost\n", lost_keys);
273 printf("No key corrupted during read-write test.\n");
275 unsigned long long int cycles_per_insertion =
276 rte_atomic64_read(&gcycles) /
277 rte_atomic64_read(&ginsertions);
279 printf("cycles per insertion and lookup: %llu\n", cycles_per_insertion);
281 rte_free(tbl_rw_test_param.found);
282 rte_free(tbl_rw_test_param.keys);
283 rte_hash_free(tbl_rw_test_param.h);
284 printf("+++++++++Complete function tests+++++++++\n");
288 rte_free(tbl_rw_test_param.found);
289 rte_free(tbl_rw_test_param.keys);
290 rte_hash_free(tbl_rw_test_param.h);
296 test_rw_reader(void *arg)
299 uint64_t begin, cycles;
300 uint64_t read_cnt = (uint64_t)((uintptr_t)arg);
302 begin = rte_rdtsc_precise();
303 for (i = 0; i < read_cnt; i++) {
305 rte_hash_lookup_data(tbl_rw_test_param.h,
306 tbl_rw_test_param.keys + i,
308 if (i != (uint64_t)(uintptr_t)data) {
309 printf("lookup find wrong value %"PRIu64","
311 (uint64_t)(uintptr_t)data);
316 cycles = rte_rdtsc_precise() - begin;
317 rte_atomic64_add(&gread_cycles, cycles);
318 rte_atomic64_add(&greads, i);
323 test_rw_writer(void *arg)
326 uint32_t lcore_id = rte_lcore_id();
327 uint64_t begin, cycles;
329 uint64_t start_coreid = (uint64_t)(uintptr_t)arg;
332 for (i = 0; i < rte_lcore_count(); i++) {
333 if (slave_core_ids[i] == lcore_id)
337 offset = TOTAL_INSERT / 2 + (i - (start_coreid)) *
338 tbl_rw_test_param.num_insert;
339 begin = rte_rdtsc_precise();
340 for (i = offset; i < offset + tbl_rw_test_param.num_insert; i++) {
341 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
342 tbl_rw_test_param.keys + i,
343 (void *)((uintptr_t)i));
345 printf("writer failed %"PRIu64"\n", i);
350 cycles = rte_rdtsc_precise() - begin;
351 rte_atomic64_add(&gwrite_cycles, cycles);
352 rte_atomic64_add(&gwrites, tbl_rw_test_param.num_insert);
357 test_hash_readwrite_perf(struct perf *perf_results, int use_htm,
363 uint64_t i, read_cnt;
365 const void *next_key;
370 uint32_t duplicated_keys = 0;
371 uint32_t lost_keys = 0;
373 uint64_t start = 0, end = 0;
375 rte_atomic64_init(&greads);
376 rte_atomic64_init(&gwrites);
377 rte_atomic64_clear(&gwrites);
378 rte_atomic64_clear(&greads);
380 rte_atomic64_init(&gread_cycles);
381 rte_atomic64_clear(&gread_cycles);
382 rte_atomic64_init(&gwrite_cycles);
383 rte_atomic64_clear(&gwrite_cycles);
385 if (init_params(0, use_htm, 0, use_jhash) != 0)
389 * Do a readers finish faster or writers finish faster test.
390 * When readers finish faster, we timing the readers, and when writers
391 * finish faster, we timing the writers.
392 * Divided by 10 or 2 is just experimental values to vary the workload
396 printf("++++++Start perf test: reader++++++++\n");
397 read_cnt = TOTAL_INSERT / 10;
399 printf("++++++Start perf test: writer++++++++\n");
400 read_cnt = TOTAL_INSERT / 2;
403 /* We first test single thread performance */
404 start = rte_rdtsc_precise();
405 /* Insert half of the keys */
406 for (i = 0; i < TOTAL_INSERT / 2; i++) {
407 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
408 tbl_rw_test_param.keys + i,
409 (void *)((uintptr_t)i));
411 printf("Failed to insert half of keys\n");
415 end = rte_rdtsc_precise() - start;
416 perf_results->single_write = end / i;
418 start = rte_rdtsc_precise();
420 for (i = 0; i < read_cnt; i++) {
422 rte_hash_lookup_data(tbl_rw_test_param.h,
423 tbl_rw_test_param.keys + i,
425 if (i != (uint64_t)(uintptr_t)data) {
426 printf("lookup find wrong value"
427 " %"PRIu64",%"PRIu64"\n", i,
428 (uint64_t)(uintptr_t)data);
432 end = rte_rdtsc_precise() - start;
433 perf_results->single_read = end / i;
435 for (n = 0; n < NUM_TEST; n++) {
436 unsigned int tot_slave_lcore = rte_lcore_count() - 1;
437 if (tot_slave_lcore < core_cnt[n] * 2)
440 rte_atomic64_clear(&greads);
441 rte_atomic64_clear(&gread_cycles);
442 rte_atomic64_clear(&gwrites);
443 rte_atomic64_clear(&gwrite_cycles);
445 rte_hash_reset(tbl_rw_test_param.h);
447 tbl_rw_test_param.num_insert = TOTAL_INSERT / 2 / core_cnt[n];
448 tbl_rw_test_param.rounded_tot_insert = TOTAL_INSERT / 2 +
449 tbl_rw_test_param.num_insert *
452 for (i = 0; i < TOTAL_INSERT / 2; i++) {
453 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
454 tbl_rw_test_param.keys + i,
455 (void *)((uintptr_t)i));
457 printf("Failed to insert half of keys\n");
462 /* Then test multiple thread case but only all reads or
466 /* Test only reader cases */
467 for (i = 0; i < core_cnt[n]; i++)
468 rte_eal_remote_launch(test_rw_reader,
469 (void *)(uintptr_t)read_cnt,
472 rte_eal_mp_wait_lcore();
475 /* Test only writer cases */
476 for (; i < core_cnt[n] * 2; i++)
477 rte_eal_remote_launch(test_rw_writer,
478 (void *)((uintptr_t)start_coreid),
481 rte_eal_mp_wait_lcore();
484 unsigned long long int cycles_per_insertion =
485 rte_atomic64_read(&gread_cycles) /
486 rte_atomic64_read(&greads);
487 perf_results->read_only[n] = cycles_per_insertion;
488 printf("Reader only: cycles per lookup: %llu\n",
489 cycles_per_insertion);
493 unsigned long long int cycles_per_insertion =
494 rte_atomic64_read(&gwrite_cycles) /
495 rte_atomic64_read(&gwrites);
496 perf_results->write_only[n] = cycles_per_insertion;
497 printf("Writer only: cycles per writes: %llu\n",
498 cycles_per_insertion);
501 rte_atomic64_clear(&greads);
502 rte_atomic64_clear(&gread_cycles);
503 rte_atomic64_clear(&gwrites);
504 rte_atomic64_clear(&gwrite_cycles);
506 rte_hash_reset(tbl_rw_test_param.h);
508 for (i = 0; i < TOTAL_INSERT / 2; i++) {
509 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
510 tbl_rw_test_param.keys + i,
511 (void *)((uintptr_t)i));
513 printf("Failed to insert half of keys\n");
518 start_coreid = core_cnt[n];
521 for (i = core_cnt[n]; i < core_cnt[n] * 2; i++)
522 rte_eal_remote_launch(test_rw_writer,
523 (void *)((uintptr_t)start_coreid),
525 for (i = 0; i < core_cnt[n]; i++)
526 rte_eal_remote_launch(test_rw_reader,
527 (void *)(uintptr_t)read_cnt,
530 for (i = 0; i < core_cnt[n]; i++)
531 rte_eal_remote_launch(test_rw_reader,
532 (void *)(uintptr_t)read_cnt,
534 for (; i < core_cnt[n] * 2; i++)
535 rte_eal_remote_launch(test_rw_writer,
536 (void *)((uintptr_t)start_coreid),
540 rte_eal_mp_wait_lcore();
543 memset(tbl_rw_test_param.found, 0, TOTAL_ENTRY);
544 while (rte_hash_iterate(tbl_rw_test_param.h,
545 &next_key, &next_data, &iter) >= 0) {
546 /* Search for the key in the list of keys added .*/
547 i = *(const uint32_t *)next_key;
548 tbl_rw_test_param.found[i]++;
551 for (i = 0; i < tbl_rw_test_param.rounded_tot_insert; i++) {
552 if (tbl_rw_test_param.keys[i] != RTE_RWTEST_FAIL) {
553 if (tbl_rw_test_param.found[i] > 1) {
557 if (tbl_rw_test_param.found[i] == 0) {
559 printf("key %"PRIu64" is lost\n", i);
565 if (duplicated_keys > 0) {
566 printf("%d key duplicated\n", duplicated_keys);
571 printf("%d key lost\n", lost_keys);
575 printf("No key corrupted during read-write test.\n");
578 unsigned long long int cycles_per_insertion =
579 rte_atomic64_read(&gread_cycles) /
580 rte_atomic64_read(&greads);
581 perf_results->read_write_r[n] = cycles_per_insertion;
582 printf("Read-write cycles per lookup: %llu\n",
583 cycles_per_insertion);
587 unsigned long long int cycles_per_insertion =
588 rte_atomic64_read(&gwrite_cycles) /
589 rte_atomic64_read(&gwrites);
590 perf_results->read_write_w[n] = cycles_per_insertion;
591 printf("Read-write cycles per writes: %llu\n",
592 cycles_per_insertion);
597 rte_free(tbl_rw_test_param.found);
598 rte_free(tbl_rw_test_param.keys);
599 rte_hash_free(tbl_rw_test_param.h);
603 rte_free(tbl_rw_test_param.found);
604 rte_free(tbl_rw_test_param.keys);
605 rte_hash_free(tbl_rw_test_param.h);
612 test_hash_rw_perf_main(void)
615 * Variables used to choose different tests.
616 * use_htm indicates if hardware transactional memory should be used.
617 * reader_faster indicates if the reader threads should finish earlier
618 * than writer threads. This is to timing either reader threads or
619 * writer threads for performance numbers.
621 int use_htm, reader_faster;
622 unsigned int i = 0, core_id = 0;
624 if (rte_lcore_count() < 3) {
625 printf("Not enough cores for hash_readwrite_autotest, expecting at least 3\n");
629 RTE_LCORE_FOREACH_SLAVE(core_id) {
630 slave_core_ids[i] = core_id;
634 setlocale(LC_NUMERIC, "");
636 if (rte_tm_supported()) {
637 printf("Hardware transactional memory (lock elision) "
640 printf("Test read-write with Hardware transactional memory\n");
645 if (test_hash_readwrite_perf(&htm_results, use_htm,
650 if (test_hash_readwrite_perf(&htm_results, use_htm,
654 printf("Hardware transactional memory (lock elision) "
655 "is NOT supported\n");
658 printf("Test read-write without Hardware transactional memory\n");
662 if (test_hash_readwrite_perf(&non_htm_results, use_htm,
666 if (test_hash_readwrite_perf(&non_htm_results, use_htm,
670 printf("================\n");
671 printf("Results summary:\n");
672 printf("================\n");
674 printf("single read: %u\n", htm_results.single_read);
675 printf("single write: %u\n", htm_results.single_write);
676 for (i = 0; i < NUM_TEST; i++) {
677 printf("+++ core_cnt: %u +++\n", core_cnt[i]);
679 printf(" read only: %u\n", htm_results.read_only[i]);
680 printf(" write only: %u\n", htm_results.write_only[i]);
681 printf(" read-write read: %u\n", htm_results.read_write_r[i]);
682 printf(" read-write write: %u\n", htm_results.read_write_w[i]);
684 printf("non HTM:\n");
685 printf(" read only: %u\n", non_htm_results.read_only[i]);
686 printf(" write only: %u\n", non_htm_results.write_only[i]);
687 printf(" read-write read: %u\n",
688 non_htm_results.read_write_r[i]);
689 printf(" read-write write: %u\n",
690 non_htm_results.read_write_w[i]);
697 test_hash_rw_func_main(void)
700 * Variables used to choose different tests.
701 * use_htm indicates if hardware transactional memory should be used.
702 * reader_faster indicates if the reader threads should finish earlier
703 * than writer threads. This is to timing either reader threads or
704 * writer threads for performance numbers.
706 unsigned int i = 0, core_id = 0;
708 if (rte_lcore_count() < 3) {
709 printf("Not enough cores for hash_readwrite_autotest, expecting at least 3\n");
713 RTE_LCORE_FOREACH_SLAVE(core_id) {
714 slave_core_ids[i] = core_id;
718 setlocale(LC_NUMERIC, "");
720 if (rte_tm_supported()) {
721 printf("Hardware transactional memory (lock elision) "
724 printf("Test read-write with Hardware transactional memory\n");
726 /* htm = 1, rw_lf = 0, ext = 0 */
727 if (test_hash_readwrite_functional(1, 0, 0) < 0)
730 /* htm = 1, rw_lf = 1, ext = 0 */
731 if (test_hash_readwrite_functional(1, 1, 0) < 0)
734 /* htm = 1, rw_lf = 0, ext = 1 */
735 if (test_hash_readwrite_functional(1, 0, 1) < 0)
738 /* htm = 1, rw_lf = 1, ext = 1 */
739 if (test_hash_readwrite_functional(1, 1, 1) < 0)
742 printf("Hardware transactional memory (lock elision) "
743 "is NOT supported\n");
746 printf("Test read-write without Hardware transactional memory\n");
747 /* htm = 0, rw_lf = 0, ext = 0 */
748 if (test_hash_readwrite_functional(0, 0, 0) < 0)
751 /* htm = 0, rw_lf = 1, ext = 0 */
752 if (test_hash_readwrite_functional(0, 1, 0) < 0)
755 /* htm = 0, rw_lf = 0, ext = 1 */
756 if (test_hash_readwrite_functional(0, 0, 1) < 0)
759 /* htm = 0, rw_lf = 1, ext = 1 */
760 if (test_hash_readwrite_functional(0, 1, 1) < 0)
766 REGISTER_TEST_COMMAND(hash_readwrite_func_autotest, test_hash_rw_func_main);
767 REGISTER_TEST_COMMAND(hash_readwrite_perf_autotest, test_hash_rw_perf_main);