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 262144 (1 GB / 4096) values and split the reporting into
9 * ./t/fio-genzipf -t zipf -i 1.2 -g 1 -b 4096 -o 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_OUTPUT 20
28 struct flist_head list;
29 unsigned long long val;
33 static struct flist_head *hash;
34 static unsigned long hash_bits = 24;
35 static unsigned long hash_size = 1 << 24;
42 static const char *dist_types[] = { "None", "Zipf", "Pareto" };
44 static int dist_type = TYPE_ZIPF;
45 static unsigned long gb_size = 500;
46 static unsigned long block_size = 4096;
47 static unsigned long output_nranges = DEF_NR_OUTPUT;
48 static double percentage;
49 static double dist_val;
50 static int output_csv = 0;
52 #define DEF_ZIPF_VAL 1.2
53 #define DEF_PARETO_VAL 0.3
55 static struct node *hash_lookup(unsigned long long val)
57 struct flist_head *l = &hash[hash_long(val, hash_bits)];
58 struct flist_head *entry;
61 flist_for_each(entry, l) {
62 n = flist_entry(entry, struct node, list);
70 static struct node *hash_insert(struct node *n, unsigned long long val)
72 struct flist_head *l = &hash[hash_long(val, hash_bits)];
76 flist_add_tail(&n->list, l);
80 static void usage(void)
82 printf("genzipf: test zipf/pareto values for fio input\n");
83 printf("\t-h\tThis help screen\n");
84 printf("\t-p\tGenerate size of data set that are hit by this percentage\n");
85 printf("\t-t\tDistribution type (zipf or pareto)\n");
86 printf("\t-i\tDistribution algorithm input (zipf theta or pareto power)\n");
87 printf("\t-b\tBlock size of a given range (in bytes)\n");
88 printf("\t-g\tSize of data set (in gigabytes)\n");
89 printf("\t-o\tNumber of output rows\n");
90 printf("\t-c\tOutput ranges in CSV format\n");
93 static int parse_options(int argc, char *argv[])
95 const char *optstring = "t:g:i:o:b:p:ch";
96 int c, dist_val_set = 0;
98 while ((c = getopt(argc, argv, optstring)) != -1) {
104 percentage = atof(optarg);
107 block_size = strtoul(optarg, NULL, 10);
110 if (!strncmp(optarg, "zipf", 4))
111 dist_type = TYPE_ZIPF;
112 else if (!strncmp(optarg, "pareto", 6))
113 dist_type = TYPE_PARETO;
115 printf("wrong dist type: %s\n", optarg);
120 gb_size = strtoul(optarg, NULL, 10);
123 dist_val = atof(optarg);
127 output_nranges = strtoul(optarg, NULL, 10);
133 printf("bad option %c\n", c);
138 if (dist_type == TYPE_PARETO) {
139 if ((dist_val >= 1.00 || dist_val < 0.00)) {
140 printf("pareto input must be > 0.00 and < 1.00\n");
144 dist_val = DEF_PARETO_VAL;
145 } else if (dist_type == TYPE_ZIPF) {
146 if (dist_val == 1.0) {
147 printf("zipf input must be different than 1.0\n");
151 dist_val = DEF_ZIPF_VAL;
159 unsigned int nranges;
162 static int node_cmp(const void *p1, const void *p2)
164 const struct node *n1 = p1;
165 const struct node *n2 = p2;
167 return n2->hits - n1->hits;
170 int main(int argc, char *argv[])
172 unsigned long offset;
173 unsigned long i, j, k, nr_vals, cur_vals, interval_step, next_interval, total_vals, nnodes;
174 unsigned long long nranges;
175 struct output_sum *output_sums;
178 struct zipf_state zs;
180 if (parse_options(argc, argv))
184 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);
186 nranges = gb_size * 1024 * 1024 * 1024ULL;
187 nranges /= block_size;
189 if (dist_type == TYPE_ZIPF)
190 zipf_init(&zs, nranges, dist_val, 1);
192 pareto_init(&zs, nranges, dist_val, 1);
196 while ((hash_size >>= 1) != 0)
199 hash_size = 1 << hash_bits;
201 hash = malloc(hash_size * sizeof(struct flist_head));
202 for (i = 0; i < hash_size; i++)
203 INIT_FLIST_HEAD(&hash[i]);
205 nodes = malloc(nranges * sizeof(struct node));
207 for (i = j = 0; i < nranges; i++) {
210 if (dist_type == TYPE_ZIPF)
211 offset = zipf_next(&zs);
213 offset = pareto_next(&zs);
215 n = hash_lookup(offset);
219 hash_insert(&nodes[j], offset);
225 qsort(nodes, j, sizeof(struct node), node_cmp);
230 printf("rank, count\n");
231 for (k = 0; k < nnodes; k++) {
232 printf("%lu, %lu\n", k, nodes[k].hits);
235 unsigned long blocks = percentage * nranges / 100;
236 double hit_percent_sum = 0;
237 unsigned long long hit_sum = 0;
238 interval_step = (nr_vals - 1) / output_nranges + 1;
239 next_interval = interval_step;
240 output_sums = malloc(output_nranges * sizeof(struct output_sum));
241 for (i = 0; i < output_nranges; i++) {
242 output_sums[i].output = 0.0;
243 output_sums[i].nranges = 0;
246 j = total_vals = cur_vals = 0;
248 for (k = 0; k < nnodes; k++) {
249 struct output_sum *os = &output_sums[j];
250 struct node *node = &nodes[k];
251 cur_vals += node->hits;
252 total_vals += node->hits;
253 os->nranges += node->hits;
254 if (k == (next_interval) -1 || k == (nnodes - 1)) {
255 os->output = (double)(cur_vals) / (double)nranges;
258 next_interval += interval_step;
263 if (total_vals >= blocks) {
264 double cs = k * block_size / (1024 * 1024);
276 printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
282 perc_i = 100.0 / (double)output_nranges;
285 printf("\n Rows Hits %% No Hits Size\n");
286 printf("--------------------------------------------------------\n");
287 for (i = 0; i < output_nranges; i++) {
288 struct output_sum *os = &output_sums[i];
289 double gb = (double)os->nranges * block_size / 1024.0;
302 hit_percent_sum += os->output;
303 hit_sum += os->nranges;
304 printf("%s %6.2f%%\t%6.2f%%\t\t%8u\t%6.2f%c\n",
305 i ? "|->" : "Top", perc, os->output, os->nranges,
308 printf("--------------------------------------------------------\n");
309 printf("Total\t\t%6.2f%%\t\t%8llu\n", hit_percent_sum, hit_sum);