forked from Mirrors/btrfs-progs
btrfs-progs: per-thread, per-call pretty buffer
We don't need callers to manage string storage for each pretty_sizes() call. We can use a macro to have per-thread and per-call static storage so that pretty_sizes() can be used as many times as needed in printf() arguments without requiring a bunch of supporting variables. This lets us have a natural interface at the cost of requiring __thread and TLS from gcc and a small amount of static storage. This seems better than the current code or doing something with illegible format specifier macros. Signed-off-by: Zach Brown <zab@redhat.com> Acked-by: Wang Shilong <wangs.fnst@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>master
parent
8e6b7ce746
commit
876e3f9380
|
@ -162,18 +162,11 @@ out_print:
|
||||||
stat.total_inline, stat.total_nodes, stat.total_leaves,
|
stat.total_inline, stat.total_nodes, stat.total_leaves,
|
||||||
level + 1);
|
level + 1);
|
||||||
} else {
|
} else {
|
||||||
char *total_size;
|
|
||||||
char *inline_size;
|
|
||||||
|
|
||||||
total_size = pretty_sizes(stat.total_bytes);
|
|
||||||
inline_size = pretty_sizes(stat.total_inline);
|
|
||||||
|
|
||||||
printf("\t%s total size, %s inline data, %Lu nodes, "
|
printf("\t%s total size, %s inline data, %Lu nodes, "
|
||||||
"%Lu leaves, %d levels\n",
|
"%Lu leaves, %d levels\n",
|
||||||
total_size, inline_size, stat.total_nodes,
|
pretty_size(stat.total_bytes),
|
||||||
stat.total_leaves, level + 1);
|
pretty_size(stat.total_inline),
|
||||||
free(total_size);
|
stat.total_nodes, stat.total_leaves, level + 1);
|
||||||
free(inline_size);
|
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
btrfs_free_path(path);
|
btrfs_free_path(path);
|
||||||
|
|
|
@ -87,7 +87,7 @@ print_bg(FILE *html, char *name, u64 start, u64 len, u64 used, u64 flags,
|
||||||
|
|
||||||
fprintf(html, "<p>%s chunk starts at %lld, size is %s, %.2f%% used, "
|
fprintf(html, "<p>%s chunk starts at %lld, size is %s, %.2f%% used, "
|
||||||
"%.2f%% fragmented</p>\n", chunk_type(flags), start,
|
"%.2f%% fragmented</p>\n", chunk_type(flags), start,
|
||||||
pretty_sizes(len), 100.0 * used / len, 100.0 * frag);
|
pretty_size(len), 100.0 * used / len, 100.0 * frag);
|
||||||
fprintf(html, "<img src=\"%s\" border=\"1\" />\n", name);
|
fprintf(html, "<img src=\"%s\" border=\"1\" />\n", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,8 +111,6 @@ static int cmd_df(int argc, char **argv)
|
||||||
|
|
||||||
for (i = 0; i < sargs->total_spaces; i++) {
|
for (i = 0; i < sargs->total_spaces; i++) {
|
||||||
char description[80];
|
char description[80];
|
||||||
char *total_bytes;
|
|
||||||
char *used_bytes;
|
|
||||||
int written = 0;
|
int written = 0;
|
||||||
u64 flags = sargs->spaces[i].flags;
|
u64 flags = sargs->spaces[i].flags;
|
||||||
|
|
||||||
|
@ -155,10 +153,9 @@ static int cmd_df(int argc, char **argv)
|
||||||
written += 7;
|
written += 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
|
printf("%s: total=%s, used=%s\n", description,
|
||||||
used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
|
pretty_size(sargs->spaces[i].total_bytes),
|
||||||
printf("%s: total=%s, used=%s\n", description, total_bytes,
|
pretty_size(sargs->spaces[i].used_bytes));
|
||||||
used_bytes);
|
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
free(sargs);
|
free(sargs);
|
||||||
|
@ -192,7 +189,6 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
|
||||||
char uuidbuf[37];
|
char uuidbuf[37];
|
||||||
struct list_head *cur;
|
struct list_head *cur;
|
||||||
struct btrfs_device *device;
|
struct btrfs_device *device;
|
||||||
char *super_bytes_used;
|
|
||||||
u64 devs_found = 0;
|
u64 devs_found = 0;
|
||||||
u64 total;
|
u64 total;
|
||||||
|
|
||||||
|
@ -204,25 +200,20 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
|
||||||
else
|
else
|
||||||
printf("Label: none ");
|
printf("Label: none ");
|
||||||
|
|
||||||
super_bytes_used = pretty_sizes(device->super_bytes_used);
|
|
||||||
|
|
||||||
total = device->total_devs;
|
total = device->total_devs;
|
||||||
printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
|
printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
|
||||||
(unsigned long long)total, super_bytes_used);
|
(unsigned long long)total,
|
||||||
|
pretty_size(device->super_bytes_used));
|
||||||
free(super_bytes_used);
|
|
||||||
|
|
||||||
list_for_each(cur, &fs_devices->devices) {
|
list_for_each(cur, &fs_devices->devices) {
|
||||||
char *total_bytes;
|
|
||||||
char *bytes_used;
|
|
||||||
device = list_entry(cur, struct btrfs_device, dev_list);
|
device = list_entry(cur, struct btrfs_device, dev_list);
|
||||||
total_bytes = pretty_sizes(device->total_bytes);
|
|
||||||
bytes_used = pretty_sizes(device->bytes_used);
|
|
||||||
printf("\tdevid %4llu size %s used %s path %s\n",
|
printf("\tdevid %4llu size %s used %s path %s\n",
|
||||||
(unsigned long long)device->devid,
|
(unsigned long long)device->devid,
|
||||||
total_bytes, bytes_used, device->name);
|
pretty_size(device->total_bytes),
|
||||||
free(total_bytes);
|
pretty_size(device->bytes_used), device->name);
|
||||||
free(bytes_used);
|
|
||||||
devs_found++;
|
devs_found++;
|
||||||
}
|
}
|
||||||
if (devs_found < total) {
|
if (devs_found < total) {
|
||||||
|
|
|
@ -139,7 +139,6 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p)
|
||||||
{
|
{
|
||||||
u64 err_cnt;
|
u64 err_cnt;
|
||||||
u64 err_cnt2;
|
u64 err_cnt2;
|
||||||
char *bytes;
|
|
||||||
|
|
||||||
err_cnt = p->read_errors +
|
err_cnt = p->read_errors +
|
||||||
p->csum_errors +
|
p->csum_errors +
|
||||||
|
@ -151,10 +150,11 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p)
|
||||||
if (p->malloc_errors)
|
if (p->malloc_errors)
|
||||||
printf("*** WARNING: memory allocation failed while scrubbing. "
|
printf("*** WARNING: memory allocation failed while scrubbing. "
|
||||||
"results may be inaccurate\n");
|
"results may be inaccurate\n");
|
||||||
bytes = pretty_sizes(p->data_bytes_scrubbed + p->tree_bytes_scrubbed);
|
|
||||||
printf("\ttotal bytes scrubbed: %s with %llu errors\n", bytes,
|
printf("\ttotal bytes scrubbed: %s with %llu errors\n",
|
||||||
|
pretty_size(p->data_bytes_scrubbed + p->tree_bytes_scrubbed),
|
||||||
max(err_cnt, err_cnt2));
|
max(err_cnt, err_cnt2));
|
||||||
free(bytes);
|
|
||||||
if (err_cnt || err_cnt2) {
|
if (err_cnt || err_cnt2) {
|
||||||
printf("\terror details:");
|
printf("\terror details:");
|
||||||
PRINT_SCRUB_ERROR(p->read_errors, "read");
|
PRINT_SCRUB_ERROR(p->read_errors, "read");
|
||||||
|
|
4
mkfs.c
4
mkfs.c
|
@ -1356,7 +1356,6 @@ int main(int ac, char **av)
|
||||||
u64 num_of_meta_chunks = 0;
|
u64 num_of_meta_chunks = 0;
|
||||||
u64 size_of_data = 0;
|
u64 size_of_data = 0;
|
||||||
u64 source_dir_size = 0;
|
u64 source_dir_size = 0;
|
||||||
char *pretty_buf;
|
|
||||||
struct btrfs_super_block *super;
|
struct btrfs_super_block *super;
|
||||||
u64 flags;
|
u64 flags;
|
||||||
int dev_cnt = 0;
|
int dev_cnt = 0;
|
||||||
|
@ -1629,8 +1628,7 @@ raid_groups:
|
||||||
printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
|
printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
|
||||||
"sectorsize %u size %s\n",
|
"sectorsize %u size %s\n",
|
||||||
label, first_file, nodesize, leafsize, sectorsize,
|
label, first_file, nodesize, leafsize, sectorsize,
|
||||||
pretty_buf = pretty_sizes(btrfs_super_total_bytes(root->fs_info->super_copy)));
|
pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
|
||||||
free(pretty_buf);
|
|
||||||
|
|
||||||
printf("%s\n", BTRFS_BUILD_VERSION);
|
printf("%s\n", BTRFS_BUILD_VERSION);
|
||||||
btrfs_commit_transaction(trans, root);
|
btrfs_commit_transaction(trans, root);
|
||||||
|
|
19
utils.c
19
utils.c
|
@ -1152,13 +1152,14 @@ out:
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
|
static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
|
||||||
"PB", "EB", "ZB", "YB"};
|
"PB", "EB"};
|
||||||
char *pretty_sizes(u64 size)
|
void pretty_size_snprintf(u64 size, char *str, size_t str_bytes)
|
||||||
{
|
{
|
||||||
int num_divs = 0;
|
int num_divs = 0;
|
||||||
int pretty_len = 16;
|
|
||||||
float fraction;
|
float fraction;
|
||||||
char *pretty;
|
|
||||||
|
if (str_bytes == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
if( size < 1024 ){
|
if( size < 1024 ){
|
||||||
fraction = size;
|
fraction = size;
|
||||||
|
@ -1172,13 +1173,13 @@ char *pretty_sizes(u64 size)
|
||||||
num_divs ++;
|
num_divs ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (num_divs >= ARRAY_SIZE(size_strs))
|
if (num_divs >= ARRAY_SIZE(size_strs)) {
|
||||||
return NULL;
|
str[0] = '\0';
|
||||||
|
return;
|
||||||
|
}
|
||||||
fraction = (float)last_size / 1024;
|
fraction = (float)last_size / 1024;
|
||||||
}
|
}
|
||||||
pretty = malloc(pretty_len);
|
snprintf(str, str_bytes, "%.2f%s", fraction, size_strs[num_divs]);
|
||||||
snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]);
|
|
||||||
return pretty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
10
utils.h
10
utils.h
|
@ -44,7 +44,15 @@ int check_mounted_where(int fd, const char *file, char *where, int size,
|
||||||
struct btrfs_fs_devices **fs_devices_mnt);
|
struct btrfs_fs_devices **fs_devices_mnt);
|
||||||
int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
|
int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
|
||||||
int super_offset);
|
int super_offset);
|
||||||
char *pretty_sizes(u64 size);
|
|
||||||
|
void pretty_size_snprintf(u64 size, char *str, size_t str_bytes);
|
||||||
|
#define pretty_size(size) \
|
||||||
|
({ \
|
||||||
|
static __thread char _str[24]; \
|
||||||
|
pretty_size_snprintf((size), _str, sizeof(_str)); \
|
||||||
|
_str; \
|
||||||
|
})
|
||||||
|
|
||||||
int get_mountpt(char *dev, char *mntpt, size_t size);
|
int get_mountpt(char *dev, char *mntpt, size_t size);
|
||||||
int btrfs_scan_block_devices(int run_ioctl);
|
int btrfs_scan_block_devices(int run_ioctl);
|
||||||
u64 parse_size(char *s);
|
u64 parse_size(char *s);
|
||||||
|
|
Loading…
Reference in New Issue