1
| |
2/* 3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer; 10 * redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution; 13 * neither the name of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29
| 1/* 2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28
|
30/* 31 * $Id$ 32 * 33 */ 34
| |
35#include <cmath> 36#include <iomanip> 37
| 29#include <cmath> 30#include <iomanip> 31
|
| 32#include "base/intmath.hh"
|
38#include "mem/ruby/common/Histogram.hh" 39 40using namespace std; 41 42Histogram::Histogram(int binsize, int bins) 43{
| 33#include "mem/ruby/common/Histogram.hh" 34 35using namespace std; 36 37Histogram::Histogram(int binsize, int bins) 38{
|
44 m_binsize = binsize; 45 m_bins = bins; 46 clear();
| 39 m_binsize = binsize; 40 m_bins = bins; 41 clear();
|
47} 48 49Histogram::~Histogram() 50{ 51} 52
| 42} 43 44Histogram::~Histogram() 45{ 46} 47
|
53void Histogram::clear(int binsize, int bins)
| 48void 49Histogram::clear(int binsize, int bins)
|
54{
| 50{
|
55 m_binsize = binsize; 56 clear(bins);
| 51 m_binsize = binsize; 52 clear(bins);
|
57} 58
| 53} 54
|
59void Histogram::clear(int bins)
| 55void 56Histogram::clear(int bins)
|
60{
| 57{
|
61 m_bins = bins; 62 m_largest_bin = 0; 63 m_max = 0; 64 m_data.setSize(m_bins); 65 for (int i = 0; i < m_bins; i++) { 66 m_data[i] = 0; 67 } 68 m_count = 0; 69 m_max = 0;
| 58 m_bins = bins; 59 m_largest_bin = 0; 60 m_max = 0; 61 m_data.setSize(m_bins); 62 for (int i = 0; i < m_bins; i++) { 63 m_data[i] = 0; 64 } 65 m_count = 0; 66 m_max = 0;
|
70
| 67
|
71 m_sumSamples = 0; 72 m_sumSquaredSamples = 0;
| 68 m_sumSamples = 0; 69 m_sumSquaredSamples = 0;
|
73} 74 75
| 70} 71 72
|
76void Histogram::add(int64 value)
| 73void 74Histogram::add(int64 value)
|
77{
| 75{
|
78 assert(value >= 0); 79 m_max = max(m_max, value); 80 m_count++;
| 76 assert(value >= 0); 77 m_max = max(m_max, value); 78 m_count++;
|
81
| 79
|
82 m_sumSamples += value; 83 m_sumSquaredSamples += (value*value);
| 80 m_sumSamples += value; 81 m_sumSquaredSamples += (value*value);
|
84
| 82
|
85 int index; 86 if (m_binsize == -1) { 87 // This is a log base 2 histogram 88 if (value == 0) { 89 index = 0;
| 83 int index; 84 if (m_binsize == -1) { 85 // This is a log base 2 histogram 86 if (value == 0) { 87 index = 0; 88 } else { 89 index = floorLog2(value) + 1; 90 if (index >= m_data.size()) { 91 index = m_data.size() - 1; 92 } 93 }
|
90 } else {
| 94 } else {
|
91 index = int(log(double(value))/log(2.0))+1; 92 if (index >= m_data.size()) { 93 index = m_data.size()-1; 94 }
| 95 // This is a linear histogram 96 while (m_max >= (m_bins * m_binsize)) { 97 for (int i = 0; i < m_bins/2; i++) { 98 m_data[i] = m_data[i*2] + m_data[i*2 + 1]; 99 } 100 for (int i = m_bins/2; i < m_bins; i++) { 101 m_data[i] = 0; 102 } 103 m_binsize *= 2; 104 } 105 index = value/m_binsize;
|
95 }
| 106 }
|
96 } else { 97 // This is a linear histogram 98 while (m_max >= (m_bins * m_binsize)) { 99 for (int i = 0; i < m_bins/2; i++) { 100 m_data[i] = m_data[i*2] + m_data[i*2 + 1]; 101 } 102 for (int i = m_bins/2; i < m_bins; i++) { 103 m_data[i] = 0; 104 } 105 m_binsize *= 2; 106 } 107 index = value/m_binsize; 108 } 109 assert(index >= 0); 110 m_data[index]++; 111 m_largest_bin = max(m_largest_bin, index);
| 107 assert(index >= 0); 108 m_data[index]++; 109 m_largest_bin = max(m_largest_bin, index);
|
112} 113
| 110} 111
|
114void Histogram::add(const Histogram& hist)
| 112void 113Histogram::add(const Histogram& hist)
|
115{
| 114{
|
116 assert(hist.getBins() == m_bins); 117 assert(hist.getBinSize() == -1); // assume log histogram 118 assert(m_binsize == -1);
| 115 assert(hist.getBins() == m_bins); 116 assert(hist.getBinSize() == -1); // assume log histogram 117 assert(m_binsize == -1);
|
119
| 118
|
120 for (int j = 0; j < hist.getData(0); j++) { 121 add(0); 122 } 123 124 for (int i = 1; i < m_bins; i++) { 125 for (int j = 0; j < hist.getData(i); j++) { 126 add(1<<(i-1)); // account for the + 1 index
| 119 for (int j = 0; j < hist.getData(0); j++) { 120 add(0);
|
127 }
| 121 }
|
128 }
| |
129
| 122
|
| 123 for (int i = 1; i < m_bins; i++) { 124 for (int j = 0; j < hist.getData(i); j++) { 125 add(1<<(i-1)); // account for the + 1 index 126 } 127 }
|
130} 131 132// Computation of standard deviation of samples a1, a2, ... aN 133// variance = [SUM {ai^2} - (SUM {ai})^2/N]/(N-1) 134// std deviation equals square root of variance
| 128} 129 130// Computation of standard deviation of samples a1, a2, ... aN 131// variance = [SUM {ai^2} - (SUM {ai})^2/N]/(N-1) 132// std deviation equals square root of variance
|
135double Histogram::getStandardDeviation() const
| 133double 134Histogram::getStandardDeviation() const
|
136{
| 135{
|
137 double variance; 138 if(m_count > 1){ 139 variance = (double)(m_sumSquaredSamples - m_sumSamples*m_sumSamples/m_count)/(m_count - 1); 140 } else { 141 return 0; 142 } 143 return sqrt(variance);
| 136 if (m_count <= 1) 137 return 0.0; 138 139 double variance = 140 (double)(m_sumSquaredSamples - m_sumSamples * m_sumSamples / m_count) 141 / (m_count - 1); 142 return sqrt(variance);
|
144} 145
| 143} 144
|
146void Histogram::print(ostream& out) const
| 145void 146Histogram::print(ostream& out) const
|
147{
| 147{
|
148 printWithMultiplier(out, 1.0);
| 148 printWithMultiplier(out, 1.0);
|
149} 150
| 149} 150
|
151void Histogram::printPercent(ostream& out) const
| 151void 152Histogram::printPercent(ostream& out) const
|
152{
| 153{
|
153 if (m_count == 0) { 154 printWithMultiplier(out, 0.0); 155 } else { 156 printWithMultiplier(out, 100.0/double(m_count)); 157 }
| 154 if (m_count == 0) { 155 printWithMultiplier(out, 0.0); 156 } else { 157 printWithMultiplier(out, 100.0 / double(m_count)); 158 }
|
158} 159
| 159} 160
|
160void Histogram::printWithMultiplier(ostream& out, double multiplier) const
| 161void 162Histogram::printWithMultiplier(ostream& out, double multiplier) const
|
161{
| 163{
|
162 if (m_binsize == -1) { 163 out << "[binsize: log2 "; 164 } else { 165 out << "[binsize: " << m_binsize << " "; 166 } 167 out << "max: " << m_max << " "; 168 out << "count: " << m_count << " "; 169 // out << "total: " << m_sumSamples << " "; 170 if (m_count == 0) { 171 out << "average: NaN |"; 172 out << "standard deviation: NaN |"; 173 } else { 174 out << "average: " << setw(5) << ((double) m_sumSamples)/m_count << " | "; 175 out << "standard deviation: " << getStandardDeviation() << " |"; 176 } 177 for (int i = 0; i < m_bins && i <= m_largest_bin; i++) { 178 if (multiplier == 1.0) { 179 out << " " << m_data[i];
| 164 if (m_binsize == -1) { 165 out << "[binsize: log2 ";
|
180 } else {
| 166 } else {
|
181 out << " " << double(m_data[i]) * multiplier;
| 167 out << "[binsize: " << m_binsize << " ";
|
182 }
| 168 }
|
183 } 184 out << " ]";
| 169 out << "max: " << m_max << " "; 170 out << "count: " << m_count << " "; 171 // out << "total: " << m_sumSamples << " "; 172 if (m_count == 0) { 173 out << "average: NaN |"; 174 out << "standard deviation: NaN |"; 175 } else { 176 out << "average: " << setw(5) << ((double) m_sumSamples)/m_count 177 << " | "; 178 out << "standard deviation: " << getStandardDeviation() << " |"; 179 } 180 for (int i = 0; i < m_bins && i <= m_largest_bin; i++) { 181 if (multiplier == 1.0) { 182 out << " " << m_data[i]; 183 } else { 184 out << " " << double(m_data[i]) * multiplier; 185 } 186 } 187 out << " ]";
|
185} 186
| 188} 189
|
187bool node_less_then_eq(const Histogram* n1, const Histogram* n2)
| 190bool 191node_less_then_eq(const Histogram* n1, const Histogram* n2)
|
188{
| 192{
|
189 return (n1->size() > n2->size());
| 193 return (n1->size() > n2->size());
|
190}
| 194}
|