Histogram.cc revision 6145
16145Snate@binkert.org
26145Snate@binkert.org/*
36145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
46145Snate@binkert.org * All rights reserved.
56145Snate@binkert.org *
66145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
76145Snate@binkert.org * modification, are permitted provided that the following conditions are
86145Snate@binkert.org * met: redistributions of source code must retain the above copyright
96145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
106145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
116145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
126145Snate@binkert.org * documentation and/or other materials provided with the distribution;
136145Snate@binkert.org * neither the name of the copyright holders nor the names of its
146145Snate@binkert.org * contributors may be used to endorse or promote products derived from
156145Snate@binkert.org * this software without specific prior written permission.
166145Snate@binkert.org *
176145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286145Snate@binkert.org */
296145Snate@binkert.org
306145Snate@binkert.org/*
316145Snate@binkert.org * $Id$
326145Snate@binkert.org *
336145Snate@binkert.org */
346145Snate@binkert.org
356145Snate@binkert.org#include "Histogram.hh"
366145Snate@binkert.org
376145Snate@binkert.orgHistogram::Histogram(int binsize, int bins)
386145Snate@binkert.org{
396145Snate@binkert.org  m_binsize = binsize;
406145Snate@binkert.org  m_bins = bins;
416145Snate@binkert.org  clear();
426145Snate@binkert.org}
436145Snate@binkert.org
446145Snate@binkert.orgHistogram::~Histogram()
456145Snate@binkert.org{
466145Snate@binkert.org}
476145Snate@binkert.org
486145Snate@binkert.orgvoid Histogram::clear(int binsize, int bins)
496145Snate@binkert.org{
506145Snate@binkert.org  m_binsize = binsize;
516145Snate@binkert.org  clear(bins);
526145Snate@binkert.org}
536145Snate@binkert.org
546145Snate@binkert.orgvoid Histogram::clear(int bins)
556145Snate@binkert.org{
566145Snate@binkert.org  m_bins = bins;
576145Snate@binkert.org  m_largest_bin = 0;
586145Snate@binkert.org  m_max = 0;
596145Snate@binkert.org  m_data.setSize(m_bins);
606145Snate@binkert.org  for (int i = 0; i < m_bins; i++) {
616145Snate@binkert.org    m_data[i] = 0;
626145Snate@binkert.org  }
636145Snate@binkert.org  m_count = 0;
646145Snate@binkert.org  m_max = 0;
656145Snate@binkert.org
666145Snate@binkert.org  m_sumSamples = 0;
676145Snate@binkert.org  m_sumSquaredSamples = 0;
686145Snate@binkert.org}
696145Snate@binkert.org
706145Snate@binkert.org
716145Snate@binkert.orgvoid Histogram::add(int64 value)
726145Snate@binkert.org{
736145Snate@binkert.org  assert(value >= 0);
746145Snate@binkert.org  m_max = max(m_max, value);
756145Snate@binkert.org  m_count++;
766145Snate@binkert.org
776145Snate@binkert.org  m_sumSamples += value;
786145Snate@binkert.org  m_sumSquaredSamples += (value*value);
796145Snate@binkert.org
806145Snate@binkert.org  int index;
816145Snate@binkert.org  if (m_binsize == -1) {
826145Snate@binkert.org    // This is a log base 2 histogram
836145Snate@binkert.org    if (value == 0) {
846145Snate@binkert.org      index = 0;
856145Snate@binkert.org    } else {
866145Snate@binkert.org      index = int(log(double(value))/log(2.0))+1;
876145Snate@binkert.org      if (index >= m_data.size()) {
886145Snate@binkert.org        index = m_data.size()-1;
896145Snate@binkert.org      }
906145Snate@binkert.org    }
916145Snate@binkert.org  } else {
926145Snate@binkert.org    // This is a linear histogram
936145Snate@binkert.org    while (m_max >= (m_bins * m_binsize)) {
946145Snate@binkert.org      for (int i = 0; i < m_bins/2; i++) {
956145Snate@binkert.org        m_data[i] = m_data[i*2] + m_data[i*2 + 1];
966145Snate@binkert.org      }
976145Snate@binkert.org      for (int i = m_bins/2; i < m_bins; i++) {
986145Snate@binkert.org        m_data[i] = 0;
996145Snate@binkert.org      }
1006145Snate@binkert.org      m_binsize *= 2;
1016145Snate@binkert.org    }
1026145Snate@binkert.org    index = value/m_binsize;
1036145Snate@binkert.org  }
1046145Snate@binkert.org  assert(index >= 0);
1056145Snate@binkert.org  m_data[index]++;
1066145Snate@binkert.org  m_largest_bin = max(m_largest_bin, index);
1076145Snate@binkert.org}
1086145Snate@binkert.org
1096145Snate@binkert.orgvoid Histogram::add(const Histogram& hist)
1106145Snate@binkert.org{
1116145Snate@binkert.org  assert(hist.getBins() == m_bins);
1126145Snate@binkert.org  assert(hist.getBinSize() == -1);  // assume log histogram
1136145Snate@binkert.org  assert(m_binsize == -1);
1146145Snate@binkert.org
1156145Snate@binkert.org  for (int j = 0; j < hist.getData(0); j++) {
1166145Snate@binkert.org    add(0);
1176145Snate@binkert.org  }
1186145Snate@binkert.org
1196145Snate@binkert.org  for (int i = 1; i < m_bins; i++) {
1206145Snate@binkert.org    for (int j = 0; j < hist.getData(i); j++) {
1216145Snate@binkert.org      add(1<<(i-1));  // account for the + 1 index
1226145Snate@binkert.org    }
1236145Snate@binkert.org  }
1246145Snate@binkert.org
1256145Snate@binkert.org}
1266145Snate@binkert.org
1276145Snate@binkert.org// Computation of standard deviation of samples a1, a2, ... aN
1286145Snate@binkert.org// variance = [SUM {ai^2} - (SUM {ai})^2/N]/(N-1)
1296145Snate@binkert.org// std deviation equals square root of variance
1306145Snate@binkert.orgdouble Histogram::getStandardDeviation() const
1316145Snate@binkert.org{
1326145Snate@binkert.org  double variance;
1336145Snate@binkert.org  if(m_count > 1){
1346145Snate@binkert.org    variance = (double)(m_sumSquaredSamples - m_sumSamples*m_sumSamples/m_count)/(m_count - 1);
1356145Snate@binkert.org  } else {
1366145Snate@binkert.org    return 0;
1376145Snate@binkert.org  }
1386145Snate@binkert.org  return sqrt(variance);
1396145Snate@binkert.org}
1406145Snate@binkert.org
1416145Snate@binkert.orgvoid Histogram::print(ostream& out) const
1426145Snate@binkert.org{
1436145Snate@binkert.org  printWithMultiplier(out, 1.0);
1446145Snate@binkert.org}
1456145Snate@binkert.org
1466145Snate@binkert.orgvoid Histogram::printPercent(ostream& out) const
1476145Snate@binkert.org{
1486145Snate@binkert.org  if (m_count == 0) {
1496145Snate@binkert.org    printWithMultiplier(out, 0.0);
1506145Snate@binkert.org  } else {
1516145Snate@binkert.org    printWithMultiplier(out, 100.0/double(m_count));
1526145Snate@binkert.org  }
1536145Snate@binkert.org}
1546145Snate@binkert.org
1556145Snate@binkert.orgvoid Histogram::printWithMultiplier(ostream& out, double multiplier) const
1566145Snate@binkert.org{
1576145Snate@binkert.org  if (m_binsize == -1) {
1586145Snate@binkert.org    out << "[binsize: log2 ";
1596145Snate@binkert.org  } else {
1606145Snate@binkert.org    out << "[binsize: " << m_binsize << " ";
1616145Snate@binkert.org  }
1626145Snate@binkert.org  out << "max: " << m_max << " ";
1636145Snate@binkert.org  out << "count: " << m_count << " ";
1646145Snate@binkert.org  //  out << "total: " <<  m_sumSamples << " ";
1656145Snate@binkert.org  if (m_count == 0) {
1666145Snate@binkert.org    out << "average: NaN |";
1676145Snate@binkert.org    out << "standard deviation: NaN |";
1686145Snate@binkert.org  } else {
1696145Snate@binkert.org    out << "average: " << setw(5) << ((double) m_sumSamples)/m_count << " | ";
1706145Snate@binkert.org    out << "standard deviation: " << getStandardDeviation() << " |";
1716145Snate@binkert.org  }
1726145Snate@binkert.org  for (int i = 0; i < m_bins && i <= m_largest_bin; i++) {
1736145Snate@binkert.org    if (multiplier == 1.0) {
1746145Snate@binkert.org      out << " " << m_data[i];
1756145Snate@binkert.org    } else {
1766145Snate@binkert.org      out << " " << double(m_data[i]) * multiplier;
1776145Snate@binkert.org    }
1786145Snate@binkert.org  }
1796145Snate@binkert.org  out << " ]";
1806145Snate@binkert.org}
1816145Snate@binkert.org
1826145Snate@binkert.orgbool node_less_then_eq(const Histogram* n1, const Histogram* n2)
1836145Snate@binkert.org{
1846145Snate@binkert.org  return (n1->size() > n2->size());
1856145Snate@binkert.org}
186