Skip to content

Commit

Permalink
Fixed engine profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
dedmen committed Oct 26, 2024
1 parent d6740b6 commit acd92a4
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 64 deletions.
2 changes: 1 addition & 1 deletion intercept
Submodule intercept updated 54 files
+107 −107 .github/workflows/ci.yml
+1 −2 README.md
+112 −0 src/client/headers/client/eventhandlers.hpp
+30 −20 src/client/headers/client/sqf/ai.hpp
+9 −2 src/client/headers/client/sqf/camera.hpp
+26 −2 src/client/headers/client/sqf/core.hpp
+119 −117 src/client/headers/client/sqf/ctrl.hpp
+5 −0 src/client/headers/client/sqf/curator.hpp
+3 −0 src/client/headers/client/sqf/debug.hpp
+12 −1 src/client/headers/client/sqf/eden.hpp
+37 −20 src/client/headers/client/sqf/inventory.hpp
+9 −7 src/client/headers/client/sqf/marker.hpp
+2 −0 src/client/headers/client/sqf/misc.hpp
+27 −29 src/client/headers/client/sqf/multiplayer.hpp
+19 −0 src/client/headers/client/sqf/position.hpp
+27 −6 src/client/headers/client/sqf/sound.hpp
+76 −14 src/client/headers/client/sqf/units.hpp
+151 −8 src/client/headers/client/sqf/vehicles.hpp
+29 −3 src/client/headers/client/sqf/world.hpp
+140 −29 src/client/headers/client/sqf_assignments.hpp
+140 −29 src/client/headers/client/sqf_pointers_declaration.hpp
+139 −28 src/client/headers/client/sqf_pointers_definitions.hpp
+39 −43 src/client/headers/shared/client_types.hpp
+10 −5 src/client/headers/shared/containers.hpp
+48 −59 src/client/headers/shared/types.hpp
+1 −1 src/client/headers/shared/vector.hpp
+115 −0 src/client/intercept/client/eventhandlers.cpp
+77 −5 src/client/intercept/client/sqf/ai.cpp
+8 −4 src/client/intercept/client/sqf/camera.cpp
+2 −3 src/client/intercept/client/sqf/config.cpp
+50 −5 src/client/intercept/client/sqf/core.cpp
+9 −0 src/client/intercept/client/sqf/ctrl.cpp
+20 −0 src/client/intercept/client/sqf/curator.cpp
+19 −0 src/client/intercept/client/sqf/debug.cpp
+38 −3 src/client/intercept/client/sqf/eden.cpp
+36 −5 src/client/intercept/client/sqf/inventory.cpp
+13 −7 src/client/intercept/client/sqf/marker.cpp
+8 −0 src/client/intercept/client/sqf/misc.cpp
+4 −0 src/client/intercept/client/sqf/multiplayer.cpp
+147 −5 src/client/intercept/client/sqf/position.cpp
+68 −21 src/client/intercept/client/sqf/sound.cpp
+60 −42 src/client/intercept/client/sqf/units.cpp
+193 −6 src/client/intercept/client/sqf/vehicles.cpp
+31 −8 src/client/intercept/client/sqf/world.cpp
+36 −0 src/client/intercept/shared/types.cpp
+34 −70 src/host/invoker/sqf_functions.cpp
+5 −5 src/host/invoker/sqf_functions.hpp
+4 −0 src/host/loader/CommandScan214.cpp
+31 −28 src/host/loader/CommandTypes.hpp
+2 −0 src/host/loader/CommandTypesDynamic.hpp
+8 −0 src/host/loader/MemorySection.hpp
+2 −1 src/host/loader/StateTypes.hpp
+71 −17 src/host/loader/loader.cpp
+40 −0 src/host/loader/ptraccess.h
62 changes: 61 additions & 1 deletion src/AdapterTracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define TRACY_ENABLE
#define TRACY_ON_DEMAND
#define TRACY_FIBERS
#include <TracyClient.cpp>
#include <Tracy.hpp>
#include <unordered_set>
Expand All @@ -28,6 +29,7 @@ extern bool EngineProfilingEnabled;
extern bool instructionLevelProfiling;
extern bool InstructionCallstack;
extern bool logPacketContent;
extern bool checkMainThread;

void TracyParameterUpdated(uint32_t idx, int32_t val) {
switch (idx) {
Expand All @@ -49,6 +51,9 @@ void TracyParameterUpdated(uint32_t idx, int32_t val) {
case TP_InstructionGetVarCallstackEnabled:
InstructionCallstack = val != 0;
break;
case TP_EngineProfilingMainThreadOnly:
checkMainThread = val != 0;
break;
}

}
Expand Down Expand Up @@ -95,6 +100,24 @@ std::shared_ptr<ScopeTempStorage> AdapterTracy::enterScope(std::shared_ptr<Scope
return ret;
}

void AdapterTracy::enterScopeNoStorage(std::shared_ptr<ScopeInfo> scope)
{
auto info = std::dynamic_pointer_cast<ScopeInfoTracy>(scope);
if (!info || !isConnected()) return; //#TODO debugbreak? log error?
ensureReady();

// Tracy code inlined, in a way such that we don't even need tempStorage
using namespace tracy;

const SourceLocationData* srcloc = &info->info;

TracyQueuePrepare(QueueType::ZoneBegin);
MemWrite(&item->zoneBegin.time, Profiler::GetTime());
MemWrite(&item->zoneBegin.srcloc, (uint64_t)srcloc);
TracyQueueCommit(zoneBeginThread);

}

std::shared_ptr<ScopeTempStorage> AdapterTracy::enterScope(std::shared_ptr<ScopeInfo> scope, uint64_t threadID) {
return enterScope(scope);
//auto info = std::dynamic_pointer_cast<ScopeInfoTracy>(scope);
Expand All @@ -117,10 +140,19 @@ std::shared_ptr<ScopeTempStorage> AdapterTracy::enterScope(std::shared_ptr<Scope
void AdapterTracy::leaveScope(std::shared_ptr<ScopeTempStorage> tempStorage) {
auto tmpStorage = std::dynamic_pointer_cast<ScopeTempStorageTracy>(tempStorage);
if (!tmpStorage) return; //#TODO debugbreak? log error?

tmpStorage->zone.end(); //zone destructor ends zone
}

void AdapterTracy::leaveScopeNoStorage(uint64_t time)
{
// Tracy code inlined, in a way such that we don't even need tempStorage
using namespace tracy;
TracyQueuePrepare(QueueType::ZoneEnd);
MemWrite(&item->zoneEnd.time, time == -1 ? Profiler::GetTime() : time);
TracyQueueCommit(zoneEndThread);
}

void AdapterTracy::setName(std::shared_ptr<ScopeTempStorage> tempStorage, const intercept::types::r_string& name) {
auto tmpStorage = std::dynamic_pointer_cast<ScopeTempStorageTracy>(tempStorage);
if (!tmpStorage) return; //#TODO debugbreak? log error?
Expand All @@ -130,9 +162,25 @@ void AdapterTracy::setName(std::shared_ptr<ScopeTempStorage> tempStorage, const
void AdapterTracy::setDescription(std::shared_ptr<ScopeTempStorage> tempStorage, const intercept::types::r_string& descr) {
auto tmpStorage = std::dynamic_pointer_cast<ScopeTempStorageTracy>(tempStorage);
if (!tmpStorage) return; //#TODO debugbreak? log error?

tmpStorage->zone.Text(descr.c_str(), descr.length());
}

void AdapterTracy::setDescriptionNoStorage(const intercept::types::r_string& descr)
{
// Tracy code inlined, in a way such that we don't even need tempStorage
using namespace tracy;

const char* txt = descr.c_str();
size_t size = descr.length();
auto ptr = (char*)tracy_malloc(size);
memcpy(ptr, txt, size);
TracyQueuePrepare(QueueType::ZoneText);
MemWrite(&item->zoneTextFat.text, (uint64_t)ptr);
MemWrite(&item->zoneTextFat.size, (uint16_t)size);
TracyQueueCommit(zoneTextFatThread);
}

void AdapterTracy::addLog(intercept::types::r_string message) {
if (message.empty()) return;
tracy::Profiler::Message(message.c_str(), message.length());
Expand Down Expand Up @@ -212,6 +260,8 @@ void AdapterTracy::sendCallstack(intercept::types::auto_array<std::pair<intercep
assert(dst - ptr == spaceNeeded + 4);


using namespace tracy;

// inlined macro TracyQueuePrepare
tracy::moodycamel::ConcurrentQueueDefaultTraits::index_t __magic;
auto __token = tracy::GetToken();
Expand All @@ -231,6 +281,16 @@ void AdapterTracy::addParameter(uint32_t idx, const char* name, bool isBool, int
TracyParameterSetup(idx, name, isBool, val);
}

void AdapterTracy::SwitchToFiber(const char* name)
{
TracyFiberEnter(name);
}

void AdapterTracy::LeaveFiber()
{
TracyFiberLeave;
}

void AdapterTracy::ensureReady() {
if (tracy::s_token.ptr) return;

Expand Down
10 changes: 9 additions & 1 deletion src/AdapterTracy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ enum TracyParams {
TP_NetworkProfilerLogPacketContent,
TP_EngineProfilingEnabled,
TP_InstructionProfilingEnabled,
TP_InstructionGetVarCallstackEnabled
TP_InstructionGetVarCallstackEnabled,
TP_EngineProfilingMainThreadOnly
};


Expand All @@ -23,11 +24,15 @@ class AdapterTracy final : public ProfilerAdapter
std::shared_ptr<ScopeInfo> createScope(intercept::types::r_string name, intercept::types::r_string filename,
uint32_t fileline) override;
std::shared_ptr<ScopeTempStorage> enterScope(std::shared_ptr<ScopeInfo> scope) override;
// The NoStorage functions can only be used if they are always paired up correctly and in correct order. Cannot leave a scope that was never entered
void enterScopeNoStorage(std::shared_ptr<ScopeInfo> scope);
std::shared_ptr<ScopeTempStorage> enterScope(std::shared_ptr<ScopeInfo> scope, uint64_t threadID) override;
std::shared_ptr<ScopeTempStorage> enterScope(std::shared_ptr<ScopeInfo> scope, ScopeWithCallstack cs) override;
void leaveScope(std::shared_ptr<ScopeTempStorage> tempStorage) override;
void leaveScopeNoStorage(uint64_t time = -1);
void setName(std::shared_ptr<ScopeTempStorage> tempStorage, const intercept::types::r_string& name) override;
void setDescription(std::shared_ptr<ScopeTempStorage> tempStorage, const intercept::types::r_string& descr) override;
void setDescriptionNoStorage(const intercept::types::r_string& descr);
void addLog(intercept::types::r_string message) override;
void setCounter(intercept::types::r_string name, float val) override;
void setCounter(const char* name, float val) const;
Expand All @@ -39,6 +44,9 @@ class AdapterTracy final : public ProfilerAdapter

static void addParameter(uint32_t idx, const char* name, bool isBool, int32_t val);

static void SwitchToFiber(const char* name);
static void LeaveFiber();

private:
static void ensureReady();
using scopeCacheKey = std::tuple<intercept::types::r_string, intercept::types::r_string,uint32_t>;
Expand Down
Loading

0 comments on commit acd92a4

Please sign in to comment.