gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
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#include "sim/system.hh"
40
41namespace gem5
42{
43
44void
46{
47 StringWrap name(tc->getCpuPtr()->name());
48 auto *symtab = &tc->getSystemPtr()->workload->symtab(tc);
49
50 DPRINTFN("------ Stack ------\n");
51
52 std::string symbol;
53 for (int i = 0, size = stack.size(); i < size; ++i) {
54 Addr addr = stack[size - i - 1];
55 getSymbol(symbol, addr, symtab);
56 DPRINTFN("%#x: %s\n", addr, symbol);
57 }
58}
59
60bool
62 const loader::SymbolTable *symtab)
63{
64 const auto it = symtab->find(addr);
65 if (it == symtab->end())
66 return false;
67 symbol = it->name();
68 return true;
69}
70
71void
72ProfileNode::dump(const std::string &symbol, uint64_t id,
73 const FunctionProfile &prof, std::ostream &os) const
74{
75 ccprintf(os, "%#x %s %d ", id, symbol, count);
76 for (const auto &p: children)
77 ccprintf(os, "%#x ", (intptr_t)(p.second));
78
79 ccprintf(os, "\n");
80
81 for (const auto &p: children) {
82 Addr addr = p.first;
83 std::string symbol;
84
85 prof.trace->getSymbol(symbol, addr, &prof.symtab);
86
87 const auto *node = p.second;
88 node->dump(symbol, (intptr_t)node, prof, os);
89 }
90}
91
92void
94{
95 count = 0;
96 for (const auto &p: children)
97 p.second->clear();
98}
99
100FunctionProfile::FunctionProfile(std::unique_ptr<BaseStackTrace> _trace,
101 const loader::SymbolTable &_symtab) :
102 symtab(_symtab), trace(std::move(_trace))
103{
105}
106
109{
110 ProfileNode *current = &top;
111 for (int i = 0, size = stack.size(); i < size; ++i) {
112 ProfileNode *&ptr = current->children[stack[size - i - 1]];
113 if (!ptr)
114 ptr = new ProfileNode;
115
116 current = ptr;
117 }
118
119 return current;
120}
121
122void
124{
125 top.clear();
126 pc_count.clear();
127}
128
129void
130FunctionProfile::dump(std::ostream &os) const
131{
132 ccprintf(os, ">>>PC data\n");
133 for (const auto &p: pc_count) {
134 Addr pc = p.first;
135 Counter count = p.second;
136
137 std::string symbol;
138 if (trace->tryGetSymbol(symbol, pc, &symtab))
139 ccprintf(os, "%s %d\n", symbol, count);
140 else
141 ccprintf(os, "%#x %d\n", pc, count);
142 }
143
144 ccprintf(os, ">>>function data\n");
145 top.dump("top", 0, *this, os);
146}
147
148void
150{
151 node->count++;
152
153 auto it = symtab.findNearest(pc);
154 if (it != symtab.end()) {
155 pc_count[it->address()]++;
156 } else {
157 // record PC even if we don't have a symbol to avoid
158 // silently biasing the histogram
159 pc_count[pc]++;
160 }
161}
162
163} // namespace gem5
#define DPRINTFN(...)
Definition trace.hh:237
std::vector< Addr > stack
Definition profile.hh:58
ThreadContext * tc
Definition profile.hh:57
void getSymbol(std::string &symbol, Addr addr, const loader::SymbolTable *symtab)
Definition profile.hh:105
virtual bool tryGetSymbol(std::string &symbol, Addr addr, const loader::SymbolTable *symtab)
Definition profile.cc:61
const loader::SymbolTable & symtab
Definition profile.hh:135
ProfileNode * consume(ThreadContext *tc, const StaticInstPtr &inst)
Definition profile.hh:152
std::map< Addr, Counter > pc_count
Definition profile.hh:137
FunctionProfile(std::unique_ptr< BaseStackTrace > _trace, const loader::SymbolTable &symtab)
Definition profile.cc:100
friend class ProfileNode
Definition profile.hh:133
void dump(std::ostream &out) const
Definition profile.cc:130
std::unique_ptr< BaseStackTrace > trace
Definition profile.hh:138
void sample(ProfileNode *node, Addr pc)
Definition profile.cc:149
friend class FunctionProfile
Definition profile.hh:116
ChildList children
Definition profile.hh:119
void dump(const std::string &symbol, uint64_t id, const FunctionProfile &prof, std::ostream &os) const
Definition profile.cc:72
const_iterator end() const
Definition symtab.hh:278
const_iterator find(Addr address) const
Search for a symbol by its address.
Definition symtab.hh:435
STL vector class.
Definition stl.hh:37
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 4 > pc
Bitfield< 0 > p
Bitfield< 17, 16 > stack
Definition misc.hh:602
Bitfield< 17 > os
Definition misc.hh:838
Bitfield< 3 > addr
Definition types.hh:84
double Counter
All counters are of 64-bit values.
Definition types.hh:46
void registerResetCallback(const std::function< void()> &callback)
Register a callback that should be called whenever statistics are reset.
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
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 binary32.hh:81
Declaration of Statistics objects.
const std::string & name()
Definition trace.cc:48

Generated on Mon Oct 27 2025 04:13:01 for gem5 by doxygen 1.14.0