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 block_size = 4096;
51 static unsigned long output_nranges = DEF_NR_OUTPUT;
52 static double percentage;
53 static double dist_val;
55 #define DEF_ZIPF_VAL 1.2
56 #define DEF_PARETO_VAL 0.3
58 static struct node *hash_lookup(unsigned long long val)
60 struct flist_head *l = &hash[hash_long(val, hash_bits)];
61 struct flist_head *entry;
64 flist_for_each(entry, l) {
65 n = flist_entry(entry, struct node, list);
73 static void hash_insert(unsigned long long val)
75 struct flist_head *l = &hash[hash_long(val, hash_bits)];
76 struct node *n = malloc(sizeof(*n));
80 flist_add_tail(&n->list, l);
83 static void rb_insert(struct node *n)
85 struct rb_node **p, *parent;
87 memset(&n->rb, 0, sizeof(n->rb));
94 __n = rb_entry(parent, struct node, rb);
95 if (n->hits > __n->hits)
101 rb_link_node(&n->rb, parent, p);
102 rb_insert_color(&n->rb, &rb);
105 static unsigned long rb_add(struct flist_head *list)
107 struct flist_head *entry;
108 unsigned long ret = 0;
111 flist_for_each(entry, list) {
112 n = flist_entry(entry, struct node, list);
121 static unsigned long rb_gen(void)
123 unsigned long ret = 0;
126 for (i = 0; i < hash_size; i++)
127 ret += rb_add(&hash[i]);
132 static int parse_options(int argc, char *argv[])
134 const char *optstring = "t:g:i:o:b:p:";
135 int c, dist_val_set = 0;
137 while ((c = getopt(argc, argv, optstring)) != -1) {
140 percentage = atof(optarg);
143 block_size = strtoul(optarg, NULL, 10);
146 if (!strncmp(optarg, "zipf", 4))
147 dist_type = TYPE_ZIPF;
148 else if (!strncmp(optarg, "pareto", 6))
149 dist_type = TYPE_PARETO;
151 printf("wrong dist type: %s\n", optarg);
156 gb_size = strtoul(optarg, NULL, 10);
159 dist_val = atof(optarg);
163 output_nranges = strtoul(optarg, NULL, 10);
166 printf("bad option %c\n", c);
171 if (dist_type == TYPE_PARETO) {
172 if ((dist_val >= 1.00 || dist_val < 0.00)) {
173 printf("pareto input must be > 0.00 and < 1.00\n");
177 dist_val = DEF_PARETO_VAL;
178 } else if (dist_type == TYPE_ZIPF) {
179 if (dist_val == 1.0) {
180 printf("zipf input must be different than 1.0\n");
184 dist_val = DEF_ZIPF_VAL;
192 unsigned int nranges;
195 int main(int argc, char *argv[])
197 unsigned long offset;
198 unsigned long i, j, nr_vals, cur_vals, interval, total_vals;
199 unsigned long long nranges;
200 struct output_sum *output_sums;
202 struct zipf_state zs;
205 if (parse_options(argc, argv))
208 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);
210 nranges = gb_size * 1024 * 1024 * 1024ULL;
211 nranges /= block_size;
213 if (dist_type == TYPE_ZIPF)
214 zipf_init(&zs, nranges, dist_val, 1);
216 pareto_init(&zs, nranges, dist_val, 1);
220 while ((hash_size >>= 1) != 0)
223 hash_size = 1 << hash_bits;
225 hash = malloc(hash_size * sizeof(struct flist_head));
226 for (i = 0; i < hash_size; i++)
227 INIT_FLIST_HEAD(&hash[i]);
229 for (nr_vals = i = 0; i < nranges; i++) {
232 if (dist_type == TYPE_ZIPF)
233 offset = zipf_next(&zs);
235 offset = pareto_next(&zs);
237 n = hash_lookup(offset);
248 interval = (nr_vals + output_nranges - 1) / output_nranges;
250 output_sums = malloc(output_nranges * sizeof(struct output_sum));
251 for (i = 0; i < output_nranges; i++) {
252 output_sums[i].output = 0.0;
253 output_sums[i].nranges = 1;
256 total_vals = i = j = cur_vals = 0;
260 struct node *node = rb_entry(n, struct node, rb);
261 struct output_sum *os = &output_sums[j];
264 os->output = (double) (cur_vals + 1) / (double) nranges;
267 cur_vals = node->hits;
268 interval += (nr_vals + output_nranges - 1) / output_nranges;
270 cur_vals += node->hits;
271 os->nranges += node->hits;
275 total_vals += node->hits;
278 unsigned long blocks = percentage * nranges / 100;
280 if (total_vals >= blocks) {
281 double cs = i * block_size / (1024 * 1024);
293 printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
301 perc_i = 100.0 / (double) output_nranges;
304 printf("\n Rows Hits No Hits Size\n");
305 printf("--------------------------------------------------------\n");
306 for (i = 0; i < j; i++) {
307 struct output_sum *os = &output_sums[i];
308 double gb = (double) os->nranges * block_size / 1024.0;
321 printf("%s %6.2f%%\t%6.2f%%\t\t%8u\t%6.2f%c\n", i ? "|->" : "Top", perc, os->output, os->nranges, gb, p);