Home | History | Annotate | Line # | Download | only in nbperf
nbperf.c revision 1.1
      1  1.1  joerg /*	$NetBSD: nbperf.c,v 1.1 2009/08/15 16:21:05 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.1  joerg #include <sys/cdefs.h>
     35  1.1  joerg __RCSID("$NetBSD: nbperf.c,v 1.1 2009/08/15 16:21:05 joerg Exp $");
     36  1.1  joerg 
     37  1.1  joerg #include <sys/endian.h>
     38  1.1  joerg #include <err.h>
     39  1.1  joerg #include <errno.h>
     40  1.1  joerg #include <inttypes.h>
     41  1.1  joerg #include <stdlib.h>
     42  1.1  joerg #include <stdio.h>
     43  1.1  joerg #include <string.h>
     44  1.1  joerg #include <unistd.h>
     45  1.1  joerg 
     46  1.1  joerg #include "nbperf.h"
     47  1.1  joerg 
     48  1.1  joerg static __dead
     49  1.1  joerg void usage(void)
     50  1.1  joerg {
     51  1.1  joerg 	fprintf(stderr,
     52  1.1  joerg 	    "%s [-s] [-c utilisation] [-i iterations] [-n name] "
     53  1.1  joerg 	    "[-o output] input\n",
     54  1.1  joerg 	    getprogname());
     55  1.1  joerg 	exit(1);
     56  1.1  joerg }
     57  1.1  joerg 
     58  1.1  joerg static void
     59  1.1  joerg mi_vector_hash_seed_hash(struct nbperf *nbperf)
     60  1.1  joerg {
     61  1.1  joerg 	nbperf->seed[0] = arc4random();
     62  1.1  joerg }
     63  1.1  joerg 
     64  1.1  joerg static void
     65  1.1  joerg mi_vector_hash_compute(struct nbperf *nbperf, const void *key, size_t keylen,
     66  1.1  joerg     uint32_t *hashes)
     67  1.1  joerg {
     68  1.1  joerg 	mi_vector_hash(key, keylen, nbperf->seed[0], hashes);
     69  1.1  joerg }
     70  1.1  joerg 
     71  1.1  joerg static void
     72  1.1  joerg mi_vector_hash_print_hash(struct nbperf *nbperf, const char *indent,
     73  1.1  joerg     const char *key, const char *keylen, const char *hash)
     74  1.1  joerg {
     75  1.1  joerg 	fprintf(nbperf->output,
     76  1.1  joerg 	    "%smi_vector_hash(%s, %s, 0x%08" PRIx32 "U, %s);\n",
     77  1.1  joerg 	    indent, key, keylen, nbperf->seed[0], hash);
     78  1.1  joerg }
     79  1.1  joerg 
     80  1.1  joerg static void
     81  1.1  joerg set_hash(struct nbperf *nbperf, const char *arg)
     82  1.1  joerg {
     83  1.1  joerg 	if (strcmp(arg, "mi_vector_hash") == 0) {
     84  1.1  joerg 		nbperf->hash_size = 3;
     85  1.1  joerg 		nbperf->seed_hash = mi_vector_hash_seed_hash;
     86  1.1  joerg 		nbperf->compute_hash = mi_vector_hash_compute;
     87  1.1  joerg 		nbperf->print_hash = mi_vector_hash_print_hash;
     88  1.1  joerg 		return;
     89  1.1  joerg 	}
     90  1.1  joerg 	errx(1, "Unknown hash function: %s", arg);
     91  1.1  joerg }
     92  1.1  joerg 
     93  1.1  joerg int
     94  1.1  joerg main(int argc, char **argv)
     95  1.1  joerg {
     96  1.1  joerg 	struct nbperf nbperf = {
     97  1.1  joerg 	    .c = 0,
     98  1.1  joerg 	    .hash_name = "hash",
     99  1.1  joerg 	    .map_output = NULL,
    100  1.1  joerg 	    .output = NULL,
    101  1.1  joerg 	    .static_hash = 0,
    102  1.1  joerg 	};
    103  1.1  joerg 	FILE *input;
    104  1.1  joerg 	size_t curlen = 0, curalloc = 0;
    105  1.1  joerg 	char *line, *eos;
    106  1.1  joerg 	size_t line_len;
    107  1.1  joerg 	const void **keys = NULL;
    108  1.1  joerg 	size_t *keylens = NULL;
    109  1.1  joerg 	uint32_t max_iterations = 0xffffffU;
    110  1.1  joerg 	long long tmp;
    111  1.1  joerg 	int looped, ch;
    112  1.1  joerg 	int (*build_hash)(struct nbperf *) = chm_compute;
    113  1.1  joerg 
    114  1.1  joerg 	set_hash(&nbperf, "mi_vector_hash");
    115  1.1  joerg 
    116  1.1  joerg 	while ((ch = getopt(argc, argv, "a:c:h:i:m:n:o:s")) != -1) {
    117  1.1  joerg 		switch (ch) {
    118  1.1  joerg 		case 'a':
    119  1.1  joerg 			if (strcmp(optarg, "chm") == 0)
    120  1.1  joerg 				build_hash = chm_compute;
    121  1.1  joerg 			else if (strcmp(optarg, "chm3") == 0)
    122  1.1  joerg 				build_hash = chm3_compute;
    123  1.1  joerg 			else if (strcmp(optarg, "bdz") == 0)
    124  1.1  joerg 				build_hash = bdz_compute;
    125  1.1  joerg 			else
    126  1.1  joerg 				errx(1, "Unsupport algorithm: %s", optarg);
    127  1.1  joerg 			break;
    128  1.1  joerg 		case 'c':
    129  1.1  joerg 			errno = 0;
    130  1.1  joerg 			nbperf.c = strtod(optarg, &eos);
    131  1.1  joerg 			if (errno || eos[0] || !nbperf.c)
    132  1.1  joerg 				errx(2, "Invalid argument for -c");
    133  1.1  joerg 			break;
    134  1.1  joerg 		case 'h':
    135  1.1  joerg 			set_hash(&nbperf, optarg);
    136  1.1  joerg 			break;
    137  1.1  joerg 		case 'i':
    138  1.1  joerg 			errno = 0;
    139  1.1  joerg 			tmp = strtoll(optarg, &eos, 0);
    140  1.1  joerg 			if (errno || eos == optarg || eos[0] ||
    141  1.1  joerg 			    tmp < 0 || tmp > 0xffffffffU)
    142  1.1  joerg 				errx(2, "Iteration count must be "
    143  1.1  joerg 				    "a 32bit integer");
    144  1.1  joerg 			max_iterations = (uint32_t)tmp;
    145  1.1  joerg 			break;
    146  1.1  joerg 		case 'm':
    147  1.1  joerg 			if (nbperf.map_output)
    148  1.1  joerg 				fclose(nbperf.map_output);
    149  1.1  joerg 			nbperf.map_output = fopen(optarg, "w");
    150  1.1  joerg 			if (nbperf.map_output == NULL)
    151  1.1  joerg 				err(2, "cannot open map file");
    152  1.1  joerg 			break;
    153  1.1  joerg 		case 'n':
    154  1.1  joerg 			nbperf.hash_name = optarg;
    155  1.1  joerg 			break;
    156  1.1  joerg 		case 'o':
    157  1.1  joerg 			if (nbperf.output)
    158  1.1  joerg 				fclose(nbperf.output);
    159  1.1  joerg 			nbperf.output = fopen(optarg, "w");
    160  1.1  joerg 			if (nbperf.output == NULL)
    161  1.1  joerg 				err(2, "cannot open output file");
    162  1.1  joerg 			break;
    163  1.1  joerg 		case 's':
    164  1.1  joerg 			nbperf.static_hash = 1;
    165  1.1  joerg 			break;
    166  1.1  joerg 		default:
    167  1.1  joerg 			usage();
    168  1.1  joerg 		}
    169  1.1  joerg 	}
    170  1.1  joerg 
    171  1.1  joerg 	argc -= optind;
    172  1.1  joerg 	argv += optind;
    173  1.1  joerg 
    174  1.1  joerg 	if (argc > 1)
    175  1.1  joerg 		usage();
    176  1.1  joerg 
    177  1.1  joerg 	if (argc == 1) {
    178  1.1  joerg 		input = fopen(argv[0], "r");
    179  1.1  joerg 		if (input == NULL)
    180  1.1  joerg 			err(1, "can't open input file");
    181  1.1  joerg 	} else
    182  1.1  joerg 		input = stdin;
    183  1.1  joerg 
    184  1.1  joerg 	if (nbperf.output == NULL)
    185  1.1  joerg 		nbperf.output = stdout;
    186  1.1  joerg 
    187  1.1  joerg 	while ((line = fgetln(input, &line_len)) != NULL) {
    188  1.1  joerg 		if (line_len && line[line_len - 1] == '\n')
    189  1.1  joerg 			--line_len;
    190  1.1  joerg 		if (curlen == curalloc) {
    191  1.1  joerg 			if (curalloc < 256)
    192  1.1  joerg 				curalloc = 256;
    193  1.1  joerg 			else
    194  1.1  joerg 				curalloc += curalloc;
    195  1.1  joerg 			keys = realloc(keys, curalloc * sizeof(*keys));
    196  1.1  joerg 			if (keys == NULL)
    197  1.1  joerg 				err(1, "realloc failed");
    198  1.1  joerg 			keylens = realloc(keylens,
    199  1.1  joerg 			    curalloc * sizeof(*keylens));
    200  1.1  joerg 			if (keylens == NULL)
    201  1.1  joerg 				err(1, "realloc failed");
    202  1.1  joerg 		}
    203  1.1  joerg 		if ((keys[curlen] = strndup(line, line_len)) == NULL)
    204  1.1  joerg 			err(1, "malloc failed");
    205  1.1  joerg 		keylens[curlen] = line_len;
    206  1.1  joerg 		++curlen;
    207  1.1  joerg 	}
    208  1.1  joerg 
    209  1.1  joerg 	if (input != stdin)
    210  1.1  joerg 		fclose(input);
    211  1.1  joerg 
    212  1.1  joerg 	nbperf.n = curlen;
    213  1.1  joerg 	nbperf.keys = keys;
    214  1.1  joerg 	nbperf.keylens = keylens;
    215  1.1  joerg 
    216  1.1  joerg 	looped = 0;
    217  1.1  joerg 	while ((*build_hash)(&nbperf)) {
    218  1.1  joerg 		fputc('.', stderr);
    219  1.1  joerg 		looped = 1;
    220  1.1  joerg 		if (max_iterations == 0xffffffffU)
    221  1.1  joerg 			continue;
    222  1.1  joerg 		if (--max_iterations == 0) {
    223  1.1  joerg 			fputc('\n', stderr);
    224  1.1  joerg 			errx(1, "Iteration count reached");
    225  1.1  joerg 		}
    226  1.1  joerg 	}
    227  1.1  joerg 	if (looped)
    228  1.1  joerg 		fputc('\n', stderr);
    229  1.1  joerg 
    230  1.1  joerg 	return 0;
    231  1.1  joerg }
    232