src/share/vm/utilities/numberSeq.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/numberSeq.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,262 @@
     1.4 +/*
     1.5 + * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "memory/allocation.inline.hpp"
    1.30 +#include "utilities/debug.hpp"
    1.31 +#include "utilities/globalDefinitions.hpp"
    1.32 +#include "utilities/numberSeq.hpp"
    1.33 +
    1.34 +AbsSeq::AbsSeq(double alpha) :
    1.35 +  _num(0), _sum(0.0), _sum_of_squares(0.0),
    1.36 +  _davg(0.0), _dvariance(0.0), _alpha(alpha) {
    1.37 +}
    1.38 +
    1.39 +void AbsSeq::add(double val) {
    1.40 +  if (_num == 0) {
    1.41 +    // if the sequence is empty, the davg is the same as the value
    1.42 +    _davg = val;
    1.43 +    // and the variance is 0
    1.44 +    _dvariance = 0.0;
    1.45 +  } else {
    1.46 +    // otherwise, calculate both
    1.47 +    _davg = (1.0 - _alpha) * val + _alpha * _davg;
    1.48 +    double diff = val - _davg;
    1.49 +    _dvariance = (1.0 - _alpha) * diff * diff + _alpha * _dvariance;
    1.50 +  }
    1.51 +}
    1.52 +
    1.53 +double AbsSeq::avg() const {
    1.54 +  if (_num == 0)
    1.55 +    return 0.0;
    1.56 +  else
    1.57 +    return _sum / total();
    1.58 +}
    1.59 +
    1.60 +double AbsSeq::variance() const {
    1.61 +  if (_num <= 1)
    1.62 +    return 0.0;
    1.63 +
    1.64 +  double x_bar = avg();
    1.65 +  double result = _sum_of_squares / total() - x_bar * x_bar;
    1.66 +  if (result < 0.0) {
    1.67 +    // due to loss-of-precision errors, the variance might be negative
    1.68 +    // by a small bit
    1.69 +
    1.70 +    //    guarantee(-0.1 < result && result < 0.0,
    1.71 +    //        "if variance is negative, it should be very small");
    1.72 +    result = 0.0;
    1.73 +  }
    1.74 +  return result;
    1.75 +}
    1.76 +
    1.77 +double AbsSeq::sd() const {
    1.78 +  double var = variance();
    1.79 +  guarantee( var >= 0.0, "variance should not be negative" );
    1.80 +  return sqrt(var);
    1.81 +}
    1.82 +
    1.83 +double AbsSeq::davg() const {
    1.84 +  return _davg;
    1.85 +}
    1.86 +
    1.87 +double AbsSeq::dvariance() const {
    1.88 +  if (_num <= 1)
    1.89 +    return 0.0;
    1.90 +
    1.91 +  double result = _dvariance;
    1.92 +  if (result < 0.0) {
    1.93 +    // due to loss-of-precision errors, the variance might be negative
    1.94 +    // by a small bit
    1.95 +
    1.96 +    guarantee(-0.1 < result && result < 0.0,
    1.97 +               "if variance is negative, it should be very small");
    1.98 +    result = 0.0;
    1.99 +  }
   1.100 +  return result;
   1.101 +}
   1.102 +
   1.103 +double AbsSeq::dsd() const {
   1.104 +  double var = dvariance();
   1.105 +  guarantee( var >= 0.0, "variance should not be negative" );
   1.106 +  return sqrt(var);
   1.107 +}
   1.108 +
   1.109 +NumberSeq::NumberSeq(double alpha) :
   1.110 +  AbsSeq(alpha), _maximum(0.0), _last(0.0) {
   1.111 +}
   1.112 +
   1.113 +bool NumberSeq::check_nums(NumberSeq *total, int n, NumberSeq **parts) {
   1.114 +  for (int i = 0; i < n; ++i) {
   1.115 +    if (parts[i] != NULL && total->num() != parts[i]->num())
   1.116 +      return false;
   1.117 +  }
   1.118 +  return true;
   1.119 +}
   1.120 +
   1.121 +void NumberSeq::add(double val) {
   1.122 +  AbsSeq::add(val);
   1.123 +
   1.124 +  _last = val;
   1.125 +  if (_num == 0) {
   1.126 +    _maximum = val;
   1.127 +  } else {
   1.128 +    if (val > _maximum)
   1.129 +      _maximum = val;
   1.130 +  }
   1.131 +  _sum += val;
   1.132 +  _sum_of_squares += val * val;
   1.133 +  ++_num;
   1.134 +}
   1.135 +
   1.136 +
   1.137 +TruncatedSeq::TruncatedSeq(int length, double alpha):
   1.138 +  AbsSeq(alpha), _length(length), _next(0) {
   1.139 +  _sequence = NEW_C_HEAP_ARRAY(double, _length, mtInternal);
   1.140 +  for (int i = 0; i < _length; ++i)
   1.141 +    _sequence[i] = 0.0;
   1.142 +}
   1.143 +
   1.144 +TruncatedSeq::~TruncatedSeq() {
   1.145 +  FREE_C_HEAP_ARRAY(double, _sequence, mtGC);
   1.146 +}
   1.147 +
   1.148 +void TruncatedSeq::add(double val) {
   1.149 +  AbsSeq::add(val);
   1.150 +
   1.151 +  // get the oldest value in the sequence...
   1.152 +  double old_val = _sequence[_next];
   1.153 +  // ...remove it from the sum and sum of squares
   1.154 +  _sum -= old_val;
   1.155 +  _sum_of_squares -= old_val * old_val;
   1.156 +
   1.157 +  // ...and update them with the new value
   1.158 +  _sum += val;
   1.159 +  _sum_of_squares += val * val;
   1.160 +
   1.161 +  // now replace the old value with the new one
   1.162 +  _sequence[_next] = val;
   1.163 +  _next = (_next + 1) % _length;
   1.164 +
   1.165 +  // only increase it if the buffer is not full
   1.166 +  if (_num < _length)
   1.167 +    ++_num;
   1.168 +
   1.169 +  guarantee( variance() > -1.0, "variance should be >= 0" );
   1.170 +}
   1.171 +
   1.172 +// can't easily keep track of this incrementally...
   1.173 +double TruncatedSeq::maximum() const {
   1.174 +  if (_num == 0)
   1.175 +    return 0.0;
   1.176 +  double ret = _sequence[0];
   1.177 +  for (int i = 1; i < _num; ++i) {
   1.178 +    double val = _sequence[i];
   1.179 +    if (val > ret)
   1.180 +      ret = val;
   1.181 +  }
   1.182 +  return ret;
   1.183 +}
   1.184 +
   1.185 +double TruncatedSeq::last() const {
   1.186 +  if (_num == 0)
   1.187 +    return 0.0;
   1.188 +  unsigned last_index = (_next + _length - 1) % _length;
   1.189 +  return _sequence[last_index];
   1.190 +}
   1.191 +
   1.192 +double TruncatedSeq::oldest() const {
   1.193 +  if (_num == 0)
   1.194 +    return 0.0;
   1.195 +  else if (_num < _length)
   1.196 +    // index 0 always oldest value until the array is full
   1.197 +    return _sequence[0];
   1.198 +  else {
   1.199 +    // since the array is full, _next is over the oldest value
   1.200 +    return _sequence[_next];
   1.201 +  }
   1.202 +}
   1.203 +
   1.204 +double TruncatedSeq::predict_next() const {
   1.205 +  if (_num == 0)
   1.206 +    return 0.0;
   1.207 +
   1.208 +  double num           = (double) _num;
   1.209 +  double x_squared_sum = 0.0;
   1.210 +  double x_sum         = 0.0;
   1.211 +  double y_sum         = 0.0;
   1.212 +  double xy_sum        = 0.0;
   1.213 +  double x_avg         = 0.0;
   1.214 +  double y_avg         = 0.0;
   1.215 +
   1.216 +  int first = (_next + _length - _num) % _length;
   1.217 +  for (int i = 0; i < _num; ++i) {
   1.218 +    double x = (double) i;
   1.219 +    double y =  _sequence[(first + i) % _length];
   1.220 +
   1.221 +    x_squared_sum += x * x;
   1.222 +    x_sum         += x;
   1.223 +    y_sum         += y;
   1.224 +    xy_sum        += x * y;
   1.225 +  }
   1.226 +  x_avg = x_sum / num;
   1.227 +  y_avg = y_sum / num;
   1.228 +
   1.229 +  double Sxx = x_squared_sum - x_sum * x_sum / num;
   1.230 +  double Sxy = xy_sum - x_sum * y_sum / num;
   1.231 +  double b1 = Sxy / Sxx;
   1.232 +  double b0 = y_avg - b1 * x_avg;
   1.233 +
   1.234 +  return b0 + b1 * num;
   1.235 +}
   1.236 +
   1.237 +
   1.238 +// Printing/Debugging Support
   1.239 +
   1.240 +void AbsSeq::dump() { dump_on(gclog_or_tty); }
   1.241 +
   1.242 +void AbsSeq::dump_on(outputStream* s) {
   1.243 +  s->print_cr("\t _num = %d, _sum = %7.3f, _sum_of_squares = %7.3f",
   1.244 +                  _num,      _sum,         _sum_of_squares);
   1.245 +  s->print_cr("\t _davg = %7.3f, _dvariance = %7.3f, _alpha = %7.3f",
   1.246 +                  _davg,         _dvariance,         _alpha);
   1.247 +}
   1.248 +
   1.249 +void NumberSeq::dump_on(outputStream* s) {
   1.250 +  AbsSeq::dump_on(s);
   1.251 +  s->print_cr("\t\t _last = %7.3f, _maximum = %7.3f", _last, _maximum);
   1.252 +}
   1.253 +
   1.254 +void TruncatedSeq::dump_on(outputStream* s) {
   1.255 +  AbsSeq::dump_on(s);
   1.256 +  s->print_cr("\t\t _length = %d, _next = %d", _length, _next);
   1.257 +  for (int i = 0; i < _length; i++) {
   1.258 +    if (i%5 == 0) {
   1.259 +      s->cr();
   1.260 +      s->print("\t");
   1.261 +    }
   1.262 +    s->print("\t[%d]=%7.3f", i, _sequence[i]);
   1.263 +  }
   1.264 +  s->cr();
   1.265 +}

mercurial