From 9d2346a78991f8a5ef74415449242f6c83678d3c Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Mon, 9 Aug 2021 09:50:50 +0100 Subject: [PATCH] Add more detail on the sizes of each segment #314 --- .../openhft/chronicle/map/ChronicleMap.java | 26 ++++++++++++ .../chronicle/map/VanillaChronicleMap.java | 41 +++++++++++++++++-- .../map/jsr166/map/ChronicleMapTest.java | 24 +++++------ 3 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java index 9ec0b1951..fc49d3e7f 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java @@ -21,6 +21,7 @@ import net.openhft.chronicle.core.util.SerializableFunction; import net.openhft.chronicle.hash.ChronicleHash; import net.openhft.chronicle.hash.serialization.SizedReader; import net.openhft.chronicle.hash.serialization.SizedWriter; +import net.openhft.chronicle.wire.SelfDescribingMarshallable; import org.jetbrains.annotations.NotNull; import java.io.Closeable; @@ -255,6 +256,31 @@ public interface ChronicleMap extends ConcurrentMap, throw new UnsupportedOperationException("todo"); } + /** + * @return an array of how full each segment is + */ + default SegmentStats[] segmentStats() { + throw new UnsupportedOperationException("todo"); + } + + public class SegmentStats extends SelfDescribingMarshallable { + long usedBytes; + long sizeInBytes; + int tiers; + + public int tiers() { + return tiers; + } + + public long usedBytes() { + return usedBytes; + } + + public long sizeInBytes() { + return sizeInBytes; + } + } + /** * @return the number of times in the future the map can expand its capacity of each segment ( by expending its capacity we mean expending the maximum number of possible entries that * can be stored into the map), the map will expand automatically. However, there is an upper limit to the number of times the map can expand. diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index d34c2ecbe..9da9a0f2a 100644 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -18,7 +18,6 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.algo.bitset.ReusableBitSet; import net.openhft.chronicle.algo.hashing.LongHashFunction; -import net.openhft.chronicle.analytics.Analytics; import net.openhft.chronicle.bytes.Bytes; import net.openhft.chronicle.bytes.BytesStore; import net.openhft.chronicle.bytes.PointerBytesStore; @@ -49,7 +48,9 @@ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; -import java.util.*; +import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; @@ -62,8 +63,6 @@ public class VanillaChronicleMap extends VanillaChronicleHash, MapSegmentContext, ExternalMapQueryContext> implements AbstractChronicleMap { - private double maxBloatFactor; - public SizeMarshaller valueSizeMarshaller; public SizedReader valueReader; public DataAccess valueDataAccess; @@ -89,6 +88,7 @@ public class VanillaChronicleMap transient boolean removeReturnsNull; transient Set> entrySet; transient ThreadLocal cxt; + private double maxBloatFactor; ///////////////////////////////////////////////// private transient String name; /** @@ -314,6 +314,39 @@ public class VanillaChronicleMap return (short) (100 - (int) (100 * totalUsed / totalSize)); } + @Override + public SegmentStats[] segmentStats() { + throwExceptionIfClosed(); + + SegmentStats[] ret = new SegmentStats[segments()]; + try (IterationContext c = iterationContext()) { + for (int segmentIndex = 0; segmentIndex < segments(); segmentIndex++) { + c.initSegmentIndex(segmentIndex); + + if (!(c instanceof CompiledMapIterationContext)) + continue; + final CompiledMapIterationContext c1 = (CompiledMapIterationContext) c; + + c1.goToFirstTier(); + final ReusableBitSet freeList = c1.freeList(); + SegmentStats ss = ret[segmentIndex] = new SegmentStats(); + ss.usedBytes = freeList.cardinality() * chunkSize; + ss.sizeInBytes = freeList.logicalSize() * chunkSize; + ss.tiers = 1; + + while (c1.hasNextTier()) { + c1.nextTier(); + final ReusableBitSet freeList0 = c1.freeList(); + ss.usedBytes += freeList0.cardinality() * chunkSize; + ss.sizeInBytes += freeList0.logicalSize() * chunkSize; + ss.tiers++; + } + } + } + + return ret; + } + @NotNull @Override public final MapClosable acquireContext(@NotNull final K key, @NotNull final V usingValue) { diff --git a/src/test/java/net/openhft/chronicle/map/jsr166/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jsr166/map/ChronicleMapTest.java index 0fef3e630..6e62ae735 100644 --- a/src/test/java/net/openhft/chronicle/map/jsr166/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jsr166/map/ChronicleMapTest.java @@ -597,23 +597,23 @@ public class ChronicleMapTest extends JSR166TestCase { .maxBloatFactor(2) .create()) { - long remainingAutoResizes = 0; - short percentageFreeSpace = 0; try { for (int i = 0; ; i++) { - map.put(i, 0); - remainingAutoResizes = map.remainingAutoResizes(); - percentageFreeSpace = map.percentageFreeSpace(); - } } catch (IllegalStateException e) { - if (e.getMessage().contains("Attempt to allocate")) { - Assert.assertEquals(0, (int) remainingAutoResizes); - Assert.assertTrue(percentageFreeSpace < 6); - return; - } - Assert.fail(); + assertTrue(e.getMessage().contains("Attempt to allocate")); + } + long remainingAutoResizes = map.remainingAutoResizes(); + short percentageFreeSpace = map.percentageFreeSpace(); + Assert.assertEquals(0, (int) remainingAutoResizes); + Assert.assertTrue(percentageFreeSpace < 6); + ChronicleMap.SegmentStats[] segmentStats = map.segmentStats(); + assertEquals(3, segmentStats.length); + for (ChronicleMap.SegmentStats ss : segmentStats) { + assertEquals(43428, ss.usedBytes(), 200); + assertEquals(43776, ss.sizeInBytes()); + assertEquals(3, ss.tiers()); } } }