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

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5614
9758d9f36299
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

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

mercurial