2 * Copyright (c) 2017 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 __thread int thread_id = 0;
18 #include <vppinfra/time.h>
19 #include <vppinfra/cache.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/heap.h>
22 #include <vppinfra/format.h>
23 #include <vppinfra/pool.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/hash.h>
26 #include <vppinfra/cache.h>
28 #define os_get_cpu_number() (thread_id)
30 #include <vppinfra/cuckoo_8_8.h>
31 #include <vppinfra/cuckoo_template.h>
32 #include <vppinfra/cuckoo_template.c>
34 #include <vppinfra/bihash_8_8.h>
35 #include <vppinfra/bihash_template.h>
36 #include <vppinfra/bihash_template.c>
40 #define MAX_THREADS 255
64 clib_time_t clib_time;
65 u64 *key_add_del_sequence;
67 u64 *key_search_sequence[MAX_THREADS];
68 unformat_input_t *input;
71 pthread_t bwriter_thread;
72 pthread_t breader_threads[MAX_THREADS];
73 pthread_t cwriter_thread;
74 pthread_t creader_threads[MAX_THREADS];
75 thread_data_t wthread_data;
76 thread_data_t rthread_data[MAX_THREADS];
79 test_main_t test_main;
87 #define w_thread(x, guts) \
88 void *x##writer_thread (void *v) \
90 test_main_t *tm = v; \
94 u64 deadline = tm->deadline; \
97 for (counter = 0; counter < vec_len (tm->key_add_del_sequence); \
100 u64 idx = tm->key_add_del_sequence[counter]; \
101 u8 op = tm->key_op_sequence[counter]; \
111 if (clib_cpu_time_now () > deadline) \
117 while (clib_cpu_time_now () < deadline); \
125 BVT (clib_bihash_kv) kv;
126 kv.key = tm->keys[idx];
127 kv.value = *hash_get (tm->key_hash, kv.key);
128 BV (clib_bihash_add_del) (&tm->bh, &kv, op);
134 CVT (clib_cuckoo_kv) kv;
135 kv.key = tm->keys[idx];
136 kv.value = *hash_get (tm->key_hash, kv.key);
137 CV (clib_cuckoo_add_del) (&tm->ch, &kv, op);
141 #define r_thread(x, guts) \
142 void *x##reader_thread (void *v) \
144 thread_data_t *data = v; \
145 thread_id = data->thread_idx; \
146 test_main_t *tm = data->tm; \
147 uword thread_idx = data->thread_idx; \
149 uword nlookups = 0; \
150 u64 deadline = tm->deadline; \
153 vec_foreach (idx, tm->key_search_sequence[thread_idx]) \
157 if (clib_cpu_time_now () > deadline) \
163 while (clib_cpu_time_now () < deadline); \
164 data->nlookups = nlookups; \
170 CVT (clib_cuckoo_kv) kv;
171 kv.key = tm->keys[*idx];
172 kv.value = *hash_get (tm->key_hash, kv.key);
173 CV (clib_cuckoo_search) (&tm->ch, &kv, &kv);
179 BVT (clib_bihash_kv) kv;
180 kv.key = tm->keys[*idx];
181 kv.value = *hash_get (tm->key_hash, kv.key);
182 BV (clib_bihash_search) (&tm->bh, &kv, &kv);
186 #define run_threads(x) \
190 before = clib_time_now (&tm->clib_time); \
191 tm->deadline = clib_cpu_time_now () + \
192 tm->runtime * tm->clib_time.clocks_per_second; \
193 fformat (stdout, #x "-> Start threads..., runtime is %llu second(s)\n", \
194 (long long unsigned)tm->runtime); \
197 fformat (stdout, #x "-> Writer thread only...\n"); \
199 pthread_create (&tm->x##writer_thread, NULL, x##writer_thread, tm)) \
201 perror ("pthread_create()"); \
205 if (0 != pthread_join (tm->x##writer_thread, NULL)) \
207 perror ("pthread_join()"); \
211 delta = clib_time_now (&tm->clib_time) - before; \
212 fformat (stdout, #x "-> %wu adds, %wu dels in %.6f seconds\n", \
213 tm->nadds, tm->ndels, delta); \
218 fformat (stdout, #x "-> Writer + %d readers\n", tm->nthreads); \
219 before = clib_time_now (&tm->clib_time); \
220 tm->deadline = clib_cpu_time_now () + \
221 tm->runtime * tm->clib_time.clocks_per_second; \
223 pthread_create (&tm->x##writer_thread, NULL, x##writer_thread, tm)) \
225 perror ("pthread_create()"); \
229 for (i = 0; i < tm->nthreads; i++) \
231 tm->rthread_data[i].nlookups = 0; \
232 if (0 != pthread_create (&tm->x##reader_threads[i], NULL, \
233 x##reader_thread, &tm->rthread_data[i])) \
235 perror ("pthread_create()"); \
240 if (0 != pthread_join (tm->x##writer_thread, NULL)) \
242 perror ("pthread_join()"); \
246 for (i = 0; i < tm->nthreads; i++) \
248 if (0 != pthread_join (tm->x##reader_threads[i], NULL)) \
250 perror ("pthread_join()"); \
255 delta = clib_time_now (&tm->clib_time) - before; \
257 total_searches = 0; \
258 for (i = 0; i < tm->nthreads; ++i) \
260 u64 nlookups = tm->rthread_data[i].nlookups; \
261 fformat (stdout, #x "-> Thread #%d: %u searches\n", i, nlookups); \
262 total_searches += nlookups; \
267 ops = (tm->nadds + tm->ndels) / (f64)delta; \
268 fformat (stdout, #x "-> %.f add/dels per second\n", ops); \
269 sps = ((f64)total_searches) / delta; \
270 fformat (stdout, #x "-> %.f searches per second\n", sps); \
274 #x "-> %wu adds, %wu dels, %lld searches in %.6f seconds\n", \
275 tm->nadds, tm->ndels, total_searches, delta); \
280 cb (CVT (clib_cuckoo) * h, void *ctx)
282 fformat (stdout, "Garbage callback called...\n");
285 static clib_error_t *
286 test_cuckoo_bihash (test_main_t * tm)
290 uword total_searches;
292 f64 ops = 0, sps = 0;
293 f64 bops = 0, bsps = 0;
294 f64 cops = 0, csps = 0;
295 CVT (clib_cuckoo) * ch;
296 BVT (clib_bihash) * bh;
301 CV (clib_cuckoo_init) (ch, "test", 1, cb, NULL);
302 BV (clib_bihash_init) (bh, (char *) "test", tm->nbuckets, 256 << 20);
304 fformat (stdout, "Pick %lld unique %s keys...\n", tm->nitems,
305 tm->non_random_keys ? "non-random" : "random");
307 for (i = 0; i < tm->nitems; i++)
311 if (tm->non_random_keys == 0)
315 rndkey = random_u64 (&tm->seed);
317 p = hash_get (tm->key_hash, rndkey);
322 rndkey = (u64) (i + 1) << 16;
324 hash_set (tm->key_hash, rndkey, i + 1);
325 vec_add1 (tm->keys, rndkey);
328 for (j = 0; j < tm->nthreads; ++j)
330 u64 *x = tm->key_search_sequence[j];
331 vec_add1 (x, random_u64 (&tm->seed) % tm->nitems);
332 tm->key_search_sequence[j] = x;
334 vec_add1 (tm->key_add_del_sequence,
335 random_u64 (&tm->seed) % tm->nitems);
336 vec_add1 (tm->key_op_sequence, (rndkey % 10 < 8) ? 1 : 0);
339 int thread_counter = 0;
340 tm->wthread_data.tm = tm;
341 tm->wthread_data.thread_idx = thread_counter;
342 for (i = 0; i < tm->nthreads; ++i)
344 tm->rthread_data[i].tm = tm;
345 tm->rthread_data[i].thread_idx = thread_counter;
346 tm->rthread_data[i].nlookups = 0;
351 for (iter = 0; iter < tm->search_iter; ++iter)
353 fformat (stdout, "Bihash test #%d\n", iter);
357 fformat (stdout, "%U", BV (format_bihash), bh, 0);
358 fformat (stdout, "Cuckoo test #%d\n", iter);
362 fformat (stdout, "%U", CV (format_cuckoo), ch, 0);
364 "Bihash add/del speed is %.2f%% of cuckoo add/del speed\n",
367 "Bihash search speed is %.2f%% of cuckoo search speed\n",
374 test_cuckoo_bihash_main (test_main_t * tm)
376 unformat_input_t *i = tm->input;
379 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
381 if (unformat (i, "seed %u", &tm->seed))
383 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
385 else if (unformat (i, "non-random-keys"))
386 tm->non_random_keys = 1;
387 else if (unformat (i, "nitems %d", &tm->nitems))
389 else if (unformat (i, "search_iter %d", &tm->search_iter))
391 else if (unformat (i, "verbose %d", &tm->verbose))
393 else if (unformat (i, "runtime %d", &tm->runtime))
395 else if (unformat (i, "nthreads %d", &tm->nthreads))
397 else if (unformat (i, "verbose"))
400 return clib_error_return (0, "unknown input '%U'",
401 format_unformat_error, i);
404 error = test_cuckoo_bihash (tm);
411 main (int argc, char *argv[])
415 test_main_t *tm = &test_main;
416 clib_memset (&test_main, 0, sizeof (test_main));
418 clib_mem_init (0, 3ULL << 30);
421 tm->seed = 0xdeaddabe;
427 clib_time_init (&tm->clib_time);
430 tm->key_hash = hash_create (0, sizeof (uword));
432 unformat_init_command_line (&i, argv);
433 error = test_cuckoo_bihash_main (tm);
438 clib_error_report (error);
443 #endif /* CLIB_UNIX */
446 * fd.io coding-style-patch-verification: ON
449 * eval: (c-set-style "gnu")