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;
31 unsigned long long val;
35 static struct flist_head *hash;
36 static unsigned long hash_bits = 24;
37 static unsigned long hash_size = 1 << 24;
44 static const char *dist_types[] = { "None", "Zipf", "Pareto" };
46 static int dist_type = TYPE_ZIPF;
47 static unsigned long gb_size = 500;
48 static unsigned long block_size = 4096;
49 static unsigned long output_nranges = DEF_NR_OUTPUT;
50 static double percentage;
51 static double dist_val;
53 #define DEF_ZIPF_VAL 1.2
54 #define DEF_PARETO_VAL 0.3
56 static struct node *hash_lookup(unsigned long long val)
58 struct flist_head *l = &hash[hash_long(val, hash_bits)];
59 struct flist_head *entry;
62 flist_for_each(entry, l) {
63 n = flist_entry(entry, struct node, list);
71 static struct node *hash_insert(struct node *n, unsigned long long val)
73 struct flist_head *l = &hash[hash_long(val, hash_bits)];
77 flist_add_tail(&n->list, l);
81 static int parse_options(int argc, char *argv[])
83 const char *optstring = "t:g:i:o:b:p:";
84 int c, dist_val_set = 0;
86 while ((c = getopt(argc, argv, optstring)) != -1) {
89 percentage = atof(optarg);
92 block_size = strtoul(optarg, NULL, 10);
95 if (!strncmp(optarg, "zipf", 4))
96 dist_type = TYPE_ZIPF;
97 else if (!strncmp(optarg, "pareto", 6))
98 dist_type = TYPE_PARETO;
100 printf("wrong dist type: %s\n", optarg);
105 gb_size = strtoul(optarg, NULL, 10);
108 dist_val = atof(optarg);
112 output_nranges = strtoul(optarg, NULL, 10);
115 printf("bad option %c\n", c);
120 if (dist_type == TYPE_PARETO) {
121 if ((dist_val >= 1.00 || dist_val < 0.00)) {
122 printf("pareto input must be > 0.00 and < 1.00\n");
126 dist_val = DEF_PARETO_VAL;
127 } else if (dist_type == TYPE_ZIPF) {
128 if (dist_val == 1.0) {
129 printf("zipf input must be different than 1.0\n");
133 dist_val = DEF_ZIPF_VAL;
141 unsigned int nranges;
144 static int node_cmp(const void *p1, const void *p2)
146 const struct node *n1 = p1;
147 const struct node *n2 = p2;
149 return n2->hits - n1->hits;
152 int main(int argc, char *argv[])
154 unsigned long offset;
155 unsigned long i, j, k, nr_vals, cur_vals, interval, total_vals, nnodes;
156 unsigned long long nranges;
157 struct output_sum *output_sums;
160 struct zipf_state zs;
162 if (parse_options(argc, argv))
165 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);
167 nranges = gb_size * 1024 * 1024 * 1024ULL;
168 nranges /= block_size;
170 if (dist_type == TYPE_ZIPF)
171 zipf_init(&zs, nranges, dist_val, 1);
173 pareto_init(&zs, nranges, dist_val, 1);
177 while ((hash_size >>= 1) != 0)
180 hash_size = 1 << hash_bits;
182 hash = malloc(hash_size * sizeof(struct flist_head));
183 for (i = 0; i < hash_size; i++)
184 INIT_FLIST_HEAD(&hash[i]);
186 nodes = malloc(nranges * sizeof(struct node));
188 for (nr_vals = i = j = 0; i < nranges; i++) {
191 if (dist_type == TYPE_ZIPF)
192 offset = zipf_next(&zs);
194 offset = pareto_next(&zs);
196 n = hash_lookup(offset);
200 hash_insert(&nodes[j], offset);
207 qsort(nodes, j, sizeof(struct node), node_cmp);
211 interval = (nr_vals + output_nranges - 1) / output_nranges;
213 output_sums = malloc(output_nranges * sizeof(struct output_sum));
214 for (i = 0; i < output_nranges; i++) {
215 output_sums[i].output = 0.0;
216 output_sums[i].nranges = 1;
219 total_vals = i = j = cur_vals = 0;
221 for (k = 0; k < nnodes; k++) {
222 struct output_sum *os = &output_sums[j];
223 struct node *node = &nodes[k];
226 os->output = (double) (cur_vals + 1) / (double) nranges;
229 cur_vals = node->hits;
230 interval += (nr_vals + output_nranges - 1) / output_nranges;
232 cur_vals += node->hits;
233 os->nranges += node->hits;
237 total_vals += node->hits;
240 unsigned long blocks = percentage * nranges / 100;
242 if (total_vals >= blocks) {
243 double cs = i * block_size / (1024 * 1024);
255 printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
261 perc_i = 100.0 / (double) output_nranges;
264 printf("\n Rows Hits No Hits Size\n");
265 printf("--------------------------------------------------------\n");
266 for (i = 0; i < j; i++) {
267 struct output_sum *os = &output_sums[i];
268 double gb = (double) os->nranges * block_size / 1024.0;
281 printf("%s %6.2f%%\t%6.2f%%\t\t%8u\t%6.2f%c\n", i ? "|->" : "Top", perc, os->output, os->nranges, gb, p);