python: Apply Black formatter to Python files
The command executed was `black src configs tests util`. Change-Id: I8dfaa6ab04658fea37618127d6ac19270028d771 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/47024 Maintainer: Bobby Bruce <bbruce@ucdavis.edu> Reviewed-by: Jason Lowe-Power <power.jg@gmail.com> Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
committed by
Giacomo Travaglini
parent
1cfaa8da83
commit
787204c92d
@@ -53,12 +53,13 @@ outputList = []
|
||||
|
||||
# Dictionary of stat visitor factories populated by the _url_factory
|
||||
# visitor.
|
||||
factories = { }
|
||||
factories = {}
|
||||
|
||||
# List of all factories. Contains tuples of (factory, schemes,
|
||||
# enabled).
|
||||
all_factories = []
|
||||
|
||||
|
||||
def _url_factory(schemes, enable=True):
|
||||
"""Wrap a plain Python function with URL parsing helpers
|
||||
|
||||
@@ -101,19 +102,23 @@ def _url_factory(schemes, enable=True):
|
||||
# values into proper Python types.
|
||||
def parse_value(key, values):
|
||||
if len(values) == 0 or (len(values) == 1 and not values[0]):
|
||||
fatal("%s: '%s' doesn't have a value." % (
|
||||
url.geturl(), key))
|
||||
fatal(
|
||||
"%s: '%s' doesn't have a value." % (url.geturl(), key)
|
||||
)
|
||||
elif len(values) > 1:
|
||||
fatal("%s: '%s' has multiple values." % (
|
||||
url.geturl(), key))
|
||||
fatal(
|
||||
"%s: '%s' has multiple values." % (url.geturl(), key)
|
||||
)
|
||||
else:
|
||||
try:
|
||||
return key, literal_eval(values[0])
|
||||
except ValueError:
|
||||
fatal("%s: %s isn't a valid Python literal" \
|
||||
% (url.geturl(), values[0]))
|
||||
fatal(
|
||||
"%s: %s isn't a valid Python literal"
|
||||
% (url.geturl(), values[0])
|
||||
)
|
||||
|
||||
kwargs = dict([ parse_value(k, v) for k, v in qs.items() ])
|
||||
kwargs = dict([parse_value(k, v) for k, v in qs.items()])
|
||||
|
||||
try:
|
||||
return func("%s%s" % (url.netloc, url.path), **kwargs)
|
||||
@@ -128,7 +133,8 @@ def _url_factory(schemes, enable=True):
|
||||
|
||||
return decorator
|
||||
|
||||
@_url_factory([ None, "", "text", "file", ])
|
||||
|
||||
@_url_factory([None, "", "text", "file"])
|
||||
def _textFactory(fn, desc=True, spaces=True):
|
||||
"""Output stats in text format.
|
||||
|
||||
@@ -147,7 +153,8 @@ def _textFactory(fn, desc=True, spaces=True):
|
||||
|
||||
return _m5.stats.initText(fn, desc, spaces)
|
||||
|
||||
@_url_factory([ "h5", ], enable=hasattr(_m5.stats, "initHDF5"))
|
||||
|
||||
@_url_factory(["h5"], enable=hasattr(_m5.stats, "initHDF5"))
|
||||
def _hdf5Factory(fn, chunking=10, desc=True, formulas=True):
|
||||
"""Output stats in HDF5 format.
|
||||
|
||||
@@ -183,6 +190,7 @@ def _hdf5Factory(fn, chunking=10, desc=True, formulas=True):
|
||||
|
||||
return _m5.stats.initHDF5(fn, chunking, desc, formulas)
|
||||
|
||||
|
||||
@_url_factory(["json"])
|
||||
def _jsonFactory(fn):
|
||||
"""Output stats in JSON format.
|
||||
@@ -194,6 +202,7 @@ def _jsonFactory(fn):
|
||||
|
||||
return JsonOutputVistor(fn)
|
||||
|
||||
|
||||
def addStatVisitor(url):
|
||||
"""Add a stat visitor specified using a URL string
|
||||
|
||||
@@ -225,6 +234,7 @@ def addStatVisitor(url):
|
||||
|
||||
outputList.append(factory(parsed))
|
||||
|
||||
|
||||
def printStatVisitorTypes():
|
||||
"""List available stat visitors and their documentation"""
|
||||
|
||||
@@ -235,17 +245,19 @@ def printStatVisitorTypes():
|
||||
print("| %s" % line)
|
||||
print()
|
||||
|
||||
enabled_visitors = [ x for x in all_factories if x[2] ]
|
||||
enabled_visitors = [x for x in all_factories if x[2]]
|
||||
for factory, schemes, _ in enabled_visitors:
|
||||
print("%s:" % ", ".join(filter(lambda x: x is not None, schemes)))
|
||||
|
||||
# Try to extract the factory doc string
|
||||
print_doc(inspect.getdoc(factory))
|
||||
|
||||
|
||||
def initSimStats():
|
||||
_m5.stats.initSimStats()
|
||||
_m5.stats.registerPythonStatsHandlers()
|
||||
|
||||
|
||||
def _visit_groups(visitor, root=None):
|
||||
if root is None:
|
||||
root = Root.getInstance()
|
||||
@@ -253,12 +265,15 @@ def _visit_groups(visitor, root=None):
|
||||
visitor(group)
|
||||
_visit_groups(visitor, root=group)
|
||||
|
||||
|
||||
def _visit_stats(visitor, root=None):
|
||||
def for_each_stat(g):
|
||||
for stat in g.getStats():
|
||||
visitor(g, stat)
|
||||
|
||||
_visit_groups(for_each_stat, root=root)
|
||||
|
||||
|
||||
def _bindStatHierarchy(root):
|
||||
def _bind_obj(name, obj):
|
||||
if isNullPointer(obj):
|
||||
@@ -278,33 +293,39 @@ def _bindStatHierarchy(root):
|
||||
if isinstance(obj.getCCObject(), _m5.stats.Group):
|
||||
parent = root
|
||||
while parent:
|
||||
if hasattr(parent, 'addStatGroup'):
|
||||
if hasattr(parent, "addStatGroup"):
|
||||
parent.addStatGroup(name, obj.getCCObject())
|
||||
break
|
||||
parent = parent.get_parent();
|
||||
parent = parent.get_parent()
|
||||
|
||||
_bindStatHierarchy(obj)
|
||||
|
||||
for name, obj in root._children.items():
|
||||
_bind_obj(name, obj)
|
||||
|
||||
|
||||
names = []
|
||||
stats_dict = {}
|
||||
stats_list = []
|
||||
|
||||
|
||||
def enable():
|
||||
'''Enable the statistics package. Before the statistics package is
|
||||
"""Enable the statistics package. Before the statistics package is
|
||||
enabled, all statistics must be created and initialized and once
|
||||
the package is enabled, no more statistics can be created.'''
|
||||
the package is enabled, no more statistics can be created."""
|
||||
|
||||
def check_stat(group, stat):
|
||||
if not stat.check() or not stat.baseCheck():
|
||||
fatal("statistic '%s' (%d) was not properly initialized " \
|
||||
"by a regStats() function\n", stat.name, stat.id)
|
||||
fatal(
|
||||
"statistic '%s' (%d) was not properly initialized "
|
||||
"by a regStats() function\n",
|
||||
stat.name,
|
||||
stat.id,
|
||||
)
|
||||
|
||||
if not (stat.flags & flags.display):
|
||||
stat.name = "__Stat%06d" % stat.id
|
||||
|
||||
|
||||
# Legacy stat
|
||||
global stats_list
|
||||
stats_list = list(_m5.stats.statsList())
|
||||
@@ -312,21 +333,21 @@ def enable():
|
||||
for stat in stats_list:
|
||||
check_stat(None, stat)
|
||||
|
||||
stats_list.sort(key=lambda s: s.name.split('.'))
|
||||
stats_list.sort(key=lambda s: s.name.split("."))
|
||||
for stat in stats_list:
|
||||
stats_dict[stat.name] = stat
|
||||
stat.enable()
|
||||
|
||||
|
||||
# New stats
|
||||
_visit_stats(check_stat)
|
||||
_visit_stats(lambda g, s: s.enable())
|
||||
|
||||
_m5.stats.enable();
|
||||
_m5.stats.enable()
|
||||
|
||||
|
||||
def prepare():
|
||||
'''Prepare all stats for data access. This must be done before
|
||||
dumping and serialization.'''
|
||||
"""Prepare all stats for data access. This must be done before
|
||||
dumping and serialization."""
|
||||
|
||||
# Legacy stats
|
||||
for stat in stats_list:
|
||||
@@ -335,6 +356,7 @@ def prepare():
|
||||
# New stats
|
||||
_visit_stats(lambda g, s: s.prepare())
|
||||
|
||||
|
||||
def _dump_to_visitor(visitor, roots=None):
|
||||
# New stats
|
||||
def dump_group(group):
|
||||
@@ -361,12 +383,14 @@ def _dump_to_visitor(visitor, roots=None):
|
||||
for stat in stats_list:
|
||||
stat.visit(visitor)
|
||||
|
||||
|
||||
lastDump = 0
|
||||
# List[SimObject].
|
||||
global_dump_roots = []
|
||||
|
||||
|
||||
def dump(roots=None):
|
||||
'''Dump all statistics data to the registered outputs'''
|
||||
"""Dump all statistics data to the registered outputs"""
|
||||
|
||||
all_roots = []
|
||||
if roots is not None:
|
||||
@@ -391,7 +415,7 @@ def dump(roots=None):
|
||||
# Notify new-style stats group that we are about to dump stats.
|
||||
sim_root = Root.getInstance()
|
||||
if sim_root:
|
||||
sim_root.preDumpStats();
|
||||
sim_root.preDumpStats()
|
||||
prepare()
|
||||
|
||||
for output in outputList:
|
||||
@@ -406,8 +430,9 @@ def dump(roots=None):
|
||||
_dump_to_visitor(output, roots=all_roots)
|
||||
output.end()
|
||||
|
||||
|
||||
def reset():
|
||||
'''Reset all statistics to the base state'''
|
||||
"""Reset all statistics to the base state"""
|
||||
|
||||
# call reset stats on all SimObjects
|
||||
root = Root.getInstance()
|
||||
@@ -420,14 +445,17 @@ def reset():
|
||||
|
||||
_m5.stats.processResetQueue()
|
||||
|
||||
flags = attrdict({
|
||||
'none' : 0x0000,
|
||||
'init' : 0x0001,
|
||||
'display' : 0x0002,
|
||||
'total' : 0x0010,
|
||||
'pdf' : 0x0020,
|
||||
'cdf' : 0x0040,
|
||||
'dist' : 0x0080,
|
||||
'nozero' : 0x0100,
|
||||
'nonan' : 0x0200,
|
||||
})
|
||||
|
||||
flags = attrdict(
|
||||
{
|
||||
"none": 0x0000,
|
||||
"init": 0x0001,
|
||||
"display": 0x0002,
|
||||
"total": 0x0010,
|
||||
"pdf": 0x0020,
|
||||
"cdf": 0x0040,
|
||||
"dist": 0x0080,
|
||||
"nozero": 0x0100,
|
||||
"nonan": 0x0200,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -39,11 +39,13 @@ from m5.ext.pystats.simstat import *
|
||||
from m5.ext.pystats.statistic import *
|
||||
from m5.ext.pystats.storagetype import *
|
||||
|
||||
class JsonOutputVistor():
|
||||
|
||||
class JsonOutputVistor:
|
||||
"""
|
||||
This is a helper vistor class used to include a JSON output via the stats
|
||||
API (`src/python/m5/stats/__init__.py`).
|
||||
"""
|
||||
|
||||
file: str
|
||||
json_args: Dict
|
||||
|
||||
@@ -77,10 +79,11 @@ class JsonOutputVistor():
|
||||
The Root, or List of roots, whose stats are are to be dumped JSON.
|
||||
"""
|
||||
|
||||
with open(self.file, 'w') as fp:
|
||||
with open(self.file, "w") as fp:
|
||||
simstat = get_simstat(root=roots, prepare_stats=False)
|
||||
simstat.dump(fp=fp, **self.json_args)
|
||||
|
||||
|
||||
def get_stats_group(group: _m5.stats.Group) -> Group:
|
||||
"""
|
||||
Translates a gem5 Group object into a Python stats Group object. A Python
|
||||
@@ -113,6 +116,7 @@ def get_stats_group(group: _m5.stats.Group) -> Group:
|
||||
|
||||
return Group(**stats_dict)
|
||||
|
||||
|
||||
def __get_statistic(statistic: _m5.stats.Info) -> Optional[Statistic]:
|
||||
"""
|
||||
Translates a _m5.stats.Info object into a Statistic object, to process
|
||||
@@ -130,7 +134,7 @@ def __get_statistic(statistic: _m5.stats.Info) -> Optional[Statistic]:
|
||||
cannot be translated.
|
||||
"""
|
||||
|
||||
assert(isinstance(statistic, _m5.stats.Info))
|
||||
assert isinstance(statistic, _m5.stats.Info)
|
||||
statistic.prepare()
|
||||
|
||||
if isinstance(statistic, _m5.stats.ScalarInfo):
|
||||
@@ -146,6 +150,7 @@ def __get_statistic(statistic: _m5.stats.Info) -> Optional[Statistic]:
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def __get_scaler(statistic: _m5.stats.ScalarInfo) -> Scalar:
|
||||
value = statistic.value
|
||||
unit = statistic.unit
|
||||
@@ -154,11 +159,9 @@ def __get_scaler(statistic: _m5.stats.ScalarInfo) -> Scalar:
|
||||
datatype = StorageType["f64"]
|
||||
|
||||
return Scalar(
|
||||
value=value,
|
||||
unit=unit,
|
||||
description=description,
|
||||
datatype=datatype,
|
||||
)
|
||||
value=value, unit=unit, description=description, datatype=datatype
|
||||
)
|
||||
|
||||
|
||||
def __get_distribution(statistic: _m5.stats.DistInfo) -> Distribution:
|
||||
unit = statistic.unit
|
||||
@@ -177,20 +180,21 @@ def __get_distribution(statistic: _m5.stats.DistInfo) -> Distribution:
|
||||
datatype = StorageType["f64"]
|
||||
|
||||
return Distribution(
|
||||
value=value,
|
||||
min=min,
|
||||
max=max,
|
||||
num_bins=num_bins,
|
||||
bin_size=bin_size,
|
||||
sum = sum_val,
|
||||
sum_squared = sum_squared,
|
||||
underflow = underflow,
|
||||
overflow = overflow,
|
||||
logs = logs,
|
||||
unit=unit,
|
||||
description=description,
|
||||
datatype=datatype,
|
||||
)
|
||||
value=value,
|
||||
min=min,
|
||||
max=max,
|
||||
num_bins=num_bins,
|
||||
bin_size=bin_size,
|
||||
sum=sum_val,
|
||||
sum_squared=sum_squared,
|
||||
underflow=underflow,
|
||||
overflow=overflow,
|
||||
logs=logs,
|
||||
unit=unit,
|
||||
description=description,
|
||||
datatype=datatype,
|
||||
)
|
||||
|
||||
|
||||
def __get_vector(statistic: _m5.stats.VectorInfo) -> Vector:
|
||||
to_add = dict()
|
||||
@@ -212,14 +216,12 @@ def __get_vector(statistic: _m5.stats.VectorInfo) -> Vector:
|
||||
index_string = str(index)
|
||||
|
||||
to_add[index_string] = Scalar(
|
||||
value=value,
|
||||
unit=unit,
|
||||
description=description,
|
||||
datatype=datatype,
|
||||
)
|
||||
value=value, unit=unit, description=description, datatype=datatype
|
||||
)
|
||||
|
||||
return Vector(scalar_map=to_add)
|
||||
|
||||
|
||||
def _prepare_stats(group: _m5.stats.Group):
|
||||
"""
|
||||
Prepares the statistics for dumping.
|
||||
@@ -234,8 +236,9 @@ def _prepare_stats(group: _m5.stats.Group):
|
||||
_prepare_stats(child)
|
||||
|
||||
|
||||
def get_simstat(root: Union[SimObject, List[SimObject]],
|
||||
prepare_stats: bool = True) -> SimStat:
|
||||
def get_simstat(
|
||||
root: Union[SimObject, List[SimObject]], prepare_stats: bool = True
|
||||
) -> SimStat:
|
||||
"""
|
||||
This function will return the SimStat object for a simulation given a
|
||||
SimObject (typically a Root SimObject), or list of SimObjects. The returned
|
||||
@@ -262,7 +265,7 @@ def get_simstat(root: Union[SimObject, List[SimObject]],
|
||||
"""
|
||||
stats_map = {}
|
||||
creation_time = datetime.now()
|
||||
time_converstion = None # TODO https://gem5.atlassian.net/browse/GEM5-846
|
||||
time_converstion = None # TODO https://gem5.atlassian.net/browse/GEM5-846
|
||||
final_tick = Root.getInstance().resolveStat("finalTick").value
|
||||
sim_ticks = Root.getInstance().resolveStat("simTicks").value
|
||||
simulated_begin_time = int(final_tick - sim_ticks)
|
||||
@@ -284,16 +287,16 @@ def get_simstat(root: Union[SimObject, List[SimObject]],
|
||||
_prepare_stats(r)
|
||||
stats_map[r.get_name()] = get_stats_group(r)
|
||||
else:
|
||||
raise TypeError("Object (" + str(r) + ") passed is not a "
|
||||
"SimObject. " + __name__ + " only processes "
|
||||
"SimObjects, or a list of SimObjects.")
|
||||
|
||||
|
||||
raise TypeError(
|
||||
"Object (" + str(r) + ") passed is not a "
|
||||
"SimObject. " + __name__ + " only processes "
|
||||
"SimObjects, or a list of SimObjects."
|
||||
)
|
||||
|
||||
return SimStat(
|
||||
creation_time=creation_time,
|
||||
time_conversion=time_converstion,
|
||||
simulated_begin_time=simulated_begin_time,
|
||||
simulated_end_time=simulated_end_time,
|
||||
**stats_map,
|
||||
)
|
||||
creation_time=creation_time,
|
||||
time_conversion=time_converstion,
|
||||
simulated_begin_time=simulated_begin_time,
|
||||
simulated_end_time=simulated_end_time,
|
||||
**stats_map,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user