View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.io.hfile;
21  
22  import java.util.concurrent.atomic.AtomicLong;
23  
24  /**
25   * Class that implements cache metrics.
26   */
27  public class CacheStats {
28    /** The number of getBlock requests that were cache hits */
29    private final AtomicLong hitCount = new AtomicLong(0);
30    /**
31     * The number of getBlock requests that were cache hits, but only from
32     * requests that were set to use the block cache.  This is because all reads
33     * attempt to read from the block cache even if they will not put new blocks
34     * into the block cache.  See HBASE-2253 for more information.
35     */
36    private final AtomicLong hitCachingCount = new AtomicLong(0);
37    /** The number of getBlock requests that were cache misses */
38    private final AtomicLong missCount = new AtomicLong(0);
39    /**
40     * The number of getBlock requests that were cache misses, but only from
41     * requests that were set to use the block cache.
42     */
43    private final AtomicLong missCachingCount = new AtomicLong(0);
44    /** The number of times an eviction has occurred */
45    private final AtomicLong evictionCount = new AtomicLong(0);
46    /** The total number of blocks that have been evicted */
47    private final AtomicLong evictedBlockCount = new AtomicLong(0);
48  
49    public void miss(boolean caching) {
50      missCount.incrementAndGet();
51      if (caching) missCachingCount.incrementAndGet();
52    }
53  
54    public void hit(boolean caching) {
55      hitCount.incrementAndGet();
56      if (caching) hitCachingCount.incrementAndGet();
57    }
58  
59    public void evict() {
60      evictionCount.incrementAndGet();
61    }
62  
63    public void evicted() {
64      evictedBlockCount.incrementAndGet();
65    }
66  
67    public long getRequestCount() {
68      return getHitCount() + getMissCount();
69    }
70  
71    public long getRequestCachingCount() {
72      return getHitCachingCount() + getMissCachingCount();
73    }
74  
75    public long getMissCount() {
76      return missCount.get();
77    }
78  
79    public long getMissCachingCount() {
80      return missCachingCount.get();
81    }
82  
83    public long getHitCount() {
84      return hitCount.get();
85    }
86  
87    public long getHitCachingCount() {
88      return hitCachingCount.get();
89    }
90  
91    public long getEvictionCount() {
92      return evictionCount.get();
93    }
94  
95    public long getEvictedCount() {
96      return evictedBlockCount.get();
97    }
98  
99    public double getHitRatio() {
100     return ((float)getHitCount()/(float)getRequestCount());
101   }
102 
103   public double getHitCachingRatio() {
104     return ((float)getHitCachingCount()/(float)getRequestCachingCount());
105   }
106 
107   public double getMissRatio() {
108     return ((float)getMissCount()/(float)getRequestCount());
109   }
110 
111   public double getMissCachingRatio() {
112     return ((float)getMissCachingCount()/(float)getRequestCachingCount());
113   }
114 
115   public double evictedPerEviction() {
116     return ((float)getEvictedCount()/(float)getEvictionCount());
117   }
118 }