提交 2994738e authored 作者: Thomas Mueller's avatar Thomas Mueller

Add metrics

上级 f9baefbb
...@@ -75,7 +75,7 @@ public class CacheLongKeyLIRS<V> { ...@@ -75,7 +75,7 @@ public class CacheLongKeyLIRS<V> {
* of the stack before the current item is moved * of the stack before the current item is moved
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public CacheLongKeyLIRS(long maxMemory, public CacheLongKeyLIRS(long maxMemory,
int segmentCount, int stackMoveDistance) { int segmentCount, int stackMoveDistance) {
setMaxMemory(maxMemory); setMaxMemory(maxMemory);
DataUtils.checkArgument( DataUtils.checkArgument(
...@@ -163,7 +163,7 @@ public class CacheLongKeyLIRS<V> { ...@@ -163,7 +163,7 @@ public class CacheLongKeyLIRS<V> {
return s.put(key, hash, value, memory); return s.put(key, hash, value, memory);
} }
} }
private Segment<V> resizeIfNeeded(Segment<V> s, int segmentIndex) { private Segment<V> resizeIfNeeded(Segment<V> s, int segmentIndex) {
int newLen = s.getNewMapLen(); int newLen = s.getNewMapLen();
if (newLen == 0) { if (newLen == 0) {
...@@ -366,6 +366,32 @@ public class CacheLongKeyLIRS<V> { ...@@ -366,6 +366,32 @@ public class CacheLongKeyLIRS<V> {
return x; return x;
} }
/**
* Get the number of cache hits.
*
* @return the cache hits
*/
public long getHits() {
long x = 0;
for (Segment<V> s : segments) {
x += s.hits;
}
return x;
}
/**
* Get the number of cache misses.
*
* @return the cache misses
*/
public long getMisses() {
int x = 0;
for (Segment<V> s : segments) {
x += s.misses;
}
return x;
}
/** /**
* Get the number of resident entries. * Get the number of resident entries.
* *
...@@ -480,6 +506,16 @@ public class CacheLongKeyLIRS<V> { ...@@ -480,6 +506,16 @@ public class CacheLongKeyLIRS<V> {
*/ */
int queue2Size; int queue2Size;
/**
* The number of cache hits.
*/
long hits;
/**
* The number of cache misses.
*/
long misses;
/** /**
* The map array. The size is always a power of 2. * The map array. The size is always a power of 2.
*/ */
...@@ -579,6 +615,8 @@ public class CacheLongKeyLIRS<V> { ...@@ -579,6 +615,8 @@ public class CacheLongKeyLIRS<V> {
*/ */
Segment(Segment<V> old, int len) { Segment(Segment<V> old, int len) {
this(old.maxMemory, old.stackMoveDistance, len); this(old.maxMemory, old.stackMoveDistance, len);
hits = old.hits;
misses = old.misses;
Entry<V> s = old.stack.stackPrev; Entry<V> s = old.stack.stackPrev;
while (s != old.stack) { while (s != old.stack) {
Entry<V> e = copy(s); Entry<V> e = copy(s);
...@@ -607,11 +645,11 @@ public class CacheLongKeyLIRS<V> { ...@@ -607,11 +645,11 @@ public class CacheLongKeyLIRS<V> {
s = s.queuePrev; s = s.queuePrev;
} }
} }
/** /**
* Calculate the new number of hash table buckets if the internal map * Calculate the new number of hash table buckets if the internal map
* should be re-sized. * should be re-sized.
* *
* @return 0 if no resizing is needed, or the new length * @return 0 if no resizing is needed, or the new length
*/ */
int getNewMapLen() { int getNewMapLen() {
...@@ -668,11 +706,13 @@ public class CacheLongKeyLIRS<V> { ...@@ -668,11 +706,13 @@ public class CacheLongKeyLIRS<V> {
Entry<V> e = find(key, hash); Entry<V> e = find(key, hash);
if (e == null) { if (e == null) {
// the entry was not found // the entry was not found
misses++;
return null; return null;
} }
V value = e.value; V value = e.value;
if (value == null) { if (value == null) {
// it was a non-resident entry // it was a non-resident entry
misses++;
return null; return null;
} }
if (e.isHot()) { if (e.isHot()) {
...@@ -685,6 +725,7 @@ public class CacheLongKeyLIRS<V> { ...@@ -685,6 +725,7 @@ public class CacheLongKeyLIRS<V> {
} else { } else {
access(key, hash); access(key, hash);
} }
hits++;
return value; return value;
} }
......
...@@ -128,7 +128,7 @@ public class TestCacheLIRS extends TestBase { ...@@ -128,7 +128,7 @@ public class TestCacheLIRS extends TestBase {
for (int i = 0; i < elements - 1; i++) { for (int i = 0; i < elements - 1; i++) {
test.put(i, i * 10); test.put(i, i * 10);
} }
assertTrue(test.sizeMapArray() + "<" + expectedMapSize, assertTrue(test.sizeMapArray() + "<" + expectedMapSize,
test.sizeMapArray() < expectedMapSize); test.sizeMapArray() < expectedMapSize);
test = createCache(elements); test = createCache(elements);
for (int i = 0; i < elements + 1; i++) { for (int i = 0; i < elements + 1; i++) {
...@@ -139,7 +139,7 @@ public class TestCacheLIRS extends TestBase { ...@@ -139,7 +139,7 @@ public class TestCacheLIRS extends TestBase {
for (int i = 0; i < elements * 2; i++) { for (int i = 0; i < elements * 2; i++) {
test.put(i, i * 10); test.put(i, i * 10);
} }
assertTrue(test.sizeMapArray() + ">" + expectedMapSize, assertTrue(test.sizeMapArray() + ">" + expectedMapSize,
test.sizeMapArray() > expectedMapSize); test.sizeMapArray() > expectedMapSize);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论