Replace nextPosToSearchFrom with lowestPossiblyFreeChunk semantically, that allows to avoid searching free chunks though a full tier, when segment is tiered, again and again. Also, make chunk block reallocation in innerDefaultReplaceValue() and putValueDeletedEntry() check it doesn't overflow actualChunksPerSegmentTier

This commit is contained in:
leventov
2015-12-07 07:20:47 +03:00
parent 82988e7435
commit 2f58bfb399
15 changed files with 125 additions and 138 deletions
@@ -50,10 +50,11 @@ public final class BigSegmentHeader implements SegmentHeader {
static final int WAIT_PARTY = 1;
static final long SIZE_OFFSET = LOCK_OFFSET + 8L; // 32-bit
static final long NEXT_POS_TO_SEARCH_FROM_OFFSET = SIZE_OFFSET + 4L;
static final long LOWEST_POSSIBLY_FREE_CHUNK_OFFSET = ENTRIES_OFFSET + 4L;
static final long EXCLUSIVE_LOCK_HOLDER_THREAD_ID_OFFSET = NEXT_POS_TO_SEARCH_FROM_OFFSET + 4L;
static final long EXCLUSIVE_LOCK_HOLDER_THREAD_ID_OFFSET =
LOWEST_POSSIBLY_FREE_CHUNK_OFFSET + 4L;
static final long DELETED_OFFSET = EXCLUSIVE_LOCK_HOLDER_THREAD_ID_OFFSET + 8L;
@@ -89,13 +90,14 @@ public final class BigSegmentHeader implements SegmentHeader {
}
@Override
public long nextPosToSearchFrom(long address) {
return OS.memory().readInt(address + NEXT_POS_TO_SEARCH_FROM_OFFSET) & UNSIGNED_INT_MASK;
public long lowestPossiblyFreeChunk(long address) {
return OS.memory().readInt(address + LOWEST_POSSIBLY_FREE_CHUNK_OFFSET) & UNSIGNED_INT_MASK;
}
@Override
public void nextPosToSearchFrom(long address, long nextPosToSearchFrom) {
OS.memory().writeInt(address + NEXT_POS_TO_SEARCH_FROM_OFFSET, (int) nextPosToSearchFrom);
public void lowestPossiblyFreeChunk(long address, long lowestPossiblyFreeChunk) {
OS.memory().writeInt(address + LOWEST_POSSIBLY_FREE_CHUNK_OFFSET,
(int) lowestPossiblyFreeChunk);
}
private static long getLockWord(long address) {
@@ -24,8 +24,8 @@ import static net.openhft.chronicle.algo.MemoryUnit.BYTES;
public abstract class CompactOffHeapLinearHashTable {
// to fit 64 bits per slot.
public static final int MAX_SEGMENT_CHUNKS = 1 << 30;
public static final int MAX_SEGMENT_ENTRIES = 1 << 29;
public static final int MAX_TIER_CHUNKS = 1 << 30;
public static final int MAX_TIER_ENTRIES = 1 << 29;
public static int valueBits(long actualChunksPerSegment) {
@@ -25,8 +25,8 @@ public interface SegmentHeader {
long deleted(long address);
void deleted(long address, long deleted);
long nextPosToSearchFrom(long address);
void nextPosToSearchFrom(long address, long nextPosToSearchFrom);
long lowestPossiblyFreeChunk(long address);
void lowestPossiblyFreeChunk(long address, long lowestPossiblyFreeChunk);
void readLock(long address);
void readLockInterruptibly(long address);
@@ -20,10 +20,10 @@ import net.openhft.chronicle.core.Memory;
import net.openhft.chronicle.core.OS;
/**
* The reason why this is not a data value generated class, and offsets are allocated by
* hand -- this functionality is accessed concurrently from many threads, and data value generated
* classes are stateful - so need to keep an instance in thread local that seems to be overall more
* pain than gain.
* The reason why this is not implemented via value interface, and offsets are allocated by hand --
* this functionality is accessed concurrently from many threads, and native value implementations
* are stateful - so need to keep an instance in thread local that seems to be overall more pain
* than gain.
*/
public enum TierCountersArea {
;
@@ -31,8 +31,8 @@ public enum TierCountersArea {
private static Memory memory = OS.memory();
public static final long NEXT_TIER_INDEX_OFFSET = 0L;
public static final long PREV_TIER_INDEX_OFFSET = NEXT_TIER_INDEX_OFFSET + 8L;
public static final long NEXT_POS_TO_SEARCH_FROM_TIERED_OFFSET = PREV_TIER_INDEX_OFFSET + 8L;
public static final long SEGMENT_INDEX_OFFSET = NEXT_POS_TO_SEARCH_FROM_TIERED_OFFSET + 8L;
public static final long LOWEST_POSSIBLY_FREE_CHUNK_TIERED_OFFSET = PREV_TIER_INDEX_OFFSET + 8L;
public static final long SEGMENT_INDEX_OFFSET = LOWEST_POSSIBLY_FREE_CHUNK_TIERED_OFFSET + 8L;
public static final long TIER_OFFSET = SEGMENT_INDEX_OFFSET + 4L;
public static long nextTierIndex(long address) {
@@ -43,13 +43,13 @@ public enum TierCountersArea {
memory.writeLong(address + NEXT_TIER_INDEX_OFFSET, nextTierIndex);
}
public static long nextPosToSearchFromTiered(long address) {
return memory.readLong(address + NEXT_POS_TO_SEARCH_FROM_TIERED_OFFSET);
public static long lowestPossiblyFreeChunkTiered(long address) {
return memory.readLong(address + LOWEST_POSSIBLY_FREE_CHUNK_TIERED_OFFSET);
}
public static void nextPosToSearchFromTiered(long address, long nextPosToSearchFrom) {
memory.writeLong(address + NEXT_POS_TO_SEARCH_FROM_TIERED_OFFSET,
nextPosToSearchFrom);
public static void lowestPossiblyFreeChunkTiered(long address, long lowestPossiblyFreeChunk) {
memory.writeLong(address + LOWEST_POSSIBLY_FREE_CHUNK_TIERED_OFFSET,
lowestPossiblyFreeChunk);
}
public static long prevTierIndex(long address) {
@@ -96,7 +96,7 @@ public abstract class VanillaChronicleHash<K,
public long chunkSize;
public int maxChunksPerEntry;
public long actualChunksPerSegment;
public long actualChunksPerSegmentTier;
/////////////////////////////////////////////////
// Precomputed offsets and sizes for fast Context init
@@ -180,12 +180,12 @@ public abstract class VanillaChronicleHash<K,
chunkSize = privateAPI.chunkSize();
maxChunksPerEntry = privateAPI.maxChunksPerEntry();
actualChunksPerSegment = privateAPI.actualChunksPerSegment();
actualChunksPerSegmentTier = privateAPI.actualChunksPerSegmentTier();
// Precomputed offsets and sizes for fast Context init
segmentHeaderSize = privateAPI.segmentHeaderSize();
tierHashLookupValueBits = valueBits(actualChunksPerSegment);
tierHashLookupValueBits = valueBits(actualChunksPerSegmentTier);
tierHashLookupKeyBits = keyBits(entriesPerSegment, tierHashLookupValueBits);
tierHashLookupEntrySize =
entrySize(tierHashLookupKeyBits, tierHashLookupValueBits);
@@ -198,10 +198,10 @@ public abstract class VanillaChronicleHash<K,
tierHashLookupOuterSize = CACHE_LINES.align(tierHashLookupInnerSize, BYTES);
tierFreeListInnerSize = LONGS.align(
BYTES.alignAndConvert(actualChunksPerSegment, BITS), BYTES);
BYTES.alignAndConvert(actualChunksPerSegmentTier, BITS), BYTES);
tierFreeListOuterSize = CACHE_LINES.align(tierFreeListInnerSize, BYTES);
tierEntrySpaceInnerSize = chunkSize * actualChunksPerSegment;
tierEntrySpaceInnerSize = chunkSize * actualChunksPerSegmentTier;
tierEntrySpaceInnerOffset = privateAPI.segmentEntrySpaceInnerOffset();
tierEntrySpaceOuterSize = CACHE_LINES.align(
tierEntrySpaceInnerOffset + tierEntrySpaceInnerSize, BYTES);
@@ -247,7 +247,7 @@ public abstract class VanillaChronicleHash<K,
chunkSize = wireIn.read(() -> "chunkSize").int64();
maxChunksPerEntry = wireIn.read(() -> "maxChunksPerEntry").int32();
actualChunksPerSegment = wireIn.read(() -> "actualChunksPerSegment").int64();
actualChunksPerSegmentTier = wireIn.read(() -> "actualChunksPerSegmentTier").int64();
segmentHeaderSize = wireIn.read(() -> "segmentHeaderSize").int32();
@@ -292,7 +292,7 @@ public abstract class VanillaChronicleHash<K,
wireOut.write(() -> "chunkSize").int64(chunkSize);
wireOut.write(() -> "maxChunksPerEntry").int32(maxChunksPerEntry);
wireOut.write(() -> "actualChunksPerSegment").int64(actualChunksPerSegment);
wireOut.write(() -> "actualChunksPerSegmentTier").int64(actualChunksPerSegmentTier);
wireOut.write(() -> "segmentHeaderSize").int32(segmentHeaderSize);
@@ -16,7 +16,6 @@
package net.openhft.chronicle.hash.impl.stage.entry;
import net.openhft.chronicle.algo.bitset.BitSetFrame;
import net.openhft.chronicle.algo.bitset.ReusableBitSet;
import net.openhft.chronicle.algo.bitset.SingleThreadedFlatBitSetFrame;
import net.openhft.chronicle.algo.bytes.Access;
@@ -40,6 +39,7 @@ import java.util.Objects;
import static net.openhft.chronicle.algo.MemoryUnit.BITS;
import static net.openhft.chronicle.algo.MemoryUnit.LONGS;
import static net.openhft.chronicle.algo.bitset.BitSetFrame.NOT_FOUND;
import static net.openhft.chronicle.hash.impl.LocalLockState.UNLOCKED;
import static net.openhft.chronicle.hash.impl.VanillaChronicleHash.TIER_COUNTERS_AREA_SIZE;
@@ -74,19 +74,20 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface {
segmentHeader.size(segmentHeaderAddress, size);
}
long nextPosToSearchFrom() {
long lowestPossiblyFreeChunk() {
if (tier == 0) {
return segmentHeader.nextPosToSearchFrom(segmentHeaderAddress);
return segmentHeader.lowestPossiblyFreeChunk(segmentHeaderAddress);
} else {
return nextPosToSearchFromTiered();
return TierCountersArea.lowestPossiblyFreeChunkTiered(tierCountersAreaAddr());
}
}
public void nextPosToSearchFrom(long nextPosToSearchFrom) {
public void lowestPossiblyFreeChunk(long lowestPossiblyFreeChunk) {
if (tier == 0) {
segmentHeader.nextPosToSearchFrom(segmentHeaderAddress, nextPosToSearchFrom);
segmentHeader.lowestPossiblyFreeChunk(segmentHeaderAddress, lowestPossiblyFreeChunk);
} else {
nextPosToSearchFromTiered(nextPosToSearchFrom);
TierCountersArea.lowestPossiblyFreeChunkTiered(tierCountersAreaAddr(),
lowestPossiblyFreeChunk);
}
}
@@ -519,14 +520,6 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface {
TierCountersArea.nextTierIndex(tierCountersAreaAddr(), nextTierIndex);
}
public long nextPosToSearchFromTiered() {
return TierCountersArea.nextPosToSearchFromTiered(tierCountersAreaAddr());
}
public void nextPosToSearchFromTiered(long nextPosToSearchFrom) {
TierCountersArea.nextPosToSearchFromTiered(tierCountersAreaAddr(), nextPosToSearchFrom);
}
public long prevTierIndex() {
return TierCountersArea.prevTierIndex(tierCountersAreaAddr());
}
@@ -573,8 +566,8 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface {
@Stage("Segment") public final PointerBytesStore segmentBS = new PointerBytesStore();
@Stage("Segment") public final Bytes segmentBytes = new VanillaBytes(segmentBS);
@Stage("Segment") public final ReusableBitSet freeList = new ReusableBitSet(
new SingleThreadedFlatBitSetFrame(LONGS.align(hh.h().actualChunksPerSegment, BITS)),
@Stage("Segment") private final ReusableBitSet freeList = new ReusableBitSet(
new SingleThreadedFlatBitSetFrame(LONGS.align(hh.h().actualChunksPerSegmentTier, BITS)),
Access.nativeAccess(), null, 0);
@Stage("Segment") long entrySpaceOffset = 0;
@@ -612,50 +605,51 @@ public abstract class SegmentStages implements SegmentLock, LocksInterface {
entrySpaceOffset = 0;
}
@Stage("Segment")
public long allocReturnCode(int chunks) {
VanillaChronicleHash<?, ?, ?, ?> h = hh.h();
if (chunks > h.maxChunksPerEntry) {
throw new IllegalArgumentException("Entry is too large: requires " + chunks +
" chucks, " + h.maxChunksPerEntry + " is maximum.");
}
long ret = freeList.setNextNContinuousClearBits(nextPosToSearchFrom(), chunks);
if (ret == BitSetFrame.NOT_FOUND || ret + chunks > h.actualChunksPerSegment) {
if (ret != BitSetFrame.NOT_FOUND &&
ret + chunks > h.actualChunksPerSegment && ret < h.actualChunksPerSegment) {
freeList.clearRange(ret, h.actualChunksPerSegment);
long lowestPossiblyFreeChunk = lowestPossiblyFreeChunk();
if (lowestPossiblyFreeChunk == h.actualChunksPerSegmentTier)
return -1;
assert lowestPossiblyFreeChunk < h.actualChunksPerSegmentTier;
long ret = freeList.setNextNContinuousClearBits(lowestPossiblyFreeChunk, chunks);
if (ret == NOT_FOUND || ret + chunks > h.actualChunksPerSegmentTier) {
if (ret + chunks > h.actualChunksPerSegmentTier) {
assert ret != NOT_FOUND;
freeList.clearRange(ret, ret + chunks);
}
ret = freeList.setNextNContinuousClearBits(0L, chunks);
if (ret == BitSetFrame.NOT_FOUND || ret + chunks > h.actualChunksPerSegment) {
if (ret != BitSetFrame.NOT_FOUND &&
ret + chunks > h.actualChunksPerSegment &&
ret < h.actualChunksPerSegment) {
freeList.clearRange(ret, h.actualChunksPerSegment);
}
return -1;
}
updateNextPosToSearchFrom(ret, chunks);
return -1;
} else {
// if bit at nextPosToSearchFrom is clear, it was skipped because
// more than 1 chunk was requested. Don't move nextPosToSearchFrom
// if bit at lowestPossiblyFreeChunk is clear, it was skipped because
// more than 1 chunk was requested. Don't move lowestPossiblyFreeChunk
// in this case. chunks == 1 clause is just a fast path.
if (chunks == 1 || freeList.isSet(nextPosToSearchFrom())) {
updateNextPosToSearchFrom(ret, chunks);
if (chunks == 1 || freeList.isSet(lowestPossiblyFreeChunk)) {
lowestPossiblyFreeChunk(ret + chunks);
}
return ret;
}
return ret;
}
@Stage("Segment")
public boolean realloc(long fromPos, int oldChunks, int newChunks) {
if (fromPos + newChunks < hh.h().actualChunksPerSegmentTier &&
freeList.isRangeClear(fromPos + oldChunks, fromPos + newChunks)) {
freeList.setRange(fromPos + oldChunks, fromPos + newChunks);
return true;
} else {
return false;
}
}
@Stage("Segment")
public void free(long fromPos, int chunks) {
freeList.clearRange(fromPos, fromPos + chunks);
if (fromPos < nextPosToSearchFrom())
nextPosToSearchFrom(fromPos);
}
public void updateNextPosToSearchFrom(long allocated, int chunks) {
long nextPosToSearchFrom = allocated + chunks;
if (nextPosToSearchFrom >= hh.h().actualChunksPerSegment)
nextPosToSearchFrom = 0L;
nextPosToSearchFrom(nextPosToSearchFrom);
if (fromPos < lowestPossiblyFreeChunk())
lowestPossiblyFreeChunk(fromPos);
}
public void verifyTierCountersAreaData() {