Home | History | Annotate | Line # | Download | only in nbperf
nbperf.c revision 1.5
      1  1.5  joerg /*	$NetBSD: nbperf.c,v 1.5 2013/01/31 16:32:02 joerg Exp $	*/
      2  1.1  joerg /*-
      3  1.1  joerg  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      4  1.1  joerg  * All rights reserved.
      5  1.1  joerg  *
      6  1.1  joerg  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1  joerg  * by Joerg Sonnenberger.
      8  1.1  joerg  *
      9  1.1  joerg  * Redistribution and use in source and binary forms, with or without
     10  1.1  joerg  * modification, are permitted provided that the following conditions
     11  1.1  joerg  * are met:
     12  1.1  joerg  *
     13  1.1  joerg  * 1. Redistributions of source code must retain the above copyright
     14  1.1  joerg  *    notice, this list of conditions and the following disclaimer.
     15  1.1  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  joerg  *    notice, this list of conditions and the following disclaimer in
     17  1.1  joerg  *    the documentation and/or other materials provided with the
     18  1.1  joerg  *    distribution.
     19  1.1  joerg  *
     20  1.1  joerg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.1  joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.1  joerg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.1  joerg  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     24  1.1  joerg  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1  joerg  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  1.1  joerg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.1  joerg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28  1.1  joerg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29  1.1  joerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     30  1.1  joerg  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  joerg  * SUCH DAMAGE.
     32  1.1  joerg  */
     33  1.1  joerg 
     34  1.4  joerg #if HAVE_NBTOOL_CONFIG_H
     35  1.4  joerg #include "nbtool_config.h"
     36  1.4  joerg #endif
     37  1.4  joerg 
     38  1.1  joerg #include <sys/cdefs.h>
     39  1.5  joerg __RCSID("$NetBSD: nbperf.c,v 1.5 2013/01/31 16:32:02 joerg Exp $");
     40  1.1  joerg 
     41  1.1  joerg #include <sys/endian.h>
     42  1.1  joerg #include <err.h>
     43  1.1  joerg #include <errno.h>
     44  1.1  joerg #include <inttypes.h>
     45  1.1  joerg #include <stdlib.h>
     46  1.1  joerg #include <stdio.h>
     47  1.1  joerg #include <string.h>
     48  1.1  joerg #include <unistd.h>
     49  1.1  joerg 
     50  1.1  joerg #include "nbperf.h"
     51  1.1  joerg 
     52  1.4  joerg static int predictable;
     53  1.4  joerg 
     54  1.1  joerg static __dead
     55  1.1  joerg void usage(void)
     56  1.1  joerg {
     57  1.1  joerg 	fprintf(stderr,
     58  1.4  joerg 	    "%s [-ps] [-c utilisation] [-i iterations] [-n name] "
     59  1.1  joerg 	    "[-o output] input\n",
     60  1.1  joerg 	    getprogname());
     61  1.1  joerg 	exit(1);
     62  1.1  joerg }
     63  1.1  joerg 
     64  1.4  joerg #if HAVE_NBTOOL_CONFIG_H && !defined(__NetBSD__)
     65  1.4  joerg #define	arc4random() rand()
     66  1.4  joerg #endif
     67  1.4  joerg 
     68  1.1  joerg static void
     69  1.1  joerg mi_vector_hash_seed_hash(struct nbperf *nbperf)
     70  1.1  joerg {
     71  1.4  joerg 	static uint32_t predictable_counter;
     72  1.4  joerg 	if (predictable)
     73  1.4  joerg 		nbperf->seed[0] = predictable_counter++;
     74  1.4  joerg 	else
     75  1.4  joerg 		nbperf->seed[0] = arc4random();
     76  1.1  joerg }
     77  1.1  joerg 
     78  1.1  joerg static void
     79  1.1  joerg mi_vector_hash_compute(struct nbperf *nbperf, const void *key, size_t keylen,
     80  1.1  joerg     uint32_t *hashes)
     81  1.1  joerg {
     82  1.1  joerg 	mi_vector_hash(key, keylen, nbperf->seed[0], hashes);
     83  1.1  joerg }
     84  1.1  joerg 
     85  1.1  joerg static void
     86  1.1  joerg mi_vector_hash_print_hash(struct nbperf *nbperf, const char *indent,
     87  1.1  joerg     const char *key, const char *keylen, const char *hash)
     88  1.1  joerg {
     89  1.1  joerg 	fprintf(nbperf->output,
     90  1.1  joerg 	    "%smi_vector_hash(%s, %s, 0x%08" PRIx32 "U, %s);\n",
     91  1.1  joerg 	    indent, key, keylen, nbperf->seed[0], hash);
     92  1.1  joerg }
     93  1.1  joerg 
     94  1.1  joerg static void
     95  1.1  joerg set_hash(struct nbperf *nbperf, const char *arg)
     96  1.1  joerg {
     97  1.1  joerg 	if (strcmp(arg, "mi_vector_hash") == 0) {
     98  1.1  joerg 		nbperf->hash_size = 3;
     99  1.1  joerg 		nbperf->seed_hash = mi_vector_hash_seed_hash;
    100  1.1  joerg 		nbperf->compute_hash = mi_vector_hash_compute;
    101  1.1  joerg 		nbperf->print_hash = mi_vector_hash_print_hash;
    102  1.1  joerg 		return;
    103  1.1  joerg 	}
    104  1.2  joerg 	if (nbperf->hash_size > NBPERF_MAX_HASH_SIZE)
    105  1.2  joerg 		errx(1, "Hash function creates too many output values");
    106  1.1  joerg 	errx(1, "Unknown hash function: %s", arg);
    107  1.1  joerg }
    108  1.1  joerg 
    109  1.1  joerg int
    110  1.1  joerg main(int argc, char **argv)
    111  1.1  joerg {
    112  1.1  joerg 	struct nbperf nbperf = {
    113  1.1  joerg 	    .c = 0,
    114  1.1  joerg 	    .hash_name = "hash",
    115  1.1  joerg 	    .map_output = NULL,
    116  1.1  joerg 	    .output = NULL,
    117  1.1  joerg 	    .static_hash = 0,
    118  1.3  joerg 	    .first_round = 1,
    119  1.3  joerg 	    .has_duplicates = 0,
    120  1.1  joerg 	};
    121  1.1  joerg 	FILE *input;
    122  1.1  joerg 	size_t curlen = 0, curalloc = 0;
    123  1.1  joerg 	char *line, *eos;
    124  1.4  joerg 	ssize_t line_len;
    125  1.4  joerg 	size_t line_allocated;
    126  1.1  joerg 	const void **keys = NULL;
    127  1.1  joerg 	size_t *keylens = NULL;
    128  1.1  joerg 	uint32_t max_iterations = 0xffffffU;
    129  1.1  joerg 	long long tmp;
    130  1.1  joerg 	int looped, ch;
    131  1.1  joerg 	int (*build_hash)(struct nbperf *) = chm_compute;
    132  1.1  joerg 
    133  1.1  joerg 	set_hash(&nbperf, "mi_vector_hash");
    134  1.1  joerg 
    135  1.4  joerg 	while ((ch = getopt(argc, argv, "a:c:h:i:m:n:o:ps")) != -1) {
    136  1.1  joerg 		switch (ch) {
    137  1.1  joerg 		case 'a':
    138  1.5  joerg 			/* Accept bdz as alias for netbsd-6 compat. */
    139  1.1  joerg 			if (strcmp(optarg, "chm") == 0)
    140  1.1  joerg 				build_hash = chm_compute;
    141  1.1  joerg 			else if (strcmp(optarg, "chm3") == 0)
    142  1.1  joerg 				build_hash = chm3_compute;
    143  1.5  joerg 			else if (strcmp(optarg, "bpz") == 0 ||
    144  1.5  joerg 			         strcmp(optarg, "bdz") == 0)
    145  1.5  joerg 				build_hash = bpz_compute;
    146  1.1  joerg 			else
    147  1.1  joerg 				errx(1, "Unsupport algorithm: %s", optarg);
    148  1.1  joerg 			break;
    149  1.1  joerg 		case 'c':
    150  1.1  joerg 			errno = 0;
    151  1.1  joerg 			nbperf.c = strtod(optarg, &eos);
    152  1.1  joerg 			if (errno || eos[0] || !nbperf.c)
    153  1.1  joerg 				errx(2, "Invalid argument for -c");
    154  1.1  joerg 			break;
    155  1.1  joerg 		case 'h':
    156  1.1  joerg 			set_hash(&nbperf, optarg);
    157  1.1  joerg 			break;
    158  1.1  joerg 		case 'i':
    159  1.1  joerg 			errno = 0;
    160  1.1  joerg 			tmp = strtoll(optarg, &eos, 0);
    161  1.1  joerg 			if (errno || eos == optarg || eos[0] ||
    162  1.1  joerg 			    tmp < 0 || tmp > 0xffffffffU)
    163  1.1  joerg 				errx(2, "Iteration count must be "
    164  1.1  joerg 				    "a 32bit integer");
    165  1.1  joerg 			max_iterations = (uint32_t)tmp;
    166  1.1  joerg 			break;
    167  1.1  joerg 		case 'm':
    168  1.1  joerg 			if (nbperf.map_output)
    169  1.1  joerg 				fclose(nbperf.map_output);
    170  1.1  joerg 			nbperf.map_output = fopen(optarg, "w");
    171  1.1  joerg 			if (nbperf.map_output == NULL)
    172  1.1  joerg 				err(2, "cannot open map file");
    173  1.1  joerg 			break;
    174  1.1  joerg 		case 'n':
    175  1.1  joerg 			nbperf.hash_name = optarg;
    176  1.1  joerg 			break;
    177  1.1  joerg 		case 'o':
    178  1.1  joerg 			if (nbperf.output)
    179  1.1  joerg 				fclose(nbperf.output);
    180  1.1  joerg 			nbperf.output = fopen(optarg, "w");
    181  1.1  joerg 			if (nbperf.output == NULL)
    182  1.1  joerg 				err(2, "cannot open output file");
    183  1.1  joerg 			break;
    184  1.4  joerg 		case 'p':
    185  1.4  joerg 			predictable = 1;
    186  1.4  joerg 			break;
    187  1.1  joerg 		case 's':
    188  1.1  joerg 			nbperf.static_hash = 1;
    189  1.1  joerg 			break;
    190  1.1  joerg 		default:
    191  1.1  joerg 			usage();
    192  1.1  joerg 		}
    193  1.1  joerg 	}
    194  1.1  joerg 
    195  1.1  joerg 	argc -= optind;
    196  1.1  joerg 	argv += optind;
    197  1.1  joerg 
    198  1.1  joerg 	if (argc > 1)
    199  1.1  joerg 		usage();
    200  1.1  joerg 
    201  1.1  joerg 	if (argc == 1) {
    202  1.1  joerg 		input = fopen(argv[0], "r");
    203  1.1  joerg 		if (input == NULL)
    204  1.1  joerg 			err(1, "can't open input file");
    205  1.1  joerg 	} else
    206  1.1  joerg 		input = stdin;
    207  1.1  joerg 
    208  1.1  joerg 	if (nbperf.output == NULL)
    209  1.1  joerg 		nbperf.output = stdout;
    210  1.1  joerg 
    211  1.4  joerg 	line = NULL;
    212  1.4  joerg 	line_allocated = 0;
    213  1.4  joerg 	while ((line_len = getline(&line, &line_allocated, input)) != -1) {
    214  1.1  joerg 		if (line_len && line[line_len - 1] == '\n')
    215  1.1  joerg 			--line_len;
    216  1.1  joerg 		if (curlen == curalloc) {
    217  1.1  joerg 			if (curalloc < 256)
    218  1.1  joerg 				curalloc = 256;
    219  1.1  joerg 			else
    220  1.1  joerg 				curalloc += curalloc;
    221  1.1  joerg 			keys = realloc(keys, curalloc * sizeof(*keys));
    222  1.1  joerg 			if (keys == NULL)
    223  1.1  joerg 				err(1, "realloc failed");
    224  1.1  joerg 			keylens = realloc(keylens,
    225  1.1  joerg 			    curalloc * sizeof(*keylens));
    226  1.1  joerg 			if (keylens == NULL)
    227  1.1  joerg 				err(1, "realloc failed");
    228  1.1  joerg 		}
    229  1.1  joerg 		if ((keys[curlen] = strndup(line, line_len)) == NULL)
    230  1.1  joerg 			err(1, "malloc failed");
    231  1.1  joerg 		keylens[curlen] = line_len;
    232  1.1  joerg 		++curlen;
    233  1.1  joerg 	}
    234  1.4  joerg 	free(line);
    235  1.1  joerg 
    236  1.1  joerg 	if (input != stdin)
    237  1.1  joerg 		fclose(input);
    238  1.1  joerg 
    239  1.1  joerg 	nbperf.n = curlen;
    240  1.1  joerg 	nbperf.keys = keys;
    241  1.1  joerg 	nbperf.keylens = keylens;
    242  1.1  joerg 
    243  1.1  joerg 	looped = 0;
    244  1.1  joerg 	while ((*build_hash)(&nbperf)) {
    245  1.3  joerg 		if (nbperf.has_duplicates)
    246  1.3  joerg 			errx(1, "Duplicate keys detected");
    247  1.1  joerg 		fputc('.', stderr);
    248  1.1  joerg 		looped = 1;
    249  1.1  joerg 		if (max_iterations == 0xffffffffU)
    250  1.1  joerg 			continue;
    251  1.1  joerg 		if (--max_iterations == 0) {
    252  1.1  joerg 			fputc('\n', stderr);
    253  1.1  joerg 			errx(1, "Iteration count reached");
    254  1.1  joerg 		}
    255  1.1  joerg 	}
    256  1.1  joerg 	if (looped)
    257  1.1  joerg 		fputc('\n', stderr);
    258  1.1  joerg 
    259  1.1  joerg 	return 0;
    260  1.1  joerg }
    261