profile.cc revision 2632:1bb2f91485ea
11689SN/A/*
28948Sandreas.hansson@arm.com * Copyright (c) 2005 The Regents of The University of Michigan
38707Sandreas.hansson@arm.com * All rights reserved.
48707Sandreas.hansson@arm.com *
58707Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
68707Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
78707Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
88707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
98707Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
108707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
118707Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
128707Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
138707Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
142325SN/A * this software without specific prior written permission.
157897Shestness@cs.utexas.edu *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271689SN/A */
281689SN/A
291689SN/A#include <string>
301689SN/A
311689SN/A#include "base/bitfield.hh"
321689SN/A#include "base/callback.hh"
331689SN/A#include "base/statistics.hh"
341689SN/A#include "base/trace.hh"
351689SN/A#include "base/loader/symtab.hh"
361689SN/A#include "cpu/base.hh"
371689SN/A#include "cpu/exec_context.hh"
381689SN/A#include "cpu/profile.hh"
391689SN/A
402665Ssaidi@eecs.umich.eduusing namespace std;
412665Ssaidi@eecs.umich.edu
422756Sksewell@umich.eduProfileNode::ProfileNode()
437897Shestness@cs.utexas.edu    : count(0)
441689SN/A{ }
451689SN/A
468779Sgblack@eecs.umich.eduvoid
476658Snate@binkert.orgProfileNode::dump(const string &symbol, uint64_t id, const SymbolTable *symtab,
488887Sgeoffrey.blake@arm.com                  ostream &os) const
498887Sgeoffrey.blake@arm.com{
508229Snate@binkert.org    ccprintf(os, "%#x %s %d ", id, symbol, count);
518229Snate@binkert.org    ChildList::const_iterator i, end = children.end();
528229Snate@binkert.org    for (i = children.begin(); i != end; ++i) {
534762Snate@binkert.org        const ProfileNode *node = i->second;
548779Sgblack@eecs.umich.edu        ccprintf(os, "%#x ", (intptr_t)node);
554762Snate@binkert.org    }
564762Snate@binkert.org
578232Snate@binkert.org    ccprintf(os, "\n");
589152Satgutier@umich.edu
598232Snate@binkert.org    for (i = children.begin(); i != end; ++i) {
608232Snate@binkert.org        Addr addr = i->first;
614762Snate@binkert.org        string symbol;
624762Snate@binkert.org        if (addr == 1)
638793Sgblack@eecs.umich.edu            symbol = "user";
648779Sgblack@eecs.umich.edu        else if (addr == 2)
654762Snate@binkert.org            symbol = "console";
668460SAli.Saidi@ARM.com        else if (addr == 3)
674762Snate@binkert.org            symbol = "unknown";
685702Ssaidi@eecs.umich.edu        else if (!symtab->findSymbol(addr, symbol))
695702Ssaidi@eecs.umich.edu            panic("could not find symbol for address %#x\n", addr);
708232Snate@binkert.org
715702Ssaidi@eecs.umich.edu        const ProfileNode *node = i->second;
725702Ssaidi@eecs.umich.edu        node->dump(symbol, (intptr_t)node, symtab, os);
738737Skoansin.tan@gmail.com    }
745529Snate@binkert.org}
752669Sktlim@umich.edu
766221Snate@binkert.orgvoid
771060SN/AProfileNode::clear()
785529Snate@binkert.org{
795712Shsul@eecs.umich.edu    count = 0;
801060SN/A    ChildList::iterator i, end = children.end();
811060SN/A    for (i = children.begin(); i != end; ++i)
821060SN/A        i->second->clear();
832292SN/A}
842733Sktlim@umich.edu
852292SN/AFunctionProfile::FunctionProfile(const SymbolTable *_symtab)
862292SN/A    : reset(0), symtab(_symtab)
872292SN/A{
882292SN/A    reset = new MakeCallback<FunctionProfile, &FunctionProfile::clear>(this);
898707Sandreas.hansson@arm.com    Stats::registerResetCallback(reset);
908707Sandreas.hansson@arm.com}
918975Sandreas.hansson@arm.com
928707Sandreas.hansson@arm.comFunctionProfile::~FunctionProfile()
938707Sandreas.hansson@arm.com{
948948Sandreas.hansson@arm.com    if (reset)
958948Sandreas.hansson@arm.com        delete reset;
968948Sandreas.hansson@arm.com}
978707Sandreas.hansson@arm.com
988707Sandreas.hansson@arm.comProfileNode *
998707Sandreas.hansson@arm.comFunctionProfile::consume(const vector<Addr> &stack)
1008707Sandreas.hansson@arm.com{
1018707Sandreas.hansson@arm.com    ProfileNode *current = &top;
1028707Sandreas.hansson@arm.com    for (int i = 0, size = stack.size(); i < size; ++i) {
1038707Sandreas.hansson@arm.com        ProfileNode *&ptr = current->children[stack[size - i - 1]];
1048707Sandreas.hansson@arm.com        if (ptr == NULL)
1058707Sandreas.hansson@arm.com            ptr = new ProfileNode;
1068707Sandreas.hansson@arm.com
1078707Sandreas.hansson@arm.com        current = ptr;
1088707Sandreas.hansson@arm.com    }
1098707Sandreas.hansson@arm.com
1108975Sandreas.hansson@arm.com    return current;
1118707Sandreas.hansson@arm.com}
1128975Sandreas.hansson@arm.com
1138707Sandreas.hansson@arm.comvoid
1148707Sandreas.hansson@arm.comFunctionProfile::clear()
1158707Sandreas.hansson@arm.com{
1168975Sandreas.hansson@arm.com    top.clear();
1178975Sandreas.hansson@arm.com    pc_count.clear();
1188948Sandreas.hansson@arm.com}
1198975Sandreas.hansson@arm.com
1208948Sandreas.hansson@arm.comvoid
1218948Sandreas.hansson@arm.comFunctionProfile::dump(ExecContext *xc, ostream &os) const
1228948Sandreas.hansson@arm.com{
1238707Sandreas.hansson@arm.com    ccprintf(os, ">>>PC data\n");
1248707Sandreas.hansson@arm.com    map<Addr, Counter>::const_iterator i, end = pc_count.end();
1258707Sandreas.hansson@arm.com    for (i = pc_count.begin(); i != end; ++i) {
1268707Sandreas.hansson@arm.com        Addr pc = i->first;
1278707Sandreas.hansson@arm.com        Counter count = i->second;
1288707Sandreas.hansson@arm.com
1291060SN/A        std::string symbol;
1301755SN/A        if (pc == 1)
1315606Snate@binkert.org            ccprintf(os, "user %d\n", count);
1321060SN/A        else if (symtab->findSymbol(pc, symbol) && !symbol.empty())
1331060SN/A            ccprintf(os, "%s %d\n", symbol, count);
1341060SN/A        else
1351060SN/A            ccprintf(os, "%#x %d\n", pc, count);
1361060SN/A    }
1371755SN/A
1381060SN/A    ccprintf(os, ">>>function data\n");
1391060SN/A    top.dump("top", 0, symtab, os);
1401060SN/A}
1411060SN/A
1421060SN/Avoid
1431060SN/AFunctionProfile::sample(ProfileNode *node, Addr pc)
1445336Shines@cs.fsu.edu{
1451060SN/A    node->count++;
1464873Sstever@eecs.umich.edu
1471060SN/A    Addr symaddr;
1481060SN/A    if (symtab->findNearestAddr(pc, symaddr)) {
1491060SN/A        pc_count[symaddr]++;
1502829Sksewell@umich.edu    } else {
1515606Snate@binkert.org        // record PC even if we don't have a symbol to avoid
1522829Sksewell@umich.edu        // silently biasing the histogram
1532829Sksewell@umich.edu        pc_count[pc]++;
1542829Sksewell@umich.edu    }
1552829Sksewell@umich.edu}
1562829Sksewell@umich.edu