Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/shared/gcUtil.hpp
38920 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP25#define SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP2627#include "memory/allocation.hpp"28#include "runtime/timer.hpp"29#include "utilities/debug.hpp"30#include "utilities/globalDefinitions.hpp"31#include "utilities/ostream.hpp"3233// Catch-all file for utility classes3435// A weighted average maintains a running, weighted average36// of some float value (templates would be handy here if we37// need different types).38//39// The average is adaptive in that we smooth it for the40// initial samples; we don't use the weight until we have41// enough samples for it to be meaningful.42//43// This serves as our best estimate of a future unknown.44//45class AdaptiveWeightedAverage : public CHeapObj<mtGC> {46private:47float _average; // The last computed average48unsigned _sample_count; // How often we've sampled this average49unsigned _weight; // The weight used to smooth the averages50// A higher weight favors the most51// recent data.52bool _is_old; // Has enough historical data5354const static unsigned OLD_THRESHOLD = 100;5556protected:57float _last_sample; // The last value sampled.5859void increment_count() {60_sample_count++;61if (!_is_old && _sample_count > OLD_THRESHOLD) {62_is_old = true;63}64}6566void set_average(float avg) { _average = avg; }6768// Helper function, computes an adaptive weighted average69// given a sample and the last average70float compute_adaptive_average(float new_sample, float average);7172public:73// Input weight must be between 0 and 10074AdaptiveWeightedAverage(unsigned weight, float avg = 0.0) :75_average(avg), _sample_count(0), _weight(weight), _last_sample(0.0),76_is_old(false) {77}7879void clear() {80_average = 0;81_sample_count = 0;82_last_sample = 0;83_is_old = false;84}8586// Useful for modifying static structures after startup.87void modify(size_t avg, unsigned wt, bool force = false) {88assert(force, "Are you sure you want to call this?");89_average = (float)avg;90_weight = wt;91}9293// Accessors94float average() const { return _average; }95unsigned weight() const { return _weight; }96unsigned count() const { return _sample_count; }97float last_sample() const { return _last_sample; }98bool is_old() const { return _is_old; }99100// Update data with a new sample.101void sample(float new_sample);102103static inline float exp_avg(float avg, float sample,104unsigned int weight) {105assert(0 <= weight && weight <= 100, "weight must be a percent");106return (100.0F - weight) * avg / 100.0F + weight * sample / 100.0F;107}108static inline size_t exp_avg(size_t avg, size_t sample,109unsigned int weight) {110// Convert to float and back to avoid integer overflow.111return (size_t)exp_avg((float)avg, (float)sample, weight);112}113114// Printing115void print_on(outputStream* st) const;116void print() const;117};118119120// A weighted average that includes a deviation from the average,121// some multiple of which is added to the average.122//123// This serves as our best estimate of an upper bound on a future124// unknown.125class AdaptivePaddedAverage : public AdaptiveWeightedAverage {126private:127float _padded_avg; // The last computed padded average128float _deviation; // Running deviation from the average129unsigned _padding; // A multiple which, added to the average,130// gives us an upper bound guess.131132protected:133void set_padded_average(float avg) { _padded_avg = avg; }134void set_deviation(float dev) { _deviation = dev; }135136public:137AdaptivePaddedAverage() :138AdaptiveWeightedAverage(0),139_padded_avg(0.0), _deviation(0.0), _padding(0) {}140141AdaptivePaddedAverage(unsigned weight, unsigned padding) :142AdaptiveWeightedAverage(weight),143_padded_avg(0.0), _deviation(0.0), _padding(padding) {}144145// Placement support146void* operator new(size_t ignored, void* p) throw() { return p; }147// Allocator148void* operator new(size_t size) throw() { return CHeapObj<mtGC>::operator new(size); }149150// Accessor151float padded_average() const { return _padded_avg; }152float deviation() const { return _deviation; }153unsigned padding() const { return _padding; }154155void clear() {156AdaptiveWeightedAverage::clear();157_padded_avg = 0;158_deviation = 0;159}160161// Override162void sample(float new_sample);163164// Printing165void print_on(outputStream* st) const;166void print() const;167};168169// A weighted average that includes a deviation from the average,170// some multiple of which is added to the average.171//172// This serves as our best estimate of an upper bound on a future173// unknown.174// A special sort of padded average: it doesn't update deviations175// if the sample is zero. The average is allowed to change. We're176// preventing the zero samples from drastically changing our padded177// average.178class AdaptivePaddedNoZeroDevAverage : public AdaptivePaddedAverage {179public:180AdaptivePaddedNoZeroDevAverage(unsigned weight, unsigned padding) :181AdaptivePaddedAverage(weight, padding) {}182// Override183void sample(float new_sample);184185// Printing186void print_on(outputStream* st) const;187void print() const;188};189190// Use a least squares fit to a set of data to generate a linear191// equation.192// y = intercept + slope * x193194class LinearLeastSquareFit : public CHeapObj<mtGC> {195double _sum_x; // sum of all independent data points x196double _sum_x_squared; // sum of all independent data points x**2197double _sum_y; // sum of all dependent data points y198double _sum_xy; // sum of all x * y.199double _intercept; // constant term200double _slope; // slope201// The weighted averages are not currently used but perhaps should202// be used to get decaying averages.203AdaptiveWeightedAverage _mean_x; // weighted mean of independent variable204AdaptiveWeightedAverage _mean_y; // weighted mean of dependent variable205206public:207LinearLeastSquareFit(unsigned weight);208void update(double x, double y);209double y(double x);210double slope() { return _slope; }211// Methods to decide if a change in the dependent variable will212// achive a desired goal. Note that these methods are not213// complementary and both are needed.214bool decrement_will_decrease();215bool increment_will_decrease();216};217218class GCPauseTimer : StackObj {219elapsedTimer* _timer;220public:221GCPauseTimer(elapsedTimer* timer) {222_timer = timer;223_timer->stop();224}225~GCPauseTimer() {226_timer->start();227}228};229230#endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_GCUTIL_HPP231232233