1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/numberSeq.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,134 @@ 1.4 +/* 1.5 + * Copyright (c) 2001, 2012, 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 +#ifndef SHARE_VM_UTILITIES_NUMBERSEQ_HPP 1.29 +#define SHARE_VM_UTILITIES_NUMBERSEQ_HPP 1.30 + 1.31 +#include "memory/allocation.hpp" 1.32 + 1.33 +/** 1.34 + ** This file contains a few classes that represent number sequence, 1.35 + ** x1, x2, x3, ..., xN, and can calculate their avg, max, and sd. 1.36 + ** 1.37 + ** Here's a quick description of the classes: 1.38 + ** 1.39 + ** AbsSeq: abstract superclass 1.40 + ** NumberSeq: the sequence is assumed to be very long and the 1.41 + ** maximum, avg, sd, davg, and dsd are calculated over all its elements 1.42 + ** TruncatedSeq: this class keeps track of the last L elements 1.43 + ** of the sequence and calculates avg, max, and sd only over them 1.44 + **/ 1.45 + 1.46 +#define DEFAULT_ALPHA_VALUE 0.7 1.47 + 1.48 +class AbsSeq: public CHeapObj<mtInternal> { 1.49 +private: 1.50 + void init(double alpha); 1.51 + 1.52 +protected: 1.53 + int _num; // the number of elements in the sequence 1.54 + double _sum; // the sum of the elements in the sequence 1.55 + double _sum_of_squares; // the sum of squares of the elements in the sequence 1.56 + 1.57 + double _davg; // decaying average 1.58 + double _dvariance; // decaying variance 1.59 + double _alpha; // factor for the decaying average / variance 1.60 + 1.61 + // This is what we divide with to get the average. In a standard 1.62 + // number sequence, this should just be the number of elements in it. 1.63 + virtual double total() const { return (double) _num; }; 1.64 + 1.65 +public: 1.66 + AbsSeq(double alpha = DEFAULT_ALPHA_VALUE); 1.67 + 1.68 + virtual void add(double val); // adds a new element to the sequence 1.69 + void add(unsigned val) { add((double) val); } 1.70 + virtual double maximum() const = 0; // maximum element in the sequence 1.71 + virtual double last() const = 0; // last element added in the sequence 1.72 + 1.73 + // the number of elements in the sequence 1.74 + int num() const { return _num; } 1.75 + // the sum of the elements in the sequence 1.76 + double sum() const { return _sum; } 1.77 + 1.78 + double avg() const; // the average of the sequence 1.79 + double variance() const; // the variance of the sequence 1.80 + double sd() const; // the standard deviation of the sequence 1.81 + 1.82 + double davg() const; // decaying average 1.83 + double dvariance() const; // decaying variance 1.84 + double dsd() const; // decaying "standard deviation" 1.85 + 1.86 + // Debugging/Printing 1.87 + virtual void dump(); 1.88 + virtual void dump_on(outputStream* s); 1.89 +}; 1.90 + 1.91 +class NumberSeq: public AbsSeq { 1.92 +private: 1.93 + bool check_nums(NumberSeq* total, int n, NumberSeq** parts); 1.94 + 1.95 +protected: 1.96 + double _last; 1.97 + double _maximum; // keep track of maximum value 1.98 + 1.99 +public: 1.100 + NumberSeq(double alpha = DEFAULT_ALPHA_VALUE); 1.101 + 1.102 + virtual void add(double val); 1.103 + virtual double maximum() const { return _maximum; } 1.104 + virtual double last() const { return _last; } 1.105 + 1.106 + // Debugging/Printing 1.107 + virtual void dump_on(outputStream* s); 1.108 +}; 1.109 + 1.110 +class TruncatedSeq: public AbsSeq { 1.111 +private: 1.112 + enum PrivateConstants { 1.113 + DefaultSeqLength = 10 1.114 + }; 1.115 + void init(); 1.116 +protected: 1.117 + double *_sequence; // buffers the last L elements in the sequence 1.118 + int _length; // this is L 1.119 + int _next; // oldest slot in the array, i.e. next to be overwritten 1.120 + 1.121 +public: 1.122 + // accepts a value for L 1.123 + TruncatedSeq(int length = DefaultSeqLength, 1.124 + double alpha = DEFAULT_ALPHA_VALUE); 1.125 + ~TruncatedSeq(); 1.126 + virtual void add(double val); 1.127 + virtual double maximum() const; 1.128 + virtual double last() const; // the last value added to the sequence 1.129 + 1.130 + double oldest() const; // the oldest valid value in the sequence 1.131 + double predict_next() const; // prediction based on linear regression 1.132 + 1.133 + // Debugging/Printing 1.134 + virtual void dump_on(outputStream* s); 1.135 +}; 1.136 + 1.137 +#endif // SHARE_VM_UTILITIES_NUMBERSEQ_HPP