Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate tracy #801

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ endif()
option(STANDALONE "Build a standalone version." ${DEFAULT_STANDALONE})
option(USE_GAMEMATH "Use own maths library rather than libc version for this platform." ON)
option(USE_FFMPEG "Use FFMPEG for codec handling." ${DEFAULT_FFMPEG})
option(USE_TRACY "Use Tracy for profiling." OFF)
option(LOGGING "Enable debug logging." ${DEFAULT_LOGGING})
option(ASSERTIONS "Enable debug assertions." ${DEFAULT_ASSERTIONS})
option(USE_CRASHPAD "Enable the use of the Crashpad library for crash handling and reporting." OFF)
Expand Down Expand Up @@ -258,6 +259,19 @@ if(USE_FFMPEG)
find_package(FFmpeg COMPONENTS AVFORMAT AVCODEC AVUTIL REQUIRED)
endif()

if(USE_TRACY)
include(FetchContent)

FetchContent_Declare(
tracy
GIT_REPOSITORY https://github.com/wolfpld/tracy.git
GIT_TAG v0.9
)

# We don't use FetchContent_MakeAvailable here because we don't want all crashpad targets including, just our dependencies.
FetchContent_MakeAvailable(tracy)
endif()

if(USE_CRASHPAD)
include(FetchContent)

Expand Down
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ set(GAMEENGINE_INCLUDES
w3d/math
w3d/renderer
w3d/saveload
# our own code
dbg/
)

set(HOOKER_SRC
Expand Down Expand Up @@ -474,6 +476,11 @@ if(USE_FFMPEG)
list(APPEND GAME_COMPILE_OPTIONS -DBUILD_WITH_FFMPEG ${FFMPEG_DEFINITIONS})
endif()

if(USE_TRACY)
list(APPEND GAME_LINK_LIBRARIES Tracy::TracyClient)
list(APPEND GAME_COMPILE_OPTIONS -DBUILD_WITH_TRACY)
endif()

if(DINPUT8_FOUND)
list(APPEND GAMEENGINE_GAME_SRC
platform/directx/dinputkeybd.cpp
Expand Down
38 changes: 38 additions & 0 deletions src/dbg/profiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @file
*
* @author feliwir
*
* @brief Profiler abstraction header.
*
* @copyright Thyme is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version
* 2 of the License, or (at your option) any later version.
* A full copy of the GNU General Public License can be found in
* LICENSE
*/
#pragma once

#include "always.h"

#ifdef BUILD_WITH_TRACY
#define USE_PROFILER
#include <tracy/Tracy.hpp>

// Misc
#define PROFILER_MSG(M) TracyMessage(M, strlen(M));
#define PROFILER_FRAME_START(N) FrameMarkStart(N);
#define PROFILER_FRAME_END(N) FrameMarkEnd(N);

// Blocks
#define PROFILER_BLOCK_SCOPED ZoneScoped;
#define PROFILER_BLOCK_TEXT(N, S) ZoneText(N, S);

// Memory profiling
#define PROFILER_ALLOC(P, S) TracySecureAlloc(P, S);
#define PROFILER_FREE(P) TracySecureFree(P);

#define PROFILER_ALLOC_NAMED(P, S, N) TracySecureAllocN(P, S, N);
#define PROFILER_FREE_NAMED(P, N) TracySecureFreeN(P, N);
#endif
9 changes: 9 additions & 0 deletions src/game/client/gametext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "gametext.h"
#include "filesystem.h"
#include "main.h" // For g_applicationHWnd
#include "profiler.h"
#include "registry.h"
#include "rtsutils.h"
#include <algorithm>
Expand Down Expand Up @@ -398,6 +399,10 @@ bool GameTextManager::Get_CSF_Info(const char *filename)
// Parses older format string files which only support ascii.
bool GameTextManager::Parse_String_File(const char *filename)
{
#ifdef USE_PROFILER
PROFILER_BLOCK_SCOPED
PROFILER_BLOCK_TEXT(filename, strlen(filename))
#endif
captainslog_info("Parsing string file '%s'.", filename);
File *file = g_theFileSystem->Open_File(filename, File::TEXT | File::READ);

Expand Down Expand Up @@ -483,6 +488,10 @@ bool GameTextManager::Parse_String_File(const char *filename)
// Parses CSF files which support UCS2 strings, essentially the BMP of unicode.
bool GameTextManager::Parse_CSF_File(const char *filename)
{
#ifdef USE_PROFILER
PROFILER_BLOCK_SCOPED
PROFILER_BLOCK_TEXT(filename, strlen(filename))
#endif
captainslog_info("Parsing CSF file '%s'.", filename);
CSFHeader header;
File *file = g_theFileSystem->Open_File(filename, File::BINARY | File::READ);
Expand Down
4 changes: 4 additions & 0 deletions src/game/common/crc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* LICENSE
*/
#include "crc.h"
#include "profiler.h"
#include <cctype>

using std::toupper;
Expand Down Expand Up @@ -47,6 +48,9 @@ void CRC::Add_CRC(uint8_t byte)

void CRC::Compute_CRC(void const *data, int bytes)
{
#ifdef USE_PROFILER
PROFILER_BLOCK_SCOPED
#endif
if (data == nullptr) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/game/common/gamemain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* LICENSE
*/
#include "gamemain.h"
#include "profiler.h"
#include "win32gameengine.h"

GameEngine *Create_Game_Engine()
Expand All @@ -29,6 +30,9 @@ GameEngine *Create_Game_Engine()

void Game_Main(int argc, char *argv[])
{
#ifdef USE_PROFILER
PROFILER_MSG("Starting the game engine!")
#endif
g_theGameEngine = Create_Game_Engine();
g_theGameEngine->Init(argc, argv);
g_theGameEngine->Execute();
Expand Down
4 changes: 4 additions & 0 deletions src/game/common/globaldata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "filesystem.h"
#include "gitverinfo.h"
#include "optionpreferences.h"
#include "profiler.h"
#include "rtsutils.h"
#include "version.h"
#include "weapon.h"
Expand Down Expand Up @@ -667,6 +668,9 @@ const FieldParse GlobalData::s_fieldParseTable[] = {

GlobalData::GlobalData()
{
#ifdef USE_PROFILER
PROFILER_BLOCK_SCOPED
#endif
if (s_theOriginal == nullptr) {
s_theOriginal = this;
}
Expand Down
5 changes: 5 additions & 0 deletions src/game/common/ini/ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "objectcreationlist.h"
#include "particlesysmanager.h"
#include "playertemplate.h"
#include "profiler.h"
#include "rankinfo.h"
#include "science.h"
#include "terrainroads.h"
Expand Down Expand Up @@ -204,6 +205,10 @@ INI::~INI() {}

void INI::Load(Utf8String filename, INILoadType type, Xfer *xfer)
{
#ifdef USE_PROFILER
PROFILER_BLOCK_SCOPED
PROFILER_BLOCK_TEXT(filename.Str(), filename.Get_Length())
#endif
Set_FP_Mode(); // Ensure floating point mode is a consistent mode for loading.
g_sXfer = xfer;
Prep_File(filename, type);
Expand Down
5 changes: 5 additions & 0 deletions src/game/common/system/archivefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
#include "archivefile.h"
#include "file.h"
#include "profiler.h"
bool Search_String_Matches(Utf8String string, Utf8String search);

const ArchivedFileInfo *ArchiveFile::Get_Archived_File_Info(Utf8String const &filename) const
Expand Down Expand Up @@ -88,6 +89,10 @@ void ArchiveFile::Get_File_List_In_Directory(Utf8String const &subdir,
std::set<Utf8String, rts::less_than_nocase<Utf8String>> &filelist,
bool search_subdir) const
{
#ifdef USE_PROFILER
PROFILER_BLOCK_SCOPED
PROFILER_BLOCK_TEXT(dirpath.Str(), dirpath.Get_Length())
#endif
Utf8String path = dirpath;
path.To_Lower();
Utf8String token;
Expand Down
5 changes: 5 additions & 0 deletions src/game/common/system/archivefilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "archivefilesystem.h"
#include "archivefile.h"
#include "globaldata.h"
#include "profiler.h"
#include <captainslog.h>

#ifndef GAME_DLL
Expand Down Expand Up @@ -79,6 +80,10 @@ bool ArchiveFileSystem::Does_File_Exist(const char *filename) const
// replace the backing for a file name if it already has an entry in the tree.
void ArchiveFileSystem::Load_Into_Directory_Tree(ArchiveFile const *file, Utf8String const &archive_path, bool overwrite)
{
#ifdef USE_PROFILER
PROFILER_BLOCK_SCOPED
PROFILER_BLOCK_TEXT(archive_path.Str(), archive_path.Get_Length())
#endif
std::set<Utf8String, rts::less_than_nocase<Utf8String>> file_list;

// Retrieve a list of files in the archive
Expand Down
9 changes: 9 additions & 0 deletions src/game/common/system/memdynalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "memblock.h"
#include "mempool.h"
#include "mempoolfact.h"
#include "profiler.h"
#include <cstring>

using std::memset;
Expand Down Expand Up @@ -147,6 +148,10 @@ void *DynamicMemoryAllocator::Allocate_Bytes_No_Zero(int bytes)

++m_usedBlocksInDma;

#ifdef USE_PROFILER
PROFILER_ALLOC(block, bytes)
#endif

return block;
#endif
}
Expand All @@ -167,6 +172,10 @@ void DynamicMemoryAllocator::Free_Bytes(void *block)
#ifdef __SANITIZE_ADDRESS__
free(block);
#else

#ifdef USE_PROFILER
PROFILER_FREE(block)
#endif
ScopedCriticalSectionClass cs(g_dmaCriticalSection);

MemoryPoolSingleBlock *sblock = MemoryPoolSingleBlock::Recover_Block_From_User_Data(block);
Expand Down
60 changes: 43 additions & 17 deletions src/game/common/system/mempoolobj.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "errorcodes.h"
#include "mempool.h"
#include "mempoolfact.h"
#include "profiler.h"
#include <captainslog.h>
#include <new>

Expand All @@ -40,6 +41,46 @@ class MemoryPoolObject
// use macros below to generated them.
};

#ifdef USE_PROFILER
#define OPERATOR_IMPL(classname) \
void *operator new(size_t size) { return operator new(size, classname##_GLUE_NOT_IMPLEMENTED); } \
void *operator new(size_t size, classname##MagicEnum) \
{ \
captainslog_dbgassert(size == sizeof(classname), \
"The wrong operator new is being called; ensure all objects in the hierarchy have MemoryPoolGlue set up " \
"correctly"); \
void *ptr = Get_Class_Pool()->Allocate_Block(); \
PROFILER_ALLOC_NAMED(ptr, size, #classname) \
return ptr; \
} \
void operator delete(void *ptr) { operator delete(ptr, classname##_GLUE_NOT_IMPLEMENTED); } \
void operator delete(void *ptr, classname##MagicEnum) \
{ \
Get_Class_Pool()->Free_Block(ptr); \
PROFILER_FREE_NAMED(ptr, #classname) \
} \
void *operator new(size_t size, void *where) { return where; } \
void operator delete(void *ptr, void *where) {}
#else
#define OPERATOR_IMPL(classname) \
void *operator new(size_t size) { return operator new(size, classname##_GLUE_NOT_IMPLEMENTED); } \
void *operator new(size_t size, classname##MagicEnum) \
{ \
captainslog_dbgassert(size == sizeof(classname), \
"The wrong operator new is being called; ensure all objects in the hierarchy have MemoryPoolGlue set up " \
"correctly"); \
return Get_Class_Pool()->Allocate_Block(); \
} \
void operator delete(void *ptr) { operator delete(ptr, classname##_GLUE_NOT_IMPLEMENTED); } \
void operator delete(void *ptr, classname##MagicEnum) \
{ \
captainslog_dbgassert(0, "Please call Delete_Instance instead of delete."); \
Get_Class_Pool()->Free_Block(ptr); \
} \
void *operator new(size_t size, void *where) { return where; } \
void operator delete(void *ptr, void *where) {}
#endif

// Use within a class declaration on a none virtual MemoryPoolObject
// based class to implement required functions. "classname" must match
// the name of the class in which it is used, "poolname" should match a
Expand Down Expand Up @@ -84,22 +125,7 @@ private: \
public: \
enum classname##MagicEnum{ classname##_GLUE_NOT_IMPLEMENTED = 0 }; \
\
void *operator new(size_t size) { return operator new(size, classname##_GLUE_NOT_IMPLEMENTED); } \
void *operator new(size_t size, classname##MagicEnum) \
{ \
captainslog_dbgassert(size == sizeof(classname), \
"The wrong operator new is being called; ensure all objects in the hierarchy have MemoryPoolGlue set up " \
"correctly"); \
return Get_Class_Pool()->Allocate_Block(); \
} \
void operator delete(void *ptr) { operator delete(ptr, classname##_GLUE_NOT_IMPLEMENTED); } \
void operator delete(void *ptr, classname##MagicEnum) \
{ \
captainslog_dbgassert(0, "Please call Delete_Instance instead of delete."); \
Get_Class_Pool()->Free_Block(ptr); \
} \
void *operator new(size_t size, void *where) { return where; } \
void operator delete(void *ptr, void *where) {}
OPERATOR_IMPL(classname)

/* NOTE: Operator delete above must exist because of the pairing operator new. However, it should never be called directly.
* Reason being, a pointer to a derived class of this class, must use the correct memory pool for deletion. This is possible
Expand Down Expand Up @@ -128,7 +154,7 @@ protected: \
inline void MemoryPoolObject::Delete_Instance()
{
if (this != nullptr) {
#ifdef __SANITIZE_ADDRESS__
#if defined __SANITIZE_ADDRESS__ || defined USE_PROFILER
delete this;
#else
MemoryPool *pool = Get_Object_Pool();
Expand Down
8 changes: 8 additions & 0 deletions src/game/common/system/subsysteminterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
#include "subsysteminterface.h"
#include "ini.h"
#include "profiler.h"
#include "xfer.h"

#ifndef GAME_DLL
Expand Down Expand Up @@ -47,6 +48,13 @@ void SubsystemInterfaceList::Init_Subsystem(SubsystemInterface *sys,
Xfer *xfer,
Utf8String sys_name)
{
#ifdef USE_PROFILER
Utf8String msg = "Initializing subsystem: ";
msg.Concat(sys_name);
PROFILER_MSG(msg.Str())
PROFILER_BLOCK_SCOPED
PROFILER_BLOCK_TEXT(sys_name.Str(), sys_name.Get_Length())
#endif
INI ini;

sys->Set_Name(sys_name);
Expand Down
5 changes: 5 additions & 0 deletions src/platform/stdlocalfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* LICENSE
*/
#include "stdlocalfilesystem.h"
#include "profiler.h"
#include "standardfile.h"
#include "win32localfile.h"

Expand Down Expand Up @@ -77,6 +78,10 @@ void StdLocalFileSystem::Get_File_List_In_Directory(Utf8String const &subdir,
std::set<Utf8String, rts::less_than_nocase<Utf8String>> &filelist,
bool search_subdirs) const
{
#ifdef USE_PROFILER
PROFILER_BLOCK_SCOPED
PROFILER_BLOCK_TEXT(subdir.Str(), subdir.Get_Length())
#endif
Utf8String search_path = dirpath;
search_path += subdir;

Expand Down
Loading