Jason Lowe-Power has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/35576 )

Change subject: base: Notify stats output of first/last dump
......................................................................

base: Notify stats output of first/last dump

To have multiple stats dumps in a single json file, we need to use a
json array. However, to have a json array, we need to know when the
first stat dump happens and the last at the json output. This changeset
adds this new interface to the stats output object, plumbs these calls
into the python code and implements them in the json output.

Change-Id: I482fd330af23b4a5f690739a81853eb5e2518839
Signed-off-by: Jason Lowe-Power <ja...@lowepower.com>
---
M src/base/stats/json.cc
M src/base/stats/json.hh
M src/base/stats/output.hh
M src/python/m5/simulate.py
M src/python/m5/stats/__init__.py
M src/python/pybind11/stats.cc
6 files changed, 51 insertions(+), 1 deletion(-)



diff --git a/src/base/stats/json.cc b/src/base/stats/json.cc
index 0dc5472..c97df81 100644
--- a/src/base/stats/json.cc
+++ b/src/base/stats/json.cc
@@ -36,8 +36,24 @@
 {

 void
+Json::firstDump()
+{
+    ccprintf(*stream, "[");
+}
+
+void
+Json::lastDump()
+{
+    ccprintf(*stream, "]");
+}
+
+void
 Json::begin()
 {
+    printComma();
+
+    _first.push(true);
+
     ccprintf(*stream, "{");
 }

diff --git a/src/base/stats/json.hh b/src/base/stats/json.hh
index 9723211..7cd5cd4 100644
--- a/src/base/stats/json.hh
+++ b/src/base/stats/json.hh
@@ -72,6 +72,8 @@
     void begin() override;
     void end() override;

+    void firstDump() override;
+    void lastDump() override;
 };

 Output *initJson(const std::string &filename);
diff --git a/src/base/stats/output.hh b/src/base/stats/output.hh
index 6ff4a5d..7ad413c 100644
--- a/src/base/stats/output.hh
+++ b/src/base/stats/output.hh
@@ -59,6 +59,20 @@
 {
     virtual ~Output() {}

+    /**
+     * This function is used to signal to the output that this is the first
+ * time that "dump" has been called. Used in formats where the first dump + * must output a special character (e.g., beginning of an array in json)
+     */
+    virtual void firstDump() { }
+
+    /**
+     * This function is used to signal to the output that this is the last
+     * time that "dump" will be called. Used in formats where the last dump
+     * must output a special character (e.g., end of an array in json)
+     */
+    virtual void lastDump() { }
+
     virtual void begin() = 0;
     virtual void end() = 0;
     virtual bool valid() const = 0;
diff --git a/src/python/m5/simulate.py b/src/python/m5/simulate.py
index 080d725..e39735e 100644
--- a/src/python/m5/simulate.py
+++ b/src/python/m5/simulate.py
@@ -164,6 +164,7 @@

         # Python exit handlers happen in reverse order.
         # We want to dump stats last.
+        atexit.register(stats.finalDump)
         atexit.register(stats.dump)

         # register our C++ exit callback function with Python
diff --git a/src/python/m5/stats/__init__.py b/src/python/m5/stats/__init__.py
index 20867da..9a721e4 100644
--- a/src/python/m5/stats/__init__.py
+++ b/src/python/m5/stats/__init__.py
@@ -382,8 +382,14 @@
     global global_dump_roots
     all_roots.extend(global_dump_roots)

-    now = m5.curTick()
     global lastDump
+
+    if lastDump == 0:
+        first_dump = True
+    else:
+        first_dump = False
+
+    now = m5.curTick()
     assert lastDump <= now
     new_dump = lastDump != now
     lastDump = now
@@ -404,10 +410,19 @@

     for output in outputList:
         if output.valid():
+            if first_dump:
+                output.firstDump()
             output.begin()
             _dump_to_visitor(output, roots=all_roots)
             output.end()

+def finalDump():
+    """Calls lastDump() on all outputs to finalize the output files
+    """
+    for output in outputList:
+        if output.valid():
+            output.lastDump()
+
 def reset():
     '''Reset all statistics to the base state'''

diff --git a/src/python/pybind11/stats.cc b/src/python/pybind11/stats.cc
index 8ac46c2..9e0ff4b 100644
--- a/src/python/pybind11/stats.cc
+++ b/src/python/pybind11/stats.cc
@@ -117,6 +117,8 @@
         ;

     py::class_<Stats::Output>(m, "Output")
+        .def("firstDump", &Stats::Output::firstDump)
+        .def("lastDump", &Stats::Output::lastDump)
         .def("begin", &Stats::Output::begin)
         .def("end", &Stats::Output::end)
         .def("valid", &Stats::Output::valid)

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/35576
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: I482fd330af23b4a5f690739a81853eb5e2518839
Gerrit-Change-Number: 35576
Gerrit-PatchSet: 1
Gerrit-Owner: Jason Lowe-Power <power...@gmail.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