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