4 * Copyright(c) 2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_cycles.h>
38 #include <rte_hash_crc.h>
39 #include <rte_spinlock.h>
40 #include <rte_launch.h>
45 * Check condition and return an error if true. Assumes that "handle" is the
46 * name of the hash structure pointer to be freed.
48 #define RETURN_IF_ERROR(cond, str, ...) do { \
50 printf("ERROR line %d: " str "\n", __LINE__, \
53 rte_hash_free(handle); \
65 uint32_t num_iterations;
69 } tbl_scaling_test_params;
71 static rte_atomic64_t gcycles;
73 static int test_hash_scaling_worker(__attribute__((unused)) void *arg)
76 uint32_t thr_id = rte_sys_gettid();
77 uint64_t begin, cycles = 0;
79 switch (tbl_scaling_test_params.locking_mode) {
83 for (i = 0; i < tbl_scaling_test_params.num_iterations; i++) {
84 /* different threads get different keys because
85 we use the thread-id in the key computation
87 key = rte_hash_crc(&i, sizeof(i), thr_id);
88 begin = rte_rdtsc_precise();
89 rte_spinlock_lock(tbl_scaling_test_params.lock);
90 rte_hash_add_key(tbl_scaling_test_params.h, &key);
91 rte_spinlock_unlock(tbl_scaling_test_params.lock);
92 cycles += rte_rdtsc_precise() - begin;
98 for (i = 0; i < tbl_scaling_test_params.num_iterations; i++) {
99 key = rte_hash_crc(&i, sizeof(i), thr_id);
100 begin = rte_rdtsc_precise();
101 rte_spinlock_lock_tm(tbl_scaling_test_params.lock);
102 rte_hash_add_key(tbl_scaling_test_params.h, &key);
103 rte_spinlock_unlock_tm(tbl_scaling_test_params.lock);
104 cycles += rte_rdtsc_precise() - begin;
110 for (i = 0; i < tbl_scaling_test_params.num_iterations; i++) {
111 key = rte_hash_crc(&i, sizeof(i), thr_id);
112 begin = rte_rdtsc_precise();
113 rte_hash_add_key(tbl_scaling_test_params.h, &key);
114 cycles += rte_rdtsc_precise() - begin;
118 rte_atomic64_add(&gcycles, cycles);
124 * Do scalability perf tests.
127 test_hash_scaling(int locking_mode)
129 static unsigned calledCount = 1;
130 uint32_t num_iterations = 1024*1024;
132 struct rte_hash_parameters hash_params = {
133 .entries = num_iterations*2,
134 .key_len = sizeof(key),
135 .hash_func = rte_hash_crc,
136 .hash_func_init_val = 0,
137 .socket_id = rte_socket_id(),
138 .extra_flag = RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT
140 struct rte_hash *handle;
141 char name[RTE_HASH_NAMESIZE];
144 rte_spinlock_init(&lock);
146 snprintf(name, 32, "test%u", calledCount++);
147 hash_params.name = name;
149 handle = rte_hash_create(&hash_params);
150 RETURN_IF_ERROR(handle == NULL, "hash creation failed");
152 tbl_scaling_test_params.num_iterations =
153 num_iterations/rte_lcore_count();
154 tbl_scaling_test_params.h = handle;
155 tbl_scaling_test_params.lock = &lock;
156 tbl_scaling_test_params.locking_mode = locking_mode;
158 rte_atomic64_init(&gcycles);
159 rte_atomic64_clear(&gcycles);
161 /* fill up to initial size */
162 for (i = 0; i < num_iterations; i++) {
163 key = rte_hash_crc(&i, sizeof(i), 0xabcdabcd);
164 rte_hash_add_key(tbl_scaling_test_params.h, &key);
167 rte_eal_mp_remote_launch(test_hash_scaling_worker, NULL, CALL_MASTER);
168 rte_eal_mp_wait_lcore();
170 unsigned long long int cycles_per_operation =
171 rte_atomic64_read(&gcycles)/
172 (tbl_scaling_test_params.num_iterations*rte_lcore_count());
173 const char *lock_name;
175 switch (locking_mode) {
177 lock_name = "normal spinlock";
180 lock_name = "lock elision";
183 lock_name = "null lock";
185 printf("--------------------------------------------------------\n");
186 printf("Cores: %d; %s mode -> cycles per operation: %llu\n",
187 rte_lcore_count(), lock_name, cycles_per_operation);
188 printf("--------------------------------------------------------\n");
190 printf(">>>%d,%s,%llu\n", rte_lcore_count(), lock_name,
191 cycles_per_operation);
193 rte_hash_free(handle);
198 test_hash_scaling_main(void)
202 if (rte_lcore_count() == 1)
203 r = test_hash_scaling(NULL_LOCK);
206 r = test_hash_scaling(NORMAL_LOCK);
208 if (!rte_tm_supported()) {
209 printf("Hardware transactional memory (lock elision) is NOT supported\n");
212 printf("Hardware transactional memory (lock elision) is supported\n");
215 r = test_hash_scaling(LOCK_ELISION);
220 REGISTER_TEST_COMMAND(hash_scaling_autotest, test_hash_scaling_main);