From 00f927a4e2d402a40f0882f72525c526fcdb727f Mon Sep 17 00:00:00 2001 From: Erin Le Date: Thu, 5 Sep 2024 17:27:59 -0700 Subject: [PATCH] mem, python: refactor error message formatting This commit refactors the error message added to convert.py. A mapping between the base 10 and base 2 suffix magnitudes (e.g. k: ki, M: Mi, etc.) and a new function that extracts the magnitude and numerical value have been added. Also, a warning message has been added to the toMemoryBandwidth function in addition to the one in toMemorySize. Change-Id: I3ae157d13c7089d38a34a6e4c35a2b58978106d0 --- src/mem/CfiMemory.py | 4 +-- src/python/m5/util/convert.py | 61 ++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/mem/CfiMemory.py b/src/mem/CfiMemory.py index 5af534a6a9..063048c592 100644 --- a/src/mem/CfiMemory.py +++ b/src/mem/CfiMemory.py @@ -53,10 +53,10 @@ class CfiMemory(AbstractMemory): latency = Param.Latency("30ns", "Request to response latency") latency_var = Param.Latency("0ns", "Request to response latency variance") - # The memory bandwidth limit default is set to 12.8GB/s which is + # The memory bandwidth limit default is set to 12.8GiB/s which is # representative of a x64 DDR3-1600 channel. bandwidth = Param.MemoryBandwidth( - "12.8GB/s", "Combined read and write bandwidth" + "12.8GiB/s", "Combined read and write bandwidth" ) vendor_id = Param.UInt16(0, "vendor ID") diff --git a/src/python/m5/util/convert.py b/src/python/m5/util/convert.py index f47195bfcf..a4bf7ae95e 100644 --- a/src/python/m5/util/convert.py +++ b/src/python/m5/util/convert.py @@ -102,6 +102,15 @@ binary_prefixes = { "k": kibi, } +base_10_to_2 = { + "k": "Ki", + "M": "Mi", + "G": "Gi", + "T": "Ti", + "P": "Pi", + "E": "Ei", +} + def assertStr(value): if not isinstance(value, str): @@ -124,6 +133,32 @@ def _split_suffix(value, suffixes): return (value[: -len(matches[0])], matches[0]) if matches else (value, "") +def _split_suffix_by_prefix(value): + """Split a string based on a suffix from a list of suffixes. + This is intended to be used with the keys of binary_prefixes. + + :param value: String value to test for a matching magnitude + (e.g. k, M, Gi, etc). + :param suffixes: Container of suffixes to test. + + :returns: A tuple of (value, suffix). Suffix is the empty string + if there is no match. + + """ + matches = [sfx for sfx in binary_prefixes.keys() if sfx in value] + + if len(matches) == 2: # e.g. matches both M and Mi + matches.sort(key=len, reverse=True) + matches.pop() + assert len(matches) <= 1 + + return ( + (value[: value.find(matches[0])], matches[0]) + if matches + else (value, "") + ) + + def toNum(value, target_type, units, prefixes, converter): """Convert a string using units and prefixes to (typically) a float or integer. @@ -260,22 +295,24 @@ def toNetworkBandwidth(value): def toMemoryBandwidth(value): + checkBaseConversion(value, "B/s") return toBinaryFloat(value, "memory bandwidth", "B/s") -def toMemorySize(value): - if ( - type(value) is str - and len(value) > 1 - and value[-2] in binary_prefixes.keys() - and not "i" in value - ): - from m5.util import warn +def checkBaseConversion(value, unit): + if type(value) is str: + size, prefix = _split_suffix_by_prefix(value) + if prefix in base_10_to_2.keys(): + from m5.util import warn - warn( - f"Base 10 memory/cache size {value} will be cast to base 2" - + f" size {value[0:-2]}{value[-2].upper()}i{value[-1]}." - ) + warn( + f"Base 10 memory/cache size {value} will be cast to base 2" + + f" size {size}{base_10_to_2[prefix]}{unit}." + ) + + +def toMemorySize(value): + checkBaseConversion(value, "B") return toBinaryInteger(value, "memory size", "B")