1 /* 2 * Copyright (c) 2002, 2008, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * 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, float avg = 0.0) : 58 _average(avg), _sample_count(0), _weight(weight), _last_sample(0.0) { 59 } 60 61 void clear() { 62 _average = 0; 63 _sample_count = 0; 64 _last_sample = 0; 65 } 66 67 // Useful for modifying static structures after startup. 68 void modify(size_t avg, unsigned wt, bool force = false) { 69 assert(force, "Are you sure you want to call this?"); 70 _average = (float)avg; 71 _weight = wt; 72 } 73 74 // Accessors 75 float average() const { return _average; } 76 unsigned weight() const { return _weight; } 77 unsigned count() const { return _sample_count; } 78 float last_sample() const { return _last_sample; } 79 80 // Update data with a new sample. 81 void sample(float new_sample); 82 83 static inline float exp_avg(float avg, float sample, 84 unsigned int weight) { 85 assert(0 <= weight && weight <= 100, "weight must be a percent"); 86 return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F; 87 } 88 static inline size_t exp_avg(size_t avg, size_t sample, 89 unsigned int weight) { 90 // Convert to float and back to avoid integer overflow. 91 return (size_t)exp_avg((float)avg, (float)sample, weight); 92 } 93 94 // Printing 95 void print_on(outputStream* st) const; 96 void print() const; 97 }; 98 99 100 // A weighted average that includes a deviation from the average, 101 // some multiple of which is added to the average. 102 // 103 // This serves as our best estimate of an upper bound on a future 104 // unknown. 105 class AdaptivePaddedAverage : public AdaptiveWeightedAverage { 106 private: 107 float _padded_avg; // The last computed padded average 108 float _deviation; // Running deviation from the average 109 unsigned _padding; // A multiple which, added to the average, 110 // gives us an upper bound guess. 111 112 protected: 113 void set_padded_average(float avg) { _padded_avg = avg; } 114 void set_deviation(float dev) { _deviation = dev; } 115 116 public: 117 AdaptivePaddedAverage() : 118 AdaptiveWeightedAverage(0), 119 _padded_avg(0.0), _deviation(0.0), _padding(0) {} 120 121 AdaptivePaddedAverage(unsigned weight, unsigned padding) : 122 AdaptiveWeightedAverage(weight), 123 _padded_avg(0.0), _deviation(0.0), _padding(padding) {} 124 125 // Placement support 126 void* operator new(size_t ignored, void* p) { return p; } 127 // Allocator 128 void* operator new(size_t size) { return CHeapObj::operator new(size); } 129 130 // Accessor 131 float padded_average() const { return _padded_avg; } 132 float deviation() const { return _deviation; } 133 unsigned padding() const { return _padding; } 134 135 void clear() { 136 AdaptiveWeightedAverage::clear(); 137 _padded_avg = 0; 138 _deviation = 0; 139 } 140 141 // Override 142 void sample(float new_sample); 143 144 // Printing 145 void print_on(outputStream* st) const; 146 void print() const; 147 }; 148 149 // A weighted average that includes a deviation from the average, 150 // some multiple of which is added to the average. 151 // 152 // This serves as our best estimate of an upper bound on a future 153 // unknown. 154 // A special sort of padded average: it doesn't update deviations 155 // if the sample is zero. The average is allowed to change. We're 156 // preventing the zero samples from drastically changing our padded 157 // average. 158 class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage { 159 public: 160 AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) : 161 AdaptivePaddedAverage(weight, padding) {} 162 // Override 163 void sample(float new_sample); 164 165 // Printing 166 void print_on(outputStream* st) const; 167 void print() const; 168 }; 169 170 // Use a least squares fit to a set of data to generate a linear 171 // equation. 172 // y = intercept + slope * x 173 174 class LinearLeastSquareFit : public CHeapObj { 175 double _sum_x; // sum of all independent data points x 176 double _sum_x_squared; // sum of all independent data points x**2 177 double _sum_y; // sum of all dependent data points y 178 double _sum_xy; // sum of all x * y. 179 double _intercept; // constant term 180 double _slope; // slope 181 // The weighted averages are not currently used but perhaps should 182 // be used to get decaying averages. 183 AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable 184 AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable 185 186 public: 187 LinearLeastSquareFit(unsigned weight); 188 void update(double x, double y); 189 double y(double x); 190 double slope() { return _slope; } 191 // Methods to decide if a change in the dependent variable will 192 // achive a desired goal. Note that these methods are not 193 // complementary and both are needed. 194 bool decrement_will_decrease(); 195 bool increment_will_decrease(); 196 }; 197 198 class GCPauseTimer : StackObj { 199 elapsedTimer* _timer; 200 public: 201 GCPauseTimer(elapsedTimer* timer) { 202 _timer = timer; 203 _timer->stop(); 204 } 205 ~GCPauseTimer() { 206 _timer->start(); 207 } 208 };