Files
gem5/src/python/m5/ext/pystats/statistic.py
Bobby R. Bruce f11617736e 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>
2021-02-26 20:44:47 +00:00

206 lines
7.3 KiB
Python

# 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.
from abc import ABC
from typing import Any, Optional, Union, List
from .jsonserializable import JsonSerializable
from .storagetype import StorageType
class Statistic(ABC, JsonSerializable):
"""
The abstract base class for all Python statistics.
"""
value: Any
type: Optional[str]
unit: Optional[str]
description: Optional[str]
datatype: Optional[StorageType]
def __init__(self, value: Any, type: Optional[str] = None,
unit: Optional[str] = None,
description: Optional[str] = None,
datatype: Optional[StorageType] = None):
self.value = value
self.type = type
self.unit = unit
self.description = description
self.datatype = datatype
class Scalar(Statistic):
"""
A scalar Python statistic type.
"""
value: Union[float, int]
def __init__(self, value: Any,
unit: Optional[str] = None,
description: Optional[str] = None,
datatype: Optional[StorageType] = None):
super(Scalar, self).__init__(
value=value,
type="Scalar",
unit=unit,
description=description,
datatype=datatype,
)
class BaseScalarVector(Statistic):
"""
An abstract base class for classes containing a vector of Scalar values.
"""
value: List[Union[int,float]]
def __init__(self, value: List[Union[int,float]],
type: Optional[str] = None,
unit: Optional[str] = None,
description: Optional[str] = None,
datatype: Optional[StorageType] = None):
super(BaseScalarVector, self).__init__(
value=value,
type=type,
unit=unit,
description=description,
datatype=datatype,
)
def mean(self) -> float:
"""
Returns the mean of the value vector.
Returns
-------
float
The mean value across all bins.
"""
assert(self.value != None)
assert(isinstance(self.value, List))
from statistics import mean as statistics_mean
return statistics_mean(self.value)
def count(self) -> int:
"""
Returns the count across all the bins.
Returns
-------
float
The sum of all bin values.
"""
assert(self.value != None)
assert(isinstance(self.value, List))
return sum(self.value)
class Distribution(BaseScalarVector):
"""
A statistic type that stores information relating to distributions. Each
distribution has a number of bins (>=1)
between this range. The values correspond to the value of each bin.
E.g., value[3]` is the value of the 4th bin.
It is assumed each bucket is of equal size.
"""
value: List[int]
min: Union[float, int]
max: Union[float, int]
num_bins: int
bin_size: Union[float, int]
sum: Optional[int]
sum_squared: Optional[int]
underflow: Optional[int]
overflow: Optional[int]
logs: Optional[float]
def __init__(self, value: List[int],
min: Union[float, int],
max: Union[float, int],
num_bins: int,
bin_size: Union[float, int],
sum: Optional[int] = None,
sum_squared: Optional[int] = None,
underflow: Optional[int] = None,
overflow: Optional[int] = None,
logs: Optional[float] = None,
unit: Optional[str] = None,
description: Optional[str] = None,
datatype: Optional[StorageType] = None):
super(Distribution, self).__init__(
value=value,
type="Distribution",
unit=unit,
description=description,
datatype=datatype,
)
self.min = min
self.max = max
self.num_bins = num_bins
self.bin_size = bin_size
self.sum = sum
self.underflow = underflow
self.overflow = overflow
self.logs = logs
self.sum_squared = sum_squared
# These check some basic conditions of a distribution.
assert(self.bin_size >= 0)
assert(self.num_bins >= 1)
class Accumulator(BaseScalarVector):
"""
A statistical type representing an accumulator.
"""
count: int
min: Union[int, float]
max: Union[int, float]
sum_squared: Optional[int]
def __init__(self, value: List[Union[int,float]],
count: int,
min: Union[int, float],
max: Union[int, float],
sum_squared: Optional[int] = None,
unit: Optional[str] = None,
description: Optional[str] = None,
datatype: Optional[StorageType] = None):
super(Accumulator, self).__init__(
value=value,
type="Accumulator",
unit=unit,
description=description,
datatype=datatype,
)
self.count = count
self.min = min
self.max = max
self.sum_squared = sum_squared