Deleted Added
sdiff udiff text old ( 7454:3a3e8e8cce1b ) new ( 9497:2759161b9d7f )
full compact
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;

--- 20 unchanged lines hidden (view full) ---

29#include <cmath>
30#include <iomanip>
31
32#include "base/intmath.hh"
33#include "mem/ruby/common/Histogram.hh"
34
35using namespace std;
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
49Histogram::clear(int binsize, int bins)
50{
51 m_binsize = binsize;
52 clear(bins);
53}
54
55void
56Histogram::clear(int bins)
57{
58 m_bins = bins;
59 m_largest_bin = 0;
60 m_max = 0;
61 m_data.resize(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;
67
68 m_sumSamples = 0;
69 m_sumSquaredSamples = 0;
70}
71
72
73void
74Histogram::add(int64 value)
75{
76 assert(value >= 0);
77 m_max = max(m_max, value);
78 m_count++;
79
80 m_sumSamples += value;
81 m_sumSquaredSamples += (value*value);
82
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 }
94 } else {
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;
106 }
107 assert(index >= 0);
108 m_data[index]++;
109 m_largest_bin = max(m_largest_bin, index);
110}
111
112void
113Histogram::add(const Histogram& hist)
114{
115 assert(hist.getBins() == m_bins);
116 assert(hist.getBinSize() == -1); // assume log histogram
117 assert(m_binsize == -1);
118
119 for (int j = 0; j < hist.getData(0); j++) {
120 add(0);
121 }
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 }
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
133double
134Histogram::getStandardDeviation() const

--- 37 unchanged lines hidden (view full) ---

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 << " ]";
188}
189
190bool
191node_less_then_eq(const Histogram* n1, const Histogram* n2)
192{
193 return (n1->size() > n2->size());
194}