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:
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.
15 #include <vppinfra/time.h>
16 #include <vppinfra/cache.h>
17 #include <vppinfra/error.h>
18 #include <sys/resource.h>
21 #include <vppinfra/bihash_8_8.h>
22 #include <vppinfra/bihash_template.h>
24 #include <vppinfra/bihash_template.c>
34 int careful_delete_tests;
39 uword hash_memory_size;
40 BVT (clib_bihash) hash;
41 clib_time_t clib_time;
43 unformat_input_t *input;
47 test_main_t test_main;
56 test_bihash_vec64 (test_main_t * tm)
58 u32 user_buckets = 1228800;
59 u32 user_memory_size = 209715200;
60 BVT (clib_bihash_kv) kv;
64 BVT (clib_bihash) * h;
68 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
70 before = clib_time_now (&tm->clib_time);
72 for (j = 0; j < 10; j++)
74 for (i = 1; i <= j * 1000 + 1; i++)
79 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
82 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
85 for (j = 0; j < vec_len (cum_times); j++)
86 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
93 test_bihash (test_main_t * tm)
99 BVT (clib_bihash) * h;
100 BVT (clib_bihash_kv) kv;
105 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
107 for (acycle = 0; acycle < tm->ncycles; acycle++)
109 if ((acycle % tm->report_every_n) == 0)
111 fformat (stdout, "Cycle %lld out of %lld...\n",
112 acycle, tm->ncycles);
114 fformat (stdout, "Pick %lld unique %s keys...\n",
115 tm->nitems, tm->non_random_keys ? "non-random" : "random");
118 for (i = 0; i < tm->nitems; i++)
122 if (tm->non_random_keys == 0)
126 rndkey = random_u64 (&tm->seed);
128 p = hash_get (tm->key_hash, rndkey);
133 rndkey = (u64) (i + 1) << 16;
136 hash_set (tm->key_hash, rndkey, i + 1);
137 vec_add1 (tm->keys, rndkey);
141 if ((acycle % tm->report_every_n) == 0)
142 fformat (stdout, "Add items...\n");
144 for (i = 0; i < tm->nitems; i++)
146 kv.key = tm->keys[i];
149 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
153 fformat (stdout, "--------------------\n");
154 fformat (stdout, "After adding key %llu value %lld...\n",
155 tm->keys[i], (u64) (i + 1));
156 fformat (stdout, "%U", BV (format_bihash), h,
157 2 /* very verbose */ );
161 if ((acycle % tm->report_every_n) == 0)
163 fformat (stdout, "%U", BV (format_bihash), h,
164 0 /* very verbose */ );
166 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
169 before = clib_time_now (&tm->clib_time);
171 for (j = 0; j < tm->search_iter; j++)
173 for (i = 0; i < tm->nitems; i++)
175 kv.key = tm->keys[i];
176 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
177 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
179 ("[%d] search for key %lld failed unexpectedly\n", i,
181 if (kv.value != (u64) (i + 1))
183 ("[%d] search for key %lld returned %lld, not %lld\n", i,
184 tm->keys, kv.value, (u64) (i + 1));
188 if ((acycle % tm->report_every_n) == 0)
190 delta = clib_time_now (&tm->clib_time) - before;
191 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
194 fformat (stdout, "%.f searches per second\n",
195 ((f64) total_searches) / delta);
197 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
200 fformat (stdout, "Standard E-hash search for items %d times...\n",
204 before = clib_time_now (&tm->clib_time);
206 for (j = 0; j < tm->search_iter; j++)
208 for (i = 0; i < tm->nitems; i++)
210 p = hash_get (tm->key_hash, tm->keys[i]);
211 if (p == 0 || p[0] != (uword) (i + 1))
212 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
216 delta = clib_time_now (&tm->clib_time) - before;
217 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
219 if ((acycle % tm->report_every_n) == 0)
221 fformat (stdout, "%lld searches in %.6f seconds\n",
222 total_searches, delta);
225 fformat (stdout, "%.f searches per second\n",
226 ((f64) total_searches) / delta);
227 fformat (stdout, "Delete items...\n");
230 for (i = 0; i < tm->nitems; i++)
235 kv.key = tm->keys[i];
236 kv.value = (u64) (i + 1);
237 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
240 clib_warning ("delete key %lld not ok but should be",
243 if (tm->careful_delete_tests)
245 for (j = 0; j < tm->nitems; j++)
247 kv.key = tm->keys[j];
248 rv = BV (clib_bihash_search) (h, &kv, &kv);
249 if (j <= i && rv >= 0)
252 ("i %d j %d search ok but should not be, value %lld",
257 clib_warning ("i %d j %d search not ok but should be",
263 if ((acycle % tm->report_every_n) == 0)
265 struct rusage r_usage;
266 getrusage (RUSAGE_SELF, &r_usage);
267 fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
268 fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
271 /* Clean up side-bet hash table and random key vector */
272 hash_free (tm->key_hash);
273 vec_reset_length (tm->keys);
274 /* Recreate hash table if we're going to need it again */
275 if (acycle != (tm->ncycles - 1))
276 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
279 fformat (stdout, "End of run, should be empty...\n");
281 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
286 test_bihash_cache (test_main_t * tm)
289 BVT (clib_bihash_bucket) _b, *b = &_b;
291 BV (clib_bihash_reset_cache) (b);
293 fformat (stdout, "Initial LRU config: %U\n", BV (format_bihash_lru), b);
295 BV (clib_bihash_update_lru_not_inline) (b, 3);
297 fformat (stdout, "use slot 3, LRU config: %U\n", BV (format_bihash_lru), b);
299 BV (clib_bihash_update_lru) (b, 1);
301 fformat (stdout, "use slot 1 LRU config: %U\n", BV (format_bihash_lru), b);
303 lru = BV (clib_bihash_get_lru) (b);
305 fformat (stdout, "least-recently-used is %d\n", lru);
307 BV (clib_bihash_update_lru) (b, 4);
309 fformat (stdout, "use slot 4 LRU config: %U\n", BV (format_bihash_lru), b);
311 lru = BV (clib_bihash_get_lru) (b);
313 fformat (stdout, "least-recently-used is %d\n", lru);
319 test_bihash_main (test_main_t * tm)
321 unformat_input_t *i = tm->input;
325 tm->report_every_n = 1;
326 tm->hash_memory_size = 4095ULL << 20;
328 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
330 if (unformat (i, "seed %u", &tm->seed))
333 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
335 else if (unformat (i, "non-random-keys"))
336 tm->non_random_keys = 1;
337 else if (unformat (i, "nitems %d", &tm->nitems))
339 else if (unformat (i, "ncycles %d", &tm->ncycles))
341 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
343 else if (unformat (i, "verbose %d", &tm->verbose))
345 else if (unformat (i, "search %d", &tm->search_iter))
347 else if (unformat (i, "report-every %d", &tm->report_every_n))
349 else if (unformat (i, "memory-size %U",
350 unformat_memory_size, &tm->hash_memory_size))
352 else if (unformat (i, "vec64"))
354 else if (unformat (i, "cache"))
357 else if (unformat (i, "verbose"))
360 return clib_error_return (0, "unknown input '%U'",
361 format_unformat_error, i);
364 /* Preallocate hash table, key vector */
365 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
366 vec_validate (tm->keys, tm->nitems - 1);
367 _vec_len (tm->keys) = 0;
373 error = test_bihash (tm);
377 error = test_bihash_vec64 (tm);
381 error = test_bihash_cache (tm);
385 return clib_error_return (0, "no such test?");
393 main (int argc, char *argv[])
397 test_main_t *tm = &test_main;
399 clib_mem_init (0, 4095ULL << 20);
402 tm->seed = 0xdeaddabe;
409 tm->careful_delete_tests = 0;
410 clib_time_init (&tm->clib_time);
412 unformat_init_command_line (&i, argv);
413 error = test_bihash_main (tm);
418 clib_error_report (error);
423 #endif /* CLIB_UNIX */
426 * fd.io coding-style-patch-verification: ON
429 * eval: (c-set-style "gnu")