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
This commit is contained in:
Erin Le
2024-09-05 17:27:59 -07:00
parent e1db67c4bd
commit 00f927a4e2
2 changed files with 51 additions and 14 deletions

View File

@@ -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")

View File

@@ -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")