Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 1 | /* |
| 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 Axboe | f880b1f | 2012-11-06 19:02:53 +0100 | [diff] [blame] | 11 | * Only the distribution type (zipf or pareto) and spread input need |
| 12 | * to be given, if not given defaults are used. |
| 13 | * |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 14 | */ |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <string.h> |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 19 | #include <unistd.h> |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 20 | |
| 21 | #include "../lib/zipf.h" |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 22 | #include "../flist.h" |
| 23 | #include "../hash.h" |
| 24 | #include "../rbtree.h" |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 25 | |
Jens Axboe | f880b1f | 2012-11-06 19:02:53 +0100 | [diff] [blame] | 26 | #define DEF_NR 1000000 |
| 27 | #define DEF_NR_OUTPUT 23 |
| 28 | |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 29 | struct node { |
| 30 | struct flist_head list; |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 31 | unsigned long long val; |
| 32 | unsigned long hits; |
| 33 | }; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 34 | |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 35 | static struct flist_head *hash; |
| 36 | static unsigned long hash_bits = 24; |
| 37 | static unsigned long hash_size = 1 << 24; |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 38 | |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 39 | enum { |
| 40 | TYPE_NONE = 0, |
| 41 | TYPE_ZIPF, |
| 42 | TYPE_PARETO, |
| 43 | }; |
| 44 | static const char *dist_types[] = { "None", "Zipf", "Pareto" }; |
| 45 | |
| 46 | static int dist_type = TYPE_ZIPF; |
| 47 | static unsigned long gb_size = 500; |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 48 | static unsigned long block_size = 4096; |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 49 | static unsigned long output_nranges = DEF_NR_OUTPUT; |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 50 | static double percentage; |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 51 | static double dist_val; |
| 52 | |
| 53 | #define DEF_ZIPF_VAL 1.2 |
| 54 | #define DEF_PARETO_VAL 0.3 |
| 55 | |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 56 | static 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 Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 71 | static struct node *hash_insert(struct node *n, unsigned long long val) |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 72 | { |
| 73 | struct flist_head *l = &hash[hash_long(val, hash_bits)]; |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 74 | |
| 75 | n->val = val; |
| 76 | n->hits = 1; |
| 77 | flist_add_tail(&n->list, l); |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 78 | return n; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 81 | static int parse_options(int argc, char *argv[]) |
| 82 | { |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 83 | const char *optstring = "t:g:i:o:b:p:"; |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 84 | int c, dist_val_set = 0; |
| 85 | |
| 86 | while ((c = getopt(argc, argv, optstring)) != -1) { |
| 87 | switch (c) { |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 88 | case 'p': |
| 89 | percentage = atof(optarg); |
| 90 | break; |
| 91 | case 'b': |
| 92 | block_size = strtoul(optarg, NULL, 10); |
| 93 | break; |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 94 | 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 Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 111 | 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 Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 139 | struct output_sum { |
| 140 | double output; |
| 141 | unsigned int nranges; |
| 142 | }; |
| 143 | |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 144 | static 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 Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 152 | int main(int argc, char *argv[]) |
| 153 | { |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 154 | unsigned long offset; |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 155 | unsigned long i, j, k, nr_vals, cur_vals, interval, total_vals, nnodes; |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 156 | unsigned long long nranges; |
| 157 | struct output_sum *output_sums; |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 158 | struct node *nodes; |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 159 | double perc, perc_i; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 160 | struct zipf_state zs; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 161 | |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 162 | if (parse_options(argc, argv)) |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 163 | return 1; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 164 | |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 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); |
| 166 | |
| 167 | nranges = gb_size * 1024 * 1024 * 1024ULL; |
| 168 | nranges /= block_size; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 169 | |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 170 | if (dist_type == TYPE_ZIPF) |
| 171 | zipf_init(&zs, nranges, dist_val, 1); |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 172 | else |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 173 | pareto_init(&zs, nranges, dist_val, 1); |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 174 | |
Jens Axboe | c71224e | 2012-11-07 13:35:41 +0100 | [diff] [blame] | 175 | 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 Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 182 | hash = malloc(hash_size * sizeof(struct flist_head)); |
| 183 | for (i = 0; i < hash_size; i++) |
| 184 | INIT_FLIST_HEAD(&hash[i]); |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 185 | |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 186 | nodes = malloc(nranges * sizeof(struct node)); |
| 187 | |
| 188 | for (nr_vals = i = j = 0; i < nranges; i++) { |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 189 | struct node *n; |
| 190 | |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 191 | if (dist_type == TYPE_ZIPF) |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 192 | offset = zipf_next(&zs); |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 193 | else |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 194 | offset = pareto_next(&zs); |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 195 | |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 196 | n = hash_lookup(offset); |
| 197 | if (n) |
| 198 | n->hits++; |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 199 | else { |
| 200 | hash_insert(&nodes[j], offset); |
| 201 | j++; |
| 202 | } |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 203 | |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 204 | nr_vals++; |
| 205 | } |
| 206 | |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 207 | qsort(nodes, j, sizeof(struct node), node_cmp); |
| 208 | nnodes = j; |
| 209 | nr_vals = nnodes; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 210 | |
Jens Axboe | c71224e | 2012-11-07 13:35:41 +0100 | [diff] [blame] | 211 | interval = (nr_vals + output_nranges - 1) / output_nranges; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 212 | |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 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; |
| 217 | } |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 218 | |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 219 | total_vals = i = j = cur_vals = 0; |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 220 | |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 221 | for (k = 0; k < nnodes; k++) { |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 222 | struct output_sum *os = &output_sums[j]; |
Jens Axboe | 24baa4c | 2012-11-12 20:54:12 -0700 | [diff] [blame^] | 223 | struct node *node = &nodes[k]; |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 224 | |
| 225 | if (i >= interval) { |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 226 | os->output = (double) (cur_vals + 1) / (double) nranges; |
| 227 | os->output *= 100.0; |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 228 | j++; |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 229 | cur_vals = node->hits; |
Jens Axboe | c71224e | 2012-11-07 13:35:41 +0100 | [diff] [blame] | 230 | interval += (nr_vals + output_nranges - 1) / output_nranges; |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 231 | } else { |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 232 | cur_vals += node->hits; |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 233 | 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 Axboe | 0779dfc | 2012-11-06 16:15:58 +0100 | [diff] [blame] | 259 | } |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 260 | |
Jens Axboe | f880b1f | 2012-11-06 19:02:53 +0100 | [diff] [blame] | 261 | perc_i = 100.0 / (double) output_nranges; |
| 262 | perc = 0.0; |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 263 | |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 264 | printf("\n Rows Hits No Hits Size\n"); |
| 265 | printf("--------------------------------------------------------\n"); |
Jens Axboe | f880b1f | 2012-11-06 19:02:53 +0100 | [diff] [blame] | 266 | for (i = 0; i < j; i++) { |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 267 | struct output_sum *os = &output_sums[i]; |
| 268 | double gb = (double) os->nranges * block_size / 1024.0; |
| 269 | char p = 'K'; |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 270 | |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 271 | if (gb > 1024.0) { |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 272 | p = 'M'; |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 273 | gb /= 1024.0; |
| 274 | } |
| 275 | if (gb > 1024.0) { |
| 276 | p = 'G'; |
| 277 | gb /= 1024.0; |
Jens Axboe | 921d17b | 2012-11-09 09:05:24 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Jens Axboe | f880b1f | 2012-11-06 19:02:53 +0100 | [diff] [blame] | 280 | perc += perc_i; |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 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); |
Jens Axboe | f880b1f | 2012-11-06 19:02:53 +0100 | [diff] [blame] | 282 | } |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 283 | |
Jens Axboe | 444256e | 2012-11-11 16:04:48 -0700 | [diff] [blame] | 284 | free(output_sums); |
Jens Axboe | fd6f237 | 2012-11-07 11:43:50 +0100 | [diff] [blame] | 285 | free(hash); |
Jens Axboe | 6ff3885 | 2012-11-06 16:09:14 +0100 | [diff] [blame] | 286 | return 0; |
| 287 | } |