From c0c2a0f9f5229063cdb92d94493b225c1c62323e Mon Sep 17 00:00:00 2001 From: Daniel Manrique Date: Thu, 4 Sep 2014 08:14:26 -0400 Subject: providers:checkbox: Improved memory size presentation in memory_compare --- bin/memory_compare | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'bin') 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 " -- cgit v1.2.3