@@ -1259,12 +1259,33 @@ Error OnDiskGraphDB::store(ObjectID ID, ArrayRef<ObjectID> Refs,
12591259 SmallString<256 > Path;
12601260 std::optional<MappedTempFile> File;
12611261 std::optional<uint64_t > FileSize;
1262+ auto AllocStandaloneFile = [&](size_t Size) -> Expected<char *> {
1263+ getStandalonePath (TrieRecord::getStandaloneFileSuffix (
1264+ TrieRecord::StorageKind::Standalone),
1265+ I, Path);
1266+ if (Error E = createTempFile (Path, Size).moveInto (File))
1267+ return std::move (E);
1268+ assert (File->size () == Size);
1269+ FileSize = Size;
1270+ SK = TrieRecord::StorageKind::Standalone;
1271+ return File->data ();
1272+ };
12621273 auto Alloc = [&](size_t Size) -> Expected<char *> {
12631274 if (Size <= TrieRecord::MaxEmbeddedSize) {
12641275 SK = TrieRecord::StorageKind::DataPool;
12651276 auto P = DataPool.allocate (Size);
1266- if (LLVM_UNLIKELY (!P))
1267- return P.takeError ();
1277+ if (LLVM_UNLIKELY (!P)) {
1278+ char *NewAlloc = nullptr ;
1279+ auto NewE = handleErrors (
1280+ P.takeError (), [&](std::unique_ptr<StringError> E) -> Error {
1281+ if (E->convertToErrorCode () == std::errc::not_enough_memory)
1282+ return AllocStandaloneFile (Size).moveInto (NewAlloc);
1283+ return Error (std::move (E));
1284+ });
1285+ if (!NewE)
1286+ return NewAlloc;
1287+ return std::move (NewE);
1288+ }
12681289 PoolOffset = P->getOffset ();
12691290 LLVM_DEBUG ({
12701291 dbgs () << " pool-alloc addr=" << (void *)PoolOffset.get ()
@@ -1273,15 +1294,9 @@ Error OnDiskGraphDB::store(ObjectID ID, ArrayRef<ObjectID> Refs,
12731294 });
12741295 return (*P)->data ();
12751296 }
1276-
1277- SK = TrieRecord::StorageKind::Standalone;
1278- getStandalonePath (TrieRecord::getStandaloneFileSuffix (SK), I, Path);
1279- if (Error E = createTempFile (Path, Size).moveInto (File))
1280- return std::move (E);
1281- assert (File->size () == Size);
1282- FileSize = Size;
1283- return File->data ();
1297+ return AllocStandaloneFile (Size);
12841298 };
1299+
12851300 DataRecordHandle Record;
12861301 if (Error E =
12871302 DataRecordHandle::createWithError (Alloc, Input).moveInto (Record))
@@ -1350,6 +1365,12 @@ size_t OnDiskGraphDB::getStorageSize() const {
13501365 return Index.size () + DataPool.size () + getStandaloneStorageSize ();
13511366}
13521367
1368+ unsigned OnDiskGraphDB::getHardStorageLimitUtilization () const {
1369+ unsigned IndexPercent = Index.size () * 100ULL / Index.capacity ();
1370+ unsigned DataPercent = DataPool.size () * 100ULL / DataPool.capacity ();
1371+ return std::max (IndexPercent, DataPercent);
1372+ }
1373+
13531374static bool useSmallMappedFiles (const Twine &P) {
13541375 // macOS tmpfs does not support sparse tails.
13551376#if defined(__APPLE__) && __has_include(<sys/mount.h>)
0 commit comments