From f1fc7ba257dbd3e16e2a41900ca84935a40d0204 Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Sat, 4 Jul 2020 15:28:02 -0700 Subject: [PATCH] cpu: Slightly modernize and simplify code in cpu/profile.(hh|cc). Change-Id: Ideb104d20b333305ead2356cbfff2aac2e0173b5 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/30960 Maintainer: Gabe Black Tested-by: kokoro Reviewed-by: Andreas Sandberg --- src/cpu/o3/thread_state.hh | 2 +- src/cpu/profile.cc | 80 +++++++++++++++++--------------------- src/cpu/profile.hh | 10 ++--- src/cpu/simple_thread.cc | 2 +- 4 files changed, 41 insertions(+), 53 deletions(-) diff --git a/src/cpu/o3/thread_state.hh b/src/cpu/o3/thread_state.hh index 76e201184..833b2b809 100644 --- a/src/cpu/o3/thread_state.hh +++ b/src/cpu/o3/thread_state.hh @@ -154,7 +154,7 @@ struct O3ThreadState : public ThreadState { { OutputStream *os( simout.create(csprintf("profile.%s.dat", cpu->name()))); - profile->dump(tc, *os->stream()); + profile->dump(*os->stream()); simout.close(os); } }; diff --git a/src/cpu/profile.cc b/src/cpu/profile.cc index d1826e439..972346e18 100644 --- a/src/cpu/profile.cc +++ b/src/cpu/profile.cc @@ -30,49 +30,39 @@ #include -#include "base/bitfield.hh" #include "base/callback.hh" #include "base/loader/symtab.hh" #include "base/statistics.hh" #include "base/trace.hh" -#include "cpu/base.hh" #include "cpu/thread_context.hh" -using namespace std; - -ProfileNode::ProfileNode() - : count(0) -{ } - void -ProfileNode::dump(const string &symbol, uint64_t id, - const Loader::SymbolTable &symtab, ostream &os) const +ProfileNode::dump(const std::string &symbol, uint64_t id, + const Loader::SymbolTable &symtab, std::ostream &os) const { ccprintf(os, "%#x %s %d ", id, symbol, count); - ChildList::const_iterator i, end = children.end(); - for (i = children.begin(); i != end; ++i) { - const ProfileNode *node = i->second; - ccprintf(os, "%#x ", (intptr_t)node); - } + for (const auto &p: children) + ccprintf(os, "%#x ", (intptr_t)(p.second)); ccprintf(os, "\n"); - Loader::SymbolTable::const_iterator it; - for (i = children.begin(); i != end; ++i) { - Addr addr = i->first; - string symbol; - if (addr == 1) + for (const auto &p: children) { + Addr addr = p.first; + std::string symbol; + if (addr == 1) { symbol = "user"; - else if (addr == 2) + } else if (addr == 2) { symbol = "console"; - else if (addr == 3) + } else if (addr == 3) { symbol = "unknown"; - else if ((it = symtab.find(addr)) != symtab.end()) + } else { + const auto it = symtab.find(addr); + panic_if(it == symtab.end(), + "Could not find symbol for address %#x\n", addr); symbol = it->name; - else - panic("could not find symbol for address %#x\n", addr); + } - const ProfileNode *node = i->second; + const auto *node = p.second; node->dump(symbol, (intptr_t)node, symtab, os); } } @@ -81,13 +71,12 @@ void ProfileNode::clear() { count = 0; - ChildList::iterator i, end = children.end(); - for (i = children.begin(); i != end; ++i) - i->second->clear(); + for (const auto &p: children) + p.second->clear(); } -FunctionProfile::FunctionProfile(const Loader::SymbolTable &_symtab) - : reset(0), symtab(_symtab) +FunctionProfile::FunctionProfile(const Loader::SymbolTable &_symtab) : + symtab(_symtab) { reset = new MakeCallback(this); Stats::registerResetCallback(reset); @@ -95,17 +84,16 @@ FunctionProfile::FunctionProfile(const Loader::SymbolTable &_symtab) FunctionProfile::~FunctionProfile() { - if (reset) - delete reset; + delete reset; } ProfileNode * -FunctionProfile::consume(const vector &stack) +FunctionProfile::consume(const std::vector &stack) { ProfileNode *current = ⊤ for (int i = 0, size = stack.size(); i < size; ++i) { ProfileNode *&ptr = current->children[stack[size - i - 1]]; - if (ptr == NULL) + if (!ptr) ptr = new ProfileNode; current = ptr; @@ -122,23 +110,25 @@ FunctionProfile::clear() } void -FunctionProfile::dump(ThreadContext *tc, ostream &os) const +FunctionProfile::dump(std::ostream &os) const { ccprintf(os, ">>>PC data\n"); - map::const_iterator i, end = pc_count.end(); - for (i = pc_count.begin(); i != end; ++i) { - Addr pc = i->first; - Counter count = i->second; + for (const auto &p: pc_count) { + Addr pc = p.first; + Counter count = p.second; - Loader::SymbolTable::const_iterator it; if (pc == 1) { ccprintf(os, "user %d\n", count); - } else if ((it = symtab.find(pc)) != symtab.end() && - !it->name.empty()) { + continue; + } + + const auto it = symtab.find(pc); + if (it != symtab.end() && !it->name.empty()) { ccprintf(os, "%s %d\n", it->name, count); - } else { - ccprintf(os, "%#x %d\n", pc, count); + continue; } + + ccprintf(os, "%#x %d\n", pc, count); } ccprintf(os, ">>>function data\n"); diff --git a/src/cpu/profile.hh b/src/cpu/profile.hh index 96d552629..f6b98d3f6 100644 --- a/src/cpu/profile.hh +++ b/src/cpu/profile.hh @@ -47,11 +47,9 @@ class ProfileNode ChildList children; public: - Counter count; + Counter count = 0; public: - ProfileNode(); - void dump(const std::string &symbol, uint64_t id, const Loader::SymbolTable &symtab, std::ostream &os) const; void clear(); @@ -61,7 +59,7 @@ class Callback; class FunctionProfile { private: - Callback *reset; + Callback *reset = nullptr; const Loader::SymbolTable &symtab; ProfileNode top; std::map pc_count; @@ -74,7 +72,7 @@ class FunctionProfile ProfileNode *consume(ThreadContext *tc, const StaticInstPtr &inst); ProfileNode *consume(const std::vector &stack); void clear(); - void dump(ThreadContext *tc, std::ostream &out) const; + void dump(std::ostream &out) const; void sample(ProfileNode *node, Addr pc); }; @@ -82,7 +80,7 @@ inline ProfileNode * FunctionProfile::consume(ThreadContext *tc, const StaticInstPtr &inst) { if (!trace.trace(tc, inst)) - return NULL; + return nullptr; trace.dprintf(); return consume(trace.getstack()); } diff --git a/src/cpu/simple_thread.cc b/src/cpu/simple_thread.cc index 8f2ab54c8..c30872c6a 100644 --- a/src/cpu/simple_thread.cc +++ b/src/cpu/simple_thread.cc @@ -150,7 +150,7 @@ void SimpleThread::dumpFuncProfile() { OutputStream *os(simout.create(csprintf("profile.%s.dat", baseCpu->name()))); - profile->dump(this, *os->stream()); + profile->dump(*os->stream()); simout.close(os); } -- 2.30.2