Histogram.cc revision 11061
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org */
286145Snate@binkert.org
297002Snate@binkert.org#include <cmath>
307002Snate@binkert.org#include <iomanip>
317002Snate@binkert.org
327039Snate@binkert.org#include "base/intmath.hh"
336154Snate@binkert.org#include "mem/ruby/common/Histogram.hh"
346145Snate@binkert.org
357002Snate@binkert.orgusing namespace std;
367002Snate@binkert.org
379497Snilay@cs.wisc.eduHistogram::Histogram(int binsize, uint32_t bins)
386145Snate@binkert.org{
397039Snate@binkert.org    m_binsize = binsize;
409497Snilay@cs.wisc.edu    clear(bins);
416145Snate@binkert.org}
426145Snate@binkert.org
436145Snate@binkert.orgHistogram::~Histogram()
446145Snate@binkert.org{
456145Snate@binkert.org}
466145Snate@binkert.org
477039Snate@binkert.orgvoid
489497Snilay@cs.wisc.eduHistogram::clear(int binsize, uint32_t bins)
496145Snate@binkert.org{
507039Snate@binkert.org    m_binsize = binsize;
517039Snate@binkert.org    clear(bins);
526145Snate@binkert.org}
536145Snate@binkert.org
547039Snate@binkert.orgvoid
559497Snilay@cs.wisc.eduHistogram::clear(uint32_t bins)
566145Snate@binkert.org{
577039Snate@binkert.org    m_largest_bin = 0;
587039Snate@binkert.org    m_max = 0;
599497Snilay@cs.wisc.edu    m_data.resize(bins);
609497Snilay@cs.wisc.edu    for (uint32_t i = 0; i < bins; i++) {
617039Snate@binkert.org        m_data[i] = 0;
627039Snate@binkert.org    }
639497Snilay@cs.wisc.edu
647039Snate@binkert.org    m_count = 0;
657039Snate@binkert.org    m_max = 0;
667039Snate@binkert.org    m_sumSamples = 0;
677039Snate@binkert.org    m_sumSquaredSamples = 0;
686145Snate@binkert.org}
696145Snate@binkert.org
709497Snilay@cs.wisc.eduvoid
719497Snilay@cs.wisc.eduHistogram::doubleBinSize()
729497Snilay@cs.wisc.edu{
739497Snilay@cs.wisc.edu    assert(m_binsize != -1);
749497Snilay@cs.wisc.edu    uint32_t t_bins = m_data.size();
759497Snilay@cs.wisc.edu
769497Snilay@cs.wisc.edu    for (uint32_t i = 0; i < t_bins/2; i++) {
779497Snilay@cs.wisc.edu        m_data[i] = m_data[i*2] + m_data[i*2 + 1];
789497Snilay@cs.wisc.edu    }
799497Snilay@cs.wisc.edu    for (uint32_t i = t_bins/2; i < t_bins; i++) {
809497Snilay@cs.wisc.edu        m_data[i] = 0;
819497Snilay@cs.wisc.edu    }
829497Snilay@cs.wisc.edu
839497Snilay@cs.wisc.edu    m_binsize *= 2;
849497Snilay@cs.wisc.edu}
856145Snate@binkert.org
867039Snate@binkert.orgvoid
8711061Snilay@cs.wisc.eduHistogram::add(int64_t value)
886145Snate@binkert.org{
897039Snate@binkert.org    assert(value >= 0);
907039Snate@binkert.org    m_max = max(m_max, value);
917039Snate@binkert.org    m_count++;
926145Snate@binkert.org
937039Snate@binkert.org    m_sumSamples += value;
947039Snate@binkert.org    m_sumSquaredSamples += (value*value);
956145Snate@binkert.org
969497Snilay@cs.wisc.edu    uint32_t index;
979497Snilay@cs.wisc.edu
987039Snate@binkert.org    if (m_binsize == -1) {
997039Snate@binkert.org        // This is a log base 2 histogram
1007039Snate@binkert.org        if (value == 0) {
1017039Snate@binkert.org            index = 0;
1027039Snate@binkert.org        } else {
1037039Snate@binkert.org            index = floorLog2(value) + 1;
1047039Snate@binkert.org            if (index >= m_data.size()) {
1057039Snate@binkert.org                index = m_data.size() - 1;
1067039Snate@binkert.org            }
1077039Snate@binkert.org        }
1086145Snate@binkert.org    } else {
1097039Snate@binkert.org        // This is a linear histogram
1109497Snilay@cs.wisc.edu        uint32_t t_bins = m_data.size();
1119497Snilay@cs.wisc.edu
1129497Snilay@cs.wisc.edu        while (m_max >= (t_bins * m_binsize)) doubleBinSize();
1137039Snate@binkert.org        index = value/m_binsize;
1146145Snate@binkert.org    }
1159497Snilay@cs.wisc.edu
1169497Snilay@cs.wisc.edu    assert(index < m_data.size());
1177039Snate@binkert.org    m_data[index]++;
1187039Snate@binkert.org    m_largest_bin = max(m_largest_bin, index);
1196145Snate@binkert.org}
1206145Snate@binkert.org
1217039Snate@binkert.orgvoid
1229497Snilay@cs.wisc.eduHistogram::add(Histogram& hist)
1236145Snate@binkert.org{
1249497Snilay@cs.wisc.edu    uint32_t t_bins = m_data.size();
1256145Snate@binkert.org
1269497Snilay@cs.wisc.edu    if (hist.getBins() != t_bins) {
1279773Snilay@cs.wisc.edu        if (m_count == 0) {
1289773Snilay@cs.wisc.edu            m_data.resize(hist.getBins());
1299773Snilay@cs.wisc.edu        } else {
1309773Snilay@cs.wisc.edu            fatal("Histograms with different number of bins "
1319773Snilay@cs.wisc.edu                  "cannot be combined!");
1329773Snilay@cs.wisc.edu        }
1337039Snate@binkert.org    }
1346145Snate@binkert.org
1359497Snilay@cs.wisc.edu    m_max = max(m_max, hist.getMax());
1369497Snilay@cs.wisc.edu    m_count += hist.size();
1379497Snilay@cs.wisc.edu    m_sumSamples += hist.getTotal();
1389497Snilay@cs.wisc.edu    m_sumSquaredSamples += hist.getSquaredTotal();
1399497Snilay@cs.wisc.edu
1409497Snilay@cs.wisc.edu    // Both histograms are log base 2.
1419497Snilay@cs.wisc.edu    if (hist.getBinSize() == -1 && m_binsize == -1) {
1429497Snilay@cs.wisc.edu        for (int j = 0; j < hist.getData(0); j++) {
1439497Snilay@cs.wisc.edu            add(0);
1447039Snate@binkert.org        }
1459497Snilay@cs.wisc.edu
1469497Snilay@cs.wisc.edu        for (uint32_t i = 1; i < t_bins; i++) {
1479497Snilay@cs.wisc.edu            for (int j = 0; j < hist.getData(i); j++) {
1489497Snilay@cs.wisc.edu                add(1<<(i-1));  // account for the + 1 index
1499497Snilay@cs.wisc.edu            }
1509497Snilay@cs.wisc.edu        }
1519497Snilay@cs.wisc.edu    } else if (hist.getBinSize() >= 1 && m_binsize >= 1) {
1529497Snilay@cs.wisc.edu        // Both the histogram are linear.
1539497Snilay@cs.wisc.edu        // We are assuming that the two histograms have the same
1549497Snilay@cs.wisc.edu        // minimum value that they can store.
1559497Snilay@cs.wisc.edu
1569497Snilay@cs.wisc.edu        while (m_binsize > hist.getBinSize()) hist.doubleBinSize();
1579497Snilay@cs.wisc.edu        while (hist.getBinSize() > m_binsize) doubleBinSize();
1589497Snilay@cs.wisc.edu
1599497Snilay@cs.wisc.edu        assert(m_binsize == hist.getBinSize());
1609497Snilay@cs.wisc.edu
1619497Snilay@cs.wisc.edu        for (uint32_t i = 0; i < t_bins; i++) {
1629497Snilay@cs.wisc.edu            m_data[i] += hist.getData(i);
1639497Snilay@cs.wisc.edu
1649497Snilay@cs.wisc.edu            if (m_data[i] > 0) m_largest_bin = i;
1659497Snilay@cs.wisc.edu        }
1669497Snilay@cs.wisc.edu    } else {
1679497Snilay@cs.wisc.edu        fatal("Don't know how to combine log and linear histograms!");
1686145Snate@binkert.org    }
1696145Snate@binkert.org}
1706145Snate@binkert.org
1716145Snate@binkert.org// Computation of standard deviation of samples a1, a2, ... aN
1726145Snate@binkert.org// variance = [SUM {ai^2} - (SUM {ai})^2/N]/(N-1)
1736145Snate@binkert.org// std deviation equals square root of variance
1747039Snate@binkert.orgdouble
1757039Snate@binkert.orgHistogram::getStandardDeviation() const
1766145Snate@binkert.org{
1777039Snate@binkert.org    if (m_count <= 1)
1787039Snate@binkert.org        return 0.0;
1797039Snate@binkert.org
1807039Snate@binkert.org    double variance =
1817039Snate@binkert.org        (double)(m_sumSquaredSamples - m_sumSamples * m_sumSamples / m_count)
1827039Snate@binkert.org        / (m_count - 1);
1837039Snate@binkert.org    return sqrt(variance);
1846145Snate@binkert.org}
1856145Snate@binkert.org
1867039Snate@binkert.orgvoid
1877039Snate@binkert.orgHistogram::print(ostream& out) const
1886145Snate@binkert.org{
1897039Snate@binkert.org    printWithMultiplier(out, 1.0);
1906145Snate@binkert.org}
1916145Snate@binkert.org
1927039Snate@binkert.orgvoid
1937039Snate@binkert.orgHistogram::printPercent(ostream& out) const
1946145Snate@binkert.org{
1957039Snate@binkert.org    if (m_count == 0) {
1967039Snate@binkert.org        printWithMultiplier(out, 0.0);
1977039Snate@binkert.org    } else {
1987039Snate@binkert.org        printWithMultiplier(out, 100.0 / double(m_count));
1997039Snate@binkert.org    }
2006145Snate@binkert.org}
2016145Snate@binkert.org
2027039Snate@binkert.orgvoid
2037039Snate@binkert.orgHistogram::printWithMultiplier(ostream& out, double multiplier) const
2046145Snate@binkert.org{
2057039Snate@binkert.org    if (m_binsize == -1) {
2067039Snate@binkert.org        out << "[binsize: log2 ";
2076145Snate@binkert.org    } else {
2087039Snate@binkert.org        out << "[binsize: " << m_binsize << " ";
2096145Snate@binkert.org    }
2107039Snate@binkert.org    out << "max: " << m_max << " ";
2117039Snate@binkert.org    out << "count: " << m_count << " ";
2127039Snate@binkert.org    //  out << "total: " <<  m_sumSamples << " ";
2137039Snate@binkert.org    if (m_count == 0) {
2147039Snate@binkert.org        out << "average: NaN |";
2157039Snate@binkert.org        out << "standard deviation: NaN |";
2167039Snate@binkert.org    } else {
2177039Snate@binkert.org        out << "average: " << setw(5) << ((double) m_sumSamples)/m_count
2187039Snate@binkert.org            << " | ";
2197039Snate@binkert.org        out << "standard deviation: " << getStandardDeviation() << " |";
2207039Snate@binkert.org    }
2219497Snilay@cs.wisc.edu
2229497Snilay@cs.wisc.edu    for (uint32_t i = 0; i <= m_largest_bin; i++) {
2237039Snate@binkert.org        if (multiplier == 1.0) {
2247039Snate@binkert.org            out << " " << m_data[i];
2257039Snate@binkert.org        } else {
2267039Snate@binkert.org            out << " " << double(m_data[i]) * multiplier;
2277039Snate@binkert.org        }
2287039Snate@binkert.org    }
2297039Snate@binkert.org    out << " ]";
2306145Snate@binkert.org}
2316145Snate@binkert.org
2327039Snate@binkert.orgbool
2337039Snate@binkert.orgnode_less_then_eq(const Histogram* n1, const Histogram* n2)
2346145Snate@binkert.org{
2357039Snate@binkert.org    return (n1->size() > n2->size());
2366145Snate@binkert.org}
237