src/share/vm/utilities/numberSeq.cpp

Tue, 24 Jul 2018 13:22:11 +0800

author
aoqi
date
Tue, 24 Jul 2018 13:22:11 +0800
changeset 9137
dc1769738300
parent 6876
710a3c8b516e
permissions
-rw-r--r--

#7048 added Loongson release info to hs_err crash files

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "memory/allocation.inline.hpp"
aoqi@0 27 #include "utilities/debug.hpp"
aoqi@0 28 #include "utilities/globalDefinitions.hpp"
aoqi@0 29 #include "utilities/numberSeq.hpp"
aoqi@0 30
aoqi@0 31 AbsSeq::AbsSeq(double alpha) :
aoqi@0 32 _num(0), _sum(0.0), _sum_of_squares(0.0),
aoqi@0 33 _davg(0.0), _dvariance(0.0), _alpha(alpha) {
aoqi@0 34 }
aoqi@0 35
aoqi@0 36 void AbsSeq::add(double val) {
aoqi@0 37 if (_num == 0) {
aoqi@0 38 // if the sequence is empty, the davg is the same as the value
aoqi@0 39 _davg = val;
aoqi@0 40 // and the variance is 0
aoqi@0 41 _dvariance = 0.0;
aoqi@0 42 } else {
aoqi@0 43 // otherwise, calculate both
aoqi@0 44 _davg = (1.0 - _alpha) * val + _alpha * _davg;
aoqi@0 45 double diff = val - _davg;
aoqi@0 46 _dvariance = (1.0 - _alpha) * diff * diff + _alpha * _dvariance;
aoqi@0 47 }
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 double AbsSeq::avg() const {
aoqi@0 51 if (_num == 0)
aoqi@0 52 return 0.0;
aoqi@0 53 else
aoqi@0 54 return _sum / total();
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 double AbsSeq::variance() const {
aoqi@0 58 if (_num <= 1)
aoqi@0 59 return 0.0;
aoqi@0 60
aoqi@0 61 double x_bar = avg();
aoqi@0 62 double result = _sum_of_squares / total() - x_bar * x_bar;
aoqi@0 63 if (result < 0.0) {
aoqi@0 64 // due to loss-of-precision errors, the variance might be negative
aoqi@0 65 // by a small bit
aoqi@0 66
aoqi@0 67 // guarantee(-0.1 < result && result < 0.0,
aoqi@0 68 // "if variance is negative, it should be very small");
aoqi@0 69 result = 0.0;
aoqi@0 70 }
aoqi@0 71 return result;
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 double AbsSeq::sd() const {
aoqi@0 75 double var = variance();
aoqi@0 76 guarantee( var >= 0.0, "variance should not be negative" );
aoqi@0 77 return sqrt(var);
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 double AbsSeq::davg() const {
aoqi@0 81 return _davg;
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 double AbsSeq::dvariance() const {
aoqi@0 85 if (_num <= 1)
aoqi@0 86 return 0.0;
aoqi@0 87
aoqi@0 88 double result = _dvariance;
aoqi@0 89 if (result < 0.0) {
aoqi@0 90 // due to loss-of-precision errors, the variance might be negative
aoqi@0 91 // by a small bit
aoqi@0 92
aoqi@0 93 guarantee(-0.1 < result && result < 0.0,
aoqi@0 94 "if variance is negative, it should be very small");
aoqi@0 95 result = 0.0;
aoqi@0 96 }
aoqi@0 97 return result;
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 double AbsSeq::dsd() const {
aoqi@0 101 double var = dvariance();
aoqi@0 102 guarantee( var >= 0.0, "variance should not be negative" );
aoqi@0 103 return sqrt(var);
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 NumberSeq::NumberSeq(double alpha) :
aoqi@0 107 AbsSeq(alpha), _maximum(0.0), _last(0.0) {
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 bool NumberSeq::check_nums(NumberSeq *total, int n, NumberSeq **parts) {
aoqi@0 111 for (int i = 0; i < n; ++i) {
aoqi@0 112 if (parts[i] != NULL && total->num() != parts[i]->num())
aoqi@0 113 return false;
aoqi@0 114 }
aoqi@0 115 return true;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 void NumberSeq::add(double val) {
aoqi@0 119 AbsSeq::add(val);
aoqi@0 120
aoqi@0 121 _last = val;
aoqi@0 122 if (_num == 0) {
aoqi@0 123 _maximum = val;
aoqi@0 124 } else {
aoqi@0 125 if (val > _maximum)
aoqi@0 126 _maximum = val;
aoqi@0 127 }
aoqi@0 128 _sum += val;
aoqi@0 129 _sum_of_squares += val * val;
aoqi@0 130 ++_num;
aoqi@0 131 }
aoqi@0 132
aoqi@0 133
aoqi@0 134 TruncatedSeq::TruncatedSeq(int length, double alpha):
aoqi@0 135 AbsSeq(alpha), _length(length), _next(0) {
aoqi@0 136 _sequence = NEW_C_HEAP_ARRAY(double, _length, mtInternal);
aoqi@0 137 for (int i = 0; i < _length; ++i)
aoqi@0 138 _sequence[i] = 0.0;
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 TruncatedSeq::~TruncatedSeq() {
aoqi@0 142 FREE_C_HEAP_ARRAY(double, _sequence, mtGC);
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 void TruncatedSeq::add(double val) {
aoqi@0 146 AbsSeq::add(val);
aoqi@0 147
aoqi@0 148 // get the oldest value in the sequence...
aoqi@0 149 double old_val = _sequence[_next];
aoqi@0 150 // ...remove it from the sum and sum of squares
aoqi@0 151 _sum -= old_val;
aoqi@0 152 _sum_of_squares -= old_val * old_val;
aoqi@0 153
aoqi@0 154 // ...and update them with the new value
aoqi@0 155 _sum += val;
aoqi@0 156 _sum_of_squares += val * val;
aoqi@0 157
aoqi@0 158 // now replace the old value with the new one
aoqi@0 159 _sequence[_next] = val;
aoqi@0 160 _next = (_next + 1) % _length;
aoqi@0 161
aoqi@0 162 // only increase it if the buffer is not full
aoqi@0 163 if (_num < _length)
aoqi@0 164 ++_num;
aoqi@0 165
aoqi@0 166 guarantee( variance() > -1.0, "variance should be >= 0" );
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 // can't easily keep track of this incrementally...
aoqi@0 170 double TruncatedSeq::maximum() const {
aoqi@0 171 if (_num == 0)
aoqi@0 172 return 0.0;
aoqi@0 173 double ret = _sequence[0];
aoqi@0 174 for (int i = 1; i < _num; ++i) {
aoqi@0 175 double val = _sequence[i];
aoqi@0 176 if (val > ret)
aoqi@0 177 ret = val;
aoqi@0 178 }
aoqi@0 179 return ret;
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 double TruncatedSeq::last() const {
aoqi@0 183 if (_num == 0)
aoqi@0 184 return 0.0;
aoqi@0 185 unsigned last_index = (_next + _length - 1) % _length;
aoqi@0 186 return _sequence[last_index];
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 double TruncatedSeq::oldest() const {
aoqi@0 190 if (_num == 0)
aoqi@0 191 return 0.0;
aoqi@0 192 else if (_num < _length)
aoqi@0 193 // index 0 always oldest value until the array is full
aoqi@0 194 return _sequence[0];
aoqi@0 195 else {
aoqi@0 196 // since the array is full, _next is over the oldest value
aoqi@0 197 return _sequence[_next];
aoqi@0 198 }
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 double TruncatedSeq::predict_next() const {
aoqi@0 202 if (_num == 0)
aoqi@0 203 return 0.0;
aoqi@0 204
aoqi@0 205 double num = (double) _num;
aoqi@0 206 double x_squared_sum = 0.0;
aoqi@0 207 double x_sum = 0.0;
aoqi@0 208 double y_sum = 0.0;
aoqi@0 209 double xy_sum = 0.0;
aoqi@0 210 double x_avg = 0.0;
aoqi@0 211 double y_avg = 0.0;
aoqi@0 212
aoqi@0 213 int first = (_next + _length - _num) % _length;
aoqi@0 214 for (int i = 0; i < _num; ++i) {
aoqi@0 215 double x = (double) i;
aoqi@0 216 double y = _sequence[(first + i) % _length];
aoqi@0 217
aoqi@0 218 x_squared_sum += x * x;
aoqi@0 219 x_sum += x;
aoqi@0 220 y_sum += y;
aoqi@0 221 xy_sum += x * y;
aoqi@0 222 }
aoqi@0 223 x_avg = x_sum / num;
aoqi@0 224 y_avg = y_sum / num;
aoqi@0 225
aoqi@0 226 double Sxx = x_squared_sum - x_sum * x_sum / num;
aoqi@0 227 double Sxy = xy_sum - x_sum * y_sum / num;
aoqi@0 228 double b1 = Sxy / Sxx;
aoqi@0 229 double b0 = y_avg - b1 * x_avg;
aoqi@0 230
aoqi@0 231 return b0 + b1 * num;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234
aoqi@0 235 // Printing/Debugging Support
aoqi@0 236
aoqi@0 237 void AbsSeq::dump() { dump_on(gclog_or_tty); }
aoqi@0 238
aoqi@0 239 void AbsSeq::dump_on(outputStream* s) {
aoqi@0 240 s->print_cr("\t _num = %d, _sum = %7.3f, _sum_of_squares = %7.3f",
aoqi@0 241 _num, _sum, _sum_of_squares);
aoqi@0 242 s->print_cr("\t _davg = %7.3f, _dvariance = %7.3f, _alpha = %7.3f",
aoqi@0 243 _davg, _dvariance, _alpha);
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 void NumberSeq::dump_on(outputStream* s) {
aoqi@0 247 AbsSeq::dump_on(s);
aoqi@0 248 s->print_cr("\t\t _last = %7.3f, _maximum = %7.3f", _last, _maximum);
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 void TruncatedSeq::dump_on(outputStream* s) {
aoqi@0 252 AbsSeq::dump_on(s);
aoqi@0 253 s->print_cr("\t\t _length = %d, _next = %d", _length, _next);
aoqi@0 254 for (int i = 0; i < _length; i++) {
aoqi@0 255 if (i%5 == 0) {
aoqi@0 256 s->cr();
aoqi@0 257 s->print("\t");
aoqi@0 258 }
aoqi@0 259 s->print("\t[%d]=%7.3f", i, _sequence[i]);
aoqi@0 260 }
aoqi@0 261 s->cr();
aoqi@0 262 }

mercurial