diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index 9d312c56f3..40fb8f8d68 100644 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -1416,7 +1416,7 @@ int CMapReader::PollMapGeneration() int CMapReader::ParseTerrain() { - TIMER(L"ParseTerrain"); + PROFILE2("ParseTerrain"); ScriptRequest rq(pSimulation2->GetScriptInterface()); // parse terrain from map data @@ -1493,7 +1493,7 @@ if (!Script::GetProperty(rq, val, #prop, out))\ int CMapReader::ParseEntities() { - TIMER(L"ParseEntities"); + PROFILE2("ParseEntities"); ScriptRequest rq(pSimulation2->GetScriptInterface()); // parse entities from map data diff --git a/source/gui/GUIManager.cpp b/source/gui/GUIManager.cpp index 78a794f98c..2898fe1320 100644 --- a/source/gui/GUIManager.cpp +++ b/source/gui/GUIManager.cpp @@ -23,7 +23,6 @@ #include "gui/SGUIMessage.h" #include "lib/debug.h" #include "lib/file/vfs/vfs_util.h" -#include "lib/timer.h" #include "lib/utf8.h" #include "ps/CLogger.h" #include "ps/Errors.h" @@ -227,7 +226,6 @@ void CGUIManager::SGUIPage::LoadPage(ScriptContext& scriptContext) PROFILE2("load gui xml"); PROFILE2_ATTR("name: %s", name.c_str()); - TIMER(nameW.c_str()); if (name.back() == '/') { VfsPath currentDirectory = VfsPath("gui") / nameW; diff --git a/source/lib/timer.h b/source/lib/timer.h index d0bf59e613..9ddd05881d 100644 --- a/source/lib/timer.h +++ b/source/lib/timer.h @@ -95,22 +95,6 @@ private: const wchar_t* m_description; }; -/** - * Measures the time taken to execute code up until end of the current scope; - * displays it via debug_printf. Can safely be nested. - * Useful for measuring time spent in a function or basic block. - * must remain valid over the lifetime of this object; - * a string literal is safest. - * - * Example usage: - * void func() - * { - * TIMER(L"description"); - * // code to be measured - * } - **/ -#define TIMER(description) ScopeTimer UID__(description) - /** * Measures the time taken to execute code between BEGIN and END markers; * displays it via debug_printf. Can safely be nested. diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index e1b6f73f0a..ec75d42727 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -106,6 +106,7 @@ CConsole::~CConsole() = default; void CConsole::Init() { + PROFILE2("CConsole::Init"); // Initialise console history file m_MaxHistoryLines = g_ConfigDB.Get("console.history.size", 200); m_HistoryIgnoreDuplicates = g_ConfigDB.Get("console.history.ignore_duplicates", true); diff --git a/source/ps/DllLoader.cpp b/source/ps/DllLoader.cpp index 92d1ac9e1e..3c73eb423b 100644 --- a/source/ps/DllLoader.cpp +++ b/source/ps/DllLoader.cpp @@ -24,6 +24,7 @@ #include "lib/posix/posix_dlfcn.h" #include "lib/sysdep/os.h" #include "lib/timer.h" +#include "ps/Profiler2.h" #include #include @@ -160,7 +161,7 @@ bool DllLoader::LoadDLL() // postcondition: m_Handle valid or == HANDLE_UNAVAILABLE. if (m_Handle == 0) { - TIMER(L"LoadDLL"); + PROFILE2("LoadDLL"); std::stringstream errors; m_Handle = LoadAnyVariant(m_Name, errors); diff --git a/source/ps/GameSetup/Config.cpp b/source/ps/GameSetup/Config.cpp index b9d694259d..24eba67c34 100644 --- a/source/ps/GameSetup/Config.cpp +++ b/source/ps/GameSetup/Config.cpp @@ -20,9 +20,12 @@ #include "Config.h" #include "lib/timer.h" +#include "ps/CConsole.h" +#include "ps/CLogger.h" #include "ps/CStr.h" #include "ps/ConfigDB.h" #include "ps/GameSetup/CmdLineArgs.h" +#include "ps/Profiler2.h" #include #include @@ -100,7 +103,7 @@ static void ProcessCommandLineArgs(const CmdLineArgs& args) void CONFIG_Init(const CmdLineArgs& args) { - TIMER(L"CONFIG_Init"); + PROFILE2("CONFIG_Init"); CConfigDB::Initialise(); diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index 50e08190fc..d13d9b7aad 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -168,7 +168,7 @@ void MountMods(const Paths& paths, const std::vector& mods) void InitVfs(const CmdLineArgs& args) { - TIMER(L"InitVfs"); + PROFILE2("InitVfs"); const Paths paths(args); @@ -208,18 +208,8 @@ void InitVfs(const CmdLineArgs& args) static void InitPs(bool setup_gui, const CStrW& gui_page, ScriptInterface* srcScriptInterface, JS::HandleValue initData) { - { - // console - TIMER(L"ps_console"); - - g_Console->Init(); - } - - // hotkeys - { - TIMER(L"ps_lang_hotkeys"); - LoadHotkeys(g_ConfigDB); - } + g_Console->Init(); + LoadHotkeys(g_ConfigDB); if (!setup_gui) { diff --git a/source/ps/GameSetup/HWDetect.cpp b/source/ps/GameSetup/HWDetect.cpp index 3435f1347a..7fae8a0d48 100644 --- a/source/ps/GameSetup/HWDetect.cpp +++ b/source/ps/GameSetup/HWDetect.cpp @@ -39,6 +39,7 @@ #include "ps/Errors.h" #include "ps/Filesystem.h" #include "ps/GameSetup/Config.h" +#include "ps/Profiler2.h" #include "ps/Pyrogenesis.h" #include "ps/UserReport.h" #include "ps/VideoMode.h" @@ -203,7 +204,7 @@ void ReportLibraries(const ScriptRequest& rq, JS::HandleValue settings) void WriteSystemInfo(Renderer::Backend::IDevice* device, const utsname& un) { - TIMER(L"write_sys_info"); + PROFILE2("WriteSystemInfo"); OsPath pathname = psLogDir() / "system_info.txt"; FILE* f = sys_OpenFile(pathname, "w"); @@ -297,7 +298,7 @@ void RunHardwareDetection(bool writeSystemInfoBeforeDetection, Renderer::Backend if (writeSystemInfoBeforeDetection) WriteSystemInfo(device, un); - TIMER(L"RunHardwareDetection"); + PROFILE2("RunHardwareDetection"); ScriptInterface scriptInterface("Engine", "HWDetect", g_ScriptContext); diff --git a/source/ps/Hotkey.cpp b/source/ps/Hotkey.cpp index 2a22dfe6ba..c7143a61d0 100644 --- a/source/ps/Hotkey.cpp +++ b/source/ps/Hotkey.cpp @@ -27,6 +27,7 @@ #include "ps/ConfigDB.h" #include "ps/Globals.h" #include "ps/KeyName.h" +#include "ps/Profiler2.h" static bool unified[UNIFIED_LAST - UNIFIED_SHIFT]; @@ -133,6 +134,7 @@ static void LoadConfigBindings(CConfigDB& configDB) void LoadHotkeys(CConfigDB& configDB) { + PROFILE2("LoadHotkeys"); pressedHotkeys.clear(); LoadConfigBindings(configDB); } diff --git a/source/ps/Profiler2.cpp b/source/ps/Profiler2.cpp index 875be29745..41bbcbda6e 100644 --- a/source/ps/Profiler2.cpp +++ b/source/ps/Profiler2.cpp @@ -34,6 +34,7 @@ #include "ps/Profiler2.h" #include "ps/Profiler2GPU.h" #include "ps/Pyrogenesis.h" +#include "ps/TaskManager.h" #include "third_party/mongoose/mongoose.h" #include @@ -106,11 +107,17 @@ static void* MgCallback(mg_event event, struct mg_connection *conn, const struct if (uri == "/download") { - profiler->SaveToFile(); + Threading::TaskManager::GetSingleton().PushTask([&] + { + profiler->SaveToFile(); + }).Get(); } else if (uri == "/overview") { - profiler->ConstructJSONOverview(stream); + Threading::TaskManager::GetSingleton().PushTask([&] + { + profiler->ConstructJSONOverview(stream); + }).Get(); } else if (uri == "/query") { @@ -130,7 +137,10 @@ static void* MgCallback(mg_event event, struct mg_connection *conn, const struct } std::string thread(buf); - const char* err = profiler->ConstructJSONResponse(stream, thread); + const char* err = Threading::TaskManager::GetSingleton().PushTask([&] + { + return profiler->ConstructJSONResponse(stream, thread); + }).Get(); if (err) { mg_printf(conn, "%s (%s)", header400, err); @@ -642,7 +652,7 @@ void rewriteBuffer(u8* buffer, u32& bufferSize) void CProfiler2::ConstructJSONOverview(std::ostream& stream) { - TIMER(L"profile2 overview"); + PROFILE2("CProfiler2::ConstructJSONOverview"); std::lock_guard lock(m_Mutex); @@ -665,7 +675,7 @@ void CProfiler2::ConstructJSONOverview(std::ostream& stream) template void RunBufferVisitor(const std::string& buffer, V& visitor) { - TIMER(L"profile2 visitor"); + PROFILE2("Profiler2 RunBufferVisitor"); // The buffer doesn't necessarily start at the beginning of an item // (we just grabbed it from some arbitrary point in the middle), @@ -817,12 +827,12 @@ public: const char* CProfiler2::ConstructJSONResponse(std::ostream& stream, const std::string& thread) { - TIMER(L"profile2 query"); + PROFILE2("CProfiler2::ConstructJSONResponse"); std::string buffer; { - TIMER(L"profile2 get buffer"); + PROFILE2("Profiler2 get buffer"); std::lock_guard lock(m_Mutex); // lock against changes to m_Threads or deletions of ThreadStorage diff --git a/source/ps/SavedGame.cpp b/source/ps/SavedGame.cpp index d62d880ab3..475907138b 100644 --- a/source/ps/SavedGame.cpp +++ b/source/ps/SavedGame.cpp @@ -40,6 +40,7 @@ #include "ps/Filesystem.h" #include "ps/Game.h" #include "ps/Mod.h" +#include "ps/Profiler2.h" #include "ps/Pyrogenesis.h" #include "scriptinterface/JSON.h" #include "scriptinterface/Object.h" @@ -275,7 +276,7 @@ std::optional SavedGames::Load(const ScriptInterface& sc JS::Value SavedGames::GetSavedGames(const ScriptInterface& scriptInterface) { - TIMER(L"GetSavedGames"); + PROFILE2("GetSavedGames"); ScriptRequest rq(scriptInterface); JS::RootedValue games(rq.cx); diff --git a/source/ps/UserReport.cpp b/source/ps/UserReport.cpp index d9647521c0..5226d9bd1c 100644 --- a/source/ps/UserReport.cpp +++ b/source/ps/UserReport.cpp @@ -344,10 +344,6 @@ private: SetStatus("connecting"); -#if DEBUG_UPLOADS - TIMER(L"CUserReporterWorker request"); -#endif - CURLcode err = curl_easy_perform(m_Curl); #if DEBUG_UPLOADS diff --git a/source/ps/VisualReplay.cpp b/source/ps/VisualReplay.cpp index 9da9e8bb2e..3c75b24229 100644 --- a/source/ps/VisualReplay.cpp +++ b/source/ps/VisualReplay.cpp @@ -33,6 +33,7 @@ #include "ps/GameSetup/CmdLineArgs.h" #include "ps/GameSetup/Paths.h" #include "ps/Mod.h" +#include "ps/Profiler2.h" #include "ps/Pyrogenesis.h" #include "ps/Replay.h" #include "ps/Util.h" @@ -116,7 +117,7 @@ void VisualReplay::StoreCacheFile(const ScriptInterface& scriptInterface, JS::Ha JS::HandleObject VisualReplay::ReloadReplayCache(const ScriptInterface& scriptInterface, bool compareFiles) { - TIMER(L"ReloadReplayCache"); + PROFILE2("ReloadReplayCache"); ScriptRequest rq(scriptInterface); // Maps the filename onto the index, mtime and size @@ -236,7 +237,7 @@ JS::HandleObject VisualReplay::ReloadReplayCache(const ScriptInterface& scriptIn JS::Value VisualReplay::GetReplays(const ScriptInterface& scriptInterface, bool compareFiles) { - TIMER(L"GetReplays"); + PROFILE2("GetReplays"); ScriptRequest rq(scriptInterface); JS::RootedObject replays(rq.cx, ReloadReplayCache(scriptInterface, compareFiles)); @@ -459,7 +460,7 @@ JS::Value VisualReplay::GetReplayAttributes(const ScriptInterface& scriptInterfa void VisualReplay::AddReplayToCache(const ScriptInterface& scriptInterface, const CStrW& directoryName) { - TIMER(L"AddReplayToCache"); + PROFILE2("AddReplayToCache"); ScriptRequest rq(scriptInterface); JS::RootedValue replayData(rq.cx, LoadReplayData(scriptInterface, OsPath(directoryName))); diff --git a/source/renderer/Renderer.cpp b/source/renderer/Renderer.cpp index 36412f9fcd..2420a81e4d 100644 --- a/source/renderer/Renderer.cpp +++ b/source/renderer/Renderer.cpp @@ -43,7 +43,6 @@ #include "lib/secure_crt.h" #include "lib/status.h" #include "lib/tex/tex.h" -#include "lib/timer.h" #include "lib/types.h" #include "maths/Matrix3D.h" #include "ps/CConsole.h" @@ -332,7 +331,7 @@ size_t CRenderer::Internals::VertexAttributesHash::operator()( CRenderer::CRenderer(Renderer::Backend::IDevice* device) { - TIMER(L"InitRenderer"); + PROFILE2("InitRenderer"); m = std::make_unique(device); diff --git a/source/simulation2/helpers/LongPathfinder.cpp b/source/simulation2/helpers/LongPathfinder.cpp index 3c96a761e0..9ac979c6a7 100644 --- a/source/simulation2/helpers/LongPathfinder.cpp +++ b/source/simulation2/helpers/LongPathfinder.cpp @@ -298,7 +298,6 @@ public: void reset(const Grid* terrain, pass_class_t passClass) { PROFILE2("JumpPointCache reset"); - TIMER(L"JumpPointCache reset"); m_Width = terrain->m_W; m_Height = terrain->m_H; diff --git a/source/simulation2/system/TurnManager.cpp b/source/simulation2/system/TurnManager.cpp index 68112b0255..cc5fb32342 100644 --- a/source/simulation2/system/TurnManager.cpp +++ b/source/simulation2/system/TurnManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2025 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -23,6 +23,7 @@ #include "maths/MathUtil.h" #include "ps/Pyrogenesis.h" #include "ps/Profile.h" +#include "ps/Profiler2.h" #include "ps/CLogger.h" #include "ps/Replay.h" #include "ps/Util.h" @@ -280,7 +281,7 @@ void CTurnManager::RewindTimeWarp() void CTurnManager::QuickSave(JS::HandleValue GUIMetadata) { - TIMER(L"QuickSave"); + PROFILE2("QuickSave"); std::stringstream stream; if (!m_Simulation2.SerializeState(stream)) @@ -302,7 +303,7 @@ void CTurnManager::QuickSave(JS::HandleValue GUIMetadata) void CTurnManager::QuickLoad() { - TIMER(L"QuickLoad"); + PROFILE2("QuickLoad"); if (m_QuickSaveState.empty()) {