gcUtil.hpp revision 8413:92457dfb91bd
1236884Smm/*
2236884Smm * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
3236884Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4236884Smm *
5236884Smm * This code is free software; you can redistribute it and/or modify it
6236884Smm * under the terms of the GNU General Public License version 2 only, as
7236884Smm * published by the Free Software Foundation.
8236884Smm *
9236884Smm * This code is distributed in the hope that it will be useful, but WITHOUT
10236884Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11236884Smm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12236884Smm * version 2 for more details (a copy is included in the LICENSE file that
13236884Smm * accompanied this code).
14236884Smm *
15236884Smm * You should have received a copy of the GNU General Public License version
16236884Smm * 2 along with this work; if not, write to the Free Software Foundation,
17236884Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18236884Smm *
19236884Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20236884Smm * or visit www.oracle.com if you need additional information or have any
21236884Smm * questions.
22236884Smm *
23286708Smav */
24246586Sdelphij
25255750Sdelphij#ifndef SHARE_VM_GC_SHARED_GCUTIL_HPP
26268126Sdelphij#define SHARE_VM_GC_SHARED_GCUTIL_HPP
27236884Smm
28236884Smm#include "memory/allocation.hpp"
29236884Smm#include "runtime/timer.hpp"
30236884Smm#include "utilities/debug.hpp"
31236884Smm#include "utilities/globalDefinitions.hpp"
32236884Smm#include "utilities/ostream.hpp"
33236884Smm
34236884Smm// Catch-all file for utility classes
35236884Smm
36236884Smm// A weighted average maintains a running, weighted average
37236884Smm// of some float value (templates would be handy here if we
38236884Smm// need different types).
39236884Smm//
40236884Smm// The average is adaptive in that we smooth it for the
41236884Smm// initial samples; we don't use the weight until we have
42236884Smm// enough samples for it to be meaningful.
43236884Smm//
44236884Smm// This serves as our best estimate of a future unknown.
45236884Smm//
46236884Smmclass AdaptiveWeightedAverage : public CHeapObj<mtGC> {
47236884Smm private:
48236884Smm  float            _average;        // The last computed average
49236884Smm  unsigned         _sample_count;   // How often we've sampled this average
50236884Smm  unsigned         _weight;         // The weight used to smooth the averages
51236884Smm                                    //   A higher weight favors the most
52236884Smm                                    //   recent data.
53236884Smm  bool             _is_old;         // Has enough historical data
54236884Smm
55236884Smm  const static unsigned OLD_THRESHOLD = 100;
56236884Smm
57236884Smm protected:
58236884Smm  float            _last_sample;    // The last value sampled.
59274337Sdelphij
60274337Sdelphij  void  increment_count() {
61236884Smm    _sample_count++;
62236884Smm    if (!_is_old && _sample_count > OLD_THRESHOLD) {
63236884Smm      _is_old = true;
64236884Smm    }
65236884Smm  }
66236884Smm
67236884Smm  void  set_average(float avg)  { _average = avg;        }
68236884Smm
69236884Smm  // Helper function, computes an adaptive weighted average
70236884Smm  // given a sample and the last average
71236884Smm  float compute_adaptive_average(float new_sample, float average);
72236884Smm
73236884Smm public:
74236884Smm  // Input weight must be between 0 and 100
75236884Smm  AdaptiveWeightedAverage(unsigned weight, float avg = 0.0) :
76236884Smm    _average(avg), _sample_count(0), _weight(weight), _last_sample(0.0),
77236884Smm    _is_old(false) {
78236884Smm  }
79236884Smm
80236884Smm  void clear() {
81236884Smm    _average = 0;
82236884Smm    _sample_count = 0;
83236884Smm    _last_sample = 0;
84236884Smm    _is_old = false;
85236884Smm  }
86236884Smm
87236884Smm  // Useful for modifying static structures after startup.
88236884Smm  void  modify(size_t avg, unsigned wt, bool force = false)  {
89236884Smm    assert(force, "Are you sure you want to call this?");
90236884Smm    _average = (float)avg;
91236884Smm    _weight  = wt;
92236884Smm  }
93236884Smm
94236884Smm  // Accessors
95236884Smm  float    average() const       { return _average;       }
96259813Sdelphij  unsigned weight()  const       { return _weight;        }
97236884Smm  unsigned count()   const       { return _sample_count;  }
98259813Sdelphij  float    last_sample() const   { return _last_sample;   }
99259813Sdelphij  bool     is_old()  const       { return _is_old;        }
100236884Smm
101259813Sdelphij  // Update data with a new sample.
102236884Smm  void sample(float new_sample);
103236884Smm
104236884Smm  static inline float exp_avg(float avg, float sample,
105259813Sdelphij                               unsigned int weight) {
106236884Smm    assert(weight <= 100, "weight must be a percent");
107259813Sdelphij    return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F;
108236884Smm  }
109236884Smm  static inline size_t exp_avg(size_t avg, size_t sample,
110236884Smm                               unsigned int weight) {
111259813Sdelphij    // Convert to float and back to avoid integer overflow.
112236884Smm    return (size_t)exp_avg((float)avg, (float)sample, weight);
113236884Smm  }
114236884Smm
115236884Smm  // Printing
116236884Smm  void print_on(outputStream* st) const;
117236884Smm  void print() const;
118236884Smm};
119260150Sdelphij
120289562Smav
121289562Smav// A weighted average that includes a deviation from the average,
122260150Sdelphij// some multiple of which is added to the average.
123260150Sdelphij//
124260150Sdelphij// This serves as our best estimate of an upper bound on a future
125260150Sdelphij// unknown.
126260150Sdelphijclass AdaptivePaddedAverage : public AdaptiveWeightedAverage {
127260150Sdelphij private:
128260150Sdelphij  float          _padded_avg;     // The last computed padded average
129260150Sdelphij  float          _deviation;      // Running deviation from the average
130260150Sdelphij  unsigned       _padding;        // A multiple which, added to the average,
131236884Smm                                  // gives us an upper bound guess.
132259813Sdelphij
133286708Smav protected:
134236884Smm  void set_padded_average(float avg)  { _padded_avg = avg;  }
135236884Smm  void set_deviation(float dev)       { _deviation  = dev;  }
136259813Sdelphij
137236884Smm public:
138236884Smm  AdaptivePaddedAverage() :
139236884Smm    AdaptiveWeightedAverage(0),
140286708Smav    _padded_avg(0.0), _deviation(0.0), _padding(0) {}
141286708Smav
142236884Smm  AdaptivePaddedAverage(unsigned weight, unsigned padding) :
143236884Smm    AdaptiveWeightedAverage(weight),
144236884Smm    _padded_avg(0.0), _deviation(0.0), _padding(padding) {}
145236884Smm
146236884Smm  // Placement support
147236884Smm  void* operator new(size_t ignored, void* p) throw() { return p; }
148259813Sdelphij  // Allocator
149236884Smm  void* operator new(size_t size) throw() { return CHeapObj<mtGC>::operator new(size); }
150236884Smm
151236884Smm  // Accessor
152286708Smav  float padded_average() const         { return _padded_avg; }
153236884Smm  float deviation()      const         { return _deviation;  }
154236884Smm  unsigned padding()     const         { return _padding;    }
155236884Smm
156236884Smm  void clear() {
157236884Smm    AdaptiveWeightedAverage::clear();
158236884Smm    _padded_avg = 0;
159236884Smm    _deviation = 0;
160236884Smm  }
161286708Smav
162286708Smav  // Override
163260150Sdelphij  void  sample(float new_sample);
164239774Smm
165239774Smm  // Printing
166286708Smav  void print_on(outputStream* st) const;
167286708Smav  void print() const;
168260150Sdelphij};
169246586Sdelphij
170246586Sdelphij// A weighted average that includes a deviation from the average,
171286708Smav// some multiple of which is added to the average.
172286708Smav//
173260150Sdelphij// This serves as our best estimate of an upper bound on a future
174255750Sdelphij// unknown.
175255750Sdelphij// A special sort of padded average:  it doesn't update deviations
176286708Smav// if the sample is zero. The average is allowed to change. We're
177286708Smav// preventing the zero samples from drastically changing our padded
178260150Sdelphij// average.
179258717Savgclass AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage {
180258717Savgpublic:
181286708Smav  AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) :
182286708Smav    AdaptivePaddedAverage(weight, padding)  {}
183260150Sdelphij  // Override
184260150Sdelphij  void  sample(float new_sample);
185260150Sdelphij
186286708Smav  // Printing
187286708Smav  void print_on(outputStream* st) const;
188260150Sdelphij  void print() const;
189260150Sdelphij};
190260150Sdelphij
191260150Sdelphij// Use a least squares fit to a set of data to generate a linear
192260150Sdelphij// equation.
193260150Sdelphij//              y = intercept + slope * x
194286708Smav
195286708Smavclass LinearLeastSquareFit : public CHeapObj<mtGC> {
196260150Sdelphij  double _sum_x;        // sum of all independent data points x
197259813Sdelphij  double _sum_x_squared; // sum of all independent data points x**2
198259813Sdelphij  double _sum_y;        // sum of all dependent data points y
199259813Sdelphij  double _sum_xy;       // sum of all x * y.
200286708Smav  double _intercept;     // constant term
201260183Sdelphij  double _slope;        // slope
202260183Sdelphij  // The weighted averages are not currently used but perhaps should
203260183Sdelphij  // be used to get decaying averages.
204260183Sdelphij  AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable
205260183Sdelphij  AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable
206260183Sdelphij
207260183Sdelphij public:
208260183Sdelphij  LinearLeastSquareFit(unsigned weight);
209286708Smav  void update(double x, double y);
210264835Sdelphij  double y(double x);
211264835Sdelphij  double slope() { return _slope; }
212264835Sdelphij  // Methods to decide if a change in the dependent variable will
213264835Sdelphij  // achieve a desired goal.  Note that these methods are not
214264835Sdelphij  // complementary and both are needed.
215264835Sdelphij  bool decrement_will_decrease();
216264835Sdelphij  bool increment_will_decrease();
217286708Smav};
218286708Smav
219268075Sdelphij#endif // SHARE_VM_GC_SHARED_GCUTIL_HPP
220268075Sdelphij