gcUtil.hpp 7.7 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33
#ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP
#define SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP

#include "memory/allocation.hpp"
#include "runtime/timer.hpp"
#include "utilities/debug.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/ostream.hpp"

D
duke 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
// Catch-all file for utility classes

// A weighted average maintains a running, weighted average
// of some float value (templates would be handy here if we
// need different types).
//
// The average is adaptive in that we smooth it for the
// initial samples; we don't use the weight until we have
// enough samples for it to be meaningful.
//
// This serves as our best estimate of a future unknown.
//
class AdaptiveWeightedAverage : public CHeapObj {
 private:
  float            _average;        // The last computed average
  unsigned         _sample_count;   // How often we've sampled this average
  unsigned         _weight;         // The weight used to smooth the averages
                                    //   A higher weight favors the most
                                    //   recent data.
53 54 55
  bool             _is_old;         // Has enough historical data

  const static unsigned OLD_THRESHOLD = 100;
D
duke 已提交
56 57 58 59

 protected:
  float            _last_sample;    // The last value sampled.

60 61 62 63 64 65 66
  void  increment_count() {
    _sample_count++;
    if (!_is_old && _sample_count > OLD_THRESHOLD) {
      _is_old = true;
    }
  }

D
duke 已提交
67 68 69 70 71 72 73 74
  void  set_average(float avg)  { _average = avg;        }

  // Helper function, computes an adaptive weighted average
  // given a sample and the last average
  float compute_adaptive_average(float new_sample, float average);

 public:
  // Input weight must be between 0 and 100
75
  AdaptiveWeightedAverage(unsigned weight, float avg = 0.0) :
76 77
    _average(avg), _sample_count(0), _weight(weight), _last_sample(0.0),
    _is_old(false) {
D
duke 已提交
78 79
  }

80 81 82 83
  void clear() {
    _average = 0;
    _sample_count = 0;
    _last_sample = 0;
84
    _is_old = false;
85 86
  }

87 88 89 90 91 92 93
  // Useful for modifying static structures after startup.
  void  modify(size_t avg, unsigned wt, bool force = false)  {
    assert(force, "Are you sure you want to call this?");
    _average = (float)avg;
    _weight  = wt;
  }

D
duke 已提交
94 95 96 97
  // Accessors
  float    average() const       { return _average;       }
  unsigned weight()  const       { return _weight;        }
  unsigned count()   const       { return _sample_count;  }
98 99
  float    last_sample() const   { return _last_sample;   }
  bool     is_old()  const       { return _is_old;        }
D
duke 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113

  // Update data with a new sample.
  void sample(float new_sample);

  static inline float exp_avg(float avg, float sample,
                               unsigned int weight) {
    assert(0 <= weight && weight <= 100, "weight must be a percent");
    return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F;
  }
  static inline size_t exp_avg(size_t avg, size_t sample,
                               unsigned int weight) {
    // Convert to float and back to avoid integer overflow.
    return (size_t)exp_avg((float)avg, (float)sample, weight);
  }
114 115 116 117

  // Printing
  void print_on(outputStream* st) const;
  void print() const;
D
duke 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
};


// A weighted average that includes a deviation from the average,
// some multiple of which is added to the average.
//
// This serves as our best estimate of an upper bound on a future
// unknown.
class AdaptivePaddedAverage : public AdaptiveWeightedAverage {
 private:
  float          _padded_avg;     // The last computed padded average
  float          _deviation;      // Running deviation from the average
  unsigned       _padding;        // A multiple which, added to the average,
                                  // gives us an upper bound guess.

 protected:
  void set_padded_average(float avg)  { _padded_avg = avg;  }
  void set_deviation(float dev)       { _deviation  = dev;  }

 public:
  AdaptivePaddedAverage() :
    AdaptiveWeightedAverage(0),
    _padded_avg(0.0), _deviation(0.0), _padding(0) {}

  AdaptivePaddedAverage(unsigned weight, unsigned padding) :
    AdaptiveWeightedAverage(weight),
    _padded_avg(0.0), _deviation(0.0), _padding(padding) {}

  // Placement support
  void* operator new(size_t ignored, void* p) { return p; }
  // Allocator
  void* operator new(size_t size) { return CHeapObj::operator new(size); }

  // Accessor
  float padded_average() const         { return _padded_avg; }
  float deviation()      const         { return _deviation;  }
  unsigned padding()     const         { return _padding;    }

156 157 158 159 160 161
  void clear() {
    AdaptiveWeightedAverage::clear();
    _padded_avg = 0;
    _deviation = 0;
  }

D
duke 已提交
162 163
  // Override
  void  sample(float new_sample);
164 165 166 167

  // Printing
  void print_on(outputStream* st) const;
  void print() const;
D
duke 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
};

// A weighted average that includes a deviation from the average,
// some multiple of which is added to the average.
//
// This serves as our best estimate of an upper bound on a future
// unknown.
// A special sort of padded average:  it doesn't update deviations
// if the sample is zero. The average is allowed to change. We're
// preventing the zero samples from drastically changing our padded
// average.
class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage {
public:
  AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) :
    AdaptivePaddedAverage(weight, padding)  {}
  // Override
  void  sample(float new_sample);
185 186 187 188

  // Printing
  void print_on(outputStream* st) const;
  void print() const;
D
duke 已提交
189
};
190

D
duke 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
// Use a least squares fit to a set of data to generate a linear
// equation.
//              y = intercept + slope * x

class LinearLeastSquareFit : public CHeapObj {
  double _sum_x;        // sum of all independent data points x
  double _sum_x_squared; // sum of all independent data points x**2
  double _sum_y;        // sum of all dependent data points y
  double _sum_xy;       // sum of all x * y.
  double _intercept;     // constant term
  double _slope;        // slope
  // The weighted averages are not currently used but perhaps should
  // be used to get decaying averages.
  AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable
  AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable

 public:
  LinearLeastSquareFit(unsigned weight);
  void update(double x, double y);
  double y(double x);
  double slope() { return _slope; }
  // Methods to decide if a change in the dependent variable will
  // achive a desired goal.  Note that these methods are not
  // complementary and both are needed.
  bool decrement_will_decrease();
  bool increment_will_decrease();
};

class GCPauseTimer : StackObj {
  elapsedTimer* _timer;
 public:
  GCPauseTimer(elapsedTimer* timer) {
    _timer = timer;
    _timer->stop();
  }
  ~GCPauseTimer() {
    _timer->start();
  }
};
230 231

#endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP