1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.io.hfile;
20
21 import java.util.concurrent.atomic.AtomicLong;
22
23 import org.apache.hadoop.classification.InterfaceAudience;
24
25
26
27
28 @InterfaceAudience.Private
29 public class CacheStats {
30
31
32
33 static final int DEFAULT_WINDOW_PERIODS = 5;
34
35
36 private final AtomicLong hitCount = new AtomicLong(0);
37
38
39
40
41
42
43
44 private final AtomicLong hitCachingCount = new AtomicLong(0);
45
46
47 private final AtomicLong missCount = new AtomicLong(0);
48
49
50
51
52
53 private final AtomicLong missCachingCount = new AtomicLong(0);
54
55
56 private final AtomicLong evictionCount = new AtomicLong(0);
57
58
59 private final AtomicLong evictedBlockCount = new AtomicLong(0);
60
61
62 private final int numPeriodsInWindow;
63
64 private final long [] hitCounts;
65
66 private final long [] hitCachingCounts;
67
68 private final long [] requestCounts;
69
70 private final long [] requestCachingCounts;
71
72 private long lastHitCount = 0;
73
74 private long lastHitCachingCount = 0;
75
76 private long lastRequestCount = 0;
77
78 private long lastRequestCachingCount = 0;
79
80 private int windowIndex = 0;
81
82 public CacheStats() {
83 this(DEFAULT_WINDOW_PERIODS);
84 }
85
86 public CacheStats(int numPeriodsInWindow) {
87 this.numPeriodsInWindow = numPeriodsInWindow;
88 this.hitCounts = initializeZeros(numPeriodsInWindow);
89 this.hitCachingCounts = initializeZeros(numPeriodsInWindow);
90 this.requestCounts = initializeZeros(numPeriodsInWindow);
91 this.requestCachingCounts = initializeZeros(numPeriodsInWindow);
92 }
93
94 public void miss(boolean caching) {
95 missCount.incrementAndGet();
96 if (caching) missCachingCount.incrementAndGet();
97 }
98
99 @Override
100 public String toString() {
101 return "hitCount=" + getHitCount() + ", hitCachingCount=" + getHitCachingCount() +
102 ", missCount=" + getMissCount() + ", missCachingCount=" + getMissCachingCount() +
103 ", evictionCount=" + getEvictionCount() +
104 ", evictedBlockCount=" + getEvictedCount();
105 }
106
107 public void hit(boolean caching) {
108 hitCount.incrementAndGet();
109 if (caching) hitCachingCount.incrementAndGet();
110 }
111
112 public void evict() {
113 evictionCount.incrementAndGet();
114 }
115
116 public void evicted() {
117 evictedBlockCount.incrementAndGet();
118 }
119
120 public long getRequestCount() {
121 return getHitCount() + getMissCount();
122 }
123
124 public long getRequestCachingCount() {
125 return getHitCachingCount() + getMissCachingCount();
126 }
127
128 public long getMissCount() {
129 return missCount.get();
130 }
131
132 public long getMissCachingCount() {
133 return missCachingCount.get();
134 }
135
136 public long getHitCount() {
137 return hitCount.get();
138 }
139
140 public long getHitCachingCount() {
141 return hitCachingCount.get();
142 }
143
144 public long getEvictionCount() {
145 return evictionCount.get();
146 }
147
148 public long getEvictedCount() {
149 return evictedBlockCount.get();
150 }
151
152 public double getHitRatio() {
153 return ((float)getHitCount()/(float)getRequestCount());
154 }
155
156 public double getHitCachingRatio() {
157 return ((float)getHitCachingCount()/(float)getRequestCachingCount());
158 }
159
160 public double getMissRatio() {
161 return ((float)getMissCount()/(float)getRequestCount());
162 }
163
164 public double getMissCachingRatio() {
165 return ((float)getMissCachingCount()/(float)getRequestCachingCount());
166 }
167
168 public double evictedPerEviction() {
169 return ((float)getEvictedCount()/(float)getEvictionCount());
170 }
171
172 public void rollMetricsPeriod() {
173 hitCounts[windowIndex] = getHitCount() - lastHitCount;
174 lastHitCount = getHitCount();
175 hitCachingCounts[windowIndex] =
176 getHitCachingCount() - lastHitCachingCount;
177 lastHitCachingCount = getHitCachingCount();
178 requestCounts[windowIndex] = getRequestCount() - lastRequestCount;
179 lastRequestCount = getRequestCount();
180 requestCachingCounts[windowIndex] =
181 getRequestCachingCount() - lastRequestCachingCount;
182 lastRequestCachingCount = getRequestCachingCount();
183 windowIndex = (windowIndex + 1) % numPeriodsInWindow;
184 }
185
186 public long getSumHitCountsPastNPeriods() {
187 return sum(hitCounts);
188 }
189
190 public long getSumRequestCountsPastNPeriods() {
191 return sum(requestCounts);
192 }
193
194 public long getSumHitCachingCountsPastNPeriods() {
195 return sum(hitCachingCounts);
196 }
197
198 public long getSumRequestCachingCountsPastNPeriods() {
199 return sum(requestCachingCounts);
200 }
201
202 public double getHitRatioPastNPeriods() {
203 double ratio = ((double)sum(hitCounts)/(double)sum(requestCounts));
204 return Double.isNaN(ratio) ? 0 : ratio;
205 }
206
207 public double getHitCachingRatioPastNPeriods() {
208 double ratio =
209 ((double)sum(hitCachingCounts)/(double)sum(requestCachingCounts));
210 return Double.isNaN(ratio) ? 0 : ratio;
211 }
212
213 private static long sum(long [] counts) {
214 long sum = 0;
215 for (long count : counts) sum += count;
216 return sum;
217 }
218
219 private static long [] initializeZeros(int n) {
220 long [] zeros = new long [n];
221 for (int i=0; i<n; i++) {
222 zeros[i] = 0L;
223 }
224 return zeros;
225 }
226 }