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() {
040    super("BucketCache");
041
042    allocationFailCount.reset();
043  }
044
045  @Override
046  public String toString() {
047    return super.toString() + ", ioHitsPerSecond=" + getIOHitsPerSecond() + ", ioTimePerHit="
048      + getIOTimePerHit() + ", allocationFailCount=" + getAllocationFailCount();
049  }
050
051  public void ioHit(long time) {
052    ioHitCount.increment();
053    ioHitTime.add(time);
054  }
055
056  public long getIOHitsPerSecond() {
057    long now = EnvironmentEdgeManager.currentTime();
058    long took = (now - lastLogTime) / 1000;
059    lastLogTime = now;
060    return took == 0 ? 0 : ioHitCount.sum() / took;
061  }
062
063  public double getIOTimePerHit() {
064    long time = ioHitTime.sum() / NANO_TIME;
065    long count = ioHitCount.sum();
066    return ((float) time / (float) count);
067  }
068
069  public void reset() {
070    ioHitCount.reset();
071    ioHitTime.reset();
072    allocationFailCount.reset();
073  }
074
075  public long getAllocationFailCount() {
076    return allocationFailCount.sum();
077  }
078
079  public void allocationFailed() {
080    allocationFailCount.increment();
081  }
082}