nbperf.c revision 1.1 1 /* $NetBSD: nbperf.c,v 1.1 2009/08/15 16:21:05 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.1 2009/08/15 16:21:05 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 errx(1, "Unknown hash function: %s", arg);
91 }
92
93 int
94 main(int argc, char **argv)
95 {
96 struct nbperf nbperf = {
97 .c = 0,
98 .hash_name = "hash",
99 .map_output = NULL,
100 .output = NULL,
101 .static_hash = 0,
102 };
103 FILE *input;
104 size_t curlen = 0, curalloc = 0;
105 char *line, *eos;
106 size_t line_len;
107 const void **keys = NULL;
108 size_t *keylens = NULL;
109 uint32_t max_iterations = 0xffffffU;
110 long long tmp;
111 int looped, ch;
112 int (*build_hash)(struct nbperf *) = chm_compute;
113
114 set_hash(&nbperf, "mi_vector_hash");
115
116 while ((ch = getopt(argc, argv, "a:c:h:i:m:n:o:s")) != -1) {
117 switch (ch) {
118 case 'a':
119 if (strcmp(optarg, "chm") == 0)
120 build_hash = chm_compute;
121 else if (strcmp(optarg, "chm3") == 0)
122 build_hash = chm3_compute;
123 else if (strcmp(optarg, "bdz") == 0)
124 build_hash = bdz_compute;
125 else
126 errx(1, "Unsupport algorithm: %s", optarg);
127 break;
128 case 'c':
129 errno = 0;
130 nbperf.c = strtod(optarg, &eos);
131 if (errno || eos[0] || !nbperf.c)
132 errx(2, "Invalid argument for -c");
133 break;
134 case 'h':
135 set_hash(&nbperf, optarg);
136 break;
137 case 'i':
138 errno = 0;
139 tmp = strtoll(optarg, &eos, 0);
140 if (errno || eos == optarg || eos[0] ||
141 tmp < 0 || tmp > 0xffffffffU)
142 errx(2, "Iteration count must be "
143 "a 32bit integer");
144 max_iterations = (uint32_t)tmp;
145 break;
146 case 'm':
147 if (nbperf.map_output)
148 fclose(nbperf.map_output);
149 nbperf.map_output = fopen(optarg, "w");
150 if (nbperf.map_output == NULL)
151 err(2, "cannot open map file");
152 break;
153 case 'n':
154 nbperf.hash_name = optarg;
155 break;
156 case 'o':
157 if (nbperf.output)
158 fclose(nbperf.output);
159 nbperf.output = fopen(optarg, "w");
160 if (nbperf.output == NULL)
161 err(2, "cannot open output file");
162 break;
163 case 's':
164 nbperf.static_hash = 1;
165 break;
166 default:
167 usage();
168 }
169 }
170
171 argc -= optind;
172 argv += optind;
173
174 if (argc > 1)
175 usage();
176
177 if (argc == 1) {
178 input = fopen(argv[0], "r");
179 if (input == NULL)
180 err(1, "can't open input file");
181 } else
182 input = stdin;
183
184 if (nbperf.output == NULL)
185 nbperf.output = stdout;
186
187 while ((line = fgetln(input, &line_len)) != NULL) {
188 if (line_len && line[line_len - 1] == '\n')
189 --line_len;
190 if (curlen == curalloc) {
191 if (curalloc < 256)
192 curalloc = 256;
193 else
194 curalloc += curalloc;
195 keys = realloc(keys, curalloc * sizeof(*keys));
196 if (keys == NULL)
197 err(1, "realloc failed");
198 keylens = realloc(keylens,
199 curalloc * sizeof(*keylens));
200 if (keylens == NULL)
201 err(1, "realloc failed");
202 }
203 if ((keys[curlen] = strndup(line, line_len)) == NULL)
204 err(1, "malloc failed");
205 keylens[curlen] = line_len;
206 ++curlen;
207 }
208
209 if (input != stdin)
210 fclose(input);
211
212 nbperf.n = curlen;
213 nbperf.keys = keys;
214 nbperf.keylens = keylens;
215
216 looped = 0;
217 while ((*build_hash)(&nbperf)) {
218 fputc('.', stderr);
219 looped = 1;
220 if (max_iterations == 0xffffffffU)
221 continue;
222 if (--max_iterations == 0) {
223 fputc('\n', stderr);
224 errx(1, "Iteration count reached");
225 }
226 }
227 if (looped)
228 fputc('\n', stderr);
229
230 return 0;
231 }
232