X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvppinfra%2Ftest_bihash_template.c;h=bdcf2cd6a8146003e6ba66b6181c8393ad40ec1b;hb=582caa3bb67a1380c12790ba6f98450b92835761;hp=ef03f565e1d20e3581f17ec0d7e2f4effdb574cc;hpb=5e6b9580f202188cbe158368bdbe35c3f39973d7;p=vpp.git diff --git a/src/vppinfra/test_bihash_template.c b/src/vppinfra/test_bihash_template.c index ef03f565e1d..bdcf2cd6a81 100644 --- a/src/vppinfra/test_bihash_template.c +++ b/src/vppinfra/test_bihash_template.c @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include @@ -26,12 +28,15 @@ typedef struct u64 seed; u32 nbuckets; u32 nitems; + u32 ncycles; + u32 report_every_n; u32 search_iter; int careful_delete_tests; int verbose; int non_random_keys; uword *key_hash; u64 *keys; + uword hash_memory_size; BVT (clib_bihash) hash; clib_time_t clib_time; @@ -47,6 +52,43 @@ vl (void *v) return vec_len (v); } +static clib_error_t * +test_bihash_vec64 (test_main_t * tm) +{ + u32 user_buckets = 1228800; + u32 user_memory_size = 209715200; + BVT (clib_bihash_kv) kv; + int i, j; + f64 before; + f64 *cum_times = 0; + BVT (clib_bihash) * h; + + h = &tm->hash; + + BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size); + + before = clib_time_now (&tm->clib_time); + + for (j = 0; j < 10; j++) + { + for (i = 1; i <= j * 1000 + 1; i++) + { + kv.key = i; + kv.value = 1; + + BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ ); + } + + vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before); + } + + for (j = 0; j < vec_len (cum_times); j++) + fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000, + cum_times[j] * 1e6); + + return 0; +} + static clib_error_t * test_bihash (test_main_t * tm) { @@ -56,154 +98,232 @@ test_bihash (test_main_t * tm) f64 before, delta; BVT (clib_bihash) * h; BVT (clib_bihash_kv) kv; + u32 acycle; h = &tm->hash; - BV (clib_bihash_init) (h, "test", tm->nbuckets, 3ULL << 30); - - fformat (stdout, "Pick %lld unique %s keys...\n", - tm->nitems, tm->non_random_keys ? "non-random" : "random"); + BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size); - for (i = 0; i < tm->nitems; i++) + for (acycle = 0; acycle < tm->ncycles; acycle++) { - u64 rndkey; - - if (tm->non_random_keys == 0) + if ((acycle % tm->report_every_n) == 0) { + fformat (stdout, "Cycle %lld out of %lld...\n", + acycle, tm->ncycles); - again: - rndkey = random_u64 (&tm->seed); - - p = hash_get (tm->key_hash, rndkey); - if (p) - goto again; + fformat (stdout, "Pick %lld unique %s keys...\n", + tm->nitems, tm->non_random_keys ? "non-random" : "random"); } - else - rndkey = (u64) (i + 1) << 16; - hash_set (tm->key_hash, rndkey, i + 1); - vec_add1 (tm->keys, rndkey); - } + for (i = 0; i < tm->nitems; i++) + { + u64 rndkey; - fformat (stdout, "Add items...\n"); - for (i = 0; i < tm->nitems; i++) - { - kv.key = tm->keys[i]; - kv.value = i + 1; + if (tm->non_random_keys == 0) + { - BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ ); + again: + rndkey = random_u64 (&tm->seed); - if (tm->verbose > 1) - { - fformat (stdout, "--------------------\n"); - fformat (stdout, "After adding key %llu value %lld...\n", - tm->keys[i], (u64) (i + 1)); - fformat (stdout, "%U", BV (format_bihash), h, - 2 /* very verbose */ ); - } - } + p = hash_get (tm->key_hash, rndkey); + if (p) + goto again; + } + else + rndkey = (u64) (i + 1) << 16; + rndkey += acycle; - fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ ); + hash_set (tm->key_hash, rndkey, i + 1); + vec_add1 (tm->keys, rndkey); + } - fformat (stdout, "Search for items %d times...\n", tm->search_iter); - before = clib_time_now (&tm->clib_time); + if ((acycle % tm->report_every_n) == 0) + fformat (stdout, "Add items...\n"); - for (j = 0; j < tm->search_iter; j++) - { for (i = 0; i < tm->nitems; i++) { kv.key = tm->keys[i]; - if (BV (clib_bihash_search) (h, &kv, &kv) < 0) - if (BV (clib_bihash_search) (h, &kv, &kv) < 0) - clib_warning ("[%d] search for key %lld failed unexpectedly\n", - i, tm->keys[i]); - if (kv.value != (u64) (i + 1)) - clib_warning - ("[%d] search for key %lld returned %lld, not %lld\n", i, - tm->keys, kv.value, (u64) (i + 1)); - } - } + kv.value = i + 1; - delta = clib_time_now (&tm->clib_time) - before; - total_searches = (uword) tm->search_iter * (uword) tm->nitems; + BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ ); - if (delta > 0) - fformat (stdout, "%.f searches per second\n", - ((f64) total_searches) / delta); + if (tm->verbose > 1) + { + fformat (stdout, "--------------------\n"); + fformat (stdout, "After adding key %llu value %lld...\n", + tm->keys[i], (u64) (i + 1)); + fformat (stdout, "%U", BV (format_bihash), h, + 2 /* very verbose */ ); + } + } - fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta); + if ((acycle % tm->report_every_n) == 0) + { + fformat (stdout, "%U", BV (format_bihash), h, + 0 /* very verbose */ ); - fformat (stdout, "Standard E-hash search for items %d times...\n", - tm->search_iter); + fformat (stdout, "Search for items %d times...\n", tm->search_iter); + } - before = clib_time_now (&tm->clib_time); + before = clib_time_now (&tm->clib_time); - for (j = 0; j < tm->search_iter; j++) - { - for (i = 0; i < tm->nitems; i++) + for (j = 0; j < tm->search_iter; j++) { - p = hash_get (tm->key_hash, tm->keys[i]); - if (p == 0 || p[0] != (uword) (i + 1)) - clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]); + for (i = 0; i < tm->nitems; i++) + { + kv.key = tm->keys[i]; + if (BV (clib_bihash_search) (h, &kv, &kv) < 0) + if (BV (clib_bihash_search) (h, &kv, &kv) < 0) + clib_warning + ("[%d] search for key %lld failed unexpectedly\n", i, + tm->keys[i]); + if (kv.value != (u64) (i + 1)) + clib_warning + ("[%d] search for key %lld returned %lld, not %lld\n", i, + tm->keys, kv.value, (u64) (i + 1)); + } } - } - delta = clib_time_now (&tm->clib_time) - before; - total_searches = (uword) tm->search_iter * (uword) tm->nitems; + if ((acycle % tm->report_every_n) == 0) + { + delta = clib_time_now (&tm->clib_time) - before; + total_searches = (uword) tm->search_iter * (uword) tm->nitems; - fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta); + if (delta > 0) + fformat (stdout, "%.f searches per second\n", + ((f64) total_searches) / delta); - if (delta > 0) - fformat (stdout, "%.f searches per second\n", - ((f64) total_searches) / delta); + fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, + delta); - fformat (stdout, "Delete items...\n"); + fformat (stdout, "Standard E-hash search for items %d times...\n", + tm->search_iter); + } - for (i = 0; i < tm->nitems; i++) - { - int j; - int rv; + before = clib_time_now (&tm->clib_time); + + for (j = 0; j < tm->search_iter; j++) + { + for (i = 0; i < tm->nitems; i++) + { + p = hash_get (tm->key_hash, tm->keys[i]); + if (p == 0 || p[0] != (uword) (i + 1)) + clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]); + } + } - kv.key = tm->keys[i]; - kv.value = (u64) (i + 1); - rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ ); + delta = clib_time_now (&tm->clib_time) - before; + total_searches = (uword) tm->search_iter * (uword) tm->nitems; - if (rv < 0) - clib_warning ("delete key %lld not ok but should be", tm->keys[i]); + if ((acycle % tm->report_every_n) == 0) + { + fformat (stdout, "%lld searches in %.6f seconds\n", + total_searches, delta); - if (tm->careful_delete_tests) + if (delta > 0) + fformat (stdout, "%.f searches per second\n", + ((f64) total_searches) / delta); + fformat (stdout, "Delete items...\n"); + } + + for (i = 0; i < tm->nitems; i++) { - for (j = 0; j < tm->nitems; j++) + int j; + int rv; + + kv.key = tm->keys[i]; + kv.value = (u64) (i + 1); + rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ ); + + if (rv < 0) + clib_warning ("delete key %lld not ok but should be", + tm->keys[i]); + + if (tm->careful_delete_tests) { - kv.key = tm->keys[j]; - rv = BV (clib_bihash_search) (h, &kv, &kv); - if (j <= i && rv >= 0) + for (j = 0; j < tm->nitems; j++) { - clib_warning - ("i %d j %d search ok but should not be, value %lld", - i, j, kv.value); - } - if (j > i && rv < 0) - { - clib_warning ("i %d j %d search not ok but should be", - i, j); + kv.key = tm->keys[j]; + rv = BV (clib_bihash_search) (h, &kv, &kv); + if (j <= i && rv >= 0) + { + clib_warning + ("i %d j %d search ok but should not be, value %lld", + i, j, kv.value); + } + if (j > i && rv < 0) + { + clib_warning ("i %d j %d search not ok but should be", + i, j); + } } } } + if ((acycle % tm->report_every_n) == 0) + { + struct rusage r_usage; + getrusage (RUSAGE_SELF, &r_usage); + fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss); + fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ ); + } + + /* Clean up side-bet hash table and random key vector */ + hash_free (tm->key_hash); + vec_reset_length (tm->keys); + /* Recreate hash table if we're going to need it again */ + if (acycle != (tm->ncycles - 1)) + tm->key_hash = hash_create (tm->nitems, sizeof (uword)); } - fformat (stdout, "After deletions, should be empty...\n"); + fformat (stdout, "End of run, should be empty...\n"); fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ ); return 0; } +clib_error_t * +test_bihash_cache (test_main_t * tm) +{ + u32 lru; + BVT (clib_bihash_bucket) _b, *b = &_b; + + BV (clib_bihash_reset_cache) (b); + + fformat (stdout, "Initial LRU config: %U\n", BV (format_bihash_lru), b); + + BV (clib_bihash_update_lru_not_inline) (b, 3); + + fformat (stdout, "use slot 3, LRU config: %U\n", BV (format_bihash_lru), b); + + BV (clib_bihash_update_lru) (b, 1); + + fformat (stdout, "use slot 1 LRU config: %U\n", BV (format_bihash_lru), b); + + lru = BV (clib_bihash_get_lru) (b); + + fformat (stdout, "least-recently-used is %d\n", lru); + + BV (clib_bihash_update_lru) (b, 4); + + fformat (stdout, "use slot 4 LRU config: %U\n", BV (format_bihash_lru), b); + + lru = BV (clib_bihash_get_lru) (b); + + fformat (stdout, "least-recently-used is %d\n", lru); + + return 0; +} + clib_error_t * test_bihash_main (test_main_t * tm) { unformat_input_t *i = tm->input; clib_error_t *error; + int which = 0; + + tm->report_every_n = 1; + tm->hash_memory_size = 4095ULL << 20; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { @@ -216,12 +336,24 @@ test_bihash_main (test_main_t * tm) tm->non_random_keys = 1; else if (unformat (i, "nitems %d", &tm->nitems)) ; + else if (unformat (i, "ncycles %d", &tm->ncycles)) + ; else if (unformat (i, "careful %d", &tm->careful_delete_tests)) ; else if (unformat (i, "verbose %d", &tm->verbose)) ; else if (unformat (i, "search %d", &tm->search_iter)) ; + else if (unformat (i, "report-every %d", &tm->report_every_n)) + ; + else if (unformat (i, "memory-size %U", + unformat_memory_size, &tm->hash_memory_size)) + ; + else if (unformat (i, "vec64")) + which = 1; + else if (unformat (i, "cache")) + which = 2; + else if (unformat (i, "verbose")) tm->verbose = 1; else @@ -229,7 +361,29 @@ test_bihash_main (test_main_t * tm) format_unformat_error, i); } - error = test_bihash (tm); + /* Preallocate hash table, key vector */ + tm->key_hash = hash_create (tm->nitems, sizeof (uword)); + vec_validate (tm->keys, tm->nitems - 1); + _vec_len (tm->keys) = 0; + + + switch (which) + { + case 0: + error = test_bihash (tm); + break; + + case 1: + error = test_bihash_vec64 (tm); + break; + + case 2: + error = test_bihash_cache (tm); + break; + + default: + return clib_error_return (0, "no such test?"); + } return error; } @@ -242,17 +396,17 @@ main (int argc, char *argv[]) clib_error_t *error; test_main_t *tm = &test_main; - clib_mem_init (0, 3ULL << 30); + clib_mem_init (0, 4095ULL << 20); tm->input = &i; tm->seed = 0xdeaddabe; tm->nbuckets = 2; tm->nitems = 5; + tm->ncycles = 1; tm->verbose = 1; tm->search_iter = 1; tm->careful_delete_tests = 0; - tm->key_hash = hash_create (0, sizeof (uword)); clib_time_init (&tm->clib_time); unformat_init_command_line (&i, argv);