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