using System; namespace RynthCore.Engine; /// /// Whether a full-memory dump may be written now. Pure arithmetic (the disk and folder are read /// by ), so tools\EngineRobustnessTests runs it directly. /// /// A full dump is about the size of the process (a 32-bit client: up to ~4 GB), the client is /// frozen while it is written, and every client that hits the OUT OF MEMORY box at once writes /// one. So: the volume must keep a margin free afterwards, and the dumps already in the folder /// (all clients, live or not) are capped in number and in bytes. When the verdict is not /// the caller writes the small dump instead. /// internal enum DumpVerdict { Full = 0, /// Free space unknown, or less than the dump plus . LowDisk, /// or more full dumps are already in the folder. TooManyDumps, /// The full dumps in the folder plus this one would pass . TooManyBytes, } internal static class DumpBudget { /// Free space that must remain on the volume after a full dump. internal const long MinFreeAfterFullDump = 2L * 1024 * 1024 * 1024; /// Full dumps in the folder, all clients together, before another is refused. internal const int MaxFullDumpsAllPids = 4; /// Bytes of full dumps in the folder, all clients together, including the one about to be written. internal const long MaxFullDumpBytesAllPids = 14L * 1024 * 1024 * 1024; /// A full dump holds the private memory plus the mapped images and the dump's own tables. internal const long FullDumpOverheadBytes = 512L * 1024 * 1024; /// The small (Normal + handle data) dump is tens of MB; it still needs some room. internal const long SmallDumpMinFree = 200L * 1024 * 1024; /// Largest a 32-bit client can be; the estimate when the process size cannot be read. internal const long FallbackProcessBytes = 4L * 1024 * 1024 * 1024; internal static long EstimateFullDumpBytes(long privateBytes) => (privateBytes > 0 ? privateBytes : FallbackProcessBytes) + FullDumpOverheadBytes; /// Free bytes available to this user on the dump volume; negative = unknown. /// The size this dump is expected to have. /// Full dumps already in the folder (any client, any state). /// Their combined size. internal static DumpVerdict Evaluate(long freeBytes, long estimateBytes, int existingCount, long existingBytes) { if (freeBytes < 0 || freeBytes - estimateBytes < MinFreeAfterFullDump) return DumpVerdict.LowDisk; if (existingCount >= MaxFullDumpsAllPids) return DumpVerdict.TooManyDumps; if (existingBytes + estimateBytes > MaxFullDumpBytesAllPids) return DumpVerdict.TooManyBytes; return DumpVerdict.Full; } internal static bool SmallDumpAllowed(long freeBytes) => freeBytes >= SmallDumpMinFree; }