9ba19bf9975fabf91e9e05c56a4b3751609d54d5
[fio.git] / t / genzipf.c
1 /*
2  * Generate/analyze pareto/zipf distributions to better understand
3  * what an access pattern would look like.
4  *
5  * For instance, the following would generate a zipf distribution
6  * with theta 1.2, using 262144 (1 GB / 4096) values and split the reporting into
7  * 20 buckets:
8  *
9  *      ./t/fio-genzipf -t zipf -i 1.2 -g 1 -b 4096 -o 20
10  *
11  * Only the distribution type (zipf or pareto) and spread input need
12  * to be given, if not given defaults are used.
13  *
14  */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 #include "../lib/zipf.h"
22 #include "../flist.h"
23 #include "../hash.h"
24
25 #define DEF_NR_OUTPUT   20
26
27 struct node {
28         struct flist_head list;
29         unsigned long long val;
30         unsigned long hits;
31 };
32
33 static struct flist_head *hash;
34 static unsigned long hash_bits = 24;
35 static unsigned long hash_size = 1 << 24;
36
37 enum {
38         TYPE_NONE = 0,
39         TYPE_ZIPF,
40         TYPE_PARETO,
41 };
42 static const char *dist_types[] = { "None", "Zipf", "Pareto" };
43
44 static int dist_type = TYPE_ZIPF;
45 static unsigned long gb_size = 500;
46 static unsigned long block_size = 4096;
47 static unsigned long output_nranges = DEF_NR_OUTPUT;
48 static double percentage;
49 static double dist_val;
50 static int output_csv = 0;
51
52 #define DEF_ZIPF_VAL    1.2
53 #define DEF_PARETO_VAL  0.3
54
55 static struct node *hash_lookup(unsigned long long val)
56 {
57         struct flist_head *l = &hash[hash_long(val, hash_bits)];
58         struct flist_head *entry;
59         struct node *n;
60
61         flist_for_each(entry, l) {
62                 n = flist_entry(entry, struct node, list);
63                 if (n->val == val)
64                         return n;
65         }
66
67         return NULL;
68 }
69
70 static struct node *hash_insert(struct node *n, unsigned long long val)
71 {
72         struct flist_head *l = &hash[hash_long(val, hash_bits)];
73
74         n->val = val;
75         n->hits = 1;
76         flist_add_tail(&n->list, l);
77         return n;
78 }
79
80 static void usage(void)
81 {
82         printf("genzipf: test zipf/pareto values for fio input\n");
83         printf("\t-h\tThis help screen\n");
84         printf("\t-p\tGenerate size of data set that are hit by this percentage\n");
85         printf("\t-t\tDistribution type (zipf or pareto)\n");
86         printf("\t-i\tDistribution algorithm input (zipf theta or pareto power)\n");
87         printf("\t-b\tBlock size of a given range (in bytes)\n");
88         printf("\t-g\tSize of data set (in gigabytes)\n");
89         printf("\t-o\tNumber of output rows\n");
90         printf("\t-c\tOutput ranges in CSV format\n");
91 }
92
93 static int parse_options(int argc, char *argv[])
94 {
95         const char *optstring = "t:g:i:o:b:p:ch";
96         int c, dist_val_set = 0;
97
98         while ((c = getopt(argc, argv, optstring)) != -1) {
99                 switch (c) {
100                 case 'h':
101                         usage();
102                         return 1;
103                 case 'p':
104                         percentage = atof(optarg);
105                         break;
106                 case 'b':
107                         block_size = strtoul(optarg, NULL, 10);
108                         break;
109                 case 't':
110                         if (!strncmp(optarg, "zipf", 4))
111                                 dist_type = TYPE_ZIPF;
112                         else if (!strncmp(optarg, "pareto", 6))
113                                 dist_type = TYPE_PARETO;
114                         else {
115                                 printf("wrong dist type: %s\n", optarg);
116                                 return 1;
117                         }
118                         break;
119                 case 'g':
120                         gb_size = strtoul(optarg, NULL, 10);
121                         break;
122                 case 'i':
123                         dist_val = atof(optarg);
124                         dist_val_set = 1;
125                         break;
126                 case 'o':
127                         output_nranges = strtoul(optarg, NULL, 10);
128                         break;
129                 case 'c':
130                         output_csv = 1;
131                         break;
132                 default:
133                         printf("bad option %c\n", c);
134                         return 1;
135                 }
136         }
137
138         if (dist_type == TYPE_PARETO) {
139                 if ((dist_val >= 1.00 || dist_val < 0.00)) {
140                         printf("pareto input must be > 0.00 and < 1.00\n");
141                         return 1;
142                 }
143                 if (!dist_val_set)
144                         dist_val = DEF_PARETO_VAL;
145         } else if (dist_type == TYPE_ZIPF) {
146                 if (dist_val == 1.0) {
147                         printf("zipf input must be different than 1.0\n");
148                         return 1;
149                 }
150                 if (!dist_val_set)
151                         dist_val = DEF_ZIPF_VAL;
152         }
153
154         return 0;
155 }
156
157 struct output_sum {
158         double output;
159         unsigned int nranges;
160 };
161
162 static int node_cmp(const void *p1, const void *p2)
163 {
164         const struct node *n1 = p1;
165         const struct node *n2 = p2;
166
167         return n2->hits - n1->hits;
168 }
169
170 int main(int argc, char *argv[])
171 {
172         unsigned long offset;
173         unsigned long i, j, k, nr_vals, cur_vals, interval_step, next_interval, total_vals, nnodes;
174         unsigned long long nranges;
175         struct output_sum *output_sums;
176         struct node *nodes;
177         double perc, perc_i;
178         struct zipf_state zs;
179
180         if (parse_options(argc, argv))
181                 return 1;
182
183         if( !output_csv )
184                 printf("Generating %s distribution with %f input and %lu GB size and %lu block_size.\n", dist_types[dist_type], dist_val, gb_size, block_size);
185
186         nranges = gb_size * 1024 * 1024 * 1024ULL;
187         nranges /= block_size;
188
189         if (dist_type == TYPE_ZIPF)
190                 zipf_init(&zs, nranges, dist_val, 1);
191         else
192                 pareto_init(&zs, nranges, dist_val, 1);
193
194         hash_bits = 0;
195         hash_size = nranges;
196         while ((hash_size >>= 1) != 0)
197                 hash_bits++;
198
199         hash_size = 1 << hash_bits;
200
201         hash = malloc(hash_size * sizeof(struct flist_head));
202         for (i = 0; i < hash_size; i++)
203                 INIT_FLIST_HEAD(&hash[i]);
204
205         nodes = malloc(nranges * sizeof(struct node));
206
207         for (i = j = 0; i < nranges; i++) {
208                 struct node *n;
209
210                 if (dist_type == TYPE_ZIPF)
211                         offset = zipf_next(&zs);
212                 else
213                         offset = pareto_next(&zs);
214
215                 n = hash_lookup(offset);
216                 if (n)
217                         n->hits++;
218                 else {
219                         hash_insert(&nodes[j], offset);
220                         j++;
221                 }
222
223         }
224
225         qsort(nodes, j, sizeof(struct node), node_cmp);
226         nnodes = j;
227         nr_vals = nnodes;
228
229         if (output_csv) {
230                 printf("rank, count\n");
231                 for (k = 0; k < nnodes; k++) {
232                         printf("%lu, %lu\n", k, nodes[k].hits);
233                 }
234         } else {
235                 unsigned long blocks = percentage * nranges / 100;
236                 double hit_percent_sum = 0;
237                 unsigned long long hit_sum = 0;
238                 interval_step = (nr_vals - 1) / output_nranges + 1;
239                 next_interval = interval_step;
240                 output_sums = malloc(output_nranges * sizeof(struct output_sum));
241                 for (i = 0; i < output_nranges; i++) {
242                         output_sums[i].output = 0.0;
243                         output_sums[i].nranges = 0;
244                 }
245
246                 j = total_vals = cur_vals = 0;
247
248                 for (k = 0; k < nnodes; k++) {
249                         struct output_sum *os = &output_sums[j];
250                         struct node *node = &nodes[k];
251                         cur_vals += node->hits;
252                         total_vals += node->hits;
253                         os->nranges += node->hits;
254                         if (k == (next_interval)  -1 || k == (nnodes - 1)) {
255                                 os->output = (double)(cur_vals) / (double)nranges;
256                                 os->output *= 100.0;
257                                 cur_vals = 0;
258                                 next_interval += interval_step;
259                                 j++;
260                         }
261
262                         if (percentage) {
263                                 if (total_vals >= blocks) {
264                                         double cs = k * block_size / (1024 * 1024);
265                                         char p = 'M';
266
267                                         if (cs > 1024.0) {
268                                                 cs /= 1024.0;
269                                                 p = 'G';
270                                         }
271                                         if (cs > 1024.0) {
272                                                 cs /= 1024.0;
273                                                 p = 'T';
274                                         }
275
276                                         printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
277                                         percentage = 0.0;
278                                 }
279                         }
280                 }
281
282                 perc_i = 100.0 / (double)output_nranges;
283                 perc = 0.0;
284
285                 printf("\n   Rows           Hits %%         No Hits         Size\n");
286                 printf("--------------------------------------------------------\n");
287                 for (i = 0; i < output_nranges; i++) {
288                         struct output_sum *os = &output_sums[i];
289                         double gb = (double)os->nranges * block_size / 1024.0;
290                         char p = 'K';
291
292                         if (gb > 1024.0) {
293                                 p = 'M';
294                                 gb /= 1024.0;
295                         }
296                         if (gb > 1024.0) {
297                                 p = 'G';
298                                 gb /= 1024.0;
299                         }
300
301                         perc += perc_i;
302                         hit_percent_sum += os->output;
303                         hit_sum += os->nranges;
304                         printf("%s %6.2f%%\t%6.2f%%\t\t%8u\t%6.2f%c\n",
305                                i ? "|->" : "Top", perc, os->output, os->nranges,
306                                gb, p);
307                 }
308                 printf("--------------------------------------------------------\n");
309                 printf("Total\t\t%6.2f%%\t\t%8llu\n", hit_percent_sum, hit_sum);
310                 free(output_sums);
311         }
312
313         free(hash);
314         free(nodes);
315         return 0;
316 }