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 100,000 values and split the reporting into
9 * t/genzipf zipf 1.2 100000 20
17 #include "../lib/zipf.h"
19 static int val_cmp(const void *p1, const void *p2)
21 const unsigned long *v1 = p1;
22 const unsigned long *v2 = p2;
27 int main(int argc, char *argv[])
29 unsigned long nranges, output_nranges;
31 unsigned long i, j, nr_vals, cur_vals, max_val, interval, total;
38 printf("%s: {zipf,pareto} val values [output ranges]\n", argv[0]);
42 if (!strcmp(argv[1], "zipf"))
44 else if (!strcmp(argv[1], "pareto"))
47 printf("Bad distribution type <%s>\n", argv[1]);
52 nranges = strtoul(argv[3], NULL, 10);
54 output_nranges = strtoul(argv[4], NULL, 10);
56 output_nranges = nranges;
58 printf("Generating %s distribution with %f input and %lu ranges\n", use_zipf ? "zipf" : "pareto", val, nranges);
62 zipf_init(&zs, nranges, val);
64 pareto_init(&zs, nranges, val);
66 vals = malloc(nranges * sizeof(unsigned long));
68 total = max_val = nr_vals = 0;
69 for (i = 0; i < nranges; i++) {
71 vals[nr_vals] = zipf_next(&zs);
73 vals[nr_vals] = pareto_next(&zs);
75 if (vals[nr_vals] > max_val)
76 max_val = vals[nr_vals];
80 qsort(vals, nr_vals, sizeof(unsigned long), val_cmp);
82 interval = (max_val + output_nranges - 1) / output_nranges;
84 output = malloc(output_nranges * sizeof(double));
86 for (i = j = 0, cur_vals = 1; i < nr_vals; i++) {
87 if (vals[i] > interval) {
88 output[j] = (double) cur_vals / (double) nr_vals;
93 interval += (max_val + output_nranges - 1) / output_nranges;
99 output[j] = (double) cur_vals / (double) nr_vals;
104 for (i = 0; i < j; i++)
105 printf("%.2f%%\n", output[i]);