@Test
public void getName() {
BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
- .fromStatsList("name", List.of());
+ .fromStatsList(List.of());
- assertThat(aggregatedBlockCacheStats.getName(), equalTo("name"));
+ assertThat(aggregatedBlockCacheStats.getName(),
+ equalTo(AggregatedBlockCacheStats.class.getName()));
}
@Test
currentSizes[PackExt.INDEX.getPosition()] = 7;
BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
- .fromStatsList("name",
- List.of(packStats, bitmapStats, indexStats));
+ .fromStatsList(List.of(packStats, bitmapStats, indexStats));
assertArrayEquals(aggregatedBlockCacheStats.getCurrentSize(),
currentSizes);
hitCounts[PackExt.INDEX.getPosition()] = 7;
BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
- .fromStatsList("name",
- List.of(packStats, bitmapStats, indexStats));
+ .fromStatsList(List.of(packStats, bitmapStats, indexStats));
assertArrayEquals(aggregatedBlockCacheStats.getHitCount(), hitCounts);
}
missCounts[PackExt.INDEX.getPosition()] = 7;
BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
- .fromStatsList("name",
- List.of(packStats, bitmapStats, indexStats));
+ .fromStatsList(List.of(packStats, bitmapStats, indexStats));
assertArrayEquals(aggregatedBlockCacheStats.getMissCount(), missCounts);
}
totalRequestCounts[PackExt.INDEX.getPosition()] = 14;
BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
- .fromStatsList("name",
- List.of(packStats, bitmapStats, indexStats));
+ .fromStatsList(List.of(packStats, bitmapStats, indexStats));
assertArrayEquals(aggregatedBlockCacheStats.getTotalRequestCount(),
totalRequestCounts);
hitRatios[PackExt.INDEX.getPosition()] = 0;
BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
- .fromStatsList("Name",
- List.of(packStats, bitmapStats, indexStats));
+ .fromStatsList(List.of(packStats, bitmapStats, indexStats));
assertArrayEquals(aggregatedBlockCacheStats.getHitRatio(), hitRatios);
}
evictions[PackExt.INDEX.getPosition()] = 7;
BlockCacheStats aggregatedBlockCacheStats = AggregatedBlockCacheStats
- .fromStatsList("Name",
- List.of(packStats, bitmapStats, indexStats));
+ .fromStatsList(List.of(packStats, bitmapStats, indexStats));
assertArrayEquals(aggregatedBlockCacheStats.getEvictions(), evictions);
}
package org.eclipse.jgit.internal.storage.dfs;
import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheConfig.DEFAULT_NAME;
+import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheTable.BlockCacheStats;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.isA;
+
+import java.util.List;
import org.junit.Test;
ClockBlockCacheTable cacheTable = new ClockBlockCacheTable(
createBlockCacheConfig());
- assertThat(cacheTable.getBlockCacheStats().getName(),
+ assertThat(cacheTable.getBlockCacheStats(), hasSize(1));
+ assertThat(cacheTable.getBlockCacheStats().get(0).getName(),
equalTo(DEFAULT_NAME));
}
ClockBlockCacheTable cacheTable = new ClockBlockCacheTable(
createBlockCacheConfig().setName(NAME));
- assertThat(cacheTable.getBlockCacheStats().getName(), equalTo(NAME));
+ assertThat(cacheTable.getBlockCacheStats(), hasSize(1));
+ assertThat(cacheTable.getBlockCacheStats().get(0).getName(),
+ equalTo(NAME));
+ }
+
+ @Test
+ public void getAllBlockCacheStats() {
+ ClockBlockCacheTable cacheTable = new ClockBlockCacheTable(
+ createBlockCacheConfig());
+
+ List<BlockCacheStats> blockCacheStats = cacheTable.getBlockCacheStats();
+ assertThat(blockCacheStats, contains(isA(BlockCacheStats.class)));
}
private static DfsBlockCacheConfig createBlockCacheConfig() {
package org.eclipse.jgit.internal.storage.dfs;
+import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheTable.BlockCacheStats;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
}
@Test
- public void getBlockCacheStats_getName_returnsPackExtCacheTableName() {
- DfsBlockCacheStats packStats = new DfsBlockCacheStats();
- PackExtBlockCacheTable tables = PackExtBlockCacheTable.fromCacheTables(
- cacheTableWithStats(/* name= */ "defaultName", packStats),
- Map.of(PackExt.PACK, cacheTableWithStats(/* name= */ "packName",
- packStats)));
+ public void getAllBlockCacheStats() {
+ String defaultTableName = "default table";
+ DfsBlockCacheStats defaultStats = new DfsBlockCacheStats(
+ defaultTableName);
+ incrementCounter(4,
+ () -> defaultStats.incrementHit(new TestKey(PackExt.REFTABLE)));
+
+ String packTableName = "pack table";
+ DfsBlockCacheStats packStats = new DfsBlockCacheStats(packTableName);
+ incrementCounter(5,
+ () -> packStats.incrementHit(new TestKey(PackExt.PACK)));
- assertThat(tables.getBlockCacheStats().getName(),
- equalTo("defaultName,packName"));
+ String bitmapTableName = "bitmap table";
+ DfsBlockCacheStats bitmapStats = new DfsBlockCacheStats(
+ bitmapTableName);
+ incrementCounter(6, () -> bitmapStats
+ .incrementHit(new TestKey(PackExt.BITMAP_INDEX)));
+
+ DfsBlockCacheTable defaultTable = cacheTableWithStats(defaultStats);
+ DfsBlockCacheTable packTable = cacheTableWithStats(packStats);
+ DfsBlockCacheTable bitmapTable = cacheTableWithStats(bitmapStats);
+ PackExtBlockCacheTable tables = PackExtBlockCacheTable
+ .fromCacheTables(defaultTable, Map.of(PackExt.PACK, packTable,
+ PackExt.BITMAP_INDEX, bitmapTable));
+
+ List<BlockCacheStats> statsList = tables.getBlockCacheStats();
+ assertThat(statsList, hasSize(3));
+
+ long[] defaultTableHitCounts = createEmptyStatsArray();
+ defaultTableHitCounts[PackExt.REFTABLE.getPosition()] = 4;
+ assertArrayEquals(
+ getCacheStatsByName(statsList, defaultTableName).getHitCount(),
+ defaultTableHitCounts);
+
+ long[] packTableHitCounts = createEmptyStatsArray();
+ packTableHitCounts[PackExt.PACK.getPosition()] = 5;
+ assertArrayEquals(
+ getCacheStatsByName(statsList, packTableName).getHitCount(),
+ packTableHitCounts);
+
+ long[] bitmapHitCounts = createEmptyStatsArray();
+ bitmapHitCounts[PackExt.BITMAP_INDEX.getPosition()] = 6;
+ assertArrayEquals(
+ getCacheStatsByName(statsList, bitmapTableName).getHitCount(),
+ bitmapHitCounts);
}
@Test
cacheTableWithStats(bitmapStats), PackExt.INDEX,
cacheTableWithStats(indexStats)));
- assertArrayEquals(tables.getBlockCacheStats().getCurrentSize(),
+ assertArrayEquals(AggregatedBlockCacheStats
+ .fromStatsList(tables.getBlockCacheStats()).getCurrentSize(),
currentSizes);
}
cacheTableWithStats(bitmapStats), PackExt.INDEX,
cacheTableWithStats(indexStats)));
- assertArrayEquals(tables.getBlockCacheStats().getHitCount(), hitCounts);
+ assertArrayEquals(AggregatedBlockCacheStats
+ .fromStatsList(tables.getBlockCacheStats()).getHitCount(),
+ hitCounts);
}
@Test
cacheTableWithStats(bitmapStats), PackExt.INDEX,
cacheTableWithStats(indexStats)));
- assertArrayEquals(tables.getBlockCacheStats().getMissCount(),
+ assertArrayEquals(AggregatedBlockCacheStats
+ .fromStatsList(tables.getBlockCacheStats()).getMissCount(),
missCounts);
}
cacheTableWithStats(bitmapStats), PackExt.INDEX,
cacheTableWithStats(indexStats)));
- assertArrayEquals(tables.getBlockCacheStats().getTotalRequestCount(),
- totalRequestCounts);
+ assertArrayEquals(AggregatedBlockCacheStats
+ .fromStatsList(tables.getBlockCacheStats())
+ .getTotalRequestCount(), totalRequestCounts);
}
@Test
cacheTableWithStats(bitmapStats), PackExt.INDEX,
cacheTableWithStats(indexStats)));
- assertArrayEquals(tables.getBlockCacheStats().getHitRatio(), hitRatios);
+ assertArrayEquals(AggregatedBlockCacheStats
+ .fromStatsList(tables.getBlockCacheStats()).getHitRatio(),
+ hitRatios);
}
@Test
cacheTableWithStats(bitmapStats), PackExt.INDEX,
cacheTableWithStats(indexStats)));
- assertArrayEquals(tables.getBlockCacheStats().getEvictions(),
+ assertArrayEquals(AggregatedBlockCacheStats
+ .fromStatsList(tables.getBlockCacheStats()).getEvictions(),
evictions);
}
+ private BlockCacheStats getCacheStatsByName(
+ List<BlockCacheStats> blockCacheStats, String name) {
+ for (BlockCacheStats entry : blockCacheStats) {
+ if (entry.getName().equals(name)) {
+ return entry;
+ }
+ }
+ return null;
+ }
+
private static void incrementCounter(int amount, Runnable fn) {
for (int i = 0; i < amount; i++) {
fn.run();
}
private static DfsBlockCacheTable cacheTableWithStats(
- DfsBlockCacheStats dfsBlockCacheStats) {
+ BlockCacheStats dfsBlockCacheStats) {
return cacheTableWithStats(CACHE_NAME, dfsBlockCacheStats);
}
private static DfsBlockCacheTable cacheTableWithStats(String name,
- DfsBlockCacheStats dfsBlockCacheStats) {
+ BlockCacheStats dfsBlockCacheStats) {
DfsBlockCacheTable cacheTable = mock(DfsBlockCacheTable.class);
when(cacheTable.getName()).thenReturn(name);
- when(cacheTable.getBlockCacheStats()).thenReturn(dfsBlockCacheStats);
+ when(cacheTable.getBlockCacheStats())
+ .thenReturn(List.of(dfsBlockCacheStats));
return cacheTable;
}
* Aggregates values for all given {@link BlockCacheStats}.
*/
class AggregatedBlockCacheStats implements BlockCacheStats {
- private final String name;
-
private final List<BlockCacheStats> blockCacheStats;
- static BlockCacheStats fromStatsList(String name,
+ static BlockCacheStats fromStatsList(
List<BlockCacheStats> blockCacheStats) {
if (blockCacheStats.size() == 1) {
return blockCacheStats.get(0);
}
- return new AggregatedBlockCacheStats(name, blockCacheStats);
+ return new AggregatedBlockCacheStats(blockCacheStats);
}
- private AggregatedBlockCacheStats(String name,
- List<BlockCacheStats> blockCacheStats) {
- this.name = name;
+ private AggregatedBlockCacheStats(List<BlockCacheStats> blockCacheStats) {
this.blockCacheStats = blockCacheStats;
}
@Override
public String getName() {
- return name;
+ return AggregatedBlockCacheStats.class.getName();
}
@Override
import java.io.IOException;
import java.time.Duration;
+import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReferenceArray;
}
@Override
- public BlockCacheStats getBlockCacheStats() {
- return dfsBlockCacheStats;
+ public List<BlockCacheStats> getBlockCacheStats() {
+ return List.of(dfsBlockCacheStats);
}
@Override
package org.eclipse.jgit.internal.storage.dfs;
+import static org.eclipse.jgit.internal.storage.dfs.DfsBlockCacheTable.BlockCacheStats;
+
import java.io.IOException;
+import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.LongStream;
* @return total number of bytes in the cache, per pack file extension.
*/
public long[] getCurrentSize() {
- return dfsBlockCacheTable.getBlockCacheStats().getCurrentSize();
+ return getAggregatedBlockCacheStats().getCurrentSize();
}
/**
* extension.
*/
public long[] getHitCount() {
- return dfsBlockCacheTable.getBlockCacheStats().getHitCount();
+ return getAggregatedBlockCacheStats().getHitCount();
}
/**
* extension.
*/
public long[] getMissCount() {
- return dfsBlockCacheTable.getBlockCacheStats().getMissCount();
+ return getAggregatedBlockCacheStats().getMissCount();
}
/**
* @return total number of requests (hit + miss), per pack file extension.
*/
public long[] getTotalRequestCount() {
- return dfsBlockCacheTable.getBlockCacheStats().getTotalRequestCount();
+ return getAggregatedBlockCacheStats().getTotalRequestCount();
}
/**
* @return hit ratios
*/
public long[] getHitRatio() {
- return dfsBlockCacheTable.getBlockCacheStats().getHitRatio();
+ return getAggregatedBlockCacheStats().getHitRatio();
}
/**
* file extension.
*/
public long[] getEvictions() {
- return dfsBlockCacheTable.getBlockCacheStats().getEvictions();
+ return getAggregatedBlockCacheStats().getEvictions();
+ }
+
+ /**
+ * Get the list of {@link BlockCacheStats} for all underlying caches.
+ * <p>
+ * Useful in monitoring caches with breakdown.
+ *
+ * @return the list of {@link BlockCacheStats} for all underlying caches.
+ */
+ public List<BlockCacheStats> getAllBlockCacheStats() {
+ return dfsBlockCacheTable.getBlockCacheStats();
}
/**
return dfsBlockCacheTable.get(key, position);
}
+ private BlockCacheStats getAggregatedBlockCacheStats() {
+ return AggregatedBlockCacheStats
+ .fromStatsList(dfsBlockCacheTable.getBlockCacheStats());
+ }
+
static final class Ref<T> {
final DfsStreamKey key;
package org.eclipse.jgit.internal.storage.dfs;
import java.io.IOException;
+import java.util.List;
/**
* Block cache table.
<T> T get(DfsStreamKey key, long position);
/**
- * Get the {@link BlockCacheStats} object for this block cache table's
- * statistics.
+ * Get the list of {@link BlockCacheStats} held by this cache.
+ * <p>
+ * The returned list has a {@link BlockCacheStats} per configured cache
+ * table, with a minimum of 1 {@link BlockCacheStats} object returned.
+ *
+ * Use {@link AggregatedBlockCacheStats} to combine the results of the stats
+ * in the list for an aggregated view of the cache's stats.
*
- * @return the {@link BlockCacheStats} tracking this block cache table's
- * statistics.
+ * @return the list of {@link BlockCacheStats} held by this cache.
*/
- BlockCacheStats getBlockCacheStats();
+ List<BlockCacheStats> getBlockCacheStats();
/**
* Get the name of the table.
}
@Override
- public BlockCacheStats getBlockCacheStats() {
- return AggregatedBlockCacheStats.fromStatsList(name,
- blockCacheTableList.stream()
- .map(DfsBlockCacheTable::getBlockCacheStats)
- .collect(Collectors.toList()));
+ public List<BlockCacheStats> getBlockCacheStats() {
+ return blockCacheTableList.stream()
+ .flatMap(cacheTable -> cacheTable.getBlockCacheStats().stream())
+ .collect(Collectors.toList());
}
@Override