cpu-o3: MemDepUnit tracks load-acquire/store-release
[gem5.git] / src / cpu / profile.cc
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/bitfield.hh"
34 #include "base/callback.hh"
35 #include "base/loader/symtab.hh"
36 #include "base/statistics.hh"
37 #include "base/trace.hh"
38 #include "cpu/base.hh"
39 #include "cpu/thread_context.hh"
40
41 using namespace std;
42
43 ProfileNode::ProfileNode()
44 : count(0)
45 { }
46
47 void
48 ProfileNode::dump(const string &symbol, uint64_t id,
49 const Loader::SymbolTable *symtab, ostream &os) const
50 {
51 ccprintf(os, "%#x %s %d ", id, symbol, count);
52 ChildList::const_iterator i, end = children.end();
53 for (i = children.begin(); i != end; ++i) {
54 const ProfileNode *node = i->second;
55 ccprintf(os, "%#x ", (intptr_t)node);
56 }
57
58 ccprintf(os, "\n");
59
60 for (i = children.begin(); i != end; ++i) {
61 Addr addr = i->first;
62 string symbol;
63 if (addr == 1)
64 symbol = "user";
65 else if (addr == 2)
66 symbol = "console";
67 else if (addr == 3)
68 symbol = "unknown";
69 else if (!symtab->findSymbol(addr, symbol))
70 panic("could not find symbol for address %#x\n", addr);
71
72 const ProfileNode *node = i->second;
73 node->dump(symbol, (intptr_t)node, symtab, os);
74 }
75 }
76
77 void
78 ProfileNode::clear()
79 {
80 count = 0;
81 ChildList::iterator i, end = children.end();
82 for (i = children.begin(); i != end; ++i)
83 i->second->clear();
84 }
85
86 FunctionProfile::FunctionProfile(const Loader::SymbolTable *_symtab)
87 : reset(0), symtab(_symtab)
88 {
89 reset = new MakeCallback<FunctionProfile, &FunctionProfile::clear>(this);
90 Stats::registerResetCallback(reset);
91 }
92
93 FunctionProfile::~FunctionProfile()
94 {
95 if (reset)
96 delete reset;
97 }
98
99 ProfileNode *
100 FunctionProfile::consume(const vector<Addr> &stack)
101 {
102 ProfileNode *current = &top;
103 for (int i = 0, size = stack.size(); i < size; ++i) {
104 ProfileNode *&ptr = current->children[stack[size - i - 1]];
105 if (ptr == NULL)
106 ptr = new ProfileNode;
107
108 current = ptr;
109 }
110
111 return current;
112 }
113
114 void
115 FunctionProfile::clear()
116 {
117 top.clear();
118 pc_count.clear();
119 }
120
121 void
122 FunctionProfile::dump(ThreadContext *tc, ostream &os) const
123 {
124 ccprintf(os, ">>>PC data\n");
125 map<Addr, Counter>::const_iterator i, end = pc_count.end();
126 for (i = pc_count.begin(); i != end; ++i) {
127 Addr pc = i->first;
128 Counter count = i->second;
129
130 std::string symbol;
131 if (pc == 1)
132 ccprintf(os, "user %d\n", count);
133 else if (symtab->findSymbol(pc, symbol) && !symbol.empty())
134 ccprintf(os, "%s %d\n", symbol, count);
135 else
136 ccprintf(os, "%#x %d\n", pc, count);
137 }
138
139 ccprintf(os, ">>>function data\n");
140 top.dump("top", 0, symtab, os);
141 }
142
143 void
144 FunctionProfile::sample(ProfileNode *node, Addr pc)
145 {
146 node->count++;
147
148 Addr symaddr;
149 if (symtab->findNearestAddr(pc, symaddr)) {
150 pc_count[symaddr]++;
151 } else {
152 // record PC even if we don't have a symbol to avoid
153 // silently biasing the histogram
154 pc_count[pc]++;
155 }
156 }