View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile.bucket;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.io.hfile.CacheStats;
23  import org.apache.hadoop.hbase.util.Counter;
24  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
25  
26  /**
27   * Class that implements cache metrics for bucket cache.
28   */
29  @InterfaceAudience.Private
30  public class BucketCacheStats extends CacheStats {
31    private final Counter ioHitCount = new Counter(0);
32    private final Counter ioHitTime = new Counter(0);
33    private final static int nanoTime = 1000000;
34    private long lastLogTime = EnvironmentEdgeManager.currentTime();
35  
36    BucketCacheStats() {
37      super("BucketCache");
38    }
39  
40    @Override
41    public String toString() {
42      return super.toString() + ", ioHitsPerSecond=" + getIOHitsPerSecond() +
43        ", ioTimePerHit=" + getIOTimePerHit();
44    }
45  
46    public void ioHit(long time) {
47      ioHitCount.increment();
48      ioHitTime.add(time);
49    }
50  
51    public long getIOHitsPerSecond() {
52      long now = EnvironmentEdgeManager.currentTime();
53      long took = (now - lastLogTime) / 1000;
54      lastLogTime = now;
55      return took == 0? 0: ioHitCount.get() / took;
56    }
57  
58    public double getIOTimePerHit() {
59      long time = ioHitTime.get() / nanoTime;
60      long count = ioHitCount.get();
61      return ((float) time / (float) count);
62    }
63  
64    public void reset() {
65      ioHitCount.set(0);
66      ioHitTime.set(0);
67    }
68  }