2016-03-11 11:49:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Red Hat. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "kerncompat.h"
|
|
|
|
#include <sys/time.h>
|
2016-03-11 11:49:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "kernel-lib/rbtree.h"
|
|
|
|
#include "kernel-lib/rbtree_types.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/disk-io.h"
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "kernel-shared/extent_io.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/utils.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/help.h"
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "common/messages.h"
|
2021-04-29 17:52:15 +00:00
|
|
|
#include "common/open-utils.h"
|
2021-04-29 21:43:24 +00:00
|
|
|
#include "common/units.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "cmds/commands.h"
|
2016-03-11 11:49:45 +00:00
|
|
|
|
|
|
|
static int verbose = 0;
|
2022-10-05 18:02:13 +00:00
|
|
|
static bool no_pretty = false;
|
2016-03-11 11:49:45 +00:00
|
|
|
|
|
|
|
struct seek {
|
|
|
|
u64 distance;
|
|
|
|
u64 count;
|
|
|
|
struct rb_node n;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct root_stats {
|
|
|
|
u64 total_nodes;
|
|
|
|
u64 total_bytes;
|
|
|
|
u64 total_inline;
|
|
|
|
u64 total_seeks;
|
|
|
|
u64 forward_seeks;
|
|
|
|
u64 backward_seeks;
|
|
|
|
u64 total_seek_len;
|
|
|
|
u64 max_seek_len;
|
|
|
|
u64 total_clusters;
|
|
|
|
u64 total_cluster_size;
|
|
|
|
u64 min_cluster_size;
|
|
|
|
u64 max_cluster_size;
|
|
|
|
u64 lowest_bytenr;
|
|
|
|
u64 highest_bytenr;
|
2020-07-09 14:00:08 +00:00
|
|
|
u64 node_counts[BTRFS_MAX_LEVEL];
|
2016-03-11 11:49:45 +00:00
|
|
|
struct rb_root seek_root;
|
|
|
|
int total_levels;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int add_seek(struct rb_root *root, u64 dist)
|
|
|
|
{
|
|
|
|
struct rb_node **p = &root->rb_node;
|
|
|
|
struct rb_node *parent = NULL;
|
|
|
|
struct seek *seek = NULL;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
parent = *p;
|
|
|
|
seek = rb_entry(parent, struct seek, n);
|
|
|
|
|
|
|
|
if (dist < seek->distance) {
|
|
|
|
p = &(*p)->rb_left;
|
|
|
|
} else if (dist > seek->distance) {
|
|
|
|
p = &(*p)->rb_right;
|
|
|
|
} else {
|
|
|
|
seek->count++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
seek = malloc(sizeof(struct seek));
|
|
|
|
if (!seek)
|
|
|
|
return -ENOMEM;
|
|
|
|
seek->distance = dist;
|
|
|
|
seek->count = 1;
|
|
|
|
rb_link_node(&seek->n, parent, p);
|
|
|
|
rb_insert_color(&seek->n, root);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int walk_leaf(struct btrfs_root *root, struct btrfs_path *path,
|
|
|
|
struct root_stats *stat, int find_inline)
|
|
|
|
{
|
|
|
|
struct extent_buffer *b = path->nodes[0];
|
|
|
|
struct btrfs_file_extent_item *fi;
|
|
|
|
struct btrfs_key found_key;
|
|
|
|
int i;
|
|
|
|
|
2017-05-18 01:54:49 +00:00
|
|
|
stat->total_bytes += root->fs_info->nodesize;
|
2020-07-09 14:00:08 +00:00
|
|
|
stat->total_nodes++;
|
|
|
|
stat->node_counts[0]++;
|
2016-03-11 11:49:45 +00:00
|
|
|
|
|
|
|
if (!find_inline)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < btrfs_header_nritems(b); i++) {
|
|
|
|
btrfs_item_key_to_cpu(b, &found_key, i);
|
|
|
|
if (found_key.type != BTRFS_EXTENT_DATA_KEY)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fi = btrfs_item_ptr(b, i, struct btrfs_file_extent_item);
|
|
|
|
if (btrfs_file_extent_type(b, fi) == BTRFS_FILE_EXTENT_INLINE)
|
|
|
|
stat->total_inline +=
|
2022-02-22 22:26:18 +00:00
|
|
|
btrfs_file_extent_inline_item_len(b, i);
|
2016-03-11 11:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 calc_distance(u64 block1, u64 block2)
|
|
|
|
{
|
|
|
|
if (block1 < block2)
|
|
|
|
return block2 - block1;
|
|
|
|
return block1 - block2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int walk_nodes(struct btrfs_root *root, struct btrfs_path *path,
|
|
|
|
struct root_stats *stat, int level, int find_inline)
|
|
|
|
{
|
|
|
|
struct extent_buffer *b = path->nodes[level];
|
2017-05-18 01:54:49 +00:00
|
|
|
u32 nodesize = root->fs_info->nodesize;
|
2016-03-11 11:49:45 +00:00
|
|
|
u64 last_block;
|
2017-05-18 01:54:49 +00:00
|
|
|
u64 cluster_size = nodesize;
|
2016-03-11 11:49:45 +00:00
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
|
2017-05-18 01:54:49 +00:00
|
|
|
stat->total_bytes += nodesize;
|
2016-03-11 11:49:45 +00:00
|
|
|
stat->total_nodes++;
|
2020-07-09 14:00:08 +00:00
|
|
|
stat->node_counts[level]++;
|
2016-03-11 11:49:45 +00:00
|
|
|
|
|
|
|
last_block = btrfs_header_bytenr(b);
|
|
|
|
for (i = 0; i < btrfs_header_nritems(b); i++) {
|
|
|
|
struct extent_buffer *tmp = NULL;
|
|
|
|
u64 cur_blocknr = btrfs_node_blockptr(b, i);
|
|
|
|
|
|
|
|
path->slots[level] = i;
|
|
|
|
if ((level - 1) > 0 || find_inline) {
|
2017-05-18 02:51:08 +00:00
|
|
|
tmp = read_tree_block(root->fs_info, cur_blocknr,
|
2016-03-11 11:49:45 +00:00
|
|
|
btrfs_node_ptr_generation(b, i));
|
|
|
|
if (!extent_buffer_uptodate(tmp)) {
|
2016-09-07 13:16:31 +00:00
|
|
|
error("failed to read blocknr %llu",
|
2016-03-11 11:49:45 +00:00
|
|
|
btrfs_node_blockptr(b, i));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
path->nodes[level - 1] = tmp;
|
|
|
|
}
|
|
|
|
if (level - 1)
|
|
|
|
ret = walk_nodes(root, path, stat, level - 1,
|
|
|
|
find_inline);
|
|
|
|
else
|
|
|
|
ret = walk_leaf(root, path, stat, find_inline);
|
2017-05-18 01:54:49 +00:00
|
|
|
if (last_block + nodesize != cur_blocknr) {
|
2016-03-11 11:49:45 +00:00
|
|
|
u64 distance = calc_distance(last_block +
|
2017-05-18 01:54:49 +00:00
|
|
|
nodesize,
|
2016-03-11 11:49:45 +00:00
|
|
|
cur_blocknr);
|
|
|
|
stat->total_seeks++;
|
|
|
|
stat->total_seek_len += distance;
|
|
|
|
if (stat->max_seek_len < distance)
|
|
|
|
stat->max_seek_len = distance;
|
|
|
|
if (add_seek(&stat->seek_root, distance)) {
|
2022-09-27 23:13:00 +00:00
|
|
|
error("cannot add new seek at distance %llu", distance);
|
2016-03-11 11:49:45 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_block < cur_blocknr)
|
|
|
|
stat->forward_seeks++;
|
|
|
|
else
|
|
|
|
stat->backward_seeks++;
|
2017-05-18 01:54:49 +00:00
|
|
|
if (cluster_size != nodesize) {
|
2016-03-11 11:49:45 +00:00
|
|
|
stat->total_cluster_size += cluster_size;
|
|
|
|
stat->total_clusters++;
|
|
|
|
if (cluster_size < stat->min_cluster_size)
|
|
|
|
stat->min_cluster_size = cluster_size;
|
|
|
|
if (cluster_size > stat->max_cluster_size)
|
|
|
|
stat->max_cluster_size = cluster_size;
|
|
|
|
}
|
2017-05-18 01:54:49 +00:00
|
|
|
cluster_size = nodesize;
|
2016-03-11 11:49:45 +00:00
|
|
|
} else {
|
2017-05-18 01:54:49 +00:00
|
|
|
cluster_size += nodesize;
|
2016-03-11 11:49:45 +00:00
|
|
|
}
|
|
|
|
last_block = cur_blocknr;
|
|
|
|
if (cur_blocknr < stat->lowest_bytenr)
|
|
|
|
stat->lowest_bytenr = cur_blocknr;
|
|
|
|
if (cur_blocknr > stat->highest_bytenr)
|
|
|
|
stat->highest_bytenr = cur_blocknr;
|
|
|
|
free_extent_buffer(tmp);
|
|
|
|
if (ret) {
|
2016-09-07 13:16:31 +00:00
|
|
|
error("walking down path failed: %d", ret);
|
2016-03-11 11:49:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_seek_histogram(struct root_stats *stat)
|
|
|
|
{
|
|
|
|
struct rb_node *n = rb_first(&stat->seek_root);
|
|
|
|
struct seek *seek;
|
|
|
|
u64 tick_interval;
|
|
|
|
u64 group_start = 0;
|
|
|
|
u64 group_count = 0;
|
|
|
|
u64 group_end = 0;
|
|
|
|
u64 i;
|
|
|
|
u64 max_seek = stat->max_seek_len;
|
|
|
|
int digits = 1;
|
|
|
|
|
|
|
|
if (stat->total_seeks < 20)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ((max_seek /= 10))
|
|
|
|
digits++;
|
|
|
|
|
|
|
|
/* Make a tick count as 5% of the total seeks */
|
|
|
|
tick_interval = stat->total_seeks / 20;
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tSeek histogram\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
for (; n; n = rb_next(n)) {
|
|
|
|
u64 ticks, gticks = 0;
|
|
|
|
|
|
|
|
seek = rb_entry(n, struct seek, n);
|
|
|
|
ticks = seek->count / tick_interval;
|
|
|
|
if (group_count)
|
|
|
|
gticks = group_count / tick_interval;
|
|
|
|
|
|
|
|
if (ticks <= 2 && gticks <= 2) {
|
|
|
|
if (group_count == 0)
|
|
|
|
group_start = seek->distance;
|
|
|
|
group_end = seek->distance;
|
|
|
|
group_count += seek->count;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group_count) {
|
|
|
|
|
|
|
|
gticks = group_count / tick_interval;
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\t\t%*llu - %*llu: %*llu ", digits, group_start,
|
2016-03-11 11:49:45 +00:00
|
|
|
digits, group_end, digits, group_count);
|
|
|
|
if (gticks) {
|
|
|
|
for (i = 0; i < gticks; i++)
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "#");
|
|
|
|
pr_verbose(LOG_DEFAULT, "\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
} else {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "|\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
}
|
|
|
|
group_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ticks <= 2)
|
|
|
|
continue;
|
|
|
|
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\t\t%*llu - %*llu: %*llu ", digits, seek->distance,
|
2016-03-11 11:49:45 +00:00
|
|
|
digits, seek->distance, digits, seek->count);
|
|
|
|
for (i = 0; i < ticks; i++)
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "#");
|
|
|
|
pr_verbose(LOG_DEFAULT, "\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
}
|
|
|
|
if (group_count) {
|
|
|
|
u64 gticks;
|
|
|
|
|
|
|
|
gticks = group_count / tick_interval;
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\t\t%*llu - %*llu: %*llu ", digits, group_start,
|
2016-03-11 11:49:45 +00:00
|
|
|
digits, group_end, digits, group_count);
|
|
|
|
if (gticks) {
|
|
|
|
for (i = 0; i < gticks; i++)
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "#");
|
|
|
|
pr_verbose(LOG_DEFAULT, "\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
} else {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "|\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
}
|
|
|
|
group_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void timeval_subtract(struct timeval *result, struct timeval *x,
|
|
|
|
struct timeval *y)
|
|
|
|
{
|
|
|
|
if (x->tv_usec < y->tv_usec) {
|
|
|
|
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
|
|
|
|
y->tv_usec -= 1000000 * nsec;
|
|
|
|
y->tv_sec += nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x->tv_usec - y->tv_usec > 1000000) {
|
|
|
|
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
|
|
|
|
y->tv_usec += 1000000 * nsec;
|
|
|
|
y->tv_sec -= nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->tv_sec = x->tv_sec - y->tv_sec;
|
|
|
|
result->tv_usec = x->tv_usec - y->tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int calc_root_size(struct btrfs_root *tree_root, struct btrfs_key *key,
|
|
|
|
int find_inline)
|
|
|
|
{
|
|
|
|
struct btrfs_root *root;
|
2016-11-02 23:37:51 +00:00
|
|
|
struct btrfs_path path;
|
2016-03-11 11:49:45 +00:00
|
|
|
struct rb_node *n;
|
|
|
|
struct timeval start, end, diff = {0};
|
|
|
|
struct root_stats stat;
|
|
|
|
int level;
|
|
|
|
int ret = 0;
|
|
|
|
int size_fail = 0;
|
2020-07-09 14:00:08 +00:00
|
|
|
int i;
|
2016-03-11 11:49:45 +00:00
|
|
|
|
|
|
|
root = btrfs_read_fs_root(tree_root->fs_info, key);
|
|
|
|
if (IS_ERR(root)) {
|
2016-09-07 13:16:31 +00:00
|
|
|
error("failed to read root %llu", key->objectid);
|
2016-03-11 11:49:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:37:51 +00:00
|
|
|
btrfs_init_path(&path);
|
2016-03-11 11:49:45 +00:00
|
|
|
memset(&stat, 0, sizeof(stat));
|
|
|
|
level = btrfs_header_level(root->node);
|
|
|
|
stat.lowest_bytenr = btrfs_header_bytenr(root->node);
|
|
|
|
stat.highest_bytenr = stat.lowest_bytenr;
|
|
|
|
stat.min_cluster_size = (u64)-1;
|
2017-05-18 01:54:49 +00:00
|
|
|
stat.max_cluster_size = root->fs_info->nodesize;
|
2016-11-02 23:37:51 +00:00
|
|
|
path.nodes[level] = root->node;
|
2016-03-11 11:49:45 +00:00
|
|
|
if (gettimeofday(&start, NULL)) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("cannot get time: %m");
|
2016-03-11 11:49:45 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (!level) {
|
2016-11-02 23:37:51 +00:00
|
|
|
ret = walk_leaf(root, &path, &stat, find_inline);
|
2016-03-11 11:49:45 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
goto out_print;
|
|
|
|
}
|
|
|
|
|
2016-11-02 23:37:51 +00:00
|
|
|
ret = walk_nodes(root, &path, &stat, level, find_inline);
|
2016-03-11 11:49:45 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
if (gettimeofday(&end, NULL)) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("cannot get time: %m");
|
2016-03-11 11:49:45 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
timeval_subtract(&diff, &end, &start);
|
|
|
|
out_print:
|
|
|
|
if (stat.min_cluster_size == (u64)-1) {
|
|
|
|
stat.min_cluster_size = 0;
|
|
|
|
stat.total_clusters = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (no_pretty || size_fail) {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal size: %llu\n", stat.total_bytes);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tInline data: %llu\n", stat.total_inline);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal seeks: %llu\n", stat.total_seeks);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tForward seeks: %llu\n", stat.forward_seeks);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tBackward seeks: %llu\n", stat.backward_seeks);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tAvg seek len: %llu\n", stat.total_seeks ?
|
2016-03-11 11:49:45 +00:00
|
|
|
stat.total_seek_len / stat.total_seeks : 0);
|
|
|
|
print_seek_histogram(&stat);
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal clusters: %llu\n", stat.total_clusters);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tAvg cluster size: %llu\n", stat.total_cluster_size /
|
2016-03-11 11:49:45 +00:00
|
|
|
stat.total_clusters);
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tMin cluster size: %llu\n", stat.min_cluster_size);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tMax cluster size: %llu\n", stat.max_cluster_size);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal disk spread: %llu\n", stat.highest_bytenr -
|
2016-03-11 11:49:45 +00:00
|
|
|
stat.lowest_bytenr);
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal read time: %d s %d us\n", (int)diff.tv_sec,
|
2016-03-11 11:49:45 +00:00
|
|
|
(int)diff.tv_usec);
|
|
|
|
} else {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal size: %s\n", pretty_size(stat.total_bytes));
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tInline data: %s\n", pretty_size(stat.total_inline));
|
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal seeks: %llu\n", stat.total_seeks);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tForward seeks: %llu\n", stat.forward_seeks);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tBackward seeks: %llu\n", stat.backward_seeks);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tAvg seek len: %s\n", stat.total_seeks ?
|
2016-03-11 11:49:45 +00:00
|
|
|
pretty_size(stat.total_seek_len / stat.total_seeks) :
|
|
|
|
pretty_size(0));
|
|
|
|
print_seek_histogram(&stat);
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal clusters: %llu\n", stat.total_clusters);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tAvg cluster size: %s\n",
|
2016-03-11 11:49:45 +00:00
|
|
|
pretty_size((stat.total_cluster_size /
|
|
|
|
stat.total_clusters)));
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tMin cluster size: %s\n",
|
2016-03-11 11:49:45 +00:00
|
|
|
pretty_size(stat.min_cluster_size));
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tMax cluster size: %s\n",
|
2016-03-11 11:49:45 +00:00
|
|
|
pretty_size(stat.max_cluster_size));
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal disk spread: %s\n",
|
2016-03-11 11:49:45 +00:00
|
|
|
pretty_size(stat.highest_bytenr -
|
|
|
|
stat.lowest_bytenr));
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal read time: %d s %d us\n", (int)diff.tv_sec,
|
2016-03-11 11:49:45 +00:00
|
|
|
(int)diff.tv_usec);
|
|
|
|
}
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\tLevels: %d\n", level + 1);
|
|
|
|
pr_verbose(LOG_DEFAULT, "\tTotal nodes: %llu\n", stat.total_nodes);
|
2020-07-27 13:39:19 +00:00
|
|
|
for (i = 0; i < level + 1; i++) {
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\t\tOn level %d: %8llu", i, stat.node_counts[i]);
|
2020-07-27 13:39:19 +00:00
|
|
|
if (i > 0) {
|
|
|
|
u64 fanout;
|
|
|
|
|
|
|
|
fanout = stat.node_counts[i - 1];
|
|
|
|
fanout /= stat.node_counts[i];
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, " (avg fanout %llu)", fanout);
|
2020-07-27 13:39:19 +00:00
|
|
|
}
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "\n");
|
2020-07-27 13:39:19 +00:00
|
|
|
}
|
2016-03-11 11:49:45 +00:00
|
|
|
out:
|
|
|
|
while ((n = rb_first(&stat.seek_root)) != NULL) {
|
|
|
|
struct seek *seek = rb_entry(n, struct seek, n);
|
|
|
|
rb_erase(n, &stat.seek_root);
|
|
|
|
free(seek);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-11-02 23:37:51 +00:00
|
|
|
* We only use path to save node data in iterating, without holding
|
|
|
|
* eb's ref_cnt in path. Don't use btrfs_release_path() here, it will
|
|
|
|
* free these eb again, and cause many problems, as negative ref_cnt or
|
|
|
|
* invalid memory access.
|
2016-03-11 11:49:45 +00:00
|
|
|
*/
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
static const char * const cmd_inspect_tree_stats_usage[] = {
|
2016-03-11 11:49:45 +00:00
|
|
|
"btrfs inspect-internal tree-stats [options] <device>",
|
|
|
|
"Print various stats for trees",
|
2019-06-21 13:36:04 +00:00
|
|
|
"",
|
2023-02-22 00:37:18 +00:00
|
|
|
OPTLINE("-b", "raw numbers in bytes"),
|
2016-03-11 11:49:45 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_inspect_tree_stats(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2016-03-11 11:49:45 +00:00
|
|
|
{
|
2022-02-22 22:22:42 +00:00
|
|
|
struct btrfs_key key = { .type = BTRFS_ROOT_ITEM_KEY };
|
2016-03-11 11:49:45 +00:00
|
|
|
struct btrfs_root *root;
|
|
|
|
int opt;
|
|
|
|
int ret = 0;
|
|
|
|
|
2018-06-20 00:38:38 +00:00
|
|
|
optind = 0;
|
2016-03-11 11:49:45 +00:00
|
|
|
while ((opt = getopt(argc, argv, "vb")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
2022-10-05 18:02:13 +00:00
|
|
|
no_pretty = true;
|
2016-03-11 11:49:45 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2016-03-11 11:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 13:49:15 +00:00
|
|
|
if (check_argc_exact(argc - optind, 1))
|
|
|
|
return 1;
|
2016-03-11 11:49:45 +00:00
|
|
|
|
2016-09-07 13:31:27 +00:00
|
|
|
ret = check_mounted(argv[optind]);
|
|
|
|
if (ret < 0) {
|
2018-10-25 12:10:54 +00:00
|
|
|
errno = -ret;
|
|
|
|
warning("unable to check mount status of: %m");
|
2016-03-11 11:49:45 +00:00
|
|
|
} else if (ret) {
|
2023-04-19 23:49:45 +00:00
|
|
|
warning("%s already mounted, tree-stats accesses the block devices directly, this may\n"
|
|
|
|
"\tresult in inaccurate numbers, various errors or it may crash if the filesystem\n"
|
|
|
|
"\tchanges unexpectedly, restart if needed or remount read-only", argv[optind]);
|
2016-03-11 11:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
root = open_ctree(argv[optind], 0, 0);
|
|
|
|
if (!root) {
|
2016-09-07 13:16:31 +00:00
|
|
|
error("cannot open ctree");
|
2016-03-11 11:49:45 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Calculating size of root tree\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
key.objectid = BTRFS_ROOT_TREE_OBJECTID;
|
|
|
|
ret = calc_root_size(root, &key, 0);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Calculating size of extent tree\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
|
|
|
|
ret = calc_root_size(root, &key, 0);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Calculating size of csum tree\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
key.objectid = BTRFS_CSUM_TREE_OBJECTID;
|
|
|
|
ret = calc_root_size(root, &key, 0);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
key.objectid = BTRFS_FS_TREE_OBJECTID;
|
|
|
|
key.offset = (u64)-1;
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "Calculating size of fs tree\n");
|
2016-03-11 11:49:45 +00:00
|
|
|
ret = calc_root_size(root, &key, 1);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
out:
|
|
|
|
close_ctree(root);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
DEFINE_SIMPLE_COMMAND(inspect_tree_stats, "tree-stats");
|