1 /* $NetBSD: nbperf.c,v 1.1 2009/08/15 16:21:05 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 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: nbperf.c,v 1.1 2009/08/15 16:21:05 joerg Exp $");
37 #include <sys/endian.h>
52 "%s [-s] [-c utilisation] [-i iterations] [-n name] "
53 "[-o output] input\n",
59 mi_vector_hash_seed_hash(struct nbperf
*nbperf
)
61 nbperf
->seed
[0] = arc4random();
65 mi_vector_hash_compute(struct nbperf
*nbperf
, const void *key
, size_t keylen
,
68 mi_vector_hash(key
, keylen
, nbperf
->seed
[0], hashes
);
72 mi_vector_hash_print_hash(struct nbperf
*nbperf
, const char *indent
,
73 const char *key
, const char *keylen
, const char *hash
)
75 fprintf(nbperf
->output
,
76 "%smi_vector_hash(%s, %s, 0x%08" PRIx32
"U, %s);\n",
77 indent
, key
, keylen
, nbperf
->seed
[0], hash
);
81 set_hash(struct nbperf
*nbperf
, const char *arg
)
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
;
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
);
96 main(int argc
, char **argv
)
98 struct nbperf nbperf
= {
106 size_t curlen
= 0, curalloc
= 0;
109 const void **keys
= NULL
;
110 size_t *keylens
= NULL
;
111 uint32_t max_iterations
= 0xffffffU
;
114 int (*build_hash
)(struct nbperf
*) = chm_compute
;
116 set_hash(&nbperf
, "mi_vector_hash");
118 while ((ch
= getopt(argc
, argv
, "a:c:h:i:m:n:o:s")) != -1) {
121 if (strcmp(optarg
, "chm") == 0)
122 build_hash
= chm_compute
;
123 else if (strcmp(optarg
, "chm3") == 0)
124 build_hash
= chm3_compute
;
125 else if (strcmp(optarg
, "bdz") == 0)
126 build_hash
= bdz_compute
;
128 errx(1, "Unsupport algorithm: %s", optarg
);
132 nbperf
.c
= strtod(optarg
, &eos
);
133 if (errno
|| eos
[0] || !nbperf
.c
)
134 errx(2, "Invalid argument for -c");
137 set_hash(&nbperf
, optarg
);
141 tmp
= strtoll(optarg
, &eos
, 0);
142 if (errno
|| eos
== optarg
|| eos
[0] ||
143 tmp
< 0 || tmp
> 0xffffffffU
)
144 errx(2, "Iteration count must be "
146 max_iterations
= (uint32_t)tmp
;
149 if (nbperf
.map_output
)
150 fclose(nbperf
.map_output
);
151 nbperf
.map_output
= fopen(optarg
, "w");
152 if (nbperf
.map_output
== NULL
)
153 err(2, "cannot open map file");
156 nbperf
.hash_name
= optarg
;
160 fclose(nbperf
.output
);
161 nbperf
.output
= fopen(optarg
, "w");
162 if (nbperf
.output
== NULL
)
163 err(2, "cannot open output file");
166 nbperf
.static_hash
= 1;
180 input
= fopen(argv
[0], "r");
182 err(1, "can't open input file");
186 if (nbperf
.output
== NULL
)
187 nbperf
.output
= stdout
;
189 while ((line
= fgetln(input
, &line_len
)) != NULL
) {
190 if (line_len
&& line
[line_len
- 1] == '\n')
192 if (curlen
== curalloc
) {
196 curalloc
+= curalloc
;
197 keys
= realloc(keys
, curalloc
* sizeof(*keys
));
199 err(1, "realloc failed");
200 keylens
= realloc(keylens
,
201 curalloc
* sizeof(*keylens
));
203 err(1, "realloc failed");
205 if ((keys
[curlen
] = strndup(line
, line_len
)) == NULL
)
206 err(1, "malloc failed");
207 keylens
[curlen
] = line_len
;
216 nbperf
.keylens
= keylens
;
219 while ((*build_hash
)(&nbperf
)) {
222 if (max_iterations
== 0xffffffffU
)
224 if (--max_iterations
== 0) {
226 errx(1, "Iteration count reached");