Klement Sekera | 470a011 | 2017-03-08 05:21:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
| 16 | #include <vppinfra/time.h> |
| 17 | #include <vppinfra/cache.h> |
| 18 | #include <vppinfra/error.h> |
| 19 | #include <vppinfra/heap.h> |
| 20 | #include <vppinfra/format.h> |
| 21 | #include <vppinfra/pool.h> |
| 22 | #include <vppinfra/error.h> |
| 23 | #include <vppinfra/hash.h> |
| 24 | #include <vppinfra/cache.h> |
| 25 | |
| 26 | #include <vppinfra/time.h> |
| 27 | #include <vppinfra/cache.h> |
| 28 | #include <vppinfra/error.h> |
| 29 | |
| 30 | #include <vppinfra/cuckoo_8_8.h> |
| 31 | #include <vppinfra/cuckoo_template.h> |
| 32 | #include <vppinfra/cuckoo_template.c> |
| 33 | |
| 34 | typedef struct |
| 35 | { |
| 36 | u64 seed; |
| 37 | u32 nbuckets; |
| 38 | u32 nitems; |
| 39 | u32 search_iter; |
| 40 | int careful_delete_tests; |
| 41 | int verbose; |
| 42 | int non_random_keys; |
| 43 | uword *key_hash; |
| 44 | u64 *keys; |
| 45 | CVT (clib_cuckoo) hash; |
| 46 | clib_time_t clib_time; |
| 47 | |
| 48 | unformat_input_t *input; |
| 49 | |
| 50 | } test_main_t; |
| 51 | |
| 52 | test_main_t test_main; |
| 53 | |
| 54 | uword |
| 55 | vl (void *v) |
| 56 | { |
| 57 | return vec_len (v); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | do_search (test_main_t * tm, CVT (clib_cuckoo) * h) |
| 62 | { |
| 63 | int i, j; |
| 64 | CVT (clib_cuckoo_kv) kv; |
| 65 | for (j = 0; j < tm->search_iter; j++) |
| 66 | { |
| 67 | for (i = 0; i < tm->nitems; i++) |
| 68 | { |
| 69 | kv.key = tm->keys[i]; |
| 70 | if (CV (clib_cuckoo_search) (h, &kv, &kv) < 0) |
| 71 | if (CV (clib_cuckoo_search) (h, &kv, &kv) < 0) |
| 72 | clib_warning ("[%d] search for key %llu failed unexpectedly\n", |
| 73 | i, tm->keys[i]); |
| 74 | if (kv.value != (u64) (i + 1)) |
| 75 | clib_warning |
| 76 | ("[%d] search for key %llu returned %llu, not %llu\n", i, |
| 77 | tm->keys[i], kv.value, (u64) (i + 1)); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | cb (CVT (clib_cuckoo) * h, void *ctx) |
| 84 | { |
| 85 | fformat (stdout, "Garbage callback called..."); |
| 86 | if (clib_cpu_time_now () % 3) |
| 87 | { |
| 88 | fformat (stdout, "collecting garbage...\n"); |
| 89 | CV (clib_cuckoo_garbage_collect) (h); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | fformat (stdout, "ignoring for now...\n"); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static clib_error_t * |
| 98 | test_cuckoo (test_main_t * tm) |
| 99 | { |
| 100 | int i; |
| 101 | uword *p; |
| 102 | uword total_searches; |
| 103 | f64 before, delta; |
| 104 | CVT (clib_cuckoo) * h; |
| 105 | CVT (clib_cuckoo_kv) kv; |
| 106 | |
| 107 | h = &tm->hash; |
| 108 | |
| 109 | CV (clib_cuckoo_init) (h, "test", tm->nbuckets, cb, NULL); |
| 110 | |
| 111 | fformat (stdout, "Pick %lld unique %s keys...\n", tm->nitems, |
| 112 | tm->non_random_keys ? "non-random" : "random"); |
| 113 | |
| 114 | for (i = 0; i < tm->nitems; i++) |
| 115 | { |
| 116 | u64 rndkey; |
| 117 | |
| 118 | if (tm->non_random_keys == 0) |
| 119 | { |
| 120 | |
| 121 | again: |
| 122 | rndkey = random_u64 (&tm->seed); |
| 123 | |
| 124 | p = hash_get (tm->key_hash, rndkey); |
| 125 | if (p) |
| 126 | goto again; |
| 127 | } |
| 128 | else |
| 129 | rndkey = (u64) (i + 1) << 16; |
| 130 | |
| 131 | hash_set (tm->key_hash, rndkey, i + 1); |
| 132 | vec_add1 (tm->keys, rndkey); |
| 133 | } |
| 134 | |
| 135 | fformat (stdout, "Add items...\n"); |
| 136 | for (i = 0; i < tm->nitems; i++) |
| 137 | { |
| 138 | kv.key = tm->keys[i]; |
| 139 | kv.value = i + 1; |
| 140 | |
| 141 | CV (clib_cuckoo_add_del) (h, &kv, 1 /* is_add */ ); |
| 142 | |
| 143 | if (tm->verbose > 1) |
| 144 | { |
| 145 | fformat (stdout, "--------------------\n"); |
| 146 | fformat (stdout, "After adding key %llu value %lld...\n", |
| 147 | tm->keys[i], (u64) (i + 1)); |
| 148 | fformat (stdout, "%U", CV (format_cuckoo), h, |
| 149 | 2 /* very verbose */ ); |
| 150 | } |
| 151 | |
| 152 | CVT (clib_cuckoo_kv) kv2; |
| 153 | int rv = CV (clib_cuckoo_search) (h, &kv, &kv2); |
| 154 | ASSERT (CLIB_CUCKOO_ERROR_SUCCESS == rv); |
| 155 | } |
| 156 | |
| 157 | fformat (stdout, "%U", CV (format_cuckoo), h, 0 /* very verbose */ ); |
| 158 | |
| 159 | fformat (stdout, "Search for items %d times...\n", tm->search_iter); |
| 160 | |
| 161 | before = clib_time_now (&tm->clib_time); |
| 162 | |
| 163 | do_search (tm, h); |
| 164 | |
| 165 | delta = clib_time_now (&tm->clib_time) - before; |
| 166 | total_searches = (uword) tm->search_iter * (uword) tm->nitems; |
| 167 | |
| 168 | if (delta > 0) |
| 169 | fformat (stdout, "%.f searches per second\n", |
| 170 | ((f64) total_searches) / delta); |
| 171 | |
| 172 | fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta); |
| 173 | |
| 174 | #if 0 |
| 175 | int j; |
| 176 | fformat (stdout, "Standard E-hash search for items %d times...\n", |
| 177 | tm->search_iter); |
| 178 | |
| 179 | before = clib_time_now (&tm->clib_time); |
| 180 | |
| 181 | for (j = 0; j < tm->search_iter; j++) |
| 182 | { |
| 183 | for (i = 0; i < tm->nitems; i++) |
| 184 | { |
| 185 | p = hash_get (tm->key_hash, tm->keys[i]); |
| 186 | if (p == 0 || p[0] != (uword) (i + 1)) |
| 187 | clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | delta = clib_time_now (&tm->clib_time) - before; |
| 192 | total_searches = (uword) tm->search_iter * (uword) tm->nitems; |
| 193 | |
| 194 | fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta); |
| 195 | |
| 196 | if (delta > 0) |
| 197 | fformat (stdout, "%.f searches per second\n", |
| 198 | ((f64) total_searches) / delta); |
| 199 | |
| 200 | #endif |
| 201 | fformat (stdout, "Delete items...\n"); |
| 202 | |
| 203 | for (i = 0; i < tm->nitems; i++) |
| 204 | { |
| 205 | int j; |
| 206 | int rv; |
| 207 | |
| 208 | kv.key = tm->keys[i]; |
| 209 | kv.value = (u64) (i + 1); |
| 210 | rv = CV (clib_cuckoo_add_del) (h, &kv, 0 /* is_add */ ); |
| 211 | |
| 212 | if (rv < 0) |
| 213 | clib_warning ("delete key %lld not ok but should be", tm->keys[i]); |
| 214 | |
| 215 | if (tm->careful_delete_tests) |
| 216 | { |
| 217 | for (j = 0; j < tm->nitems; j++) |
| 218 | { |
| 219 | kv.key = tm->keys[j]; |
| 220 | rv = CV (clib_cuckoo_search) (h, &kv, &kv); |
| 221 | if (j <= i && rv >= 0) |
| 222 | { |
| 223 | clib_warning |
| 224 | ("i %d j %d search ok but should not be, value %lld", i, |
| 225 | j, kv.value); |
| 226 | } |
| 227 | if (j > i && rv < 0) |
| 228 | { |
| 229 | clib_warning ("i %d j %d search not ok but should be", i, |
| 230 | j); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | fformat (stdout, "After deletions, should be empty...\n"); |
| 237 | |
| 238 | fformat (stdout, "%U", CV (format_cuckoo), h, 0 /* very verbose */ ); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | clib_error_t * |
| 243 | test_cuckoo_main (test_main_t * tm) |
| 244 | { |
| 245 | unformat_input_t *i = tm->input; |
| 246 | clib_error_t *error; |
| 247 | |
| 248 | while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) |
| 249 | { |
| 250 | if (unformat (i, "seed %u", &tm->seed)) |
| 251 | ; |
| 252 | else if (unformat (i, "nbuckets %d", &tm->nbuckets)) |
| 253 | ; |
| 254 | else if (unformat (i, "non-random-keys")) |
| 255 | tm->non_random_keys = 1; |
| 256 | else if (unformat (i, "nitems %d", &tm->nitems)) |
| 257 | ; |
| 258 | else if (unformat (i, "careful %d", &tm->careful_delete_tests)) |
| 259 | ; |
| 260 | else if (unformat (i, "verbose %d", &tm->verbose)) |
| 261 | ; |
| 262 | else if (unformat (i, "search %d", &tm->search_iter)) |
| 263 | ; |
| 264 | else if (unformat (i, "verbose")) |
| 265 | tm->verbose = 1; |
| 266 | else |
| 267 | return clib_error_return (0, "unknown input '%U'", |
| 268 | format_unformat_error, i); |
| 269 | } |
| 270 | |
| 271 | error = test_cuckoo (tm); |
| 272 | |
| 273 | return error; |
| 274 | } |
| 275 | |
| 276 | #ifdef CLIB_UNIX |
| 277 | int |
| 278 | main (int argc, char *argv[]) |
| 279 | { |
| 280 | unformat_input_t i; |
| 281 | clib_error_t *error; |
| 282 | test_main_t *tm = &test_main; |
| 283 | |
| 284 | clib_mem_init (0, 3ULL << 30); |
| 285 | |
| 286 | tm->input = &i; |
| 287 | tm->seed = 0xdeaddabe; |
| 288 | |
| 289 | tm->nbuckets = 2; |
| 290 | tm->nitems = 100000; |
| 291 | tm->verbose = 1; |
| 292 | tm->search_iter = 10000; |
| 293 | tm->careful_delete_tests = 0; |
| 294 | tm->key_hash = hash_create (0, sizeof (uword)); |
| 295 | clib_time_init (&tm->clib_time); |
| 296 | |
| 297 | unformat_init_command_line (&i, argv); |
| 298 | error = test_cuckoo_main (tm); |
| 299 | unformat_free (&i); |
| 300 | |
| 301 | if (error) |
| 302 | { |
| 303 | clib_error_report (error); |
| 304 | return 1; |
| 305 | } |
| 306 | return 0; |
| 307 | } |
| 308 | #endif /* CLIB_UNIX */ |
| 309 | |
| 310 | /* |
| 311 | * fd.io coding-style-patch-verification: ON |
| 312 | * |
| 313 | * Local Variables: |
| 314 | * eval: (c-set-style "gnu") |
| 315 | * End: |
| 316 | */ |