diff options
| author | Daniel Manrique <roadmr@ubuntu.com> | 2014-09-08 14:39:14 +0000 |
|---|---|---|
| committer | Daniel Manrique <> | 2014-09-08 14:39:14 +0000 |
| commit | 313c0432fbbbab8d6d12da2b8b31a3d348f5f930 (patch) | |
| tree | f62393a8ff223a5d2d24cc5d71f99e1e23ff4818 /bin | |
| parent | 7268611e0c568cd0e4eb65ae20b7e1d156d95428 (diff) | |
| parent | c0c2a0f9f5229063cdb92d94493b225c1c62323e (diff) | |
"providers:checkbox: Improved memory size presentation in memory_compare. [r=zkrynicki][bug=1185508][author=roadmr]"
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 " |
