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
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"
24 #include "../rbtree.h"
26 #define DEF_NR 1000000
27 #define DEF_NR_OUTPUT 23
30 struct flist_head list;
32 unsigned long long val;
36 static struct flist_head *hash;
37 static unsigned long hash_bits = 24;
38 static unsigned long hash_size = 1 << 24;
39 static struct rb_root rb;
46 static const char *dist_types[] = { "None", "Zipf", "Pareto" };
48 static int dist_type = TYPE_ZIPF;
49 static unsigned long gb_size = 500;
50 static unsigned long nranges = DEF_NR;
51 static unsigned long output_nranges = DEF_NR_OUTPUT;
52 static double dist_val;
54 #define DEF_ZIPF_VAL 1.2
55 #define DEF_PARETO_VAL 0.3
57 static struct node *hash_lookup(unsigned long long val)
59 struct flist_head *l = &hash[hash_long(val, hash_bits)];
60 struct flist_head *entry;
63 flist_for_each(entry, l) {
64 n = flist_entry(entry, struct node, list);
72 static void hash_insert(unsigned long long val)
74 struct flist_head *l = &hash[hash_long(val, hash_bits)];
75 struct node *n = malloc(sizeof(*n));
79 flist_add_tail(&n->list, l);
82 static void rb_insert(struct node *n)
84 struct rb_node **p, *parent;
86 memset(&n->rb, 0, sizeof(n->rb));
93 __n = rb_entry(parent, struct node, rb);
94 if (n->hits > __n->hits)
100 rb_link_node(&n->rb, parent, p);
101 rb_insert_color(&n->rb, &rb);
104 static unsigned long rb_add(struct flist_head *list)
106 struct flist_head *entry;
107 unsigned long ret = 0;
110 flist_for_each(entry, list) {
111 n = flist_entry(entry, struct node, list);
120 static unsigned long rb_gen(void)
122 unsigned long ret = 0;
125 for (i = 0; i < hash_size; i++)
126 ret += rb_add(&hash[i]);
131 static int parse_options(int argc, char *argv[])
133 const char *optstring = "t:g:i:r:o:";
134 int c, dist_val_set = 0;
136 while ((c = getopt(argc, argv, optstring)) != -1) {
139 if (!strncmp(optarg, "zipf", 4))
140 dist_type = TYPE_ZIPF;
141 else if (!strncmp(optarg, "pareto", 6))
142 dist_type = TYPE_PARETO;
144 printf("wrong dist type: %s\n", optarg);
149 gb_size = strtoul(optarg, NULL, 10);
152 dist_val = atof(optarg);
156 nranges = strtoul(optarg, NULL, 10);
159 output_nranges = strtoul(optarg, NULL, 10);
162 printf("bad option %c\n", c);
167 if (dist_type == TYPE_PARETO) {
168 if ((dist_val >= 1.00 || dist_val < 0.00)) {
169 printf("pareto input must be > 0.00 and < 1.00\n");
173 dist_val = DEF_PARETO_VAL;
174 } else if (dist_type == TYPE_ZIPF) {
175 if (dist_val == 1.0) {
176 printf("zipf input must be different than 1.0\n");
180 dist_val = DEF_ZIPF_VAL;
186 int main(int argc, char *argv[])
188 unsigned long offset;
189 unsigned long i, j, nr_vals, cur_vals, interval;
190 double *output, perc, perc_i;
191 struct zipf_state zs;
194 if (parse_options(argc, argv))
197 printf("Generating %s distribution with %f input and %lu ranges.\n", dist_types[dist_type], dist_val, nranges);
198 printf("Using device gb=%lu\n\n", gb_size);
200 if (dist_type == TYPE_ZIPF)
201 zipf_init(&zs, nranges, dist_val, 1);
203 pareto_init(&zs, nranges, dist_val, 1);
207 while ((hash_size >>= 1) != 0)
210 hash_size = 1 << hash_bits;
212 hash = malloc(hash_size * sizeof(struct flist_head));
213 for (i = 0; i < hash_size; i++)
214 INIT_FLIST_HEAD(&hash[i]);
216 for (nr_vals = 0, i = 0; i < nranges; i++) {
219 if (dist_type == TYPE_ZIPF)
220 offset = zipf_next(&zs);
222 offset = pareto_next(&zs);
224 n = hash_lookup(offset);
235 interval = (nr_vals + output_nranges - 1) / output_nranges;
237 output = malloc(output_nranges * sizeof(double));
239 i = j = cur_vals = 0;
243 struct node *node = rb_entry(n, struct node, rb);
246 output[j] = (double) (cur_vals + 1) / (double) nranges;
249 cur_vals = node->hits;
250 interval += (nr_vals + output_nranges - 1) / output_nranges;
252 cur_vals += node->hits;
258 perc_i = 100.0 / (double) output_nranges;
261 printf(" Rows Hits Size\n");
262 printf("-------------------------------------------\n");
263 for (i = 0; i < j; i++) {
264 double gb = (double) gb_size * perc_i / 100.0;
273 printf("%s %6.2f%%\t%6.2f%%\t\t%6.2f%c\n", i ? "|->" : "Top", perc, output[i], gb, p);