1 /* 2 * Copyright (c) 2002, 2010, 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 #ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP 27 28 #include "memory/allocation.hpp" 29 #include "runtime/timer.hpp" 30 #include "utilities/debug.hpp" 31 #include "utilities/globalDefinitions.hpp" 32 #include "utilities/ostream.hpp" 33 34 // Catch-all file for utility classes 35 36 // A weighted average maintains a running, weighted average 37 // of some float value (templates would be handy here if we 38 // need different types). 39 // 40 // The average is adaptive in that we smooth it for the 41 // initial samples; we don't use the weight until we have 42 // enough samples for it to be meaningful. 43 // 44 // This serves as our best estimate of a future unknown. 45 // 46 class AdaptiveWeightedAverage : public CHeapObj { 47 private: 48 float _average; // The last computed average 49 unsigned _sample_count; // How often we've sampled this average 50 unsigned _weight; // The weight used to smooth the averages 51 // A higher weight favors the most 52 // recent data. 53 54 protected: 55 float _last_sample; // The last value sampled. 56 57 void increment_count() { _sample_count++; } 58 void set_average(float avg) { _average = avg; } 59 60 // Helper function, computes an adaptive weighted average 61 // given a sample and the last average 62 float compute_adaptive_average(float new_sample, float average); 63 64 public: 65 // Input weight must be between 0 and 100 66 AdaptiveWeightedAverage(unsigned weight, float avg = 0.0) : 67 _average(avg), _sample_count(0), _weight(weight), _last_sample(0.0) { 68 } 69 70 void clear() { 71 _average = 0; 72 _sample_count = 0; 73 _last_sample = 0; 74 } 75 76 // Useful for modifying static structures after startup. 77 void modify(size_t avg, unsigned wt, bool force = false) { 78 assert(force, "Are you sure you want to call this?"); 79 _average = (float)avg; 80 _weight = wt; 81 } 82 83 // Accessors 84 float average() const { return _average; } 85 unsigned weight() const { return _weight; } 86 unsigned count() const { return _sample_count; } 87 float last_sample() const { return _last_sample; } 88 89 // Update data with a new sample. 90 void sample(float new_sample); 91 92 static inline float exp_avg(float avg, float sample, 93 unsigned int weight) { 94 assert(0 <= weight && weight <= 100, "weight must be a percent"); 95 return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F; 96 } 97 static inline size_t exp_avg(size_t avg, size_t sample, 98 unsigned int weight) { 99 // Convert to float and back to avoid integer overflow. 100 return (size_t)exp_avg((float)avg, (float)sample, weight); 101 } 102 103 // Printing 104 void print_on(outputStream* st) const; 105 void print() const; 106 }; 107 108 109 // A weighted average that includes a deviation from the average, 110 // some multiple of which is added to the average. 111 // 112 // This serves as our best estimate of an upper bound on a future 113 // unknown. 114 class AdaptivePaddedAverage : public AdaptiveWeightedAverage { 115 private: 116 float _padded_avg; // The last computed padded average 117 float _deviation; // Running deviation from the average 118 unsigned _padding; // A multiple which, added to the average, 119 // gives us an upper bound guess. 120 121 protected: 122 void set_padded_average(float avg) { _padded_avg = avg; } 123 void set_deviation(float dev) { _deviation = dev; } 124 125 public: 126 AdaptivePaddedAverage() : 127 AdaptiveWeightedAverage(0), 128 _padded_avg(0.0), _deviation(0.0), _padding(0) {} 129 130 AdaptivePaddedAverage(unsigned weight, unsigned padding) : 131 AdaptiveWeightedAverage(weight), 132 _padded_avg(0.0), _deviation(0.0), _padding(padding) {} 133 134 // Placement support 135 void* operator new(size_t ignored, void* p) { return p; } 136 // Allocator 137 void* operator new(size_t size) { return CHeapObj::operator new(size); } 138 139 // Accessor 140 float padded_average() const { return _padded_avg; } 141 float deviation() const { return _deviation; } 142 unsigned padding() const { return _padding; } 143 144 void clear() { 145 AdaptiveWeightedAverage::clear(); 146 _padded_avg = 0; 147 _deviation = 0; 148 } 149 150 // Override 151 void sample(float new_sample); 152 153 // Printing 154 void print_on(outputStream* st) const; 155 void print() const; 156 }; 157 158 // A weighted average that includes a deviation from the average, 159 // some multiple of which is added to the average. 160 // 161 // This serves as our best estimate of an upper bound on a future 162 // unknown. 163 // A special sort of padded average: it doesn't update deviations 164 // if the sample is zero. The average is allowed to change. We're 165 // preventing the zero samples from drastically changing our padded 166 // average. 167 class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage { 168 public: 169 AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) : 170 AdaptivePaddedAverage(weight, padding) {} 171 // Override 172 void sample(float new_sample); 173 174 // Printing 175 void print_on(outputStream* st) const; 176 void print() const; 177 }; 178 179 // Use a least squares fit to a set of data to generate a linear 180 // equation. 181 // y = intercept + slope * x 182 183 class LinearLeastSquareFit : public CHeapObj { 184 double _sum_x; // sum of all independent data points x 185 double _sum_x_squared; // sum of all independent data points x**2 186 double _sum_y; // sum of all dependent data points y 187 double _sum_xy; // sum of all x * y. 188 double _intercept; // constant term 189 double _slope; // slope 190 // The weighted averages are not currently used but perhaps should 191 // be used to get decaying averages. 192 AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable 193 AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable 194 195 public: 196 LinearLeastSquareFit(unsigned weight); 197 void update(double x, double y); 198 double y(double x); 199 double slope() { return _slope; } 200 // Methods to decide if a change in the dependent variable will 201 // achive a desired goal. Note that these methods are not 202 // complementary and both are needed. 203 bool decrement_will_decrease(); 204 bool increment_will_decrease(); 205 }; 206 207 class GCPauseTimer : StackObj { 208 elapsedTimer* _timer; 209 public: 210 GCPauseTimer(elapsedTimer* timer) { 211 _timer = timer; 212 _timer->stop(); 213 } 214 ~GCPauseTimer() { 215 _timer->start(); 216 } 217 }; 218 219 #endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP