base-stats,python: Add Python Stats
This model is used to store and represent the "new" hierarchical stats
at the Python level. Over time these classes may be extended with
functions to ease in the analysis of gem5 stats. Though, for this
commit, such functions have been kept to a minimum.
`m5/pystats/loader.py` contains functions for translating the gem5 `_m5.stats`
statistics exposed via Pybind11 to the Python Stats model. For example:
```
import m5.pystats.gem5stats as gem5stats
simstat = gem5stats.get_simstat(root)
```
All the python Stats model classes inherit from JsonSerializable meaning
they can be translated to JSON. For example:
```
import m5.pystats.gem5stats as gem5stats
simstat = gem5stats.get_simstat(root)
with open('test.json', 'w') as f:
simstat.dump(f)
```
The stats have also been exposed via the python statistics API. Via
command line, a JSON output may be specified with the argument
`--stats-file json://<file path>`.
Change-Id: I253a869f6b6d8c0de4dbed708892ee0cc33c5665
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/38615
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
@@ -42,6 +42,7 @@ import m5
|
||||
import _m5.stats
|
||||
from m5.objects import Root
|
||||
from m5.params import isNullPointer
|
||||
from .gem5stats import JsonOutputVistor
|
||||
from m5.util import attrdict, fatal
|
||||
|
||||
# Stat exports
|
||||
@@ -182,6 +183,17 @@ 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.
|
||||
|
||||
Example:
|
||||
json://stats.json
|
||||
|
||||
"""
|
||||
|
||||
return JsonOutputVistor(fn)
|
||||
|
||||
def addStatVisitor(url):
|
||||
"""Add a stat visitor specified using a URL string
|
||||
|
||||
@@ -383,10 +395,16 @@ def dump(roots=None):
|
||||
prepare()
|
||||
|
||||
for output in outputList:
|
||||
if output.valid():
|
||||
output.begin()
|
||||
_dump_to_visitor(output, roots=all_roots)
|
||||
output.end()
|
||||
if isinstance(output, JsonOutputVistor):
|
||||
if not all_roots:
|
||||
output.dump(Root.getInstance())
|
||||
else:
|
||||
output.dump(all_roots)
|
||||
else:
|
||||
if output.valid():
|
||||
output.begin()
|
||||
_dump_to_visitor(output, roots=all_roots)
|
||||
output.end()
|
||||
|
||||
def reset():
|
||||
'''Reset all statistics to the base state'''
|
||||
|
||||
294
src/python/m5/stats/gem5stats.py
Normal file
294
src/python/m5/stats/gem5stats.py
Normal file
@@ -0,0 +1,294 @@
|
||||
# Copyright (c) 2021 The Regents of The University of California
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met: redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer;
|
||||
# redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution;
|
||||
# neither the name of the copyright holders nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
"""
|
||||
This serves as the bridge between the gem5 statistics exposed via PyBind11 and
|
||||
the Python Stats model.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from typing import IO, List, Union
|
||||
|
||||
import _m5.stats
|
||||
from m5.objects import *
|
||||
from m5.ext.pystats.group import *
|
||||
from m5.ext.pystats.simstat import *
|
||||
from m5.ext.pystats.statistic import *
|
||||
from m5.ext.pystats.storagetype import *
|
||||
|
||||
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
|
||||
|
||||
def __init__(self, file: str, **kwargs):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
|
||||
file: str
|
||||
The output file location in which the JSON will be dumped.
|
||||
|
||||
kwargs: Dict[str, Any]
|
||||
Additional parameters to be passed to the `json.dumps` method.
|
||||
"""
|
||||
|
||||
self.file = file
|
||||
self.json_args = kwargs
|
||||
|
||||
def dump(self, roots: Union[List[SimObject], Root]) -> None:
|
||||
"""
|
||||
Dumps the stats of a simulation root (or list of roots) to the output
|
||||
JSON file specified in the JsonOutput constructor.
|
||||
|
||||
WARNING: This dump assumes the statistics have already been prepared
|
||||
for the target root.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
|
||||
roots: Union[List[Root], Root]]
|
||||
The Root, or List of roots, whose stats are are to be dumped JSON.
|
||||
"""
|
||||
|
||||
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
|
||||
statistic Group object is a dictionary of labeled Statistic objects. Any
|
||||
gem5 object passed to this will have its `getStats()` and `getStatGroups`
|
||||
function called, and all the stats translated (inclusive of the stats
|
||||
further down the hierarchy).
|
||||
|
||||
Parameters
|
||||
----------
|
||||
group: _m5.stats.Group
|
||||
The gem5 _m5.stats.Group object to be translated to be a Python stats
|
||||
Group object. Typically this will be a gem5 SimObject.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Group
|
||||
The stats group object translated from the input gem5 object.
|
||||
"""
|
||||
|
||||
stats_dict = {}
|
||||
|
||||
for stat in group.getStats():
|
||||
statistic = __get_statistic(stat)
|
||||
if statistic is not None:
|
||||
stats_dict[stat.name] = statistic
|
||||
|
||||
for key in group.getStatGroups():
|
||||
stats_dict[key] = get_stats_group(group.getStatGroups()[key])
|
||||
|
||||
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
|
||||
statistics at the Python level.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
statistic: Info
|
||||
The Info object to be translated to a Statistic object.
|
||||
|
||||
Returns
|
||||
-------
|
||||
Optional[Statistic]
|
||||
The Statistic object of the Info object. Returns None if Info object
|
||||
cannot be translated.
|
||||
"""
|
||||
|
||||
assert(isinstance(statistic, _m5.stats.Info))
|
||||
statistic.prepare()
|
||||
|
||||
if isinstance(statistic, _m5.stats.ScalarInfo):
|
||||
return __get_scaler(statistic)
|
||||
elif isinstance(statistic, _m5.stats.DistInfo):
|
||||
return __get_distribution(statistic)
|
||||
elif isinstance(statistic, _m5.stats.FormulaInfo):
|
||||
# We don't do anything with Formula's right now.
|
||||
# We may never do so, see https://gem5.atlassian.net/browse/GEM5-868.
|
||||
pass
|
||||
elif isinstance(statistic, _m5.stats.VectorInfo):
|
||||
return __get_vector(statistic)
|
||||
|
||||
return None
|
||||
|
||||
def __get_scaler(statistic: _m5.stats.ScalarInfo) -> Scalar:
|
||||
value = statistic.value
|
||||
unit = None # TODO https://gem5.atlassian.net/browse/GEM5-850.
|
||||
description = statistic.desc
|
||||
# ScalarInfo uses the C++ `double`.
|
||||
datatype = StorageType["f64"]
|
||||
|
||||
return Scalar(
|
||||
value=value,
|
||||
unit=unit,
|
||||
description=description,
|
||||
datatype=datatype,
|
||||
)
|
||||
|
||||
def __get_distribution(statistic: _m5.stats.DistInfo) -> Distribution:
|
||||
unit = None # TODO https://gem5.atlassian.net/browse/GEM5-850.
|
||||
description = statistic.desc
|
||||
value = statistic.values
|
||||
bin_size = statistic.bucket_size
|
||||
min = statistic.min_val
|
||||
max = statistic.max_val
|
||||
num_bins = len(value)
|
||||
sum_val = statistic.sum
|
||||
sum_squared = statistic.squares
|
||||
underflow = statistic.underflow
|
||||
overflow = statistic.overflow
|
||||
logs = statistic.logs
|
||||
# DistInfo uses the C++ `double`.
|
||||
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,
|
||||
)
|
||||
|
||||
def __get_vector(statistic: _m5.stats.VectorInfo) -> Vector:
|
||||
to_add = dict()
|
||||
|
||||
for index in range(statistic.size):
|
||||
# All the values in a Vector are Scalar values
|
||||
value = statistic.value[index]
|
||||
unit = None # TODO https://gem5.atlassian.net/browse/GEM5-850.
|
||||
description = statistic.subdescs[index]
|
||||
# ScalarInfo uses the C++ `double`.
|
||||
datatype = StorageType["f64"]
|
||||
|
||||
# Sometimes elements within a vector are defined by their name. Other
|
||||
# times they have no name. When a name is not available, we name the
|
||||
# stat the index value.
|
||||
if str(statistic.subnames[index]):
|
||||
index_string = str(statistic.subnames[index])
|
||||
else:
|
||||
index_string = str(index)
|
||||
|
||||
to_add[index_string] = Scalar(
|
||||
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.
|
||||
"""
|
||||
|
||||
group.preDumpStats()
|
||||
|
||||
for stat in group.getStats():
|
||||
stat.prepare()
|
||||
|
||||
for child in getStatGroups().values():
|
||||
_prepare_stats(child)
|
||||
|
||||
|
||||
def get_simstat(root: Union[Root, List[SimObject]],
|
||||
prepare_stats: bool = True) -> SimStat:
|
||||
"""
|
||||
This function will return the SimStat object for a simulation. From the
|
||||
SimStat object all stats within the current gem5 simulation are present.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
root: Union[Root, List[Root]]
|
||||
The root, or a list of Simobjects, of the simulation for translation to
|
||||
a SimStat object.
|
||||
|
||||
prepare_stats: bool
|
||||
Dictates whether the stats are to be prepared prior to creating the
|
||||
SimStat object. By default this is 'True'.
|
||||
|
||||
Returns
|
||||
-------
|
||||
SimStat
|
||||
The SimStat Object of the current simulation.
|
||||
|
||||
"""
|
||||
stats_map = {}
|
||||
creation_time = datetime.now()
|
||||
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)
|
||||
simulated_end_time = int(final_tick)
|
||||
|
||||
if prepare_stats:
|
||||
_m5.stats.processDumpQueue()
|
||||
|
||||
for r in root:
|
||||
if isinstance(r, Root):
|
||||
if prepare_stats:
|
||||
_prepare_stats(r)
|
||||
for key in r.getStatGroups():
|
||||
stats_map[key] = get_stats_group(r.getStatGroups()[key])
|
||||
elif isinstance(r, SimObject):
|
||||
if prepare_stats:
|
||||
_prepare_stats(r)
|
||||
stats_map[r.name] = get_stats_group(r)
|
||||
else:
|
||||
raise TypeError("Object (" + str(r) + ") passed is neither Root "
|
||||
"nor SimObject. " + __name__ + " only processes "
|
||||
"Roots, SimObjects, or a list of Roots and/or "
|
||||
"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,
|
||||
)
|
||||
Reference in New Issue
Block a user