0
|
1 /*
|
|
2 * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
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
|
|
61 // Accessors
|
|
62 float average() const { return _average; }
|
|
63 unsigned weight() const { return _weight; }
|
|
64 unsigned count() const { return _sample_count; }
|
|
65 float last_sample() const { return _last_sample; }
|
|
66
|
|
67 // Update data with a new sample.
|
|
68 void sample(float new_sample);
|
|
69
|
|
70 static inline float exp_avg(float avg, float sample,
|
|
71 unsigned int weight) {
|
|
72 assert(0 <= weight && weight <= 100, "weight must be a percent");
|
|
73 return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F;
|
|
74 }
|
|
75 static inline size_t exp_avg(size_t avg, size_t sample,
|
|
76 unsigned int weight) {
|
|
77 // Convert to float and back to avoid integer overflow.
|
|
78 return (size_t)exp_avg((float)avg, (float)sample, weight);
|
|
79 }
|
|
80 };
|
|
81
|
|
82
|
|
83 // A weighted average that includes a deviation from the average,
|
|
84 // some multiple of which is added to the average.
|
|
85 //
|
|
86 // This serves as our best estimate of an upper bound on a future
|
|
87 // unknown.
|
|
88 class AdaptivePaddedAverage : public AdaptiveWeightedAverage {
|
|
89 private:
|
|
90 float _padded_avg; // The last computed padded average
|
|
91 float _deviation; // Running deviation from the average
|
|
92 unsigned _padding; // A multiple which, added to the average,
|
|
93 // gives us an upper bound guess.
|
|
94
|
|
95 protected:
|
|
96 void set_padded_average(float avg) { _padded_avg = avg; }
|
|
97 void set_deviation(float dev) { _deviation = dev; }
|
|
98
|
|
99 public:
|
|
100 AdaptivePaddedAverage() :
|
|
101 AdaptiveWeightedAverage(0),
|
|
102 _padded_avg(0.0), _deviation(0.0), _padding(0) {}
|
|
103
|
|
104 AdaptivePaddedAverage(unsigned weight, unsigned padding) :
|
|
105 AdaptiveWeightedAverage(weight),
|
|
106 _padded_avg(0.0), _deviation(0.0), _padding(padding) {}
|
|
107
|
|
108 // Placement support
|
|
109 void* operator new(size_t ignored, void* p) { return p; }
|
|
110 // Allocator
|
|
111 void* operator new(size_t size) { return CHeapObj::operator new(size); }
|
|
112
|
|
113 // Accessor
|
|
114 float padded_average() const { return _padded_avg; }
|
|
115 float deviation() const { return _deviation; }
|
|
116 unsigned padding() const { return _padding; }
|
|
117
|
|
118 // Override
|
|
119 void sample(float new_sample);
|
|
120 };
|
|
121
|
|
122 // A weighted average that includes a deviation from the average,
|
|
123 // some multiple of which is added to the average.
|
|
124 //
|
|
125 // This serves as our best estimate of an upper bound on a future
|
|
126 // unknown.
|
|
127 // A special sort of padded average: it doesn't update deviations
|
|
128 // if the sample is zero. The average is allowed to change. We're
|
|
129 // preventing the zero samples from drastically changing our padded
|
|
130 // average.
|
|
131 class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage {
|
|
132 public:
|
|
133 AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) :
|
|
134 AdaptivePaddedAverage(weight, padding) {}
|
|
135 // Override
|
|
136 void sample(float new_sample);
|
|
137 };
|
|
138 // Use a least squares fit to a set of data to generate a linear
|
|
139 // equation.
|
|
140 // y = intercept + slope * x
|
|
141
|
|
142 class LinearLeastSquareFit : public CHeapObj {
|
|
143 double _sum_x; // sum of all independent data points x
|
|
144 double _sum_x_squared; // sum of all independent data points x**2
|
|
145 double _sum_y; // sum of all dependent data points y
|
|
146 double _sum_xy; // sum of all x * y.
|
|
147 double _intercept; // constant term
|
|
148 double _slope; // slope
|
|
149 // The weighted averages are not currently used but perhaps should
|
|
150 // be used to get decaying averages.
|
|
151 AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable
|
|
152 AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable
|
|
153
|
|
154 public:
|
|
155 LinearLeastSquareFit(unsigned weight);
|
|
156 void update(double x, double y);
|
|
157 double y(double x);
|
|
158 double slope() { return _slope; }
|
|
159 // Methods to decide if a change in the dependent variable will
|
|
160 // achive a desired goal. Note that these methods are not
|
|
161 // complementary and both are needed.
|
|
162 bool decrement_will_decrease();
|
|
163 bool increment_will_decrease();
|
|
164 };
|
|
165
|
|
166 class GCPauseTimer : StackObj {
|
|
167 elapsedTimer* _timer;
|
|
168 public:
|
|
169 GCPauseTimer(elapsedTimer* timer) {
|
|
170 _timer = timer;
|
|
171 _timer->stop();
|
|
172 }
|
|
173 ~GCPauseTimer() {
|
|
174 _timer->start();
|
|
175 }
|
|
176 };
|