btrfs-progs: read_tree_block() and read_node_slot() cleanup.
[platform/upstream/btrfs-progs.git] / btrfs-calc-size.c
1 /*
2  * Copyright (C) 2011 Red Hat.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <zlib.h>
28 #include "kerncompat.h"
29 #include "ctree.h"
30 #include "disk-io.h"
31 #include "print-tree.h"
32 #include "transaction.h"
33 #include "list.h"
34 #include "volumes.h"
35 #include "utils.h"
36
37 static int verbose = 0;
38 static int no_pretty = 0;
39
40 struct seek {
41         u64 distance;
42         u64 count;
43         struct rb_node n;
44 };
45
46 struct root_stats {
47         u64 total_nodes;
48         u64 total_leaves;
49         u64 total_bytes;
50         u64 total_inline;
51         u64 total_seeks;
52         u64 forward_seeks;
53         u64 backward_seeks;
54         u64 total_seek_len;
55         u64 max_seek_len;
56         u64 total_clusters;
57         u64 total_cluster_size;
58         u64 min_cluster_size;
59         u64 max_cluster_size;
60         u64 lowest_bytenr;
61         u64 highest_bytenr;
62         struct rb_root seek_root;
63         int total_levels;
64 };
65
66 struct fs_root {
67         struct btrfs_key key;
68         struct btrfs_key *snaps;
69 };
70
71 static int add_seek(struct rb_root *root, u64 dist)
72 {
73         struct rb_node **p = &root->rb_node;
74         struct rb_node *parent = NULL;
75         struct seek *seek = NULL;
76
77         while (*p) {
78                 parent = *p;
79                 seek = rb_entry(parent, struct seek, n);
80
81                 if (dist < seek->distance) {
82                         p = &(*p)->rb_left;
83                 } else if (dist > seek->distance) {
84                         p = &(*p)->rb_right;
85                 } else {
86                         seek->count++;
87                         return 0;
88                 }
89         }
90
91         seek = malloc(sizeof(struct seek));
92         if (!seek)
93                 return -ENOMEM;
94         seek->distance = dist;
95         seek->count = 1;
96         rb_link_node(&seek->n, parent, p);
97         rb_insert_color(&seek->n, root);
98         return 0;
99 }
100
101 static int walk_leaf(struct btrfs_root *root, struct btrfs_path *path,
102                      struct root_stats *stat, int find_inline)
103 {
104         struct extent_buffer *b = path->nodes[0];
105         struct btrfs_file_extent_item *fi;
106         struct btrfs_key found_key;
107         int i;
108
109         stat->total_bytes += root->leafsize;
110         stat->total_leaves++;
111
112         if (!find_inline)
113                 return 0;
114
115         for (i = 0; i < btrfs_header_nritems(b); i++) {
116                 btrfs_item_key_to_cpu(b, &found_key, i);
117                 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
118                         continue;
119
120                 fi = btrfs_item_ptr(b, i, struct btrfs_file_extent_item);
121                 if (btrfs_file_extent_type(b, fi) == BTRFS_FILE_EXTENT_INLINE)
122                         stat->total_inline +=
123                                 btrfs_file_extent_inline_item_len(b,
124                                                         btrfs_item_nr(i));
125         }
126
127         return 0;
128 }
129
130 static u64 calc_distance(u64 block1, u64 block2)
131 {
132         if (block1 < block2)
133                 return block2 - block1;
134         return block1 - block2;
135 }
136
137 static int walk_nodes(struct btrfs_root *root, struct btrfs_path *path,
138                       struct root_stats *stat, int level, int find_inline)
139 {
140         struct extent_buffer *b = path->nodes[level];
141         u64 last_block;
142         u64 cluster_size = root->leafsize;
143         int i;
144         int ret = 0;
145
146         stat->total_bytes += root->nodesize;
147         stat->total_nodes++;
148
149         last_block = btrfs_header_bytenr(b);
150         for (i = 0; i < btrfs_header_nritems(b); i++) {
151                 struct extent_buffer *tmp = NULL;
152                 u64 cur_blocknr = btrfs_node_blockptr(b, i);
153
154                 path->slots[level] = i;
155                 if ((level - 1) > 0 || find_inline) {
156                         tmp = read_tree_block(root, cur_blocknr,
157                                               btrfs_level_size(root, level - 1),
158                                               btrfs_node_ptr_generation(b, i));
159                         if (!extent_buffer_uptodate(tmp)) {
160                                 fprintf(stderr, "Failed to read blocknr %Lu\n",
161                                         btrfs_node_blockptr(b, i));
162                                 continue;
163                         }
164                         path->nodes[level - 1] = tmp;
165                 }
166                 if (level - 1)
167                         ret = walk_nodes(root, path, stat, level - 1,
168                                          find_inline);
169                 else
170                         ret = walk_leaf(root, path, stat, find_inline);
171                 if (last_block + root->leafsize != cur_blocknr) {
172                         u64 distance = calc_distance(last_block +
173                                                      root->leafsize,
174                                                      cur_blocknr);
175                         stat->total_seeks++;
176                         stat->total_seek_len += distance;
177                         if (stat->max_seek_len < distance)
178                                 stat->max_seek_len = distance;
179                         if (add_seek(&stat->seek_root, distance)) {
180                                 fprintf(stderr, "Error adding new seek\n");
181                                 ret = -ENOMEM;
182                                 break;
183                         }
184
185                         if (last_block < cur_blocknr)
186                                 stat->forward_seeks++;
187                         else
188                                 stat->backward_seeks++;
189                         if (cluster_size != root->leafsize) {
190                                 stat->total_cluster_size += cluster_size;
191                                 stat->total_clusters++;
192                                 if (cluster_size < stat->min_cluster_size)
193                                         stat->min_cluster_size = cluster_size;
194                                 if (cluster_size > stat->max_cluster_size)
195                                         stat->max_cluster_size = cluster_size;
196                         }
197                         cluster_size = root->leafsize;
198                 } else {
199                         cluster_size += root->leafsize;
200                 }
201                 last_block = cur_blocknr;
202                 if (cur_blocknr < stat->lowest_bytenr)
203                         stat->lowest_bytenr = cur_blocknr;
204                 if (cur_blocknr > stat->highest_bytenr)
205                         stat->highest_bytenr = cur_blocknr;
206                 free_extent_buffer(tmp);
207                 if (ret) {
208                         fprintf(stderr, "Error walking down path\n");
209                         break;
210                 }
211         }
212
213         return ret;
214 }
215
216 static void print_seek_histogram(struct root_stats *stat)
217 {
218         struct rb_node *n = rb_first(&stat->seek_root);
219         struct seek *seek;
220         u64 tick_interval;
221         u64 group_start;
222         u64 group_count = 0;
223         u64 group_end;
224         u64 i;
225         u64 max_seek = stat->max_seek_len;
226         int digits = 1;
227
228         if (stat->total_seeks < 20)
229                 return;
230
231         while ((max_seek /= 10))
232                 digits++;
233
234         /* Make a tick count as 5% of the total seeks */
235         tick_interval = stat->total_seeks / 20;
236         printf("\tSeek histogram\n");
237         for (; n; n = rb_next(n)) {
238                 u64 ticks, gticks = 0;
239
240                 seek = rb_entry(n, struct seek, n);
241                 ticks = seek->count / tick_interval;
242                 if (group_count)
243                         gticks = group_count / tick_interval;
244
245                 if (ticks <= 2 && gticks <= 2) {
246                         if (group_count == 0)
247                                 group_start = seek->distance;
248                         group_end = seek->distance;
249                         group_count += seek->count;
250                         continue;
251                 }
252
253                 if (group_count) {
254
255                         gticks = group_count / tick_interval;
256                         printf("\t\t%*Lu - %*Lu: %*Lu ", digits, group_start,
257                                digits, group_end, digits, group_count);
258                         if (gticks) {
259                                 for (i = 0; i < gticks; i++)
260                                         printf("#");
261                                 printf("\n");
262                         } else {
263                                 printf("|\n");
264                         }
265                         group_count = 0;
266                 }
267
268                 if (ticks <= 2)
269                         continue;
270
271                 printf("\t\t%*Lu - %*Lu: %*Lu ", digits, seek->distance,
272                        digits, seek->distance, digits, seek->count);
273                 for (i = 0; i < ticks; i++)
274                         printf("#");
275                 printf("\n");
276         }
277         if (group_count) {
278                 u64 gticks;
279
280                 gticks = group_count / tick_interval;
281                 printf("\t\t%*Lu - %*Lu: %*Lu ", digits, group_start,
282                        digits, group_end, digits, group_count);
283                 if (gticks) {
284                         for (i = 0; i < gticks; i++)
285                                 printf("#");
286                         printf("\n");
287                 } else {
288                         printf("|\n");
289                 }
290                 group_count = 0;
291         }
292 }
293
294 static void timeval_subtract(struct timeval *result,struct timeval *x,
295                              struct timeval *y)
296 {
297         if (x->tv_usec < y->tv_usec) {
298                 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
299                 y->tv_usec -= 1000000 * nsec;
300                 y->tv_sec += nsec;
301         }
302
303         if (x->tv_usec - y->tv_usec > 1000000) {
304                 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
305                 y->tv_usec += 1000000 * nsec;
306                 y->tv_sec -= nsec;
307         }
308
309         result->tv_sec = x->tv_sec - y->tv_sec;
310         result->tv_usec = x->tv_usec - y->tv_usec;
311 }
312
313 static int calc_root_size(struct btrfs_root *tree_root, struct btrfs_key *key,
314                           int find_inline)
315 {
316         struct btrfs_root *root;
317         struct btrfs_path *path;
318         struct rb_node *n;
319         struct timeval start, end, diff = {0};
320         struct root_stats stat;
321         int level;
322         int ret = 0;
323         int size_fail = 0;
324
325         root = btrfs_read_fs_root(tree_root->fs_info, key);
326         if (IS_ERR(root)) {
327                 fprintf(stderr, "Failed to read root %Lu\n", key->objectid);
328                 return 1;
329         }
330
331         path = btrfs_alloc_path();
332         if (!path) {
333                 fprintf(stderr, "Could not allocate path\n");
334                 return 1;
335         }
336
337         memset(&stat, 0, sizeof(stat));
338         level = btrfs_header_level(root->node);
339         stat.lowest_bytenr = btrfs_header_bytenr(root->node);
340         stat.highest_bytenr = stat.lowest_bytenr;
341         stat.min_cluster_size = (u64)-1;
342         stat.max_cluster_size = root->leafsize;
343         path->nodes[level] = root->node;
344         if (gettimeofday(&start, NULL)) {
345                 fprintf(stderr, "Error getting time: %d\n", errno);
346                 goto out;
347         }
348         if (!level) {
349                 ret = walk_leaf(root, path, &stat, find_inline);
350                 if (ret)
351                         goto out;
352                 goto out_print;
353         }
354
355         ret = walk_nodes(root, path, &stat, level, find_inline);
356         if (ret)
357                 goto out;
358         if (gettimeofday(&end, NULL)) {
359                 fprintf(stderr, "Error getting time: %d\n", errno);
360                 goto out;
361         }
362         timeval_subtract(&diff, &end, &start);
363 out_print:
364         if (stat.min_cluster_size == (u64)-1) {
365                 stat.min_cluster_size = 0;
366                 stat.total_clusters = 1;
367         }
368
369         if (no_pretty || size_fail) {
370                 printf("\tTotal size: %Lu\n", stat.total_bytes);
371                 printf("\t\tInline data: %Lu\n", stat.total_inline);
372                 printf("\tTotal seeks: %Lu\n", stat.total_seeks);
373                 printf("\t\tForward seeks: %Lu\n", stat.forward_seeks);
374                 printf("\t\tBackward seeks: %Lu\n", stat.backward_seeks);
375                 printf("\t\tAvg seek len: %Lu\n", stat.total_seek_len /
376                        stat.total_seeks);
377                 print_seek_histogram(&stat);
378                 printf("\tTotal clusters: %Lu\n", stat.total_clusters);
379                 printf("\t\tAvg cluster size: %Lu\n", stat.total_cluster_size /
380                        stat.total_clusters);
381                 printf("\t\tMin cluster size: %Lu\n", stat.min_cluster_size);
382                 printf("\t\tMax cluster size: %Lu\n", stat.max_cluster_size);
383                 printf("\tTotal disk spread: %Lu\n", stat.highest_bytenr -
384                        stat.lowest_bytenr);
385                 printf("\tTotal read time: %d s %d us\n", (int)diff.tv_sec,
386                        (int)diff.tv_usec);
387                 printf("\tLevels: %d\n", level + 1);
388         } else {
389                 printf("\tTotal size: %s\n", pretty_size(stat.total_bytes));
390                 printf("\t\tInline data: %s\n", pretty_size(stat.total_inline));
391                 printf("\tTotal seeks: %Lu\n", stat.total_seeks);
392                 printf("\t\tForward seeks: %Lu\n", stat.forward_seeks);
393                 printf("\t\tBackward seeks: %Lu\n", stat.backward_seeks);
394                 printf("\t\tAvg seek len: %s\n", stat.total_seeks ?
395                         pretty_size(stat.total_seek_len / stat.total_seeks) :
396                         pretty_size(0));
397                 print_seek_histogram(&stat);
398                 printf("\tTotal clusters: %Lu\n", stat.total_clusters);
399                 printf("\t\tAvg cluster size: %s\n",
400                                 pretty_size((stat.total_cluster_size /
401                                                 stat.total_clusters)));
402                 printf("\t\tMin cluster size: %s\n",
403                                 pretty_size(stat.min_cluster_size));
404                 printf("\t\tMax cluster size: %s\n",
405                                 pretty_size(stat.max_cluster_size));
406                 printf("\tTotal disk spread: %s\n",
407                                 pretty_size(stat.highest_bytenr -
408                                         stat.lowest_bytenr));
409                 printf("\tTotal read time: %d s %d us\n", (int)diff.tv_sec,
410                        (int)diff.tv_usec);
411                 printf("\tLevels: %d\n", level + 1);
412         }
413 out:
414         while ((n = rb_first(&stat.seek_root)) != NULL) {
415                 struct seek *seek = rb_entry(n, struct seek, n);
416                 rb_erase(n, &stat.seek_root);
417                 free(seek);
418         }
419
420         btrfs_free_path(path);
421         return ret;
422 }
423
424 static void usage()
425 {
426         fprintf(stderr, "Usage: calc-size [-v] [-b] <device>\n");
427 }
428
429 int main(int argc, char **argv)
430 {
431         struct btrfs_key key;
432         struct fs_root *roots;
433         struct btrfs_root *root;
434         size_t fs_roots_size = sizeof(struct fs_root);
435         int opt;
436         int ret = 0;
437
438         while ((opt = getopt(argc, argv, "vb")) != -1) {
439                 switch (opt) {
440                         case 'v':
441                                 verbose++;
442                                 break;
443                         case 'b':
444                                 no_pretty = 1;
445                                 break;
446                         default:
447                                 usage();
448                                 exit(1);
449                 }
450         }
451
452         set_argv0(argv);
453         argc = argc - optind;
454         if (check_argc_min(argc, 1)) {
455                 usage();
456                 exit(1);
457         }
458
459         /*
460         if ((ret = check_mounted(argv[optind])) < 0) {
461                 fprintf(stderr, "Could not check mount status: %d\n", ret);
462                 if (ret == -EACCES)
463                         fprintf(stderr, "Maybe you need to run as root?\n");
464                 return ret;
465         } else if (ret) {
466                 fprintf(stderr, "%s is currently mounted.  Aborting.\n",
467                         argv[optind]);
468                 return -EBUSY;
469         }
470         */
471
472         root = open_ctree(argv[optind], 0, 0);
473         if (!root) {
474                 fprintf(stderr, "Couldn't open ctree\n");
475                 exit(1);
476         }
477
478         roots = malloc(fs_roots_size);
479         if (!roots) {
480                 fprintf(stderr, "No memory\n");
481                 goto out;
482         }
483
484         printf("Calculating size of root tree\n");
485         key.objectid = BTRFS_ROOT_TREE_OBJECTID;
486         ret = calc_root_size(root, &key, 0);
487         if (ret)
488                 goto out;
489
490         printf("Calculating size of extent tree\n");
491         key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
492         ret = calc_root_size(root, &key, 0);
493         if (ret)
494                 goto out;
495
496         printf("Calculating size of csum tree\n");
497         key.objectid = BTRFS_CSUM_TREE_OBJECTID;
498         ret = calc_root_size(root, &key, 0);
499         if (ret)
500                 goto out;
501
502         roots[0].key.objectid = BTRFS_FS_TREE_OBJECTID;
503         roots[0].key.offset = (u64)-1;
504         printf("Calculatin' size of fs tree\n");
505         ret = calc_root_size(root, &roots[0].key, 1);
506         if (ret)
507                 goto out;
508 out:
509         close_ctree(root);
510         free(roots);
511         return ret;
512 }