mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Add more detail on the sizes of each segment #314
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user