diff options
| author | Daniel Manrique <roadmr@ubuntu.com> | 2014-09-04 08:14:26 -0400 |
|---|---|---|
| committer | Daniel Manrique <roadmr@ubuntu.com> | 2014-09-04 08:14:26 -0400 |
| commit | c0c2a0f9f5229063cdb92d94493b225c1c62323e (patch) | |
| tree | dac6542a6b2e0658929c421b0d7a489c8a6aedca /bin | |
| parent | 1a97a137beb2cdee94a06905c55d152734f14626 (diff) | |
providers:checkbox: Improved memory size presentation in memory_compare
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/memory_compare | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/bin/memory_compare b/bin/memory_compare index 4143673..2c26f67 100755 --- a/bin/memory_compare +++ b/bin/memory_compare @@ -24,6 +24,7 @@ import os import sys +from math import log, copysign from subprocess import check_output, PIPE from checkbox_support.parsers.lshwjson import LshwJsonParser @@ -81,6 +82,32 @@ def get_threshold(installed_memory): return 10 +def bytes_to_human(my_bytes): + """ Convert my_bytes to a scaled representation with a + suffix + """ + if my_bytes == 0: + return "0 bytes" + + suffixes = ["bytes", "KiB", "MiB", "GiB", "TiB", + "PiB", "EiB", "ZiB", "YiB"] + + try: + sign = copysign(1, my_bytes) + except OverflowError as excp: + return "(Number too large: {})".format(excp) + my_bytes = abs(my_bytes) + # my_bytes' base-1024 logarithm. + exponent = log(my_bytes, 1024) + try: + suffix = suffixes[int(exponent)] + except IndexError: + return "(Number too large)" + scalar = my_bytes / (1024**int(exponent)) + + return "{:.2f} {}".format(sign * scalar, suffix) + + def main(): if os.geteuid() != 0: print("This script must be run as root.", file=sys.stderr) @@ -95,9 +122,11 @@ def main(): percentage = difference / installed_memory * 100 except ZeroDivisionError: print("Results:") - print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024), + print("\t/proc/meminfo reports:\t{}".format( + bytes_to_human(visible_memory)), file=sys.stderr) - print("\tlshw reports:\t%s kB" % (installed_memory / 1024), + print("\tlshw reports:\t{}".format( + bytes_to_human(installed_memory)), file=sys.stderr) print("\nFAIL: Either lshw or /proc/meminfo returned a memory size " "of 0 kB", file=sys.stderr) @@ -105,17 +134,20 @@ def main(): if percentage <= threshold: print("Results:") - print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024)) - print("\tlshw reports:\t%s kB" % (installed_memory / 1024)) - print("\nPASS: Meminfo reports %d bytes less than lshw, a " + print("\t/proc/meminfo reports:\t{}".format( + bytes_to_human(visible_memory))) + print("\tlshw reports:\t{}".format(bytes_to_human(installed_memory))) + print("\nPASS: Meminfo reports %s less than lshw, a " "difference of %.2f%%. This is less than the " - "%d%% variance allowed." % (difference, percentage, threshold)) + "%d%% variance allowed." % (bytes_to_human(difference), + percentage, threshold)) return 0 else: print("Results:", file=sys.stderr) - print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024), + print("\t/proc/meminfo reports:\t{}".format( + bytes_to_human(visible_memory)), file=sys.stderr) - print("\tlshw reports:\t%s kB" % (installed_memory / 1024), + print("\tlshw reports:\t{}".format(bytes_to_human(installed_memory)), file=sys.stderr) print("\nFAIL: Meminfo reports %d bytes less than lshw, " "a difference of %.2f%%. Only a variance of %d%% in " |
