fio-genzipf results output are slightly wrong, it doesn't help to understand how...
[fio.git] / t / genzipf.c
index 4ba940cfefeb60f69045ca9f14f8ea20e1fc4a5b..9ba19bf9975fabf91e9e05c56a4b3751609d54d5 100644 (file)
@@ -3,10 +3,10 @@
  * what an access pattern would look like.
  *
  * For instance, the following would generate a zipf distribution
- * with theta 1.2, using 100,000 values and split the reporting into
+ * with theta 1.2, using 262144 (1 GB / 4096) values and split the reporting into
  * 20 buckets:
  *
- *     t/genzipf zipf 1.2 100000 20
+ *     ./t/fio-genzipf -t zipf -i 1.2 -g 1 -b 4096 -o 20
  *
  * Only the distribution type (zipf or pareto) and spread input need
  * to be given, if not given defaults are used.
 #include "../lib/zipf.h"
 #include "../flist.h"
 #include "../hash.h"
-#include "../rbtree.h"
 
-#define DEF_NR         1000000
-#define DEF_NR_OUTPUT  23
+#define DEF_NR_OUTPUT  20
 
 struct node {
        struct flist_head list;
-       struct rb_node rb;
        unsigned long long val;
        unsigned long hits;
 };
@@ -36,7 +33,6 @@ struct node {
 static struct flist_head *hash;
 static unsigned long hash_bits = 24;
 static unsigned long hash_size = 1 << 24;
-static struct rb_root rb;
 
 enum {
        TYPE_NONE = 0,
@@ -51,6 +47,7 @@ static unsigned long block_size = 4096;
 static unsigned long output_nranges = DEF_NR_OUTPUT;
 static double percentage;
 static double dist_val;
+static int output_csv = 0;
 
 #define DEF_ZIPF_VAL   1.2
 #define DEF_PARETO_VAL 0.3
@@ -70,72 +67,39 @@ static struct node *hash_lookup(unsigned long long val)
        return NULL;
 }
 
-static void hash_insert(unsigned long long val)
+static struct node *hash_insert(struct node *n, unsigned long long val)
 {
        struct flist_head *l = &hash[hash_long(val, hash_bits)];
-       struct node *n = malloc(sizeof(*n));
 
        n->val = val;
        n->hits = 1;
        flist_add_tail(&n->list, l);
+       return n;
 }
 
-static void rb_insert(struct node *n)
+static void usage(void)
 {
-       struct rb_node **p, *parent;
-
-       memset(&n->rb, 0, sizeof(n->rb));
-       p = &rb.rb_node;
-       parent = NULL;
-       while (*p) {
-               struct node *__n;
-
-               parent = *p;
-               __n = rb_entry(parent, struct node, rb);
-               if (n->hits > __n->hits)
-                       p = &(*p)->rb_left;
-               else
-                       p = &(*p)->rb_right;
-       }
-
-       rb_link_node(&n->rb, parent, p);
-       rb_insert_color(&n->rb, &rb);
-}
-
-static unsigned long rb_add(struct flist_head *list)
-{
-       struct flist_head *entry;
-       unsigned long ret = 0;
-       struct node *n;
-
-       flist_for_each(entry, list) {
-               n = flist_entry(entry, struct node, list);
-
-               rb_insert(n);
-               ret++;
-       }
-
-       return ret;
-}
-
-static unsigned long rb_gen(void)
-{
-       unsigned long ret = 0;
-       unsigned int i;
-
-       for (i = 0; i < hash_size; i++)
-               ret += rb_add(&hash[i]);
-
-       return ret;
+       printf("genzipf: test zipf/pareto values for fio input\n");
+       printf("\t-h\tThis help screen\n");
+       printf("\t-p\tGenerate size of data set that are hit by this percentage\n");
+       printf("\t-t\tDistribution type (zipf or pareto)\n");
+       printf("\t-i\tDistribution algorithm input (zipf theta or pareto power)\n");
+       printf("\t-b\tBlock size of a given range (in bytes)\n");
+       printf("\t-g\tSize of data set (in gigabytes)\n");
+       printf("\t-o\tNumber of output rows\n");
+       printf("\t-c\tOutput ranges in CSV format\n");
 }
 
 static int parse_options(int argc, char *argv[])
 {
-       const char *optstring = "t:g:i:o:b:p:";
+       const char *optstring = "t:g:i:o:b:p:ch";
        int c, dist_val_set = 0;
 
        while ((c = getopt(argc, argv, optstring)) != -1) {
                switch (c) {
+               case 'h':
+                       usage();
+                       return 1;
                case 'p':
                        percentage = atof(optarg);
                        break;
@@ -162,6 +126,9 @@ static int parse_options(int argc, char *argv[])
                case 'o':
                        output_nranges = strtoul(optarg, NULL, 10);
                        break;
+               case 'c':
+                       output_csv = 1;
+                       break;
                default:
                        printf("bad option %c\n", c);
                        return 1;
@@ -192,20 +159,29 @@ struct output_sum {
        unsigned int nranges;
 };
 
+static int node_cmp(const void *p1, const void *p2)
+{
+       const struct node *n1 = p1;
+       const struct node *n2 = p2;
+
+       return n2->hits - n1->hits;
+}
+
 int main(int argc, char *argv[])
 {
        unsigned long offset;
-       unsigned long i, j, nr_vals, cur_vals, interval, total_vals;
+       unsigned long i, j, k, nr_vals, cur_vals, interval_step, next_interval, total_vals, nnodes;
        unsigned long long nranges;
        struct output_sum *output_sums;
+       struct node *nodes;
        double perc, perc_i;
        struct zipf_state zs;
-       struct rb_node *n;
 
        if (parse_options(argc, argv))
                return 1;
 
-       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);
+       if( !output_csv )
+               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);
 
        nranges = gb_size * 1024 * 1024 * 1024ULL;
        nranges /= block_size;
@@ -226,7 +202,9 @@ int main(int argc, char *argv[])
        for (i = 0; i < hash_size; i++)
                INIT_FLIST_HEAD(&hash[i]);
 
-       for (nr_vals = i = 0; i < nranges; i++) {
+       nodes = malloc(nranges * sizeof(struct node));
+
+       for (i = j = 0; i < nranges; i++) {
                struct node *n;
 
                if (dist_type == TYPE_ZIPF)
@@ -237,91 +215,102 @@ int main(int argc, char *argv[])
                n = hash_lookup(offset);
                if (n)
                        n->hits++;
-               else
-                       hash_insert(offset);
+               else {
+                       hash_insert(&nodes[j], offset);
+                       j++;
+               }
 
-               nr_vals++;
        }
 
-       nr_vals = rb_gen();
+       qsort(nodes, j, sizeof(struct node), node_cmp);
+       nnodes = j;
+       nr_vals = nnodes;
 
-       interval = (nr_vals + output_nranges - 1) / output_nranges;
-
-       output_sums = malloc(output_nranges * sizeof(struct output_sum));
-       for (i = 0; i < output_nranges; i++) {
-               output_sums[i].output = 0.0;
-               output_sums[i].nranges = 1;
-       }
+       if (output_csv) {
+               printf("rank, count\n");
+               for (k = 0; k < nnodes; k++) {
+                       printf("%lu, %lu\n", k, nodes[k].hits);
+               }
+       } else {
+               unsigned long blocks = percentage * nranges / 100;
+               double hit_percent_sum = 0;
+               unsigned long long hit_sum = 0;
+               interval_step = (nr_vals - 1) / output_nranges + 1;
+               next_interval = interval_step;
+               output_sums = malloc(output_nranges * sizeof(struct output_sum));
+               for (i = 0; i < output_nranges; i++) {
+                       output_sums[i].output = 0.0;
+                       output_sums[i].nranges = 0;
+               }
 
-       total_vals = i = j = cur_vals = 0;
-       
-       n = rb_first(&rb);
-       while (n) {
-               struct node *node = rb_entry(n, struct node, rb);
-               struct output_sum *os = &output_sums[j];
+               j = total_vals = cur_vals = 0;
 
-               if (i >= interval) {
-                       os->output = (double) (cur_vals + 1) / (double) nranges;
-                       os->output *= 100.0;
-                       j++;
-                       cur_vals = node->hits;
-                       interval += (nr_vals + output_nranges - 1) / output_nranges;
-               } else {
+               for (k = 0; k < nnodes; k++) {
+                       struct output_sum *os = &output_sums[j];
+                       struct node *node = &nodes[k];
                        cur_vals += node->hits;
+                       total_vals += node->hits;
                        os->nranges += node->hits;
-               }
-
-               i++;
-               total_vals += node->hits;
-
-               if (percentage) {
-                       unsigned long blocks = percentage * nranges / 100;
-
-                       if (total_vals >= blocks) {
-                               double cs = i * block_size / (1024 * 1024);
-                               char p = 'M';
+                       if (k == (next_interval)  -1 || k == (nnodes - 1)) {
+                               os->output = (double)(cur_vals) / (double)nranges;
+                               os->output *= 100.0;
+                               cur_vals = 0;
+                               next_interval += interval_step;
+                               j++;
+                       }
 
-                               if (cs > 1024.0) {
-                                       cs /= 1024.0;
-                                       p = 'G';
-                               }
-                               if (cs > 1024.0) {
-                                       cs /= 1024.0;
-                                       p = 'T';
+                       if (percentage) {
+                               if (total_vals >= blocks) {
+                                       double cs = k * block_size / (1024 * 1024);
+                                       char p = 'M';
+
+                                       if (cs > 1024.0) {
+                                               cs /= 1024.0;
+                                               p = 'G';
+                                       }
+                                       if (cs > 1024.0) {
+                                               cs /= 1024.0;
+                                               p = 'T';
+                                       }
+
+                                       printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
+                                       percentage = 0.0;
                                }
-
-                               printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
-                               percentage = 0.0;
                        }
                }
 
-               n = rb_next(n);
-       }
+               perc_i = 100.0 / (double)output_nranges;
+               perc = 0.0;
 
-       perc_i = 100.0 / (double) output_nranges;
-       perc = 0.0;
+               printf("\n   Rows           Hits %%         No Hits         Size\n");
+               printf("--------------------------------------------------------\n");
+               for (i = 0; i < output_nranges; i++) {
+                       struct output_sum *os = &output_sums[i];
+                       double gb = (double)os->nranges * block_size / 1024.0;
+                       char p = 'K';
 
-       printf("\n   Rows           Hits           No Hits         Size\n");
-       printf("--------------------------------------------------------\n");
-       for (i = 0; i < j; i++) {
-               struct output_sum *os = &output_sums[i];
-               double gb = (double) os->nranges * block_size / 1024.0;
-               char p = 'K';
+                       if (gb > 1024.0) {
+                               p = 'M';
+                               gb /= 1024.0;
+                       }
+                       if (gb > 1024.0) {
+                               p = 'G';
+                               gb /= 1024.0;
+                       }
 
-               if (gb > 1024.0) {
-                       p = 'M';
-                       gb /= 1024.0;
+                       perc += perc_i;
+                       hit_percent_sum += os->output;
+                       hit_sum += os->nranges;
+                       printf("%s %6.2f%%\t%6.2f%%\t\t%8u\t%6.2f%c\n",
+                              i ? "|->" : "Top", perc, os->output, os->nranges,
+                              gb, p);
                }
-               if (gb > 1024.0) {
-                       p = 'G';
-                       gb /= 1024.0;
-               }
-
-               perc += perc_i;
-               printf("%s %6.2f%%\t%6.2f%%\t\t%8u\t%6.2f%c\n", i ? "|->" : "Top", perc, os->output, os->nranges, gb, p);
+               printf("--------------------------------------------------------\n");
+               printf("Total\t\t%6.2f%%\t\t%8llu\n", hit_percent_sum, hit_sum);
+               free(output_sums);
        }
 
-       free(output_sums);
        free(hash);
+       free(nodes);
        return 0;
 }