blob: 589c815dff1a1ecda9ed93d35ae9437fe60a4380 [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>
18
19#include <vppinfra/bihash_8_8.h>
20#include <vppinfra/bihash_template.h>
21
22#include <vppinfra/bihash_template.c>
23
Dave Barachc3799992016-08-15 11:12:27 -040024typedef struct
25{
Filip Tehlar397fd7d2016-10-26 14:31:24 +020026 u64 seed;
Ed Warnickecb9cada2015-12-08 15:45:58 -070027 u32 nbuckets;
28 u32 nitems;
29 u32 search_iter;
30 int careful_delete_tests;
31 int verbose;
32 int non_random_keys;
Dave Barachc3799992016-08-15 11:12:27 -040033 uword *key_hash;
34 u64 *keys;
35 BVT (clib_bihash) hash;
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 clib_time_t clib_time;
37
Dave Barachc3799992016-08-15 11:12:27 -040038 unformat_input_t *input;
39
Ed Warnickecb9cada2015-12-08 15:45:58 -070040} test_main_t;
41
42test_main_t test_main;
43
Dave Barachc3799992016-08-15 11:12:27 -040044uword
45vl (void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
Dave Barachc3799992016-08-15 11:12:27 -040047 return vec_len (v);
Ed Warnickecb9cada2015-12-08 15:45:58 -070048}
49
Dave Barachc3799992016-08-15 11:12:27 -040050static clib_error_t *
Dave Barachba7ddfe2017-05-17 20:20:50 -040051test_bihash_vec64 (test_main_t * tm)
52{
53 u32 user_buckets = 1228800;
54 u32 user_memory_size = 209715200;
55 BVT (clib_bihash_kv) kv;
56 int i, j;
57 f64 before;
58 f64 *cum_times = 0;
59 BVT (clib_bihash) * h;
60
61 h = &tm->hash;
62
63 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
64
65 before = clib_time_now (&tm->clib_time);
66
67 for (j = 0; j < 10; j++)
68 {
69 for (i = 1; i <= j * 1000 + 1; i++)
70 {
71 kv.key = i;
72 kv.value = 1;
73
74 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
75 }
76
77 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
78 }
79
80 for (j = 0; j < vec_len (cum_times); j++)
81 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
82 cum_times[j] * 1e6);
83
84 return 0;
85}
86
87static clib_error_t *
Dave Barachc3799992016-08-15 11:12:27 -040088test_bihash (test_main_t * tm)
Ed Warnickecb9cada2015-12-08 15:45:58 -070089{
90 int i, j;
Dave Barachc3799992016-08-15 11:12:27 -040091 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 uword total_searches;
93 f64 before, delta;
Dave Barachc3799992016-08-15 11:12:27 -040094 BVT (clib_bihash) * h;
95 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 h = &tm->hash;
98
Dave Barachc3799992016-08-15 11:12:27 -040099 BV (clib_bihash_init) (h, "test", tm->nbuckets, 3ULL << 30);
100
101 fformat (stdout, "Pick %lld unique %s keys...\n",
102 tm->nitems, tm->non_random_keys ? "non-random" : "random");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
104 for (i = 0; i < tm->nitems; i++)
105 {
106 u64 rndkey;
107
108 if (tm->non_random_keys == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400109 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
Dave Barachc3799992016-08-15 11:12:27 -0400111 again:
112 rndkey = random_u64 (&tm->seed);
113
114 p = hash_get (tm->key_hash, rndkey);
115 if (p)
116 goto again;
117 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 else
Dave Barachc3799992016-08-15 11:12:27 -0400119 rndkey = (u64) (i + 1) << 16;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
Dave Barachc3799992016-08-15 11:12:27 -0400121 hash_set (tm->key_hash, rndkey, i + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 vec_add1 (tm->keys, rndkey);
123 }
124
125 fformat (stdout, "Add items...\n");
126 for (i = 0; i < tm->nitems; i++)
127 {
128 kv.key = tm->keys[i];
Dave Barachc3799992016-08-15 11:12:27 -0400129 kv.value = i + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
Dave Barachc3799992016-08-15 11:12:27 -0400131 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
133 if (tm->verbose > 1)
Dave Barachc3799992016-08-15 11:12:27 -0400134 {
135 fformat (stdout, "--------------------\n");
136 fformat (stdout, "After adding key %llu value %lld...\n",
137 tm->keys[i], (u64) (i + 1));
138 fformat (stdout, "%U", BV (format_bihash), h,
139 2 /* very verbose */ );
140 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 }
142
Dave Barachc3799992016-08-15 11:12:27 -0400143 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
145 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
146
147 before = clib_time_now (&tm->clib_time);
148
149 for (j = 0; j < tm->search_iter; j++)
150 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 for (i = 0; i < tm->nitems; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400152 {
Dave Barachc3799992016-08-15 11:12:27 -0400153 kv.key = tm->keys[i];
154 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
Dave Barach5e6b9582016-12-12 15:37:29 -0500155 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
156 clib_warning ("[%d] search for key %lld failed unexpectedly\n",
157 i, tm->keys[i]);
Dave Barachc3799992016-08-15 11:12:27 -0400158 if (kv.value != (u64) (i + 1))
Dave Barach5e6b9582016-12-12 15:37:29 -0500159 clib_warning
160 ("[%d] search for key %lld returned %lld, not %lld\n", i,
161 tm->keys, kv.value, (u64) (i + 1));
Dave Barachc3799992016-08-15 11:12:27 -0400162 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 }
164
165 delta = clib_time_now (&tm->clib_time) - before;
Dave Barachc3799992016-08-15 11:12:27 -0400166 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
168 if (delta > 0)
169 fformat (stdout, "%.f searches per second\n",
Dave Barachc3799992016-08-15 11:12:27 -0400170 ((f64) total_searches) / delta);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
173
Dave Barachc3799992016-08-15 11:12:27 -0400174 fformat (stdout, "Standard E-hash search for items %d times...\n",
175 tm->search_iter);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
177 before = clib_time_now (&tm->clib_time);
178
179 for (j = 0; j < tm->search_iter; j++)
180 {
181 for (i = 0; i < tm->nitems; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400182 {
183 p = hash_get (tm->key_hash, tm->keys[i]);
184 if (p == 0 || p[0] != (uword) (i + 1))
185 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
186 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 }
188
189 delta = clib_time_now (&tm->clib_time) - before;
Dave Barachc3799992016-08-15 11:12:27 -0400190 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
192 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
193
194 if (delta > 0)
195 fformat (stdout, "%.f searches per second\n",
Dave Barachc3799992016-08-15 11:12:27 -0400196 ((f64) total_searches) / delta);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
198 fformat (stdout, "Delete items...\n");
199
200 for (i = 0; i < tm->nitems; i++)
201 {
202 int j;
203 int rv;
204
205 kv.key = tm->keys[i];
Dave Barachc3799992016-08-15 11:12:27 -0400206 kv.value = (u64) (i + 1);
207 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
209 if (rv < 0)
Dave Barachc3799992016-08-15 11:12:27 -0400210 clib_warning ("delete key %lld not ok but should be", tm->keys[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
212 if (tm->careful_delete_tests)
Dave Barachc3799992016-08-15 11:12:27 -0400213 {
214 for (j = 0; j < tm->nitems; j++)
215 {
216 kv.key = tm->keys[j];
217 rv = BV (clib_bihash_search) (h, &kv, &kv);
218 if (j <= i && rv >= 0)
219 {
220 clib_warning
221 ("i %d j %d search ok but should not be, value %lld",
222 i, j, kv.value);
223 }
224 if (j > i && rv < 0)
225 {
226 clib_warning ("i %d j %d search not ok but should be",
227 i, j);
228 }
229 }
230 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 }
232
233 fformat (stdout, "After deletions, should be empty...\n");
234
Dave Barachc3799992016-08-15 11:12:27 -0400235 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 return 0;
237}
238
Dave Barachc3799992016-08-15 11:12:27 -0400239clib_error_t *
Dave Barach908a5ea2017-07-14 12:42:21 -0400240test_bihash_cache (test_main_t * tm)
241{
242 u32 lru;
243 BVT (clib_bihash_bucket) _b, *b = &_b;
244
245 BV (clib_bihash_reset_cache) (b);
246
247 fformat (stdout, "Initial LRU config: %U\n", BV (format_bihash_lru), b);
248
249 BV (clib_bihash_update_lru_not_inline) (b, 3);
250
251 fformat (stdout, "use slot 3, LRU config: %U\n", BV (format_bihash_lru), b);
252
253 BV (clib_bihash_update_lru) (b, 1);
254
255 fformat (stdout, "use slot 1 LRU config: %U\n", BV (format_bihash_lru), b);
256
257 lru = BV (clib_bihash_get_lru) (b);
258
259 fformat (stdout, "least-recently-used is %d\n", lru);
260
261 BV (clib_bihash_update_lru) (b, 4);
262
263 fformat (stdout, "use slot 4 LRU config: %U\n", BV (format_bihash_lru), b);
264
265 lru = BV (clib_bihash_get_lru) (b);
266
267 fformat (stdout, "least-recently-used is %d\n", lru);
268
269 return 0;
270}
271
272clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273test_bihash_main (test_main_t * tm)
274{
Dave Barachc3799992016-08-15 11:12:27 -0400275 unformat_input_t *i = tm->input;
276 clib_error_t *error;
Dave Barach908a5ea2017-07-14 12:42:21 -0400277 int which = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
279 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
280 {
281 if (unformat (i, "seed %u", &tm->seed))
Dave Barachc3799992016-08-15 11:12:27 -0400282 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
284 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
Dave Barachc3799992016-08-15 11:12:27 -0400285 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286 else if (unformat (i, "non-random-keys"))
Dave Barachc3799992016-08-15 11:12:27 -0400287 tm->non_random_keys = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 else if (unformat (i, "nitems %d", &tm->nitems))
Dave Barachc3799992016-08-15 11:12:27 -0400289 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
Dave Barachc3799992016-08-15 11:12:27 -0400291 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 else if (unformat (i, "verbose %d", &tm->verbose))
Dave Barachc3799992016-08-15 11:12:27 -0400293 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 else if (unformat (i, "search %d", &tm->search_iter))
Dave Barachc3799992016-08-15 11:12:27 -0400295 ;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400296 else if (unformat (i, "vec64"))
Dave Barach908a5ea2017-07-14 12:42:21 -0400297 which = 1;
298 else if (unformat (i, "cache"))
299 which = 2;
300
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 else if (unformat (i, "verbose"))
Dave Barachc3799992016-08-15 11:12:27 -0400302 tm->verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 else
Dave Barachc3799992016-08-15 11:12:27 -0400304 return clib_error_return (0, "unknown input '%U'",
305 format_unformat_error, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 }
Dave Barachc3799992016-08-15 11:12:27 -0400307
Dave Barach908a5ea2017-07-14 12:42:21 -0400308 switch (which)
309 {
310 case 0:
311 error = test_bihash (tm);
312 break;
313
314 case 1:
315 error = test_bihash_vec64 (tm);
316 break;
317
318 case 2:
319 error = test_bihash_cache (tm);
320 break;
321
322 default:
323 return clib_error_return (0, "no such test?");
324 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
326 return error;
327}
328
329#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400330int
331main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332{
333 unformat_input_t i;
Dave Barachc3799992016-08-15 11:12:27 -0400334 clib_error_t *error;
335 test_main_t *tm = &test_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Dave Barachc3799992016-08-15 11:12:27 -0400337 clib_mem_init (0, 3ULL << 30);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
339 tm->input = &i;
340 tm->seed = 0xdeaddabe;
341
342 tm->nbuckets = 2;
343 tm->nitems = 5;
344 tm->verbose = 1;
345 tm->search_iter = 1;
346 tm->careful_delete_tests = 0;
347 tm->key_hash = hash_create (0, sizeof (uword));
348 clib_time_init (&tm->clib_time);
349
350 unformat_init_command_line (&i, argv);
351 error = test_bihash_main (tm);
352 unformat_free (&i);
353
354 if (error)
355 {
356 clib_error_report (error);
357 return 1;
358 }
359 return 0;
360}
361#endif /* CLIB_UNIX */
Dave Barachc3799992016-08-15 11:12:27 -0400362
363/*
364 * fd.io coding-style-patch-verification: ON
365 *
366 * Local Variables:
367 * eval: (c-set-style "gnu")
368 * End:
369 */