gem5  v22.1.0.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 "pybind11/pybind11.h"
42 #include "pybind11/stl.h"
43 
44 #include "base/statistics.hh"
45 #include "base/stats/text.hh"
46 #include "config/have_hdf5.hh"
47 
48 #if HAVE_HDF5
49 #include "base/stats/hdf5.hh"
50 
51 #endif
52 #include "sim/stat_control.hh"
53 #include "sim/stat_register.hh"
54 
55 namespace py = pybind11;
56 
57 namespace gem5
58 {
59 
60 static const py::object
62 {
63  /* PyBind11 gets confused by the InfoProxy magic, so we need to
64  * explicitly cast to the right wrapper type. */
65 
66 #define TRY_CAST(T) do { \
67  auto _stat = dynamic_cast<const T *>(info); \
68  if (_stat) \
69  return py::cast(_stat); \
70  } while (0)
71 
73  /* FormulaInfo is a subclass of VectorInfo. Therefore, a cast to
74  * FormulaInfo must be attempted before a cast to VectorInfo. Otherwise
75  * instances of ForumlaInfo will be cast to VectorInfo.
76  */
80 
81  return py::cast(info);
82 
83 #undef TRY_CAST
84 }
85 
86 GEM5_DEPRECATED_NAMESPACE(Stats, statistics);
87 namespace statistics
88 {
89 
90 void
92 {
93  py::module_ m = py::module_::import("m5.stats");
94  m.attr("dump")();
95 }
96 
97 void
99 {
100  py::module_ m = py::module_::import("m5.stats");
101  m.attr("reset")();
102 }
103 
104 }
105 
106 void
107 pybind_init_stats(py::module_ &m_native)
108 {
109  py::module_ m = m_native.def_submodule("stats");
110 
111  m
112  .def("initSimStats", &statistics::initSimStats)
113  .def("initText", &statistics::initText,
114  py::return_value_policy::reference)
115 #if HAVE_HDF5
116  .def("initHDF5", &statistics::initHDF5)
117 #endif
118  .def("registerPythonStatsHandlers",
120  .def("schedStatEvent", &statistics::schedStatEvent)
121  .def("periodicStatDump", &statistics::periodicStatDump)
122  .def("updateEvents", &statistics::updateEvents)
123  .def("processResetQueue", &statistics::processResetQueue)
124  .def("processDumpQueue", &statistics::processDumpQueue)
125  .def("enable", &statistics::enable)
126  .def("enabled", &statistics::enabled)
127  .def("statsList", &statistics::statsList)
128  ;
129 
130  py::class_<statistics::Output>(m, "Output")
131  .def("begin", &statistics::Output::begin)
132  .def("end", &statistics::Output::end)
133  .def("valid", &statistics::Output::valid)
134  .def("beginGroup", &statistics::Output::beginGroup)
135  .def("endGroup", &statistics::Output::endGroup)
136  ;
137 
138  py::class_<statistics::Info,
139  std::unique_ptr<statistics::Info, py::nodelete>>(m, "Info")
140  .def_readwrite("name", &statistics::Info::name)
141  .def_property_readonly("unit", [](const statistics::Info &info) {
142  return info.unit->getUnitString();
143  })
144  .def_readonly("desc", &statistics::Info::desc)
145  .def_readonly("id", &statistics::Info::id)
146  .def_property_readonly("flags", [](const statistics::Info &info) {
147  return (statistics::FlagsType)info.flags;
148  })
149  .def("check", &statistics::Info::check)
150  .def("baseCheck", &statistics::Info::baseCheck)
151  .def("enable", &statistics::Info::enable)
152  .def("prepare", &statistics::Info::prepare)
153  .def("reset", &statistics::Info::reset)
154  .def("zero", &statistics::Info::zero)
155  .def("visit", &statistics::Info::visit)
156  ;
157 
159  std::unique_ptr<statistics::ScalarInfo, py::nodelete>>(
160  m, "ScalarInfo")
161  .def_property_readonly("value",
162  [](const statistics::ScalarInfo &info) { return info.value(); })
163  .def_property_readonly("result",
164  [](const statistics::ScalarInfo &info) { return info.result(); })
165  .def_property_readonly("total",
166  [](const statistics::ScalarInfo &info) { return info.total(); })
167  ;
168 
170  std::unique_ptr<statistics::VectorInfo, py::nodelete>>(
171  m, "VectorInfo")
172  .def_readwrite("subnames", &statistics::VectorInfo::subnames)
173  .def_readwrite("subdescs", &statistics::VectorInfo::subdescs)
174  .def_property_readonly("size",
175  [](const statistics::VectorInfo &info) { return info.size(); })
176  .def_property_readonly("value",
177  [](const statistics::VectorInfo &info) { return info.value(); })
178  .def_property_readonly("result",
179  [](const statistics::VectorInfo &info) { return info.result(); })
180  .def_property_readonly("total",
181  [](const statistics::VectorInfo &info) { return info.total(); })
182  ;
183 
185  std::unique_ptr<statistics::FormulaInfo, py::nodelete>>(
186  m, "FormulaInfo")
187  .def_property_readonly("str",
188  [](const statistics::FormulaInfo &info) { return info.str(); })
189  ;
190 
192  std::unique_ptr<statistics::DistInfo, py::nodelete>>(
193  m, "DistInfo")
194  .def_property_readonly("min_val",
195  [](const statistics::DistInfo &info) { return info.data.min_val; })
196  .def_property_readonly("max_val",
197  [](const statistics::DistInfo &info) { return info.data.max_val; })
198  .def_property_readonly("bucket_size",
199  [](const statistics::DistInfo &info) {
200  return info.data.bucket_size;
201  })
202  .def_property_readonly("values",
203  [](const statistics::DistInfo &info) { return info.data.cvec; })
204  .def_property_readonly("overflow",
205  [](const statistics::DistInfo &info) {
206  return info.data.overflow;
207  })
208  .def_property_readonly("underflow",
209  [](const statistics::DistInfo &info) {
210  return info.data.underflow;
211  })
212  .def_property_readonly("sum",
213  [](const statistics::DistInfo &info) { return info.data.sum; })
214  .def_property_readonly("logs",
215  [](const statistics::DistInfo &info) { return info.data.logs; })
216  .def_property_readonly("squares",
217  [](const statistics::DistInfo &info) { return info.data.squares; })
218  ;
219 
220  py::class_<statistics::Group,
221  std::unique_ptr<statistics::Group, py::nodelete>>(m, "Group")
222  .def("regStats", &statistics::Group::regStats)
223  .def("resetStats", &statistics::Group::resetStats)
224  .def("preDumpStats", &statistics::Group::preDumpStats)
225  .def("getStats", [](const statistics::Group &self)
227 
228  auto stats = self.getStats();
229  std::vector<py::object> py_stats;
230  py_stats.reserve(stats.size());
231  std::transform(stats.begin(), stats.end(),
232  std::back_inserter(py_stats),
234  return py_stats;
235  })
236  .def("getStatGroups", &statistics::Group::getStatGroups)
237  .def("addStatGroup", &statistics::Group::addStatGroup)
238  .def("resolveStat", [](const statistics::Group &self,
239  const std::string &name) -> py::object {
240  const statistics::Info *stat = self.resolveStat(name);
241  if (!stat)
242  throw pybind11::key_error("Unknown stat name");
243 
244  return cast_stat_info(stat);
245  })
246  ;
247 }
248 
249 } // namespace gem5
DistData data
Local storage for the entry values, used for printing.
Definition: info.hh:205
virtual std::string str() const =0
Statistics container.
Definition: group.hh:94
Flags flags
The formatting flags.
Definition: info.hh:92
const units::Base * unit
The unit of the stat.
Definition: info.hh:88
virtual void enable()
Enable the stat for use.
Definition: info.cc:195
std::string name
The name of the stat.
Definition: info.hh:84
std::string desc
The description of the stat.
Definition: info.hh:90
virtual bool zero() const =0
virtual void prepare()=0
Prepare the stat for dumping.
bool baseCheck() const
Definition: info.cc:173
virtual void reset()=0
Reset the stat to the default state.
virtual void visit(Output &visitor)=0
Visitor entry for outputing statistics data.
virtual bool check() const =0
Check that this stat has been set up properly and is ready for use.
virtual Result total() const =0
virtual Result result() const =0
virtual Counter value() const =0
std::vector< std::string > subdescs
Definition: info.hh:189
std::vector< std::string > subnames
Names and descriptions of subfields.
Definition: info.hh:188
virtual std::string getUnitString() const =0
STL vector class.
Definition: stl.hh:37
const std::map< std::string, Group * > & getStatGroups() const
Get all child groups associated with this object.
Definition: group.cc:181
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:69
void addStatGroup(const char *name, Group *block)
Add a stat block as a child of this block.
Definition: group.cc:117
virtual void preDumpStats()
Callback before stats are dumped.
Definition: group.cc:99
virtual void resetStats()
Callback to reset stats.
Definition: group.cc:86
void schedStatEvent(bool dump, bool reset, Tick when, Tick repeat)
Schedule statistics dumping.
uint16_t FlagsType
Definition: info.hh:50
void pythonDump()
Definition: stats.cc:91
void processResetQueue()
Process all the callbacks in the reset callbacks queue.
Definition: statistics.cc:266
std::unique_ptr< Output > initHDF5(const std::string &filename, unsigned chunking, bool desc, bool formulas)
Definition: hdf5.cc:332
void pythonReset()
Definition: stats.cc:98
void periodicStatDump(Tick period)
Schedule periodic statistics dumping.
void updateEvents()
Update the events after resuming from a checkpoint.
void registerPythonStatsHandlers()
Register py_...
std::list< Info * > & statsList()
Definition: statistics.cc:62
void processDumpQueue()
Process all the callbacks in the dump callbacks queue.
Definition: statistics.cc:272
Output * initText(const std::string &filename, bool desc, bool spaces)
Definition: text.cc:805
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
void pybind_init_stats(pybind11::module_ &m_native)
static const py::object cast_stat_info(const statistics::Info *info)
Definition: stats.cc:61
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
#define TRY_CAST(T)
Declaration of Statistics objects.
virtual void end()=0
virtual void begin()=0
virtual bool valid() const =0
virtual void beginGroup(const char *name)=0
virtual void endGroup()=0
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:30 for gem5 by doxygen 1.9.1