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>
19 #include <vppinfra/bihash_8_8.h>
20 #include <vppinfra/bihash_template.h>
22 #include <vppinfra/bihash_template.c>
30 int careful_delete_tests;
35 BVT (clib_bihash) hash;
36 clib_time_t clib_time;
38 unformat_input_t *input;
42 test_main_t test_main;
51 test_bihash_vec64 (test_main_t * tm)
53 u32 user_buckets = 1228800;
54 u32 user_memory_size = 209715200;
55 BVT (clib_bihash_kv) kv;
59 BVT (clib_bihash) * h;
63 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
65 before = clib_time_now (&tm->clib_time);
67 for (j = 0; j < 10; j++)
69 for (i = 1; i <= j * 1000 + 1; i++)
74 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
77 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
80 for (j = 0; j < vec_len (cum_times); j++)
81 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
88 test_bihash (test_main_t * tm)
94 BVT (clib_bihash) * h;
95 BVT (clib_bihash_kv) kv;
99 BV (clib_bihash_init) (h, "test", tm->nbuckets, 3ULL << 30);
101 fformat (stdout, "Pick %lld unique %s keys...\n",
102 tm->nitems, tm->non_random_keys ? "non-random" : "random");
104 for (i = 0; i < tm->nitems; i++)
108 if (tm->non_random_keys == 0)
112 rndkey = random_u64 (&tm->seed);
114 p = hash_get (tm->key_hash, rndkey);
119 rndkey = (u64) (i + 1) << 16;
121 hash_set (tm->key_hash, rndkey, i + 1);
122 vec_add1 (tm->keys, rndkey);
125 fformat (stdout, "Add items...\n");
126 for (i = 0; i < tm->nitems; i++)
128 kv.key = tm->keys[i];
131 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
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 */ );
143 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
145 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
147 before = clib_time_now (&tm->clib_time);
149 for (j = 0; j < tm->search_iter; j++)
151 for (i = 0; i < tm->nitems; i++)
153 kv.key = tm->keys[i];
154 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
155 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
156 clib_warning ("[%d] search for key %lld failed unexpectedly\n",
158 if (kv.value != (u64) (i + 1))
160 ("[%d] search for key %lld returned %lld, not %lld\n", i,
161 tm->keys, kv.value, (u64) (i + 1));
165 delta = clib_time_now (&tm->clib_time) - before;
166 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
169 fformat (stdout, "%.f searches per second\n",
170 ((f64) total_searches) / delta);
172 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
174 fformat (stdout, "Standard E-hash search for items %d times...\n",
177 before = clib_time_now (&tm->clib_time);
179 for (j = 0; j < tm->search_iter; j++)
181 for (i = 0; i < tm->nitems; i++)
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]);
189 delta = clib_time_now (&tm->clib_time) - before;
190 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
192 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
195 fformat (stdout, "%.f searches per second\n",
196 ((f64) total_searches) / delta);
198 fformat (stdout, "Delete items...\n");
200 for (i = 0; i < tm->nitems; i++)
205 kv.key = tm->keys[i];
206 kv.value = (u64) (i + 1);
207 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
210 clib_warning ("delete key %lld not ok but should be", tm->keys[i]);
212 if (tm->careful_delete_tests)
214 for (j = 0; j < tm->nitems; j++)
216 kv.key = tm->keys[j];
217 rv = BV (clib_bihash_search) (h, &kv, &kv);
218 if (j <= i && rv >= 0)
221 ("i %d j %d search ok but should not be, value %lld",
226 clib_warning ("i %d j %d search not ok but should be",
233 fformat (stdout, "After deletions, should be empty...\n");
235 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
240 test_bihash_cache (test_main_t * tm)
243 BVT (clib_bihash_bucket) _b, *b = &_b;
245 BV (clib_bihash_reset_cache) (b);
247 fformat (stdout, "Initial LRU config: %U\n", BV (format_bihash_lru), b);
249 BV (clib_bihash_update_lru_not_inline) (b, 3);
251 fformat (stdout, "use slot 3, LRU config: %U\n", BV (format_bihash_lru), b);
253 BV (clib_bihash_update_lru) (b, 1);
255 fformat (stdout, "use slot 1 LRU config: %U\n", BV (format_bihash_lru), b);
257 lru = BV (clib_bihash_get_lru) (b);
259 fformat (stdout, "least-recently-used is %d\n", lru);
261 BV (clib_bihash_update_lru) (b, 4);
263 fformat (stdout, "use slot 4 LRU config: %U\n", BV (format_bihash_lru), b);
265 lru = BV (clib_bihash_get_lru) (b);
267 fformat (stdout, "least-recently-used is %d\n", lru);
273 test_bihash_main (test_main_t * tm)
275 unformat_input_t *i = tm->input;
279 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
281 if (unformat (i, "seed %u", &tm->seed))
284 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
286 else if (unformat (i, "non-random-keys"))
287 tm->non_random_keys = 1;
288 else if (unformat (i, "nitems %d", &tm->nitems))
290 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
292 else if (unformat (i, "verbose %d", &tm->verbose))
294 else if (unformat (i, "search %d", &tm->search_iter))
296 else if (unformat (i, "vec64"))
298 else if (unformat (i, "cache"))
301 else if (unformat (i, "verbose"))
304 return clib_error_return (0, "unknown input '%U'",
305 format_unformat_error, i);
311 error = test_bihash (tm);
315 error = test_bihash_vec64 (tm);
319 error = test_bihash_cache (tm);
323 return clib_error_return (0, "no such test?");
331 main (int argc, char *argv[])
335 test_main_t *tm = &test_main;
337 clib_mem_init (0, 3ULL << 30);
340 tm->seed = 0xdeaddabe;
346 tm->careful_delete_tests = 0;
347 tm->key_hash = hash_create (0, sizeof (uword));
348 clib_time_init (&tm->clib_time);
350 unformat_init_command_line (&i, argv);
351 error = test_bihash_main (tm);
356 clib_error_report (error);
361 #endif /* CLIB_UNIX */
364 * fd.io coding-style-patch-verification: ON
367 * eval: (c-set-style "gnu")