Histogram.cc revision 7039
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 376145Snate@binkert.orgHistogram::Histogram(int binsize, int bins) 386145Snate@binkert.org{ 397039Snate@binkert.org m_binsize = binsize; 407039Snate@binkert.org m_bins = bins; 417039Snate@binkert.org clear(); 426145Snate@binkert.org} 436145Snate@binkert.org 446145Snate@binkert.orgHistogram::~Histogram() 456145Snate@binkert.org{ 466145Snate@binkert.org} 476145Snate@binkert.org 487039Snate@binkert.orgvoid 497039Snate@binkert.orgHistogram::clear(int binsize, int bins) 506145Snate@binkert.org{ 517039Snate@binkert.org m_binsize = binsize; 527039Snate@binkert.org clear(bins); 536145Snate@binkert.org} 546145Snate@binkert.org 557039Snate@binkert.orgvoid 567039Snate@binkert.orgHistogram::clear(int bins) 576145Snate@binkert.org{ 587039Snate@binkert.org m_bins = bins; 597039Snate@binkert.org m_largest_bin = 0; 607039Snate@binkert.org m_max = 0; 617039Snate@binkert.org m_data.setSize(m_bins); 627039Snate@binkert.org for (int i = 0; i < m_bins; i++) { 637039Snate@binkert.org m_data[i] = 0; 647039Snate@binkert.org } 657039Snate@binkert.org m_count = 0; 667039Snate@binkert.org m_max = 0; 676145Snate@binkert.org 687039Snate@binkert.org m_sumSamples = 0; 697039Snate@binkert.org m_sumSquaredSamples = 0; 706145Snate@binkert.org} 716145Snate@binkert.org 726145Snate@binkert.org 737039Snate@binkert.orgvoid 747039Snate@binkert.orgHistogram::add(int64 value) 756145Snate@binkert.org{ 767039Snate@binkert.org assert(value >= 0); 777039Snate@binkert.org m_max = max(m_max, value); 787039Snate@binkert.org m_count++; 796145Snate@binkert.org 807039Snate@binkert.org m_sumSamples += value; 817039Snate@binkert.org m_sumSquaredSamples += (value*value); 826145Snate@binkert.org 837039Snate@binkert.org int index; 847039Snate@binkert.org if (m_binsize == -1) { 857039Snate@binkert.org // This is a log base 2 histogram 867039Snate@binkert.org if (value == 0) { 877039Snate@binkert.org index = 0; 887039Snate@binkert.org } else { 897039Snate@binkert.org index = floorLog2(value) + 1; 907039Snate@binkert.org if (index >= m_data.size()) { 917039Snate@binkert.org index = m_data.size() - 1; 927039Snate@binkert.org } 937039Snate@binkert.org } 946145Snate@binkert.org } else { 957039Snate@binkert.org // This is a linear histogram 967039Snate@binkert.org while (m_max >= (m_bins * m_binsize)) { 977039Snate@binkert.org for (int i = 0; i < m_bins/2; i++) { 987039Snate@binkert.org m_data[i] = m_data[i*2] + m_data[i*2 + 1]; 997039Snate@binkert.org } 1007039Snate@binkert.org for (int i = m_bins/2; i < m_bins; i++) { 1017039Snate@binkert.org m_data[i] = 0; 1027039Snate@binkert.org } 1037039Snate@binkert.org m_binsize *= 2; 1047039Snate@binkert.org } 1057039Snate@binkert.org index = value/m_binsize; 1066145Snate@binkert.org } 1077039Snate@binkert.org assert(index >= 0); 1087039Snate@binkert.org m_data[index]++; 1097039Snate@binkert.org m_largest_bin = max(m_largest_bin, index); 1106145Snate@binkert.org} 1116145Snate@binkert.org 1127039Snate@binkert.orgvoid 1137039Snate@binkert.orgHistogram::add(const Histogram& hist) 1146145Snate@binkert.org{ 1157039Snate@binkert.org assert(hist.getBins() == m_bins); 1167039Snate@binkert.org assert(hist.getBinSize() == -1); // assume log histogram 1177039Snate@binkert.org assert(m_binsize == -1); 1186145Snate@binkert.org 1197039Snate@binkert.org for (int j = 0; j < hist.getData(0); j++) { 1207039Snate@binkert.org add(0); 1217039Snate@binkert.org } 1226145Snate@binkert.org 1237039Snate@binkert.org for (int i = 1; i < m_bins; i++) { 1247039Snate@binkert.org for (int j = 0; j < hist.getData(i); j++) { 1257039Snate@binkert.org add(1<<(i-1)); // account for the + 1 index 1267039Snate@binkert.org } 1276145Snate@binkert.org } 1286145Snate@binkert.org} 1296145Snate@binkert.org 1306145Snate@binkert.org// Computation of standard deviation of samples a1, a2, ... aN 1316145Snate@binkert.org// variance = [SUM {ai^2} - (SUM {ai})^2/N]/(N-1) 1326145Snate@binkert.org// std deviation equals square root of variance 1337039Snate@binkert.orgdouble 1347039Snate@binkert.orgHistogram::getStandardDeviation() const 1356145Snate@binkert.org{ 1367039Snate@binkert.org if (m_count <= 1) 1377039Snate@binkert.org return 0.0; 1387039Snate@binkert.org 1397039Snate@binkert.org double variance = 1407039Snate@binkert.org (double)(m_sumSquaredSamples - m_sumSamples * m_sumSamples / m_count) 1417039Snate@binkert.org / (m_count - 1); 1427039Snate@binkert.org return sqrt(variance); 1436145Snate@binkert.org} 1446145Snate@binkert.org 1457039Snate@binkert.orgvoid 1467039Snate@binkert.orgHistogram::print(ostream& out) const 1476145Snate@binkert.org{ 1487039Snate@binkert.org printWithMultiplier(out, 1.0); 1496145Snate@binkert.org} 1506145Snate@binkert.org 1517039Snate@binkert.orgvoid 1527039Snate@binkert.orgHistogram::printPercent(ostream& out) const 1536145Snate@binkert.org{ 1547039Snate@binkert.org if (m_count == 0) { 1557039Snate@binkert.org printWithMultiplier(out, 0.0); 1567039Snate@binkert.org } else { 1577039Snate@binkert.org printWithMultiplier(out, 100.0 / double(m_count)); 1587039Snate@binkert.org } 1596145Snate@binkert.org} 1606145Snate@binkert.org 1617039Snate@binkert.orgvoid 1627039Snate@binkert.orgHistogram::printWithMultiplier(ostream& out, double multiplier) const 1636145Snate@binkert.org{ 1647039Snate@binkert.org if (m_binsize == -1) { 1657039Snate@binkert.org out << "[binsize: log2 "; 1666145Snate@binkert.org } else { 1677039Snate@binkert.org out << "[binsize: " << m_binsize << " "; 1686145Snate@binkert.org } 1697039Snate@binkert.org out << "max: " << m_max << " "; 1707039Snate@binkert.org out << "count: " << m_count << " "; 1717039Snate@binkert.org // out << "total: " << m_sumSamples << " "; 1727039Snate@binkert.org if (m_count == 0) { 1737039Snate@binkert.org out << "average: NaN |"; 1747039Snate@binkert.org out << "standard deviation: NaN |"; 1757039Snate@binkert.org } else { 1767039Snate@binkert.org out << "average: " << setw(5) << ((double) m_sumSamples)/m_count 1777039Snate@binkert.org << " | "; 1787039Snate@binkert.org out << "standard deviation: " << getStandardDeviation() << " |"; 1797039Snate@binkert.org } 1807039Snate@binkert.org for (int i = 0; i < m_bins && i <= m_largest_bin; i++) { 1817039Snate@binkert.org if (multiplier == 1.0) { 1827039Snate@binkert.org out << " " << m_data[i]; 1837039Snate@binkert.org } else { 1847039Snate@binkert.org out << " " << double(m_data[i]) * multiplier; 1857039Snate@binkert.org } 1867039Snate@binkert.org } 1877039Snate@binkert.org out << " ]"; 1886145Snate@binkert.org} 1896145Snate@binkert.org 1907039Snate@binkert.orgbool 1917039Snate@binkert.orgnode_less_then_eq(const Histogram* n1, const Histogram* n2) 1926145Snate@binkert.org{ 1937039Snate@binkert.org return (n1->size() > n2->size()); 1946145Snate@binkert.org} 195