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