1 /* $NetBSD: nbperf.c,v 1.10 2026/10/09 11:54:25 kre Exp $ */ 2 /*- 3 * Copyright (c) 2009 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Joerg Sonnenberger. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if HAVE_NBTOOL_CONFIG_H 35 #include "nbtool_config.h" 36 #endif 37 38 #include <sys/cdefs.h> 39 __RCSID("$NetBSD: nbperf.c,v 1.10 2026/10/09 11:54:25 kre Exp $"); 40 41 #include <sys/endian.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <inttypes.h> 45 #include <stdlib.h> 46 #include <stdio.h> 47 #include <string.h> 48 #include <time.h> 49 #include <unistd.h> 50 51 #include "nbperf.h" 52 53 static int predictable; 54 55 static __dead 56 void usage(void) 57 { 58 fprintf(stderr, 59 "%s [-ps] [-c utilisation] [-i iterations] [-n name] " 60 "[-o output] input\n", 61 getprogname()); 62 exit(1); 63 } 64 65 #if HAVE_NBTOOL_CONFIG_H 66 #define arc4random() rand() 67 #endif 68 69 static void 70 mi_vector_hash_seed_hash(struct nbperf *nbperf) 71 { 72 static uint32_t predictable_counter; 73 if (predictable) 74 nbperf->seed[0] = predictable_counter++; 75 else 76 nbperf->seed[0] = arc4random(); 77 } 78 79 static void 80 mi_vector_hash_compute(struct nbperf *nbperf, const void *key, size_t keylen, 81 uint32_t *hashes) 82 { 83 mi_vector_hash(key, keylen, nbperf->seed[0], hashes); 84 } 85 86 static void 87 mi_vector_hash_print_hash(struct nbperf *nbperf, const char *indent, 88 const char *key, const char *keylen, const char *hash) 89 { 90 fprintf(nbperf->output, 91 "%smi_vector_hash(%s, %s, 0x%08" PRIx32 "U, %s);\n", 92 indent, key, keylen, nbperf->seed[0], hash); 93 } 94 95 static void 96 set_hash(struct nbperf *nbperf, const char *arg) 97 { 98 if (strcmp(arg, "mi_vector_hash") == 0) { 99 nbperf->hash_size = 3; 100 nbperf->seed_hash = mi_vector_hash_seed_hash; 101 nbperf->compute_hash = mi_vector_hash_compute; 102 nbperf->print_hash = mi_vector_hash_print_hash; 103 return; 104 } 105 if (nbperf->hash_size > NBPERF_MAX_HASH_SIZE) 106 errx(1, "Hash function creates too many output values"); 107 errx(1, "Unknown hash function: %s", arg); 108 } 109 110 int 111 main(int argc, char **argv) 112 { 113 struct nbperf nbperf = { 114 .c = 0, 115 .hash_name = "hash", 116 .map_output = NULL, 117 .output = NULL, 118 .static_hash = 0, 119 .check_duplicates = 0, 120 .has_duplicates = 0, 121 .allow_hash_fudging = 0, 122 }; 123 FILE *input; 124 const char *outfile = NULL; 125 const char *mapfile = NULL; 126 size_t curlen = 0, curalloc = 0; 127 char *line, *eos; 128 ssize_t line_len; 129 size_t line_allocated; 130 const void **keys = NULL; 131 size_t *keylens = NULL; 132 uint32_t max_iterations = ~0U; 133 long long tmp; 134 int looped, ch; 135 int (*build_hash)(struct nbperf *) = chm_compute; 136 137 set_hash(&nbperf, "mi_vector_hash"); 138 139 while ((ch = getopt(argc, argv, "a:c:fh:i:m:n:o:ps")) != -1) { 140 switch (ch) { 141 case 'a': 142 /* Accept bdz as alias for netbsd-6 compat. */ 143 if (strcmp(optarg, "chm") == 0) 144 build_hash = chm_compute; 145 else if (strcmp(optarg, "chm3") == 0) 146 build_hash = chm3_compute; 147 else if (strcmp(optarg, "bpz") == 0 || 148 strcmp(optarg, "bdz") == 0) 149 build_hash = bpz_compute; 150 else 151 errx(1, "Unsupported algorithm: %s", optarg); 152 break; 153 case 'c': 154 errno = 0; 155 nbperf.c = strtod(optarg, &eos); 156 if (errno || eos[0] || !nbperf.c) 157 errx(2, "Invalid argument for -c"); 158 break; 159 case 'f': 160 nbperf.allow_hash_fudging = 1; 161 break; 162 case 'h': 163 set_hash(&nbperf, optarg); 164 break; 165 case 'i': 166 errno = 0; 167 tmp = strtoll(optarg, &eos, 0); 168 if (errno || eos == optarg || eos[0] || 169 tmp < 0 || tmp > 0xffffffffU) 170 errx(2, "Iteration count must be " 171 "a 32bit integer"); 172 max_iterations = (uint32_t)tmp; 173 break; 174 case 'm': 175 mapfile = optarg; 176 break; 177 case 'n': 178 nbperf.hash_name = optarg; 179 break; 180 case 'o': 181 outfile = optarg; 182 break; 183 case 'p': 184 predictable = 1; 185 break; 186 case 's': 187 nbperf.static_hash = 1; 188 break; 189 default: 190 usage(); 191 } 192 } 193 194 argc -= optind; 195 argv += optind; 196 197 if (argc > 1) 198 usage(); 199 200 if (outfile != NULL && mapfile != NULL && strcmp(outfile, mapfile) == 0) 201 errx(2, "-m and -o must not name the same file"); 202 203 if (argc == 1) { 204 input = fopen(argv[0], "r"); 205 if (input == NULL) 206 err(1, "can't open input file"); 207 } else 208 input = stdin; 209 210 line = NULL; 211 line_allocated = 0; 212 while ((line_len = getline(&line, &line_allocated, input)) != -1) { 213 if (line_len && line[line_len - 1] == '\n') 214 --line_len; 215 if (curlen == curalloc) { 216 if (curalloc < 256) 217 curalloc = 256; 218 else 219 curalloc += curalloc; 220 keys = realloc(keys, curalloc * sizeof(*keys)); 221 if (keys == NULL) 222 err(1, "realloc failed"); 223 keylens = realloc(keylens, 224 curalloc * sizeof(*keylens)); 225 if (keylens == NULL) 226 err(1, "realloc failed"); 227 } 228 if ((keys[curlen] = strndup(line, line_len)) == NULL) 229 err(1, "malloc failed"); 230 keylens[curlen] = line_len; 231 ++curlen; 232 } 233 free(line); 234 235 if (input != stdin) 236 fclose(input); 237 238 if (curlen == 0) 239 errx(1, "No keys!"); 240 241 nbperf.n = curlen; 242 nbperf.keys = keys; 243 nbperf.keylens = keylens; 244 245 if (outfile == NULL) 246 nbperf.output = stdout; 247 else { 248 nbperf.output = fopen(outfile, "w"); 249 if (nbperf.output == NULL) 250 err(2, "cannot open output file: '%s'", outfile); 251 } 252 if (mapfile != NULL) { 253 nbperf.map_output = fopen(mapfile, "w"); 254 if (nbperf.map_output == NULL) 255 err(2, "cannot open map file: '%s'", mapfile); 256 } 257 258 looped = 0; 259 int rv; 260 for (;;) { 261 rv = (*build_hash)(&nbperf); 262 if (!rv) 263 break; 264 if (nbperf.has_duplicates) { 265 fputc('\n', stderr); 266 errx(1, "Duplicate keys detected"); 267 } 268 fputc('.', stderr); 269 if (!looped) 270 nbperf.check_duplicates = 1; 271 looped = 1; 272 if (max_iterations == ~0U) 273 continue; 274 if (--max_iterations == 0) { 275 fputc('\n', stderr); 276 errx(1, "Iteration count reached"); 277 } 278 } 279 if (looped) 280 fputc('\n', stderr); 281 282 return 0; 283 } 284