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 */
018package org.apache.hadoop.hbase.io.hfile.bucket;
019
020import java.util.concurrent.TimeUnit;
021import java.util.concurrent.atomic.LongAdder;
022import org.apache.hadoop.hbase.io.hfile.CacheStats;
023import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Class that implements cache metrics for bucket cache.
028 */
029@InterfaceAudience.Private
030public class BucketCacheStats extends CacheStats {
031  private final LongAdder ioHitCount = new LongAdder();
032  private final LongAdder ioHitTime = new LongAdder();
033  private static final long NANO_TIME = TimeUnit.MILLISECONDS.toNanos(1);
034  private long lastLogTime = EnvironmentEdgeManager.currentTime();
035
036  /* Tracing failed Bucket Cache allocations. */
037  private LongAdder allocationFailCount = new LongAdder();
038
039  BucketCacheStats(int numPeriodsInWindow, int periodTimeInMinutes) {
040    super("BucketCache", numPeriodsInWindow, periodTimeInMinutes);
041    allocationFailCount.reset();
042  }
043
044  @Override
045  public String toString() {
046    return super.toString() + ", ioHitsPerSecond=" + getIOHitsPerSecond() + ", ioTimePerHit="
047      + getIOTimePerHit() + ", allocationFailCount=" + getAllocationFailCount();
048  }
049
050  public void ioHit(long time) {
051    ioHitCount.increment();
052    ioHitTime.add(time);
053  }
054
055  public long getIOHitsPerSecond() {
056    long now = EnvironmentEdgeManager.currentTime();
057    long took = (now - lastLogTime) / 1000;
058    lastLogTime = now;
059    return took == 0 ? 0 : ioHitCount.sum() / took;
060  }
061
062  public double getIOTimePerHit() {
063    long time = ioHitTime.sum() / NANO_TIME;
064    long count = ioHitCount.sum();
065    return ((float) time / (float) count);
066  }
067
068  public void reset() {
069    ioHitCount.reset();
070    ioHitTime.reset();
071    allocationFailCount.reset();
072  }
073
074  public long getAllocationFailCount() {
075    return allocationFailCount.sum();
076  }
077
078  public void allocationFailed() {
079    allocationFailCount.increment();
080  }
081}