gem5  v22.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 namespace gem5
41 {
42 
43 class ThreadContext;
44 class FunctionProfile;
45 
46 GEM5_DEPRECATED_NAMESPACE(Loader, loader);
47 namespace loader
48 {
49  class SymbolTable;
50 } // namespace loader
51 
53 {
54  private:
55  void dump();
56 
57  protected:
58  ThreadContext *tc = nullptr;
60 
61  // Subclasses should implement this function so that it collects the
62  // the current and return addresses on the stack in the "stack" vector.
63  virtual void trace(ThreadContext *tc, bool is_call) = 0;
64 
65  public:
66  BaseStackTrace() : stack(64) {}
67  virtual ~BaseStackTrace() {}
68 
69  void
71  {
72  tc = nullptr;
73  stack.clear();
74  }
75 
76  bool valid() const { return tc; }
77 
78  bool
80  {
81  if (!inst->isCall() && !inst->isReturn())
82  return false;
83 
84  if (valid())
85  clear();
86 
87  trace(tc, !inst->isReturn());
88  return true;
89  }
90 
91  const std::vector<Addr> &getstack() const { return stack; }
92 
93  void dprintf() { if (debug::Stack) dump(); }
94 
95  // This function can be overridden so that special addresses which don't
96  // actually refer to PCs can be translated into special names. For
97  // instance, the address 1 could translate into "user" for user level
98  // code when the symbol table only has kernel symbols.
99  //
100  // It should return whether addr was recognized and symbol has been set to
101  // something.
102  virtual bool tryGetSymbol(std::string &symbol, Addr addr,
103  const loader::SymbolTable *symtab);
104 
105  void
106  getSymbol(std::string &symbol, Addr addr,
107  const loader::SymbolTable *symtab)
108  {
109  panic_if(!tryGetSymbol(symbol, addr, symtab),
110  "Could not find symbol for address %#x\n", addr);
111  }
112 };
113 
115 {
116  private:
117  friend class FunctionProfile;
118 
119  typedef std::map<Addr, ProfileNode *> ChildList;
121 
122  public:
124 
125  public:
126  void dump(const std::string &symbol, uint64_t id,
127  const FunctionProfile &prof, std::ostream &os) const;
128  void clear();
129 };
130 
132 {
133  private:
134  friend class ProfileNode;
135 
138  std::map<Addr, Counter> pc_count;
139  std::unique_ptr<BaseStackTrace> trace;
140 
141  public:
142  FunctionProfile(std::unique_ptr<BaseStackTrace> _trace,
143  const loader::SymbolTable &symtab);
144 
145  ProfileNode *consume(ThreadContext *tc, const StaticInstPtr &inst);
147  void clear();
148  void dump(std::ostream &out) const;
149  void sample(ProfileNode *node, Addr pc);
150 };
151 
152 inline ProfileNode *
154 {
155  if (!trace->trace(tc, inst))
156  return nullptr;
157  trace->dprintf();
158  return consume(trace->getstack());
159 }
160 
161 } // namespace gem5
162 
163 #endif // __CPU_PROFILE_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
std::vector< Addr > stack
Definition: profile.hh:59
ThreadContext * tc
Definition: profile.hh:58
virtual void trace(ThreadContext *tc, bool is_call)=0
bool trace(ThreadContext *tc, const StaticInstPtr &inst)
Definition: profile.hh:79
const std::vector< Addr > & getstack() const
Definition: profile.hh:91
bool valid() const
Definition: profile.hh:76
virtual ~BaseStackTrace()
Definition: profile.hh:67
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
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
std::map< Addr, ProfileNode * > ChildList
Definition: profile.hh:119
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
bool isReturn() const
Definition: static_inst.hh:162
bool isCall() const
Definition: static_inst.hh:161
ThreadContext is the external interface to all thread state for anything outside of the CPU.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:204
Bitfield< 4 > pc
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
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
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)

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