Skip to content

Commit

Permalink
Added Engine profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
dedmen committed Jan 23, 2019
1 parent 83848ad commit 18b7b26
Show file tree
Hide file tree
Showing 15 changed files with 495 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/AdapterArmaDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

extern scriptProfiler profiler;

AdapterArmaDiag::AdapterArmaDiag()
{
AdapterArmaDiag::AdapterArmaDiag() {
frames.resize(framesToGo + 1);
type = AdapterType::ArmaDiag;
}


AdapterArmaDiag::~AdapterArmaDiag()
{
}
AdapterArmaDiag::~AdapterArmaDiag() {}

std::shared_ptr<ScopeInfo> AdapterArmaDiag::createScope(intercept::types::r_string name,
intercept::types::r_string filename, uint32_t fileline) {
Expand Down
7 changes: 3 additions & 4 deletions src/AdapterBrofiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,12 @@ uint32_t getRandColor() {
}


AdapterBrofiler::AdapterBrofiler()
{
AdapterBrofiler::AdapterBrofiler() {
type = AdapterType::Brofiler;
}


AdapterBrofiler::~AdapterBrofiler()
{
AdapterBrofiler::~AdapterBrofiler() {
cleanup();
}

Expand Down
8 changes: 3 additions & 5 deletions src/AdapterChrome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ void ChromeEvent::writeTo(std::ofstream& str) const {
str << "},"; //#TODO \n for human readable
}

AdapterChrome::AdapterChrome()
{
AdapterChrome::AdapterChrome() {
profStart = std::chrono::high_resolution_clock::now();
type = AdapterType::Chrome;
}


AdapterChrome::~AdapterChrome()
{
}
AdapterChrome::~AdapterChrome() {}

void AdapterChrome::perFrame() {}

Expand Down
13 changes: 9 additions & 4 deletions src/AdapterTracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ class ScopeTempStorageTracy final : public ScopeTempStorage {
std::unique_ptr<tracy::ScopedZone> zone;
};

AdapterTracy::AdapterTracy()
{
AdapterTracy::AdapterTracy() {
type = AdapterType::Tracy;
}

AdapterTracy::~AdapterTracy()
{
AdapterTracy::~AdapterTracy() {
tracy::s_profiler.RequestShutdown();
while (!tracy::s_profiler.HasShutdownFinished())
std::this_thread::sleep_for(5ms);
Expand Down Expand Up @@ -72,3 +71,9 @@ void AdapterTracy::setCounter(intercept::types::r_string name, float val) {
counterCache.insert(name);
tracy::Profiler::PlotData(name.c_str(), val);
}

std::shared_ptr<ScopeInfo> AdapterTracy::createScopeStatic(const char* name, const char* filename, uint32_t fileline) {
auto info = std::make_shared<ScopeInfoTracy>();
info->info = tracy::SourceLocationData{nullptr, name, filename,fileline, 0};
return info;
}
4 changes: 4 additions & 0 deletions src/AdapterTracy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class AdapterTracy final : public ProfilerAdapter
void leaveScope(std::shared_ptr<ScopeTempStorage> tempStorage) override;
void addLog(intercept::types::r_string message) override;
void setCounter(intercept::types::r_string name, float val) override;

std::shared_ptr<ScopeInfo> createScopeStatic(const char* name, const char* filename, uint32_t fileline);


private:

using scopeCacheKey = std::tuple<intercept::types::r_string, intercept::types::r_string,uint32_t>;
Expand Down
11 changes: 11 additions & 0 deletions src/ArmaScriptProfiler.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.props" />
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
Expand Down Expand Up @@ -167,6 +168,8 @@
<ClInclude Include="AdapterBrofiler.hpp" />
<ClInclude Include="AdapterChrome.hpp" />
<ClInclude Include="AdapterTracy.hpp" />
<ClInclude Include="EngineProfiling.h" />
<ClInclude Include="HookManager.hpp" />
<ClInclude Include="ProfilerAdapter.hpp" />
<ClInclude Include="scriptProfiler.hpp" />
</ItemGroup>
Expand Down Expand Up @@ -234,10 +237,18 @@
<ClCompile Include="AdapterBrofiler.cpp" />
<ClCompile Include="AdapterChrome.cpp" />
<ClCompile Include="AdapterTracy.cpp" />
<ClCompile Include="EngineProfiling.cpp" />
<ClCompile Include="HookManager.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="scriptProfiler.cpp" />
</ItemGroup>
<ItemGroup>
<MASM Include="hooks.asm">
<FileType>Document</FileType>
</MASM>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
</ImportGroup>
</Project>
17 changes: 17 additions & 0 deletions src/ArmaScriptProfiler.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
<ClInclude Include="AdapterTracy.hpp">
<Filter>Quelldateien\Adapters</Filter>
</ClInclude>
<ClInclude Include="EngineProfiling.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="HookManager.hpp">
<Filter>Quelldateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="scriptProfiler.cpp">
Expand Down Expand Up @@ -257,5 +263,16 @@
<ClCompile Include="AdapterTracy.cpp">
<Filter>Quelldateien\Adapters</Filter>
</ClCompile>
<ClCompile Include="EngineProfiling.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="HookManager.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="hooks.asm">
<Filter>Quelldateien</Filter>
</None>
</ItemGroup>
</Project>
86 changes: 86 additions & 0 deletions src/EngineProfiling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "EngineProfiling.h"
#include "ProfilerAdapter.hpp"
#include "AdapterTracy.hpp"

extern std::shared_ptr<ProfilerAdapter> GProfilerAdapter;
std::unordered_map<PCounter*, std::shared_ptr<ScopeTempStorage>> openScopes;
std::unordered_map<PCounter*, std::shared_ptr<ScopeInfo>> scopeCache;

extern "C" {
uintptr_t profEndJmpback;
uintptr_t shouldTimeJmpback;


void shouldTime();
void doEnd();
}

bool PCounter::shouldTime() {

if (GProfilerAdapter->getType() == AdapterType::Tracy) {
auto tracyProf = std::dynamic_pointer_cast<AdapterTracy>(GProfilerAdapter);

auto found = scopeCache.find(this);
if (found == scopeCache.end()) {
auto res = scopeCache.insert({this, tracyProf->createScopeStatic(name, cat, 0)});
found = res.first;
}

//make sure the add is not inside the scope
openScopes[this] = nullptr;
auto ins = openScopes.find(this);

ins->second = tracyProf->enterScope(found->second);
}




if (slot < 0) return false;
if (!boss) return false;
if (!boss->da) return false;
if (!boss->stuffzi) return false;
if (!boss->stuffzi[slot].enabled) return false;
return true;
}

void ScopeProf::doEnd() {
auto found = openScopes.find(counter);
if (found == openScopes.end()) return;
GProfilerAdapter->leaveScope(found->second);
openScopes.erase(found);
}


HookManager::Pattern pat_doEnd{
"xxxxxxxxxxxxxxxxx?????xxxx?????xxxxxxxxxxxxxx????xxxxxxxxxxxxxxxxx????xxxx?x????xxxxxxxxxxxxxxxxx????xxxxxxxxx?????xxxxxx",
"\x40\x53\x48\x83\xEC\x30\x80\x79\x11\x00\x48\x8B\xD9\x75\x09\x80\x3D\x00\x00\x00\x00\x00\x74\x38\x80\x3D\x00\x00\x00\x00\x00\x74\x0B\x0F\x31\x48\xC1\xE2\x20\x48\x0B\xC2\xEB\x05\xE8\x00\x00\x00\x00\x48\x8B\x13\x4C\x8B\xC0\x48\x8B\x43\x08\x4C\x8D\x4B\x18\x48\x8D\x0D\x00\x00\x00\x00\x48\x89\x44\x24\x00\xE8\x00\x00\x00\x00\x48\x8B\x53\x18\x48\x85\xD2\x74\x1A\xF0\xFF\x0A\x75\x0D\x48\x8B\x0D\x00\x00\x00\x00\x48\x8B\x01\xFF\x50\x18\x48\xC7\x43\x00\x00\x00\x00\x00\x48\x83\xC4\x30\x5B\xC3"
};


HookManager::Pattern pat_shouldTime{
"xxxxxxxxxxxxxxxxxxxxxxxxxx????xxxxxxxxxxxxxxxxxxxx????xxxxxxx",
"\x48\x63\x41\x18\x85\xC0\x78\x32\x4C\x8B\x01\x33\xD2\x4D\x85\xC0\x74\x12\x41\x38\x10\x74\x0D\x48\x69\xC8\x00\x00\x00\x00\x49\x03\x48\x20\xEB\x03\x48\x8B\xCA\x48\x85\xC9\x74\x0A\x38\x51\x4A\x74\x05\xBA\x00\x00\x00\x00\x0F\xB6\xC2\xC3\x32\xC0\xC3"
};


EngineProfiling::EngineProfiling()
{

hooks.placeHook(hookTypes::shouldTime, pat_shouldTime, reinterpret_cast<uintptr_t>(shouldTime), shouldTimeJmpback, 0);
hooks.placeHook(hookTypes::doEnd, pat_doEnd, reinterpret_cast<uintptr_t>(doEnd), profEndJmpback, 0);


}


EngineProfiling::~EngineProfiling()
{
}


extern "C" {



}
104 changes: 104 additions & 0 deletions src/EngineProfiling.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#pragma once
#include <containers.hpp>
#include "HookManager.hpp"


class ArmaProf;

class PCounter {
public:
ArmaProf* boss;
const char* name;
const char* cat;
int slot, stuff2;

__declspec(noinline) bool shouldTime();

};


class PCSlot {
public:
intercept::types::r_string name;
intercept::types::r_string category;
int a,b,c,d,e,f;
bool g,h,enabled;
};


class ScopeProf {
public:
int64_t start;
PCounter* counter;
bool enabled, other;
intercept::types::r_string stuffz;

__declspec(noinline) void doEnd();
};

class ArmaProf {
friend class PCounter;
public:

void scopeCompleted(int64_t start, int64_t end, intercept::types::r_string* stuff, void* stuff2);


private:
//This is engine stuff.
bool da,db,dc;
int dd,df;
bool de,dg,dh;
int ab,dj;
float stuff;
int numba;
PCSlot* stuffzi;
long suttfu2;


//
bool dfddfg;
char sfsdf[40];
//


void* stuffings;
long stuffonz;
intercept::types::auto_array<void*> stuffz;
intercept::types::auto_array<void*> stuffz2;
intercept::types::auto_array<void*> stuffz3;
intercept::types::auto_array<void*> stuffz4;

bool megaOof;
int64_t ouf1;
int64_t ouf2;
int64_t ouf3;
int64_t ouf4;
int64_t ouf5;
int64_t ouf6;
int blios;
intercept::types::r_string blip;
float blop;
bool asd,adasd;
int stuffiz;

int64_t framestart;
int64_t stuffdfdf;


//
void* blisdfsd;
bool lsdfsdf;
unsigned int sdfsdfsdf;
//
intercept::types::r_string blooorp;

};

class EngineProfiling {
public:
EngineProfiling();
~EngineProfiling();


HookManager hooks;
};
Loading

0 comments on commit 18b7b26

Please sign in to comment.