From bea82def100c57b9ef7e7c035c4d54698c6a6c69 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Sun, 12 Nov 2023 14:19:53 +0000 Subject: [PATCH] Add node memory usage info to V3Stats (#4684) --- src/V3Ast.h | 1 + src/V3Stats.cpp | 18 +++++++++++++++--- src/V3Stats.h | 30 +++++++++++++++++------------- src/V3StatsReport.cpp | 7 ++----- src/astgen | 4 ++-- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/V3Ast.h b/src/V3Ast.h index 5e6c8b483..c22913184 100644 --- a/src/V3Ast.h +++ b/src/V3Ast.h @@ -107,6 +107,7 @@ struct VNTypeInfo { OP_OPTIONAL, } m_opType[4]; const char* m_opNamep[4]; + size_t m_sizeof; }; class VNType final { diff --git a/src/V3Stats.cpp b/src/V3Stats.cpp index 03d823d8b..7ca0a5832 100644 --- a/src/V3Stats.cpp +++ b/src/V3Stats.cpp @@ -104,8 +104,8 @@ public: iterateConst(nodep); // Shorthand - const auto addStat = [&](const std::string& name, double count) { // - V3Stats::addStat(stage, name, count); + const auto addStat = [&](const std::string& name, double count, unsigned precision = 0) { + V3Stats::addStat(stage, name, count, precision); }; // Variable widths @@ -124,13 +124,25 @@ public: } } - // Node types + // Node types (also total memory usage) const auto typeName = [](int type) { return std::string{VNType{type}.ascii()}; }; + const auto typeSize = [](int type) { return VNType{type}.typeInfo()->m_sizeof; }; + size_t totalNodeMemoryUsage = 0; for (int t = 0; t < VNType::_ENUM_END; ++t) { if (const uint64_t count = m_counters.m_statTypeCount[t]) { + totalNodeMemoryUsage += count * typeSize(t); addStat("Node count, " + typeName(t), count); } } + addStat("Node memory TOTAL (MiB)", totalNodeMemoryUsage >> 20); + + // Node Memory usage + for (int t = 0; t < VNType::_ENUM_END; ++t) { + if (const uint64_t count = m_counters.m_statTypeCount[t]) { + const double share = 100.0 * count * typeSize(t) / totalNodeMemoryUsage; + addStat("Node memory share (%), " + typeName(t), share, 2); + } + } // Expression combinations for (int t1 = 0; t1 < VNType::_ENUM_END; ++t1) { diff --git a/src/V3Stats.h b/src/V3Stats.h index 8fd207d28..27af42bb8 100644 --- a/src/V3Stats.h +++ b/src/V3Stats.h @@ -68,7 +68,8 @@ public: class V3Statistic final { // A statistical entry we want published into the database const string m_name; ///< Name of this statistic - double m_count; ///< Count of occurrences/ value + double m_value; ///< Value of statistic (count, ratio, etc.) + unsigned m_precision; ///< Precision to print with (number of fractional digits) const string m_stage; ///< Runtime stage const bool m_sumit; ///< Do summation of similar stats const bool m_perf; ///< Performance section @@ -77,20 +78,22 @@ public: // METHODS string stage() const VL_MT_SAFE { return m_stage; } string name() const VL_MT_SAFE { return m_name; } - double count() const VL_MT_SAFE { return m_count; } + double value() const VL_MT_SAFE { return m_value; } + unsigned precision() const VL_MT_SAFE { return m_precision; } bool sumit() const VL_MT_SAFE { return m_sumit; } bool perf() const VL_MT_SAFE { return m_perf; } bool printit() const VL_MT_SAFE { return m_printit; } virtual void dump(std::ofstream& os) const VL_MT_SAFE; void combineWith(V3Statistic* otherp) { - m_count += otherp->count(); + m_value += otherp->value(); otherp->m_printit = false; } // CONSTRUCTORS - V3Statistic(const string& stage, const string& name, double count, bool sumit = false, - bool perf = false) + V3Statistic(const string& stage, const string& name, double value, unsigned precision, + bool sumit = false, bool perf = false) : m_name{name} - , m_count{count} + , m_value{value} + , m_precision{precision} , m_stage{stage} , m_sumit{sumit} , m_perf{perf} {} @@ -102,17 +105,18 @@ public: class V3Stats final { public: static void addStat(const V3Statistic&); - static void addStat(const string& stage, const string& name, double count) { - addStat(V3Statistic{stage, name, count}); + static void addStat(const string& stage, const string& name, double value, + unsigned precision = 0) { + addStat(V3Statistic{stage, name, value, precision}); } - static void addStat(const string& name, double count) { - addStat(V3Statistic{"*", name, count}); + static void addStat(const string& name, double value, unsigned precision = 0) { + addStat(V3Statistic{"*", name, value, precision}); } static void addStatSum(const string& name, double count) { - addStat(V3Statistic{"*", name, count, true}); + addStat(V3Statistic{"*", name, count, 0, true}); } - static void addStatPerf(const string& name, double count) { - addStat(V3Statistic{"*", name, count, true, true}); + static void addStatPerf(const string& name, double value) { + addStat(V3Statistic{"*", name, value, 6, true, true}); } /// Called each stage static void statsStage(const string& name); diff --git a/src/V3StatsReport.cpp b/src/V3StatsReport.cpp index a41f116d6..365540f99 100644 --- a/src/V3StatsReport.cpp +++ b/src/V3StatsReport.cpp @@ -181,11 +181,8 @@ StatsReport::StatColl StatsReport::s_allStats; // V3Statstic class void V3Statistic::dump(std::ofstream& os) const { - if (perf()) { - os << " " << std::right << std::fixed << std::setprecision(6) << std::setw(9) << count(); - } else { - os << " " << std::right << std::fixed << std::setprecision(0) << std::setw(9) << count(); - } + os << " " << std::right << std::fixed << std::setprecision(precision()) << std::setw(9) + << value(); } //###################################################################### diff --git a/src/astgen b/src/astgen index ae111ffbc..a9662cd6a 100755 --- a/src/astgen +++ b/src/astgen @@ -933,8 +933,8 @@ def write_ast_type_info(filename): ['VNTypeInfo::{0}'.format(s) for s in opTypeList]) opNameStr = ', '.join(['"{0}"'.format(s) for s in opNameList]) fh.write( - ' {{ "Ast{name}", {{{opTypeStr}}}, {{{opNameStr}}} }},\n'. - format( + ' {{ "Ast{name}", {{{opTypeStr}}}, {{{opNameStr}}}, sizeof(Ast{name}) }},\n' + .format( name=node.name, opTypeStr=opTypeStr, opNameStr=opNameStr,