1 /*
   2  * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "memory/allocation.inline.hpp"
  27 #include "utilities/debug.hpp"
  28 #include "utilities/globalDefinitions.hpp"
  29 #include "utilities/numberSeq.hpp"
  30 
  31 AbsSeq::AbsSeq(double alpha) :
  32   _num(0), _sum(0.0), _sum_of_squares(0.0),
  33   _davg(0.0), _dvariance(0.0), _alpha(alpha) {
  34 }
  35 
  36 void AbsSeq::add(double val) {
  37   if (_num == 0) {
  38     // if the sequence is empty, the davg is the same as the value
  39     _davg = val;
  40     // and the variance is 0
  41     _dvariance = 0.0;
  42   } else {
  43     // otherwise, calculate both
  44     _davg = (1.0 - _alpha) * val + _alpha * _davg;
  45     double diff = val - _davg;
  46     _dvariance = (1.0 - _alpha) * diff * diff + _alpha * _dvariance;
  47   }
  48 }
  49 
  50 double AbsSeq::avg() const {
  51   if (_num == 0)
  52     return 0.0;
  53   else
  54     return _sum / total();
  55 }
  56 
  57 double AbsSeq::variance() const {
  58   if (_num <= 1)
  59     return 0.0;
  60 
  61   double x_bar = avg();
  62   double result = _sum_of_squares / total() - x_bar * x_bar;
  63   if (result < 0.0) {
  64     // due to loss-of-precision errors, the variance might be negative
  65     // by a small bit
  66 
  67     //    guarantee(-0.1 < result && result < 0.0,
  68     //        "if variance is negative, it should be very small");
  69     result = 0.0;
  70   }
  71   return result;
  72 }
  73 
  74 double AbsSeq::sd() const {
  75   double var = variance();
  76   guarantee( var >= 0.0, "variance should not be negative" );
  77   return sqrt(var);
  78 }
  79 
  80 double AbsSeq::davg() const {
  81   return _davg;
  82 }
  83 
  84 double AbsSeq::dvariance() const {
  85   if (_num <= 1)
  86     return 0.0;
  87 
  88   double result = _dvariance;
  89   if (result < 0.0) {
  90     // due to loss-of-precision errors, the variance might be negative
  91     // by a small bit
  92 
  93     guarantee(-0.1 < result && result < 0.0,
  94                "if variance is negative, it should be very small");
  95     result = 0.0;
  96   }
  97   return result;
  98 }
  99 
 100 double AbsSeq::dsd() const {
 101   double var = dvariance();
 102   guarantee( var >= 0.0, "variance should not be negative" );
 103   return sqrt(var);
 104 }
 105 
 106 NumberSeq::NumberSeq(double alpha) :
 107   AbsSeq(alpha), _maximum(0.0), _last(0.0) {
 108 }
 109 
 110 bool NumberSeq::check_nums(NumberSeq *total, int n, NumberSeq **parts) {
 111   for (int i = 0; i < n; ++i) {
 112     if (parts[i] != NULL && total->num() != parts[i]->num())
 113       return false;
 114   }
 115   return true;
 116 }
 117 
 118 void NumberSeq::add(double val) {
 119   AbsSeq::add(val);
 120 
 121   _last = val;
 122   if (_num == 0) {
 123     _maximum = val;
 124   } else {
 125     if (val > _maximum)
 126       _maximum = val;
 127   }
 128   _sum += val;
 129   _sum_of_squares += val * val;
 130   ++_num;
 131 }
 132 
 133 
 134 TruncatedSeq::TruncatedSeq(int length, double alpha):
 135   AbsSeq(alpha), _length(length), _next(0) {
 136   _sequence = NEW_C_HEAP_ARRAY(double, _length, mtInternal);
 137   for (int i = 0; i < _length; ++i)
 138     _sequence[i] = 0.0;
 139 }
 140 
 141 TruncatedSeq::~TruncatedSeq() {
 142   FREE_C_HEAP_ARRAY(double, _sequence, mtGC);
 143 }
 144 
 145 void TruncatedSeq::add(double val) {
 146   AbsSeq::add(val);
 147 
 148   // get the oldest value in the sequence...
 149   double old_val = _sequence[_next];
 150   // ...remove it from the sum and sum of squares
 151   _sum -= old_val;
 152   _sum_of_squares -= old_val * old_val;
 153 
 154   // ...and update them with the new value
 155   _sum += val;
 156   _sum_of_squares += val * val;
 157 
 158   // now replace the old value with the new one
 159   _sequence[_next] = val;
 160   _next = (_next + 1) % _length;
 161 
 162   // only increase it if the buffer is not full
 163   if (_num < _length)
 164     ++_num;
 165 
 166   guarantee( variance() > -1.0, "variance should be >= 0" );
 167 }
 168 
 169 // can't easily keep track of this incrementally...
 170 double TruncatedSeq::maximum() const {
 171   if (_num == 0)
 172     return 0.0;
 173   double ret = _sequence[0];
 174   for (int i = 1; i < _num; ++i) {
 175     double val = _sequence[i];
 176     if (val > ret)
 177       ret = val;
 178   }
 179   return ret;
 180 }
 181 
 182 double TruncatedSeq::last() const {
 183   if (_num == 0)
 184     return 0.0;
 185   unsigned last_index = (_next + _length - 1) % _length;
 186   return _sequence[last_index];
 187 }
 188 
 189 double TruncatedSeq::oldest() const {
 190   if (_num == 0)
 191     return 0.0;
 192   else if (_num < _length)
 193     // index 0 always oldest value until the array is full
 194     return _sequence[0];
 195   else {
 196     // since the array is full, _next is over the oldest value
 197     return _sequence[_next];
 198   }
 199 }
 200 
 201 double TruncatedSeq::predict_next() const {
 202   if (_num == 0)
 203     return 0.0;
 204 
 205   double num           = (double) _num;
 206   double x_squared_sum = 0.0;
 207   double x_sum         = 0.0;
 208   double y_sum         = 0.0;
 209   double xy_sum        = 0.0;
 210   double x_avg         = 0.0;
 211   double y_avg         = 0.0;
 212 
 213   int first = (_next + _length - _num) % _length;
 214   for (int i = 0; i < _num; ++i) {
 215     double x = (double) i;
 216     double y =  _sequence[(first + i) % _length];
 217 
 218     x_squared_sum += x * x;
 219     x_sum         += x;
 220     y_sum         += y;
 221     xy_sum        += x * y;
 222   }
 223   x_avg = x_sum / num;
 224   y_avg = y_sum / num;
 225 
 226   double Sxx = x_squared_sum - x_sum * x_sum / num;
 227   double Sxy = xy_sum - x_sum * y_sum / num;
 228   double b1 = Sxy / Sxx;
 229   double b0 = y_avg - b1 * x_avg;
 230 
 231   return b0 + b1 * num;
 232 }
 233 
 234 
 235 // Printing/Debugging Support
 236 
 237 void AbsSeq::dump() { dump_on(gclog_or_tty); }
 238 
 239 void AbsSeq::dump_on(outputStream* s) {
 240   s->print_cr("\t _num = %d, _sum = %7.3f, _sum_of_squares = %7.3f",
 241                   _num,      _sum,         _sum_of_squares);
 242   s->print_cr("\t _davg = %7.3f, _dvariance = %7.3f, _alpha = %7.3f",
 243                   _davg,         _dvariance,         _alpha);
 244 }
 245 
 246 void NumberSeq::dump_on(outputStream* s) {
 247   AbsSeq::dump_on(s);
 248   s->print_cr("\t\t _last = %7.3f, _maximum = %7.3f", _last, _maximum);
 249 }
 250 
 251 void TruncatedSeq::dump_on(outputStream* s) {
 252   AbsSeq::dump_on(s);
 253   s->print_cr("\t\t _length = %d, _next = %d", _length, _next);
 254   for (int i = 0; i < _length; i++) {
 255     if (i%5 == 0) {
 256       s->cr();
 257       s->print("\t");
 258     }
 259     s->print("\t[%d]=%7.3f", i, _sequence[i]);
 260   }
 261   s->cr();
 262 }
 263 
 264 HdrSeq::HdrSeq() {
 265   _hdr = NEW_C_HEAP_ARRAY(int*, MagBuckets, mtInternal);
 266   for (int c = 0; c < MagBuckets; c++) {
 267     _hdr[c] = NULL;
 268   }
 269 }
 270 
 271 HdrSeq::~HdrSeq() {
 272   for (int c = 0; c < MagBuckets; c++) {
 273     int* sub = _hdr[c];
 274     if (sub != NULL) {
 275       FREE_C_HEAP_ARRAY(int, sub, mtInternal);
 276     }
 277   }
 278   FREE_C_HEAP_ARRAY(int*, _hdr, mtInternal);
 279 }
 280 
 281 void HdrSeq::add(double val) {
 282   if (val < 0) {
 283     assert (false, err_msg("value (%8.2f) is not negative", val));
 284     val = 0;
 285   }
 286 
 287   NumberSeq::add(val);
 288 
 289   double v = val;
 290   int mag;
 291   if (v > 0) {
 292     mag = 0;
 293     while (v > 1) {
 294       mag++;
 295       v /= 10;
 296     }
 297     while (v < 0.1) {
 298       mag--;
 299       v *= 10;
 300     }
 301   } else {
 302     mag = MagMinimum;
 303   }
 304 
 305   int bucket = -MagMinimum + mag;
 306   int sub_bucket = (int) (v * ValBuckets);
 307 
 308   // Defensively saturate for product bits:
 309   if (bucket < 0) {
 310     assert (false, err_msg("bucket index (%d) underflow for value (%8.2f)", bucket, val));
 311     bucket = 0;
 312   }
 313 
 314   if (bucket >= MagBuckets) {
 315     assert (false, err_msg("bucket index (%d) overflow for value (%8.2f)", bucket, val));
 316     bucket = MagBuckets - 1;
 317   }
 318 
 319   if (sub_bucket < 0) {
 320     assert (false, err_msg("sub-bucket index (%d) underflow for value (%8.2f)", sub_bucket, val));
 321     sub_bucket = 0;
 322   }
 323 
 324   if (sub_bucket >= ValBuckets) {
 325     assert (false, err_msg("sub-bucket index (%d) overflow for value (%8.2f)", sub_bucket, val));
 326     sub_bucket = ValBuckets - 1;
 327   }
 328 
 329   int* b = _hdr[bucket];
 330   if (b == NULL) {
 331     b = NEW_C_HEAP_ARRAY(int, ValBuckets, mtInternal);
 332     for (int c = 0; c < ValBuckets; c++) {
 333       b[c] = 0;
 334     }
 335     _hdr[bucket] = b;
 336   }
 337   b[sub_bucket]++;
 338 }
 339 
 340 double HdrSeq::percentile(double level) const {
 341   // target should be non-zero to find the first sample
 342   int target = MAX2(1, (int) (level * num() / 100));
 343   int cnt = 0;
 344   for (int mag = 0; mag < MagBuckets; mag++) {
 345     if (_hdr[mag] != NULL) {
 346       for (int val = 0; val < ValBuckets; val++) {
 347         cnt += _hdr[mag][val];
 348         if (cnt >= target) {
 349           return pow(10.0, MagMinimum + mag) * val / ValBuckets;
 350         }
 351       }
 352     }
 353   }
 354   return maximum();
 355 }
 356 
 357 BinaryMagnitudeSeq::BinaryMagnitudeSeq() {
 358   _mags = NEW_C_HEAP_ARRAY(jlong, BitsPerJavaLong, mtInternal);
 359   for (int c = 0; c < BitsPerJavaLong; c++) {
 360     _mags[c] = 0;
 361   }
 362   _sum = 0;
 363 }
 364 
 365 BinaryMagnitudeSeq::~BinaryMagnitudeSeq() {
 366   FREE_C_HEAP_ARRAY(size_t, _mags, mtInternal);
 367 }
 368 
 369 void BinaryMagnitudeSeq::add(size_t val) {
 370   Atomic::add(val, &_sum);
 371 
 372   int mag = log2_intptr(val) + 1;
 373 
 374   // Defensively saturate for product bits:
 375   if (mag < 0) {
 376     assert (false, err_msg("bucket index (%d) underflow for value (" SIZE_FORMAT ")", mag, val));
 377     mag = 0;
 378   }
 379 
 380   if (mag >= BitsPerJavaLong) {
 381     assert (false, err_msg("bucket index (%d) overflow for value (" SIZE_FORMAT ")", mag, val));
 382     mag = BitsPerJavaLong - 1;
 383   }
 384 
 385   Atomic::add(1, &_mags[mag]);
 386 }
 387 
 388 size_t BinaryMagnitudeSeq::level(int level) const {
 389   if (0 <= level && level < BitsPerJavaLong) {
 390     return _mags[level];
 391   } else {
 392     return 0;
 393   }
 394 }
 395 
 396 size_t BinaryMagnitudeSeq::num() const {
 397   int r = 0;
 398   for (int c = 0; c < BitsPerJavaLong; c++) {
 399     r += _mags[c];
 400   }
 401   return r;
 402 }
 403 
 404 size_t BinaryMagnitudeSeq::sum() const {
 405   return _sum;
 406 }
 407 
 408 int BinaryMagnitudeSeq::min_level() const {
 409   for (int c = 0; c < BitsPerJavaLong; c++) {
 410     if (_mags[c] != 0) {
 411       return c;
 412     }
 413   }
 414   return BitsPerJavaLong - 1;
 415 }
 416 
 417 int BinaryMagnitudeSeq::max_level() const {
 418   for (int c = BitsPerJavaLong - 1; c > 0; c--) {
 419     if (_mags[c] != 0) {
 420       return c;
 421     }
 422   }
 423   return 0;
 424 }
 425