blob: 80e11511f62d20f665ec88821b9ec4e20d4a4b53 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <vppinfra/time.h>
16#include <vppinfra/cache.h>
17#include <vppinfra/error.h>
Dave Barache7d212f2018-02-07 13:14:06 -050018#include <sys/resource.h>
19#include <stdio.h>
Dave Barach508498f2018-07-19 12:11:16 -040020#include <pthread.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070021
22#include <vppinfra/bihash_8_8.h>
23#include <vppinfra/bihash_template.h>
24
25#include <vppinfra/bihash_template.c>
26
Dave Barachc3799992016-08-15 11:12:27 -040027typedef struct
28{
Dave Barach508498f2018-07-19 12:11:16 -040029 volatile u32 thread_barrier;
30 volatile u32 threads_running;
31 volatile u64 sequence_number;
Filip Tehlar397fd7d2016-10-26 14:31:24 +020032 u64 seed;
Ed Warnickecb9cada2015-12-08 15:45:58 -070033 u32 nbuckets;
34 u32 nitems;
Dave Barache7d212f2018-02-07 13:14:06 -050035 u32 ncycles;
36 u32 report_every_n;
Ed Warnickecb9cada2015-12-08 15:45:58 -070037 u32 search_iter;
38 int careful_delete_tests;
39 int verbose;
40 int non_random_keys;
Dave Barach508498f2018-07-19 12:11:16 -040041 u32 nthreads;
Dave Barachc3799992016-08-15 11:12:27 -040042 uword *key_hash;
43 u64 *keys;
Dave Barach97f5af02018-02-22 09:48:45 -050044 uword hash_memory_size;
Dave Barachc3799992016-08-15 11:12:27 -040045 BVT (clib_bihash) hash;
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 clib_time_t clib_time;
Dave Barach508498f2018-07-19 12:11:16 -040047 void *global_heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Dave Barachc3799992016-08-15 11:12:27 -040049 unformat_input_t *input;
50
Ed Warnickecb9cada2015-12-08 15:45:58 -070051} test_main_t;
52
53test_main_t test_main;
54
Dave Barachc3799992016-08-15 11:12:27 -040055uword
56vl (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070057{
Dave Barachc3799992016-08-15 11:12:27 -040058 return vec_len (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -070059}
60
Dave Barachc3799992016-08-15 11:12:27 -040061static clib_error_t *
Dave Barachba7ddfe2017-05-17 20:20:50 -040062test_bihash_vec64 (test_main_t * tm)
63{
64 u32 user_buckets = 1228800;
65 u32 user_memory_size = 209715200;
66 BVT (clib_bihash_kv) kv;
67 int i, j;
68 f64 before;
69 f64 *cum_times = 0;
70 BVT (clib_bihash) * h;
71
72 h = &tm->hash;
73
74 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
75
76 before = clib_time_now (&tm->clib_time);
77
78 for (j = 0; j < 10; j++)
79 {
80 for (i = 1; i <= j * 1000 + 1; i++)
81 {
82 kv.key = i;
83 kv.value = 1;
84
85 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
86 }
87
88 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
89 }
90
91 for (j = 0; j < vec_len (cum_times); j++)
92 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
93 cum_times[j] * 1e6);
94
95 return 0;
96}
97
Dave Barach508498f2018-07-19 12:11:16 -040098void *
99test_bihash_thread_fn (void *arg)
100{
101 BVT (clib_bihash) * h;
102 BVT (clib_bihash_kv) kv;
103 test_main_t *tm = &test_main;
104
105 int i, j;
106
107 u32 my_thread_index = (u32) (u64) arg;
108 __os_thread_index = my_thread_index;
109 clib_mem_set_per_cpu_heap (tm->global_heap);
110
111 while (tm->thread_barrier)
112 ;
113
114 h = &tm->hash;
115
116 for (i = 0; i < tm->ncycles; i++)
117 {
118 for (j = 0; j < tm->nitems; j++)
119 {
120 kv.key = ((u64) my_thread_index << 32) | (u64) j;
121 kv.value = ((u64) my_thread_index << 32) | (u64) j;
122 (void) __atomic_add_fetch (&tm->sequence_number, 1,
123 __ATOMIC_ACQUIRE);
124 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
125 }
126 for (j = 0; j < tm->nitems; j++)
127 {
128 kv.key = ((u64) my_thread_index << 32) | (u64) j;
129 kv.value = ((u64) my_thread_index << 32) | (u64) j;
130 (void) __atomic_add_fetch (&tm->sequence_number, 1,
131 __ATOMIC_ACQUIRE);
132 BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
133 }
134 }
135
136 (void) __atomic_sub_fetch (&tm->threads_running, 1, __ATOMIC_ACQUIRE);
137 while (1)
138 {
139 struct timespec ts, tsrem;
140 ts.tv_sec = 1;
141 ts.tv_nsec = 0;
142
143 while (nanosleep (&ts, &tsrem) < 0)
144 ts = tsrem;
145 }
146 return (0); /* not so much */
147}
148
149static clib_error_t *
150test_bihash_threads (test_main_t * tm)
151{
152 int i;
153 pthread_t handle;
154 BVT (clib_bihash) * h;
155 int rv;
156
157 h = &tm->hash;
158
159 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
160
161 tm->thread_barrier = 1;
162
163 /* Start the worker threads */
164 for (i = 0; i < tm->nthreads; i++)
165 {
166 rv = pthread_create (&handle, NULL, test_bihash_thread_fn,
167 (void *) (u64) i);
168 if (rv)
169 {
170 clib_unix_warning ("pthread_create returned %d", rv);
171 }
172 }
173 tm->threads_running = i;
174 tm->sequence_number = 0;
175 CLIB_MEMORY_BARRIER ();
176
177 /* start the workers */
178 tm->thread_barrier = 0;
179
180 while (tm->threads_running)
181 {
182 struct timespec ts, tsrem;
183 ts.tv_sec = 0;
184 ts.tv_nsec = 20 * 1000 * 1000; /* sleep for 20ms at a time */
185
186 while (nanosleep (&ts, &tsrem) < 0)
187 ts = tsrem;
188 }
189
190 return 0;
191}
192
193
Dave Barachba7ddfe2017-05-17 20:20:50 -0400194static clib_error_t *
Dave Barachc3799992016-08-15 11:12:27 -0400195test_bihash (test_main_t * tm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196{
197 int i, j;
Dave Barachc3799992016-08-15 11:12:27 -0400198 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 uword total_searches;
200 f64 before, delta;
Dave Barachc3799992016-08-15 11:12:27 -0400201 BVT (clib_bihash) * h;
202 BVT (clib_bihash_kv) kv;
Dave Barache7d212f2018-02-07 13:14:06 -0500203 u32 acycle;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
205 h = &tm->hash;
206
Dave Barach97f5af02018-02-22 09:48:45 -0500207 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Dave Barache7d212f2018-02-07 13:14:06 -0500209 for (acycle = 0; acycle < tm->ncycles; acycle++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 {
Dave Barache7d212f2018-02-07 13:14:06 -0500211 if ((acycle % tm->report_every_n) == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400212 {
Dave Barache7d212f2018-02-07 13:14:06 -0500213 fformat (stdout, "Cycle %lld out of %lld...\n",
214 acycle, tm->ncycles);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Dave Barache7d212f2018-02-07 13:14:06 -0500216 fformat (stdout, "Pick %lld unique %s keys...\n",
217 tm->nitems, tm->non_random_keys ? "non-random" : "random");
Dave Barachc3799992016-08-15 11:12:27 -0400218 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
Dave Barache7d212f2018-02-07 13:14:06 -0500220 for (i = 0; i < tm->nitems; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400221 {
Dave Barache7d212f2018-02-07 13:14:06 -0500222 u64 rndkey;
223
224 if (tm->non_random_keys == 0)
225 {
226
227 again:
228 rndkey = random_u64 (&tm->seed);
229
230 p = hash_get (tm->key_hash, rndkey);
231 if (p)
232 goto again;
233 }
234 else
235 rndkey = (u64) (i + 1) << 16;
236 rndkey += acycle;
237
238 hash_set (tm->key_hash, rndkey, i + 1);
239 vec_add1 (tm->keys, rndkey);
Dave Barachc3799992016-08-15 11:12:27 -0400240 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Dave Barache7d212f2018-02-07 13:14:06 -0500243 if ((acycle % tm->report_every_n) == 0)
244 fformat (stdout, "Add items...\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 for (i = 0; i < tm->nitems; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400247 {
Dave Barachc3799992016-08-15 11:12:27 -0400248 kv.key = tm->keys[i];
Dave Barache7d212f2018-02-07 13:14:06 -0500249 kv.value = i + 1;
250
251 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
252
253 if (tm->verbose > 1)
254 {
255 fformat (stdout, "--------------------\n");
256 fformat (stdout, "After adding key %llu value %lld...\n",
257 tm->keys[i], (u64) (i + 1));
258 fformat (stdout, "%U", BV (format_bihash), h,
259 2 /* very verbose */ );
260 }
Dave Barachc3799992016-08-15 11:12:27 -0400261 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
Dave Barache7d212f2018-02-07 13:14:06 -0500263 if ((acycle % tm->report_every_n) == 0)
264 {
265 fformat (stdout, "%U", BV (format_bihash), h,
266 0 /* very verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
Dave Barache7d212f2018-02-07 13:14:06 -0500268 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
269 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Dave Barache7d212f2018-02-07 13:14:06 -0500271 before = clib_time_now (&tm->clib_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
Dave Barache7d212f2018-02-07 13:14:06 -0500273 for (j = 0; j < tm->search_iter; j++)
274 {
275 for (i = 0; i < tm->nitems; i++)
276 {
277 kv.key = tm->keys[i];
278 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
279 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
280 clib_warning
281 ("[%d] search for key %lld failed unexpectedly\n", i,
282 tm->keys[i]);
283 if (kv.value != (u64) (i + 1))
284 clib_warning
285 ("[%d] search for key %lld returned %lld, not %lld\n", i,
286 tm->keys, kv.value, (u64) (i + 1));
287 }
288 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289
Dave Barache7d212f2018-02-07 13:14:06 -0500290 if ((acycle % tm->report_every_n) == 0)
291 {
292 delta = clib_time_now (&tm->clib_time) - before;
293 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Dave Barache7d212f2018-02-07 13:14:06 -0500295 if (delta > 0)
296 fformat (stdout, "%.f searches per second\n",
297 ((f64) total_searches) / delta);
298
299 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
300 delta);
301
302 fformat (stdout, "Standard E-hash search for items %d times...\n",
303 tm->search_iter);
304 }
305
306 before = clib_time_now (&tm->clib_time);
307
308 for (j = 0; j < tm->search_iter; j++)
309 {
310 for (i = 0; i < tm->nitems; i++)
311 {
312 p = hash_get (tm->key_hash, tm->keys[i]);
313 if (p == 0 || p[0] != (uword) (i + 1))
314 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
315 }
316 }
317
318 delta = clib_time_now (&tm->clib_time) - before;
319 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
320
321 if ((acycle % tm->report_every_n) == 0)
322 {
323 fformat (stdout, "%lld searches in %.6f seconds\n",
324 total_searches, delta);
325
326 if (delta > 0)
327 fformat (stdout, "%.f searches per second\n",
328 ((f64) total_searches) / delta);
329 fformat (stdout, "Delete items...\n");
330 }
331
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 for (i = 0; i < tm->nitems; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400333 {
Dave Barache7d212f2018-02-07 13:14:06 -0500334 int j;
335 int rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Dave Barache7d212f2018-02-07 13:14:06 -0500337 kv.key = tm->keys[i];
338 kv.value = (u64) (i + 1);
339 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Dave Barache7d212f2018-02-07 13:14:06 -0500341 if (rv < 0)
342 clib_warning ("delete key %lld not ok but should be",
343 tm->keys[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
Dave Barache7d212f2018-02-07 13:14:06 -0500345 if (tm->careful_delete_tests)
Dave Barachc3799992016-08-15 11:12:27 -0400346 {
Dave Barache7d212f2018-02-07 13:14:06 -0500347 for (j = 0; j < tm->nitems; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400348 {
Dave Barache7d212f2018-02-07 13:14:06 -0500349 kv.key = tm->keys[j];
350 rv = BV (clib_bihash_search) (h, &kv, &kv);
351 if (j <= i && rv >= 0)
352 {
353 clib_warning
354 ("i %d j %d search ok but should not be, value %lld",
355 i, j, kv.value);
356 }
357 if (j > i && rv < 0)
358 {
359 clib_warning ("i %d j %d search not ok but should be",
360 i, j);
361 }
Dave Barachc3799992016-08-15 11:12:27 -0400362 }
363 }
364 }
Dave Barache7d212f2018-02-07 13:14:06 -0500365 if ((acycle % tm->report_every_n) == 0)
366 {
367 struct rusage r_usage;
368 getrusage (RUSAGE_SELF, &r_usage);
369 fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
370 fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
371 }
372
373 /* Clean up side-bet hash table and random key vector */
Dave Barach97f5af02018-02-22 09:48:45 -0500374 hash_free (tm->key_hash);
Dave Barache7d212f2018-02-07 13:14:06 -0500375 vec_reset_length (tm->keys);
Dave Barach97f5af02018-02-22 09:48:45 -0500376 /* Recreate hash table if we're going to need it again */
377 if (acycle != (tm->ncycles - 1))
378 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 }
380
Dave Barache7d212f2018-02-07 13:14:06 -0500381 fformat (stdout, "End of run, should be empty...\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382
Dave Barachc3799992016-08-15 11:12:27 -0400383 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 return 0;
385}
386
Dave Barachc3799992016-08-15 11:12:27 -0400387clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388test_bihash_main (test_main_t * tm)
389{
Dave Barachc3799992016-08-15 11:12:27 -0400390 unformat_input_t *i = tm->input;
391 clib_error_t *error;
Dave Barach908a5ea2017-07-14 12:42:21 -0400392 int which = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393
Dave Barache7d212f2018-02-07 13:14:06 -0500394 tm->report_every_n = 1;
Dave Barach97f5af02018-02-22 09:48:45 -0500395 tm->hash_memory_size = 4095ULL << 20;
Dave Barache7d212f2018-02-07 13:14:06 -0500396
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
398 {
399 if (unformat (i, "seed %u", &tm->seed))
Dave Barachc3799992016-08-15 11:12:27 -0400400 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401
402 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
Dave Barachc3799992016-08-15 11:12:27 -0400403 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 else if (unformat (i, "non-random-keys"))
Dave Barachc3799992016-08-15 11:12:27 -0400405 tm->non_random_keys = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 else if (unformat (i, "nitems %d", &tm->nitems))
Dave Barachc3799992016-08-15 11:12:27 -0400407 ;
Dave Barache7d212f2018-02-07 13:14:06 -0500408 else if (unformat (i, "ncycles %d", &tm->ncycles))
409 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
Dave Barachc3799992016-08-15 11:12:27 -0400411 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 else if (unformat (i, "verbose %d", &tm->verbose))
Dave Barachc3799992016-08-15 11:12:27 -0400413 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414 else if (unformat (i, "search %d", &tm->search_iter))
Dave Barachc3799992016-08-15 11:12:27 -0400415 ;
Dave Barache7d212f2018-02-07 13:14:06 -0500416 else if (unformat (i, "report-every %d", &tm->report_every_n))
417 ;
Dave Barach97f5af02018-02-22 09:48:45 -0500418 else if (unformat (i, "memory-size %U",
419 unformat_memory_size, &tm->hash_memory_size))
420 ;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400421 else if (unformat (i, "vec64"))
Dave Barach908a5ea2017-07-14 12:42:21 -0400422 which = 1;
Dave Barach508498f2018-07-19 12:11:16 -0400423 else if (unformat (i, "threads %u", &tm->nthreads))
Dave Barach908a5ea2017-07-14 12:42:21 -0400424 which = 2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 else if (unformat (i, "verbose"))
Dave Barachc3799992016-08-15 11:12:27 -0400426 tm->verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 else
Dave Barachc3799992016-08-15 11:12:27 -0400428 return clib_error_return (0, "unknown input '%U'",
429 format_unformat_error, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 }
Dave Barachc3799992016-08-15 11:12:27 -0400431
Dave Barach97f5af02018-02-22 09:48:45 -0500432 /* Preallocate hash table, key vector */
433 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
434 vec_validate (tm->keys, tm->nitems - 1);
435 _vec_len (tm->keys) = 0;
436
437
Dave Barach908a5ea2017-07-14 12:42:21 -0400438 switch (which)
439 {
440 case 0:
441 error = test_bihash (tm);
442 break;
443
444 case 1:
445 error = test_bihash_vec64 (tm);
446 break;
447
448 case 2:
Dave Barach508498f2018-07-19 12:11:16 -0400449 error = test_bihash_threads (tm);
Dave Barach908a5ea2017-07-14 12:42:21 -0400450 break;
451
452 default:
453 return clib_error_return (0, "no such test?");
454 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455
456 return error;
457}
458
459#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400460int
461main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462{
463 unformat_input_t i;
Dave Barachc3799992016-08-15 11:12:27 -0400464 clib_error_t *error;
465 test_main_t *tm = &test_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466
Dave Barach97f5af02018-02-22 09:48:45 -0500467 clib_mem_init (0, 4095ULL << 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Dave Barach508498f2018-07-19 12:11:16 -0400469 tm->global_heap = clib_mem_get_per_cpu_heap ();
470
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 tm->input = &i;
472 tm->seed = 0xdeaddabe;
473
474 tm->nbuckets = 2;
475 tm->nitems = 5;
Dave Barache7d212f2018-02-07 13:14:06 -0500476 tm->ncycles = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 tm->verbose = 1;
478 tm->search_iter = 1;
479 tm->careful_delete_tests = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 clib_time_init (&tm->clib_time);
481
482 unformat_init_command_line (&i, argv);
483 error = test_bihash_main (tm);
484 unformat_free (&i);
485
486 if (error)
487 {
488 clib_error_report (error);
489 return 1;
490 }
491 return 0;
492}
493#endif /* CLIB_UNIX */
Dave Barachc3799992016-08-15 11:12:27 -0400494
495/*
496 * fd.io coding-style-patch-verification: ON
497 *
498 * Local Variables:
499 * eval: (c-set-style "gnu")
500 * End:
501 */