001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.metrics2.util;
020
021import org.apache.hadoop.classification.InterfaceAudience;
022
023/**
024 * Helper to compute running sample stats
025 */
026@InterfaceAudience.Private
027public class SampleStat {
028  private final MinMax minmax = new MinMax();
029  private long numSamples = 0;
030  private double a0, a1, s0, s1, total;
031
032  /**
033   * Construct a new running sample stat
034   */
035  public SampleStat() {
036    a0 = s0 = 0.0;
037    total = 0.0;
038  }
039
040  public void reset() {
041    numSamples = 0;
042    a0 = s0 = 0.0;
043    total = 0.0;
044    minmax.reset();
045  }
046
047  // We want to reuse the object, sometimes.
048  void reset(long numSamples, double a0, double a1, double s0, double s1,
049      double total, MinMax minmax) {
050    this.numSamples = numSamples;
051    this.a0 = a0;
052    this.a1 = a1;
053    this.s0 = s0;
054    this.s1 = s1;
055    this.total = total;
056    this.minmax.reset(minmax);
057  }
058
059  /**
060   * Copy the values to other (saves object creation and gc.)
061   * @param other the destination to hold our values
062   */
063  public void copyTo(SampleStat other) {
064    other.reset(numSamples, a0, a1, s0, s1, total, minmax);
065  }
066
067  /**
068   * Add a sample the running stat.
069   * @param x the sample number
070   * @return  self
071   */
072  public SampleStat add(double x) {
073    minmax.add(x);
074    return add(1, x);
075  }
076
077  /**
078   * Add some sample and a partial sum to the running stat.
079   * Note, min/max is not evaluated using this method.
080   * @param nSamples  number of samples
081   * @param x the partial sum
082   * @return  self
083   */
084  public SampleStat add(long nSamples, double x) {
085    numSamples += nSamples;
086    total += x;
087
088    if (numSamples == 1) {
089      a0 = a1 = x;
090      s0 = 0.0;
091    }
092    else {
093      // The Welford method for numerical stability
094      a1 = a0 + (x - a0) / numSamples;
095      s1 = s0 + (x - a0) * (x - a1);
096      a0 = a1;
097      s0 = s1;
098    }
099    return this;
100  }
101
102  /**
103   * @return  the total number of samples
104   */
105  public long numSamples() {
106    return numSamples;
107  }
108
109  /**
110   * @return the total of all samples added
111   */
112  public double total() {
113    return total;
114  }
115
116  /**
117   * @return  the arithmetic mean of the samples
118   */
119  public double mean() {
120    return numSamples > 0 ? (total / numSamples) : 0.0;
121  }
122
123  /**
124   * @return  the variance of the samples
125   */
126  public double variance() {
127    return numSamples > 1 ? s1 / (numSamples - 1) : 0.0;
128  }
129
130  /**
131   * @return  the standard deviation of the samples
132   */
133  public double stddev() {
134    return Math.sqrt(variance());
135  }
136
137  /**
138   * @return  the minimum value of the samples
139   */
140  public double min() {
141    return minmax.min();
142  }
143
144  /**
145   * @return  the maximum value of the samples
146   */
147  public double max() {
148    return minmax.max();
149  }
150
151  @Override
152  public String toString() {
153    try {
154      return "Samples = " + numSamples() +
155          "  Min = " + min() +
156          "  Mean = " + mean() +
157          "  Std Dev = " + stddev() +
158          "  Max = " + max();
159    } catch (Throwable t) {
160      return super.toString();
161    }
162  }
163
164  /**
165   * Helper to keep running min/max
166   */
167  @SuppressWarnings("PublicInnerClass")
168  public static class MinMax {
169
170    // Float.MAX_VALUE is used rather than Double.MAX_VALUE, even though the
171    // min and max variables are of type double.
172    // Float.MAX_VALUE is big enough, and using Double.MAX_VALUE makes 
173    // Ganglia core due to buffer overflow.
174    // The same reasoning applies to the MIN_VALUE counterparts.
175    static final double DEFAULT_MIN_VALUE = Float.MAX_VALUE;
176    static final double DEFAULT_MAX_VALUE = Float.MIN_VALUE;
177
178    private double min = DEFAULT_MIN_VALUE;
179    private double max = DEFAULT_MAX_VALUE;
180
181    public void add(double value) {
182      if (value > max) max = value;
183      if (value < min) min = value;
184    }
185
186    public double min() { return min; }
187    public double max() { return max; }
188
189    public void reset() {
190      min = DEFAULT_MIN_VALUE;
191      max = DEFAULT_MAX_VALUE;
192    }
193
194    public void reset(MinMax other) {
195      min = other.min();
196      max = other.max();
197    }
198  }
199}