1 /* $NetBSD: nbperf.c,v 1.5 2013/01/31 16:32:02 joerg Exp $ */
3 * Copyright (c) 2009 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Joerg Sonnenberger.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: nbperf.c,v 1.5 2013/01/31 16:32:02 joerg Exp $");
41 #include <sys/endian.h>
52 static int predictable
;
58 "%s [-ps] [-c utilisation] [-i iterations] [-n name] "
59 "[-o output] input\n",
64 #if HAVE_NBTOOL_CONFIG_H && !defined(__NetBSD__)
65 #define arc4random() rand()
69 mi_vector_hash_seed_hash(struct nbperf
*nbperf
)
71 static uint32_t predictable_counter
;
73 nbperf
->seed
[0] = predictable_counter
++;
75 nbperf
->seed
[0] = arc4random();
79 mi_vector_hash_compute(struct nbperf
*nbperf
, const void *key
, size_t keylen
,
82 mi_vector_hash(key
, keylen
, nbperf
->seed
[0], hashes
);
86 mi_vector_hash_print_hash(struct nbperf
*nbperf
, const char *indent
,
87 const char *key
, const char *keylen
, const char *hash
)
89 fprintf(nbperf
->output
,
90 "%smi_vector_hash(%s, %s, 0x%08" PRIx32
"U, %s);\n",
91 indent
, key
, keylen
, nbperf
->seed
[0], hash
);
95 set_hash(struct nbperf
*nbperf
, const char *arg
)
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
;
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
);
110 main(int argc
, char **argv
)
112 struct nbperf nbperf
= {
122 size_t curlen
= 0, curalloc
= 0;
125 size_t line_allocated
;
126 const void **keys
= NULL
;
127 size_t *keylens
= NULL
;
128 uint32_t max_iterations
= 0xffffffU
;
131 int (*build_hash
)(struct nbperf
*) = chm_compute
;
133 set_hash(&nbperf
, "mi_vector_hash");
135 while ((ch
= getopt(argc
, argv
, "a:c:h:i:m:n:o:ps")) != -1) {
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
;
147 errx(1, "Unsupport algorithm: %s", optarg
);
151 nbperf
.c
= strtod(optarg
, &eos
);
152 if (errno
|| eos
[0] || !nbperf
.c
)
153 errx(2, "Invalid argument for -c");
156 set_hash(&nbperf
, optarg
);
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 "
165 max_iterations
= (uint32_t)tmp
;
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");
175 nbperf
.hash_name
= optarg
;
179 fclose(nbperf
.output
);
180 nbperf
.output
= fopen(optarg
, "w");
181 if (nbperf
.output
== NULL
)
182 err(2, "cannot open output file");
188 nbperf
.static_hash
= 1;
202 input
= fopen(argv
[0], "r");
204 err(1, "can't open input file");
208 if (nbperf
.output
== NULL
)
209 nbperf
.output
= stdout
;
213 while ((line_len
= getline(&line
, &line_allocated
, input
)) != -1) {
214 if (line_len
&& line
[line_len
- 1] == '\n')
216 if (curlen
== curalloc
) {
220 curalloc
+= curalloc
;
221 keys
= realloc(keys
, curalloc
* sizeof(*keys
));
223 err(1, "realloc failed");
224 keylens
= realloc(keylens
,
225 curalloc
* sizeof(*keylens
));
227 err(1, "realloc failed");
229 if ((keys
[curlen
] = strndup(line
, line_len
)) == NULL
)
230 err(1, "malloc failed");
231 keylens
[curlen
] = line_len
;
241 nbperf
.keylens
= keylens
;
244 while ((*build_hash
)(&nbperf
)) {
245 if (nbperf
.has_duplicates
)
246 errx(1, "Duplicate keys detected");
249 if (max_iterations
== 0xffffffffU
)
251 if (--max_iterations
== 0) {
253 errx(1, "Iteration count reached");