Daniel Carvalho has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/45413 )

Change subject: base-stats: Create base struct for print structs
......................................................................

base-stats: Create base struct for print structs

Reduce code duplication.

Change-Id: I4d31b80ef946d9c1d964910e861088cdd2472f7c
Signed-off-by: Daniel R. Carvalho <[email protected]>
---
M src/base/stats/text.cc
1 file changed, 65 insertions(+), 94 deletions(-)



diff --git a/src/base/stats/text.cc b/src/base/stats/text.cc
index 26f1622..5718a36 100644
--- a/src/base/stats/text.cc
+++ b/src/base/stats/text.cc
@@ -219,17 +219,47 @@
     return val.str();
 }

-struct ScalarPrint
+struct BasePrint
+{
+    std::string name;
+    Flags flags;
+    int precision;
+    bool descriptions;
+    std::string desc;
+    bool units;
+    std::string unitStr;
+    bool spaces;
+
+    BasePrint(bool _spaces=false) : spaces(_spaces) {}
+
+    void
+    setup(std::string _name, Flags _flags, int _precision,
+        bool enable_descriptions, std::string _desc,
+        bool enable_units, std::string unit_str,
+        bool enable_spaces)
+    {
+        name = _name;
+        flags = _flags;
+        precision = _precision;
+        descriptions = enable_descriptions;
+        desc = _desc;
+        units = enable_units;
+        unitStr = unit_str;
+        spaces = enable_spaces;
+    }
+
+    void
+    printUnits(std::ostream &stream) const
+    {
+        if (units && !unitStr.empty()) {
+            ccprintf(stream, " (%s)", unitStr);
+        }
+    }
+};
+
+struct ScalarPrint : public BasePrint
 {
     Result value;
-    std::string name;
-    std::string desc;
-    std::string unitStr;
-    Flags flags;
-    bool descriptions;
-    bool spaces;
-    bool units;
-    int precision;
     Result pdf;
     Result cdf;
     int nameSpaces;
@@ -237,7 +267,9 @@
     int pdfstrSpaces;
     int cdfstrSpaces;

-    ScalarPrint(bool spaces) : spaces(spaces) {
+    ScalarPrint(bool spaces)
+      : BasePrint(spaces)
+    {
         if (spaces) {
             nameSpaces = 40;
             valueSpaces = 12;
@@ -294,33 +326,25 @@
             if (!desc.empty())
                 ccprintf(stream, " # %s", desc);
         }
-        if (units && !unitStr.empty()) {
-            ccprintf(stream, " (%s)", unitStr);
-        }
+        printUnits(stream);
         stream << std::endl;
     }
 }

-struct VectorPrint
+struct VectorPrint : public BasePrint
 {
-    std::string name;
     std::string separatorString;
-    std::string desc;
-    std::string unitStr;
     std::vector<std::string> subnames;
     std::vector<std::string> subdescs;
-    Flags flags;
-    bool units;
-    bool descriptions;
-    bool spaces;
-    int precision;
     VResult vec;
     Result total;
     bool forceSubnames;
     int nameSpaces;

     VectorPrint() = delete;
-    VectorPrint(bool spaces) : spaces(spaces) {
+    VectorPrint(bool spaces)
+      : BasePrint(spaces)
+    {
         if (spaces) {
             nameSpaces = 40;
         } else {
@@ -345,13 +369,8 @@
     std::string base = name + separatorString;

     ScalarPrint print(spaces);
-    print.name = name;
-    print.desc = desc;
-    print.unitStr = unitStr;
-    print.precision = precision;
-    print.descriptions = descriptions;
-    print.units = units;
-    print.flags = flags;
+    print.setup(name, flags, precision, descriptions, desc, units, unitStr,
+        spaces);
     print.pdf = _total ? 0.0 : NAN;
     print.cdf = _total ? 0.0 : NAN;

@@ -391,9 +410,7 @@
                 if (!desc.empty())
                     ccprintf(stream, " # %s", desc);
             }
-            if (units && !unitStr.empty()) {
-                ccprintf(stream, " (%s)", unitStr);
-            }
+            printUnits(stream);
             stream << std::endl;
         }
     }
@@ -409,17 +426,9 @@
     }
 }

-struct DistPrint
+struct DistPrint : public BasePrint
 {
-    std::string name;
     std::string separatorString;
-    std::string desc;
-    std::string unitStr;
-    Flags flags;
-    bool units;
-    bool descriptions;
-    bool spaces;
-    int precision;
     int nameSpaces;

     const DistData &data;
@@ -454,15 +463,10 @@
 void
 DistPrint::init(const Text *text, const Info &info)
 {
-    name = text->statName(info.name);
+    setup(text->statName(info.name), info.flags, info.precision,
+ text->descriptions, info.desc, text->units, info.unit->getUnitString(),
+        text->spaces);
     separatorString = info.separatorString;
-    desc = info.desc;
-    unitStr = info.unit->getUnitString();
-    flags = info.flags;
-    precision = info.precision;
-    descriptions = text->descriptions;
-    units = text->units;
-    spaces = text->spaces;
     if (spaces) {
         nameSpaces = 40;
     } else {
@@ -569,9 +573,7 @@
             if (!desc.empty())
                 ccprintf(stream, " # %s", desc);
         }
-        if (units && !unitStr.empty()) {
-            ccprintf(stream, " (%s)", unitStr);
-        }
+        printUnits(stream);
         stream << std::endl;
     }

@@ -608,14 +610,9 @@
         return;

     ScalarPrint print(spaces);
+ print.setup(statName(info.name), info.flags, info.precision, descriptions,
+        info.desc, units, info.unit->getUnitString(), spaces);
     print.value = info.result();
-    print.name = statName(info.name);
-    print.desc = info.desc;
-    print.unitStr = info.unit->getUnitString();
-    print.flags = info.flags;
-    print.descriptions = descriptions;
-    print.units = units;
-    print.precision = info.precision;
     print.pdf = NAN;
     print.cdf = NAN;

@@ -630,15 +627,9 @@

     size_type size = info.size();
     VectorPrint print(spaces);
-
-    print.name = statName(info.name);
+ print.setup(statName(info.name), info.flags, info.precision, descriptions,
+        info.desc, units, info.unit->getUnitString(), spaces);
     print.separatorString = info.separatorString;
-    print.desc = info.desc;
-    print.unitStr = info.unit->getUnitString();
-    print.flags = info.flags;
-    print.descriptions = descriptions;
-    print.units = units;
-    print.precision = info.precision;
     print.vec = info.result();
     print.total = info.total();
     print.forceSubnames = false;
@@ -672,7 +663,6 @@

     bool havesub = false;
     VectorPrint print(spaces);
-
     if (!info.y_subnames.empty()) {
         for (off_type i = 0; i < info.y; ++i) {
             if (!info.y_subnames[i].empty()) {
@@ -766,17 +756,9 @@
   This struct implements the output methods for the sparse
   histogram stat
 */
-struct SparseHistPrint
+struct SparseHistPrint : public BasePrint
 {
-    std::string name;
     std::string separatorString;
-    std::string desc;
-    std::string unitStr;
-    Flags flags;
-    bool descriptions;
-    bool units;
-    bool spaces;
-    int precision;

     const SparseHistData &data;

@@ -796,15 +778,10 @@
 void
 SparseHistPrint::init(const Text *text, const Info &info)
 {
-    name = text->statName(info.name);
+    setup(text->statName(info.name), info.flags, info.precision,
+        text->descriptions, info.desc, text->units,
+        info.unit->getUnitString(), text->spaces);
     separatorString = info.separatorString;
-    desc = info.desc;
-    unitStr = info.unit->getUnitString();
-    flags = info.flags;
-    precision = info.precision;
-    descriptions = text->descriptions;
-    units = text->units;
-    spaces = text->spaces;
 }

 /* Grab data from map and write to output stream */
@@ -814,16 +791,10 @@
     std::string base = name + separatorString;

     ScalarPrint print(spaces);
-    print.precision = precision;
-    print.flags = flags;
-    print.descriptions = descriptions;
-    print.units = units;
-    print.desc = desc;
-    print.unitStr = unitStr;
+ print.setup(base + "samples", flags, precision, descriptions, desc, units,
+        unitStr, spaces);
     print.pdf = NAN;
     print.cdf = NAN;
-
-    print.name = base + "samples";
     print.value = data.samples;
     print(stream);


--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/45413
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: I4d31b80ef946d9c1d964910e861088cdd2472f7c
Gerrit-Change-Number: 45413
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Carvalho <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to