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