xref: /spdk/examples/util/zipf/zipf.c (revision 7506a7aa53d239f533af3bc768f0d2af55e735fe)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 #include "spdk/zipf.h"
36 #include "spdk/histogram_data.h"
37 #include "spdk/string.h"
38 
39 static void
40 usage(const char *prog)
41 {
42 	printf("usage: %s <theta> <range> <count>\n", prog);
43 }
44 
45 static void
46 print_bucket(void *ctx, uint64_t start, uint64_t end, uint64_t count,
47 	     uint64_t total, uint64_t so_far)
48 {
49 	double so_far_pct;
50 	char range[64];
51 
52 	if (count == 0) {
53 		return;
54 	}
55 
56 	so_far_pct = (double)so_far * 100 / total;
57 	snprintf(range, sizeof(range), "[%ju, %ju)", start, end);
58 	printf("%24s: %9.4f%%  (%9ju)\n", range, so_far_pct, count);
59 }
60 
61 int
62 main(int argc, char **argv)
63 {
64 	struct spdk_zipf *zipf;
65 	struct spdk_histogram_data *h;
66 	float theta;
67 	int range, count, i;
68 
69 	if (argc < 4) {
70 		usage(argv[0]);
71 		return 1;
72 	}
73 
74 	theta = atof(argv[1]);
75 	range = spdk_strtol(argv[2], 10);
76 	count = spdk_strtol(argv[3], 10);
77 
78 	if (range <= 0 || count <= 0) {
79 		printf("range and count must be positive integers\n");
80 		usage(argv[0]);
81 		return 1;
82 	}
83 
84 	zipf = spdk_zipf_create(range, theta, time(NULL));
85 	h = spdk_histogram_data_alloc();
86 	if (zipf == NULL || h == NULL) {
87 		spdk_zipf_free(&zipf);
88 		spdk_histogram_data_free(h);
89 		printf("out of resource\n");
90 		return 1;
91 	}
92 
93 	for (i = 0; i < count; i++) {
94 		spdk_histogram_data_tally(h, spdk_zipf_generate(zipf));
95 	}
96 
97 	spdk_histogram_data_iterate(h, print_bucket, NULL);
98 	spdk_histogram_data_free(h);
99 	spdk_zipf_free(&zipf);
100 
101 	return 0;
102 }
103