xref: /spdk/examples/util/zipf/zipf.c (revision a6dbe3721eb3b5990707fc3e378c95e505dd8ab5)
1488570ebSJim Harris /*   SPDX-License-Identifier: BSD-3-Clause
2*a6dbe372Spaul luse  *   Copyright (C) 2021 Intel Corporation.
3e6b7e585SJim Harris  *   All rights reserved.
4e6b7e585SJim Harris  */
5e6b7e585SJim Harris 
6e6b7e585SJim Harris #include "spdk/stdinc.h"
7e6b7e585SJim Harris #include "spdk/zipf.h"
8e6b7e585SJim Harris #include "spdk/histogram_data.h"
9e6b7e585SJim Harris #include "spdk/string.h"
10e6b7e585SJim Harris 
11e6b7e585SJim Harris static void
usage(const char * prog)12e6b7e585SJim Harris usage(const char *prog)
13e6b7e585SJim Harris {
14e6b7e585SJim Harris 	printf("usage: %s <theta> <range> <count>\n", prog);
15e6b7e585SJim Harris }
16e6b7e585SJim Harris 
17e6b7e585SJim Harris static void
print_bucket(void * ctx,uint64_t start,uint64_t end,uint64_t count,uint64_t total,uint64_t so_far)18e6b7e585SJim Harris print_bucket(void *ctx, uint64_t start, uint64_t end, uint64_t count,
19e6b7e585SJim Harris 	     uint64_t total, uint64_t so_far)
20e6b7e585SJim Harris {
21e6b7e585SJim Harris 	double so_far_pct;
22e6b7e585SJim Harris 	char range[64];
23e6b7e585SJim Harris 
24e6b7e585SJim Harris 	if (count == 0) {
25e6b7e585SJim Harris 		return;
26e6b7e585SJim Harris 	}
27e6b7e585SJim Harris 
28e6b7e585SJim Harris 	so_far_pct = (double)so_far * 100 / total;
29e6b7e585SJim Harris 	snprintf(range, sizeof(range), "[%ju, %ju)", start, end);
30e6b7e585SJim Harris 	printf("%24s: %9.4f%%  (%9ju)\n", range, so_far_pct, count);
31e6b7e585SJim Harris }
32e6b7e585SJim Harris 
33e6b7e585SJim Harris int
main(int argc,char ** argv)34e6b7e585SJim Harris main(int argc, char **argv)
35e6b7e585SJim Harris {
36e6b7e585SJim Harris 	struct spdk_zipf *zipf;
37e6b7e585SJim Harris 	struct spdk_histogram_data *h;
38e6b7e585SJim Harris 	float theta;
39e6b7e585SJim Harris 	int range, count, i;
40e6b7e585SJim Harris 
41e6b7e585SJim Harris 	if (argc < 4) {
42e6b7e585SJim Harris 		usage(argv[0]);
43e6b7e585SJim Harris 		return 1;
44e6b7e585SJim Harris 	}
45e6b7e585SJim Harris 
46e6b7e585SJim Harris 	theta = atof(argv[1]);
47e6b7e585SJim Harris 	range = spdk_strtol(argv[2], 10);
48e6b7e585SJim Harris 	count = spdk_strtol(argv[3], 10);
49e6b7e585SJim Harris 
50e6b7e585SJim Harris 	if (range <= 0 || count <= 0) {
51e6b7e585SJim Harris 		printf("range and count must be positive integers\n");
52e6b7e585SJim Harris 		usage(argv[0]);
53e6b7e585SJim Harris 		return 1;
54e6b7e585SJim Harris 	}
55e6b7e585SJim Harris 
56e6b7e585SJim Harris 	zipf = spdk_zipf_create(range, theta, time(NULL));
57e6b7e585SJim Harris 	h = spdk_histogram_data_alloc();
589cedc720SGangCao 	if (zipf == NULL || h == NULL) {
599cedc720SGangCao 		spdk_zipf_free(&zipf);
609cedc720SGangCao 		spdk_histogram_data_free(h);
619cedc720SGangCao 		printf("out of resource\n");
629cedc720SGangCao 		return 1;
639cedc720SGangCao 	}
64e6b7e585SJim Harris 
65e6b7e585SJim Harris 	for (i = 0; i < count; i++) {
66e6b7e585SJim Harris 		spdk_histogram_data_tally(h, spdk_zipf_generate(zipf));
67e6b7e585SJim Harris 	}
68e6b7e585SJim Harris 
69e6b7e585SJim Harris 	spdk_histogram_data_iterate(h, print_bucket, NULL);
70e6b7e585SJim Harris 	spdk_histogram_data_free(h);
71e6b7e585SJim Harris 	spdk_zipf_free(&zipf);
72e6b7e585SJim Harris 
73e6b7e585SJim Harris 	return 0;
74e6b7e585SJim Harris }
75