hotspot/src/share/vm/gc_implementation/shared/gcUtil.hpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 976 241230d48896
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2005 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// Catch-all file for utility classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
// A weighted average maintains a running, weighted average
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// of some float value (templates would be handy here if we
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// need different types).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// The average is adaptive in that we smooth it for the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// initial samples; we don't use the weight until we have
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// enough samples for it to be meaningful.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// This serves as our best estimate of a future unknown.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
class AdaptiveWeightedAverage : public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  float            _average;        // The last computed average
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  unsigned         _sample_count;   // How often we've sampled this average
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  unsigned         _weight;         // The weight used to smooth the averages
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
                                    //   A higher weight favors the most
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
                                    //   recent data.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  float            _last_sample;    // The last value sampled.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  void  increment_count()       { _sample_count++;       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  void  set_average(float avg)  { _average = avg;        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  // Helper function, computes an adaptive weighted average
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  // given a sample and the last average
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  float compute_adaptive_average(float new_sample, float average);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  // Input weight must be between 0 and 100
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  AdaptiveWeightedAverage(unsigned weight) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    _average(0.0), _sample_count(0), _weight(weight), _last_sample(0.0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  // Accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  float    average() const       { return _average;       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  unsigned weight()  const       { return _weight;        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  unsigned count()   const       { return _sample_count;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  float    last_sample() const   { return _last_sample; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // Update data with a new sample.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  void sample(float new_sample);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  static inline float exp_avg(float avg, float sample,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
                               unsigned int weight) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    assert(0 <= weight && weight <= 100, "weight must be a percent");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  static inline size_t exp_avg(size_t avg, size_t sample,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
                               unsigned int weight) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    // Convert to float and back to avoid integer overflow.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    return (size_t)exp_avg((float)avg, (float)sample, weight);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
// A weighted average that includes a deviation from the average,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
// some multiple of which is added to the average.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
// This serves as our best estimate of an upper bound on a future
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
// unknown.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
class AdaptivePaddedAverage : public AdaptiveWeightedAverage {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  float          _padded_avg;     // The last computed padded average
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  float          _deviation;      // Running deviation from the average
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  unsigned       _padding;        // A multiple which, added to the average,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
                                  // gives us an upper bound guess.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  void set_padded_average(float avg)  { _padded_avg = avg;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  void set_deviation(float dev)       { _deviation  = dev;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  AdaptivePaddedAverage() :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    AdaptiveWeightedAverage(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    _padded_avg(0.0), _deviation(0.0), _padding(0) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  AdaptivePaddedAverage(unsigned weight, unsigned padding) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    AdaptiveWeightedAverage(weight),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    _padded_avg(0.0), _deviation(0.0), _padding(padding) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  // Placement support
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  void* operator new(size_t ignored, void* p) { return p; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  // Allocator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  void* operator new(size_t size) { return CHeapObj::operator new(size); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // Accessor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  float padded_average() const         { return _padded_avg; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  float deviation()      const         { return _deviation;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  unsigned padding()     const         { return _padding;    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // Override
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  void  sample(float new_sample);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
// A weighted average that includes a deviation from the average,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
// some multiple of which is added to the average.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
// This serves as our best estimate of an upper bound on a future
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
// unknown.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
// A special sort of padded average:  it doesn't update deviations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
// if the sample is zero. The average is allowed to change. We're
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
// preventing the zero samples from drastically changing our padded
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
// average.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    AdaptivePaddedAverage(weight, padding)  {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  // Override
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  void  sample(float new_sample);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
// Use a least squares fit to a set of data to generate a linear
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
// equation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
//              y = intercept + slope * x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
class LinearLeastSquareFit : public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  double _sum_x;        // sum of all independent data points x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  double _sum_x_squared; // sum of all independent data points x**2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  double _sum_y;        // sum of all dependent data points y
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  double _sum_xy;       // sum of all x * y.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  double _intercept;     // constant term
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  double _slope;        // slope
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  // The weighted averages are not currently used but perhaps should
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  // be used to get decaying averages.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  LinearLeastSquareFit(unsigned weight);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  void update(double x, double y);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  double y(double x);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  double slope() { return _slope; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  // Methods to decide if a change in the dependent variable will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  // achive a desired goal.  Note that these methods are not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  // complementary and both are needed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  bool decrement_will_decrease();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  bool increment_will_decrease();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
class GCPauseTimer : StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  elapsedTimer* _timer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  GCPauseTimer(elapsedTimer* timer) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    _timer = timer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    _timer->stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  ~GCPauseTimer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    _timer->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
};