xref: /netbsd-src/usr.bin/nbperf/nbperf.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: nbperf.c,v 1.5 2013/01/31 16:32:02 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.5 2013/01/31 16:32:02 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 			/* Accept bdz as alias for netbsd-6 compat. */
139 			if (strcmp(optarg, "chm") == 0)
140 				build_hash = chm_compute;
141 			else if (strcmp(optarg, "chm3") == 0)
142 				build_hash = chm3_compute;
143 			else if (strcmp(optarg, "bpz") == 0 ||
144 			         strcmp(optarg, "bdz") == 0)
145 				build_hash = bpz_compute;
146 			else
147 				errx(1, "Unsupport algorithm: %s", optarg);
148 			break;
149 		case 'c':
150 			errno = 0;
151 			nbperf.c = strtod(optarg, &eos);
152 			if (errno || eos[0] || !nbperf.c)
153 				errx(2, "Invalid argument for -c");
154 			break;
155 		case 'h':
156 			set_hash(&nbperf, optarg);
157 			break;
158 		case 'i':
159 			errno = 0;
160 			tmp = strtoll(optarg, &eos, 0);
161 			if (errno || eos == optarg || eos[0] ||
162 			    tmp < 0 || tmp > 0xffffffffU)
163 				errx(2, "Iteration count must be "
164 				    "a 32bit integer");
165 			max_iterations = (uint32_t)tmp;
166 			break;
167 		case 'm':
168 			if (nbperf.map_output)
169 				fclose(nbperf.map_output);
170 			nbperf.map_output = fopen(optarg, "w");
171 			if (nbperf.map_output == NULL)
172 				err(2, "cannot open map file");
173 			break;
174 		case 'n':
175 			nbperf.hash_name = optarg;
176 			break;
177 		case 'o':
178 			if (nbperf.output)
179 				fclose(nbperf.output);
180 			nbperf.output = fopen(optarg, "w");
181 			if (nbperf.output == NULL)
182 				err(2, "cannot open output file");
183 			break;
184 		case 'p':
185 			predictable = 1;
186 			break;
187 		case 's':
188 			nbperf.static_hash = 1;
189 			break;
190 		default:
191 			usage();
192 		}
193 	}
194 
195 	argc -= optind;
196 	argv += optind;
197 
198 	if (argc > 1)
199 		usage();
200 
201 	if (argc == 1) {
202 		input = fopen(argv[0], "r");
203 		if (input == NULL)
204 			err(1, "can't open input file");
205 	} else
206 		input = stdin;
207 
208 	if (nbperf.output == NULL)
209 		nbperf.output = stdout;
210 
211 	line = NULL;
212 	line_allocated = 0;
213 	while ((line_len = getline(&line, &line_allocated, input)) != -1) {
214 		if (line_len && line[line_len - 1] == '\n')
215 			--line_len;
216 		if (curlen == curalloc) {
217 			if (curalloc < 256)
218 				curalloc = 256;
219 			else
220 				curalloc += curalloc;
221 			keys = realloc(keys, curalloc * sizeof(*keys));
222 			if (keys == NULL)
223 				err(1, "realloc failed");
224 			keylens = realloc(keylens,
225 			    curalloc * sizeof(*keylens));
226 			if (keylens == NULL)
227 				err(1, "realloc failed");
228 		}
229 		if ((keys[curlen] = strndup(line, line_len)) == NULL)
230 			err(1, "malloc failed");
231 		keylens[curlen] = line_len;
232 		++curlen;
233 	}
234 	free(line);
235 
236 	if (input != stdin)
237 		fclose(input);
238 
239 	nbperf.n = curlen;
240 	nbperf.keys = keys;
241 	nbperf.keylens = keylens;
242 
243 	looped = 0;
244 	while ((*build_hash)(&nbperf)) {
245 		if (nbperf.has_duplicates)
246 			errx(1, "Duplicate keys detected");
247 		fputc('.', stderr);
248 		looped = 1;
249 		if (max_iterations == 0xffffffffU)
250 			continue;
251 		if (--max_iterations == 0) {
252 			fputc('\n', stderr);
253 			errx(1, "Iteration count reached");
254 		}
255 	}
256 	if (looped)
257 		fputc('\n', stderr);
258 
259 	return 0;
260 }
261