gem5  v20.1.0.0
profile.hh
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 #ifndef __CPU_PROFILE_HH__
30 #define __CPU_PROFILE_HH__
31 
32 #include <map>
33 #include <string>
34 
35 #include "base/logging.hh"
36 #include "base/types.hh"
37 #include "cpu/static_inst.hh"
38 #include "debug/Stack.hh"
39 
40 class ThreadContext;
41 class FunctionProfile;
42 
43 namespace Loader
44 {
45  class SymbolTable;
46 } // Loader
47 
49 {
50  private:
51  void dump();
52 
53  protected:
54  ThreadContext *tc = nullptr;
56 
57  // Subclasses should implement this function so that it collects the
58  // the current and return addresses on the stack in the "stack" vector.
59  virtual void trace(ThreadContext *tc, bool is_call) = 0;
60 
61  public:
62  BaseStackTrace() : stack(64) {}
63  virtual ~BaseStackTrace() {}
64 
65  void
67  {
68  tc = nullptr;
69  stack.clear();
70  }
71 
72  bool valid() const { return tc; }
73 
74  bool
76  {
77  if (!inst->isCall() && !inst->isReturn())
78  return false;
79 
80  if (valid())
81  clear();
82 
83  trace(tc, !inst->isReturn());
84  return true;
85  }
86 
87  const std::vector<Addr> &getstack() const { return stack; }
88 
89  void dprintf() { if (DTRACE(Stack)) dump(); }
90 
91  // This function can be overridden so that special addresses which don't
92  // actually refer to PCs can be translated into special names. For
93  // instance, the address 1 could translate into "user" for user level
94  // code when the symbol table only has kernel symbols.
95  //
96  // It should return whether addr was recognized and symbol has been set to
97  // something.
98  virtual bool tryGetSymbol(std::string &symbol, Addr addr,
99  const Loader::SymbolTable *symtab);
100 
101  void
102  getSymbol(std::string &symbol, Addr addr,
103  const Loader::SymbolTable *symtab)
104  {
105  panic_if(!tryGetSymbol(symbol, addr, symtab),
106  "Could not find symbol for address %#x\n", addr);
107  }
108 };
109 
111 {
112  private:
113  friend class FunctionProfile;
114 
115  typedef std::map<Addr, ProfileNode *> ChildList;
117 
118  public:
120 
121  public:
122  void dump(const std::string &symbol, uint64_t id,
123  const FunctionProfile &prof, std::ostream &os) const;
124  void clear();
125 };
126 
128 {
129  private:
130  friend class ProfileNode;
131 
134  std::map<Addr, Counter> pc_count;
135  std::unique_ptr<BaseStackTrace> trace;
136 
137  public:
138  FunctionProfile(std::unique_ptr<BaseStackTrace> _trace,
139  const Loader::SymbolTable &symtab);
140 
141  ProfileNode *consume(ThreadContext *tc, const StaticInstPtr &inst);
143  void clear();
144  void dump(std::ostream &out) const;
145  void sample(ProfileNode *node, Addr pc);
146 };
147 
148 inline ProfileNode *
150 {
151  if (!trace->trace(tc, inst))
152  return nullptr;
153  trace->dprintf();
154  return consume(trace->getstack());
155 }
156 
157 #endif // __CPU_PROFILE_HH__
ProfileNode::ChildList
std::map< Addr, ProfileNode * > ChildList
Definition: profile.hh:115
BaseStackTrace::tc
ThreadContext * tc
Definition: profile.hh:54
BaseStackTrace::valid
bool valid() const
Definition: profile.hh:72
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
BaseStackTrace::clear
void clear()
Definition: profile.hh:66
BaseStackTrace::trace
bool trace(ThreadContext *tc, const StaticInstPtr &inst)
Definition: profile.hh:75
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
DTRACE
#define DTRACE(x)
Definition: debug.hh:146
std::vector< Addr >
BaseStackTrace
Definition: profile.hh:48
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
BaseStackTrace::~BaseStackTrace
virtual ~BaseStackTrace()
Definition: profile.hh:63
Counter
int64_t Counter
Statistics counter type.
Definition: types.hh:58
Loader
Definition: process.hh:39
FunctionProfile::top
ProfileNode top
Definition: profile.hh:133
FunctionProfile::pc_count
std::map< Addr, Counter > pc_count
Definition: profile.hh:134
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
BaseStackTrace::getSymbol
void getSymbol(std::string &symbol, Addr addr, const Loader::SymbolTable *symtab)
Definition: profile.hh:102
BaseStackTrace::getstack
const std::vector< Addr > & getstack() const
Definition: profile.hh:87
FunctionProfile
Definition: profile.hh:127
MipsISA::pc
Bitfield< 4 > pc
Definition: pra_constants.hh:240
FunctionProfile::sample
void sample(ProfileNode *node, Addr pc)
Definition: profile.cc:145
static_inst.hh
BaseStackTrace::BaseStackTrace
BaseStackTrace()
Definition: profile.hh:62
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
FunctionProfile::dump
void dump(std::ostream &out) const
Definition: profile.cc:126
BaseStackTrace::dprintf
void dprintf()
Definition: profile.hh:89
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
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
types.hh
BaseStackTrace::trace
virtual void trace(ThreadContext *tc, bool is_call)=0
addr
ip6_addr_t addr
Definition: inet.hh:423
FunctionProfile::trace
std::unique_ptr< BaseStackTrace > trace
Definition: profile.hh:135
StaticInst::isCall
bool isCall() const
Definition: static_inst.hh:176
logging.hh
RefCountingPtr< StaticInst >
FunctionProfile::clear
void clear()
Definition: profile.cc:119
StaticInst::isReturn
bool isReturn() const
Definition: static_inst.hh:177
ProfileNode
Definition: profile.hh:110

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