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