001/**
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with this
006 * work for additional information regarding copyright ownership. The ASF
007 * licenses this file to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016 * License for the specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.hadoop.hbase.io.hfile.bucket;
020
021import java.util.concurrent.TimeUnit;
022import java.util.concurrent.atomic.LongAdder;
023
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.hadoop.hbase.io.hfile.CacheStats;
026import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
027
028/**
029 * Class that implements cache metrics for bucket cache.
030 */
031@InterfaceAudience.Private
032public class BucketCacheStats extends CacheStats {
033  private final LongAdder ioHitCount = new LongAdder();
034  private final LongAdder ioHitTime = new LongAdder();
035  private static final long NANO_TIME = TimeUnit.MILLISECONDS.toNanos(1);
036  private long lastLogTime = EnvironmentEdgeManager.currentTime();
037
038  BucketCacheStats() {
039    super("BucketCache");
040  }
041
042  @Override
043  public String toString() {
044    return super.toString() + ", ioHitsPerSecond=" + getIOHitsPerSecond() +
045      ", ioTimePerHit=" + getIOTimePerHit();
046  }
047
048  public void ioHit(long time) {
049    ioHitCount.increment();
050    ioHitTime.add(time);
051  }
052
053  public long getIOHitsPerSecond() {
054    long now = EnvironmentEdgeManager.currentTime();
055    long took = (now - lastLogTime) / 1000;
056    lastLogTime = now;
057    return took == 0 ? 0 : ioHitCount.sum() / took;
058  }
059
060  public double getIOTimePerHit() {
061    long time = ioHitTime.sum() / NANO_TIME;
062    long count = ioHitCount.sum();
063    return ((float) time / (float) count);
064  }
065
066  public void reset() {
067    ioHitCount.reset();
068    ioHitTime.reset();
069  }
070}