duke@435: /* mikael@4153: * Copyright (c) 2002, 2012, 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: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/shared/gcUtil.hpp" duke@435: duke@435: // Catch-all file for utility classes duke@435: duke@435: float AdaptiveWeightedAverage::compute_adaptive_average(float new_sample, duke@435: float average) { duke@435: // We smooth the samples by not using weight() directly until we've duke@435: // had enough data to make it meaningful. We'd like the first weight mikael@3763: // used to be 1, the second to be 1/2, etc until we have mikael@3763: // OLD_THRESHOLD/weight samples. mikael@3763: unsigned count_weight = 0; mikael@3763: mikael@3763: // Avoid division by zero if the counter wraps (7158457) mikael@3763: if (!is_old()) { mikael@3763: count_weight = OLD_THRESHOLD/count(); mikael@3763: } mikael@3763: duke@435: unsigned adaptive_weight = (MAX2(weight(), count_weight)); duke@435: duke@435: float new_avg = exp_avg(average, new_sample, adaptive_weight); duke@435: duke@435: return new_avg; duke@435: } duke@435: duke@435: void AdaptiveWeightedAverage::sample(float new_sample) { duke@435: increment_count(); duke@435: duke@435: // Compute the new weighted average duke@435: float new_avg = compute_adaptive_average(new_sample, average()); duke@435: set_average(new_avg); duke@435: _last_sample = new_sample; duke@435: } duke@435: ysr@1580: void AdaptiveWeightedAverage::print() const { ysr@1580: print_on(tty); ysr@1580: } ysr@1580: ysr@1580: void AdaptiveWeightedAverage::print_on(outputStream* st) const { ysr@1580: guarantee(false, "NYI"); ysr@1580: } ysr@1580: ysr@1580: void AdaptivePaddedAverage::print() const { ysr@1580: print_on(tty); ysr@1580: } ysr@1580: ysr@1580: void AdaptivePaddedAverage::print_on(outputStream* st) const { ysr@1580: guarantee(false, "NYI"); ysr@1580: } ysr@1580: ysr@1580: void AdaptivePaddedNoZeroDevAverage::print() const { ysr@1580: print_on(tty); ysr@1580: } ysr@1580: ysr@1580: void AdaptivePaddedNoZeroDevAverage::print_on(outputStream* st) const { ysr@1580: guarantee(false, "NYI"); ysr@1580: } ysr@1580: duke@435: void AdaptivePaddedAverage::sample(float new_sample) { ysr@1580: // Compute new adaptive weighted average based on new sample. duke@435: AdaptiveWeightedAverage::sample(new_sample); duke@435: ysr@1580: // Now update the deviation and the padded average. duke@435: float new_avg = average(); duke@435: float new_dev = compute_adaptive_average(fabsd(new_sample - new_avg), duke@435: deviation()); duke@435: set_deviation(new_dev); duke@435: set_padded_average(new_avg + padding() * new_dev); duke@435: _last_sample = new_sample; duke@435: } duke@435: duke@435: void AdaptivePaddedNoZeroDevAverage::sample(float new_sample) { duke@435: // Compute our parent classes sample information duke@435: AdaptiveWeightedAverage::sample(new_sample); duke@435: duke@435: float new_avg = average(); duke@435: if (new_sample != 0) { duke@435: // We only create a new deviation if the sample is non-zero duke@435: float new_dev = compute_adaptive_average(fabsd(new_sample - new_avg), duke@435: deviation()); duke@435: duke@435: set_deviation(new_dev); duke@435: } duke@435: set_padded_average(new_avg + padding() * deviation()); duke@435: _last_sample = new_sample; duke@435: } duke@435: duke@435: LinearLeastSquareFit::LinearLeastSquareFit(unsigned weight) : phh@2505: _sum_x(0), _sum_x_squared(0), _sum_y(0), _sum_xy(0), phh@2505: _intercept(0), _slope(0), _mean_x(weight), _mean_y(weight) {} duke@435: duke@435: void LinearLeastSquareFit::update(double x, double y) { duke@435: _sum_x = _sum_x + x; duke@435: _sum_x_squared = _sum_x_squared + x * x; duke@435: _sum_y = _sum_y + y; duke@435: _sum_xy = _sum_xy + x * y; duke@435: _mean_x.sample(x); duke@435: _mean_y.sample(y); duke@435: assert(_mean_x.count() == _mean_y.count(), "Incorrect count"); duke@435: if ( _mean_x.count() > 1 ) { duke@435: double slope_denominator; duke@435: slope_denominator = (_mean_x.count() * _sum_x_squared - _sum_x * _sum_x); duke@435: // Some tolerance should be injected here. A denominator that is duke@435: // nearly 0 should be avoided. duke@435: duke@435: if (slope_denominator != 0.0) { duke@435: double slope_numerator; duke@435: slope_numerator = (_mean_x.count() * _sum_xy - _sum_x * _sum_y); duke@435: _slope = slope_numerator / slope_denominator; duke@435: duke@435: // The _mean_y and _mean_x are decaying averages and can duke@435: // be used to discount earlier data. If they are used, duke@435: // first consider whether all the quantities should be duke@435: // kept as decaying averages. duke@435: // _intercept = _mean_y.average() - _slope * _mean_x.average(); duke@435: _intercept = (_sum_y - _slope * _sum_x) / ((double) _mean_x.count()); duke@435: } duke@435: } duke@435: } duke@435: duke@435: double LinearLeastSquareFit::y(double x) { duke@435: double new_y; duke@435: duke@435: if ( _mean_x.count() > 1 ) { duke@435: new_y = (_intercept + _slope * x); duke@435: return new_y; duke@435: } else { duke@435: return _mean_y.average(); duke@435: } duke@435: } duke@435: duke@435: // Both decrement_will_decrease() and increment_will_decrease() return duke@435: // true for a slope of 0. That is because a change is necessary before duke@435: // a slope can be calculated and a 0 slope will, in general, indicate duke@435: // that no calculation of the slope has yet been done. Returning true duke@435: // for a slope equal to 0 reflects the intuitive expectation of the duke@435: // dependence on the slope. Don't use the complement of these functions duke@435: // since that untuitive expectation is not built into the complement. duke@435: bool LinearLeastSquareFit::decrement_will_decrease() { duke@435: return (_slope >= 0.00); duke@435: } duke@435: duke@435: bool LinearLeastSquareFit::increment_will_decrease() { duke@435: return (_slope <= 0.00); duke@435: }