author | ysr |
Thu, 29 Jan 2009 21:25:42 -0800 | |
changeset 1912 | 13cc94b84c47 |
parent 1217 | 5eb97f366a6a |
child 4574 | b2d5b0975515 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
1217 | 2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. |
1 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
// Catch-all file for utility classes |
|
26 |
||
27 |
// A weighted average maintains a running, weighted average |
|
28 |
// of some float value (templates would be handy here if we |
|
29 |
// need different types). |
|
30 |
// |
|
31 |
// The average is adaptive in that we smooth it for the |
|
32 |
// initial samples; we don't use the weight until we have |
|
33 |
// enough samples for it to be meaningful. |
|
34 |
// |
|
35 |
// This serves as our best estimate of a future unknown. |
|
36 |
// |
|
37 |
class AdaptiveWeightedAverage : public CHeapObj { |
|
38 |
private: |
|
39 |
float _average; // The last computed average |
|
40 |
unsigned _sample_count; // How often we've sampled this average |
|
41 |
unsigned _weight; // The weight used to smooth the averages |
|
42 |
// A higher weight favors the most |
|
43 |
// recent data. |
|
44 |
||
45 |
protected: |
|
46 |
float _last_sample; // The last value sampled. |
|
47 |
||
48 |
void increment_count() { _sample_count++; } |
|
49 |
void set_average(float avg) { _average = avg; } |
|
50 |
||
51 |
// Helper function, computes an adaptive weighted average |
|
52 |
// given a sample and the last average |
|
53 |
float compute_adaptive_average(float new_sample, float average); |
|
54 |
||
55 |
public: |
|
56 |
// Input weight must be between 0 and 100 |
|
57 |
AdaptiveWeightedAverage(unsigned weight) : |
|
58 |
_average(0.0), _sample_count(0), _weight(weight), _last_sample(0.0) { |
|
59 |
} |
|
60 |
||
976
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
61 |
void clear() { |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
62 |
_average = 0; |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
63 |
_sample_count = 0; |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
64 |
_last_sample = 0; |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
65 |
} |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
66 |
|
1 | 67 |
// Accessors |
68 |
float average() const { return _average; } |
|
69 |
unsigned weight() const { return _weight; } |
|
70 |
unsigned count() const { return _sample_count; } |
|
71 |
float last_sample() const { return _last_sample; } |
|
72 |
||
73 |
// Update data with a new sample. |
|
74 |
void sample(float new_sample); |
|
75 |
||
76 |
static inline float exp_avg(float avg, float sample, |
|
77 |
unsigned int weight) { |
|
78 |
assert(0 <= weight && weight <= 100, "weight must be a percent"); |
|
79 |
return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F; |
|
80 |
} |
|
81 |
static inline size_t exp_avg(size_t avg, size_t sample, |
|
82 |
unsigned int weight) { |
|
83 |
// Convert to float and back to avoid integer overflow. |
|
84 |
return (size_t)exp_avg((float)avg, (float)sample, weight); |
|
85 |
} |
|
86 |
}; |
|
87 |
||
88 |
||
89 |
// A weighted average that includes a deviation from the average, |
|
90 |
// some multiple of which is added to the average. |
|
91 |
// |
|
92 |
// This serves as our best estimate of an upper bound on a future |
|
93 |
// unknown. |
|
94 |
class AdaptivePaddedAverage : public AdaptiveWeightedAverage { |
|
95 |
private: |
|
96 |
float _padded_avg; // The last computed padded average |
|
97 |
float _deviation; // Running deviation from the average |
|
98 |
unsigned _padding; // A multiple which, added to the average, |
|
99 |
// gives us an upper bound guess. |
|
100 |
||
101 |
protected: |
|
102 |
void set_padded_average(float avg) { _padded_avg = avg; } |
|
103 |
void set_deviation(float dev) { _deviation = dev; } |
|
104 |
||
105 |
public: |
|
106 |
AdaptivePaddedAverage() : |
|
107 |
AdaptiveWeightedAverage(0), |
|
108 |
_padded_avg(0.0), _deviation(0.0), _padding(0) {} |
|
109 |
||
110 |
AdaptivePaddedAverage(unsigned weight, unsigned padding) : |
|
111 |
AdaptiveWeightedAverage(weight), |
|
112 |
_padded_avg(0.0), _deviation(0.0), _padding(padding) {} |
|
113 |
||
114 |
// Placement support |
|
115 |
void* operator new(size_t ignored, void* p) { return p; } |
|
116 |
// Allocator |
|
117 |
void* operator new(size_t size) { return CHeapObj::operator new(size); } |
|
118 |
||
119 |
// Accessor |
|
120 |
float padded_average() const { return _padded_avg; } |
|
121 |
float deviation() const { return _deviation; } |
|
122 |
unsigned padding() const { return _padding; } |
|
123 |
||
976
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
124 |
void clear() { |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
125 |
AdaptiveWeightedAverage::clear(); |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
126 |
_padded_avg = 0; |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
127 |
_deviation = 0; |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
128 |
} |
241230d48896
6723228: NUMA allocator: assert(lgrp_id != -1, "No lgrp_id set")
iveresov
parents:
1
diff
changeset
|
129 |
|
1 | 130 |
// Override |
131 |
void sample(float new_sample); |
|
132 |
}; |
|
133 |
||
134 |
// A weighted average that includes a deviation from the average, |
|
135 |
// some multiple of which is added to the average. |
|
136 |
// |
|
137 |
// This serves as our best estimate of an upper bound on a future |
|
138 |
// unknown. |
|
139 |
// A special sort of padded average: it doesn't update deviations |
|
140 |
// if the sample is zero. The average is allowed to change. We're |
|
141 |
// preventing the zero samples from drastically changing our padded |
|
142 |
// average. |
|
143 |
class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage { |
|
144 |
public: |
|
145 |
AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) : |
|
146 |
AdaptivePaddedAverage(weight, padding) {} |
|
147 |
// Override |
|
148 |
void sample(float new_sample); |
|
149 |
}; |
|
150 |
// Use a least squares fit to a set of data to generate a linear |
|
151 |
// equation. |
|
152 |
// y = intercept + slope * x |
|
153 |
||
154 |
class LinearLeastSquareFit : public CHeapObj { |
|
155 |
double _sum_x; // sum of all independent data points x |
|
156 |
double _sum_x_squared; // sum of all independent data points x**2 |
|
157 |
double _sum_y; // sum of all dependent data points y |
|
158 |
double _sum_xy; // sum of all x * y. |
|
159 |
double _intercept; // constant term |
|
160 |
double _slope; // slope |
|
161 |
// The weighted averages are not currently used but perhaps should |
|
162 |
// be used to get decaying averages. |
|
163 |
AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable |
|
164 |
AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable |
|
165 |
||
166 |
public: |
|
167 |
LinearLeastSquareFit(unsigned weight); |
|
168 |
void update(double x, double y); |
|
169 |
double y(double x); |
|
170 |
double slope() { return _slope; } |
|
171 |
// Methods to decide if a change in the dependent variable will |
|
172 |
// achive a desired goal. Note that these methods are not |
|
173 |
// complementary and both are needed. |
|
174 |
bool decrement_will_decrease(); |
|
175 |
bool increment_will_decrease(); |
|
176 |
}; |
|
177 |
||
178 |
class GCPauseTimer : StackObj { |
|
179 |
elapsedTimer* _timer; |
|
180 |
public: |
|
181 |
GCPauseTimer(elapsedTimer* timer) { |
|
182 |
_timer = timer; |
|
183 |
_timer->stop(); |
|
184 |
} |
|
185 |
~GCPauseTimer() { |
|
186 |
_timer->start(); |
|
187 |
} |
|
188 |
}; |