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>
29 int careful_delete_tests;
34 BVT(clib_bihash) hash;
35 clib_time_t clib_time;
37 unformat_input_t * input;
41 test_main_t test_main;
48 static clib_error_t * test_bihash (test_main_t * tm)
55 BVT(clib_bihash_kv) kv;
59 BV(clib_bihash_init) (h, "test", tm->nbuckets, 3ULL<<30);
61 fformat (stdout, "Pick %lld unique %s keys...\n",
62 tm->nitems, tm->non_random_keys ? "non-random" : "random");
64 for (i = 0; i < tm->nitems; i++)
68 if (tm->non_random_keys == 0)
72 rndkey = random_u64 (&tm->seed);
74 p = hash_get (tm->key_hash, rndkey);
79 rndkey = (u64)(i+1) << 16;
81 hash_set (tm->key_hash, rndkey, i+1);
82 vec_add1 (tm->keys, rndkey);
85 fformat (stdout, "Add items...\n");
86 for (i = 0; i < tm->nitems; i++)
91 BV(clib_bihash_add_del) (h, &kv, 1 /* is_add */);
95 fformat (stdout, "--------------------\n");
96 fformat (stdout, "After adding key %llu value %lld...\n",
97 tm->keys[i], (u64)(i+1));
98 fformat (stdout, "%U", BV(format_bihash), h, 2 /* very verbose */);
102 fformat (stdout, "%U", BV(format_bihash), h, 0 /* very verbose */);
104 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
106 before = clib_time_now (&tm->clib_time);
108 for (j = 0; j < tm->search_iter; j++)
110 u64 hash1 = clib_xxhash (tm->keys[0]);
112 for (i = 0; i < tm->nitems; i++)
114 if (i < (tm->nitems - 3))
116 clib_bihash_bucket_t * b;
117 BVT(clib_bihash_value) * v;
118 u64 hash2 = clib_xxhash (tm->keys[i+3]);
119 u32 bucket_index = hash2 & (h->nbuckets-1);
120 b = &h->buckets[bucket_index];
121 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
123 bucket_index = hash1 & (h->nbuckets-1);
124 b = &h->buckets[bucket_index];
125 v = BV(clib_bihash_get_value) (h, b->offset);
126 hash1 >>= h->log2_nbuckets;
127 hash1 = hash1 & ((1<<b->log2_pages)-1);
129 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
134 kv.key = tm->keys[i];
135 if (BV(clib_bihash_search) (h, &kv, &kv) < 0)
136 clib_warning ("search for key %lld failed unexpectedly\n",
138 if (kv.value != (u64)(i+1))
139 clib_warning ("search for key %lld returned %lld, not %lld\n",
140 tm->keys, kv.value, (u64)(i+1));
144 delta = clib_time_now (&tm->clib_time) - before;
145 total_searches = (uword)tm->search_iter * (uword) tm->nitems;
148 fformat (stdout, "%.f searches per second\n",
149 ((f64)total_searches) / delta);
151 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
153 fformat (stdout, "Standard E-hash search for items %d times...\n",
156 before = clib_time_now (&tm->clib_time);
158 for (j = 0; j < tm->search_iter; j++)
160 for (i = 0; i < tm->nitems; i++)
162 p = hash_get (tm->key_hash, tm->keys[i]);
163 if (p == 0 || p[0] != (uword)(i+1))
164 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
168 delta = clib_time_now (&tm->clib_time) - before;
169 total_searches = (uword)tm->search_iter * (uword) tm->nitems;
171 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
174 fformat (stdout, "%.f searches per second\n",
175 ((f64)total_searches) / delta);
177 fformat (stdout, "Delete items...\n");
179 for (i = 0; i < tm->nitems; i++)
184 kv.key = tm->keys[i];
185 kv.value = (u64)(i+1);
186 rv = BV(clib_bihash_add_del) (h, &kv, 0 /* is_add */);
189 clib_warning ("delete key %lld not ok but should be",
192 if (tm->careful_delete_tests)
194 for (j = 0; j < tm->nitems; j++)
196 kv.key = tm->keys[j];
197 rv = BV(clib_bihash_search) (h, &kv, &kv);
198 if (j <= i && rv >= 0)
201 ( "i %d j %d search ok but should not be, value %lld",
206 clib_warning ("i %d j %d search not ok but should be",
213 fformat (stdout, "After deletions, should be empty...\n");
215 fformat (stdout, "%U", BV(format_bihash), h, 0 /* very verbose */);
220 test_bihash_main (test_main_t * tm)
222 unformat_input_t * i = tm->input;
223 clib_error_t * error;
225 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
227 if (unformat (i, "seed %u", &tm->seed))
230 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
232 else if (unformat (i, "non-random-keys"))
233 tm->non_random_keys = 1;
234 else if (unformat (i, "nitems %d", &tm->nitems))
236 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
238 else if (unformat (i, "verbose %d", &tm->verbose))
240 else if (unformat (i, "search %d", &tm->search_iter))
242 else if (unformat (i, "verbose"))
245 return clib_error_return (0, "unknown input '%U'",
246 format_unformat_error, i);
249 error = test_bihash (tm);
255 int main (int argc, char * argv[])
258 clib_error_t * error;
259 test_main_t * tm = &test_main;
261 clib_mem_init (0, 3ULL<<30);
264 tm->seed = 0xdeaddabe;
270 tm->careful_delete_tests = 0;
271 tm->key_hash = hash_create (0, sizeof (uword));
272 clib_time_init (&tm->clib_time);
274 unformat_init_command_line (&i, argv);
275 error = test_bihash_main (tm);
280 clib_error_report (error);
285 #endif /* CLIB_UNIX */