Skip to contentSkip to conversation

btrfs-progs: print procentage of used space

PATCH In review Stanislaw Gruszka 2 participants 4 messages Aug 29, 2019 – Nov 19, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: print procentage of used space

This patch adds -p option for 'fi df' and 'fi show' commands to print procentate of used space. Output with the option will look like on example below:

Data, single: total=43.99GiB, used=37.25GiB (84.7%) System, single: total=4.00MiB, used=12.00KiB (0.3%) Metadata, single: total=1.01GiB, used=511.23MiB (49.5%) GlobalReserve, single: total=92.50MiB, used=0.00B (0.0%)

I considered to change the prints by default without extra option, but not sure if that would not break existing scripts that could parse the output.

---
 cmds/filesystem.c | 55 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 45 insertions(+), 10 deletions(-)
cmds/filesystem.c +45 -10
index 4f22089abeaa..c25301aa0df9 100644
@@ -61,20 +61,39 @@ static const char * const cmd_filesystem_df_usage[] = {
 	"Show space usage information for a mount point",
 	"",
 	HELPINFO_UNITS_SHORT_LONG,
+	"-p                 show procentage of disk usage",
 	NULL
 };
 
-static void print_df(struct btrfs_ioctl_space_args *sargs, unsigned unit_mode)
+static char *usage_procentage(struct btrfs_ioctl_space_info *sp, int p)
+{
+	static char __thread s[12];
+	float procentage;
+
+	if (p) {
+		ASSERT(sp->total_bytes);
+		procentage = 100.0*((float) sp->used_bytes / sp->total_bytes);
+		snprintf(s, 12, " (%.1f%)", procentage);
+	} else {
+		s[0] = '\0';
+	}
+
+	return s;
+}
+
+static void print_df(struct btrfs_ioctl_space_args *sargs, unsigned unit_mode,
+		     int p)
 {
 	u64 i;
 	struct btrfs_ioctl_space_info *sp = sargs->spaces;
 
 	for (i = 0; i < sargs->total_spaces; i++, sp++) {
-		printf("%s, %s: total=%s, used=%s\n",
+		printf("%s, %s: total=%s, used=%s%s\n",
 			btrfs_group_type_str(sp->flags),
 			btrfs_group_profile_str(sp->flags),
 			pretty_size_mode(sp->total_bytes, unit_mode),
-			pretty_size_mode(sp->used_bytes, unit_mode));
+			pretty_size_mode(sp->used_bytes, unit_mode),
+			usage_procentage(sp, p));
 	}
 }
 
@@ -84,12 +103,17 @@ static int cmd_filesystem_df(const struct cmd_struct *cmd,
 	struct btrfs_ioctl_space_args *sargs = NULL;
 	int ret;
 	int fd;
+	int p;
 	char *path;
 	DIR *dirstream = NULL;
 	unsigned unit_mode;
 
 	unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
 
+	p = getopt(argc, argv, "p");
+	if (p != 'p')
+		p = 0;
+
 	clean_args_no_options(cmd, argc, argv);
 
 	if (check_argc_exact(argc - optind, 1))
@@ -104,7 +128,7 @@ static int cmd_filesystem_df(const struct cmd_struct *cmd,
 	ret = get_df(fd, &sargs);
 
 	if (ret == 0) {
-		print_df(sargs, unit_mode);
+		print_df(sargs, unit_mode, p);
 		free(sargs);
 	} else {
 		errno = -ret;
@@ -280,7 +304,7 @@ static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
 		struct btrfs_ioctl_dev_info_args *dev_info,
 		struct btrfs_ioctl_space_args *space_info,
-		char *label, unsigned unit_mode)
+		char *label, unsigned unit_mode, int p)
 {
 	int i;
 	int fd;
@@ -308,6 +332,7 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
 
 	for (i = 0; i < fs_info->num_devices; i++) {
 		char *canonical_path;
+		struct btrfs_ioctl_space_info tmp_sp;
 
 		tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
 
@@ -319,10 +344,15 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
 		}
 		close(fd);
 		canonical_path = canonicalize_path((char *)tmp_dev_info->path);
-		printf("\tdevid %4llu size %s used %s path %s\n",
+
+		tmp_sp.total_bytes = tmp_dev_info->total_bytes;
+		tmp_sp.used_bytes = tmp_dev_info->bytes_used;
+
+		printf("\tdevid %4llu size %s used %s%s path %s\n",
 			tmp_dev_info->devid,
 			pretty_size_mode(tmp_dev_info->total_bytes, unit_mode),
 			pretty_size_mode(tmp_dev_info->bytes_used, unit_mode),
+			usage_procentage(&tmp_sp, p),
 			canonical_path);
 
 		free(canonical_path);
@@ -334,7 +364,7 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
 	return 0;
 }
 
-static int btrfs_scan_kernel(void *search, unsigned unit_mode)
+static int btrfs_scan_kernel(void *search, unsigned unit_mode, int p)
 {
 	int ret = 0, fd;
 	int found = 0;
@@ -381,7 +411,7 @@ static int btrfs_scan_kernel(void *search, unsigned unit_mode)
 		fd = open(mnt->mnt_dir, O_RDONLY);
 		if ((fd != -1) && !get_df(fd, &space_info_arg)) {
 			print_one_fs(&fs_info_arg, dev_info_arg,
-				     space_info_arg, label, unit_mode);
+				     space_info_arg, label, unit_mode, p);
 			free(space_info_arg);
 			memset(label, 0, sizeof(label));
 			found = 1;
@@ -630,6 +660,7 @@ static const char * const cmd_filesystem_show_usage[] = {
 	"-d|--all-devices   show only disks under /dev containing btrfs filesystem",
 	"-m|--mounted       show only mounted btrfs",
 	HELPINFO_UNITS_LONG,
+	"-p                 show procentage of disk usage",
 	"If no argument is given, structure of all present filesystems is shown.",
 	NULL
 };
@@ -644,6 +675,7 @@ static int cmd_filesystem_show(const struct cmd_struct *cmd,
 	/* default, search both kernel and udev */
 	int where = -1;
 	int type = 0;
+	int p = 0;
 	char mp[PATH_MAX];
 	char path[PATH_MAX];
 	u8 fsid[BTRFS_FSID_SIZE];
@@ -662,7 +694,7 @@ static int cmd_filesystem_show(const struct cmd_struct *cmd,
 			{ NULL, 0, NULL, 0 }
 		};
 
-		c = getopt_long(argc, argv, "dm", long_options, NULL);
+		c = getopt_long(argc, argv, "dmp", long_options, NULL);
 		if (c < 0)
 			break;
 		switch (c) {
@@ -672,6 +704,9 @@ static int cmd_filesystem_show(const struct cmd_struct *cmd,
 		case 'm':
 			where = BTRFS_SCAN_MOUNTED;
 			break;
+		case 'p':
+			p = 1;
+			break;
 		default:
 			usage_unknown_option(cmd, argv);
 		}
@@ -725,7 +760,7 @@ static int cmd_filesystem_show(const struct cmd_struct *cmd,
 		goto devs_only;
 
 	/* show mounted btrfs */
-	ret = btrfs_scan_kernel(search, unit_mode);
+	ret = btrfs_scan_kernel(search, unit_mode, p);
 	if (search && !ret) {
 		/* since search is found we are done */
 		goto out;
Signature · 1 line
1.9.3
#1 lore raw
Quoted 4 lines · Stanislaw Gruszka

This patch adds -p option for 'fi df' and 'fi show' commands to print procentate of used space. Output with the option will look like on example below:

Quoted 8 lines

Data, single: total=43.99GiB, used=37.25GiB (84.7%) System, single: total=4.00MiB, used=12.00KiB (0.3%) Metadata, single: total=1.01GiB, used=511.23MiB (49.5%) GlobalReserve, single: total=92.50MiB, used=0.00B (0.0%)

I considered to change the prints by default without extra option, but not sure if that would not break existing scripts that could parse the output.

Would it be sufficient for your usecase to print the % values in the 'btrfs fi usage' command?

After the summary, the's listing of the chunks with the same values and slightly different format:

Data,single: Size:296.00GiB, Used:166.99GiB

Metadata,single: Size:5.00GiB, Used:2.74GiB

System,single: Size:32.00MiB, Used:48.00KiB

For reference, this is from 'fi df':

Data, single: total=296.00GiB, used=166.99GiB System, single: total=32.00MiB, used=48.00KiB Metadata, single: total=5.00GiB, used=2.74GiB GlobalReserve, single: total=368.48MiB, used=0.00B

I'd rather not extend the output of 'fi df' as this was supposed to be a debugging tool and 'fi usage' is gives better and more complete overview, so the % seem more logical to be put there.

#2 lore raw
Quoted 29 lines · David Sterba
Quoted 4 lines · Stanislaw Gruszka

This patch adds -p option for 'fi df' and 'fi show' commands to print procentate of used space. Output with the option will look like on example below:

Quoted 8 lines

Data, single: total=43.99GiB, used=37.25GiB (84.7%) System, single: total=4.00MiB, used=12.00KiB (0.3%) Metadata, single: total=1.01GiB, used=511.23MiB (49.5%) GlobalReserve, single: total=92.50MiB, used=0.00B (0.0%)

I considered to change the prints by default without extra option, but not sure if that would not break existing scripts that could parse the output.

Would it be sufficient for your usecase to print the % values in the 'btrfs fi usage' command?

After the summary, the's listing of the chunks with the same values and slightly different format:

Data,single: Size:296.00GiB, Used:166.99GiB

Metadata,single: Size:5.00GiB, Used:2.74GiB

System,single: Size:32.00MiB, Used:48.00KiB

For reference, this is from 'fi df':

Quoted 8 lines

Data, single: total=296.00GiB, used=166.99GiB System, single: total=32.00MiB, used=48.00KiB Metadata, single: total=5.00GiB, used=2.74GiB GlobalReserve, single: total=368.48MiB, used=0.00B

I'd rather not extend the output of 'fi df' as this was supposed to be a debugging tool and 'fi usage' is gives better and more complete overview, so the % seem more logical to be put there.

It would be sufficient for me to print percentage in 'fi usage' command. I assume without extra -p option, just change the default output ?

What you think about adding percentage option to 'fi show'? It's not super needed, but I somehow got use to have that information from standard df tool.

Stanislaw

#3 lore raw

Hi,

sorry, I lost track of this patch.

Quoted 35 lines · Stanislaw Gruszka
Quoted 34 lines · David Sterba
Quoted 4 lines · Stanislaw Gruszka

This patch adds -p option for 'fi df' and 'fi show' commands to print procentate of used space. Output with the option will look like on example below:

Quoted 8 lines

Data, single: total=43.99GiB, used=37.25GiB (84.7%) System, single: total=4.00MiB, used=12.00KiB (0.3%) Metadata, single: total=1.01GiB, used=511.23MiB (49.5%) GlobalReserve, single: total=92.50MiB, used=0.00B (0.0%)

I considered to change the prints by default without extra option, but not sure if that would not break existing scripts that could parse the output.

Would it be sufficient for your usecase to print the % values in the 'btrfs fi usage' command?

After the summary, the's listing of the chunks with the same values and slightly different format:

Data,single: Size:296.00GiB, Used:166.99GiB

Metadata,single: Size:5.00GiB, Used:2.74GiB

System,single: Size:32.00MiB, Used:48.00KiB

For reference, this is from 'fi df':

Data, single: total=296.00GiB, used=166.99GiB System, single: total=32.00MiB, used=48.00KiB Metadata, single: total=5.00GiB, used=2.74GiB GlobalReserve, single: total=368.48MiB, used=0.00B

Quoted 6 lines
Quoted 3 lines

I'd rather not extend the output of 'fi df' as this was supposed to be a debugging tool and 'fi usage' is gives better and more complete overview, so the % seem more logical to be put there.

It would be sufficient for me to print percentage in 'fi usage' command. I assume without extra -p option, just change the default output ?

Yes, it will print the percentage by default, like this:

Data,single: Size:339.00GiB, Used:172.05GiB (50.75%)

Metadata,single: Size:7.00GiB, Used:3.41GiB (48.70%)

System,single: Size:32.00MiB, Used:64.00KiB (0.20%)

Quoted 3 lines

What you think about adding percentage option to 'fi show'? It's not super needed, but I somehow got use to have that information from standard df tool.

For the 'fi show' output it looks a bit risky to add the number there. I know the output is parsed by scripts so this can break things. OTOH somethimes we need to do such things so it is possible to enhance the output. I'd rather group that with other changes so for now I'll apply only the 'fi us' part.