gem5  v20.1.0.0
profile.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "cpu/profile.hh"
30 
31 #include <string>
32 
33 #include "base/callback.hh"
34 #include "base/loader/symtab.hh"
35 #include "base/statistics.hh"
36 #include "base/trace.hh"
37 #include "cpu/base.hh"
38 #include "cpu/thread_context.hh"
39 
40 void
42 {
43  StringWrap name(tc->getCpuPtr()->name());
44  auto *symtab = &tc->getSystemPtr()->workload->symtab(tc);
45 
46  DPRINTFN("------ Stack ------\n");
47 
48  std::string symbol;
49  for (int i = 0, size = stack.size(); i < size; ++i) {
50  Addr addr = stack[size - i - 1];
51  getSymbol(symbol, addr, symtab);
52  DPRINTFN("%#x: %s\n", addr, symbol);
53  }
54 }
55 
56 bool
57 BaseStackTrace::tryGetSymbol(std::string &symbol, Addr addr,
58  const Loader::SymbolTable *symtab)
59 {
60  const auto it = symtab->find(addr);
61  if (it == symtab->end())
62  return false;
63  symbol = it->name;
64  return true;
65 }
66 
67 void
68 ProfileNode::dump(const std::string &symbol, uint64_t id,
69  const FunctionProfile &prof, std::ostream &os) const
70 {
71  ccprintf(os, "%#x %s %d ", id, symbol, count);
72  for (const auto &p: children)
73  ccprintf(os, "%#x ", (intptr_t)(p.second));
74 
75  ccprintf(os, "\n");
76 
77  for (const auto &p: children) {
78  Addr addr = p.first;
79  std::string symbol;
80 
81  prof.trace->getSymbol(symbol, addr, &prof.symtab);
82 
83  const auto *node = p.second;
84  node->dump(symbol, (intptr_t)node, prof, os);
85  }
86 }
87 
88 void
90 {
91  count = 0;
92  for (const auto &p: children)
93  p.second->clear();
94 }
95 
96 FunctionProfile::FunctionProfile(std::unique_ptr<BaseStackTrace> _trace,
97  const Loader::SymbolTable &_symtab) :
98  symtab(_symtab), trace(std::move(_trace))
99 {
100  Stats::registerResetCallback([this]() { clear(); });
101 }
102 
103 ProfileNode *
105 {
106  ProfileNode *current = &top;
107  for (int i = 0, size = stack.size(); i < size; ++i) {
108  ProfileNode *&ptr = current->children[stack[size - i - 1]];
109  if (!ptr)
110  ptr = new ProfileNode;
111 
112  current = ptr;
113  }
114 
115  return current;
116 }
117 
118 void
120 {
121  top.clear();
122  pc_count.clear();
123 }
124 
125 void
126 FunctionProfile::dump(std::ostream &os) const
127 {
128  ccprintf(os, ">>>PC data\n");
129  for (const auto &p: pc_count) {
130  Addr pc = p.first;
131  Counter count = p.second;
132 
133  std::string symbol;
134  if (trace->tryGetSymbol(symbol, pc, &symtab))
135  ccprintf(os, "%s %d\n", symbol, count);
136  else
137  ccprintf(os, "%#x %d\n", pc, count);
138  }
139 
140  ccprintf(os, ">>>function data\n");
141  top.dump("top", 0, *this, os);
142 }
143 
144 void
146 {
147  node->count++;
148 
149  auto it = symtab.findNearest(pc);
150  if (it != symtab.end()) {
151  pc_count[it->address]++;
152  } else {
153  // record PC even if we don't have a symbol to avoid
154  // silently biasing the histogram
155  pc_count[pc]++;
156  }
157 }
BaseStackTrace::tc
ThreadContext * tc
Definition: profile.hh:54
Loader::SymbolTable::findNearest
const_iterator findNearest(Addr addr, Addr &nextaddr) const
Find the nearest symbol equal to or less than the supplied address (e.g., the label for the enclosing...
Definition: symtab.hh:209
X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:803
FunctionProfile::FunctionProfile
FunctionProfile(std::unique_ptr< BaseStackTrace > _trace, const Loader::SymbolTable &symtab)
Definition: profile.cc:96
profile.hh
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Workload::symtab
virtual const Loader::SymbolTable & symtab(ThreadContext *tc)=0
FunctionProfile::symtab
const Loader::SymbolTable & symtab
Definition: profile.hh:132
ProfileNode::count
Counter count
Definition: profile.hh:119
Loader::SymbolTable
Definition: symtab.hh:59
BaseStackTrace::dump
void dump()
Definition: profile.cc:41
top
Definition: test.h:61
std::vector< Addr >
X86ISA::count
count
Definition: misc.hh:703
ProfileNode::clear
void clear()
Definition: profile.cc:89
ProfileNode::children
ChildList children
Definition: profile.hh:116
FunctionProfile::consume
ProfileNode * consume(ThreadContext *tc, const StaticInstPtr &inst)
Definition: profile.hh:149
X86ISA::stack
Bitfield< 17, 16 > stack
Definition: misc.hh:587
Loader::SymbolTable::find
const_iterator find(Addr address) const
Definition: symtab.hh:181
Counter
int64_t Counter
Statistics counter type.
Definition: types.hh:58
FunctionProfile::top
ProfileNode top
Definition: profile.hh:133
FunctionProfile::pc_count
std::map< Addr, Counter > pc_count
Definition: profile.hh:134
System::workload
Workload * workload
OS kernel.
Definition: system.hh:327
BaseStackTrace::getSymbol
void getSymbol(std::string &symbol, Addr addr, const Loader::SymbolTable *symtab)
Definition: profile.hh:102
FunctionProfile
Definition: profile.hh:127
Stats::registerResetCallback
void registerResetCallback(const std::function< void()> &callback)
Register a callback that should be called whenever statistics are reset.
Definition: statistics.cc:537
MipsISA::pc
Bitfield< 4 > pc
Definition: pra_constants.hh:240
statistics.hh
FunctionProfile::sample
void sample(ProfileNode *node, Addr pc)
Definition: profile.cc:145
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
name
const std::string & name()
Definition: trace.cc:50
FunctionProfile::dump
void dump(std::ostream &out) const
Definition: profile.cc:126
Loader::SymbolTable::end
const_iterator end() const
Definition: symtab.hh:126
BaseStackTrace::stack
std::vector< Addr > stack
Definition: profile.hh:55
ProfileNode::dump
void dump(const std::string &symbol, uint64_t id, const FunctionProfile &prof, std::ostream &os) const
Definition: profile.cc:68
BaseStackTrace::tryGetSymbol
virtual bool tryGetSymbol(std::string &symbol, Addr addr, const Loader::SymbolTable *symtab)
Definition: profile.cc:57
base.hh
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
addr
ip6_addr_t addr
Definition: inet.hh:423
FunctionProfile::trace
std::unique_ptr< BaseStackTrace > trace
Definition: profile.hh:135
ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:127
StringWrap
Definition: trace.hh:134
trace.hh
symtab.hh
DPRINTFN
#define DPRINTFN(...)
Definition: trace.hh:238
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
FunctionProfile::ProfileNode
friend class ProfileNode
Definition: profile.hh:130
FunctionProfile::clear
void clear()
Definition: profile.cc:119
ThreadContext::getCpuPtr
virtual BaseCPU * getCpuPtr()=0
ProfileNode
Definition: profile.hh:110
thread_context.hh
callback.hh
ThreadContext::getSystemPtr
virtual System * getSystemPtr()=0

Generated on Wed Sep 30 2020 14:02:09 for gem5 by doxygen 1.8.17