test/eventdev: add octeontx unit test infrastructure
[dpdk.git] / test / test / test_hash_scaling.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35
36 #include <rte_cycles.h>
37 #include <rte_hash.h>
38 #include <rte_hash_crc.h>
39 #include <rte_spinlock.h>
40 #include <rte_launch.h>
41
42 #include "test.h"
43
44 /*
45  * Check condition and return an error if true. Assumes that "handle" is the
46  * name of the hash structure pointer to be freed.
47  */
48 #define RETURN_IF_ERROR(cond, str, ...) do {                            \
49         if (cond) {                                                     \
50                 printf("ERROR line %d: " str "\n", __LINE__,            \
51                                                         ##__VA_ARGS__); \
52                 if (handle)                                             \
53                         rte_hash_free(handle);                          \
54                 return -1;                                              \
55         }                                                               \
56 } while (0)
57
58 enum locking_mode_t {
59         NORMAL_LOCK,
60         LOCK_ELISION,
61         NULL_LOCK
62 };
63
64 struct {
65         uint32_t num_iterations;
66         struct rte_hash *h;
67         rte_spinlock_t *lock;
68         int locking_mode;
69 } tbl_scaling_test_params;
70
71 static rte_atomic64_t gcycles;
72
73 static int test_hash_scaling_worker(__attribute__((unused)) void *arg)
74 {
75         uint64_t i, key;
76         uint32_t thr_id = rte_sys_gettid();
77         uint64_t begin, cycles = 0;
78
79         switch (tbl_scaling_test_params.locking_mode) {
80
81         case NORMAL_LOCK:
82
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
86                          */
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;
93                 }
94                 break;
95
96         case LOCK_ELISION:
97
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;
105                 }
106                 break;
107
108         default:
109
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;
115                 }
116         }
117
118         rte_atomic64_add(&gcycles, cycles);
119
120         return 0;
121 }
122
123 /*
124  * Do scalability perf tests.
125  */
126 static int
127 test_hash_scaling(int locking_mode)
128 {
129         static unsigned calledCount =    1;
130         uint32_t num_iterations = 1024*1024;
131         uint64_t i, key;
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
139         };
140         struct rte_hash *handle;
141         char name[RTE_HASH_NAMESIZE];
142         rte_spinlock_t lock;
143
144         rte_spinlock_init(&lock);
145
146         snprintf(name, 32, "test%u", calledCount++);
147         hash_params.name = name;
148
149         handle = rte_hash_create(&hash_params);
150         RETURN_IF_ERROR(handle == NULL, "hash creation failed");
151
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;
157
158         rte_atomic64_init(&gcycles);
159         rte_atomic64_clear(&gcycles);
160
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);
165         }
166
167         rte_eal_mp_remote_launch(test_hash_scaling_worker, NULL, CALL_MASTER);
168         rte_eal_mp_wait_lcore();
169
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;
174
175         switch (locking_mode) {
176         case NORMAL_LOCK:
177                 lock_name = "normal spinlock";
178                 break;
179         case LOCK_ELISION:
180                 lock_name = "lock elision";
181                 break;
182         default:
183                 lock_name = "null lock";
184         }
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");
189         /* CSV output */
190         printf(">>>%d,%s,%llu\n", rte_lcore_count(), lock_name,
191                 cycles_per_operation);
192
193         rte_hash_free(handle);
194         return 0;
195 }
196
197 static int
198 test_hash_scaling_main(void)
199 {
200         int r = 0;
201
202         if (rte_lcore_count() == 1)
203                 r = test_hash_scaling(NULL_LOCK);
204
205         if (r == 0)
206                 r = test_hash_scaling(NORMAL_LOCK);
207
208         if (!rte_tm_supported()) {
209                 printf("Hardware transactional memory (lock elision) is NOT supported\n");
210                 return r;
211         }
212         printf("Hardware transactional memory (lock elision) is supported\n");
213
214         if (r == 0)
215                 r = test_hash_scaling(LOCK_ELISION);
216
217         return r;
218 }
219
220 REGISTER_TEST_COMMAND(hash_scaling_autotest, test_hash_scaling_main);