5454#include " llvm/CAS/MappedFileRegionBumpPtr.h"
5555#include " OnDiskCommon.h"
5656#include " llvm/ADT/StringMap.h"
57+ #include " llvm/CAS/OnDiskCASLogger.h"
5758
5859using namespace llvm ;
5960using namespace llvm ::cas;
@@ -89,9 +90,11 @@ struct FileLockRAII {
8990
9091Expected<MappedFileRegionBumpPtr> MappedFileRegionBumpPtr::create (
9192 const Twine &Path, uint64_t Capacity, int64_t BumpPtrOffset,
93+ std::shared_ptr<ondisk::OnDiskCASLogger> Logger,
9294 function_ref<Error(MappedFileRegionBumpPtr &)> NewFileConstructor) {
9395 MappedFileRegionBumpPtr Result;
9496 Result.Path = Path.str ();
97+ Result.Logger = std::move (Logger);
9598 // Open the main file.
9699 int FD;
97100 if (std::error_code EC = sys::fs::openFileForReadWrite (
@@ -147,6 +150,10 @@ Expected<MappedFileRegionBumpPtr> MappedFileRegionBumpPtr::create(
147150 // to make this a sparse region, if supported.
148151 if (std::error_code EC = sys::fs::resize_file (FD, Capacity))
149152 return createFileError (Result.Path , EC);
153+
154+ if (Result.Logger )
155+ Result.Logger ->log_MappedFileRegionBumpPtr_resizeFile (
156+ Result.Path , Status.getSize (), Capacity);
150157 } else {
151158 // Someone else initialized it.
152159 Capacity = Status.getSize ();
@@ -186,9 +193,14 @@ void MappedFileRegionBumpPtr::destroyImpl() {
186193 if (BumpPtr) {
187194 assert (SharedLockFD && " Must have shared lock file open" );
188195 if (tryLockFileThreadSafe (*SharedLockFD) == std::error_code ()) {
189- assert (size () <= capacity ());
196+ size_t Size = size ();
197+ size_t Capacity = capacity ();
198+ assert (Size < Capacity);
190199 (void )sys::fs::resize_file (*FD, size ());
191200 (void )unlockFileThreadSafe (*SharedLockFD);
201+
202+ if (Logger)
203+ Logger->log_MappedFileRegionBumpPtr_resizeFile (Path, Capacity, Size);
192204 }
193205 }
194206
@@ -203,6 +215,9 @@ void MappedFileRegionBumpPtr::destroyImpl() {
203215 // Close the file and shared lock.
204216 Close (FD);
205217 Close (SharedLockFD);
218+
219+ if (Logger)
220+ Logger->log_MappedFileRegionBumpPtr_close (Path);
206221}
207222
208223void MappedFileRegionBumpPtr::initializeBumpPtr (int64_t BumpPtrOffset) {
@@ -218,6 +233,10 @@ void MappedFileRegionBumpPtr::initializeBumpPtr(int64_t BumpPtrOffset) {
218233 if (!BumpPtr->compare_exchange_strong (ExistingValue, BumpPtrEndOffset))
219234 assert (ExistingValue >= BumpPtrEndOffset &&
220235 " Expected 0, or past the end of the BumpPtr itself" );
236+
237+ if (Logger)
238+ Logger->log_MappedFileRegionBumpPtr_create (Path, *FD, data (), capacity (),
239+ size ());
221240}
222241
223242static Error createAllocatorOutOfSpaceError () {
@@ -239,7 +258,15 @@ Expected<int64_t> MappedFileRegionBumpPtr::allocateOffset(uint64_t AllocSize) {
239258 if (OldEnd <= (int64_t )capacity ())
240259 (void )BumpPtr->exchange (OldEnd);
241260
261+ if (Logger)
262+ Logger->log_MappedFileRegionBumpPtr_oom (Path, capacity (), OldEnd,
263+ AllocSize);
264+
242265 return createAllocatorOutOfSpaceError ();
243266 }
267+
268+ if (Logger)
269+ Logger->log_MappedFileRegionBumpPtr_allocate (data (), OldEnd, AllocSize);
270+
244271 return OldEnd;
245272}
0 commit comments