Histogram.cc revision 6154
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
30/*
31 * $Id$
32 *
33 */
34
35#include "mem/ruby/common/Histogram.hh"
36
37Histogram::Histogram(int binsize, int bins)
38{
39  m_binsize = binsize;
40  m_bins = bins;
41  clear();
42}
43
44Histogram::~Histogram()
45{
46}
47
48void Histogram::clear(int binsize, int bins)
49{
50  m_binsize = binsize;
51  clear(bins);
52}
53
54void Histogram::clear(int bins)
55{
56  m_bins = bins;
57  m_largest_bin = 0;
58  m_max = 0;
59  m_data.setSize(m_bins);
60  for (int i = 0; i < m_bins; i++) {
61    m_data[i] = 0;
62  }
63  m_count = 0;
64  m_max = 0;
65
66  m_sumSamples = 0;
67  m_sumSquaredSamples = 0;
68}
69
70
71void Histogram::add(int64 value)
72{
73  assert(value >= 0);
74  m_max = max(m_max, value);
75  m_count++;
76
77  m_sumSamples += value;
78  m_sumSquaredSamples += (value*value);
79
80  int index;
81  if (m_binsize == -1) {
82    // This is a log base 2 histogram
83    if (value == 0) {
84      index = 0;
85    } else {
86      index = int(log(double(value))/log(2.0))+1;
87      if (index >= m_data.size()) {
88        index = m_data.size()-1;
89      }
90    }
91  } else {
92    // This is a linear histogram
93    while (m_max >= (m_bins * m_binsize)) {
94      for (int i = 0; i < m_bins/2; i++) {
95        m_data[i] = m_data[i*2] + m_data[i*2 + 1];
96      }
97      for (int i = m_bins/2; i < m_bins; i++) {
98        m_data[i] = 0;
99      }
100      m_binsize *= 2;
101    }
102    index = value/m_binsize;
103  }
104  assert(index >= 0);
105  m_data[index]++;
106  m_largest_bin = max(m_largest_bin, index);
107}
108
109void Histogram::add(const Histogram& hist)
110{
111  assert(hist.getBins() == m_bins);
112  assert(hist.getBinSize() == -1);  // assume log histogram
113  assert(m_binsize == -1);
114
115  for (int j = 0; j < hist.getData(0); j++) {
116    add(0);
117  }
118
119  for (int i = 1; i < m_bins; i++) {
120    for (int j = 0; j < hist.getData(i); j++) {
121      add(1<<(i-1));  // account for the + 1 index
122    }
123  }
124
125}
126
127// Computation of standard deviation of samples a1, a2, ... aN
128// variance = [SUM {ai^2} - (SUM {ai})^2/N]/(N-1)
129// std deviation equals square root of variance
130double Histogram::getStandardDeviation() const
131{
132  double variance;
133  if(m_count > 1){
134    variance = (double)(m_sumSquaredSamples - m_sumSamples*m_sumSamples/m_count)/(m_count - 1);
135  } else {
136    return 0;
137  }
138  return sqrt(variance);
139}
140
141void Histogram::print(ostream& out) const
142{
143  printWithMultiplier(out, 1.0);
144}
145
146void Histogram::printPercent(ostream& out) const
147{
148  if (m_count == 0) {
149    printWithMultiplier(out, 0.0);
150  } else {
151    printWithMultiplier(out, 100.0/double(m_count));
152  }
153}
154
155void Histogram::printWithMultiplier(ostream& out, double multiplier) const
156{
157  if (m_binsize == -1) {
158    out << "[binsize: log2 ";
159  } else {
160    out << "[binsize: " << m_binsize << " ";
161  }
162  out << "max: " << m_max << " ";
163  out << "count: " << m_count << " ";
164  //  out << "total: " <<  m_sumSamples << " ";
165  if (m_count == 0) {
166    out << "average: NaN |";
167    out << "standard deviation: NaN |";
168  } else {
169    out << "average: " << setw(5) << ((double) m_sumSamples)/m_count << " | ";
170    out << "standard deviation: " << getStandardDeviation() << " |";
171  }
172  for (int i = 0; i < m_bins && i <= m_largest_bin; i++) {
173    if (multiplier == 1.0) {
174      out << " " << m_data[i];
175    } else {
176      out << " " << double(m_data[i]) * multiplier;
177    }
178  }
179  out << " ]";
180}
181
182bool node_less_then_eq(const Histogram* n1, const Histogram* n2)
183{
184  return (n1->size() > n2->size());
185}
186