Ciro Santilli has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/28630 )

Change subject: stats: refactor ScalarPrint and VectorPrint constructors
......................................................................

stats: refactor ScalarPrint and VectorPrint constructors

This commit is only a refactoring to improve code quality after the need
was felt in the previous commits.

It creates constructors for those classes, and define the parameters that
don't change often across calls in the constructors.

Parameters that change often across calls such as value and name are now
passed to the operator() call directly.

Change-Id: Ibc12ed354e8e949d5a8b9ca23c6958c713f58800
---
M src/base/stats/text.cc
1 file changed, 148 insertions(+), 152 deletions(-)



diff --git a/src/base/stats/text.cc b/src/base/stats/text.cc
index aabcc0c..0ae7953 100644
--- a/src/base/stats/text.cc
+++ b/src/base/stats/text.cc
@@ -223,8 +223,6 @@

 struct ScalarPrint
 {
-    Result value;
-    string name;
     string desc;
     Flags flags;
     bool descriptions;
@@ -238,7 +236,24 @@
     int pdfstrSpaces;
     int cdfstrSpaces;

-    ScalarPrint(bool spaces) : spaces(spaces) {
+    ScalarPrint(
+        const string &desc,
+        const Flags &flags,
+        bool descriptions,
+        bool spaces,
+        bool names,
+        int precision,
+        const Result &pdf,
+        const Result &cdf
+    ) : desc(desc),
+        flags(flags),
+        descriptions(descriptions),
+        spaces(spaces),
+        names(names),
+        precision(precision),
+        pdf(pdf),
+        cdf(cdf)
+    {
         if (spaces) {
             nameSpaces = 40;
             valueSpaces = 12;
@@ -251,24 +266,34 @@
             cdfstrSpaces = 0;
         }
     }
-    void update(Result val, Result total);
-    void operator()(ostream &stream, bool oneLine = false,
-            bool firstBegin = false) const;
+    void update(const Result &value, const Result &total);
+    void operator()(
+        ostream &stream,
+        const string &name,
+        const Result &value,
+        bool oneLine = false,
+        bool firstBegin = false) const;
 };

 void
-ScalarPrint::update(Result val, Result total)
-{
-    value = val;
+ScalarPrint::update(
+    const Result &value,
+    const Result &total
+) {
     if (total) {
-        pdf = val / total;
+        pdf = value / total;
         cdf += pdf;
     }
 }

 void
-ScalarPrint::operator()(ostream &stream, bool oneLine, bool firstBegin) const
-{
+ScalarPrint::operator()(
+    ostream &stream,
+    const string &name,
+    const Result &value,
+    bool oneLine,
+    bool firstBegin
+) const {
     if (names && (
         (flags.isSet(nozero) && (!oneLine) && value == 0.0) ||
         (flags.isSet(nonan) && std::isnan(value))
@@ -306,7 +331,6 @@

 struct VectorPrint
 {
-    string name;
     string separatorString;
     string desc;
     vector<string> subnames;
@@ -316,24 +340,50 @@
     bool spaces;
     bool names;
     int precision;
-    VResult vec;
-    Result total;
     bool forceSubnames;
     int nameSpaces;

-    VectorPrint() = delete;
-    VectorPrint(bool spaces) : spaces(spaces) {
+    VectorPrint(
+        const string &separatorString,
+        const string &desc,
+        const Flags &flags,
+        bool descriptions,
+        bool spaces,
+        bool names,
+        int precision,
+        bool forceSubnames
+    ) : separatorString(separatorString),
+        desc(desc),
+        flags(flags),
+        descriptions(descriptions),
+        spaces(spaces),
+        names(names),
+        precision(precision),
+        forceSubnames(forceSubnames)
+    {
         if (spaces) {
             nameSpaces = 40;
         } else {
             nameSpaces = 0;
         }
     }
-    void operator()(ostream &stream, bool firstBegin) const;
+    void operator()(
+        ostream &stream,
+        const string &name,
+        const VResult &vec,
+        const Result &total,
+        bool firstBegin
+    ) const;
 };

 void
-VectorPrint::operator()(std::ostream &stream, bool firstBegin) const
+VectorPrint::operator()(
+    ostream &stream,
+    const string &name,
+    const VResult &vec,
+    const Result &total,
+    bool firstBegin
+) const
 {
     size_type _size = vec.size();
     Result _total = 0.0;
@@ -346,15 +396,16 @@

     string base = name + separatorString;

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

     bool havesub = !subnames.empty();

@@ -362,10 +413,14 @@
         // If forceSubnames is set, get the first subname (or index in
         // the case where there are no subnames) and append it to the
         // base name.
-        if (forceSubnames)
- print.name = base + (havesub ? subnames[0] : std::to_string(0));
-        print.value = vec[0];
-        print(stream, false, firstBegin);
+        std::string new_name;
+        if (forceSubnames) {
+            new_name = base + (havesub ? subnames[0]
+                    : std::to_string(0));
+        } else {
+            new_name = name;
+        }
+        print(stream, new_name, vec[0], false, firstBegin);
         return;
     }

@@ -380,12 +435,11 @@
         for (off_type i = 0; i < _size; ++i) {
             if (havesub && (i >= subnames.size() || subnames[i].empty()))
                 continue;
-
- print.name = base + (havesub ? subnames[i] : std::to_string(i));
+            const auto& name = base + (havesub ? subnames[i]
+                    : std::to_string(i));
             print.desc = subdescs.empty() ? desc : subdescs[i];
-
             print.update(vec[i], _total);
-            print(stream, flags.isSet(oneline), firstBegin);
+            print(stream, name, vec[i], flags.isSet(oneline), firstBegin);
         }

         if (flags.isSet(oneline)) {
@@ -400,10 +454,8 @@
     if (flags.isSet(::Stats::total)) {
         print.pdf = NAN;
         print.cdf = NAN;
-        print.name = base + "total";
         print.desc = desc;
-        print.value = total;
-        print(stream, false, firstBegin);
+        print(stream, base + "total", total, false, firstBegin);
     }
 }

@@ -470,50 +522,29 @@
     if (names && flags.isSet(nozero) && data.samples == 0) return;
     string base = name + separatorString;

-    ScalarPrint print(spaces);
-    print.names = names;
-    print.precision = precision;
-    print.flags = flags;
-    print.descriptions = descriptions;
-    print.desc = desc;
-    print.pdf = NAN;
-    print.cdf = NAN;
+    ScalarPrint print(desc, flags, descriptions, spaces, names, precision,
+            NAN, NAN);

     if (flags.isSet(oneline)) {
-        print.name = base + "bucket_size";
-        print.value = data.bucket_size;
-        print(stream, false, firstBegin);
-
-        print.name = base + "min_bucket";
-        print.value = data.min;
-        print(stream, false, firstBegin);
-
-        print.name = base + "max_bucket";
-        print.value = data.max;
-        print(stream, false, firstBegin);
+        print(stream, base + "bucket_size", data.bucket_size, false,
+                firstBegin);
+        print(stream, base + "min_bucket", data.min, false, firstBegin);
+        print(stream, base + "max_bucket", data.max, false, firstBegin);
     }
-
-    print.name = base + "samples";
-    print.value = data.samples;
-    print(stream, false, firstBegin);
-
-    print.name = base + "mean";
-    print.value = data.samples ? data.sum / data.samples : NAN;
-    print(stream, false, firstBegin);
+    print(stream, base + "samples", data.samples, false, firstBegin);
+    const auto& value = data.samples ? data.sum / data.samples : NAN;
+    print(stream, base + "mean", value, false, firstBegin);

     if (data.type == Hist) {
-        print.name = base + "gmean";
-        print.value = data.samples ? exp(data.logs / data.samples) : NAN;
-        print(stream, false, firstBegin);
+ const auto& value = data.samples ? exp(data.logs / data.samples) : NAN;
+        print(stream, base + "gmean", value, false, firstBegin);
     }

     Result stdev = NAN;
     if (data.samples)
         stdev = sqrt((data.samples * data.squares - data.sum * data.sum) /
                      (data.samples * (data.samples - 1.0)));
-    print.name = base + "stdev";
-    print.value = stdev;
-    print(stream, false, firstBegin);
+    print(stream, base + "stdev", stdev, false, firstBegin);

     if (data.type == Deviation)
         return;
@@ -534,9 +565,9 @@
     }

     if (data.type == Dist && data.underflow != NAN) {
-        print.name = base + "underflows";
         print.update(data.underflow, total);
-        print(stream, false, firstBegin);
+        print(stream, base + "underflows", data.underflow, false,
+                firstBegin);
     }

     if (flags.isSet(oneline)) {
@@ -552,10 +583,9 @@
         namestr << low;
         if (low < high)
             namestr << "-" << high;
-
-        print.name = namestr.str();
         print.update(data.cvec[i], total);
-        print(stream, flags.isSet(oneline), firstBegin);
+        print(stream, namestr.str(), data.cvec[i],
+                flags.isSet(oneline), firstBegin);
     }

     if (flags.isSet(oneline)) {
@@ -567,29 +597,23 @@
     }

     if (data.type == Dist && data.overflow != NAN) {
-        print.name = base + "overflows";
         print.update(data.overflow, total);
-        print(stream, false, firstBegin);
+        print(stream, base + "overflows", data.overflow,
+                false, firstBegin);
     }

     print.pdf = NAN;
     print.cdf = NAN;

     if (data.type == Dist && data.min_val != NAN) {
-        print.name = base + "min_value";
-        print.value = data.min_val;
-        print(stream, false, firstBegin);
+        print(stream, base + "min_value", data.min_val, false, firstBegin);
     }

     if (data.type == Dist && data.max_val != NAN) {
-        print.name = base + "max_value";
-        print.value = data.max_val;
-        print(stream, false, firstBegin);
+        print(stream, base + "max_value", data.max_val, false, firstBegin);
     }

-    print.name = base + "total";
-    print.value = total;
-    print(stream, false, firstBegin);
+    print(stream, base + "total", total, false, firstBegin);
 }

 void
@@ -598,18 +622,9 @@
     if (noOutput(info))
         return;

-    ScalarPrint print(spaces);
-    print.names = names;
-    print.value = info.result();
-    print.name = statName(info.name);
-    print.desc = info.desc;
-    print.flags = info.flags;
-    print.descriptions = descriptions;
-    print.precision = info.precision;
-    print.pdf = NAN;
-    print.cdf = NAN;
-
-    print(*stream, false, firstBegin);
+    ScalarPrint print(info.desc, info.flags, descriptions, spaces, names,
+            info.precision, NAN, NAN);
+    print(*stream, statName(info.name), info.result(), false, firstBegin);
 }

 void
@@ -617,21 +632,17 @@
 {
     if (noOutput(info))
         return;
-
     size_type size = info.size();
-    VectorPrint print(spaces);
-
-    print.name = statName(info.name);
-    print.separatorString = info.separatorString;
-    print.desc = info.desc;
-    print.flags = info.flags;
-    print.descriptions = descriptions;
-    print.names = names;
-    print.precision = info.precision;
-    print.vec = info.result();
-    print.total = info.total();
-    print.forceSubnames = false;
-
+    VectorPrint print(
+        info.separatorString,
+        info.desc,
+        info.flags,
+        descriptions,
+        spaces,
+        names,
+        info.precision,
+        false
+    );
     if (!info.subnames.empty()) {
         for (off_type i = 0; i < size; ++i) {
             if (!info.subnames[i].empty()) {
@@ -649,8 +660,8 @@
             }
         }
     }
-
-    print(*stream, firstBegin);
+    print(*stream, statName(info.name), info.result(), info.total(),
+            firstBegin);
 }

 void
@@ -660,8 +671,16 @@
         return;

     bool havesub = false;
-    VectorPrint print(spaces);
-
+    VectorPrint print(
+        info.separatorString,
+        info.desc,
+        info.flags,
+        descriptions,
+        spaces,
+        names,
+        info.precision,
+        true
+    );
     if (!info.y_subnames.empty()) {
         for (off_type i = 0; i < info.y; ++i) {
             if (!info.y_subnames[i].empty()) {
@@ -670,12 +689,6 @@
             }
         }
     }
-    print.flags = info.flags;
-    print.separatorString = info.separatorString;
-    print.descriptions = descriptions;
-    print.names = names;
-    print.precision = info.precision;
-    print.forceSubnames = true;

     if (!info.subnames.empty()) {
         for (off_type i = 0; i < info.x; ++i)
@@ -684,6 +697,7 @@
     }

     VResult tot_vec(info.y);
+    Result total;
     for (off_type i = 0; i < info.x; ++i) {
if (havesub && (i >= info.subnames.size() || info.subnames[i].empty()))
             continue;
@@ -691,20 +705,16 @@
         off_type iy = i * info.y;
         VResult yvec(info.y);

-        Result total = 0.0;
+        total = 0.0;
         for (off_type j = 0; j < info.y; ++j) {
             yvec[j] = info.cvec[iy + j];
             tot_vec[j] += yvec[j];
             total += yvec[j];
         }
-
-        print.name = statName(
+        const auto &name = statName(
             info.name + "_" +
             (havesub ? info.subnames[i] : std::to_string(i)));
-        print.desc = info.desc;
-        print.vec = yvec;
-        print.total = total;
-        print(*stream, firstBegin);
+        print(*stream, name, yvec, total, firstBegin);
     }

     // Create a subname for printing the total
@@ -712,12 +722,10 @@
     total_subname.push_back("total");

     if (info.flags.isSet(::Stats::total) && (info.x > 1)) {
-        print.name = statName(info.name);
         print.subnames = total_subname;
-        print.desc = info.desc;
-        print.vec = VResult(1, info.total());
-        print.flags = print.flags & ~total;
-        print(*stream, firstBegin);
+        print.flags = print.flags & ~::Stats::total;
+        print(*stream, statName(info.name), VResult(1, info.total()),
+                total, firstBegin);
     }
 }

@@ -798,28 +806,16 @@
 {
     string base = name + separatorString;

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

     MCounter::const_iterator it;
     for (it = data.cmap.begin(); it != data.cmap.end(); it++) {
         stringstream namestr;
         namestr << base;
-
         namestr <<(*it).first;
-        print.name = namestr.str();
-        print.value = (*it).second;
-        print(stream, false, firstBegin);
+        print(stream, namestr.str(), (*it).second, false, firstBegin);
     }
 }


--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/28630
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: Ibc12ed354e8e949d5a8b9ca23c6958c713f58800
Gerrit-Change-Number: 28630
Gerrit-PatchSet: 1
Gerrit-Owner: Ciro Santilli <ciro.santi...@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