refactor: consolidate timing utilities into header-only Timer.h

This commit is contained in:
MatthewBeshay
2026-04-03 18:57:29 +11:00
parent 98e23cfd4d
commit 5bbb045e86
19 changed files with 214 additions and 203 deletions
+2 -3
View File
@@ -271,7 +271,6 @@ Minecraft::Minecraft(Component* mouseComponent, Canvas* parent,
levelTickEventQueue =
new C4JThread::EventQueue(levelTickUpdateFunc, levelTickThreadInitFunc,
"LevelTick_EventQueuePoll");
levelTickEventQueue->setProcessor(3);
levelTickEventQueue->setPriority(C4JThread::ThreadPriority::Normal);
#endif
}
@@ -1146,7 +1145,7 @@ void Minecraft::run_middle() {
}
#endif
} else {
unsigned int uiTimeToAutosave =
int64_t uiTimeToAutosave =
app.SecondsToAutosave();
if (uiTimeToAutosave < 6) {
@@ -3960,7 +3959,7 @@ void Minecraft::fileDownloaded(const std::wstring& name, File* file) {
std::wstring Minecraft::gatherStats1() {
// return levelRenderer->gatherStats1();
return L"Time to autosave: " +
_toString<unsigned int>(app.SecondsToAutosave()) + L"s";
_toString<int64_t>(app.SecondsToAutosave()) + L"s";
}
std::wstring Minecraft::gatherStats2() {
@@ -35,7 +35,7 @@
#include "app/linux/Stubs/winapi_stubs.h"
#include "MultiPlayerLevel.h"
#include "ReceivingLevelScreen.h"
#include "console_helpers/PlatformTime.h"
#include "console_helpers/Timer.h"
#include "console_helpers/StringHelpers.h"
#include "java/Class.h"
#include "java/InputOutputStream/ByteArrayInputStream.h"
@@ -3575,8 +3575,8 @@ void ClientConnection::checkDeferredEntityLinkPackets(int newEntityId) {
bool remove = false;
// Only consider recently deferred packets
int tickInterval =
PlatformTime::GetTickCount() - deferred->m_recievedTick;
auto tickInterval =
std::chrono::duration_cast<std::chrono::milliseconds>(time_util::clock::now() - deferred->m_recievedTick).count();
if (tickInterval < MAX_ENTITY_LINK_DEFERRAL_INTERVAL) {
// Note: we assume it's the destination entity
if (deferred->m_packet->destId == newEntityId) {
@@ -3599,6 +3599,6 @@ void ClientConnection::checkDeferredEntityLinkPackets(int newEntityId) {
ClientConnection::DeferredEntityLinkPacket::DeferredEntityLinkPacket(
std::shared_ptr<SetEntityLinkPacket> packet) {
m_recievedTick = PlatformTime::GetTickCount();
m_recievedTick = time_util::clock::now();
m_packet = packet;
}
@@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include "console_helpers/Timer.h"
#include "platform/sdl2/Storage.h"
#include "minecraft/network/Connection.h"
@@ -216,7 +217,7 @@ private:
// 4J: Entity link packet deferred
class DeferredEntityLinkPacket {
public:
uint32_t m_recievedTick;
time_util::time_point m_recievedTick;
std::shared_ptr<SetEntityLinkPacket> m_packet;
DeferredEntityLinkPacket(std::shared_ptr<SetEntityLinkPacket> packet);
+11 -18
View File
@@ -23,7 +23,7 @@
#include "app/linux/Linux_App.h"
#include "PlayerList.h"
#include "Settings.h"
#include "console_helpers/PlatformTime.h"
#include "console_helpers/Timer.h"
#include "java/Class.h"
#include "java/File.h"
#include "java/InputOutputStream/DataOutputStream.h"
@@ -102,7 +102,7 @@ int64_t MinecraftServer::s_tickStartTime = 0;
std::vector<INetworkPlayer*> MinecraftServer::s_sentTo;
#else
int MinecraftServer::s_slowQueuePlayerIndex = 0;
int MinecraftServer::s_slowQueueLastTime = 0;
time_util::time_point MinecraftServer::s_slowQueueLastTime = {};
bool MinecraftServer::s_slowQueuePacketSent = false;
#endif
@@ -528,7 +528,6 @@ bool MinecraftServer::loadLevel(LevelStorageSource* storageSource,
new C4JThread(runPostUpdate, this, "Post processing", 256 * 1024);
m_postUpdateTerminate = false;
m_postUpdateThread->setProcessor(CPU_CORE_POST_PROCESSING);
m_postUpdateThread->setPriority(C4JThread::ThreadPriority::AboveNormal);
m_postUpdateThread->run();
@@ -858,10 +857,7 @@ void MinecraftServer::saveGameRules() {
void MinecraftServer::Suspend() {
m_suspending = true;
// Get the frequency of the timer
float fElapsedTime = 0.0f;
// Save the start time
auto qwTime = PlatformTime::QueryPerformanceCounter();
time_util::Timer timer;
if (m_bLoaded && (!StorageManager.GetSaveDisabled())) {
if (players != nullptr) {
players->saveAll(nullptr);
@@ -880,13 +876,10 @@ void MinecraftServer::Suspend() {
levels[0]->saveToDisc(nullptr, true);
}
}
auto qwNewTime = PlatformTime::QueryPerformanceCounter();
fElapsedTime =
static_cast<float>(PlatformTime::ElapsedSeconds(qwTime, qwNewTime));
m_suspending = false;
app.DebugPrintf("Suspend server: Elapsed time %f\n", fElapsedTime);
app.DebugPrintf("Suspend server: Elapsed time %f\n",
static_cast<float>(timer.elapsed_seconds()));
}
bool MinecraftServer::IsSuspending() { return m_suspending; }
@@ -1644,9 +1637,9 @@ void MinecraftServer::chunkPacketManagement_PostTick() {}
bool MinecraftServer::chunkPacketManagement_CanSendTo(INetworkPlayer* player) {
if (player == nullptr) return false;
int time = PlatformTime::GetTickCount();
auto now = time_util::clock::now();
if (player->GetSessionIndex() == s_slowQueuePlayerIndex &&
(time - s_slowQueueLastTime) > MINECRAFT_SERVER_SLOW_QUEUE_DELAY) {
(now - s_slowQueueLastTime) > std::chrono::milliseconds(MINECRAFT_SERVER_SLOW_QUEUE_DELAY)) {
// app.DebugPrintf("Slow queue OK for player #%d\n",
// player->GetSessionIndex());
return true;
@@ -1664,15 +1657,15 @@ void MinecraftServer::chunkPacketManagement_PreTick() {}
void MinecraftServer::chunkPacketManagement_PostTick() {
// 4J Ensure that the slow queue owner keeps cycling if it's not been used
// in a while
int time = PlatformTime::GetTickCount();
if ((s_slowQueuePacketSent) || ((time - s_slowQueueLastTime) >
(2 * MINECRAFT_SERVER_SLOW_QUEUE_DELAY))) {
auto now = time_util::clock::now();
if ((s_slowQueuePacketSent) || ((now - s_slowQueueLastTime) >
std::chrono::milliseconds(2 * MINECRAFT_SERVER_SLOW_QUEUE_DELAY))) {
// app.DebugPrintf("Considering cycling: (%d) %d - %d -> %d
//> %d\n",s_slowQueuePacketSent, time, s_slowQueueLastTime, (time -
// s_slowQueueLastTime), (2*MINECRAFT_SERVER_SLOW_QUEUE_DELAY));
MinecraftServer::cycleSlowQueueIndex();
s_slowQueuePacketSent = false;
s_slowQueueLastTime = time;
s_slowQueueLastTime = now;
}
// else
// {
+2 -1
View File
@@ -8,6 +8,7 @@
#include "ConsoleInputSource.h"
#include "console_helpers/C4JThread.h"
#include "console_helpers/Timer.h"
#include "minecraft/SharedConstants.h"
#include "minecraft/world/level/chunk/ChunkSource.h"
#include "minecraft/world/level/storage/ConsoleSaveFileIO/FileHeader.h"
@@ -293,7 +294,7 @@ private:
static const int MAX_TICK_TIME_FOR_PACKET_SENDS = 35;
#else
static int s_slowQueuePlayerIndex;
static int s_slowQueueLastTime;
static time_util::time_point s_slowQueueLastTime;
static bool s_slowQueuePacketSent;
#endif
@@ -7,7 +7,7 @@
#include "app/common/src/GameRules/LevelGeneration/LevelGenerationOptions.h"
#include "app/linux/Linux_App.h"
#include "console_helpers/PlatformTime.h"
#include "console_helpers/Timer.h"
#include "java/Random.h"
#include "minecraft/util/Mth.h"
#include "minecraft/world/entity/MobCategory.h"
@@ -102,8 +102,8 @@ RandomLevelSource::~RandomLevelSource() {
}
int g_numPrepareHeightCalls = 0;
std::int64_t g_totalPrepareHeightsTime = 0;
std::int64_t g_averagePrepareHeightsTime = 0;
time_util::clock::duration g_totalPrepareHeightsTime{};
time_util::clock::duration g_averagePrepareHeightsTime{};
#if defined(_LARGE_WORLDS)
@@ -243,7 +243,6 @@ float RandomLevelSource::getHeightFalloff(int xxx, int zzz, int* pEMin) {
void RandomLevelSource::prepareHeights(int xOffs, int zOffs,
std::vector<uint8_t>& blocks) {
std::int64_t startTime;
int xChunks = 16 / CHUNK_WIDTH;
int yChunks = Level::genDepth / CHUNK_HEIGHT;
int waterHeight = level->seaLevel;
@@ -265,7 +264,7 @@ void RandomLevelSource::prepareHeights(int xOffs, int zOffs,
buffer = getHeights(buffer, xOffs * xChunks, 0, zOffs * xChunks, xSize,
ySize, zSize, biomes);
startTime = PlatformTime::QueryPerformanceCounter();
time_util::Timer timer;
for (int xc = 0; xc < xChunks; xc++) {
for (int zc = 0; zc < xChunks; zc++) {
for (int yc = 0; yc < yChunks; yc++) {
@@ -377,10 +376,8 @@ void RandomLevelSource::prepareHeights(int xOffs, int zOffs,
}
}
}
auto endTime = PlatformTime::QueryPerformanceCounter();
auto timeInFunc = endTime - startTime;
g_numPrepareHeightCalls++;
g_totalPrepareHeightsTime += timeInFunc;
g_totalPrepareHeightsTime += timer.elapsed();
g_averagePrepareHeightsTime =
g_totalPrepareHeightsTime / g_numPrepareHeightCalls;
}
@@ -19,7 +19,7 @@
#include "app/common/src/GameRules/LevelGeneration/LevelGenerationOptions.h"
#include "app/linux/Linux_App.h"
#include "app/linux/Stubs/winapi_stubs.h"
#include "console_helpers/PlatformTime.h"
#include "console_helpers/Timer.h"
#include "console_helpers/StringHelpers.h"
#include "console_helpers/compression.h"
#include "java/File.h"
@@ -1282,10 +1282,7 @@ void ConsoleSaveFileSplit::Flush(bool autosave, bool updateThumbnail) {
m_autosave = autosave;
if (!m_autosave) processSubfilesForWrite();
// Get the frequency of the timer
auto qwTime = PlatformTime::QueryPerformanceCounter();
auto qwNewTime = qwTime;
float fElapsedTime = 0.0f;
time_util::Timer timer;
unsigned int fileSize = header.GetFileSize();
@@ -1311,16 +1308,12 @@ void ConsoleSaveFileSplit::Flush(bool autosave, bool updateThumbnail) {
compLength = 0;
// Pre-calculate the buffer size required for the compressed data
// Save the start time
qwTime = PlatformTime::QueryPerformanceCounter();
timer.reset();
Compression::getCompression()->Compress(nullptr, &compLength, pvSaveMem,
fileSize);
qwNewTime = PlatformTime::QueryPerformanceCounter();
fElapsedTime =
static_cast<float>(PlatformTime::ElapsedSeconds(qwTime, qwNewTime));
app.DebugPrintf("Check buffer size: Elapsed time %f\n", fElapsedTime);
app.DebugPrintf("Check buffer size: Elapsed time %f\n",
static_cast<float>(timer.elapsed_seconds()));
// We add 4 bytes to the start so that we can signal compressed data
// And another 4 bytes to store the decompressed data size
@@ -1332,16 +1325,12 @@ void ConsoleSaveFileSplit::Flush(bool autosave, bool updateThumbnail) {
if (compData != nullptr) {
// Re-compress all save data before we save it to disk
// Save the start time
qwTime = PlatformTime::QueryPerformanceCounter();
timer.reset();
Compression::getCompression()->Compress(compData + 8, &compLength,
pvSaveMem, fileSize);
qwNewTime = PlatformTime::QueryPerformanceCounter();
fElapsedTime =
static_cast<float>(PlatformTime::ElapsedSeconds(qwTime, qwNewTime));
app.DebugPrintf("Compress: Elapsed time %f\n", fElapsedTime);
app.DebugPrintf("Compress: Elapsed time %f\n",
static_cast<float>(timer.elapsed_seconds()));
memset(compData, 0, 8);
int saveVer = 0;