blob: 2d1b1078b94a0b6fad89ee0a1a7b30b31e839579 [file] [log] [blame]
Jens Axboe6ff38852012-11-06 16:09:14 +01001/*
2 * Generate/analyze pareto/zipf distributions to better understand
3 * what an access pattern would look like.
4 *
5 * For instance, the following would generate a zipf distribution
6 * with theta 1.2, using 100,000 values and split the reporting into
7 * 20 buckets:
8 *
9 * t/genzipf zipf 1.2 100000 20
10 *
Jens Axboef880b1f2012-11-06 19:02:53 +010011 * Only the distribution type (zipf or pareto) and spread input need
12 * to be given, if not given defaults are used.
13 *
Jens Axboe6ff38852012-11-06 16:09:14 +010014 */
15#include <stdio.h>
16#include <stdlib.h>
17#include <fcntl.h>
18#include <string.h>
Jens Axboe921d17b2012-11-09 09:05:24 +010019#include <unistd.h>
Jens Axboe6ff38852012-11-06 16:09:14 +010020
21#include "../lib/zipf.h"
Jens Axboefd6f2372012-11-07 11:43:50 +010022#include "../flist.h"
23#include "../hash.h"
24#include "../rbtree.h"
Jens Axboe6ff38852012-11-06 16:09:14 +010025
Jens Axboef880b1f2012-11-06 19:02:53 +010026#define DEF_NR 1000000
27#define DEF_NR_OUTPUT 23
28
Jens Axboefd6f2372012-11-07 11:43:50 +010029struct node {
30 struct flist_head list;
Jens Axboefd6f2372012-11-07 11:43:50 +010031 unsigned long long val;
32 unsigned long hits;
33};
Jens Axboe6ff38852012-11-06 16:09:14 +010034
Jens Axboefd6f2372012-11-07 11:43:50 +010035static struct flist_head *hash;
36static unsigned long hash_bits = 24;
37static unsigned long hash_size = 1 << 24;
Jens Axboefd6f2372012-11-07 11:43:50 +010038
Jens Axboe921d17b2012-11-09 09:05:24 +010039enum {
40 TYPE_NONE = 0,
41 TYPE_ZIPF,
42 TYPE_PARETO,
43};
44static const char *dist_types[] = { "None", "Zipf", "Pareto" };
45
46static int dist_type = TYPE_ZIPF;
47static unsigned long gb_size = 500;
Jens Axboe444256e2012-11-11 16:04:48 -070048static unsigned long block_size = 4096;
Jens Axboe921d17b2012-11-09 09:05:24 +010049static unsigned long output_nranges = DEF_NR_OUTPUT;
Jens Axboe444256e2012-11-11 16:04:48 -070050static double percentage;
Jens Axboe921d17b2012-11-09 09:05:24 +010051static double dist_val;
52
53#define DEF_ZIPF_VAL 1.2
54#define DEF_PARETO_VAL 0.3
55
Jens Axboefd6f2372012-11-07 11:43:50 +010056static struct node *hash_lookup(unsigned long long val)
57{
58 struct flist_head *l = &hash[hash_long(val, hash_bits)];
59 struct flist_head *entry;
60 struct node *n;
61
62 flist_for_each(entry, l) {
63 n = flist_entry(entry, struct node, list);
64 if (n->val == val)
65 return n;
66 }
67
68 return NULL;
69}
70
Jens Axboe24baa4c2012-11-12 20:54:12 -070071static struct node *hash_insert(struct node *n, unsigned long long val)
Jens Axboefd6f2372012-11-07 11:43:50 +010072{
73 struct flist_head *l = &hash[hash_long(val, hash_bits)];
Jens Axboefd6f2372012-11-07 11:43:50 +010074
75 n->val = val;
76 n->hits = 1;
77 flist_add_tail(&n->list, l);
Jens Axboe24baa4c2012-11-12 20:54:12 -070078 return n;
Jens Axboe6ff38852012-11-06 16:09:14 +010079}
80
Jens Axboe921d17b2012-11-09 09:05:24 +010081static int parse_options(int argc, char *argv[])
82{
Jens Axboe444256e2012-11-11 16:04:48 -070083 const char *optstring = "t:g:i:o:b:p:";
Jens Axboe921d17b2012-11-09 09:05:24 +010084 int c, dist_val_set = 0;
85
86 while ((c = getopt(argc, argv, optstring)) != -1) {
87 switch (c) {
Jens Axboe444256e2012-11-11 16:04:48 -070088 case 'p':
89 percentage = atof(optarg);
90 break;
91 case 'b':
92 block_size = strtoul(optarg, NULL, 10);
93 break;
Jens Axboe921d17b2012-11-09 09:05:24 +010094 case 't':
95 if (!strncmp(optarg, "zipf", 4))
96 dist_type = TYPE_ZIPF;
97 else if (!strncmp(optarg, "pareto", 6))
98 dist_type = TYPE_PARETO;
99 else {
100 printf("wrong dist type: %s\n", optarg);
101 return 1;
102 }
103 break;
104 case 'g':
105 gb_size = strtoul(optarg, NULL, 10);
106 break;
107 case 'i':
108 dist_val = atof(optarg);
109 dist_val_set = 1;
110 break;
Jens Axboe921d17b2012-11-09 09:05:24 +0100111 case 'o':
112 output_nranges = strtoul(optarg, NULL, 10);
113 break;
114 default:
115 printf("bad option %c\n", c);
116 return 1;
117 }
118 }
119
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");
123 return 1;
124 }
125 if (!dist_val_set)
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");
130 return 1;
131 }
132 if (!dist_val_set)
133 dist_val = DEF_ZIPF_VAL;
134 }
135
136 return 0;
137}
138
Jens Axboe444256e2012-11-11 16:04:48 -0700139struct output_sum {
140 double output;
141 unsigned int nranges;
142};
143
Jens Axboe24baa4c2012-11-12 20:54:12 -0700144static int node_cmp(const void *p1, const void *p2)
145{
146 const struct node *n1 = p1;
147 const struct node *n2 = p2;
148
149 return n2->hits - n1->hits;
150}
151
Jens Axboe6ff38852012-11-06 16:09:14 +0100152int main(int argc, char *argv[])
153{
Jens Axboefd6f2372012-11-07 11:43:50 +0100154 unsigned long offset;
Jens Axboe24baa4c2012-11-12 20:54:12 -0700155 unsigned long i, j, k, nr_vals, cur_vals, interval, total_vals, nnodes;
Jens Axboe444256e2012-11-11 16:04:48 -0700156 unsigned long long nranges;
157 struct output_sum *output_sums;
Jens Axboe24baa4c2012-11-12 20:54:12 -0700158 struct node *nodes;
Jens Axboe444256e2012-11-11 16:04:48 -0700159 double perc, perc_i;
Jens Axboe6ff38852012-11-06 16:09:14 +0100160 struct zipf_state zs;
Jens Axboe6ff38852012-11-06 16:09:14 +0100161
Jens Axboe921d17b2012-11-09 09:05:24 +0100162 if (parse_options(argc, argv))
Jens Axboe6ff38852012-11-06 16:09:14 +0100163 return 1;
Jens Axboe6ff38852012-11-06 16:09:14 +0100164
Jens Axboe444256e2012-11-11 16:04:48 -0700165 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);
166
167 nranges = gb_size * 1024 * 1024 * 1024ULL;
168 nranges /= block_size;
Jens Axboe6ff38852012-11-06 16:09:14 +0100169
Jens Axboe921d17b2012-11-09 09:05:24 +0100170 if (dist_type == TYPE_ZIPF)
171 zipf_init(&zs, nranges, dist_val, 1);
Jens Axboe6ff38852012-11-06 16:09:14 +0100172 else
Jens Axboe921d17b2012-11-09 09:05:24 +0100173 pareto_init(&zs, nranges, dist_val, 1);
Jens Axboe6ff38852012-11-06 16:09:14 +0100174
Jens Axboec71224e2012-11-07 13:35:41 +0100175 hash_bits = 0;
176 hash_size = nranges;
177 while ((hash_size >>= 1) != 0)
178 hash_bits++;
179
180 hash_size = 1 << hash_bits;
181
Jens Axboefd6f2372012-11-07 11:43:50 +0100182 hash = malloc(hash_size * sizeof(struct flist_head));
183 for (i = 0; i < hash_size; i++)
184 INIT_FLIST_HEAD(&hash[i]);
Jens Axboe6ff38852012-11-06 16:09:14 +0100185
Jens Axboe24baa4c2012-11-12 20:54:12 -0700186 nodes = malloc(nranges * sizeof(struct node));
187
188 for (nr_vals = i = j = 0; i < nranges; i++) {
Jens Axboefd6f2372012-11-07 11:43:50 +0100189 struct node *n;
190
Jens Axboe921d17b2012-11-09 09:05:24 +0100191 if (dist_type == TYPE_ZIPF)
Jens Axboefd6f2372012-11-07 11:43:50 +0100192 offset = zipf_next(&zs);
Jens Axboe6ff38852012-11-06 16:09:14 +0100193 else
Jens Axboefd6f2372012-11-07 11:43:50 +0100194 offset = pareto_next(&zs);
Jens Axboe6ff38852012-11-06 16:09:14 +0100195
Jens Axboefd6f2372012-11-07 11:43:50 +0100196 n = hash_lookup(offset);
197 if (n)
198 n->hits++;
Jens Axboe24baa4c2012-11-12 20:54:12 -0700199 else {
200 hash_insert(&nodes[j], offset);
201 j++;
202 }
Jens Axboefd6f2372012-11-07 11:43:50 +0100203
Jens Axboe6ff38852012-11-06 16:09:14 +0100204 nr_vals++;
205 }
206
Jens Axboe24baa4c2012-11-12 20:54:12 -0700207 qsort(nodes, j, sizeof(struct node), node_cmp);
208 nnodes = j;
209 nr_vals = nnodes;
Jens Axboe6ff38852012-11-06 16:09:14 +0100210
Jens Axboec71224e2012-11-07 13:35:41 +0100211 interval = (nr_vals + output_nranges - 1) / output_nranges;
Jens Axboe6ff38852012-11-06 16:09:14 +0100212
Jens Axboe444256e2012-11-11 16:04:48 -0700213 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;
217 }
Jens Axboe6ff38852012-11-06 16:09:14 +0100218
Jens Axboe444256e2012-11-11 16:04:48 -0700219 total_vals = i = j = cur_vals = 0;
Jens Axboefd6f2372012-11-07 11:43:50 +0100220
Jens Axboe24baa4c2012-11-12 20:54:12 -0700221 for (k = 0; k < nnodes; k++) {
Jens Axboe444256e2012-11-11 16:04:48 -0700222 struct output_sum *os = &output_sums[j];
Jens Axboe24baa4c2012-11-12 20:54:12 -0700223 struct node *node = &nodes[k];
Jens Axboefd6f2372012-11-07 11:43:50 +0100224
225 if (i >= interval) {
Jens Axboe444256e2012-11-11 16:04:48 -0700226 os->output = (double) (cur_vals + 1) / (double) nranges;
227 os->output *= 100.0;
Jens Axboe6ff38852012-11-06 16:09:14 +0100228 j++;
Jens Axboefd6f2372012-11-07 11:43:50 +0100229 cur_vals = node->hits;
Jens Axboec71224e2012-11-07 13:35:41 +0100230 interval += (nr_vals + output_nranges - 1) / output_nranges;
Jens Axboe444256e2012-11-11 16:04:48 -0700231 } else {
Jens Axboefd6f2372012-11-07 11:43:50 +0100232 cur_vals += node->hits;
Jens Axboe444256e2012-11-11 16:04:48 -0700233 os->nranges += node->hits;
234 }
235
236 i++;
237 total_vals += node->hits;
238
239 if (percentage) {
240 unsigned long blocks = percentage * nranges / 100;
241
242 if (total_vals >= blocks) {
243 double cs = i * block_size / (1024 * 1024);
244 char p = 'M';
245
246 if (cs > 1024.0) {
247 cs /= 1024.0;
248 p = 'G';
249 }
250 if (cs > 1024.0) {
251 cs /= 1024.0;
252 p = 'T';
253 }
254
255 printf("%.2f%% of hits satisfied in %.3f%cB of cache\n", percentage, cs, p);
256 percentage = 0.0;
257 }
258 }
Jens Axboe0779dfc2012-11-06 16:15:58 +0100259 }
Jens Axboe6ff38852012-11-06 16:09:14 +0100260
Jens Axboef880b1f2012-11-06 19:02:53 +0100261 perc_i = 100.0 / (double) output_nranges;
262 perc = 0.0;
Jens Axboe921d17b2012-11-09 09:05:24 +0100263
Jens Axboe444256e2012-11-11 16:04:48 -0700264 printf("\n Rows Hits No Hits Size\n");
265 printf("--------------------------------------------------------\n");
Jens Axboef880b1f2012-11-06 19:02:53 +0100266 for (i = 0; i < j; i++) {
Jens Axboe444256e2012-11-11 16:04:48 -0700267 struct output_sum *os = &output_sums[i];
268 double gb = (double) os->nranges * block_size / 1024.0;
269 char p = 'K';
Jens Axboe921d17b2012-11-09 09:05:24 +0100270
Jens Axboe444256e2012-11-11 16:04:48 -0700271 if (gb > 1024.0) {
Jens Axboe921d17b2012-11-09 09:05:24 +0100272 p = 'M';
Jens Axboe444256e2012-11-11 16:04:48 -0700273 gb /= 1024.0;
274 }
275 if (gb > 1024.0) {
276 p = 'G';
277 gb /= 1024.0;
Jens Axboe921d17b2012-11-09 09:05:24 +0100278 }
279
Jens Axboef880b1f2012-11-06 19:02:53 +0100280 perc += perc_i;
Jens Axboe444256e2012-11-11 16:04:48 -0700281 printf("%s %6.2f%%\t%6.2f%%\t\t%8u\t%6.2f%c\n", i ? "|->" : "Top", perc, os->output, os->nranges, gb, p);
Jens Axboef880b1f2012-11-06 19:02:53 +0100282 }
Jens Axboe6ff38852012-11-06 16:09:14 +0100283
Jens Axboe444256e2012-11-11 16:04:48 -0700284 free(output_sums);
Jens Axboefd6f2372012-11-07 11:43:50 +0100285 free(hash);
Jens Axboe6ff38852012-11-06 16:09:14 +0100286 return 0;
287}