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 (test_main_t * tm)
57 BVT (clib_bihash) * h;
58 BVT (clib_bihash_kv) kv;
62 BV (clib_bihash_init) (h, "test", tm->nbuckets, 3ULL << 30);
64 fformat (stdout, "Pick %lld unique %s keys...\n",
65 tm->nitems, tm->non_random_keys ? "non-random" : "random");
67 for (i = 0; i < tm->nitems; i++)
71 if (tm->non_random_keys == 0)
75 rndkey = random_u64 (&tm->seed);
77 p = hash_get (tm->key_hash, rndkey);
82 rndkey = (u64) (i + 1) << 16;
84 hash_set (tm->key_hash, rndkey, i + 1);
85 vec_add1 (tm->keys, rndkey);
88 fformat (stdout, "Add items...\n");
89 for (i = 0; i < tm->nitems; i++)
94 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
98 fformat (stdout, "--------------------\n");
99 fformat (stdout, "After adding key %llu value %lld...\n",
100 tm->keys[i], (u64) (i + 1));
101 fformat (stdout, "%U", BV (format_bihash), h,
102 2 /* very verbose */ );
106 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
108 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
110 before = clib_time_now (&tm->clib_time);
112 for (j = 0; j < tm->search_iter; j++)
114 u64 hash1 = clib_xxhash (tm->keys[0]);
116 for (i = 0; i < tm->nitems; i++)
118 if (i < (tm->nitems - 3))
120 clib_bihash_bucket_t *b;
121 BVT (clib_bihash_value) * v;
122 u64 hash2 = clib_xxhash (tm->keys[i + 3]);
123 u32 bucket_index = hash2 & (h->nbuckets - 1);
124 b = &h->buckets[bucket_index];
125 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
127 bucket_index = hash1 & (h->nbuckets - 1);
128 b = &h->buckets[bucket_index];
129 v = BV (clib_bihash_get_value) (h, b->offset);
130 hash1 >>= h->log2_nbuckets;
131 hash1 = hash1 & ((1 << b->log2_pages) - 1);
133 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
138 kv.key = tm->keys[i];
139 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
140 clib_warning ("search for key %lld failed unexpectedly\n",
142 if (kv.value != (u64) (i + 1))
143 clib_warning ("search for key %lld returned %lld, not %lld\n",
144 tm->keys, kv.value, (u64) (i + 1));
148 delta = clib_time_now (&tm->clib_time) - before;
149 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
152 fformat (stdout, "%.f searches per second\n",
153 ((f64) total_searches) / delta);
155 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
157 fformat (stdout, "Standard E-hash search for items %d times...\n",
160 before = clib_time_now (&tm->clib_time);
162 for (j = 0; j < tm->search_iter; j++)
164 for (i = 0; i < tm->nitems; i++)
166 p = hash_get (tm->key_hash, tm->keys[i]);
167 if (p == 0 || p[0] != (uword) (i + 1))
168 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
172 delta = clib_time_now (&tm->clib_time) - before;
173 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
175 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
178 fformat (stdout, "%.f searches per second\n",
179 ((f64) total_searches) / delta);
181 fformat (stdout, "Delete items...\n");
183 for (i = 0; i < tm->nitems; i++)
188 kv.key = tm->keys[i];
189 kv.value = (u64) (i + 1);
190 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
193 clib_warning ("delete key %lld not ok but should be", tm->keys[i]);
195 if (tm->careful_delete_tests)
197 for (j = 0; j < tm->nitems; j++)
199 kv.key = tm->keys[j];
200 rv = BV (clib_bihash_search) (h, &kv, &kv);
201 if (j <= i && rv >= 0)
204 ("i %d j %d search ok but should not be, value %lld",
209 clib_warning ("i %d j %d search not ok but should be",
216 fformat (stdout, "After deletions, should be empty...\n");
218 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
223 test_bihash_main (test_main_t * tm)
225 unformat_input_t *i = tm->input;
228 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
230 if (unformat (i, "seed %u", &tm->seed))
233 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
235 else if (unformat (i, "non-random-keys"))
236 tm->non_random_keys = 1;
237 else if (unformat (i, "nitems %d", &tm->nitems))
239 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
241 else if (unformat (i, "verbose %d", &tm->verbose))
243 else if (unformat (i, "search %d", &tm->search_iter))
245 else if (unformat (i, "verbose"))
248 return clib_error_return (0, "unknown input '%U'",
249 format_unformat_error, i);
252 error = test_bihash (tm);
259 main (int argc, char *argv[])
263 test_main_t *tm = &test_main;
265 clib_mem_init (0, 3ULL << 30);
268 tm->seed = 0xdeaddabe;
274 tm->careful_delete_tests = 0;
275 tm->key_hash = hash_create (0, sizeof (uword));
276 clib_time_init (&tm->clib_time);
278 unformat_init_command_line (&i, argv);
279 error = test_bihash_main (tm);
284 clib_error_report (error);
289 #endif /* CLIB_UNIX */
292 * fd.io coding-style-patch-verification: ON
295 * eval: (c-set-style "gnu")