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
2911793Sbrandon.potter@amd.com#include "mem/ruby/common/Histogram.hh"
3011793Sbrandon.potter@amd.com
317002Snate@binkert.org#include <cmath>
327002Snate@binkert.org#include <iomanip>
337002Snate@binkert.org
347039Snate@binkert.org#include "base/intmath.hh"
356145Snate@binkert.org
367002Snate@binkert.orgusing namespace std;
377002Snate@binkert.org
389497Snilay@cs.wisc.eduHistogram::Histogram(int binsize, uint32_t bins)
396145Snate@binkert.org{
407039Snate@binkert.org    m_binsize = binsize;
419497Snilay@cs.wisc.edu    clear(bins);
426145Snate@binkert.org}
436145Snate@binkert.org
446145Snate@binkert.orgHistogram::~Histogram()
456145Snate@binkert.org{
466145Snate@binkert.org}
476145Snate@binkert.org
487039Snate@binkert.orgvoid
499497Snilay@cs.wisc.eduHistogram::clear(int binsize, uint32_t bins)
506145Snate@binkert.org{
517039Snate@binkert.org    m_binsize = binsize;
527039Snate@binkert.org    clear(bins);
536145Snate@binkert.org}
546145Snate@binkert.org
557039Snate@binkert.orgvoid
569497Snilay@cs.wisc.eduHistogram::clear(uint32_t bins)
576145Snate@binkert.org{
587039Snate@binkert.org    m_largest_bin = 0;
597039Snate@binkert.org    m_max = 0;
609497Snilay@cs.wisc.edu    m_data.resize(bins);
619497Snilay@cs.wisc.edu    for (uint32_t i = 0; i < bins; i++) {
627039Snate@binkert.org        m_data[i] = 0;
637039Snate@binkert.org    }
649497Snilay@cs.wisc.edu
657039Snate@binkert.org    m_count = 0;
667039Snate@binkert.org    m_max = 0;
677039Snate@binkert.org    m_sumSamples = 0;
687039Snate@binkert.org    m_sumSquaredSamples = 0;
696145Snate@binkert.org}
706145Snate@binkert.org
719497Snilay@cs.wisc.eduvoid
729497Snilay@cs.wisc.eduHistogram::doubleBinSize()
739497Snilay@cs.wisc.edu{
749497Snilay@cs.wisc.edu    assert(m_binsize != -1);
759497Snilay@cs.wisc.edu    uint32_t t_bins = m_data.size();
769497Snilay@cs.wisc.edu
779497Snilay@cs.wisc.edu    for (uint32_t i = 0; i < t_bins/2; i++) {
789497Snilay@cs.wisc.edu        m_data[i] = m_data[i*2] + m_data[i*2 + 1];
799497Snilay@cs.wisc.edu    }
809497Snilay@cs.wisc.edu    for (uint32_t i = t_bins/2; i < t_bins; i++) {
819497Snilay@cs.wisc.edu        m_data[i] = 0;
829497Snilay@cs.wisc.edu    }
839497Snilay@cs.wisc.edu
849497Snilay@cs.wisc.edu    m_binsize *= 2;
859497Snilay@cs.wisc.edu}
866145Snate@binkert.org
877039Snate@binkert.orgvoid
8811061Snilay@cs.wisc.eduHistogram::add(int64_t value)
896145Snate@binkert.org{
907039Snate@binkert.org    assert(value >= 0);
917039Snate@binkert.org    m_max = max(m_max, value);
927039Snate@binkert.org    m_count++;
936145Snate@binkert.org
947039Snate@binkert.org    m_sumSamples += value;
957039Snate@binkert.org    m_sumSquaredSamples += (value*value);
966145Snate@binkert.org
979497Snilay@cs.wisc.edu    uint32_t index;
989497Snilay@cs.wisc.edu
997039Snate@binkert.org    if (m_binsize == -1) {
1007039Snate@binkert.org        // This is a log base 2 histogram
1017039Snate@binkert.org        if (value == 0) {
1027039Snate@binkert.org            index = 0;
1037039Snate@binkert.org        } else {
1047039Snate@binkert.org            index = floorLog2(value) + 1;
1057039Snate@binkert.org            if (index >= m_data.size()) {
1067039Snate@binkert.org                index = m_data.size() - 1;
1077039Snate@binkert.org            }
1087039Snate@binkert.org        }
1096145Snate@binkert.org    } else {
1107039Snate@binkert.org        // This is a linear histogram
1119497Snilay@cs.wisc.edu        uint32_t t_bins = m_data.size();
1129497Snilay@cs.wisc.edu
1139497Snilay@cs.wisc.edu        while (m_max >= (t_bins * m_binsize)) doubleBinSize();
1147039Snate@binkert.org        index = value/m_binsize;
1156145Snate@binkert.org    }
1169497Snilay@cs.wisc.edu
1179497Snilay@cs.wisc.edu    assert(index < m_data.size());
1187039Snate@binkert.org    m_data[index]++;
1197039Snate@binkert.org    m_largest_bin = max(m_largest_bin, index);
1206145Snate@binkert.org}
1216145Snate@binkert.org
1227039Snate@binkert.orgvoid
1239497Snilay@cs.wisc.eduHistogram::add(Histogram& hist)
1246145Snate@binkert.org{
1259497Snilay@cs.wisc.edu    uint32_t t_bins = m_data.size();
1266145Snate@binkert.org
1279497Snilay@cs.wisc.edu    if (hist.getBins() != t_bins) {
1289773Snilay@cs.wisc.edu        if (m_count == 0) {
1299773Snilay@cs.wisc.edu            m_data.resize(hist.getBins());
1309773Snilay@cs.wisc.edu        } else {
1319773Snilay@cs.wisc.edu            fatal("Histograms with different number of bins "
1329773Snilay@cs.wisc.edu                  "cannot be combined!");
1339773Snilay@cs.wisc.edu        }
1347039Snate@binkert.org    }
1356145Snate@binkert.org
1369497Snilay@cs.wisc.edu    m_max = max(m_max, hist.getMax());
1379497Snilay@cs.wisc.edu    m_count += hist.size();
1389497Snilay@cs.wisc.edu    m_sumSamples += hist.getTotal();
1399497Snilay@cs.wisc.edu    m_sumSquaredSamples += hist.getSquaredTotal();
1409497Snilay@cs.wisc.edu
1419497Snilay@cs.wisc.edu    // Both histograms are log base 2.
1429497Snilay@cs.wisc.edu    if (hist.getBinSize() == -1 && m_binsize == -1) {
1439497Snilay@cs.wisc.edu        for (int j = 0; j < hist.getData(0); j++) {
1449497Snilay@cs.wisc.edu            add(0);
1457039Snate@binkert.org        }
1469497Snilay@cs.wisc.edu
1479497Snilay@cs.wisc.edu        for (uint32_t i = 1; i < t_bins; i++) {
1489497Snilay@cs.wisc.edu            for (int j = 0; j < hist.getData(i); j++) {
1499497Snilay@cs.wisc.edu                add(1<<(i-1));  // account for the + 1 index
1509497Snilay@cs.wisc.edu            }
1519497Snilay@cs.wisc.edu        }
1529497Snilay@cs.wisc.edu    } else if (hist.getBinSize() >= 1 && m_binsize >= 1) {
1539497Snilay@cs.wisc.edu        // Both the histogram are linear.
1549497Snilay@cs.wisc.edu        // We are assuming that the two histograms have the same
1559497Snilay@cs.wisc.edu        // minimum value that they can store.
1569497Snilay@cs.wisc.edu
1579497Snilay@cs.wisc.edu        while (m_binsize > hist.getBinSize()) hist.doubleBinSize();
1589497Snilay@cs.wisc.edu        while (hist.getBinSize() > m_binsize) doubleBinSize();
1599497Snilay@cs.wisc.edu
1609497Snilay@cs.wisc.edu        assert(m_binsize == hist.getBinSize());
1619497Snilay@cs.wisc.edu
1629497Snilay@cs.wisc.edu        for (uint32_t i = 0; i < t_bins; i++) {
1639497Snilay@cs.wisc.edu            m_data[i] += hist.getData(i);
1649497Snilay@cs.wisc.edu
1659497Snilay@cs.wisc.edu            if (m_data[i] > 0) m_largest_bin = i;
1669497Snilay@cs.wisc.edu        }
1679497Snilay@cs.wisc.edu    } else {
1689497Snilay@cs.wisc.edu        fatal("Don't know how to combine log and linear histograms!");
1696145Snate@binkert.org    }
1706145Snate@binkert.org}
1716145Snate@binkert.org
1726145Snate@binkert.org// Computation of standard deviation of samples a1, a2, ... aN
1736145Snate@binkert.org// variance = [SUM {ai^2} - (SUM {ai})^2/N]/(N-1)
1746145Snate@binkert.org// std deviation equals square root of variance
1757039Snate@binkert.orgdouble
1767039Snate@binkert.orgHistogram::getStandardDeviation() const
1776145Snate@binkert.org{
1787039Snate@binkert.org    if (m_count <= 1)
1797039Snate@binkert.org        return 0.0;
1807039Snate@binkert.org
1817039Snate@binkert.org    double variance =
1827039Snate@binkert.org        (double)(m_sumSquaredSamples - m_sumSamples * m_sumSamples / m_count)
1837039Snate@binkert.org        / (m_count - 1);
1847039Snate@binkert.org    return sqrt(variance);
1856145Snate@binkert.org}
1866145Snate@binkert.org
1877039Snate@binkert.orgvoid
1887039Snate@binkert.orgHistogram::print(ostream& out) const
1896145Snate@binkert.org{
1907039Snate@binkert.org    printWithMultiplier(out, 1.0);
1916145Snate@binkert.org}
1926145Snate@binkert.org
1937039Snate@binkert.orgvoid
1947039Snate@binkert.orgHistogram::printPercent(ostream& out) const
1956145Snate@binkert.org{
1967039Snate@binkert.org    if (m_count == 0) {
1977039Snate@binkert.org        printWithMultiplier(out, 0.0);
1987039Snate@binkert.org    } else {
1997039Snate@binkert.org        printWithMultiplier(out, 100.0 / double(m_count));
2007039Snate@binkert.org    }
2016145Snate@binkert.org}
2026145Snate@binkert.org
2037039Snate@binkert.orgvoid
2047039Snate@binkert.orgHistogram::printWithMultiplier(ostream& out, double multiplier) const
2056145Snate@binkert.org{
2067039Snate@binkert.org    if (m_binsize == -1) {
2077039Snate@binkert.org        out << "[binsize: log2 ";
2086145Snate@binkert.org    } else {
2097039Snate@binkert.org        out << "[binsize: " << m_binsize << " ";
2106145Snate@binkert.org    }
2117039Snate@binkert.org    out << "max: " << m_max << " ";
2127039Snate@binkert.org    out << "count: " << m_count << " ";
2137039Snate@binkert.org    //  out << "total: " <<  m_sumSamples << " ";
2147039Snate@binkert.org    if (m_count == 0) {
2157039Snate@binkert.org        out << "average: NaN |";
2167039Snate@binkert.org        out << "standard deviation: NaN |";
2177039Snate@binkert.org    } else {
2187039Snate@binkert.org        out << "average: " << setw(5) << ((double) m_sumSamples)/m_count
2197039Snate@binkert.org            << " | ";
2207039Snate@binkert.org        out << "standard deviation: " << getStandardDeviation() << " |";
2217039Snate@binkert.org    }
2229497Snilay@cs.wisc.edu
2239497Snilay@cs.wisc.edu    for (uint32_t i = 0; i <= m_largest_bin; i++) {
2247039Snate@binkert.org        if (multiplier == 1.0) {
2257039Snate@binkert.org            out << " " << m_data[i];
2267039Snate@binkert.org        } else {
2277039Snate@binkert.org            out << " " << double(m_data[i]) * multiplier;
2287039Snate@binkert.org        }
2297039Snate@binkert.org    }
2307039Snate@binkert.org    out << " ]";
2316145Snate@binkert.org}
2326145Snate@binkert.org
2337039Snate@binkert.orgbool
2347039Snate@binkert.orgnode_less_then_eq(const Histogram* n1, const Histogram* n2)
2356145Snate@binkert.org{
2367039Snate@binkert.org    return (n1->size() > n2->size());
2376145Snate@binkert.org}
238