Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 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 | Copyright (c) 2005 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #include <vppinfra/phash.h> |
| 39 | #include <vppinfra/format.h> |
| 40 | #include <vppinfra/random.h> |
| 41 | |
| 42 | static int verbose; |
| 43 | #define if_verbose(format,args...) \ |
| 44 | if (verbose) { clib_warning(format, ## args); } |
| 45 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 46 | int |
| 47 | test_phash_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | phash_main_t _pm = { 0 }, *pm = &_pm; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | int n_keys, random_keys; |
| 51 | u32 seed; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 52 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
| 54 | random_keys = 1; |
| 55 | n_keys = 1000; |
| 56 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 57 | { |
| 58 | if (0 == unformat (input, "keys %d", &n_keys) |
| 59 | && 0 == unformat (input, "verbose %=", &verbose, 1) |
| 60 | && 0 == unformat (input, "random-keys %=", &random_keys, 1) |
| 61 | && 0 == unformat (input, "sequential-keys %=", &random_keys, 0) |
| 62 | && 0 == unformat (input, "seed %d", &pm->random_seed) |
| 63 | && 0 == unformat (input, "64-bit %|", &pm->flags, PHASH_FLAG_MIX64) |
| 64 | && 0 == unformat (input, "32-bit %|", &pm->flags, PHASH_FLAG_MIX32) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 65 | && 0 == unformat (input, "fast %|", &pm->flags, |
| 66 | PHASH_FLAG_FAST_MODE) |
| 67 | && 0 == unformat (input, "slow %|", &pm->flags, |
| 68 | PHASH_FLAG_SLOW_MODE) |
| 69 | && 0 == unformat (input, "minimal %|", &pm->flags, |
| 70 | PHASH_FLAG_MINIMAL) |
| 71 | && 0 == unformat (input, "non-minimal %|", &pm->flags, |
| 72 | PHASH_FLAG_NON_MINIMAL)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | clib_error ("unknown input `%U'", format_unformat_error, input); |
| 74 | } |
| 75 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 76 | if (!pm->random_seed) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | pm->random_seed = random_default_seed (); |
| 78 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 79 | if_verbose |
| 80 | ("%d %d-bit keys, random seed %d, %s mode, looking for %sminimal hash", |
| 81 | n_keys, (pm->flags & PHASH_FLAG_MIX64) ? 64 : 32, pm->random_seed, |
| 82 | (pm->flags & PHASH_FLAG_FAST_MODE) ? "fast" : "slow", |
| 83 | (pm->flags & PHASH_FLAG_MINIMAL) ? "" : "non-"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
| 85 | seed = pm->random_seed; |
| 86 | |
| 87 | /* Initialize random keys. */ |
| 88 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 89 | phash_key_t *k; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | |
| 91 | vec_resize (pm->keys, n_keys); |
| 92 | vec_foreach (k, pm->keys) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 93 | { |
| 94 | k->key = k - pm->keys; |
| 95 | if (random_keys) |
| 96 | { |
| 97 | if (pm->flags & PHASH_FLAG_MIX64) |
| 98 | k->key = random_u64 (&seed); |
| 99 | else |
| 100 | k->key = random_u32 (&seed); |
| 101 | } |
| 102 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | error = phash_find_perfect_hash (pm); |
| 106 | if (error) |
| 107 | { |
| 108 | clib_error_report (error); |
| 109 | return 1; |
| 110 | } |
| 111 | else |
| 112 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 113 | if_verbose ("(%d,%d) (a,b) bits, %d seeds tried, %d tree walks", |
| 114 | pm->a_bits, pm->b_bits, |
| 115 | pm->n_seed_trials, pm->n_perfect_calls); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
| 117 | error = phash_validate (pm); |
| 118 | if (error) |
| 119 | { |
| 120 | clib_error_report (error); |
| 121 | return 1; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 129 | int |
| 130 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | { |
| 132 | unformat_input_t i; |
| 133 | int res; |
| 134 | |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 135 | clib_mem_init (0, 64ULL << 20); |
| 136 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | verbose = (argc > 1); |
| 138 | unformat_init_command_line (&i, argv); |
| 139 | res = test_phash_main (&i); |
| 140 | unformat_free (&i); |
| 141 | return res; |
| 142 | } |
| 143 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * fd.io coding-style-patch-verification: ON |
| 147 | * |
| 148 | * Local Variables: |
| 149 | * eval: (c-set-style "gnu") |
| 150 | * End: |
| 151 | */ |