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};
27 unsigned int slave_core_ids[RTE_MAX_LCORE];
30 uint32_t single_write;
31 uint32_t read_only[NUM_TEST];
32 uint32_t write_only[NUM_TEST];
33 uint32_t read_write_r[NUM_TEST];
34 uint32_t read_write_w[NUM_TEST];
37 static struct perf htm_results, non_htm_results;
43 uint32_t rounded_tot_insert;
47 static rte_atomic64_t gcycles;
48 static rte_atomic64_t ginsertions;
50 static rte_atomic64_t gread_cycles;
51 static rte_atomic64_t gwrite_cycles;
53 static rte_atomic64_t greads;
54 static rte_atomic64_t gwrites;
57 test_hash_readwrite_worker(__attribute__((unused)) void *arg)
60 uint32_t lcore_id = rte_lcore_id();
61 uint64_t begin, cycles;
64 for (i = 0; i < rte_lcore_count(); i++) {
65 if (slave_core_ids[i] == lcore_id)
68 offset = tbl_rw_test_param.num_insert * i;
70 printf("Core #%d inserting and reading %d: %'"PRId64" - %'"PRId64"\n",
71 lcore_id, tbl_rw_test_param.num_insert,
72 offset, offset + tbl_rw_test_param.num_insert - 1);
74 begin = rte_rdtsc_precise();
76 for (i = offset; i < offset + tbl_rw_test_param.num_insert; i++) {
78 if (rte_hash_lookup(tbl_rw_test_param.h,
79 tbl_rw_test_param.keys + i) > 0)
82 ret = rte_hash_add_key(tbl_rw_test_param.h,
83 tbl_rw_test_param.keys + i);
87 if (rte_hash_lookup(tbl_rw_test_param.h,
88 tbl_rw_test_param.keys + i) != ret)
92 cycles = rte_rdtsc_precise() - begin;
93 rte_atomic64_add(&gcycles, cycles);
94 rte_atomic64_add(&ginsertions, i - offset);
96 for (; i < offset + tbl_rw_test_param.num_insert; i++)
97 tbl_rw_test_param.keys[i] = RTE_RWTEST_FAIL;
103 init_params(int use_htm, int use_jhash)
107 uint32_t *keys = NULL;
108 uint32_t *found = NULL;
109 struct rte_hash *handle;
111 struct rte_hash_parameters hash_params = {
112 .entries = TOTAL_ENTRY,
113 .key_len = sizeof(uint32_t),
114 .hash_func_init_val = 0,
115 .socket_id = rte_socket_id(),
118 hash_params.hash_func = rte_jhash;
120 hash_params.hash_func = rte_hash_crc;
123 hash_params.extra_flag =
124 RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT |
125 RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY;
127 hash_params.extra_flag =
128 RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY;
130 hash_params.name = "tests";
132 handle = rte_hash_create(&hash_params);
133 if (handle == NULL) {
134 printf("hash creation failed");
138 tbl_rw_test_param.h = handle;
139 keys = rte_malloc(NULL, sizeof(uint32_t) * TOTAL_ENTRY, 0);
142 printf("RTE_MALLOC failed\n");
146 found = rte_zmalloc(NULL, sizeof(uint32_t) * TOTAL_ENTRY, 0);
148 printf("RTE_ZMALLOC failed\n");
152 tbl_rw_test_param.keys = keys;
153 tbl_rw_test_param.found = found;
155 for (i = 0; i < TOTAL_ENTRY; i++)
162 rte_hash_free(handle);
168 test_hash_readwrite_functional(int use_htm)
171 const void *next_key;
175 uint32_t duplicated_keys = 0;
176 uint32_t lost_keys = 0;
178 int slave_cnt = rte_lcore_count() - 1;
180 rte_atomic64_init(&gcycles);
181 rte_atomic64_clear(&gcycles);
183 rte_atomic64_init(&ginsertions);
184 rte_atomic64_clear(&ginsertions);
186 if (init_params(use_htm, use_jhash) != 0)
189 tbl_rw_test_param.num_insert =
190 TOTAL_INSERT / slave_cnt;
192 tbl_rw_test_param.rounded_tot_insert =
193 tbl_rw_test_param.num_insert
196 printf("++++++++Start function tests:+++++++++\n");
198 /* Fire all threads. */
199 rte_eal_mp_remote_launch(test_hash_readwrite_worker,
201 rte_eal_mp_wait_lcore();
203 while (rte_hash_iterate(tbl_rw_test_param.h, &next_key,
204 &next_data, &iter) >= 0) {
205 /* Search for the key in the list of keys added .*/
206 i = *(const uint32_t *)next_key;
207 tbl_rw_test_param.found[i]++;
210 for (i = 0; i < tbl_rw_test_param.rounded_tot_insert; i++) {
211 if (tbl_rw_test_param.keys[i] != RTE_RWTEST_FAIL) {
212 if (tbl_rw_test_param.found[i] > 1) {
216 if (tbl_rw_test_param.found[i] == 0) {
218 printf("key %d is lost\n", i);
224 if (duplicated_keys > 0) {
225 printf("%d key duplicated\n", duplicated_keys);
230 printf("%d key lost\n", lost_keys);
234 printf("No key corrupted during read-write test.\n");
236 unsigned long long int cycles_per_insertion =
237 rte_atomic64_read(&gcycles) /
238 rte_atomic64_read(&ginsertions);
240 printf("cycles per insertion and lookup: %llu\n", cycles_per_insertion);
242 rte_free(tbl_rw_test_param.found);
243 rte_free(tbl_rw_test_param.keys);
244 rte_hash_free(tbl_rw_test_param.h);
245 printf("+++++++++Complete function tests+++++++++\n");
249 rte_free(tbl_rw_test_param.found);
250 rte_free(tbl_rw_test_param.keys);
251 rte_hash_free(tbl_rw_test_param.h);
257 test_rw_reader(void *arg)
260 uint64_t begin, cycles;
261 uint64_t read_cnt = (uint64_t)((uintptr_t)arg);
263 begin = rte_rdtsc_precise();
264 for (i = 0; i < read_cnt; i++) {
266 rte_hash_lookup_data(tbl_rw_test_param.h,
267 tbl_rw_test_param.keys + i,
269 if (i != (uint64_t)(uintptr_t)data) {
270 printf("lookup find wrong value %"PRIu64","
272 (uint64_t)(uintptr_t)data);
277 cycles = rte_rdtsc_precise() - begin;
278 rte_atomic64_add(&gread_cycles, cycles);
279 rte_atomic64_add(&greads, i);
284 test_rw_writer(void *arg)
287 uint32_t lcore_id = rte_lcore_id();
288 uint64_t begin, cycles;
290 uint64_t start_coreid = (uint64_t)(uintptr_t)arg;
293 for (i = 0; i < rte_lcore_count(); i++) {
294 if (slave_core_ids[i] == lcore_id)
298 offset = TOTAL_INSERT / 2 + (i - (start_coreid)) *
299 tbl_rw_test_param.num_insert;
300 begin = rte_rdtsc_precise();
301 for (i = offset; i < offset + tbl_rw_test_param.num_insert; i++) {
302 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
303 tbl_rw_test_param.keys + i,
304 (void *)((uintptr_t)i));
306 printf("writer failed %"PRIu64"\n", i);
311 cycles = rte_rdtsc_precise() - begin;
312 rte_atomic64_add(&gwrite_cycles, cycles);
313 rte_atomic64_add(&gwrites, tbl_rw_test_param.num_insert);
318 test_hash_readwrite_perf(struct perf *perf_results, int use_htm,
324 uint64_t i, read_cnt;
326 const void *next_key;
331 uint32_t duplicated_keys = 0;
332 uint32_t lost_keys = 0;
334 uint64_t start = 0, end = 0;
336 rte_atomic64_init(&greads);
337 rte_atomic64_init(&gwrites);
338 rte_atomic64_clear(&gwrites);
339 rte_atomic64_clear(&greads);
341 rte_atomic64_init(&gread_cycles);
342 rte_atomic64_clear(&gread_cycles);
343 rte_atomic64_init(&gwrite_cycles);
344 rte_atomic64_clear(&gwrite_cycles);
346 if (init_params(use_htm, use_jhash) != 0)
350 * Do a readers finish faster or writers finish faster test.
351 * When readers finish faster, we timing the readers, and when writers
352 * finish faster, we timing the writers.
353 * Divided by 10 or 2 is just experimental values to vary the workload
357 printf("++++++Start perf test: reader++++++++\n");
358 read_cnt = TOTAL_INSERT / 10;
360 printf("++++++Start perf test: writer++++++++\n");
361 read_cnt = TOTAL_INSERT / 2;
364 /* We first test single thread performance */
365 start = rte_rdtsc_precise();
366 /* Insert half of the keys */
367 for (i = 0; i < TOTAL_INSERT / 2; i++) {
368 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
369 tbl_rw_test_param.keys + i,
370 (void *)((uintptr_t)i));
372 printf("Failed to insert half of keys\n");
376 end = rte_rdtsc_precise() - start;
377 perf_results->single_write = end / i;
379 start = rte_rdtsc_precise();
381 for (i = 0; i < read_cnt; i++) {
383 rte_hash_lookup_data(tbl_rw_test_param.h,
384 tbl_rw_test_param.keys + i,
386 if (i != (uint64_t)(uintptr_t)data) {
387 printf("lookup find wrong value"
388 " %"PRIu64",%"PRIu64"\n", i,
389 (uint64_t)(uintptr_t)data);
393 end = rte_rdtsc_precise() - start;
394 perf_results->single_read = end / i;
396 for (n = 0; n < NUM_TEST; n++) {
397 unsigned int tot_slave_lcore = rte_lcore_count() - 1;
398 if (tot_slave_lcore < core_cnt[n] * 2)
401 rte_atomic64_clear(&greads);
402 rte_atomic64_clear(&gread_cycles);
403 rte_atomic64_clear(&gwrites);
404 rte_atomic64_clear(&gwrite_cycles);
406 rte_hash_reset(tbl_rw_test_param.h);
408 tbl_rw_test_param.num_insert = TOTAL_INSERT / 2 / core_cnt[n];
409 tbl_rw_test_param.rounded_tot_insert = TOTAL_INSERT / 2 +
410 tbl_rw_test_param.num_insert *
413 for (i = 0; i < TOTAL_INSERT / 2; i++) {
414 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
415 tbl_rw_test_param.keys + i,
416 (void *)((uintptr_t)i));
418 printf("Failed to insert half of keys\n");
423 /* Then test multiple thread case but only all reads or
427 /* Test only reader cases */
428 for (i = 0; i < core_cnt[n]; i++)
429 rte_eal_remote_launch(test_rw_reader,
430 (void *)(uintptr_t)read_cnt,
433 rte_eal_mp_wait_lcore();
436 /* Test only writer cases */
437 for (; i < core_cnt[n] * 2; i++)
438 rte_eal_remote_launch(test_rw_writer,
439 (void *)((uintptr_t)start_coreid),
442 rte_eal_mp_wait_lcore();
445 unsigned long long int cycles_per_insertion =
446 rte_atomic64_read(&gread_cycles) /
447 rte_atomic64_read(&greads);
448 perf_results->read_only[n] = cycles_per_insertion;
449 printf("Reader only: cycles per lookup: %llu\n",
450 cycles_per_insertion);
454 unsigned long long int cycles_per_insertion =
455 rte_atomic64_read(&gwrite_cycles) /
456 rte_atomic64_read(&gwrites);
457 perf_results->write_only[n] = cycles_per_insertion;
458 printf("Writer only: cycles per writes: %llu\n",
459 cycles_per_insertion);
462 rte_atomic64_clear(&greads);
463 rte_atomic64_clear(&gread_cycles);
464 rte_atomic64_clear(&gwrites);
465 rte_atomic64_clear(&gwrite_cycles);
467 rte_hash_reset(tbl_rw_test_param.h);
469 for (i = 0; i < TOTAL_INSERT / 2; i++) {
470 ret = rte_hash_add_key_data(tbl_rw_test_param.h,
471 tbl_rw_test_param.keys + i,
472 (void *)((uintptr_t)i));
474 printf("Failed to insert half of keys\n");
479 start_coreid = core_cnt[n];
482 for (i = core_cnt[n]; i < core_cnt[n] * 2; i++)
483 rte_eal_remote_launch(test_rw_writer,
484 (void *)((uintptr_t)start_coreid),
486 for (i = 0; i < core_cnt[n]; i++)
487 rte_eal_remote_launch(test_rw_reader,
488 (void *)(uintptr_t)read_cnt,
491 for (i = 0; i < core_cnt[n]; i++)
492 rte_eal_remote_launch(test_rw_reader,
493 (void *)(uintptr_t)read_cnt,
495 for (; i < core_cnt[n] * 2; i++)
496 rte_eal_remote_launch(test_rw_writer,
497 (void *)((uintptr_t)start_coreid),
501 rte_eal_mp_wait_lcore();
503 while (rte_hash_iterate(tbl_rw_test_param.h,
504 &next_key, &next_data, &iter) >= 0) {
505 /* Search for the key in the list of keys added .*/
506 i = *(const uint32_t *)next_key;
507 tbl_rw_test_param.found[i]++;
510 for (i = 0; i < tbl_rw_test_param.rounded_tot_insert; i++) {
511 if (tbl_rw_test_param.keys[i] != RTE_RWTEST_FAIL) {
512 if (tbl_rw_test_param.found[i] > 1) {
516 if (tbl_rw_test_param.found[i] == 0) {
518 printf("key %"PRIu64" is lost\n", i);
524 if (duplicated_keys > 0) {
525 printf("%d key duplicated\n", duplicated_keys);
530 printf("%d key lost\n", lost_keys);
534 printf("No key corrupted during read-write test.\n");
537 unsigned long long int cycles_per_insertion =
538 rte_atomic64_read(&gread_cycles) /
539 rte_atomic64_read(&greads);
540 perf_results->read_write_r[n] = cycles_per_insertion;
541 printf("Read-write cycles per lookup: %llu\n",
542 cycles_per_insertion);
546 unsigned long long int cycles_per_insertion =
547 rte_atomic64_read(&gwrite_cycles) /
548 rte_atomic64_read(&gwrites);
549 perf_results->read_write_w[n] = cycles_per_insertion;
550 printf("Read-write cycles per writes: %llu\n",
551 cycles_per_insertion);
556 rte_free(tbl_rw_test_param.found);
557 rte_free(tbl_rw_test_param.keys);
558 rte_hash_free(tbl_rw_test_param.h);
562 rte_free(tbl_rw_test_param.found);
563 rte_free(tbl_rw_test_param.keys);
564 rte_hash_free(tbl_rw_test_param.h);
571 test_hash_readwrite_main(void)
574 * Variables used to choose different tests.
575 * use_htm indicates if hardware transactional memory should be used.
576 * reader_faster indicates if the reader threads should finish earlier
577 * than writer threads. This is to timing either reader threads or
578 * writer threads for performance numbers.
580 int use_htm, reader_faster;
581 unsigned int i = 0, core_id = 0;
583 if (rte_lcore_count() <= 2) {
584 printf("More than two lcores are required "
585 "to do read write test\n");
589 RTE_LCORE_FOREACH_SLAVE(core_id) {
590 slave_core_ids[i] = core_id;
594 setlocale(LC_NUMERIC, "");
596 if (rte_tm_supported()) {
597 printf("Hardware transactional memory (lock elision) "
600 printf("Test read-write with Hardware transactional memory\n");
603 if (test_hash_readwrite_functional(use_htm) < 0)
607 if (test_hash_readwrite_perf(&htm_results, use_htm,
612 if (test_hash_readwrite_perf(&htm_results, use_htm,
616 printf("Hardware transactional memory (lock elision) "
617 "is NOT supported\n");
620 printf("Test read-write without Hardware transactional memory\n");
622 if (test_hash_readwrite_functional(use_htm) < 0)
625 if (test_hash_readwrite_perf(&non_htm_results, use_htm,
629 if (test_hash_readwrite_perf(&non_htm_results, use_htm,
633 printf("Results summary:\n");
635 printf("single read: %u\n", htm_results.single_read);
636 printf("single write: %u\n", htm_results.single_write);
637 for (i = 0; i < NUM_TEST; i++) {
638 printf("core_cnt: %u\n", core_cnt[i]);
640 printf("read only: %u\n", htm_results.read_only[i]);
641 printf("write only: %u\n", htm_results.write_only[i]);
642 printf("read-write read: %u\n", htm_results.read_write_r[i]);
643 printf("read-write write: %u\n", htm_results.read_write_w[i]);
645 printf("non HTM:\n");
646 printf("read only: %u\n", non_htm_results.read_only[i]);
647 printf("write only: %u\n", non_htm_results.write_only[i]);
648 printf("read-write read: %u\n",
649 non_htm_results.read_write_r[i]);
650 printf("read-write write: %u\n",
651 non_htm_results.read_write_w[i]);
657 REGISTER_TEST_COMMAND(hash_readwrite_autotest, test_hash_readwrite_main);