mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/LCE-Revelations.git
synced 2026-06-07 01:35:28 +00:00
perf: async autosave for dedicated server
Autosave previously froze the main thread for 2-6 seconds while compressing the entire save file with zlib. Now the save buffer is snapshotted under the lock (~18ms), then compression runs on a background thread. The compressed data is committed to StorageManager on the next main-thread tick via CommitPendingAsyncSave(). Also skip redundant full chunk saves during autosave on the dedicated server -- chunks are already persisted by the per-tick trickle save. Only entity data is flushed, matching Xbox/Orbis behavior. Added per-step timing to the autosave handler for diagnostics.
This commit is contained in:
@@ -12,6 +12,27 @@
|
||||
#include "..\Minecraft.Client\Common\GameRules\LevelGenerationOptions.h"
|
||||
#include "..\Minecraft.World\net.minecraft.world.level.chunk.storage.h"
|
||||
|
||||
#ifdef _WINDOWS64
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
extern bool g_Win64DedicatedServer;
|
||||
static std::atomic<bool> s_asyncSaveInFlight{false};
|
||||
|
||||
// Pending async save: background thread fills this, main thread commits it.
|
||||
struct PendingAsyncSave
|
||||
{
|
||||
ConsoleSaveFile *self;
|
||||
PBYTE thumbData;
|
||||
DWORD thumbSize;
|
||||
BYTE textMetadata[88];
|
||||
int textMetadataBytes;
|
||||
bool ready;
|
||||
};
|
||||
static std::mutex s_pendingSaveMutex;
|
||||
static PendingAsyncSave s_pendingSave = {};
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _XBOX
|
||||
#define RESERVE_ALLOCATION MEM_RESERVE | MEM_LARGE_PAGES
|
||||
@@ -673,6 +694,138 @@ void ConsoleSaveFileOriginal::Flush(bool autosave, bool updateThumbnail )
|
||||
|
||||
unsigned int fileSize = header.GetFileSize();
|
||||
|
||||
#ifdef _WINDOWS64
|
||||
// --- Dedicated server async flush path ---
|
||||
// Snapshot pvSaveMem while holding the lock (fast memcpy), then release
|
||||
// the lock immediately so the main thread can continue ticking. Compression
|
||||
// and disk write happen on a detached background thread.
|
||||
if (g_Win64DedicatedServer)
|
||||
{
|
||||
// If a previous async save is still compressing, fall through to the
|
||||
// synchronous path to avoid queuing unbounded background work.
|
||||
if (s_asyncSaveInFlight.load(std::memory_order_acquire))
|
||||
{
|
||||
app.DebugPrintf("Async save: previous still in flight, falling back to sync\n");
|
||||
goto sync_flush;
|
||||
}
|
||||
|
||||
// Snapshot: copy the entire save buffer so we can release the lock
|
||||
QueryPerformanceCounter(&qwTime);
|
||||
byte *snapshot = new (std::nothrow) byte[fileSize];
|
||||
if (snapshot == nullptr)
|
||||
{
|
||||
app.DebugPrintf("Async save: failed to allocate %u byte snapshot, falling back to sync\n", fileSize);
|
||||
goto sync_flush;
|
||||
}
|
||||
memcpy(snapshot, pvSaveMem, fileSize);
|
||||
QueryPerformanceCounter(&qwNewTime);
|
||||
qwDeltaTime.QuadPart = qwNewTime.QuadPart - qwTime.QuadPart;
|
||||
fElapsedTime = fSecsPerTick * static_cast<FLOAT>(qwDeltaTime.QuadPart);
|
||||
app.DebugPrintf("Async save: snapshot %u bytes in %.3f sec\n", fileSize, fElapsedTime);
|
||||
|
||||
// Gather metadata while still on the main thread
|
||||
PBYTE pbThumbnailData = nullptr;
|
||||
DWORD dwThumbnailDataSize = 0;
|
||||
app.GetSaveThumbnail(&pbThumbnailData, &dwThumbnailDataSize);
|
||||
|
||||
BYTE bTextMetadata[88];
|
||||
ZeroMemory(bTextMetadata, 88);
|
||||
int64_t seed = 0;
|
||||
bool hasSeed = false;
|
||||
if (MinecraftServer::getInstance() != nullptr && MinecraftServer::getInstance()->levels[0] != nullptr)
|
||||
{
|
||||
seed = MinecraftServer::getInstance()->levels[0]->getLevelData()->getSeed();
|
||||
hasSeed = true;
|
||||
}
|
||||
int iTextMetadataBytes = app.CreateImageTextData(bTextMetadata, seed, hasSeed,
|
||||
app.GetGameHostOption(eGameHostOption_All), Minecraft::GetInstance()->getCurrentTexturePackId());
|
||||
|
||||
INT saveOrCheckpointId = 0;
|
||||
StorageManager.GetSaveUniqueNumber(&saveOrCheckpointId);
|
||||
TelemetryManager->RecordLevelSaveOrCheckpoint(ProfileManager.GetPrimaryPad(), saveOrCheckpointId, fileSize);
|
||||
|
||||
// Release the lock -- main thread and chunk trickle saves can resume
|
||||
ReleaseSaveAccess();
|
||||
|
||||
// Compress and write on a background thread.
|
||||
// Pack metadata into a heap struct so the lambda captures a single
|
||||
// owning pointer instead of stack arrays that go out of scope.
|
||||
struct AsyncSaveContext
|
||||
{
|
||||
byte *snapshot;
|
||||
unsigned int fileSize;
|
||||
ConsoleSaveFile *self;
|
||||
PBYTE thumbData;
|
||||
DWORD thumbSize;
|
||||
BYTE textMetadata[88];
|
||||
int textMetadataBytes;
|
||||
};
|
||||
|
||||
auto *ctx = new AsyncSaveContext();
|
||||
ctx->snapshot = snapshot;
|
||||
ctx->fileSize = fileSize;
|
||||
ctx->self = this;
|
||||
ctx->thumbData = pbThumbnailData;
|
||||
ctx->thumbSize = dwThumbnailDataSize;
|
||||
memcpy(ctx->textMetadata, bTextMetadata, 88);
|
||||
ctx->textMetadataBytes = iTextMetadataBytes;
|
||||
|
||||
s_asyncSaveInFlight.store(true, std::memory_order_release);
|
||||
|
||||
std::thread([ctx]()
|
||||
{
|
||||
unsigned int compLength = ctx->fileSize + 8;
|
||||
byte *compData = static_cast<byte *>(StorageManager.AllocateSaveData(compLength));
|
||||
if (compData == nullptr)
|
||||
{
|
||||
// Pre-calculate compressed size
|
||||
compLength = 0;
|
||||
Compression::getCompression()->Compress(nullptr, &compLength, ctx->snapshot, ctx->fileSize);
|
||||
compLength += 8;
|
||||
compData = static_cast<byte *>(StorageManager.AllocateSaveData(compLength));
|
||||
}
|
||||
|
||||
if (compData != nullptr)
|
||||
{
|
||||
Compression::getCompression()->Compress(compData + 8, &compLength, ctx->snapshot, ctx->fileSize);
|
||||
|
||||
ZeroMemory(compData, 8);
|
||||
int saveVer = 0;
|
||||
memcpy(compData, &saveVer, sizeof(int));
|
||||
unsigned int fs = ctx->fileSize;
|
||||
memcpy(compData + 4, &fs, sizeof(int));
|
||||
|
||||
app.DebugPrintf("Async save: compressed %u -> %u bytes\n", ctx->fileSize, compLength);
|
||||
|
||||
// Queue for the main thread to commit via StorageManager
|
||||
// (StorageManager requires main-thread calls + Tick() to flush)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(s_pendingSaveMutex);
|
||||
s_pendingSave.self = ctx->self;
|
||||
s_pendingSave.thumbData = ctx->thumbData;
|
||||
s_pendingSave.thumbSize = ctx->thumbSize;
|
||||
memcpy(s_pendingSave.textMetadata, ctx->textMetadata, 88);
|
||||
s_pendingSave.textMetadataBytes = ctx->textMetadataBytes;
|
||||
s_pendingSave.ready = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
app.DebugPrintf("Async save: failed to allocate compression buffer\n");
|
||||
s_asyncSaveInFlight.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
delete[] ctx->snapshot;
|
||||
delete ctx;
|
||||
}).detach();
|
||||
|
||||
return;
|
||||
}
|
||||
sync_flush:
|
||||
#endif
|
||||
|
||||
// --- Original synchronous flush path (game client / non-server) ---
|
||||
|
||||
// Assume that the compression will make it smaller so initially attempt to allocate the current file size
|
||||
// We add 4 bytes to the start so that we can signal compressed data
|
||||
// And another 4 bytes to store the decompressed data size
|
||||
@@ -1130,3 +1283,21 @@ void *ConsoleSaveFileOriginal::getWritePointer(FileEntry *file)
|
||||
{
|
||||
return static_cast<char *>(pvSaveMem) + file->currentFilePointer;;
|
||||
}
|
||||
|
||||
#ifdef _WINDOWS64
|
||||
void ConsoleSaveFileOriginal::CommitPendingAsyncSave()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(s_pendingSaveMutex);
|
||||
if (!s_pendingSave.ready)
|
||||
return;
|
||||
|
||||
StorageManager.SetSaveImages(
|
||||
s_pendingSave.thumbData, s_pendingSave.thumbSize,
|
||||
nullptr, 0, s_pendingSave.textMetadata, s_pendingSave.textMetadataBytes);
|
||||
StorageManager.SaveSaveData(
|
||||
&ConsoleSaveFileOriginal::SaveSaveDataCallback, s_pendingSave.self);
|
||||
|
||||
s_pendingSave.ready = false;
|
||||
s_asyncSaveInFlight.store(false, std::memory_order_release);
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user