statistics.cc revision 9857
12SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
318229Snate@binkert.org#include <fstream>
322SN/A#include <iomanip>
332SN/A#include <list>
342SN/A#include <map>
352SN/A#include <string>
362SN/A
37148SN/A#include "base/callback.hh"
3856SN/A#include "base/cprintf.hh"
395889Snate@binkert.org#include "base/debug.hh"
40441SN/A#include "base/hostinfo.hh"
4156SN/A#include "base/misc.hh"
4256SN/A#include "base/statistics.hh"
4356SN/A#include "base/str.hh"
44441SN/A#include "base/time.hh"
45433SN/A#include "base/trace.hh"
462SN/A
472SN/Ausing namespace std;
482SN/A
49729SN/Anamespace Stats {
50388SN/A
518243Sbradley.danofsky@amd.comstd::string Info::separatorString = "::";
525887Snate@binkert.org
535887Snate@binkert.org// We wrap these in a function to make sure they're built in time.
545887Snate@binkert.orglist<Info *> &
555887Snate@binkert.orgstatsList()
56388SN/A{
575887Snate@binkert.org    static list<Info *> the_list;
585887Snate@binkert.org    return the_list;
59388SN/A}
60388SN/A
615887Snate@binkert.orgMapType &
625887Snate@binkert.orgstatsMap()
63441SN/A{
645887Snate@binkert.org    static MapType the_map;
655887Snate@binkert.org    return the_map;
66441SN/A}
67441SN/A
68388SN/Avoid
695886Snate@binkert.orgInfoAccess::setInfo(Info *info)
70388SN/A{
715887Snate@binkert.org    if (statsMap().find(this) != statsMap().end())
725887Snate@binkert.org        panic("shouldn't register stat twice!");
735887Snate@binkert.org
745887Snate@binkert.org    statsList().push_back(info);
755887Snate@binkert.org
765887Snate@binkert.org#ifndef NDEBUG
775887Snate@binkert.org    pair<MapType::iterator, bool> result =
785887Snate@binkert.org#endif
795887Snate@binkert.org        statsMap().insert(make_pair(this, info));
805887Snate@binkert.org    assert(result.second && "this should never fail");
815887Snate@binkert.org    assert(statsMap().find(this) != statsMap().end());
82388SN/A}
83388SN/A
84388SN/Avoid
855889Snate@binkert.orgInfoAccess::setParams(const StorageParams *params)
865889Snate@binkert.org{
875889Snate@binkert.org    info()->storageParams = params;
885889Snate@binkert.org}
895889Snate@binkert.org
905889Snate@binkert.orgvoid
915886Snate@binkert.orgInfoAccess::setInit()
92388SN/A{
936130Snate@binkert.org    info()->flags.set(init);
94388SN/A}
95388SN/A
965886Snate@binkert.orgInfo *
975886Snate@binkert.orgInfoAccess::info()
98388SN/A{
995887Snate@binkert.org    MapType::const_iterator i = statsMap().find(this);
1005887Snate@binkert.org    assert(i != statsMap().end());
1015887Snate@binkert.org    return (*i).second;
102388SN/A}
103388SN/A
1045886Snate@binkert.orgconst Info *
1055886Snate@binkert.orgInfoAccess::info() const
1065886Snate@binkert.org{
1075887Snate@binkert.org    MapType::const_iterator i = statsMap().find(this);
1085887Snate@binkert.org    assert(i != statsMap().end());
1095887Snate@binkert.org    return (*i).second;
1105886Snate@binkert.org}
1115886Snate@binkert.org
1125889Snate@binkert.orgStorageParams::~StorageParams()
1135889Snate@binkert.org{
1145889Snate@binkert.org}
1155889Snate@binkert.org
1166026Snate@binkert.orgNameMapType &
1176026Snate@binkert.orgnameMap()
1186026Snate@binkert.org{
1196026Snate@binkert.org    static NameMapType the_map;
1206026Snate@binkert.org    return the_map;
1216026Snate@binkert.org}
1226026Snate@binkert.org
1235889Snate@binkert.orgint Info::id_count = 0;
1245889Snate@binkert.org
1255889Snate@binkert.orgint debug_break_id = -1;
1265889Snate@binkert.org
1275886Snate@binkert.orgInfo::Info()
1285889Snate@binkert.org    : flags(none), precision(-1), prereq(0), storageParams(NULL)
129582SN/A{
1305889Snate@binkert.org    id = id_count++;
1315889Snate@binkert.org    if (debug_break_id >= 0 and debug_break_id == id)
1328231Snate@binkert.org        Debug::breakpoint();
133582SN/A}
134582SN/A
1355886Snate@binkert.orgInfo::~Info()
136388SN/A{
137388SN/A}
138388SN/A
1398248Snate@binkert.orgbool
1408248Snate@binkert.orgvalidateStatName(const string &name)
1418248Snate@binkert.org{
1428248Snate@binkert.org    if (name.empty())
1438248Snate@binkert.org        return false;
1448248Snate@binkert.org
1458248Snate@binkert.org    vector<string> vec;
1468248Snate@binkert.org    tokenize(vec, name, '.');
1478248Snate@binkert.org    vector<string>::const_iterator item = vec.begin();
1488248Snate@binkert.org    while (item != vec.end()) {
1498248Snate@binkert.org        if (item->empty())
1508248Snate@binkert.org            return false;
1518248Snate@binkert.org
1528248Snate@binkert.org        string::const_iterator c = item->begin();
1538248Snate@binkert.org
1548248Snate@binkert.org        // The first character is different
1558248Snate@binkert.org        if (!isalpha(*c) && *c != '_')
1568248Snate@binkert.org            return false;
1578248Snate@binkert.org
1588248Snate@binkert.org        // The rest of the characters have different rules.
1598248Snate@binkert.org        while (++c != item->end()) {
1608248Snate@binkert.org            if (!isalnum(*c) && *c != '_')
1618248Snate@binkert.org                return false;
1628248Snate@binkert.org        }
1638248Snate@binkert.org
1648248Snate@binkert.org        ++item;
1658248Snate@binkert.org    }
1668248Snate@binkert.org
1678248Snate@binkert.org    return true;
1688248Snate@binkert.org}
1698248Snate@binkert.org
1706026Snate@binkert.orgvoid
1716026Snate@binkert.orgInfo::setName(const string &name)
1726026Snate@binkert.org{
1738248Snate@binkert.org    if (!validateStatName(name))
1748248Snate@binkert.org        panic("invalid stat name '%s'", name);
1758248Snate@binkert.org
1766026Snate@binkert.org    pair<NameMapType::iterator, bool> p =
1776026Snate@binkert.org        nameMap().insert(make_pair(name, this));
1786026Snate@binkert.org
1796026Snate@binkert.org    Info *other = p.first->second;
1806026Snate@binkert.org    bool result = p.second;
1816026Snate@binkert.org
1826026Snate@binkert.org    if (!result) {
1836026Snate@binkert.org        // using other->name instead of just name to avoid a compiler
1846026Snate@binkert.org        // warning.  They should be the same.
1856026Snate@binkert.org        panic("same statistic name used twice! name=%s\n", other->name);
1866026Snate@binkert.org    }
1876026Snate@binkert.org
1886026Snate@binkert.org    this->name = name;
1896026Snate@binkert.org}
1906026Snate@binkert.org
1912SN/Abool
1925886Snate@binkert.orgInfo::less(Info *stat1, Info *stat2)
1932SN/A{
194388SN/A    const string &name1 = stat1->name;
195388SN/A    const string &name2 = stat2->name;
1962SN/A
1972SN/A    vector<string> v1;
1982SN/A    vector<string> v2;
1992SN/A
2002SN/A    tokenize(v1, name1, '.');
2012SN/A    tokenize(v2, name2, '.');
2022SN/A
2035599Snate@binkert.org    size_type last = min(v1.size(), v2.size()) - 1;
2045599Snate@binkert.org    for (off_type i = 0; i < last; ++i)
2052SN/A        if (v1[i] != v2[i])
2062SN/A            return v1[i] < v2[i];
2072SN/A
2082SN/A    // Special compare for last element.
2092SN/A    if (v1[last] == v2[last])
2102SN/A        return v1.size() < v2.size();
2112SN/A    else
2122SN/A        return v1[last] < v2[last];
2132SN/A
2142SN/A    return false;
2152SN/A}
2162SN/A
217388SN/Abool
2185886Snate@binkert.orgInfo::baseCheck() const
2192SN/A{
2206000Snate@binkert.org    if (!(flags & Stats::init)) {
221582SN/A#ifdef DEBUG
222695SN/A        cprintf("this is stat number %d\n", id);
223388SN/A#endif
224388SN/A        panic("Not all stats have been initialized");
225388SN/A        return false;
226388SN/A    }
2272SN/A
2287462Snate@binkert.org    if ((flags & display) && name.empty()) {
229388SN/A        panic("all printable stats must be named");
230388SN/A        return false;
231388SN/A    }
2322SN/A
233388SN/A    return true;
2342SN/A}
2352SN/A
2366001Snate@binkert.orgvoid
2376001Snate@binkert.orgInfo::enable()
2386001Snate@binkert.org{
2396001Snate@binkert.org}
2406001Snate@binkert.org
2416001Snate@binkert.orgvoid
2426128Snate@binkert.orgVectorInfo::enable()
2436001Snate@binkert.org{
2446001Snate@binkert.org    size_type s = size();
2456001Snate@binkert.org    if (subnames.size() < s)
2466001Snate@binkert.org        subnames.resize(s);
2476001Snate@binkert.org    if (subdescs.size() < s)
2486001Snate@binkert.org        subdescs.resize(s);
2496001Snate@binkert.org}
2506001Snate@binkert.org
2516001Snate@binkert.orgvoid
2526128Snate@binkert.orgVectorDistInfo::enable()
2536001Snate@binkert.org{
2546001Snate@binkert.org    size_type s = size();
2556001Snate@binkert.org    if (subnames.size() < s)
2566001Snate@binkert.org        subnames.resize(s);
2576001Snate@binkert.org    if (subdescs.size() < s)
2586001Snate@binkert.org        subdescs.resize(s);
2596001Snate@binkert.org}
2606001Snate@binkert.org
2616001Snate@binkert.orgvoid
2626128Snate@binkert.orgVector2dInfo::enable()
2636001Snate@binkert.org{
2646001Snate@binkert.org    if (subnames.size() < x)
2656001Snate@binkert.org        subnames.resize(x);
2666001Snate@binkert.org    if (subdescs.size() < x)
2676001Snate@binkert.org        subdescs.resize(x);
2686001Snate@binkert.org    if (y_subnames.size() < y)
2696001Snate@binkert.org        y_subnames.resize(y);
2706001Snate@binkert.org}
271695SN/A
2727831Snate@binkert.orgvoid
2737831Snate@binkert.orgHistStor::grow_out()
2747831Snate@binkert.org{
2757831Snate@binkert.org    int size = cvec.size();
2767831Snate@binkert.org    int zero = size / 2; // round down!
2777831Snate@binkert.org    int top_half = zero + (size - zero + 1) / 2; // round up!
2787831Snate@binkert.org    int bottom_half = (size - zero) / 2; // round down!
2797831Snate@binkert.org
2807831Snate@binkert.org    // grow down
2817831Snate@binkert.org    int low_pair = zero - 1;
2827831Snate@binkert.org    for (int i = zero - 1; i >= bottom_half; i--) {
2837831Snate@binkert.org        cvec[i] = cvec[low_pair];
2847831Snate@binkert.org        if (low_pair - 1 >= 0)
2857831Snate@binkert.org            cvec[i] += cvec[low_pair - 1];
2867831Snate@binkert.org        low_pair -= 2;
2877831Snate@binkert.org    }
2887831Snate@binkert.org    assert(low_pair == 0 || low_pair == -1 || low_pair == -2);
2897831Snate@binkert.org
2907831Snate@binkert.org    for (int i = bottom_half - 1; i >= 0; i--)
2917831Snate@binkert.org        cvec[i] = Counter();
2927831Snate@binkert.org
2937831Snate@binkert.org    // grow up
2947831Snate@binkert.org    int high_pair = zero;
2957831Snate@binkert.org    for (int i = zero; i < top_half; i++) {
2967831Snate@binkert.org        cvec[i] = cvec[high_pair];
2977831Snate@binkert.org        if (high_pair + 1 < size)
2987831Snate@binkert.org            cvec[i] += cvec[high_pair + 1];
2997831Snate@binkert.org        high_pair += 2;
3007831Snate@binkert.org    }
3017831Snate@binkert.org    assert(high_pair == size || high_pair == size + 1);
3027831Snate@binkert.org
3037831Snate@binkert.org    for (int i = top_half; i < size; i++)
3047831Snate@binkert.org        cvec[i] = Counter();
3057831Snate@binkert.org
3067831Snate@binkert.org    max_bucket *= 2;
3077831Snate@binkert.org    min_bucket *= 2;
3087831Snate@binkert.org    bucket_size *= 2;
3097831Snate@binkert.org}
3107831Snate@binkert.org
3117831Snate@binkert.orgvoid
3127831Snate@binkert.orgHistStor::grow_convert()
3137831Snate@binkert.org{
3147831Snate@binkert.org    int size = cvec.size();
3157831Snate@binkert.org    int half = (size + 1) / 2; // round up!
3167831Snate@binkert.org    //bool even = (size & 1) == 0;
3177831Snate@binkert.org
3187831Snate@binkert.org    int pair = size - 1;
3197831Snate@binkert.org    for (int i = size - 1; i >= half; --i) {
3207831Snate@binkert.org        cvec[i] = cvec[pair];
3217831Snate@binkert.org        if (pair - 1 >= 0)
3227831Snate@binkert.org            cvec[i] += cvec[pair - 1];
3237831Snate@binkert.org        pair -= 2;
3247831Snate@binkert.org    }
3257831Snate@binkert.org
3267831Snate@binkert.org    for (int i = half - 1; i >= 0; i--)
3277831Snate@binkert.org        cvec[i] = Counter();
3287831Snate@binkert.org
3297831Snate@binkert.org    min_bucket = -max_bucket;// - (even ? bucket_size : 0);
3307831Snate@binkert.org    bucket_size *= 2;
3317831Snate@binkert.org}
3327831Snate@binkert.org
3337831Snate@binkert.orgvoid
3347831Snate@binkert.orgHistStor::grow_up()
3357831Snate@binkert.org{
3367831Snate@binkert.org    int size = cvec.size();
3377831Snate@binkert.org    int half = (size + 1) / 2; // round up!
3387831Snate@binkert.org
3397831Snate@binkert.org    int pair = 0;
3407831Snate@binkert.org    for (int i = 0; i < half; i++) {
3417831Snate@binkert.org        cvec[i] = cvec[pair];
3427831Snate@binkert.org        if (pair + 1 < size)
3437831Snate@binkert.org            cvec[i] += cvec[pair + 1];
3447831Snate@binkert.org        pair += 2;
3457831Snate@binkert.org    }
3467831Snate@binkert.org    assert(pair == size || pair == size + 1);
3477831Snate@binkert.org
3487831Snate@binkert.org    for (int i = half; i < size; i++)
3497831Snate@binkert.org        cvec[i] = Counter();
3507831Snate@binkert.org
3517831Snate@binkert.org    max_bucket *= 2;
3527831Snate@binkert.org    bucket_size *= 2;
3537831Snate@binkert.org}
3547831Snate@binkert.org
355388SN/AFormula::Formula()
356388SN/A{
357388SN/A}
358388SN/A
359388SN/AFormula::Formula(Temp r)
360388SN/A{
361388SN/A    root = r;
3627461Snate@binkert.org    setInit();
363388SN/A    assert(size());
364388SN/A}
365388SN/A
366388SN/Aconst Formula &
367388SN/AFormula::operator=(Temp r)
368388SN/A{
369388SN/A    assert(!root && "Can't change formulas");
370388SN/A    root = r;
3717461Snate@binkert.org    setInit();
372388SN/A    assert(size());
373388SN/A    return *this;
374388SN/A}
375388SN/A
376388SN/Aconst Formula &
377388SN/AFormula::operator+=(Temp r)
378388SN/A{
379388SN/A    if (root)
380695SN/A        root = NodePtr(new BinaryNode<std::plus<Result> >(root, r));
3817461Snate@binkert.org    else {
382388SN/A        root = r;
3837461Snate@binkert.org        setInit();
3847461Snate@binkert.org    }
3857461Snate@binkert.org
386388SN/A    assert(size());
387388SN/A    return *this;
388388SN/A}
389388SN/A
3909857Snilay@cs.wisc.educonst Formula &
3919857Snilay@cs.wisc.eduFormula::operator/=(Temp r)
3929857Snilay@cs.wisc.edu{
3939857Snilay@cs.wisc.edu    assert (root);
3949857Snilay@cs.wisc.edu    root = NodePtr(new BinaryNode<std::divides<Result> >(root, r));
3959857Snilay@cs.wisc.edu
3969857Snilay@cs.wisc.edu    assert(size());
3979857Snilay@cs.wisc.edu    return *this;
3989857Snilay@cs.wisc.edu}
3999857Snilay@cs.wisc.edu
400142SN/Avoid
4016000Snate@binkert.orgFormula::result(VResult &vec) const
4026000Snate@binkert.org{
4036000Snate@binkert.org    if (root)
4046000Snate@binkert.org        vec = root->result();
4056000Snate@binkert.org}
4066000Snate@binkert.org
4076000Snate@binkert.orgResult
4086000Snate@binkert.orgFormula::total() const
4096000Snate@binkert.org{
4106000Snate@binkert.org    return root ? root->total() : 0.0;
4116000Snate@binkert.org}
4126000Snate@binkert.org
4136000Snate@binkert.orgsize_type
4146000Snate@binkert.orgFormula::size() const
4156000Snate@binkert.org{
4166000Snate@binkert.org    if (!root)
4176000Snate@binkert.org        return 0;
4186000Snate@binkert.org    else
4196000Snate@binkert.org        return root->size();
4206000Snate@binkert.org}
4216000Snate@binkert.org
4226000Snate@binkert.orgvoid
4236000Snate@binkert.orgFormula::reset()
4246000Snate@binkert.org{
4256000Snate@binkert.org}
4266000Snate@binkert.org
4276000Snate@binkert.orgbool
4286000Snate@binkert.orgFormula::zero() const
4296000Snate@binkert.org{
4306000Snate@binkert.org    VResult vec;
4316000Snate@binkert.org    result(vec);
4326227Snate@binkert.org    for (VResult::size_type i = 0; i < vec.size(); ++i)
4336000Snate@binkert.org        if (vec[i] != 0.0)
4346000Snate@binkert.org            return false;
4356000Snate@binkert.org    return true;
4366000Snate@binkert.org}
4376000Snate@binkert.org
4386000Snate@binkert.orgstring
4396000Snate@binkert.orgFormula::str() const
4406000Snate@binkert.org{
4416000Snate@binkert.org    return root ? root->str() : "";
4426000Snate@binkert.org}
4436000Snate@binkert.org
4449042SMitchell.Hayenga@ARM.comCallbackQueue dumpQueue;
445695SN/ACallbackQueue resetQueue;
4462SN/A
447456SN/Avoid
448394SN/AregisterResetCallback(Callback *cb)
449148SN/A{
450148SN/A    resetQueue.add(cb);
451148SN/A}
452148SN/A
4538986SAli.Saidi@ARM.combool _enabled = false;
4548986SAli.Saidi@ARM.com
4558986SAli.Saidi@ARM.combool
4568986SAli.Saidi@ARM.comenabled()
4578986SAli.Saidi@ARM.com{
4588986SAli.Saidi@ARM.com    return _enabled;
4598986SAli.Saidi@ARM.com}
4608986SAli.Saidi@ARM.com
4618986SAli.Saidi@ARM.comvoid
4628986SAli.Saidi@ARM.comenable()
4638986SAli.Saidi@ARM.com{
4648986SAli.Saidi@ARM.com    if (_enabled)
4658986SAli.Saidi@ARM.com        fatal("Stats are already enabled");
4668986SAli.Saidi@ARM.com
4678986SAli.Saidi@ARM.com    _enabled = true;
4688986SAli.Saidi@ARM.com}
4698986SAli.Saidi@ARM.com
4709042SMitchell.Hayenga@ARM.comvoid
4719042SMitchell.Hayenga@ARM.comregisterDumpCallback(Callback *cb)
4729042SMitchell.Hayenga@ARM.com{
4739042SMitchell.Hayenga@ARM.com    dumpQueue.add(cb);
4749042SMitchell.Hayenga@ARM.com}
4759042SMitchell.Hayenga@ARM.com
4767811Ssteve.reinhardt@amd.com} // namespace Stats
4778296Snate@binkert.org
4788296Snate@binkert.orgvoid
4798296Snate@binkert.orgdebugDumpStats()
4808296Snate@binkert.org{
4818296Snate@binkert.org    Stats::dump();
4828296Snate@binkert.org}
483