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