Andreas Sandberg has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/33176 )

Change subject: python: Add support for introspecting scalar stats
......................................................................

python: Add support for introspecting scalar stats

This change adds a wrapper for the ScalarInfo stat type to enable
introspection of scalar stats from Python. Due to the slightly
confusing use of proxy objects in the stat system, PyBind11 fails to
automatically cast to the right wrapper type. This is worked around in
the by explicitly casting to the relevant type's Python wrapper.

To make the interface more Python-friendly, this change also changes
the semantics of resolveStat to raise an exception if the stat can't
be found.

Change-Id: If1fc6fe238fc9d69d4e22369a4988a06407d2f7c
Signed-off-by: Andreas Sandberg <andreas.sandb...@arm.com>
---
M src/python/pybind11/stats.cc
1 file changed, 46 insertions(+), 2 deletions(-)



diff --git a/src/python/pybind11/stats.cc b/src/python/pybind11/stats.cc
index 1149eba..f69aa70 100644
--- a/src/python/pybind11/stats.cc
+++ b/src/python/pybind11/stats.cc
@@ -54,6 +54,25 @@

 namespace py = pybind11;

+static const py::object
+cast_stat_info(const Stats::Info *info)
+{
+    /* PyBind11 gets confused by the InfoProxy magic, so we need to
+     * explicitly cast to the right wrapper type. */
+
+#define TRY_CAST(T) do {                                \
+        auto _stat = dynamic_cast<const T *>(info);     \
+        if (_stat)                                      \
+            return py::cast(_stat);                     \
+    } while (0)
+
+    TRY_CAST(Stats::ScalarInfo);
+
+    return py::cast(info);
+
+#undef TRY_CAST
+}
+
 namespace Stats {

 void
@@ -120,14 +139,39 @@
         .def("visit", &Stats::Info::visit)
         ;

+    py::class_<Stats::ScalarInfo, Stats::Info,
+               std::unique_ptr<Stats::ScalarInfo, py::nodelete>>(
+                   m, "ScalarInfo")
+        .def("value", &Stats::ScalarInfo::value)
+        .def("result", &Stats::ScalarInfo::result)
+        .def("total", &Stats::ScalarInfo::result)
+        ;
+
     py::class_<Stats::Group, std::unique_ptr<Stats::Group, py::nodelete>>(
         m, "Group")
         .def("regStats", &Stats::Group::regStats)
         .def("resetStats", &Stats::Group::resetStats)
         .def("preDumpStats", &Stats::Group::preDumpStats)
-        .def("getStats", &Stats::Group::getStats)
+        .def("getStats", [](const Stats::Group &self)
+             -> std::vector<py::object> {
+
+                 auto stats = self.getStats();
+                std::vector<py::object> py_stats;
+                py_stats.reserve(stats.size());
+                std::transform(stats.begin(), stats.end(),
+                               std::back_inserter(py_stats),
+                               cast_stat_info);
+                return py_stats;
+            })
         .def("getStatGroups", &Stats::Group::getStatGroups)
         .def("addStatGroup", &Stats::Group::addStatGroup)
-        .def("resolveStat", &Stats::Group::resolveStat)
+        .def("resolveStat", [](const Stats::Group &self,
+                               const std::string &name) -> py::object {
+                 const Stats::Info *stat = self.resolveStat(name);
+                 if (!stat)
+                     throw pybind11::key_error("Unknown stat name");
+
+                 return cast_stat_info(stat);
+             })
         ;
 }

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/33176
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: If1fc6fe238fc9d69d4e22369a4988a06407d2f7c
Gerrit-Change-Number: 33176
Gerrit-PatchSet: 1
Gerrit-Owner: Andreas Sandberg <andreas.sandb...@arm.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to