gem5  v22.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 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
#define DPRINTFN(...)
Definition: trace.hh:214
std::vector< Addr > stack
Definition: profile.hh:59
ThreadContext * tc
Definition: profile.hh:58
void getSymbol(std::string &symbol, Addr addr, const loader::SymbolTable *symtab)
Definition: profile.hh:106
virtual bool tryGetSymbol(std::string &symbol, Addr addr, const loader::SymbolTable *symtab)
Definition: profile.cc:60
const loader::SymbolTable & symtab
Definition: profile.hh:136
ProfileNode * consume(ThreadContext *tc, const StaticInstPtr &inst)
Definition: profile.hh:153
std::map< Addr, Counter > pc_count
Definition: profile.hh:138
ProfileNode top
Definition: profile.hh:137
FunctionProfile(std::unique_ptr< BaseStackTrace > _trace, const loader::SymbolTable &symtab)
Definition: profile.cc:99
friend class ProfileNode
Definition: profile.hh:134
void dump(std::ostream &out) const
Definition: profile.cc:129
std::unique_ptr< BaseStackTrace > trace
Definition: profile.hh:139
void sample(ProfileNode *node, Addr pc)
Definition: profile.cc:148
virtual std::string name() const
Definition: named.hh:47
ChildList children
Definition: profile.hh:120
void dump(const std::string &symbol, uint64_t id, const FunctionProfile &prof, std::ostream &os) const
Definition: profile.cc:71
Workload * workload
OS kernel.
Definition: system.hh:329
virtual System * getSystemPtr()=0
virtual BaseCPU * getCpuPtr()=0
virtual const loader::SymbolTable & symtab(ThreadContext *tc)=0
const_iterator end() const
Definition: symtab.hh:176
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
const_iterator find(Addr address) const
Search for a symbol by its address.
Definition: symtab.hh:322
Definition: test.h:63
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 4 > pc
Bitfield< 54 > p
Definition: pagetable.hh:70
Bitfield< 17, 16 > stack
Definition: misc.hh:592
Bitfield< 17 > os
Definition: misc.hh:810
Bitfield< 3 > addr
Definition: types.hh:84
double Counter
All counters are of 64-bit values.
Definition: types.hh:47
void registerResetCallback(const std::function< void()> &callback)
Register a callback that should be called whenever statistics are reset.
Definition: statistics.cc:278
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
Overload hash function for BasicBlockRange type.
Definition: misc.hh:2826
Declaration of Statistics objects.
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:31 for gem5 by doxygen 1.9.1