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.lib; 020 021import java.util.concurrent.atomic.AtomicLong; 022 023import org.apache.commons.lang.StringUtils; 024import org.apache.hadoop.hbase.classification.InterfaceAudience; 025import org.apache.hadoop.metrics2.MetricHistogram; 026import org.apache.hadoop.metrics2.MetricsInfo; 027import org.apache.hadoop.metrics2.MetricsRecordBuilder; 028 029import com.yammer.metrics.stats.ExponentiallyDecayingSample; 030import com.yammer.metrics.stats.Sample; 031import com.yammer.metrics.stats.Snapshot; 032 033/** 034 * A histogram implementation that runs in constant space, and exports to hadoop2's metrics2 system. 035 */ 036@InterfaceAudience.Private 037public class MutableHistogram extends MutableMetric implements MetricHistogram { 038 039 private static final int DEFAULT_SAMPLE_SIZE = 2046; 040 // the bias towards sampling from more recent data. 041 // Per Cormode et al. an alpha of 0.015 strongly biases to the last 5 minutes 042 private static final double DEFAULT_ALPHA = 0.015; 043 044 private final String name; 045 private final String desc; 046 private final Sample sample; 047 private final AtomicLong min; 048 private final AtomicLong max; 049 private final AtomicLong sum; 050 private final AtomicLong count; 051 052 public MutableHistogram(MetricsInfo info) { 053 this(info.name(), info.description()); 054 } 055 056 public MutableHistogram(String name, String description) { 057 this.name = StringUtils.capitalize(name); 058 this.desc = StringUtils.uncapitalize(description); 059 sample = new ExponentiallyDecayingSample(DEFAULT_SAMPLE_SIZE, DEFAULT_ALPHA); 060 count = new AtomicLong(); 061 min = new AtomicLong(Long.MAX_VALUE); 062 max = new AtomicLong(Long.MIN_VALUE); 063 sum = new AtomicLong(); 064 } 065 066 public void add(final long val) { 067 setChanged(); 068 count.incrementAndGet(); 069 sample.update(val); 070 setMax(val); 071 setMin(val); 072 sum.getAndAdd(val); 073 } 074 075 private void setMax(final long potentialMax) { 076 boolean done = false; 077 while (!done) { 078 final long currentMax = max.get(); 079 done = currentMax >= potentialMax 080 || max.compareAndSet(currentMax, potentialMax); 081 } 082 } 083 084 private void setMin(long potentialMin) { 085 boolean done = false; 086 while (!done) { 087 final long currentMin = min.get(); 088 done = currentMin <= potentialMin 089 || min.compareAndSet(currentMin, potentialMin); 090 } 091 } 092 093 public long getMax() { 094 if (count.get() > 0) { 095 return max.get(); 096 } 097 return 0L; 098 } 099 100 public long getMin() { 101 if (count.get() > 0) { 102 return min.get(); 103 } 104 return 0L; 105 } 106 107 public double getMean() { 108 long cCount = count.get(); 109 if (cCount > 0) { 110 return sum.get() / (double) cCount; 111 } 112 return 0.0; 113 } 114 115 @Override 116 public void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) { 117 if (all || changed()) { 118 clearChanged(); 119 final Snapshot s = sample.getSnapshot(); 120 metricsRecordBuilder.addCounter(Interns.info(name + NUM_OPS_METRIC_NAME, desc), count.get()); 121 122 metricsRecordBuilder.addGauge(Interns.info(name + MIN_METRIC_NAME, desc), getMin()); 123 metricsRecordBuilder.addGauge(Interns.info(name + MAX_METRIC_NAME, desc), getMax()); 124 metricsRecordBuilder.addGauge(Interns.info(name + MEAN_METRIC_NAME, desc), getMean()); 125 126 metricsRecordBuilder.addGauge(Interns.info(name + MEDIAN_METRIC_NAME, desc), s.getMedian()); 127 metricsRecordBuilder.addGauge(Interns.info(name + SEVENTY_FIFTH_PERCENTILE_METRIC_NAME, desc), 128 s.get75thPercentile()); 129 metricsRecordBuilder.addGauge(Interns.info(name + NINETY_FIFTH_PERCENTILE_METRIC_NAME, desc), 130 s.get95thPercentile()); 131 metricsRecordBuilder.addGauge(Interns.info(name + NINETY_NINETH_PERCENTILE_METRIC_NAME, desc), 132 s.get99thPercentile()); 133 } 134 } 135}