Add more detail on the sizes of each segment #314

This commit is contained in:
Peter Lawrey
2021-08-09 09:50:50 +01:00
parent 651ce9630e
commit 9d2346a789
3 changed files with 75 additions and 16 deletions
@@ -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<K, V> extends ConcurrentMap<K, V>,
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.
@@ -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<K, V, R>
extends VanillaChronicleHash<K, MapEntry<K, V>, MapSegmentContext<K, V, ?>, ExternalMapQueryContext<K, V, ?>>
implements AbstractChronicleMap<K, V> {
private double maxBloatFactor;
public SizeMarshaller valueSizeMarshaller;
public SizedReader<V> valueReader;
public DataAccess<V> valueDataAccess;
@@ -89,6 +88,7 @@ public class VanillaChronicleMap<K, V, R>
transient boolean removeReturnsNull;
transient Set<Entry<K, V>> entrySet;
transient ThreadLocal<ContextHolder> cxt;
private double maxBloatFactor;
/////////////////////////////////////////////////
private transient String name;
/**
@@ -314,6 +314,39 @@ public class VanillaChronicleMap<K, V, R>
return (short) (100 - (int) (100 * totalUsed / totalSize));
}
@Override
public SegmentStats[] segmentStats() {
throwExceptionIfClosed();
SegmentStats[] ret = new SegmentStats[segments()];
try (IterationContext<K, V, ?> 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) {