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"
25 #define DEF_NR 1000000
26 #define DEF_NR_OUTPUT 23
29 struct flist_head list;
30 unsigned long long val;
34 static struct flist_head *hash;
35 static unsigned long hash_bits = 24;
36 static unsigned long hash_size = 1 << 24;
43 static const char *dist_types[] = { "None", "Zipf", "Pareto" };
45 static int dist_type = TYPE_ZIPF;
46 static unsigned long gb_size = 500;
47 static unsigned long block_size = 4096;
48 static unsigned long output_nranges = DEF_NR_OUTPUT;
49 static double percentage;
50 static double dist_val;
51 static int output_csv = 0;
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 void usage(void)
83 printf("genzipf: test zipf/pareto values for fio input\n");
84 printf("\t-h\tThis help screen\n");
85 printf("\t-p\tGenerate size of data set that are hit by this percentage\n");
86 printf("\t-t\tDistribution type (zipf or pareto)\n");
87 printf("\t-i\tDistribution algorithm input (zipf theta or pareto power)\n");
88 printf("\t-b\tBlock size of a given range (in bytes)\n");
89 printf("\t-g\tSize of data set (in gigabytes)\n");
90 printf("\t-o\tNumber of output columns\n");
91 printf("\t-c\tOutput ranges in CSV format\n");
94 static int parse_options(int argc, char *argv[])
96 const char *optstring = "t:g:i:o:b:p:ch";
97 int c, dist_val_set = 0;
99 while ((c = getopt(argc, argv, optstring)) != -1) {
105 percentage = atof(optarg);
108 block_size = strtoul(optarg, NULL, 10);
111 if (!strncmp(optarg, "zipf", 4))
112 dist_type = TYPE_ZIPF;
113 else if (!strncmp(optarg, "pareto", 6))
114 dist_type = TYPE_PARETO;
116 printf("wrong dist type: %s\n", optarg);
121 gb_size = strtoul(optarg, NULL, 10);
124 dist_val = atof(optarg);
128 output_nranges = strtoul(optarg, NULL, 10);
134 printf("bad option %c\n", c);
139 if (dist_type == TYPE_PARETO) {
140 if ((dist_val >= 1.00 || dist_val < 0.00)) {
141 printf("pareto input must be > 0.00 and < 1.00\n");
145 dist_val = DEF_PARETO_VAL;
146 } else if (dist_type == TYPE_ZIPF) {
147 if (dist_val == 1.0) {
148 printf("zipf input must be different than 1.0\n");
152 dist_val = DEF_ZIPF_VAL;
160 unsigned int nranges;
163 static int node_cmp(const void *p1, const void *p2)
165 const struct node *n1 = p1;
166 const struct node *n2 = p2;
168 return n2->hits - n1->hits;
171 int main(int argc, char *argv[])
173 unsigned long offset;
174 unsigned long i, j, k, nr_vals, cur_vals, interval, total_vals, nnodes;
175 unsigned long long nranges;
176 struct output_sum *output_sums;
179 struct zipf_state zs;
181 if (parse_options(argc, argv))
185 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);
187 nranges = gb_size * 1024 * 1024 * 1024ULL;
188 nranges /= block_size;
190 if (dist_type == TYPE_ZIPF)
191 zipf_init(&zs, nranges, dist_val, 1);
193 pareto_init(&zs, nranges, dist_val, 1);
197 while ((hash_size >>= 1) != 0)
200 hash_size = 1 << hash_bits;
202 hash = malloc(hash_size * sizeof(struct flist_head));
203 for (i = 0; i < hash_size; i++)
204 INIT_FLIST_HEAD(&hash[i]);
206 nodes = malloc(nranges * sizeof(struct node));
208 for (nr_vals = i = j = 0; i < nranges; i++) {
211 if (dist_type == TYPE_ZIPF)
212 offset = zipf_next(&zs);
214 offset = pareto_next(&zs);
216 n = hash_lookup(offset);
220 hash_insert(&nodes[j], offset);
227 qsort(nodes, j, sizeof(struct node), node_cmp);
232 printf("rank, count\n");
233 for (k = 0; k < nnodes; k++)
234 printf("%lu, %lu\n", k, nodes[k].hits);
236 interval = (nr_vals + output_nranges - 1) / output_nranges;
238 output_sums = malloc(output_nranges * sizeof(struct output_sum));
239 for (i = 0; i < output_nranges; i++) {
240 output_sums[i].output = 0.0;
241 output_sums[i].nranges = 1;
244 total_vals = i = j = cur_vals = 0;
246 for (k = 0; k < nnodes; k++) {
247 struct output_sum *os = &output_sums[j];
248 struct node *node = &nodes[k];
252 (double)(cur_vals + 1) / (double)nranges;
255 cur_vals = node->hits;
257 (nr_vals + output_nranges -
260 cur_vals += node->hits;
261 os->nranges += node->hits;
265 total_vals += node->hits;
268 unsigned long blocks =
269 percentage * nranges / 100;
271 if (total_vals >= blocks) {
273 i * block_size / (1024 * 1024);
285 printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
291 perc_i = 100.0 / (double)output_nranges;
294 printf("\n Rows Hits No Hits Size\n");
295 printf("--------------------------------------------------------\n");
296 for (i = 0; i < j; i++) {
297 struct output_sum *os = &output_sums[i];
298 double gb = (double)os->nranges * block_size / 1024.0;
311 printf("%s %6.2f%%\t%6.2f%%\t\t%8u\t%6.2f%c\n",
312 i ? "|->" : "Top", perc, os->output, os->nranges,