Changeset View
Changeset View
Standalone View
Standalone View
llvm/lib/DebugInfo/MSF/MappedBlockStream.cpp
Show All 29 Lines | |||||
public: | public: | ||||
template <typename... Args> | template <typename... Args> | ||||
MappedBlockStreamImpl(Args &&... Params) | MappedBlockStreamImpl(Args &&... Params) | ||||
: Base(std::forward<Args>(Params)...) {} | : Base(std::forward<Args>(Params)...) {} | ||||
}; | }; | ||||
} // end anonymous namespace | } // end anonymous namespace | ||||
using Interval = std::pair<uint32_t, uint32_t>; | using Interval = std::pair<uint64_t, uint64_t>; | ||||
static Interval intersect(const Interval &I1, const Interval &I2) { | static Interval intersect(const Interval &I1, const Interval &I2) { | ||||
return std::make_pair(std::max(I1.first, I2.first), | return std::make_pair(std::max(I1.first, I2.first), | ||||
std::min(I1.second, I2.second)); | std::min(I1.second, I2.second)); | ||||
} | } | ||||
MappedBlockStream::MappedBlockStream(uint32_t BlockSize, | MappedBlockStream::MappedBlockStream(uint32_t BlockSize, | ||||
const MSFStreamLayout &Layout, | const MSFStreamLayout &Layout, | ||||
Show All 33 Lines | |||||
std::unique_ptr<MappedBlockStream> | std::unique_ptr<MappedBlockStream> | ||||
MappedBlockStream::createFpmStream(const MSFLayout &Layout, | MappedBlockStream::createFpmStream(const MSFLayout &Layout, | ||||
BinaryStreamRef MsfData, | BinaryStreamRef MsfData, | ||||
BumpPtrAllocator &Allocator) { | BumpPtrAllocator &Allocator) { | ||||
MSFStreamLayout SL(getFpmStreamLayout(Layout)); | MSFStreamLayout SL(getFpmStreamLayout(Layout)); | ||||
return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); | return createStream(Layout.SB->BlockSize, SL, MsfData, Allocator); | ||||
} | } | ||||
Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, | Error MappedBlockStream::readBytes(uint64_t Offset, uint64_t Size, | ||||
ArrayRef<uint8_t> &Buffer) { | ArrayRef<uint8_t> &Buffer) { | ||||
// Make sure we aren't trying to read beyond the end of the stream. | // Make sure we aren't trying to read beyond the end of the stream. | ||||
if (auto EC = checkOffsetForRead(Offset, Size)) | if (auto EC = checkOffsetForRead(Offset, Size)) | ||||
return EC; | return EC; | ||||
if (tryReadContiguously(Offset, Size, Buffer)) | if (tryReadContiguously(Offset, Size, Buffer)) | ||||
return Error::success(); | return Error::success(); | ||||
Show All 36 Lines | if (RequestExtent.first >= CachedExtent.first + CachedExtent.second) | ||||
continue; | continue; | ||||
Interval Intersection = intersect(CachedExtent, RequestExtent); | Interval Intersection = intersect(CachedExtent, RequestExtent); | ||||
// Only use this if the entire request extent is contained in the cached | // Only use this if the entire request extent is contained in the cached | ||||
// extent. | // extent. | ||||
if (Intersection != RequestExtent) | if (Intersection != RequestExtent) | ||||
continue; | continue; | ||||
uint32_t CacheRangeOffset = | uint64_t CacheRangeOffset = | ||||
AbsoluteDifference(CachedExtent.first, Intersection.first); | AbsoluteDifference(CachedExtent.first, Intersection.first); | ||||
Buffer = CachedAlloc.slice(CacheRangeOffset, Size); | Buffer = CachedAlloc.slice(CacheRangeOffset, Size); | ||||
return Error::success(); | return Error::success(); | ||||
} | } | ||||
// Otherwise allocate a large enough buffer in the pool, memcpy the data | // Otherwise allocate a large enough buffer in the pool, memcpy the data | ||||
// into it, and return an ArrayRef to that. Do not touch existing pool | // into it, and return an ArrayRef to that. Do not touch existing pool | ||||
// allocations, as existing clients may be holding a pointer which must | // allocations, as existing clients may be holding a pointer which must | ||||
// not be invalidated. | // not be invalidated. | ||||
uint8_t *WriteBuffer = static_cast<uint8_t *>(Allocator.Allocate(Size, 8)); | uint8_t *WriteBuffer = static_cast<uint8_t *>(Allocator.Allocate(Size, 8)); | ||||
if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size))) | if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size))) | ||||
return EC; | return EC; | ||||
if (CacheIter != CacheMap.end()) { | if (CacheIter != CacheMap.end()) { | ||||
CacheIter->second.emplace_back(WriteBuffer, Size); | CacheIter->second.emplace_back(WriteBuffer, Size); | ||||
} else { | } else { | ||||
std::vector<CacheEntry> List; | std::vector<CacheEntry> List; | ||||
List.emplace_back(WriteBuffer, Size); | List.emplace_back(WriteBuffer, Size); | ||||
CacheMap.insert(std::make_pair(Offset, List)); | CacheMap.insert(std::make_pair(Offset, List)); | ||||
} | } | ||||
Buffer = ArrayRef<uint8_t>(WriteBuffer, Size); | Buffer = ArrayRef<uint8_t>(WriteBuffer, Size); | ||||
return Error::success(); | return Error::success(); | ||||
} | } | ||||
Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset, | Error MappedBlockStream::readLongestContiguousChunk(uint64_t Offset, | ||||
ArrayRef<uint8_t> &Buffer) { | ArrayRef<uint8_t> &Buffer) { | ||||
// Make sure we aren't trying to read beyond the end of the stream. | // Make sure we aren't trying to read beyond the end of the stream. | ||||
if (auto EC = checkOffsetForRead(Offset, 1)) | if (auto EC = checkOffsetForRead(Offset, 1)) | ||||
return EC; | return EC; | ||||
uint32_t First = Offset / BlockSize; | uint64_t First = Offset / BlockSize; | ||||
uint32_t Last = First; | uint64_t Last = First; | ||||
while (Last < getNumBlocks() - 1) { | while (Last < getNumBlocks() - 1) { | ||||
if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1) | if (StreamLayout.Blocks[Last] != StreamLayout.Blocks[Last + 1] - 1) | ||||
break; | break; | ||||
++Last; | ++Last; | ||||
} | } | ||||
uint32_t OffsetInFirstBlock = Offset % BlockSize; | uint64_t OffsetInFirstBlock = Offset % BlockSize; | ||||
uint32_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock; | uint64_t BytesFromFirstBlock = BlockSize - OffsetInFirstBlock; | ||||
uint32_t BlockSpan = Last - First + 1; | uint64_t BlockSpan = Last - First + 1; | ||||
uint32_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize; | uint64_t ByteSpan = BytesFromFirstBlock + (BlockSpan - 1) * BlockSize; | ||||
ArrayRef<uint8_t> BlockData; | ArrayRef<uint8_t> BlockData; | ||||
uint32_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize); | uint64_t MsfOffset = blockToOffset(StreamLayout.Blocks[First], BlockSize); | ||||
if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) | if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) | ||||
return EC; | return EC; | ||||
BlockData = BlockData.drop_front(OffsetInFirstBlock); | BlockData = BlockData.drop_front(OffsetInFirstBlock); | ||||
Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan); | Buffer = ArrayRef<uint8_t>(BlockData.data(), ByteSpan); | ||||
return Error::success(); | return Error::success(); | ||||
} | } | ||||
uint32_t MappedBlockStream::getLength() { return StreamLayout.Length; } | uint64_t MappedBlockStream::getLength() { return StreamLayout.Length; } | ||||
bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size, | bool MappedBlockStream::tryReadContiguously(uint64_t Offset, uint64_t Size, | ||||
ArrayRef<uint8_t> &Buffer) { | ArrayRef<uint8_t> &Buffer) { | ||||
if (Size == 0) { | if (Size == 0) { | ||||
Buffer = ArrayRef<uint8_t>(); | Buffer = ArrayRef<uint8_t>(); | ||||
return true; | return true; | ||||
} | } | ||||
// Attempt to fulfill the request with a reference directly into the stream. | // Attempt to fulfill the request with a reference directly into the stream. | ||||
// This can work even if the request crosses a block boundary, provided that | // This can work even if the request crosses a block boundary, provided that | ||||
// all subsequent blocks are contiguous. For example, a 10k read with a 4k | // all subsequent blocks are contiguous. For example, a 10k read with a 4k | ||||
// block size can be filled with a reference if, from the starting offset, | // block size can be filled with a reference if, from the starting offset, | ||||
// 3 blocks in a row are contiguous. | // 3 blocks in a row are contiguous. | ||||
uint32_t BlockNum = Offset / BlockSize; | uint64_t BlockNum = Offset / BlockSize; | ||||
uint32_t OffsetInBlock = Offset % BlockSize; | uint64_t OffsetInBlock = Offset % BlockSize; | ||||
uint32_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock); | uint64_t BytesFromFirstBlock = std::min(Size, BlockSize - OffsetInBlock); | ||||
uint32_t NumAdditionalBlocks = | uint64_t NumAdditionalBlocks = | ||||
alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize; | alignTo(Size - BytesFromFirstBlock, BlockSize) / BlockSize; | ||||
uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1; | uint64_t RequiredContiguousBlocks = NumAdditionalBlocks + 1; | ||||
uint32_t E = StreamLayout.Blocks[BlockNum]; | uint64_t E = StreamLayout.Blocks[BlockNum]; | ||||
for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) { | for (uint64_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) { | ||||
if (StreamLayout.Blocks[I + BlockNum] != E) | if (StreamLayout.Blocks[I + BlockNum] != E) | ||||
return false; | return false; | ||||
} | } | ||||
// Read out the entire block where the requested offset starts. Then drop | // Read out the entire block where the requested offset starts. Then drop | ||||
// bytes from the beginning so that the actual starting byte lines up with | // bytes from the beginning so that the actual starting byte lines up with | ||||
// the requested starting byte. Then, since we know this is a contiguous | // the requested starting byte. Then, since we know this is a contiguous | ||||
// cross-block span, explicitly resize the ArrayRef to cover the entire | // cross-block span, explicitly resize the ArrayRef to cover the entire | ||||
// request length. | // request length. | ||||
ArrayRef<uint8_t> BlockData; | ArrayRef<uint8_t> BlockData; | ||||
uint32_t FirstBlockAddr = StreamLayout.Blocks[BlockNum]; | uint64_t FirstBlockAddr = StreamLayout.Blocks[BlockNum]; | ||||
uint32_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize); | uint64_t MsfOffset = blockToOffset(FirstBlockAddr, BlockSize); | ||||
if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) { | if (auto EC = MsfData.readBytes(MsfOffset, BlockSize, BlockData)) { | ||||
consumeError(std::move(EC)); | consumeError(std::move(EC)); | ||||
return false; | return false; | ||||
} | } | ||||
BlockData = BlockData.drop_front(OffsetInBlock); | BlockData = BlockData.drop_front(OffsetInBlock); | ||||
Buffer = ArrayRef<uint8_t>(BlockData.data(), Size); | Buffer = ArrayRef<uint8_t>(BlockData.data(), Size); | ||||
return true; | return true; | ||||
} | } | ||||
Error MappedBlockStream::readBytes(uint32_t Offset, | Error MappedBlockStream::readBytes(uint64_t Offset, | ||||
MutableArrayRef<uint8_t> Buffer) { | MutableArrayRef<uint8_t> Buffer) { | ||||
uint32_t BlockNum = Offset / BlockSize; | uint64_t BlockNum = Offset / BlockSize; | ||||
uint32_t OffsetInBlock = Offset % BlockSize; | uint64_t OffsetInBlock = Offset % BlockSize; | ||||
// Make sure we aren't trying to read beyond the end of the stream. | // Make sure we aren't trying to read beyond the end of the stream. | ||||
if (auto EC = checkOffsetForRead(Offset, Buffer.size())) | if (auto EC = checkOffsetForRead(Offset, Buffer.size())) | ||||
return EC; | return EC; | ||||
uint32_t BytesLeft = Buffer.size(); | uint64_t BytesLeft = Buffer.size(); | ||||
uint32_t BytesWritten = 0; | uint64_t BytesWritten = 0; | ||||
uint8_t *WriteBuffer = Buffer.data(); | uint8_t *WriteBuffer = Buffer.data(); | ||||
while (BytesLeft > 0) { | while (BytesLeft > 0) { | ||||
uint32_t StreamBlockAddr = StreamLayout.Blocks[BlockNum]; | uint64_t StreamBlockAddr = StreamLayout.Blocks[BlockNum]; | ||||
ArrayRef<uint8_t> BlockData; | ArrayRef<uint8_t> BlockData; | ||||
uint32_t Offset = blockToOffset(StreamBlockAddr, BlockSize); | uint64_t Offset = blockToOffset(StreamBlockAddr, BlockSize); | ||||
if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData)) | if (auto EC = MsfData.readBytes(Offset, BlockSize, BlockData)) | ||||
return EC; | return EC; | ||||
const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock; | const uint8_t *ChunkStart = BlockData.data() + OffsetInBlock; | ||||
uint32_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock); | uint64_t BytesInChunk = std::min(BytesLeft, BlockSize - OffsetInBlock); | ||||
::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk); | ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk); | ||||
BytesWritten += BytesInChunk; | BytesWritten += BytesInChunk; | ||||
BytesLeft -= BytesInChunk; | BytesLeft -= BytesInChunk; | ||||
++BlockNum; | ++BlockNum; | ||||
OffsetInBlock = 0; | OffsetInBlock = 0; | ||||
} | } | ||||
return Error::success(); | return Error::success(); | ||||
} | } | ||||
void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); } | void MappedBlockStream::invalidateCache() { CacheMap.shrink_and_clear(); } | ||||
void MappedBlockStream::fixCacheAfterWrite(uint32_t Offset, | void MappedBlockStream::fixCacheAfterWrite(uint64_t Offset, | ||||
ArrayRef<uint8_t> Data) const { | ArrayRef<uint8_t> Data) const { | ||||
// If this write overlapped a read which previously came from the pool, | // If this write overlapped a read which previously came from the pool, | ||||
// someone may still be holding a pointer to that alloc which is now invalid. | // someone may still be holding a pointer to that alloc which is now invalid. | ||||
// Compute the overlapping range and update the cache entry, so any | // Compute the overlapping range and update the cache entry, so any | ||||
// outstanding buffers are automatically updated. | // outstanding buffers are automatically updated. | ||||
for (const auto &MapEntry : CacheMap) { | for (const auto &MapEntry : CacheMap) { | ||||
// If the end of the written extent precedes the beginning of the cached | // If the end of the written extent precedes the beginning of the cached | ||||
// extent, ignore this map entry. | // extent, ignore this map entry. | ||||
Show All 9 Lines | for (const auto &Alloc : MapEntry.second) { | ||||
Interval WriteInterval = std::make_pair(Offset, Offset + Data.size()); | Interval WriteInterval = std::make_pair(Offset, Offset + Data.size()); | ||||
Interval CachedInterval = | Interval CachedInterval = | ||||
std::make_pair(MapEntry.first, MapEntry.first + Alloc.size()); | std::make_pair(MapEntry.first, MapEntry.first + Alloc.size()); | ||||
// If they overlap, we need to write the new data into the overlapping | // If they overlap, we need to write the new data into the overlapping | ||||
// range. | // range. | ||||
auto Intersection = intersect(WriteInterval, CachedInterval); | auto Intersection = intersect(WriteInterval, CachedInterval); | ||||
assert(Intersection.first <= Intersection.second); | assert(Intersection.first <= Intersection.second); | ||||
uint32_t Length = Intersection.second - Intersection.first; | uint64_t Length = Intersection.second - Intersection.first; | ||||
uint32_t SrcOffset = | uint64_t SrcOffset = | ||||
AbsoluteDifference(WriteInterval.first, Intersection.first); | AbsoluteDifference(WriteInterval.first, Intersection.first); | ||||
uint32_t DestOffset = | uint64_t DestOffset = | ||||
AbsoluteDifference(CachedInterval.first, Intersection.first); | AbsoluteDifference(CachedInterval.first, Intersection.first); | ||||
::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length); | ::memcpy(Alloc.data() + DestOffset, Data.data() + SrcOffset, Length); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
WritableMappedBlockStream::WritableMappedBlockStream( | WritableMappedBlockStream::WritableMappedBlockStream( | ||||
uint32_t BlockSize, const MSFStreamLayout &Layout, | uint32_t BlockSize, const MSFStreamLayout &Layout, | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | if (!Result) | ||||
return Result; | return Result; | ||||
std::vector<uint8_t> InitData(Layout.SB->BlockSize, 0xFF); | std::vector<uint8_t> InitData(Layout.SB->BlockSize, 0xFF); | ||||
BinaryStreamWriter Initializer(*Result); | BinaryStreamWriter Initializer(*Result); | ||||
while (Initializer.bytesRemaining() > 0) | while (Initializer.bytesRemaining() > 0) | ||||
cantFail(Initializer.writeBytes(InitData)); | cantFail(Initializer.writeBytes(InitData)); | ||||
return createStream(Layout.SB->BlockSize, MinLayout, MsfData, Allocator); | return createStream(Layout.SB->BlockSize, MinLayout, MsfData, Allocator); | ||||
} | } | ||||
Error WritableMappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, | Error WritableMappedBlockStream::readBytes(uint64_t Offset, uint64_t Size, | ||||
ArrayRef<uint8_t> &Buffer) { | ArrayRef<uint8_t> &Buffer) { | ||||
return ReadInterface.readBytes(Offset, Size, Buffer); | return ReadInterface.readBytes(Offset, Size, Buffer); | ||||
} | } | ||||
Error WritableMappedBlockStream::readLongestContiguousChunk( | Error WritableMappedBlockStream::readLongestContiguousChunk( | ||||
uint32_t Offset, ArrayRef<uint8_t> &Buffer) { | uint64_t Offset, ArrayRef<uint8_t> &Buffer) { | ||||
return ReadInterface.readLongestContiguousChunk(Offset, Buffer); | return ReadInterface.readLongestContiguousChunk(Offset, Buffer); | ||||
} | } | ||||
uint32_t WritableMappedBlockStream::getLength() { | uint64_t WritableMappedBlockStream::getLength() { | ||||
return ReadInterface.getLength(); | return ReadInterface.getLength(); | ||||
} | } | ||||
Error WritableMappedBlockStream::writeBytes(uint32_t Offset, | Error WritableMappedBlockStream::writeBytes(uint64_t Offset, | ||||
ArrayRef<uint8_t> Buffer) { | ArrayRef<uint8_t> Buffer) { | ||||
// Make sure we aren't trying to write beyond the end of the stream. | // Make sure we aren't trying to write beyond the end of the stream. | ||||
if (auto EC = checkOffsetForWrite(Offset, Buffer.size())) | if (auto EC = checkOffsetForWrite(Offset, Buffer.size())) | ||||
return EC; | return EC; | ||||
uint32_t BlockNum = Offset / getBlockSize(); | uint64_t BlockNum = Offset / getBlockSize(); | ||||
uint32_t OffsetInBlock = Offset % getBlockSize(); | uint64_t OffsetInBlock = Offset % getBlockSize(); | ||||
uint32_t BytesLeft = Buffer.size(); | uint64_t BytesLeft = Buffer.size(); | ||||
uint32_t BytesWritten = 0; | uint64_t BytesWritten = 0; | ||||
while (BytesLeft > 0) { | while (BytesLeft > 0) { | ||||
uint32_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum]; | uint64_t StreamBlockAddr = getStreamLayout().Blocks[BlockNum]; | ||||
uint32_t BytesToWriteInChunk = | uint64_t BytesToWriteInChunk = | ||||
std::min(BytesLeft, getBlockSize() - OffsetInBlock); | std::min(BytesLeft, getBlockSize() - OffsetInBlock); | ||||
const uint8_t *Chunk = Buffer.data() + BytesWritten; | const uint8_t *Chunk = Buffer.data() + BytesWritten; | ||||
ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk); | ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk); | ||||
uint32_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize()); | uint64_t MsfOffset = blockToOffset(StreamBlockAddr, getBlockSize()); | ||||
MsfOffset += OffsetInBlock; | MsfOffset += OffsetInBlock; | ||||
if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData)) | if (auto EC = WriteInterface.writeBytes(MsfOffset, ChunkData)) | ||||
return EC; | return EC; | ||||
BytesLeft -= BytesToWriteInChunk; | BytesLeft -= BytesToWriteInChunk; | ||||
BytesWritten += BytesToWriteInChunk; | BytesWritten += BytesToWriteInChunk; | ||||
++BlockNum; | ++BlockNum; | ||||
OffsetInBlock = 0; | OffsetInBlock = 0; | ||||
} | } | ||||
ReadInterface.fixCacheAfterWrite(Offset, Buffer); | ReadInterface.fixCacheAfterWrite(Offset, Buffer); | ||||
return Error::success(); | return Error::success(); | ||||
} | } | ||||
Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); } | Error WritableMappedBlockStream::commit() { return WriteInterface.commit(); } |