duke@435: /* coleenp@5614: * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP stefank@2314: #define SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "runtime/timer.hpp" stefank@2314: #include "utilities/debug.hpp" stefank@2314: #include "utilities/globalDefinitions.hpp" stefank@2314: #include "utilities/ostream.hpp" stefank@2314: duke@435: // Catch-all file for utility classes duke@435: duke@435: // A weighted average maintains a running, weighted average duke@435: // of some float value (templates would be handy here if we duke@435: // need different types). duke@435: // duke@435: // The average is adaptive in that we smooth it for the duke@435: // initial samples; we don't use the weight until we have duke@435: // enough samples for it to be meaningful. duke@435: // duke@435: // This serves as our best estimate of a future unknown. duke@435: // zgu@3900: class AdaptiveWeightedAverage : public CHeapObj { duke@435: private: duke@435: float _average; // The last computed average duke@435: unsigned _sample_count; // How often we've sampled this average duke@435: unsigned _weight; // The weight used to smooth the averages duke@435: // A higher weight favors the most duke@435: // recent data. mikael@3763: bool _is_old; // Has enough historical data mikael@3763: mikael@3763: const static unsigned OLD_THRESHOLD = 100; duke@435: duke@435: protected: duke@435: float _last_sample; // The last value sampled. duke@435: mikael@3763: void increment_count() { mikael@3763: _sample_count++; mikael@3763: if (!_is_old && _sample_count > OLD_THRESHOLD) { mikael@3763: _is_old = true; mikael@3763: } mikael@3763: } mikael@3763: duke@435: void set_average(float avg) { _average = avg; } duke@435: duke@435: // Helper function, computes an adaptive weighted average duke@435: // given a sample and the last average duke@435: float compute_adaptive_average(float new_sample, float average); duke@435: duke@435: public: duke@435: // Input weight must be between 0 and 100 ysr@1580: AdaptiveWeightedAverage(unsigned weight, float avg = 0.0) : mikael@3763: _average(avg), _sample_count(0), _weight(weight), _last_sample(0.0), mikael@3763: _is_old(false) { duke@435: } duke@435: iveresov@703: void clear() { iveresov@703: _average = 0; iveresov@703: _sample_count = 0; iveresov@703: _last_sample = 0; mikael@3763: _is_old = false; iveresov@703: } iveresov@703: ysr@1580: // Useful for modifying static structures after startup. ysr@1580: void modify(size_t avg, unsigned wt, bool force = false) { ysr@1580: assert(force, "Are you sure you want to call this?"); ysr@1580: _average = (float)avg; ysr@1580: _weight = wt; ysr@1580: } ysr@1580: duke@435: // Accessors duke@435: float average() const { return _average; } duke@435: unsigned weight() const { return _weight; } duke@435: unsigned count() const { return _sample_count; } mikael@3763: float last_sample() const { return _last_sample; } mikael@3763: bool is_old() const { return _is_old; } duke@435: duke@435: // Update data with a new sample. duke@435: void sample(float new_sample); duke@435: duke@435: static inline float exp_avg(float avg, float sample, duke@435: unsigned int weight) { duke@435: assert(0 <= weight && weight <= 100, "weight must be a percent"); duke@435: return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F; duke@435: } duke@435: static inline size_t exp_avg(size_t avg, size_t sample, duke@435: unsigned int weight) { duke@435: // Convert to float and back to avoid integer overflow. duke@435: return (size_t)exp_avg((float)avg, (float)sample, weight); duke@435: } ysr@1580: ysr@1580: // Printing ysr@1580: void print_on(outputStream* st) const; ysr@1580: void print() const; duke@435: }; duke@435: duke@435: duke@435: // A weighted average that includes a deviation from the average, duke@435: // some multiple of which is added to the average. duke@435: // duke@435: // This serves as our best estimate of an upper bound on a future duke@435: // unknown. duke@435: class AdaptivePaddedAverage : public AdaptiveWeightedAverage { duke@435: private: duke@435: float _padded_avg; // The last computed padded average duke@435: float _deviation; // Running deviation from the average duke@435: unsigned _padding; // A multiple which, added to the average, duke@435: // gives us an upper bound guess. duke@435: duke@435: protected: duke@435: void set_padded_average(float avg) { _padded_avg = avg; } duke@435: void set_deviation(float dev) { _deviation = dev; } duke@435: duke@435: public: duke@435: AdaptivePaddedAverage() : duke@435: AdaptiveWeightedAverage(0), duke@435: _padded_avg(0.0), _deviation(0.0), _padding(0) {} duke@435: duke@435: AdaptivePaddedAverage(unsigned weight, unsigned padding) : duke@435: AdaptiveWeightedAverage(weight), duke@435: _padded_avg(0.0), _deviation(0.0), _padding(padding) {} duke@435: duke@435: // Placement support coleenp@5614: void* operator new(size_t ignored, void* p) throw() { return p; } duke@435: // Allocator coleenp@5614: void* operator new(size_t size) throw() { return CHeapObj::operator new(size); } duke@435: duke@435: // Accessor duke@435: float padded_average() const { return _padded_avg; } duke@435: float deviation() const { return _deviation; } duke@435: unsigned padding() const { return _padding; } duke@435: iveresov@703: void clear() { iveresov@703: AdaptiveWeightedAverage::clear(); iveresov@703: _padded_avg = 0; iveresov@703: _deviation = 0; iveresov@703: } iveresov@703: duke@435: // Override duke@435: void sample(float new_sample); ysr@1580: ysr@1580: // Printing ysr@1580: void print_on(outputStream* st) const; ysr@1580: void print() const; duke@435: }; duke@435: duke@435: // A weighted average that includes a deviation from the average, duke@435: // some multiple of which is added to the average. duke@435: // duke@435: // This serves as our best estimate of an upper bound on a future duke@435: // unknown. duke@435: // A special sort of padded average: it doesn't update deviations duke@435: // if the sample is zero. The average is allowed to change. We're duke@435: // preventing the zero samples from drastically changing our padded duke@435: // average. duke@435: class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage { duke@435: public: duke@435: AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) : duke@435: AdaptivePaddedAverage(weight, padding) {} duke@435: // Override duke@435: void sample(float new_sample); ysr@1580: ysr@1580: // Printing ysr@1580: void print_on(outputStream* st) const; ysr@1580: void print() const; duke@435: }; ysr@1580: duke@435: // Use a least squares fit to a set of data to generate a linear duke@435: // equation. duke@435: // y = intercept + slope * x duke@435: zgu@3900: class LinearLeastSquareFit : public CHeapObj { duke@435: double _sum_x; // sum of all independent data points x duke@435: double _sum_x_squared; // sum of all independent data points x**2 duke@435: double _sum_y; // sum of all dependent data points y duke@435: double _sum_xy; // sum of all x * y. duke@435: double _intercept; // constant term duke@435: double _slope; // slope duke@435: // The weighted averages are not currently used but perhaps should duke@435: // be used to get decaying averages. duke@435: AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable duke@435: AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable duke@435: duke@435: public: duke@435: LinearLeastSquareFit(unsigned weight); duke@435: void update(double x, double y); duke@435: double y(double x); duke@435: double slope() { return _slope; } duke@435: // Methods to decide if a change in the dependent variable will duke@435: // achive a desired goal. Note that these methods are not duke@435: // complementary and both are needed. duke@435: bool decrement_will_decrease(); duke@435: bool increment_will_decrease(); duke@435: }; duke@435: duke@435: class GCPauseTimer : StackObj { duke@435: elapsedTimer* _timer; duke@435: public: duke@435: GCPauseTimer(elapsedTimer* timer) { duke@435: _timer = timer; duke@435: _timer->stop(); duke@435: } duke@435: ~GCPauseTimer() { duke@435: _timer->start(); duke@435: } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP