statistics.cc revision 8248
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.orgtypedef map<const void *, Info *> MapType;
535887Snate@binkert.org
545887Snate@binkert.org// We wrap these in a function to make sure they're built in time.
555887Snate@binkert.orglist<Info *> &
565887Snate@binkert.orgstatsList()
57388SN/A{
585887Snate@binkert.org    static list<Info *> the_list;
595887Snate@binkert.org    return the_list;
60388SN/A}
61388SN/A
625887Snate@binkert.orgMapType &
635887Snate@binkert.orgstatsMap()
64441SN/A{
655887Snate@binkert.org    static MapType the_map;
665887Snate@binkert.org    return the_map;
67441SN/A}
68441SN/A
69388SN/Avoid
705886Snate@binkert.orgInfoAccess::setInfo(Info *info)
71388SN/A{
725887Snate@binkert.org    if (statsMap().find(this) != statsMap().end())
735887Snate@binkert.org        panic("shouldn't register stat twice!");
745887Snate@binkert.org
755887Snate@binkert.org    statsList().push_back(info);
765887Snate@binkert.org
775887Snate@binkert.org#ifndef NDEBUG
785887Snate@binkert.org    pair<MapType::iterator, bool> result =
795887Snate@binkert.org#endif
805887Snate@binkert.org        statsMap().insert(make_pair(this, info));
815887Snate@binkert.org    assert(result.second && "this should never fail");
825887Snate@binkert.org    assert(statsMap().find(this) != statsMap().end());
83388SN/A}
84388SN/A
85388SN/Avoid
865889Snate@binkert.orgInfoAccess::setParams(const StorageParams *params)
875889Snate@binkert.org{
885889Snate@binkert.org    info()->storageParams = params;
895889Snate@binkert.org}
905889Snate@binkert.org
915889Snate@binkert.orgvoid
925886Snate@binkert.orgInfoAccess::setInit()
93388SN/A{
946130Snate@binkert.org    info()->flags.set(init);
95388SN/A}
96388SN/A
975886Snate@binkert.orgInfo *
985886Snate@binkert.orgInfoAccess::info()
99388SN/A{
1005887Snate@binkert.org    MapType::const_iterator i = statsMap().find(this);
1015887Snate@binkert.org    assert(i != statsMap().end());
1025887Snate@binkert.org    return (*i).second;
103388SN/A}
104388SN/A
1055886Snate@binkert.orgconst Info *
1065886Snate@binkert.orgInfoAccess::info() const
1075886Snate@binkert.org{
1085887Snate@binkert.org    MapType::const_iterator i = statsMap().find(this);
1095887Snate@binkert.org    assert(i != statsMap().end());
1105887Snate@binkert.org    return (*i).second;
1115886Snate@binkert.org}
1125886Snate@binkert.org
1135889Snate@binkert.orgStorageParams::~StorageParams()
1145889Snate@binkert.org{
1155889Snate@binkert.org}
1165889Snate@binkert.org
1176026Snate@binkert.orgtypedef map<std::string, Info *> NameMapType;
1186026Snate@binkert.orgNameMapType &
1196026Snate@binkert.orgnameMap()
1206026Snate@binkert.org{
1216026Snate@binkert.org    static NameMapType the_map;
1226026Snate@binkert.org    return the_map;
1236026Snate@binkert.org}
1246026Snate@binkert.org
1255889Snate@binkert.orgint Info::id_count = 0;
1265889Snate@binkert.org
1275889Snate@binkert.orgint debug_break_id = -1;
1285889Snate@binkert.org
1295886Snate@binkert.orgInfo::Info()
1305889Snate@binkert.org    : flags(none), precision(-1), prereq(0), storageParams(NULL)
131582SN/A{
1325889Snate@binkert.org    id = id_count++;
1335889Snate@binkert.org    if (debug_break_id >= 0 and debug_break_id == id)
1348231Snate@binkert.org        Debug::breakpoint();
135582SN/A}
136582SN/A
1375886Snate@binkert.orgInfo::~Info()
138388SN/A{
139388SN/A}
140388SN/A
1418248Snate@binkert.orgbool
1428248Snate@binkert.orgvalidateStatName(const string &name)
1438248Snate@binkert.org{
1448248Snate@binkert.org    if (name.empty())
1458248Snate@binkert.org        return false;
1468248Snate@binkert.org
1478248Snate@binkert.org    vector<string> vec;
1488248Snate@binkert.org    tokenize(vec, name, '.');
1498248Snate@binkert.org    vector<string>::const_iterator item = vec.begin();
1508248Snate@binkert.org    while (item != vec.end()) {
1518248Snate@binkert.org        if (item->empty())
1528248Snate@binkert.org            return false;
1538248Snate@binkert.org
1548248Snate@binkert.org        string::const_iterator c = item->begin();
1558248Snate@binkert.org
1568248Snate@binkert.org        // The first character is different
1578248Snate@binkert.org        if (!isalpha(*c) && *c != '_')
1588248Snate@binkert.org            return false;
1598248Snate@binkert.org
1608248Snate@binkert.org        // The rest of the characters have different rules.
1618248Snate@binkert.org        while (++c != item->end()) {
1628248Snate@binkert.org            if (!isalnum(*c) && *c != '_')
1638248Snate@binkert.org                return false;
1648248Snate@binkert.org        }
1658248Snate@binkert.org
1668248Snate@binkert.org        ++item;
1678248Snate@binkert.org    }
1688248Snate@binkert.org
1698248Snate@binkert.org    return true;
1708248Snate@binkert.org}
1718248Snate@binkert.org
1726026Snate@binkert.orgvoid
1736026Snate@binkert.orgInfo::setName(const string &name)
1746026Snate@binkert.org{
1758248Snate@binkert.org    if (!validateStatName(name))
1768248Snate@binkert.org        panic("invalid stat name '%s'", name);
1778248Snate@binkert.org
1786026Snate@binkert.org    pair<NameMapType::iterator, bool> p =
1796026Snate@binkert.org        nameMap().insert(make_pair(name, this));
1806026Snate@binkert.org
1816026Snate@binkert.org    Info *other = p.first->second;
1826026Snate@binkert.org    bool result = p.second;
1836026Snate@binkert.org
1846026Snate@binkert.org    if (!result) {
1856026Snate@binkert.org        // using other->name instead of just name to avoid a compiler
1866026Snate@binkert.org        // warning.  They should be the same.
1876026Snate@binkert.org        panic("same statistic name used twice! name=%s\n", other->name);
1886026Snate@binkert.org    }
1896026Snate@binkert.org
1906026Snate@binkert.org    this->name = name;
1916026Snate@binkert.org}
1926026Snate@binkert.org
1932SN/Abool
1945886Snate@binkert.orgInfo::less(Info *stat1, Info *stat2)
1952SN/A{
196388SN/A    const string &name1 = stat1->name;
197388SN/A    const string &name2 = stat2->name;
1982SN/A
1992SN/A    vector<string> v1;
2002SN/A    vector<string> v2;
2012SN/A
2022SN/A    tokenize(v1, name1, '.');
2032SN/A    tokenize(v2, name2, '.');
2042SN/A
2055599Snate@binkert.org    size_type last = min(v1.size(), v2.size()) - 1;
2065599Snate@binkert.org    for (off_type i = 0; i < last; ++i)
2072SN/A        if (v1[i] != v2[i])
2082SN/A            return v1[i] < v2[i];
2092SN/A
2102SN/A    // Special compare for last element.
2112SN/A    if (v1[last] == v2[last])
2122SN/A        return v1.size() < v2.size();
2132SN/A    else
2142SN/A        return v1[last] < v2[last];
2152SN/A
2162SN/A    return false;
2172SN/A}
2182SN/A
219388SN/Abool
2205886Snate@binkert.orgInfo::baseCheck() const
2212SN/A{
2226000Snate@binkert.org    if (!(flags & Stats::init)) {
223582SN/A#ifdef DEBUG
224695SN/A        cprintf("this is stat number %d\n", id);
225388SN/A#endif
226388SN/A        panic("Not all stats have been initialized");
227388SN/A        return false;
228388SN/A    }
2292SN/A
2307462Snate@binkert.org    if ((flags & display) && name.empty()) {
231388SN/A        panic("all printable stats must be named");
232388SN/A        return false;
233388SN/A    }
2342SN/A
235388SN/A    return true;
2362SN/A}
2372SN/A
2386001Snate@binkert.orgvoid
2396001Snate@binkert.orgInfo::enable()
2406001Snate@binkert.org{
2416001Snate@binkert.org}
2426001Snate@binkert.org
2436001Snate@binkert.orgvoid
2446128Snate@binkert.orgVectorInfo::enable()
2456001Snate@binkert.org{
2466001Snate@binkert.org    size_type s = size();
2476001Snate@binkert.org    if (subnames.size() < s)
2486001Snate@binkert.org        subnames.resize(s);
2496001Snate@binkert.org    if (subdescs.size() < s)
2506001Snate@binkert.org        subdescs.resize(s);
2516001Snate@binkert.org}
2526001Snate@binkert.org
2536001Snate@binkert.orgvoid
2546128Snate@binkert.orgVectorDistInfo::enable()
2556001Snate@binkert.org{
2566001Snate@binkert.org    size_type s = size();
2576001Snate@binkert.org    if (subnames.size() < s)
2586001Snate@binkert.org        subnames.resize(s);
2596001Snate@binkert.org    if (subdescs.size() < s)
2606001Snate@binkert.org        subdescs.resize(s);
2616001Snate@binkert.org}
2626001Snate@binkert.org
2636001Snate@binkert.orgvoid
2646128Snate@binkert.orgVector2dInfo::enable()
2656001Snate@binkert.org{
2666001Snate@binkert.org    if (subnames.size() < x)
2676001Snate@binkert.org        subnames.resize(x);
2686001Snate@binkert.org    if (subdescs.size() < x)
2696001Snate@binkert.org        subdescs.resize(x);
2706001Snate@binkert.org    if (y_subnames.size() < y)
2716001Snate@binkert.org        y_subnames.resize(y);
2726001Snate@binkert.org}
273695SN/A
2747831Snate@binkert.orgvoid
2757831Snate@binkert.orgHistStor::grow_out()
2767831Snate@binkert.org{
2777831Snate@binkert.org    int size = cvec.size();
2787831Snate@binkert.org    int zero = size / 2; // round down!
2797831Snate@binkert.org    int top_half = zero + (size - zero + 1) / 2; // round up!
2807831Snate@binkert.org    int bottom_half = (size - zero) / 2; // round down!
2817831Snate@binkert.org
2827831Snate@binkert.org    // grow down
2837831Snate@binkert.org    int low_pair = zero - 1;
2847831Snate@binkert.org    for (int i = zero - 1; i >= bottom_half; i--) {
2857831Snate@binkert.org        cvec[i] = cvec[low_pair];
2867831Snate@binkert.org        if (low_pair - 1 >= 0)
2877831Snate@binkert.org            cvec[i] += cvec[low_pair - 1];
2887831Snate@binkert.org        low_pair -= 2;
2897831Snate@binkert.org    }
2907831Snate@binkert.org    assert(low_pair == 0 || low_pair == -1 || low_pair == -2);
2917831Snate@binkert.org
2927831Snate@binkert.org    for (int i = bottom_half - 1; i >= 0; i--)
2937831Snate@binkert.org        cvec[i] = Counter();
2947831Snate@binkert.org
2957831Snate@binkert.org    // grow up
2967831Snate@binkert.org    int high_pair = zero;
2977831Snate@binkert.org    for (int i = zero; i < top_half; i++) {
2987831Snate@binkert.org        cvec[i] = cvec[high_pair];
2997831Snate@binkert.org        if (high_pair + 1 < size)
3007831Snate@binkert.org            cvec[i] += cvec[high_pair + 1];
3017831Snate@binkert.org        high_pair += 2;
3027831Snate@binkert.org    }
3037831Snate@binkert.org    assert(high_pair == size || high_pair == size + 1);
3047831Snate@binkert.org
3057831Snate@binkert.org    for (int i = top_half; i < size; i++)
3067831Snate@binkert.org        cvec[i] = Counter();
3077831Snate@binkert.org
3087831Snate@binkert.org    max_bucket *= 2;
3097831Snate@binkert.org    min_bucket *= 2;
3107831Snate@binkert.org    bucket_size *= 2;
3117831Snate@binkert.org}
3127831Snate@binkert.org
3137831Snate@binkert.orgvoid
3147831Snate@binkert.orgHistStor::grow_convert()
3157831Snate@binkert.org{
3167831Snate@binkert.org    int size = cvec.size();
3177831Snate@binkert.org    int half = (size + 1) / 2; // round up!
3187831Snate@binkert.org    //bool even = (size & 1) == 0;
3197831Snate@binkert.org
3207831Snate@binkert.org    int pair = size - 1;
3217831Snate@binkert.org    for (int i = size - 1; i >= half; --i) {
3227831Snate@binkert.org        cvec[i] = cvec[pair];
3237831Snate@binkert.org        if (pair - 1 >= 0)
3247831Snate@binkert.org            cvec[i] += cvec[pair - 1];
3257831Snate@binkert.org        pair -= 2;
3267831Snate@binkert.org    }
3277831Snate@binkert.org
3287831Snate@binkert.org    for (int i = half - 1; i >= 0; i--)
3297831Snate@binkert.org        cvec[i] = Counter();
3307831Snate@binkert.org
3317831Snate@binkert.org    min_bucket = -max_bucket;// - (even ? bucket_size : 0);
3327831Snate@binkert.org    bucket_size *= 2;
3337831Snate@binkert.org}
3347831Snate@binkert.org
3357831Snate@binkert.orgvoid
3367831Snate@binkert.orgHistStor::grow_up()
3377831Snate@binkert.org{
3387831Snate@binkert.org    int size = cvec.size();
3397831Snate@binkert.org    int half = (size + 1) / 2; // round up!
3407831Snate@binkert.org
3417831Snate@binkert.org    int pair = 0;
3427831Snate@binkert.org    for (int i = 0; i < half; i++) {
3437831Snate@binkert.org        cvec[i] = cvec[pair];
3447831Snate@binkert.org        if (pair + 1 < size)
3457831Snate@binkert.org            cvec[i] += cvec[pair + 1];
3467831Snate@binkert.org        pair += 2;
3477831Snate@binkert.org    }
3487831Snate@binkert.org    assert(pair == size || pair == size + 1);
3497831Snate@binkert.org
3507831Snate@binkert.org    for (int i = half; i < size; i++)
3517831Snate@binkert.org        cvec[i] = Counter();
3527831Snate@binkert.org
3537831Snate@binkert.org    max_bucket *= 2;
3547831Snate@binkert.org    bucket_size *= 2;
3557831Snate@binkert.org}
3567831Snate@binkert.org
357388SN/AFormula::Formula()
358388SN/A{
359388SN/A}
360388SN/A
361388SN/AFormula::Formula(Temp r)
362388SN/A{
363388SN/A    root = r;
3647461Snate@binkert.org    setInit();
365388SN/A    assert(size());
366388SN/A}
367388SN/A
368388SN/Aconst Formula &
369388SN/AFormula::operator=(Temp r)
370388SN/A{
371388SN/A    assert(!root && "Can't change formulas");
372388SN/A    root = r;
3737461Snate@binkert.org    setInit();
374388SN/A    assert(size());
375388SN/A    return *this;
376388SN/A}
377388SN/A
378388SN/Aconst Formula &
379388SN/AFormula::operator+=(Temp r)
380388SN/A{
381388SN/A    if (root)
382695SN/A        root = NodePtr(new BinaryNode<std::plus<Result> >(root, r));
3837461Snate@binkert.org    else {
384388SN/A        root = r;
3857461Snate@binkert.org        setInit();
3867461Snate@binkert.org    }
3877461Snate@binkert.org
388388SN/A    assert(size());
389388SN/A    return *this;
390388SN/A}
391388SN/A
392142SN/Avoid
3936000Snate@binkert.orgFormula::result(VResult &vec) const
3946000Snate@binkert.org{
3956000Snate@binkert.org    if (root)
3966000Snate@binkert.org        vec = root->result();
3976000Snate@binkert.org}
3986000Snate@binkert.org
3996000Snate@binkert.orgResult
4006000Snate@binkert.orgFormula::total() const
4016000Snate@binkert.org{
4026000Snate@binkert.org    return root ? root->total() : 0.0;
4036000Snate@binkert.org}
4046000Snate@binkert.org
4056000Snate@binkert.orgsize_type
4066000Snate@binkert.orgFormula::size() const
4076000Snate@binkert.org{
4086000Snate@binkert.org    if (!root)
4096000Snate@binkert.org        return 0;
4106000Snate@binkert.org    else
4116000Snate@binkert.org        return root->size();
4126000Snate@binkert.org}
4136000Snate@binkert.org
4146000Snate@binkert.orgvoid
4156000Snate@binkert.orgFormula::reset()
4166000Snate@binkert.org{
4176000Snate@binkert.org}
4186000Snate@binkert.org
4196000Snate@binkert.orgbool
4206000Snate@binkert.orgFormula::zero() const
4216000Snate@binkert.org{
4226000Snate@binkert.org    VResult vec;
4236000Snate@binkert.org    result(vec);
4246227Snate@binkert.org    for (VResult::size_type i = 0; i < vec.size(); ++i)
4256000Snate@binkert.org        if (vec[i] != 0.0)
4266000Snate@binkert.org            return false;
4276000Snate@binkert.org    return true;
4286000Snate@binkert.org}
4296000Snate@binkert.org
4306000Snate@binkert.orgstring
4316000Snate@binkert.orgFormula::str() const
4326000Snate@binkert.org{
4336000Snate@binkert.org    return root ? root->str() : "";
4346000Snate@binkert.org}
4356000Snate@binkert.org
4366000Snate@binkert.orgvoid
4376001Snate@binkert.orgenable()
4382SN/A{
4395887Snate@binkert.org    typedef list<Info *>::iterator iter_t;
440695SN/A
4415887Snate@binkert.org    iter_t i, end = statsList().end();
4425887Snate@binkert.org    for (i = statsList().begin(); i != end; ++i) {
4435886Snate@binkert.org        Info *info = *i;
4445886Snate@binkert.org        assert(info);
4455886Snate@binkert.org        if (!info->check() || !info->baseCheck())
4465889Snate@binkert.org            panic("stat check failed for '%s' %d\n", info->name, info->id);
447695SN/A    }
448695SN/A
4495599Snate@binkert.org    off_t j = 0;
4505887Snate@binkert.org    for (i = statsList().begin(); i != end; ++i) {
4515886Snate@binkert.org        Info *info = *i;
4527462Snate@binkert.org        if (!(info->flags & display))
4535886Snate@binkert.org            info->name = "__Stat" + to_string(j++);
454695SN/A    }
455695SN/A
4565887Snate@binkert.org    statsList().sort(Info::less);
457695SN/A
4586001Snate@binkert.org    for (i = statsList().begin(); i != end; ++i) {
4596001Snate@binkert.org        Info *info = *i;
4606001Snate@binkert.org        info->enable();
4616001Snate@binkert.org    }
4626001Snate@binkert.org}
463695SN/A
4646001Snate@binkert.orgvoid
4656001Snate@binkert.orgprepare()
4666001Snate@binkert.org{
4676001Snate@binkert.org    list<Info *>::iterator i = statsList().begin();
4686001Snate@binkert.org    list<Info *>::iterator end = statsList().end();
4696001Snate@binkert.org    while (i != end) {
4706001Snate@binkert.org        Info *info = *i;
4716001Snate@binkert.org        info->prepare();
4726001Snate@binkert.org        ++i;
473695SN/A    }
4742SN/A}
4752SN/A
476695SN/ACallbackQueue resetQueue;
4772SN/A
478456SN/Avoid
479695SN/Areset()
480456SN/A{
4815887Snate@binkert.org    list<Info *>::iterator i = statsList().begin();
4825887Snate@binkert.org    list<Info *>::iterator end = statsList().end();
483695SN/A    while (i != end) {
4845886Snate@binkert.org        Info *info = *i;
4855886Snate@binkert.org        info->reset();
486695SN/A        ++i;
487695SN/A    }
488695SN/A
489695SN/A    resetQueue.process();
490456SN/A}
491456SN/A
492456SN/Avoid
493394SN/AregisterResetCallback(Callback *cb)
494148SN/A{
495148SN/A    resetQueue.add(cb);
496148SN/A}
497148SN/A
4987811Ssteve.reinhardt@amd.com} // namespace Stats
499