2 * Generate/analyze pareto/zipf distributions to better understand
3 * what an access pattern would look like.
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
9 * ./t/fio-genzipf -t zipf -i 1.2 -g 1 -b 4096 -o 20
11 * Only the distribution type (zipf or pareto) and spread input need
12 * to be given, if not given defaults are used.
21 #include "../lib/zipf.h"
22 #include "../lib/gauss.h"
26 #define DEF_NR_OUTPUT 20
29 struct flist_head list;
30 unsigned long long val;
34 static struct flist_head *hash;
35 static unsigned long hash_bits = 24;
36 static unsigned long hash_size = 1 << 24;
44 static const char *dist_types[] = { "None", "Zipf", "Pareto", "Normal" };
51 static int dist_type = TYPE_ZIPF;
52 static unsigned long gb_size = 500;
53 static unsigned long block_size = 4096;
54 static unsigned long output_nranges = DEF_NR_OUTPUT;
55 static double percentage;
56 static double dist_val;
57 static int output_type = OUTPUT_NORMAL;
59 #define DEF_ZIPF_VAL 1.2
60 #define DEF_PARETO_VAL 0.3
62 static struct node *hash_lookup(unsigned long long val)
64 struct flist_head *l = &hash[hash_long(val, hash_bits)];
65 struct flist_head *entry;
68 flist_for_each(entry, l) {
69 n = flist_entry(entry, struct node, list);
77 static struct node *hash_insert(struct node *n, unsigned long long val)
79 struct flist_head *l = &hash[hash_long(val, hash_bits)];
83 flist_add_tail(&n->list, l);
87 static void usage(void)
89 printf("genzipf: test zipf/pareto values for fio input\n");
90 printf("\t-h\tThis help screen\n");
91 printf("\t-p\tGenerate size of data set that are hit by this percentage\n");
92 printf("\t-t\tDistribution type (zipf, pareto, or normal)\n");
93 printf("\t-i\tDistribution algorithm input (zipf theta, pareto power,\n"
94 "\t\tor normal %% deviation)\n");
95 printf("\t-b\tBlock size of a given range (in bytes)\n");
96 printf("\t-g\tSize of data set (in gigabytes)\n");
97 printf("\t-o\tNumber of output rows\n");
98 printf("\t-c\tOutput ranges in CSV format\n");
101 static int parse_options(int argc, char *argv[])
103 const char *optstring = "t:g:i:o:b:p:ch";
104 int c, dist_val_set = 0;
106 while ((c = getopt(argc, argv, optstring)) != -1) {
112 percentage = atof(optarg);
115 block_size = strtoul(optarg, NULL, 10);
118 if (!strncmp(optarg, "zipf", 4))
119 dist_type = TYPE_ZIPF;
120 else if (!strncmp(optarg, "pareto", 6))
121 dist_type = TYPE_PARETO;
122 else if (!strncmp(optarg, "normal", 6))
123 dist_type = TYPE_NORMAL;
125 printf("wrong dist type: %s\n", optarg);
130 gb_size = strtoul(optarg, NULL, 10);
133 dist_val = atof(optarg);
137 output_nranges = strtoul(optarg, NULL, 10);
140 output_type = OUTPUT_CSV;
143 printf("bad option %c\n", c);
148 if (dist_type == TYPE_PARETO) {
149 if ((dist_val >= 1.00 || dist_val < 0.00)) {
150 printf("pareto input must be > 0.00 and < 1.00\n");
154 dist_val = DEF_PARETO_VAL;
155 } else if (dist_type == TYPE_ZIPF) {
156 if (dist_val == 1.0) {
157 printf("zipf input must be different than 1.0\n");
161 dist_val = DEF_ZIPF_VAL;
169 unsigned int nranges;
172 static int node_cmp(const void *p1, const void *p2)
174 const struct node *n1 = p1;
175 const struct node *n2 = p2;
177 return n2->hits - n1->hits;
180 static void output_csv(struct node *nodes, unsigned long nnodes)
184 printf("rank, count\n");
185 for (i = 0; i < nnodes; i++)
186 printf("%lu, %lu\n", i, nodes[i].hits);
189 static void output_normal(struct node *nodes, unsigned long nnodes,
190 unsigned long nranges)
192 unsigned long i, j, cur_vals, interval_step, next_interval, total_vals;
193 unsigned long blocks = percentage * nnodes / 100;
194 double hit_percent_sum = 0;
195 unsigned long long hit_sum = 0;
197 struct output_sum *output_sums;
199 interval_step = (nnodes - 1) / output_nranges + 1;
200 next_interval = interval_step;
201 output_sums = malloc(output_nranges * sizeof(struct output_sum));
203 for (i = 0; i < output_nranges; i++) {
204 output_sums[i].output = 0.0;
205 output_sums[i].nranges = 0;
208 j = total_vals = cur_vals = 0;
210 for (i = 0; i < nnodes; i++) {
211 struct output_sum *os = &output_sums[j];
212 struct node *node = &nodes[i];
213 cur_vals += node->hits;
214 total_vals += node->hits;
215 os->nranges += node->hits;
216 if (i == (next_interval) -1 || i == nnodes - 1) {
217 os->output = (double) cur_vals / (double) nranges;
220 next_interval += interval_step;
225 if (total_vals >= blocks) {
226 double cs = i * block_size / (1024 * 1024);
238 printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
244 perc_i = 100.0 / (double)output_nranges;
247 printf("\n Rows Hits %% Sum %% # Hits Size\n");
248 printf("-----------------------------------------------------------------------\n");
249 for (i = 0; i < output_nranges; i++) {
250 struct output_sum *os = &output_sums[i];
251 double gb = (double)os->nranges * block_size / 1024.0;
264 hit_percent_sum += os->output;
265 hit_sum += os->nranges;
266 printf("%s %6.2f%%\t%6.2f%%\t\t%6.2f%%\t\t%8u\t%6.2f%c\n",
267 i ? "|->" : "Top", perc, os->output, hit_percent_sum,
271 printf("-----------------------------------------------------------------------\n");
272 printf("Total\t\t\t\t\t\t%8llu\n", hit_sum);
276 int main(int argc, char *argv[])
278 unsigned long offset;
279 unsigned long long nranges;
280 unsigned long nnodes;
282 struct zipf_state zs;
283 struct gauss_state gs;
286 if (parse_options(argc, argv))
289 if (output_type != OUTPUT_CSV)
290 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);
292 nranges = gb_size * 1024 * 1024 * 1024ULL;
293 nranges /= block_size;
295 if (dist_type == TYPE_ZIPF)
296 zipf_init(&zs, nranges, dist_val, 1);
297 else if (dist_type == TYPE_PARETO)
298 pareto_init(&zs, nranges, dist_val, 1);
300 gauss_init(&gs, nranges, dist_val, 1);
304 while ((hash_size >>= 1) != 0)
307 hash_size = 1 << hash_bits;
309 hash = malloc(hash_size * sizeof(struct flist_head));
310 for (i = 0; i < hash_size; i++)
311 INIT_FLIST_HEAD(&hash[i]);
313 nodes = malloc(nranges * sizeof(struct node));
315 for (i = j = 0; i < nranges; i++) {
318 if (dist_type == TYPE_ZIPF)
319 offset = zipf_next(&zs);
320 else if (dist_type == TYPE_PARETO)
321 offset = pareto_next(&zs);
323 offset = gauss_next(&gs);
325 n = hash_lookup(offset);
329 hash_insert(&nodes[j], offset);
335 qsort(nodes, j, sizeof(struct node), node_cmp);
338 if (output_type == OUTPUT_CSV)
339 output_csv(nodes, nnodes);
341 output_normal(nodes, nnodes, nranges);