src/share/vm/gc_implementation/shared/gcUtil.hpp

Fri, 25 Sep 2009 12:17:06 -0700

author
trims
date
Fri, 25 Sep 2009 12:17:06 -0700
changeset 1417
7a102acc9f17
parent 772
9ee9cf798b59
child 1580
e018e6884bd8
permissions
-rw-r--r--

Merge

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

mercurial