1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/shared/gcUtil.hpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,176 @@ 1.4 +/* 1.5 + * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +// Catch-all file for utility classes 1.29 + 1.30 +// A weighted average maintains a running, weighted average 1.31 +// of some float value (templates would be handy here if we 1.32 +// need different types). 1.33 +// 1.34 +// The average is adaptive in that we smooth it for the 1.35 +// initial samples; we don't use the weight until we have 1.36 +// enough samples for it to be meaningful. 1.37 +// 1.38 +// This serves as our best estimate of a future unknown. 1.39 +// 1.40 +class AdaptiveWeightedAverage : public CHeapObj { 1.41 + private: 1.42 + float _average; // The last computed average 1.43 + unsigned _sample_count; // How often we've sampled this average 1.44 + unsigned _weight; // The weight used to smooth the averages 1.45 + // A higher weight favors the most 1.46 + // recent data. 1.47 + 1.48 + protected: 1.49 + float _last_sample; // The last value sampled. 1.50 + 1.51 + void increment_count() { _sample_count++; } 1.52 + void set_average(float avg) { _average = avg; } 1.53 + 1.54 + // Helper function, computes an adaptive weighted average 1.55 + // given a sample and the last average 1.56 + float compute_adaptive_average(float new_sample, float average); 1.57 + 1.58 + public: 1.59 + // Input weight must be between 0 and 100 1.60 + AdaptiveWeightedAverage(unsigned weight) : 1.61 + _average(0.0), _sample_count(0), _weight(weight), _last_sample(0.0) { 1.62 + } 1.63 + 1.64 + // Accessors 1.65 + float average() const { return _average; } 1.66 + unsigned weight() const { return _weight; } 1.67 + unsigned count() const { return _sample_count; } 1.68 + float last_sample() const { return _last_sample; } 1.69 + 1.70 + // Update data with a new sample. 1.71 + void sample(float new_sample); 1.72 + 1.73 + static inline float exp_avg(float avg, float sample, 1.74 + unsigned int weight) { 1.75 + assert(0 <= weight && weight <= 100, "weight must be a percent"); 1.76 + return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F; 1.77 + } 1.78 + static inline size_t exp_avg(size_t avg, size_t sample, 1.79 + unsigned int weight) { 1.80 + // Convert to float and back to avoid integer overflow. 1.81 + return (size_t)exp_avg((float)avg, (float)sample, weight); 1.82 + } 1.83 +}; 1.84 + 1.85 + 1.86 +// A weighted average that includes a deviation from the average, 1.87 +// some multiple of which is added to the average. 1.88 +// 1.89 +// This serves as our best estimate of an upper bound on a future 1.90 +// unknown. 1.91 +class AdaptivePaddedAverage : public AdaptiveWeightedAverage { 1.92 + private: 1.93 + float _padded_avg; // The last computed padded average 1.94 + float _deviation; // Running deviation from the average 1.95 + unsigned _padding; // A multiple which, added to the average, 1.96 + // gives us an upper bound guess. 1.97 + 1.98 + protected: 1.99 + void set_padded_average(float avg) { _padded_avg = avg; } 1.100 + void set_deviation(float dev) { _deviation = dev; } 1.101 + 1.102 + public: 1.103 + AdaptivePaddedAverage() : 1.104 + AdaptiveWeightedAverage(0), 1.105 + _padded_avg(0.0), _deviation(0.0), _padding(0) {} 1.106 + 1.107 + AdaptivePaddedAverage(unsigned weight, unsigned padding) : 1.108 + AdaptiveWeightedAverage(weight), 1.109 + _padded_avg(0.0), _deviation(0.0), _padding(padding) {} 1.110 + 1.111 + // Placement support 1.112 + void* operator new(size_t ignored, void* p) { return p; } 1.113 + // Allocator 1.114 + void* operator new(size_t size) { return CHeapObj::operator new(size); } 1.115 + 1.116 + // Accessor 1.117 + float padded_average() const { return _padded_avg; } 1.118 + float deviation() const { return _deviation; } 1.119 + unsigned padding() const { return _padding; } 1.120 + 1.121 + // Override 1.122 + void sample(float new_sample); 1.123 +}; 1.124 + 1.125 +// A weighted average that includes a deviation from the average, 1.126 +// some multiple of which is added to the average. 1.127 +// 1.128 +// This serves as our best estimate of an upper bound on a future 1.129 +// unknown. 1.130 +// A special sort of padded average: it doesn't update deviations 1.131 +// if the sample is zero. The average is allowed to change. We're 1.132 +// preventing the zero samples from drastically changing our padded 1.133 +// average. 1.134 +class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage { 1.135 +public: 1.136 + AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) : 1.137 + AdaptivePaddedAverage(weight, padding) {} 1.138 + // Override 1.139 + void sample(float new_sample); 1.140 +}; 1.141 +// Use a least squares fit to a set of data to generate a linear 1.142 +// equation. 1.143 +// y = intercept + slope * x 1.144 + 1.145 +class LinearLeastSquareFit : public CHeapObj { 1.146 + double _sum_x; // sum of all independent data points x 1.147 + double _sum_x_squared; // sum of all independent data points x**2 1.148 + double _sum_y; // sum of all dependent data points y 1.149 + double _sum_xy; // sum of all x * y. 1.150 + double _intercept; // constant term 1.151 + double _slope; // slope 1.152 + // The weighted averages are not currently used but perhaps should 1.153 + // be used to get decaying averages. 1.154 + AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable 1.155 + AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable 1.156 + 1.157 + public: 1.158 + LinearLeastSquareFit(unsigned weight); 1.159 + void update(double x, double y); 1.160 + double y(double x); 1.161 + double slope() { return _slope; } 1.162 + // Methods to decide if a change in the dependent variable will 1.163 + // achive a desired goal. Note that these methods are not 1.164 + // complementary and both are needed. 1.165 + bool decrement_will_decrease(); 1.166 + bool increment_will_decrease(); 1.167 +}; 1.168 + 1.169 +class GCPauseTimer : StackObj { 1.170 + elapsedTimer* _timer; 1.171 + public: 1.172 + GCPauseTimer(elapsedTimer* timer) { 1.173 + _timer = timer; 1.174 + _timer->stop(); 1.175 + } 1.176 + ~GCPauseTimer() { 1.177 + _timer->start(); 1.178 + } 1.179 +};