gem5  v21.0.1.0
stats.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2006 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 #include "config/use_hdf5.hh"
42 
43 #include "pybind11/pybind11.h"
44 #include "pybind11/stl.h"
45 
46 #include "base/statistics.hh"
47 #include "base/stats/text.hh"
48 #if USE_HDF5
49 #include "base/stats/hdf5.hh"
50 #endif
51 #include "sim/stat_control.hh"
52 #include "sim/stat_register.hh"
53 
54 
55 namespace py = pybind11;
56 
57 static const py::object
59 {
60  /* PyBind11 gets confused by the InfoProxy magic, so we need to
61  * explicitly cast to the right wrapper type. */
62 
63 #define TRY_CAST(T) do { \
64  auto _stat = dynamic_cast<const T *>(info); \
65  if (_stat) \
66  return py::cast(_stat); \
67  } while (0)
68 
70  /* FormulaInfo is a subclass of VectorInfo. Therefore, a cast to
71  * FormulaInfo must be attempted before a cast to VectorInfo. Otherwise
72  * instances of ForumlaInfo will be cast to VectorInfo.
73  */
77 
78  return py::cast(info);
79 
80 #undef TRY_CAST
81 }
82 
83 namespace Stats {
84 
85 void
87 {
88  py::module_ m = py::module_::import("m5.stats");
89  m.attr("dump")();
90 }
91 
92 void
94 {
95  py::module_ m = py::module_::import("m5.stats");
96  m.attr("reset")();
97 }
98 
99 }
100 
101 void
102 pybind_init_stats(py::module_ &m_native)
103 {
104  py::module_ m = m_native.def_submodule("stats");
105 
106  m
107  .def("initSimStats", &Stats::initSimStats)
108  .def("initText", &Stats::initText, py::return_value_policy::reference)
109 #if USE_HDF5
110  .def("initHDF5", &Stats::initHDF5)
111 #endif
112  .def("registerPythonStatsHandlers",
114  .def("schedStatEvent", &Stats::schedStatEvent)
115  .def("periodicStatDump", &Stats::periodicStatDump)
116  .def("updateEvents", &Stats::updateEvents)
117  .def("processResetQueue", &Stats::processResetQueue)
118  .def("processDumpQueue", &Stats::processDumpQueue)
119  .def("enable", &Stats::enable)
120  .def("enabled", &Stats::enabled)
121  .def("statsList", &Stats::statsList)
122  ;
123 
124  py::class_<Stats::Output>(m, "Output")
125  .def("begin", &Stats::Output::begin)
126  .def("end", &Stats::Output::end)
127  .def("valid", &Stats::Output::valid)
128  .def("beginGroup", &Stats::Output::beginGroup)
129  .def("endGroup", &Stats::Output::endGroup)
130  ;
131 
132  py::class_<Stats::Info, std::unique_ptr<Stats::Info, py::nodelete>>(
133  m, "Info")
134  .def_readwrite("name", &Stats::Info::name)
135  .def_property_readonly("unit", [](const Stats::Info &info) {
136  return info.unit->getUnitString();
137  })
138  .def_readonly("desc", &Stats::Info::desc)
139  .def_readonly("id", &Stats::Info::id)
140  .def_property_readonly("flags", [](const Stats::Info &info) {
141  return (Stats::FlagsType)info.flags;
142  })
143  .def("check", &Stats::Info::check)
144  .def("baseCheck", &Stats::Info::baseCheck)
145  .def("enable", &Stats::Info::enable)
146  .def("prepare", &Stats::Info::prepare)
147  .def("reset", &Stats::Info::reset)
148  .def("zero", &Stats::Info::zero)
149  .def("visit", &Stats::Info::visit)
150  ;
151 
152  py::class_<Stats::ScalarInfo, Stats::Info,
153  std::unique_ptr<Stats::ScalarInfo, py::nodelete>>(
154  m, "ScalarInfo")
155  .def_property_readonly("value", [](const Stats::ScalarInfo &info) {
156  return info.value();
157  })
158  .def_property_readonly("result", [](const Stats::ScalarInfo &info) {
159  return info.result();
160  })
161  .def_property_readonly("total", [](const Stats::ScalarInfo &info) {
162  return info.total();
163  })
164  ;
165 
166  py::class_<Stats::VectorInfo, Stats::Info,
167  std::unique_ptr<Stats::VectorInfo, py::nodelete>>(
168  m, "VectorInfo")
169  .def_readwrite("subnames", &Stats::VectorInfo::subnames)
170  .def_readwrite("subdescs", &Stats::VectorInfo::subdescs)
171  .def_property_readonly("size", [](const Stats::VectorInfo &info) {
172  return info.size();
173  })
174  .def_property_readonly("value", [](const Stats::VectorInfo &info) {
175  return info.value();
176  })
177  .def_property_readonly("result", [](const Stats::VectorInfo &info) {
178  return info.result();
179  })
180  .def_property_readonly("total", [](const Stats::VectorInfo &info) {
181  return info.total();
182  })
183  ;
184 
186  std::unique_ptr<Stats::FormulaInfo, py::nodelete>>(
187  m, "FormulaInfo")
188  .def_property_readonly("str", [](const Stats::FormulaInfo &info) {
189  return info.str();
190  })
191  ;
192 
193  py::class_<Stats::DistInfo, Stats::Info,
194  std::unique_ptr<Stats::DistInfo, py::nodelete>>(
195  m, "DistInfo")
196  .def_property_readonly("min_val", [](const Stats::DistInfo &info) {
197  return info.data.min_val;
198  })
199  .def_property_readonly("max_val", [](const Stats::DistInfo &info) {
200  return info.data.max_val;
201  })
202  .def_property_readonly("bucket_size", [](const Stats::DistInfo &info) {
203  return info.data.bucket_size;
204  })
205  .def_property_readonly("values", [](const Stats::DistInfo &info) {
206  return info.data.cvec;
207  })
208  .def_property_readonly("overflow", [](const Stats::DistInfo &info) {
209  return info.data.overflow;
210  })
211  .def_property_readonly("underflow", [](const Stats::DistInfo &info) {
212  return info.data.underflow;
213  })
214  .def_property_readonly("sum", [](const Stats::DistInfo &info) {
215  return info.data.sum;
216  })
217  .def_property_readonly("logs", [](const Stats::DistInfo &info) {
218  return info.data.logs;
219  })
220  .def_property_readonly("squares", [](const Stats::DistInfo &info) {
221  return info.data.squares;
222  })
223  ;
224 
225  py::class_<Stats::Group, std::unique_ptr<Stats::Group, py::nodelete>>(
226  m, "Group")
227  .def("regStats", &Stats::Group::regStats)
228  .def("resetStats", &Stats::Group::resetStats)
229  .def("preDumpStats", &Stats::Group::preDumpStats)
230  .def("getStats", [](const Stats::Group &self)
232 
233  auto stats = self.getStats();
234  std::vector<py::object> py_stats;
235  py_stats.reserve(stats.size());
236  std::transform(stats.begin(), stats.end(),
237  std::back_inserter(py_stats),
239  return py_stats;
240  })
241  .def("getStatGroups", &Stats::Group::getStatGroups)
242  .def("addStatGroup", &Stats::Group::addStatGroup)
243  .def("resolveStat", [](const Stats::Group &self,
244  const std::string &name) -> py::object {
245  const Stats::Info *stat = self.resolveStat(name);
246  if (!stat)
247  throw pybind11::key_error("Unknown stat name");
248 
249  return cast_stat_info(stat);
250  })
251  ;
252 }
Stats::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:63
Stats::Info::zero
virtual bool zero() const =0
Stats::VectorInfo::value
virtual const VCounter & value() const =0
Stats::Info::visit
virtual void visit(Output &visitor)=0
Visitor entry for outputing statistics data.
Stats::Info::baseCheck
bool baseCheck() const
Definition: info.cc:165
Stats::FormulaInfo::str
virtual std::string str() const =0
Stats::Output::endGroup
virtual void endGroup()=0
Stats::Output::begin
virtual void begin()=0
Stats::processResetQueue
void processResetQueue()
Process all the callbacks in the reset callbacks queue.
Definition: statistics.cc:255
Stats::VectorInfo
Definition: info.hh:159
Stats::Output::beginGroup
virtual void beginGroup(const char *name)=0
Stats::enable
void enable()
Definition: statistics.cc:281
Stats::DistData::sum
Counter sum
Definition: info.hh:190
Stats::periodicStatDump
void periodicStatDump(Tick period)
Schedule periodic statistics dumping.
Definition: stat_control.cc:111
Stats::Group::getStatGroups
const std::map< std::string, Group * > & getStatGroups() const
Get all child groups associated with this object.
Definition: group.cc:174
Stats::initSimStats
void initSimStats()
Definition: stat_control.cc:62
Stats::Group::preDumpStats
virtual void preDumpStats()
Callback before stats are dumped.
Definition: group.cc:94
std::vector
STL vector class.
Definition: stl.hh:37
Stats::DistData::underflow
Counter underflow
Definition: info.hh:187
pybind_init_stats
void pybind_init_stats(py::module_ &m_native)
Definition: stats.cc:102
Stats::ScalarInfo::result
virtual Result result() const =0
Stats::registerPythonStatsHandlers
void registerPythonStatsHandlers()
Register py_...
Definition: stat_register.cc:48
Stats::ScalarInfo::total
virtual Result total() const =0
Stats::DistData::logs
Counter logs
Definition: info.hh:192
Stats::Info::check
virtual bool check() const =0
Check that this stat has been set up properly and is ready for use.
Stats::DistData::min_val
Counter min_val
Definition: info.hh:185
Stats::Group::resetStats
virtual void resetStats()
Callback to reset stats.
Definition: group.cc:81
Stats::Info::id
int id
Definition: info.hh:92
Stats::Info::enable
virtual void enable()
Enable the stat for use.
Definition: info.cc:187
stat_register.hh
Stats::DistData::bucket_size
Counter bucket_size
Definition: info.hh:183
Stats::DistInfo
Definition: info.hh:196
Stats::ScalarInfo::value
virtual Counter value() const =0
Stats::Info::unit
const Units::Base * unit
The unit of the stat.
Definition: info.hh:78
Stats::enabled
bool enabled()
Definition: statistics.cc:275
statistics.hh
Stats::VectorInfo::total
virtual Result total() const =0
Stats::FlagsType
uint16_t FlagsType
Definition: info.hh:38
Stats::VectorInfo::result
virtual const VResult & result() const =0
Stats::DistData::squares
Counter squares
Definition: info.hh:191
TRY_CAST
#define TRY_CAST(T)
Stats::VectorInfo::subdescs
std::vector< std::string > subdescs
Definition: info.hh:164
Stats::Info::prepare
virtual void prepare()=0
Prepare the stat for dumping.
Stats::updateEvents
void updateEvents()
Update the events after resuming from a checkpoint.
Definition: stat_control.cc:141
Stats::statsList
std::list< Info * > & statsList()
Definition: statistics.cc:57
Stats::Info::reset
virtual void reset()=0
Reset the stat to the default state.
Stats::VectorInfo::size
virtual size_type size() const =0
cast_stat_info
static const py::object cast_stat_info(const Stats::Info *info)
Definition: stats.cc:58
name
const std::string & name()
Definition: trace.cc:48
Stats::schedStatEvent
void schedStatEvent(bool dump, bool reset, Tick when, Tick repeat)
Schedule statistics dumping.
Definition: stat_control.cc:101
Stats::Info
Definition: info.hh:70
Stats::pythonReset
void pythonReset()
Definition: stats.cc:93
text.hh
Stats::VectorInfo::subnames
std::vector< std::string > subnames
Names and descriptions of subfields.
Definition: info.hh:163
stat_control.hh
Stats::FormulaInfo
Definition: info.hh:240
Stats::processDumpQueue
void processDumpQueue()
Process all the callbacks in the dump callbacks queue.
Definition: statistics.cc:261
Stats::Output::valid
virtual bool valid() const =0
Stats::Info::flags
Flags flags
The formatting flags.
Definition: info.hh:82
Stats::Group
Statistics container.
Definition: group.hh:87
Stats::Info::name
std::string name
The name of the stat.
Definition: info.hh:74
Stats::initHDF5
std::unique_ptr< Output > initHDF5(const std::string &filename, unsigned chunking, bool desc, bool formulas)
Definition: hdf5.cc:316
Stats::Output::end
virtual void end()=0
Stats::Units::Base::getUnitString
virtual std::string getUnitString() const =0
Stats::DistInfo::data
DistData data
Local storage for the entry values, used for printing.
Definition: info.hh:200
Stats::ScalarInfo
Definition: info.hh:151
Stats::Info::desc
std::string desc
The description of the stat.
Definition: info.hh:80
Stats::DistData::overflow
Counter overflow
Definition: info.hh:188
Stats
Definition: statistics.cc:53
Stats::initText
Output * initText(const std::string &filename, bool desc, bool spaces)
Definition: text.cc:852
hdf5.hh
Stats::pythonDump
void pythonDump()
Definition: stats.cc:86
Stats::DistData::max_val
Counter max_val
Definition: info.hh:186
ArmISA::m
Bitfield< 0 > m
Definition: miscregs_types.hh:389
Stats::Group::addStatGroup
void addStatGroup(const char *name, Group *block)
Add a stat block as a child of this block.
Definition: group.cc:112
Stats::DistData::cvec
VCounter cvec
Definition: info.hh:189

Generated on Tue Jun 22 2021 15:28:26 for gem5 by doxygen 1.8.17