1 /* $NetBSD: nbperf.c,v 1.4 2011/10/21 23:47:11 joerg 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.4 2011/10/21 23:47:11 joerg 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 <unistd.h> 49 50 #include "nbperf.h" 51 52 static int predictable; 53 54 static __dead 55 void usage(void) 56 { 57 fprintf(stderr, 58 "%s [-ps] [-c utilisation] [-i iterations] [-n name] " 59 "[-o output] input\n", 60 getprogname()); 61 exit(1); 62 } 63 64 #if HAVE_NBTOOL_CONFIG_H && !defined(__NetBSD__) 65 #define arc4random() rand() 66 #endif 67 68 static void 69 mi_vector_hash_seed_hash(struct nbperf *nbperf) 70 { 71 static uint32_t predictable_counter; 72 if (predictable) 73 nbperf->seed[0] = predictable_counter++; 74 else 75 nbperf->seed[0] = arc4random(); 76 } 77 78 static void 79 mi_vector_hash_compute(struct nbperf *nbperf, const void *key, size_t keylen, 80 uint32_t *hashes) 81 { 82 mi_vector_hash(key, keylen, nbperf->seed[0], hashes); 83 } 84 85 static void 86 mi_vector_hash_print_hash(struct nbperf *nbperf, const char *indent, 87 const char *key, const char *keylen, const char *hash) 88 { 89 fprintf(nbperf->output, 90 "%smi_vector_hash(%s, %s, 0x%08" PRIx32 "U, %s);\n", 91 indent, key, keylen, nbperf->seed[0], hash); 92 } 93 94 static void 95 set_hash(struct nbperf *nbperf, const char *arg) 96 { 97 if (strcmp(arg, "mi_vector_hash") == 0) { 98 nbperf->hash_size = 3; 99 nbperf->seed_hash = mi_vector_hash_seed_hash; 100 nbperf->compute_hash = mi_vector_hash_compute; 101 nbperf->print_hash = mi_vector_hash_print_hash; 102 return; 103 } 104 if (nbperf->hash_size > NBPERF_MAX_HASH_SIZE) 105 errx(1, "Hash function creates too many output values"); 106 errx(1, "Unknown hash function: %s", arg); 107 } 108 109 int 110 main(int argc, char **argv) 111 { 112 struct nbperf nbperf = { 113 .c = 0, 114 .hash_name = "hash", 115 .map_output = NULL, 116 .output = NULL, 117 .static_hash = 0, 118 .first_round = 1, 119 .has_duplicates = 0, 120 }; 121 FILE *input; 122 size_t curlen = 0, curalloc = 0; 123 char *line, *eos; 124 ssize_t line_len; 125 size_t line_allocated; 126 const void **keys = NULL; 127 size_t *keylens = NULL; 128 uint32_t max_iterations = 0xffffffU; 129 long long tmp; 130 int looped, ch; 131 int (*build_hash)(struct nbperf *) = chm_compute; 132 133 set_hash(&nbperf, "mi_vector_hash"); 134 135 while ((ch = getopt(argc, argv, "a:c:h:i:m:n:o:ps")) != -1) { 136 switch (ch) { 137 case 'a': 138 if (strcmp(optarg, "chm") == 0) 139 build_hash = chm_compute; 140 else if (strcmp(optarg, "chm3") == 0) 141 build_hash = chm3_compute; 142 else if (strcmp(optarg, "bdz") == 0) 143 build_hash = bdz_compute; 144 else 145 errx(1, "Unsupport algorithm: %s", optarg); 146 break; 147 case 'c': 148 errno = 0; 149 nbperf.c = strtod(optarg, &eos); 150 if (errno || eos[0] || !nbperf.c) 151 errx(2, "Invalid argument for -c"); 152 break; 153 case 'h': 154 set_hash(&nbperf, optarg); 155 break; 156 case 'i': 157 errno = 0; 158 tmp = strtoll(optarg, &eos, 0); 159 if (errno || eos == optarg || eos[0] || 160 tmp < 0 || tmp > 0xffffffffU) 161 errx(2, "Iteration count must be " 162 "a 32bit integer"); 163 max_iterations = (uint32_t)tmp; 164 break; 165 case 'm': 166 if (nbperf.map_output) 167 fclose(nbperf.map_output); 168 nbperf.map_output = fopen(optarg, "w"); 169 if (nbperf.map_output == NULL) 170 err(2, "cannot open map file"); 171 break; 172 case 'n': 173 nbperf.hash_name = optarg; 174 break; 175 case 'o': 176 if (nbperf.output) 177 fclose(nbperf.output); 178 nbperf.output = fopen(optarg, "w"); 179 if (nbperf.output == NULL) 180 err(2, "cannot open output file"); 181 break; 182 case 'p': 183 predictable = 1; 184 break; 185 case 's': 186 nbperf.static_hash = 1; 187 break; 188 default: 189 usage(); 190 } 191 } 192 193 argc -= optind; 194 argv += optind; 195 196 if (argc > 1) 197 usage(); 198 199 if (argc == 1) { 200 input = fopen(argv[0], "r"); 201 if (input == NULL) 202 err(1, "can't open input file"); 203 } else 204 input = stdin; 205 206 if (nbperf.output == NULL) 207 nbperf.output = stdout; 208 209 line = NULL; 210 line_allocated = 0; 211 while ((line_len = getline(&line, &line_allocated, input)) != -1) { 212 if (line_len && line[line_len - 1] == '\n') 213 --line_len; 214 if (curlen == curalloc) { 215 if (curalloc < 256) 216 curalloc = 256; 217 else 218 curalloc += curalloc; 219 keys = realloc(keys, curalloc * sizeof(*keys)); 220 if (keys == NULL) 221 err(1, "realloc failed"); 222 keylens = realloc(keylens, 223 curalloc * sizeof(*keylens)); 224 if (keylens == NULL) 225 err(1, "realloc failed"); 226 } 227 if ((keys[curlen] = strndup(line, line_len)) == NULL) 228 err(1, "malloc failed"); 229 keylens[curlen] = line_len; 230 ++curlen; 231 } 232 free(line); 233 234 if (input != stdin) 235 fclose(input); 236 237 nbperf.n = curlen; 238 nbperf.keys = keys; 239 nbperf.keylens = keylens; 240 241 looped = 0; 242 while ((*build_hash)(&nbperf)) { 243 if (nbperf.has_duplicates) 244 errx(1, "Duplicate keys detected"); 245 fputc('.', stderr); 246 looped = 1; 247 if (max_iterations == 0xffffffffU) 248 continue; 249 if (--max_iterations == 0) { 250 fputc('\n', stderr); 251 errx(1, "Iteration count reached"); 252 } 253 } 254 if (looped) 255 fputc('\n', stderr); 256 257 return 0; 258 } 259