btrfs-progs: latest_devid is not always the probed devid
[btrfs-progs-unstable/devel.git] / btrfs-calc-size.c
bloba832ee0be3220de715fa598210ab0b5abe9afc42
1 /*
2 * Copyright (C) 2011 Red Hat. All rights reserved.
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.
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.
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.
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <zlib.h>
30 #include "kerncompat.h"
31 #include "ctree.h"
32 #include "disk-io.h"
33 #include "print-tree.h"
34 #include "transaction.h"
35 #include "list.h"
36 #include "version.h"
37 #include "volumes.h"
38 #include "utils.h"
40 static int verbose = 0;
41 static int no_pretty = 0;
43 struct seek {
44 u64 distance;
45 u64 count;
46 struct rb_node n;
49 struct root_stats {
50 u64 total_nodes;
51 u64 total_leaves;
52 u64 total_bytes;
53 u64 total_inline;
54 u64 total_seeks;
55 u64 forward_seeks;
56 u64 backward_seeks;
57 u64 total_seek_len;
58 u64 max_seek_len;
59 u64 total_clusters;
60 u64 total_cluster_size;
61 u64 min_cluster_size;
62 u64 max_cluster_size;
63 u64 lowest_bytenr;
64 u64 highest_bytenr;
65 struct rb_root seek_root;
66 int total_levels;
69 struct fs_root {
70 struct btrfs_key key;
71 struct btrfs_key *snaps;
74 static int add_seek(struct rb_root *root, u64 dist)
76 struct rb_node **p = &root->rb_node;
77 struct rb_node *parent = NULL;
78 struct seek *seek = NULL;
80 while (*p) {
81 parent = *p;
82 seek = rb_entry(parent, struct seek, n);
84 if (dist < seek->distance) {
85 p = &(*p)->rb_left;
86 } else if (dist > seek->distance) {
87 p = &(*p)->rb_right;
88 } else {
89 seek->count++;
90 return 0;
94 seek = malloc(sizeof(struct seek));
95 if (!seek)
96 return -ENOMEM;
97 seek->distance = dist;
98 seek->count = 1;
99 rb_link_node(&seek->n, parent, p);
100 rb_insert_color(&seek->n, root);
101 return 0;
104 static int walk_leaf(struct btrfs_root *root, struct btrfs_path *path,
105 struct root_stats *stat, int find_inline)
107 struct extent_buffer *b = path->nodes[0];
108 struct btrfs_file_extent_item *fi;
109 struct btrfs_key found_key;
110 int i;
112 stat->total_bytes += root->leafsize;
113 stat->total_leaves++;
115 if (!find_inline)
116 return 0;
118 for (i = 0; i < btrfs_header_nritems(b); i++) {
119 btrfs_item_key_to_cpu(b, &found_key, i);
120 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
121 continue;
123 fi = btrfs_item_ptr(b, i, struct btrfs_file_extent_item);
124 if (btrfs_file_extent_type(b, fi) == BTRFS_FILE_EXTENT_INLINE)
125 stat->total_inline +=
126 btrfs_file_extent_inline_item_len(b,
127 btrfs_item_nr(i));
130 return 0;
133 static u64 calc_distance(u64 block1, u64 block2)
135 if (block1 < block2)
136 return block2 - block1;
137 return block1 - block2;
140 static int walk_nodes(struct btrfs_root *root, struct btrfs_path *path,
141 struct root_stats *stat, int level, int find_inline)
143 struct extent_buffer *b = path->nodes[level];
144 u64 last_block;
145 u64 cluster_size = root->leafsize;
146 int i;
147 int ret = 0;
149 stat->total_bytes += root->nodesize;
150 stat->total_nodes++;
152 last_block = btrfs_header_bytenr(b);
153 for (i = 0; i < btrfs_header_nritems(b); i++) {
154 struct extent_buffer *tmp = NULL;
155 u64 cur_blocknr = btrfs_node_blockptr(b, i);
157 path->slots[level] = i;
158 if ((level - 1) > 0 || find_inline) {
159 tmp = read_tree_block(root, cur_blocknr,
160 btrfs_level_size(root, level - 1),
161 btrfs_node_ptr_generation(b, i));
162 if (!tmp) {
163 fprintf(stderr, "Failed to read blocknr %Lu\n",
164 btrfs_node_blockptr(b, i));
165 continue;
167 path->nodes[level - 1] = tmp;
169 if (level - 1)
170 ret = walk_nodes(root, path, stat, level - 1,
171 find_inline);
172 else
173 ret = walk_leaf(root, path, stat, find_inline);
174 if (last_block + root->leafsize != cur_blocknr) {
175 u64 distance = calc_distance(last_block +
176 root->leafsize,
177 cur_blocknr);
178 stat->total_seeks++;
179 stat->total_seek_len += distance;
180 if (stat->max_seek_len < distance)
181 stat->max_seek_len = distance;
182 if (add_seek(&stat->seek_root, distance)) {
183 fprintf(stderr, "Error adding new seek\n");
184 ret = -ENOMEM;
185 break;
188 if (last_block < cur_blocknr)
189 stat->forward_seeks++;
190 else
191 stat->backward_seeks++;
192 if (cluster_size != root->leafsize) {
193 stat->total_cluster_size += cluster_size;
194 stat->total_clusters++;
195 if (cluster_size < stat->min_cluster_size)
196 stat->min_cluster_size = cluster_size;
197 if (cluster_size > stat->max_cluster_size)
198 stat->max_cluster_size = cluster_size;
200 cluster_size = root->leafsize;
201 } else {
202 cluster_size += root->leafsize;
204 last_block = cur_blocknr;
205 if (cur_blocknr < stat->lowest_bytenr)
206 stat->lowest_bytenr = cur_blocknr;
207 if (cur_blocknr > stat->highest_bytenr)
208 stat->highest_bytenr = cur_blocknr;
209 free_extent_buffer(tmp);
210 if (ret) {
211 fprintf(stderr, "Error walking down path\n");
212 break;
216 return ret;
219 static void print_seek_histogram(struct root_stats *stat)
221 struct rb_node *n = rb_first(&stat->seek_root);
222 struct seek *seek;
223 u64 tick_interval;
224 u64 group_start;
225 u64 group_count = 0;
226 u64 group_end;
227 u64 i;
228 u64 max_seek = stat->max_seek_len;
229 int digits = 1;
231 if (stat->total_seeks < 20)
232 return;
234 while ((max_seek /= 10))
235 digits++;
237 /* Make a tick count as 5% of the total seeks */
238 tick_interval = stat->total_seeks / 20;
239 printf("\tSeek histogram\n");
240 for (; n; n = rb_next(n)) {
241 u64 ticks, gticks = 0;
243 seek = rb_entry(n, struct seek, n);
244 ticks = seek->count / tick_interval;
245 if (group_count)
246 gticks = group_count / tick_interval;
248 if (ticks <= 2 && gticks <= 2) {
249 if (group_count == 0)
250 group_start = seek->distance;
251 group_end = seek->distance;
252 group_count += seek->count;
253 continue;
256 if (group_count) {
258 gticks = group_count / tick_interval;
259 printf("\t\t%*Lu - %*Lu: %*Lu ", digits, group_start,
260 digits, group_end, digits, group_count);
261 if (gticks) {
262 for (i = 0; i < gticks; i++)
263 printf("#");
264 printf("\n");
265 } else {
266 printf("|\n");
268 group_count = 0;
271 if (ticks <= 2)
272 continue;
274 printf("\t\t%*Lu - %*Lu: %*Lu ", digits, seek->distance,
275 digits, seek->distance, digits, seek->count);
276 for (i = 0; i < ticks; i++)
277 printf("#");
278 printf("\n");
280 if (group_count) {
281 u64 gticks;
283 gticks = group_count / tick_interval;
284 printf("\t\t%*Lu - %*Lu: %*Lu ", digits, group_start,
285 digits, group_end, digits, group_count);
286 if (gticks) {
287 for (i = 0; i < gticks; i++)
288 printf("#");
289 printf("\n");
290 } else {
291 printf("|\n");
293 group_count = 0;
297 static void timeval_subtract(struct timeval *result,struct timeval *x,
298 struct timeval *y)
300 if (x->tv_usec < y->tv_usec) {
301 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
302 y->tv_usec -= 1000000 * nsec;
303 y->tv_sec += nsec;
306 if (x->tv_usec - y->tv_usec > 1000000) {
307 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
308 y->tv_usec += 1000000 * nsec;
309 y->tv_sec -= nsec;
312 result->tv_sec = x->tv_sec - y->tv_sec;
313 result->tv_usec = x->tv_usec - y->tv_usec;
316 static int calc_root_size(struct btrfs_root *tree_root, struct btrfs_key *key,
317 int find_inline)
319 struct btrfs_root *root;
320 struct btrfs_path *path;
321 struct rb_node *n;
322 struct timeval start, end, diff = {0};
323 struct root_stats stat;
324 int level;
325 int ret = 0;
326 int size_fail = 0;
328 root = btrfs_read_fs_root(tree_root->fs_info, key);
329 if (!root) {
330 fprintf(stderr, "Failed to read root %Lu\n", key->objectid);
331 return 1;
334 path = btrfs_alloc_path();
335 if (!path) {
336 fprintf(stderr, "Could not allocate path\n");
337 return 1;
340 memset(&stat, 0, sizeof(stat));
341 level = btrfs_header_level(root->node);
342 stat.lowest_bytenr = btrfs_header_bytenr(root->node);
343 stat.highest_bytenr = stat.lowest_bytenr;
344 stat.min_cluster_size = (u64)-1;
345 stat.max_cluster_size = root->leafsize;
346 path->nodes[level] = root->node;
347 if (gettimeofday(&start, NULL)) {
348 fprintf(stderr, "Error getting time: %d\n", errno);
349 goto out;
351 if (!level) {
352 ret = walk_leaf(root, path, &stat, find_inline);
353 if (ret)
354 goto out;
355 goto out_print;
358 ret = walk_nodes(root, path, &stat, level, find_inline);
359 if (ret)
360 goto out;
361 if (gettimeofday(&end, NULL)) {
362 fprintf(stderr, "Error getting time: %d\n", errno);
363 goto out;
365 timeval_subtract(&diff, &end, &start);
366 out_print:
367 if (stat.min_cluster_size == (u64)-1) {
368 stat.min_cluster_size = 0;
369 stat.total_clusters = 1;
372 if (no_pretty || size_fail) {
373 printf("\tTotal size: %Lu\n", stat.total_bytes);
374 printf("\t\tInline data: %Lu\n", stat.total_inline);
375 printf("\tTotal seeks: %Lu\n", stat.total_seeks);
376 printf("\t\tForward seeks: %Lu\n", stat.forward_seeks);
377 printf("\t\tBackward seeks: %Lu\n", stat.backward_seeks);
378 printf("\t\tAvg seek len: %Lu\n", stat.total_seek_len /
379 stat.total_seeks);
380 print_seek_histogram(&stat);
381 printf("\tTotal clusters: %Lu\n", stat.total_clusters);
382 printf("\t\tAvg cluster size: %Lu\n", stat.total_cluster_size /
383 stat.total_clusters);
384 printf("\t\tMin cluster size: %Lu\n", stat.min_cluster_size);
385 printf("\t\tMax cluster size: %Lu\n", stat.max_cluster_size);
386 printf("\tTotal disk spread: %Lu\n", stat.highest_bytenr -
387 stat.lowest_bytenr);
388 printf("\tTotal read time: %d s %d us\n", (int)diff.tv_sec,
389 (int)diff.tv_usec);
390 printf("\tLevels: %d\n", level + 1);
391 } else {
392 printf("\tTotal size: %s\n", pretty_size(stat.total_bytes));
393 printf("\t\tInline data: %s\n", pretty_size(stat.total_inline));
394 printf("\tTotal seeks: %Lu\n", stat.total_seeks);
395 printf("\t\tForward seeks: %Lu\n", stat.forward_seeks);
396 printf("\t\tBackward seeks: %Lu\n", stat.backward_seeks);
397 printf("\t\tAvg seek len: %s\n", stat.total_seeks ?
398 pretty_size(stat.total_seek_len / stat.total_seeks) :
399 pretty_size(0));
400 print_seek_histogram(&stat);
401 printf("\tTotal clusters: %Lu\n", stat.total_clusters);
402 printf("\t\tAvg cluster size: %s\n",
403 pretty_size((stat.total_cluster_size /
404 stat.total_clusters)));
405 printf("\t\tMin cluster size: %s\n",
406 pretty_size(stat.min_cluster_size));
407 printf("\t\tMax cluster size: %s\n",
408 pretty_size(stat.max_cluster_size));
409 printf("\tTotal disk spread: %s\n",
410 pretty_size(stat.highest_bytenr -
411 stat.lowest_bytenr));
412 printf("\tTotal read time: %d s %d us\n", (int)diff.tv_sec,
413 (int)diff.tv_usec);
414 printf("\tLevels: %d\n", level + 1);
416 out:
417 while ((n = rb_first(&stat.seek_root)) != NULL) {
418 struct seek *seek = rb_entry(n, struct seek, n);
419 rb_erase(n, &stat.seek_root);
420 free(seek);
423 btrfs_free_path(path);
424 return ret;
427 static void usage()
429 fprintf(stderr, "Usage: calc-size [-v] [-b] <device>\n");
432 int main(int argc, char **argv)
434 struct btrfs_key key;
435 struct fs_root *roots;
436 struct btrfs_root *root;
437 size_t fs_roots_size = sizeof(struct fs_root);
438 int opt;
439 int ret = 0;
441 while ((opt = getopt(argc, argv, "vb")) != -1) {
442 switch (opt) {
443 case 'v':
444 verbose++;
445 break;
446 case 'b':
447 no_pretty = 1;
448 break;
449 default:
450 usage();
451 exit(1);
455 if (optind >= argc) {
456 usage();
457 exit(1);
461 if ((ret = check_mounted(argv[optind])) < 0) {
462 fprintf(stderr, "Could not check mount status: %d\n", ret);
463 if (ret == -EACCES)
464 fprintf(stderr, "Maybe you need to run as root?\n");
465 return ret;
466 } else if (ret) {
467 fprintf(stderr, "%s is currently mounted. Aborting.\n",
468 argv[optind]);
469 return -EBUSY;
473 root = open_ctree(argv[optind], 0, 0);
474 if (!root) {
475 fprintf(stderr, "Couldn't open ctree\n");
476 exit(1);
479 roots = malloc(fs_roots_size);
480 if (!roots) {
481 fprintf(stderr, "No memory\n");
482 goto out;
485 printf("Calculating size of root tree\n");
486 key.objectid = BTRFS_ROOT_TREE_OBJECTID;
487 ret = calc_root_size(root, &key, 0);
488 if (ret)
489 goto out;
491 printf("Calculating size of extent tree\n");
492 key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
493 ret = calc_root_size(root, &key, 0);
494 if (ret)
495 goto out;
497 printf("Calculating size of csum tree\n");
498 key.objectid = BTRFS_CSUM_TREE_OBJECTID;
499 ret = calc_root_size(root, &key, 0);
500 if (ret)
501 goto out;
503 roots[0].key.objectid = BTRFS_FS_TREE_OBJECTID;
504 roots[0].key.offset = (u64)-1;
505 printf("Calculatin' size of fs tree\n");
506 ret = calc_root_size(root, &roots[0].key, 1);
507 if (ret)
508 goto out;
509 out:
510 close_ctree(root);
511 free(roots);
512 return ret;