profile.cc revision 1977
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 i->second->clear(); 80} 81 82FunctionProfile::FunctionProfile(const SymbolTable *_symtab) 83 : symtab(_symtab) 84{ 85} 86 87FunctionProfile::~FunctionProfile() 88{ 89} 90 91ProfileNode * 92FunctionProfile::consume(const vector<Addr> &stack) 93{ 94 ProfileNode *current = ⊤ 95 for (int i = 0, size = stack.size(); i < size; ++i) { 96 ProfileNode *&ptr = current->children[stack[size - i - 1]]; 97 if (ptr == NULL) 98 ptr = new ProfileNode; 99 100 current = ptr; 101 } 102 103 return current; 104} 105 106void 107FunctionProfile::clear() 108{ 109 top.clear(); 110 pc_count.clear(); 111} 112 113void 114FunctionProfile::dump(ExecContext *xc, ostream &os) const 115{ 116 ccprintf(os, ">>>PC data\n"); 117 map<Addr, Counter>::const_iterator i, end = pc_count.end(); 118 for (i = pc_count.begin(); i != end; ++i) { 119 Addr pc = i->first; 120 Counter count = i->second; 121 122 std::string symbol; 123 if (pc == 1) 124 ccprintf(os, "user %d\n", count); 125 else if (symtab->findSymbol(pc, symbol) && !symbol.empty()) 126 ccprintf(os, "%s %d\n", symbol, count); 127 else 128 ccprintf(os, "%#x %d\n", pc, count); 129 } 130 131 ccprintf(os, ">>>function data\n"); 132 top.dump("top", 0, symtab, os); 133} 134 135void 136FunctionProfile::sample(ProfileNode *node, Addr pc) 137{ 138 node->count++; 139 140 Addr symaddr; 141 if (symtab->findNearestAddr(pc, symaddr)) { 142 pc_count[symaddr]++; 143 } else { 144 // record PC even if we don't have a symbol to avoid 145 // silently biasing the histogram 146 pc_count[pc]++; 147 } 148} 149