gem5  v21.1.0.2
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 namespace gem5
41 {
42 
43 void
45 {
47  auto *symtab = &tc->getSystemPtr()->workload->symtab(tc);
48 
49  DPRINTFN("------ Stack ------\n");
50 
51  std::string symbol;
52  for (int i = 0, size = stack.size(); i < size; ++i) {
53  Addr addr = stack[size - i - 1];
54  getSymbol(symbol, addr, symtab);
55  DPRINTFN("%#x: %s\n", addr, symbol);
56  }
57 }
58 
59 bool
60 BaseStackTrace::tryGetSymbol(std::string &symbol, Addr addr,
61  const loader::SymbolTable *symtab)
62 {
63  const auto it = symtab->find(addr);
64  if (it == symtab->end())
65  return false;
66  symbol = it->name;
67  return true;
68 }
69 
70 void
71 ProfileNode::dump(const std::string &symbol, uint64_t id,
72  const FunctionProfile &prof, std::ostream &os) const
73 {
74  ccprintf(os, "%#x %s %d ", id, symbol, count);
75  for (const auto &p: children)
76  ccprintf(os, "%#x ", (intptr_t)(p.second));
77 
78  ccprintf(os, "\n");
79 
80  for (const auto &p: children) {
81  Addr addr = p.first;
82  std::string symbol;
83 
84  prof.trace->getSymbol(symbol, addr, &prof.symtab);
85 
86  const auto *node = p.second;
87  node->dump(symbol, (intptr_t)node, prof, os);
88  }
89 }
90 
91 void
93 {
94  count = 0;
95  for (const auto &p: children)
96  p.second->clear();
97 }
98 
99 FunctionProfile::FunctionProfile(std::unique_ptr<BaseStackTrace> _trace,
100  const loader::SymbolTable &_symtab) :
101  symtab(_symtab), trace(std::move(_trace))
102 {
103  statistics::registerResetCallback([this]() { clear(); });
104 }
105 
106 ProfileNode *
108 {
109  ProfileNode *current = &top;
110  for (int i = 0, size = stack.size(); i < size; ++i) {
111  ProfileNode *&ptr = current->children[stack[size - i - 1]];
112  if (!ptr)
113  ptr = new ProfileNode;
114 
115  current = ptr;
116  }
117 
118  return current;
119 }
120 
121 void
123 {
124  top.clear();
125  pc_count.clear();
126 }
127 
128 void
129 FunctionProfile::dump(std::ostream &os) const
130 {
131  ccprintf(os, ">>>PC data\n");
132  for (const auto &p: pc_count) {
133  Addr pc = p.first;
134  Counter count = p.second;
135 
136  std::string symbol;
137  if (trace->tryGetSymbol(symbol, pc, &symtab))
138  ccprintf(os, "%s %d\n", symbol, count);
139  else
140  ccprintf(os, "%#x %d\n", pc, count);
141  }
142 
143  ccprintf(os, ">>>function data\n");
144  top.dump("top", 0, *this, os);
145 }
146 
147 void
149 {
150  node->count++;
151 
152  auto it = symtab.findNearest(pc);
153  if (it != symtab.end()) {
154  pc_count[it->address]++;
155  } else {
156  // record PC even if we don't have a symbol to avoid
157  // silently biasing the histogram
158  pc_count[pc]++;
159  }
160 }
161 
162 } // namespace gem5
gem5::ThreadContext::getSystemPtr
virtual System * getSystemPtr()=0
profile.hh
gem5::FunctionProfile::clear
void clear()
Definition: profile.cc:122
gem5::BaseStackTrace::stack
std::vector< Addr > stack
Definition: profile.hh:59
gem5::ProfileNode::dump
void dump(const std::string &symbol, uint64_t id, const FunctionProfile &prof, std::ostream &os) const
Definition: profile.cc:71
gem5::FunctionProfile::sample
void sample(ProfileNode *node, Addr pc)
Definition: profile.cc:148
gem5::FunctionProfile::ProfileNode
friend class ProfileNode
Definition: profile.hh:134
gem5::loader::SymbolTable::end
const_iterator end() const
Definition: symtab.hh:176
top
Definition: test.h:61
gem5::FunctionProfile
Definition: profile.hh:131
gem5::loader::SymbolTable
Definition: symtab.hh:65
gem5::FunctionProfile::pc_count
std::map< Addr, Counter > pc_count
Definition: profile.hh:138
gem5::System::workload
Workload * workload
OS kernel.
Definition: system.hh:335
std::vector< Addr >
gem5::ProfileNode::clear
void clear()
Definition: profile.cc:92
gem5::ProfileNode::count
Counter count
Definition: profile.hh:123
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::FunctionProfile::top
ProfileNode top
Definition: profile.hh:137
gem5::ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
gem5::BaseStackTrace::tryGetSymbol
virtual bool tryGetSymbol(std::string &symbol, Addr addr, const loader::SymbolTable *symtab)
Definition: profile.cc:60
gem5::X86ISA::stack
Bitfield< 17, 16 > stack
Definition: misc.hh:593
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
gem5::X86ISA::count
count
Definition: misc.hh:709
gem5::StringWrap
Definition: trace.hh:145
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
statistics.hh
gem5::ProfileNode
Definition: profile.hh:114
gem5::loader::SymbolTable::findNearest
const_iterator findNearest(Addr addr, Addr &next_addr) const
Find the nearest symbol equal to or less than the supplied address (e.g., the label for the enclosing...
Definition: symtab.hh:361
gem5::statistics::registerResetCallback
void registerResetCallback(const std::function< void()> &callback)
Register a callback that should be called whenever statistics are reset.
Definition: statistics.cc:272
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::Workload::symtab
virtual const loader::SymbolTable & symtab(ThreadContext *tc)=0
name
const std::string & name()
Definition: trace.cc:49
gem5::FunctionProfile::dump
void dump(std::ostream &out) const
Definition: profile.cc:129
gem5::loader::SymbolTable::find
const_iterator find(Addr address) const
Search for a symbol by its address.
Definition: symtab.hh:322
base.hh
std
Overload hash function for BasicBlockRange type.
Definition: types.hh:111
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:809
gem5::BaseStackTrace::tc
ThreadContext * tc
Definition: profile.hh:58
gem5::MipsISA::pc
Bitfield< 4 > pc
Definition: pra_constants.hh:243
gem5::statistics::Counter
double Counter
All counters are of 64-bit values.
Definition: types.hh:47
gem5::ProfileNode::children
ChildList children
Definition: profile.hh:120
gem5::ThreadContext::getCpuPtr
virtual BaseCPU * getCpuPtr()=0
trace.hh
gem5::FunctionProfile::trace
std::unique_ptr< BaseStackTrace > trace
Definition: profile.hh:139
symtab.hh
DPRINTFN
#define DPRINTFN(...)
Definition: trace.hh:214
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::BaseStackTrace::dump
void dump()
Definition: profile.cc:44
gem5::FunctionProfile::FunctionProfile
FunctionProfile(std::unique_ptr< BaseStackTrace > _trace, const loader::SymbolTable &symtab)
Definition: profile.cc:99
gem5::FunctionProfile::consume
ProfileNode * consume(ThreadContext *tc, const StaticInstPtr &inst)
Definition: profile.hh:153
thread_context.hh
callback.hh
gem5::BaseStackTrace::getSymbol
void getSymbol(std::string &symbol, Addr addr, const loader::SymbolTable *symtab)
Definition: profile.hh:106
gem5::FunctionProfile::symtab
const loader::SymbolTable & symtab
Definition: profile.hh:136
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84

Generated on Tue Sep 21 2021 12:25:06 for gem5 by doxygen 1.8.17