zfsonlinux/zfs-patches/0011-Rewrite-of-function-fBytes-in-arc_summary.py.patch
Fabian Grünbichler 6beaed3f99 add remaining zfs-0.7.6 changes as patches
since Debian unstable has not been updated yet.
2018-02-21 09:52:27 +01:00

99 lines
3.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: "Scot W. Stevenson" <scot.stevenson@gmail.com>
Date: Wed, 25 Oct 2017 08:29:02 +0200
Subject: [PATCH] Rewrite of function fBytes() in arc_summary.py
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Replace if-elif-else construction with shorter loop;
remove unused parameter "Decimal"; centralize format
string; add function documentation string; conform to
PEP8.
Reviewed-by: Giuseppe Di Natale <dinatale2@llnl.gov>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Scot W. Stevenson <scot.stevenson@gmail.com>
Closes #6784
(cherry picked from commit 5dc25de668ce8c4e43ebd8c589202392297699b9)
Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
cmd/arc_summary/arc_summary.py | 60 ++++++++++++++++++++++--------------------
1 file changed, 31 insertions(+), 29 deletions(-)
diff --git a/cmd/arc_summary/arc_summary.py b/cmd/arc_summary/arc_summary.py
index 93918a08f..e19278cef 100755
--- a/cmd/arc_summary/arc_summary.py
+++ b/cmd/arc_summary/arc_summary.py
@@ -88,36 +88,38 @@ def div2():
sys.stdout.write("\n")
-def fBytes(Bytes=0, Decimal=2):
- kbytes = (2 ** 10)
- mbytes = (2 ** 20)
- gbytes = (2 ** 30)
- tbytes = (2 ** 40)
- pbytes = (2 ** 50)
- ebytes = (2 ** 60)
- zbytes = (2 ** 70)
- ybytes = (2 ** 80)
-
- if Bytes >= ybytes:
- return str("%0." + str(Decimal) + "f") % (Bytes / ybytes) + "\tYiB"
- elif Bytes >= zbytes:
- return str("%0." + str(Decimal) + "f") % (Bytes / zbytes) + "\tZiB"
- elif Bytes >= ebytes:
- return str("%0." + str(Decimal) + "f") % (Bytes / ebytes) + "\tEiB"
- elif Bytes >= pbytes:
- return str("%0." + str(Decimal) + "f") % (Bytes / pbytes) + "\tPiB"
- elif Bytes >= tbytes:
- return str("%0." + str(Decimal) + "f") % (Bytes / tbytes) + "\tTiB"
- elif Bytes >= gbytes:
- return str("%0." + str(Decimal) + "f") % (Bytes / gbytes) + "\tGiB"
- elif Bytes >= mbytes:
- return str("%0." + str(Decimal) + "f") % (Bytes / mbytes) + "\tMiB"
- elif Bytes >= kbytes:
- return str("%0." + str(Decimal) + "f") % (Bytes / kbytes) + "\tKiB"
- elif Bytes == 0:
- return str("%d" % 0) + "\tBytes"
+def fBytes(b=0):
+ """Return human-readable representation of a byte value in
+ powers of 2 (eg "KiB" for "kibibytes", etc) to two decimal
+ points. Values smaller than one KiB are returned without
+ decimal points.
+ """
+
+ prefixes = [
+ [2**80, "YiB"], # yobibytes (yotta)
+ [2**70, "ZiB"], # zebibytes (zetta)
+ [2**60, "EiB"], # exbibytes (exa)
+ [2**50, "PiB"], # pebibytes (peta)
+ [2**40, "TiB"], # tebibytes (tera)
+ [2**30, "GiB"], # gibibytes (giga)
+ [2**20, "MiB"], # mebibytes (mega)
+ [2**10, "KiB"]] # kibibytes (kilo)
+
+ if b >= 2**10:
+
+ for limit, unit in prefixes:
+
+ if b >= limit:
+ value = b / limit
+ break
+
+ result = "%0.2f\t%s" % (value, unit)
+
else:
- return str("%d" % Bytes) + "\tBytes"
+
+ result = "%d\tBytes" % b
+
+ return result
def fHits(Hits=0, Decimal=2):
--
2.14.2