Histogram.cc revision 9497
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 877039Snate@binkert.orgHistogram::add(int64 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) { 1279497Snilay@cs.wisc.edu fatal("Histograms with different number of bins cannot be combined!"); 1287039Snate@binkert.org } 1296145Snate@binkert.org 1309497Snilay@cs.wisc.edu m_max = max(m_max, hist.getMax()); 1319497Snilay@cs.wisc.edu m_count += hist.size(); 1329497Snilay@cs.wisc.edu m_sumSamples += hist.getTotal(); 1339497Snilay@cs.wisc.edu m_sumSquaredSamples += hist.getSquaredTotal(); 1349497Snilay@cs.wisc.edu 1359497Snilay@cs.wisc.edu // Both histograms are log base 2. 1369497Snilay@cs.wisc.edu if (hist.getBinSize() == -1 && m_binsize == -1) { 1379497Snilay@cs.wisc.edu for (int j = 0; j < hist.getData(0); j++) { 1389497Snilay@cs.wisc.edu add(0); 1397039Snate@binkert.org } 1409497Snilay@cs.wisc.edu 1419497Snilay@cs.wisc.edu for (uint32_t i = 1; i < t_bins; i++) { 1429497Snilay@cs.wisc.edu for (int j = 0; j < hist.getData(i); j++) { 1439497Snilay@cs.wisc.edu add(1<<(i-1)); // account for the + 1 index 1449497Snilay@cs.wisc.edu } 1459497Snilay@cs.wisc.edu } 1469497Snilay@cs.wisc.edu } else if (hist.getBinSize() >= 1 && m_binsize >= 1) { 1479497Snilay@cs.wisc.edu // Both the histogram are linear. 1489497Snilay@cs.wisc.edu // We are assuming that the two histograms have the same 1499497Snilay@cs.wisc.edu // minimum value that they can store. 1509497Snilay@cs.wisc.edu 1519497Snilay@cs.wisc.edu while (m_binsize > hist.getBinSize()) hist.doubleBinSize(); 1529497Snilay@cs.wisc.edu while (hist.getBinSize() > m_binsize) doubleBinSize(); 1539497Snilay@cs.wisc.edu 1549497Snilay@cs.wisc.edu assert(m_binsize == hist.getBinSize()); 1559497Snilay@cs.wisc.edu 1569497Snilay@cs.wisc.edu for (uint32_t i = 0; i < t_bins; i++) { 1579497Snilay@cs.wisc.edu m_data[i] += hist.getData(i); 1589497Snilay@cs.wisc.edu 1599497Snilay@cs.wisc.edu if (m_data[i] > 0) m_largest_bin = i; 1609497Snilay@cs.wisc.edu } 1619497Snilay@cs.wisc.edu } else { 1629497Snilay@cs.wisc.edu fatal("Don't know how to combine log and linear histograms!"); 1636145Snate@binkert.org } 1646145Snate@binkert.org} 1656145Snate@binkert.org 1666145Snate@binkert.org// Computation of standard deviation of samples a1, a2, ... aN 1676145Snate@binkert.org// variance = [SUM {ai^2} - (SUM {ai})^2/N]/(N-1) 1686145Snate@binkert.org// std deviation equals square root of variance 1697039Snate@binkert.orgdouble 1707039Snate@binkert.orgHistogram::getStandardDeviation() const 1716145Snate@binkert.org{ 1727039Snate@binkert.org if (m_count <= 1) 1737039Snate@binkert.org return 0.0; 1747039Snate@binkert.org 1757039Snate@binkert.org double variance = 1767039Snate@binkert.org (double)(m_sumSquaredSamples - m_sumSamples * m_sumSamples / m_count) 1777039Snate@binkert.org / (m_count - 1); 1787039Snate@binkert.org return sqrt(variance); 1796145Snate@binkert.org} 1806145Snate@binkert.org 1817039Snate@binkert.orgvoid 1827039Snate@binkert.orgHistogram::print(ostream& out) const 1836145Snate@binkert.org{ 1847039Snate@binkert.org printWithMultiplier(out, 1.0); 1856145Snate@binkert.org} 1866145Snate@binkert.org 1877039Snate@binkert.orgvoid 1887039Snate@binkert.orgHistogram::printPercent(ostream& out) const 1896145Snate@binkert.org{ 1907039Snate@binkert.org if (m_count == 0) { 1917039Snate@binkert.org printWithMultiplier(out, 0.0); 1927039Snate@binkert.org } else { 1937039Snate@binkert.org printWithMultiplier(out, 100.0 / double(m_count)); 1947039Snate@binkert.org } 1956145Snate@binkert.org} 1966145Snate@binkert.org 1977039Snate@binkert.orgvoid 1987039Snate@binkert.orgHistogram::printWithMultiplier(ostream& out, double multiplier) const 1996145Snate@binkert.org{ 2007039Snate@binkert.org if (m_binsize == -1) { 2017039Snate@binkert.org out << "[binsize: log2 "; 2026145Snate@binkert.org } else { 2037039Snate@binkert.org out << "[binsize: " << m_binsize << " "; 2046145Snate@binkert.org } 2057039Snate@binkert.org out << "max: " << m_max << " "; 2067039Snate@binkert.org out << "count: " << m_count << " "; 2077039Snate@binkert.org // out << "total: " << m_sumSamples << " "; 2087039Snate@binkert.org if (m_count == 0) { 2097039Snate@binkert.org out << "average: NaN |"; 2107039Snate@binkert.org out << "standard deviation: NaN |"; 2117039Snate@binkert.org } else { 2127039Snate@binkert.org out << "average: " << setw(5) << ((double) m_sumSamples)/m_count 2137039Snate@binkert.org << " | "; 2147039Snate@binkert.org out << "standard deviation: " << getStandardDeviation() << " |"; 2157039Snate@binkert.org } 2169497Snilay@cs.wisc.edu 2179497Snilay@cs.wisc.edu for (uint32_t i = 0; i <= m_largest_bin; i++) { 2187039Snate@binkert.org if (multiplier == 1.0) { 2197039Snate@binkert.org out << " " << m_data[i]; 2207039Snate@binkert.org } else { 2217039Snate@binkert.org out << " " << double(m_data[i]) * multiplier; 2227039Snate@binkert.org } 2237039Snate@binkert.org } 2247039Snate@binkert.org out << " ]"; 2256145Snate@binkert.org} 2266145Snate@binkert.org 2277039Snate@binkert.orgbool 2287039Snate@binkert.orgnode_less_then_eq(const Histogram* n1, const Histogram* n2) 2296145Snate@binkert.org{ 2307039Snate@binkert.org return (n1->size() > n2->size()); 2316145Snate@binkert.org} 232