arch,cpu: Consolidate most of the StackTrace classes into a base class.

These classes are all basically empty now that Alpha has been deleted,
except in cases where the arch versions had copied versions of the Alpha
code.

This change pulls all the generic logic out of the arch versions, making
the arch versions much simpler and making it clearer what the core
functionality of the class is, and what parts are architecture specific
details.

In the future, the way the StackTrace class is instantiated should be
delegated to the Workload class so that ISA agnostic code doesn't need
to know about a particular ISA's StackTrace class, and so that
StackTrace logic can, at least theoretically, be specialized for a
particular workload. The way a stack trace is collected could vary from
OS to OS, for example.

Change-Id: Id8108f94e9fe8baf9b4056f2b6404571e9fa52f1
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/30961
Reviewed-by: Gabe Black <gabeblack@google.com>
Maintainer: Gabe Black <gabeblack@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2020-07-04 23:16:08 -07:00
parent f1fc7ba257
commit a6721c7a73
20 changed files with 155 additions and 1037 deletions

View File

@@ -41,7 +41,9 @@
#ifndef __CPU_O3_THREAD_STATE_HH__
#define __CPU_O3_THREAD_STATE_HH__
#include "arch/stacktrace.hh"
#include "base/callback.hh"
#include "base/compiler.hh"
#include "base/output.hh"
#include "cpu/thread_context.hh"
#include "cpu/thread_state.hh"
@@ -103,6 +105,7 @@ struct O3ThreadState : public ThreadState {
if (cpu->params()->profile) {
profile = new FunctionProfile(
m5::make_unique<TheISA::StackTrace>(),
cpu->params()->system->workload->symtab(tc));
Callback *cb =
new MakeCallback<O3ThreadState,

View File

@@ -34,11 +34,39 @@
#include "base/loader/symtab.hh"
#include "base/statistics.hh"
#include "base/trace.hh"
#include "cpu/base.hh"
#include "cpu/thread_context.hh"
void
BaseStackTrace::dump()
{
StringWrap name(tc->getCpuPtr()->name());
auto *symtab = &tc->getSystemPtr()->workload->symtab(tc);
DPRINTFN("------ Stack ------\n");
std::string symbol;
for (int i = 0, size = stack.size(); i < size; ++i) {
Addr addr = stack[size - i - 1];
getSymbol(symbol, addr, symtab);
DPRINTFN("%#x: %s\n", addr, symbol);
}
}
bool
BaseStackTrace::tryGetSymbol(std::string &symbol, Addr addr,
const Loader::SymbolTable *symtab)
{
const auto it = symtab->find(addr);
if (it == symtab->end())
return false;
symbol = it->name;
return true;
}
void
ProfileNode::dump(const std::string &symbol, uint64_t id,
const Loader::SymbolTable &symtab, std::ostream &os) const
const FunctionProfile &prof, std::ostream &os) const
{
ccprintf(os, "%#x %s %d ", id, symbol, count);
for (const auto &p: children)
@@ -49,21 +77,11 @@ ProfileNode::dump(const std::string &symbol, uint64_t id,
for (const auto &p: children) {
Addr addr = p.first;
std::string symbol;
if (addr == 1) {
symbol = "user";
} else if (addr == 2) {
symbol = "console";
} else if (addr == 3) {
symbol = "unknown";
} else {
const auto it = symtab.find(addr);
panic_if(it == symtab.end(),
"Could not find symbol for address %#x\n", addr);
symbol = it->name;
}
prof.trace->getSymbol(symbol, addr, &prof.symtab);
const auto *node = p.second;
node->dump(symbol, (intptr_t)node, symtab, os);
node->dump(symbol, (intptr_t)node, prof, os);
}
}
@@ -75,8 +93,9 @@ ProfileNode::clear()
p.second->clear();
}
FunctionProfile::FunctionProfile(const Loader::SymbolTable &_symtab) :
symtab(_symtab)
FunctionProfile::FunctionProfile(std::unique_ptr<BaseStackTrace> _trace,
const Loader::SymbolTable &_symtab) :
symtab(_symtab), trace(std::move(_trace))
{
reset = new MakeCallback<FunctionProfile, &FunctionProfile::clear>(this);
Stats::registerResetCallback(reset);
@@ -117,22 +136,15 @@ FunctionProfile::dump(std::ostream &os) const
Addr pc = p.first;
Counter count = p.second;
if (pc == 1) {
ccprintf(os, "user %d\n", count);
continue;
}
const auto it = symtab.find(pc);
if (it != symtab.end() && !it->name.empty()) {
ccprintf(os, "%s %d\n", it->name, count);
continue;
}
ccprintf(os, "%#x %d\n", pc, count);
std::string symbol;
if (trace->tryGetSymbol(symbol, pc, &symtab))
ccprintf(os, "%s %d\n", symbol, count);
else
ccprintf(os, "%#x %d\n", pc, count);
}
ccprintf(os, ">>>function data\n");
top.dump("top", 0, symtab, os);
top.dump("top", 0, *this, os);
}
void

View File

@@ -30,13 +30,82 @@
#define __CPU_PROFILE_HH__
#include <map>
#include <string>
#include "arch/stacktrace.hh"
#include "base/logging.hh"
#include "base/types.hh"
#include "config/the_isa.hh"
#include "cpu/static_inst.hh"
#include "debug/Stack.hh"
class ThreadContext;
class FunctionProfile;
namespace Loader
{
class SymbolTable;
} // Loader
class BaseStackTrace
{
private:
void dump();
protected:
ThreadContext *tc = nullptr;
std::vector<Addr> stack;
// Subclasses should implement this function so that it collects the
// the current and return addresses on the stack in the "stack" vector.
virtual void trace(ThreadContext *tc, bool is_call) = 0;
public:
BaseStackTrace() : stack(64) {}
virtual ~BaseStackTrace() {}
void
clear()
{
tc = nullptr;
stack.clear();
}
bool valid() const { return tc; }
bool
trace(ThreadContext *tc, const StaticInstPtr &inst)
{
if (!inst->isCall() && !inst->isReturn())
return false;
if (valid())
clear();
trace(tc, !inst->isReturn());
return true;
}
const std::vector<Addr> &getstack() const { return stack; }
void dprintf() { if (DTRACE(Stack)) dump(); }
// This function can be overridden so that special addresses which don't
// actually refer to PCs can be translated into special names. For
// instance, the address 1 could translate into "user" for user level
// code when the symbol table only has kernel symbols.
//
// It should return whether addr was recognized and symbol has been set to
// something.
virtual bool tryGetSymbol(std::string &symbol, Addr addr,
const Loader::SymbolTable *symtab);
void
getSymbol(std::string &symbol, Addr addr,
const Loader::SymbolTable *symtab)
{
panic_if(!tryGetSymbol(symbol, addr, symtab),
"Could not find symbol for address %#x\n", addr);
}
};
class ProfileNode
{
@@ -51,7 +120,7 @@ class ProfileNode
public:
void dump(const std::string &symbol, uint64_t id,
const Loader::SymbolTable &symtab, std::ostream &os) const;
const FunctionProfile &prof, std::ostream &os) const;
void clear();
};
@@ -59,14 +128,17 @@ class Callback;
class FunctionProfile
{
private:
friend class ProfileNode;
Callback *reset = nullptr;
const Loader::SymbolTable &symtab;
ProfileNode top;
std::map<Addr, Counter> pc_count;
TheISA::StackTrace trace;
std::unique_ptr<BaseStackTrace> trace;
public:
FunctionProfile(const Loader::SymbolTable &symtab);
FunctionProfile(std::unique_ptr<BaseStackTrace> _trace,
const Loader::SymbolTable &symtab);
~FunctionProfile();
ProfileNode *consume(ThreadContext *tc, const StaticInstPtr &inst);
@@ -79,10 +151,10 @@ class FunctionProfile
inline ProfileNode *
FunctionProfile::consume(ThreadContext *tc, const StaticInstPtr &inst)
{
if (!trace.trace(tc, inst))
if (!trace->trace(tc, inst))
return nullptr;
trace.dprintf();
return consume(trace.getstack());
trace->dprintf();
return consume(trace->getstack());
}
#endif // __CPU_PROFILE_HH__

View File

@@ -46,6 +46,7 @@
#include "arch/stacktrace.hh"
#include "arch/utility.hh"
#include "base/callback.hh"
#include "base/compiler.hh"
#include "base/cprintf.hh"
#include "base/output.hh"
#include "base/trace.hh"
@@ -92,7 +93,8 @@ SimpleThread::SimpleThread(BaseCPU *_cpu, int _thread_num, System *_sys,
clearArchRegs();
if (baseCpu->params()->profile) {
profile = new FunctionProfile(system->workload->symtab(this));
profile = new FunctionProfile(m5::make_unique<TheISA::StackTrace>(),
system->workload->symtab(this));
Callback *cb =
new MakeCallback<SimpleThread,
&SimpleThread::dumpFuncProfile>(this);