profile.cc revision 2235
12315SN/A/*
22332SN/A * Copyright (c) 2005 The Regents of The University of Michigan
32315SN/A * All rights reserved.
42315SN/A *
52315SN/A * Redistribution and use in source and binary forms, with or without
62315SN/A * modification, are permitted provided that the following conditions are
72315SN/A * met: redistributions of source code must retain the above copyright
82315SN/A * notice, this list of conditions and the following disclaimer;
92315SN/A * redistributions in binary form must reproduce the above copyright
102315SN/A * notice, this list of conditions and the following disclaimer in the
112315SN/A * documentation and/or other materials provided with the distribution;
122315SN/A * neither the name of the copyright holders nor the names of its
132315SN/A * contributors may be used to endorse or promote products derived from
142315SN/A * this software without specific prior written permission.
152315SN/A *
162315SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172315SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182315SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192315SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202315SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212315SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222315SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232315SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242315SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252315SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262315SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272689Sktlim@umich.edu */
282689Sktlim@umich.edu
292315SN/A#include <string>
302315SN/A
312315SN/A#include "base/bitfield.hh"
322315SN/A#include "base/callback.hh"
332315SN/A#include "base/statistics.hh"
342315SN/A#include "base/trace.hh"
352315SN/A#include "base/loader/symtab.hh"
362315SN/A#include "cpu/base.hh"
372315SN/A#include "cpu/exec_context.hh"
382669Sktlim@umich.edu#include "cpu/profile.hh"
392315SN/A
402315SN/Ausing namespace std;
412315SN/A
422315SN/AProfileNode::ProfileNode()
432683Sktlim@umich.edu    : count(0)
442315SN/A{ }
452315SN/A
462315SN/Avoid
472315SN/AProfileNode::dump(const string &symbol, uint64_t id, const SymbolTable *symtab,
482315SN/A                  ostream &os) const
492315SN/A{
502315SN/A    ccprintf(os, "%#x %s %d ", id, symbol, count);
512315SN/A    ChildList::const_iterator i, end = children.end();
522315SN/A    for (i = children.begin(); i != end; ++i) {
532315SN/A        const ProfileNode *node = i->second;
542315SN/A        ccprintf(os, "%#x ", (intptr_t)node);
552315SN/A    }
562315SN/A
572315SN/A    ccprintf(os, "\n");
582315SN/A
592315SN/A    for (i = children.begin(); i != end; ++i) {
602315SN/A        Addr addr = i->first;
612315SN/A        string symbol;
622315SN/A        if (addr == 1)
632315SN/A            symbol = "user";
642315SN/A        else if (addr == 2)
652680Sktlim@umich.edu            symbol = "console";
662315SN/A        else if (addr == 3)
672315SN/A            symbol = "unknown";
682669Sktlim@umich.edu        else if (!symtab->findSymbol(addr, symbol))
692332SN/A            panic("could not find symbol for address %#x\n", addr);
702315SN/A
712350SN/A        const ProfileNode *node = i->second;
722350SN/A        node->dump(symbol, (intptr_t)node, symtab, os);
732350SN/A    }
742350SN/A}
752350SN/A
762350SN/Avoid
772350SN/AProfileNode::clear()
782350SN/A{
792350SN/A    count = 0;
802350SN/A    ChildList::iterator i, end = children.end();
812680Sktlim@umich.edu    for (i = children.begin(); i != end; ++i)
822683Sktlim@umich.edu        i->second->clear();
832680Sktlim@umich.edu}
842350SN/A
852680Sktlim@umich.eduFunctionProfile::FunctionProfile(const SymbolTable *_symtab)
862350SN/A    : reset(0), symtab(_symtab)
872315SN/A{
882315SN/A    reset = new MakeCallback<FunctionProfile, &FunctionProfile::clear>(this);
892315SN/A    Stats::registerResetCallback(reset);
902315SN/A}
912669Sktlim@umich.edu
922669Sktlim@umich.eduFunctionProfile::~FunctionProfile()
932315SN/A{
942315SN/A    if (reset)
952315SN/A        delete reset;
962315SN/A}
972315SN/A
982315SN/AProfileNode *
992315SN/AFunctionProfile::consume(const vector<Addr> &stack)
1002315SN/A{
1012315SN/A    ProfileNode *current = &top;
1022315SN/A    for (int i = 0, size = stack.size(); i < size; ++i) {
1032315SN/A        ProfileNode *&ptr = current->children[stack[size - i - 1]];
1042315SN/A        if (ptr == NULL)
1052315SN/A            ptr = new ProfileNode;
1062315SN/A
1072315SN/A        current = ptr;
1082315SN/A    }
1092315SN/A
1102315SN/A    return current;
1112315SN/A}
1122679Sktlim@umich.edu
1132679Sktlim@umich.eduvoid
1142669Sktlim@umich.eduFunctionProfile::clear()
1152315SN/A{
1162669Sktlim@umich.edu    top.clear();
1172315SN/A    pc_count.clear();
1182315SN/A}
1192315SN/A
1202315SN/Avoid
1212679Sktlim@umich.eduFunctionProfile::dump(ExecContext *xc, ostream &os) const
1222679Sktlim@umich.edu{
1232679Sktlim@umich.edu    ccprintf(os, ">>>PC data\n");
1242679Sktlim@umich.edu    map<Addr, Counter>::const_iterator i, end = pc_count.end();
1252679Sktlim@umich.edu    for (i = pc_count.begin(); i != end; ++i) {
1262679Sktlim@umich.edu        Addr pc = i->first;
1272679Sktlim@umich.edu        Counter count = i->second;
1282679Sktlim@umich.edu
1292679Sktlim@umich.edu        std::string symbol;
1302315SN/A        if (pc == 1)
1312683Sktlim@umich.edu            ccprintf(os, "user %d\n", count);
1322683Sktlim@umich.edu        else if (symtab->findSymbol(pc, symbol) && !symbol.empty())
1332315SN/A            ccprintf(os, "%s %d\n", symbol, count);
1342680Sktlim@umich.edu        else
1352315SN/A            ccprintf(os, "%#x %d\n", pc, count);
1362315SN/A    }
1372315SN/A
1382315SN/A    ccprintf(os, ">>>function data\n");
1392315SN/A    top.dump("top", 0, symtab, os);
1402315SN/A}
1412315SN/A
1422315SN/Avoid
1432315SN/AFunctionProfile::sample(ProfileNode *node, Addr pc)
1442315SN/A{
1452315SN/A    node->count++;
1462315SN/A
1472315SN/A    Addr symaddr;
1482315SN/A    if (symtab->findNearestAddr(pc, symaddr)) {
1492315SN/A        pc_count[symaddr]++;
1502315SN/A    } else {
1512315SN/A        // record PC even if we don't have a symbol to avoid
1522315SN/A        // silently biasing the histogram
1532315SN/A        pc_count[pc]++;
1542679Sktlim@umich.edu    }
1552679Sktlim@umich.edu}
1562315SN/A