Skip to contentSkip to conversation

Replace the units from KB to KiB..

PATCH In review Goffredo Baroncelli 3 participants 7 messages Oct 13, 2012 – Oct 15, 2012 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Replace the units from KB to KiB..

Hi All,

several people asked to update the units showed by the "btrfs" tool form the SI ones (KB, MB, GB...) to the IEC ones (KiB, MiB...).

The aim of this patch is to allow the user to choice which units are showed. Depending by the BTRFS_UNIT environmental variable, the following units are showed:

BTRFS_UNIT=SI the suffix is KB for 1000bytes, MB for 10^6 bytes... BTRFS_UNIT=IEC the suffix is KiB for 1024bytes, MiB for 1024 KiB ... BTRFS_UNIT=COMPACT the suffix is KB for 1024 bytes, MB for 1024 KiB;
no space between the number and the suffix.

BR
G.Baroncelli

Signed-off-by Goffredo Baroncelli <[email protected]>

#1 lore raw

PATCH 1/3 Add support for different unit.

From: Goffredo Baroncelli <[email protected]>

The function pretty_sizes() returns a string containing the passed number. It add a suffix depending by the number: eg KiB, MiB. This change replace the old SI suffix (KB, MB..) by the IEC ones (KiB, MiB..). Moreover a space is added between the suffix and the number.

Setting opprtunately the enviroment variable BTRFS_UNIT, it is possible to:
BTRFS_UNIT=SI the suffix is KB for 1000bytes, MB for 10^6 bytes... BTRFS_UNIT=IEC the suffix is KiB for 1024bytes, MiB for 1024 KiB ... BTRFS_UNIT=COMPACT the suffix is KB for 1024 bytes, MB for 1024 KiB;
no space between the number and the suffix.

See http://en.wikipedia.org/wiki/Byte for further information about the different suffix.

---
 utils.c |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 9 deletions(-)
utils.c +61 -9
index 205e667..8528cc1 100644
@@ -1085,33 +1085,85 @@ out:
 	return ret;
 }
 
-static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
+static char *size_strs_inf[] = { "", "KiB", "MiB", "GiB", "TiB",
+			    "PiB", "EiB", "ZiB", "YiB"};
+static char *size_strs_dec[] = { "", "KB", "MB", "GB", "TB",
 			    "PB", "EB", "ZB", "YB"};
+
+static int which_unit( ){
+	static int unit=-1;
+	char	*u;
+
+	if( unit != -1 )
+		return unit;
+
+	unit = 0;
+
+	u =getenv("BTRFS_UNIT");
+	if(!u) return 0;
+
+	if( !strcmp(u,"SI") )
+		unit = 1;
+	else if( !strcmp(u, "COMPACT") )
+		unit = 2;
+	else if( !strcmp(u, "IEC") )
+		unit = 0;
+	/* else
+		Shall we raise an error ? 
+	*/
+		
+	return unit;
+
+}
+
+
 char *pretty_sizes(u64 size)
 {
 	int num_divs = 0;
-        int pretty_len = 16;
+        int pretty_len = 20;
 	float fraction;
-	char *pretty;
+	char *pretty, *space;
+	int  shift = 1024;
+	char **size_strs;
+
+	if( which_unit() == 1 ){		/* SI */
+		shift = 1000;
+		size_strs = size_strs_dec;
+		space = " ";
+	} else if( which_unit() == 2 ){		/* Old method:
+						   SI suffix, but 
+						   multiply of 1024 */
+		shift = 1024;
+		size_strs = size_strs_dec;
+		space = "";
+	}else{
+		shift = 1024;			/* IEC */
+		size_strs = size_strs_inf;
+		space = " ";
+	}
 
-	if( size < 1024 ){
+	if( size < shift ){
 		fraction = size;
 		num_divs = 0;
 	} else {
 		u64 last_size = size;
 		num_divs = 0;
-		while(size >= 1024){
+		while(size >= shift){
 			last_size = size;
-			size /= 1024;
+			size /= shift;
 			num_divs ++;
 		}
 
-		if (num_divs > ARRAY_SIZE(size_strs))
+		if (num_divs > ARRAY_SIZE(size_strs_inf))
 			return NULL;
-		fraction = (float)last_size / 1024;
+		fraction = (float)last_size / shift;
 	}
 	pretty = malloc(pretty_len);
-	snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]);
+
+	snprintf(pretty, pretty_len, "%.2f%s%s", 
+		fraction, 
+		space,
+		size_strs[num_divs]);
 	return pretty;
 }
 
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 2/3 Deleted the byte prefix with pretty_sizes().

In reply to Goffredo Baroncelli

From: Goffredo Baroncelli <[email protected]>

When the function pretty_sizes() is used, the word "bytes" must avoided.

---
 cmds-filesystem.c |    2 +-
 cmds-scrub.c      |    8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
cmds-filesystem.c +1 -1
index 9c43d35..21cdd7f 100644
@@ -202,7 +202,7 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
 	super_bytes_used = pretty_sizes(device->super_bytes_used);
 
 	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 space used %s\n", uuidbuf,
 	       (unsigned long long)total, super_bytes_used);
 
 	free(super_bytes_used);
cmds-scrub.c +4 -4
index 24be20f..69dfa7e 100644
@@ -125,7 +125,7 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p)
 {
 	u64 err_cnt;
 	u64 err_cnt2;
-	char *bytes;
+	char *total;
 
 	err_cnt = p->read_errors +
 			p->csum_errors +
@@ -137,10 +137,10 @@ static void print_scrub_summary(struct btrfs_scrub_progress *p)
 	if (p->malloc_errors)
 		printf("*** WARNING: memory allocation failed while scrubbing. "
 		       "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,
+	total = pretty_sizes(p->data_bytes_scrubbed + p->tree_bytes_scrubbed);
+	printf("\ttotal scrubbed: %s with %llu errors\n", total,
 		max(err_cnt, err_cnt2));
-	free(bytes);
+	free(total);
 	if (err_cnt || err_cnt2) {
 		printf("\terror details:");
 		PRINT_SCRUB_ERROR(p->read_errors, "read");
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH 3/3 Document the use of BTRFS_UNIT in man page.

In reply to Goffredo Baroncelli

From: Goffredo Baroncelli <[email protected]>

---
 man/btrfs.8.in |   14 ++++++++++++++
 1 file changed, 14 insertions(+)
man/btrfs.8.in +14 -0
index 9222580..fe7a5e3 100644
@@ -355,6 +355,20 @@ not enough to read all the resolved results. The max value one can set is 64k.
 \fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in
 case of failure.
 
+.SH ENVIRONMENTAL VARIABLES
+\fBBTRFS_UNIT\fR set the kind of suffix for the number output by \fBbtrfs\fR. 
+Possible values:
+.RS
+.IP BTRFS_UNIT=IEC
+The number have the IEC suffix: e.g. KiB = 1024 bytes, MiB = 1024 KiB...
+.IP BTRFS_UNIT=SI 
+The number have the SI suffix: e.g. KB = 1000 bytes, MB = 1000 KB...
+.IP BTRFS_UNIT=COMPACT
+For compatibility; the number have the SI suffix but the unit is multiply
+of power of two. E.g. KB = 1024 bytes, MB = 1024 KiB...
+There is no space between the number and the suffix.
+.RE
+
 .SH AVAILABILITY
 .B btrfs
 is part of btrfs-progs. Btrfs filesystem is currently under heavy development,
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH 1/3] Add support for different unit.

In reply to Goffredo Baroncelli

Am Samstag, 13. Oktober 2012 schrieb Goffredo Baroncelli:

Quoted 9 lines

From: Goffredo Baroncelli <[email protected]>

The function pretty_sizes() returns a string containing the passed number. It add a suffix depending by the number: eg KiB, MiB. This change replace the old SI suffix (KB, MB..) by the IEC ones (KiB, MiB..). Moreover a space is added between the suffix and the number.

Setting opprtunately the enviroment variable BTRFS_UNIT, it is

opprtunately => how its written without typo :)

(I didn´t find it in dict.leo.org, maybe due to my writing of it is wrong as well)

Anyway pretty minor.

And thanks for the patches to improve size reporting. I try to give them a test this week and unit setting.

Quoted 112 lines

possible to:
BTRFS_UNIT=SI the suffix is KB for 1000bytes, MB for 10^6 bytes... BTRFS_UNIT=IEC the suffix is KiB for 1024bytes, MiB for 1024 KiB ... BTRFS_UNIT=COMPACT the suffix is KB for 1024 bytes, MB for 1024 KiB;
no space between the number and the suffix.

See http://en.wikipedia.org/wiki/Byte for further information about the different suffix.

---
 utils.c |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 9 deletions(-)
utils.c +61 -9
index 205e667..8528cc1 100644
@@ -1085,33 +1085,85 @@ out:
 	return ret;
 }
 
-static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
+static char *size_strs_inf[] = { "", "KiB", "MiB", "GiB", "TiB",
+			    "PiB", "EiB", "ZiB", "YiB"};
+static char *size_strs_dec[] = { "", "KB", "MB", "GB", "TB",
 			    "PB", "EB", "ZB", "YB"};
+
+static int which_unit( ){
+	static int unit=-1;
+	char	*u;
+
+	if( unit != -1 )
+		return unit;
+
+	unit = 0;
+
+	u =getenv("BTRFS_UNIT");
+	if(!u) return 0;
+
+	if( !strcmp(u,"SI") )
+		unit = 1;
+	else if( !strcmp(u, "COMPACT") )
+		unit = 2;
+	else if( !strcmp(u, "IEC") )
+		unit = 0;
+	/* else
+		Shall we raise an error ? 
+	*/
+		
+	return unit;
+
+}
+
+
 char *pretty_sizes(u64 size)
 {
 	int num_divs = 0;
-        int pretty_len = 16;
+        int pretty_len = 20;
 	float fraction;
-	char *pretty;
+	char *pretty, *space;
+	int  shift = 1024;
+	char **size_strs;
+
+	if( which_unit() == 1 ){		/* SI */
+		shift = 1000;
+		size_strs = size_strs_dec;
+		space = " ";
+	} else if( which_unit() == 2 ){		/* Old method:
+						   SI suffix, but 
+						   multiply of 1024 */
+		shift = 1024;
+		size_strs = size_strs_dec;
+		space = "";
+	}else{
+		shift = 1024;			/* IEC */
+		size_strs = size_strs_inf;
+		space = " ";
+	}
 
-	if( size < 1024 ){
+	if( size < shift ){
 		fraction = size;
 		num_divs = 0;
 	} else {
 		u64 last_size = size;
 		num_divs = 0;
-		while(size >= 1024){
+		while(size >= shift){
 			last_size = size;
-			size /= 1024;
+			size /= shift;
 			num_divs ++;
 		}
 
-		if (num_divs > ARRAY_SIZE(size_strs))
+		if (num_divs > ARRAY_SIZE(size_strs_inf))
 			return NULL;
-		fraction = (float)last_size / 1024;
+		fraction = (float)last_size / shift;
 	}
 	pretty = malloc(pretty_len);
-	snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]);
+
+	snprintf(pretty, pretty_len, "%.2f%s%s", 
+		fraction, 
+		space,
+		size_strs[num_divs]);
 	return pretty;
 }
 
Signature · 2 lines
Martin 'Helios' Steigerwald - http://www.Lichtvoll.de
GPG: 03B0 0D6C 0040 0710 4AFA  B82F 991B EAAC A599 84C7
#5 lore raw
In reply to Goffredo Baroncelli

Am Samstag, 13. Oktober 2012 schrieb Goffredo Baroncelli:

Quoted 9 lines

Hi All,

several people asked to update the units showed by the "btrfs" tool form the SI ones (KB, MB, GB...) to the IEC ones (KiB, MiB...).

The aim of this patch is to allow the user to choice which units are showed. Depending by the BTRFS_UNIT environmental variable, the following units are showed:

Quoted 7 lines

BTRFS_UNIT=SI the suffix is KB for 1000bytes, MB for 10^6 bytes... BTRFS_UNIT=IEC the suffix is KiB for 1024bytes, MiB for 1024 KiB ... BTRFS_UNIT=COMPACT the suffix is KB for 1024 bytes, MB for 1024 KiB;
no space between the number and the suffix.

BR
G.Baroncelli

What base does this build upon?

I tried to apply upon your disk_free branch, but got:

martin@merkaba:~/Linux/Dateisysteme/BTRFS/btrfs-progs-unstable> patch -p1 < ../\[PATCH\]\[V1\]\[BTRFS-PROGS\]\ Replace\ the\ units\ from\ KB\ to\ KiB...mbox patching file utils.c
Hunk #1 FAILED at 1085.
1 out of 1 hunk FAILED -- saving rejects to file utils.c.rej
patching file cmds-filesystem.c
Hunk #1 succeeded at 366 (offset 164 lines).
patching file cmds-scrub.c
patching file man/btrfs.8.in
Hunk #1 succeeded at 424 (offset 69 lines).

Thanks,

Signature · 2 lines
Martin 'Helios' Steigerwald - http://www.Lichtvoll.de
GPG: 03B0 0D6C 0040 0710 4AFA  B82F 991B EAAC A599 84C7
#6 lore raw

Hi Martin,

thanks for your testing.
My patches are based on the Chris's master branch (git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-progs.git, commit 91d9eec1ff044394f2b98ee7fcb76713dd33b994).

I was able to apply both without problem.
disk_free cannot be the source of the problem because it doesn't touch utils.c.

I tryed to pull from my repo,

ghigo@venice:/tmp$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-progs.git
Cloning into 'btrfs-progs'...
remote: Counting objects: 2692, done.
remote: Compressing objects: 100% (894/894), done.
remote: Total 2692 (delta 2015), reused 2380 (delta 1794)
Receiving objects: 100% (2692/2692), 819.72 KiB | 83 KiB/s, done.
Resolving deltas: 100% (2015/2015), done.
ghigo@venice:/tmp$ cd btrfs-progs/
ghigo@venice:/tmp/btrfs-progs$ git fetch http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git unit:unit
ghigo@venice:/tmp/btrfs-progs$ git fetch http://cassiopea.homelinux.net/git/btrfs-progs-unstable.git disk_free:disk_free
From http://cassiopea.homelinux.net/git/btrfs-progs-unstable
 * [new branch]      disk_free  -> disk_free
ghigo@venice:/tmp/btrfs-progs$ git branch
  disk_free
* master
  unit
ghigo@venice:/tmp/btrfs-progs$ git merge -m "Merge branches" disk_free unit
Fast-forwarding to: disk_free
Trying simple merge with unit
Simple merge did not work, trying automatic merge.
Auto-merging cmds-filesystem.c
Auto-merging man/btrfs.8.in
Merge made by the 'octopus' strategy.
 cmds-filesystem.c |  313 
++++++++++++++++++++++++++++++++++++++++++++---------
 cmds-scrub.c      |    8 +-
 man/btrfs.8.in    |  114 +++++++++++++++++++
 utils.c           |   70 ++++++++++--
 4 files changed, 440 insertions(+), 65 deletions(-)

Finally I tried with the email patches:

ghigo@venice:/tmp$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-progs.git
Cloning into 'btrfs-progs'...
remote: Counting objects: 2692, done.
remote: Compressing objects: 100% (894/894), done.
remote: Total 2692 (delta 2015), reused 2380 (delta 1794)
Receiving objects: 100% (2692/2692), 818.83 KiB | 101 KiB/s, done.
Resolving deltas: 100% (2015/2015), done.
ghigo@venice:/tmp$ ls *.eml
[PATCH 1_2] Update btrfs filesystem df command.eml
[PATCH 1_3] Add support for different unit. - Goffredo Baroncelli <[email protected]> - 2012-10-13 2127.eml
[PATCH 2_2] Update help page.eml
[PATCH 2_3] Deleted the byte prefix with pretty_sizes(). - Goffredo Baroncelli <[email protected]> - 2012-10-13 2127.eml
[PATCH 3_3] Document the use of BTRFS_UNIT in man page. - Goffredo Baroncelli <[email protected]> - 2012-10-13 2127.eml
ghigo@venice:/tmp$ cd btrfs-progs/
ghigo@venice:/tmp/btrfs-progs$ patch -p1 <../\[PATCH\ 1_2\]\ Update\ btrfs\ filesystem\ df\ command.eml
(Stripping trailing CRs from patch.)
patching file cmds-filesystem.c
ghigo@venice:/tmp/btrfs-progs$ patch -p1 <../\[PATCH\ 2_2\]\ Update\ help\ page.eml
(Stripping trailing CRs from patch.)
patching file man/btrfs.8.in
ghigo@venice:/tmp/btrfs-progs$ patch -p1 <../\[PATCH\ 1_3\]\ Add\ support\ for\ different\ unit.\ -\ Goffredo\ Baroncelli\ \<kreijack\@gmail.com\>\ -\ 2012-10-13\ 2127.eml
(Stripping trailing CRs from patch.)
patching file utils.c
ghigo@venice:/tmp/btrfs-progs$ patch -p1 <../\[PATCH\ 2_3\]\ Deleted\ the\ byte\ prefix\ with\ pretty_sizes\(\).\ -\ Goffredo\ Baroncelli\ \<kreijack\@gmail.com\>\ -\ 2012-10-13\ 2127.eml
(Stripping trailing CRs from patch.)
patching file cmds-filesystem.c
Hunk #1 succeeded at 411 (offset 209 lines).
(Stripping trailing CRs from patch.)
patching file cmds-scrub.c
ghigo@venice:/tmp/btrfs-progs$ patch -p1 <../\[PATCH\ 3_3\]\ Document\ the\ use\ of\ BTRFS_UNIT\ in\ man\ page.\ -\ Goffredo\ Baroncelli\ \<kreijack\@gmail.com\>\ -\ 2012-10-13\ 2127.eml
(Stripping trailing CRs from patch.)
patching file man/btrfs.8.in
Hunk #1 succeeded at 455 (offset 100 lines).

I have no idea on what happened...

BR
Goffredo

Quoted 32 lines · Martin Steigerwald

Am Samstag, 13. Oktober 2012 schrieb Goffredo Baroncelli:

Quoted 9 lines

Hi All,

several people asked to update the units showed by the "btrfs" tool form the SI ones (KB, MB, GB...) to the IEC ones (KiB, MiB...).

The aim of this patch is to allow the user to choice which units are showed. Depending by the BTRFS_UNIT environmental variable, the following units are showed:

Quoted 7 lines

BTRFS_UNIT=SI the suffix is KB for 1000bytes, MB for 10^6 bytes... BTRFS_UNIT=IEC the suffix is KiB for 1024bytes, MiB for 1024 KiB ... BTRFS_UNIT=COMPACT the suffix is KB for 1024 bytes, MB for 1024 KiB;
no space between the number and the suffix.

BR
G.Baroncelli

What base does this build upon?

I tried to apply upon your disk_free branch, but got:

martin@merkaba:~/Linux/Dateisysteme/BTRFS/btrfs-progs-unstable> patch -p1< ../\[PATCH\]\[V1\]\[BTRFS-PROGS\]\ Replace\ the\ units\ from\ KB\ to\ KiB...mbox patching file utils.c
Hunk #1 FAILED at 1085.
1 out of 1 hunk FAILED -- saving rejects to file utils.c.rej
patching file cmds-filesystem.c
Hunk #1 succeeded at 366 (offset 164 lines).
patching file cmds-scrub.c
patching file man/btrfs.8.in
Hunk #1 succeeded at 424 (offset 69 lines).

Thanks,