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