Skip to content

Commit

Permalink
Added NetworkProfiler
Browse files Browse the repository at this point in the history
  • Loading branch information
dedmen committed Nov 24, 2019
1 parent 76f676d commit cfbebc6
Show file tree
Hide file tree
Showing 10 changed files with 639 additions and 13 deletions.
2 changes: 1 addition & 1 deletion intercept
79 changes: 72 additions & 7 deletions src/AdapterTracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ScopeTempStorageTracy final : public ScopeTempStorage {
public:

ScopeTempStorageTracy(const tracy::SourceLocationData* srcloc) : zone(srcloc) {}
ScopeTempStorageTracy(const tracy::SourceLocationData* srcloc, uint64_t threadID) : zone(srcloc, threadID) {}
//ScopeTempStorageTracy(const tracy::SourceLocationData* srcloc, uint64_t threadID) : zone(srcloc, threadID) {}
tracy::ScopedZone zone;
};

Expand Down Expand Up @@ -63,12 +63,13 @@ std::shared_ptr<ScopeTempStorage> AdapterTracy::enterScope(std::shared_ptr<Scope
}

std::shared_ptr<ScopeTempStorage> AdapterTracy::enterScope(std::shared_ptr<ScopeInfo> scope, uint64_t threadID) {
auto info = std::dynamic_pointer_cast<ScopeInfoTracy>(scope);
if (!info || !isConnected()) return nullptr; //#TODO debugbreak? log error?
ensureReady();

auto ret = std::make_shared<ScopeTempStorageTracy>(&info->info, threadID);
return ret;
return enterScope(scope);
//auto info = std::dynamic_pointer_cast<ScopeInfoTracy>(scope);
//if (!info || !isConnected()) return nullptr; //#TODO debugbreak? log error?
//ensureReady();
//
//auto ret = std::make_shared<ScopeTempStorageTracy>(&info->info, threadID);
//return ret;
}

void AdapterTracy::leaveScope(std::shared_ptr<ScopeTempStorage> tempStorage) {
Expand Down Expand Up @@ -114,6 +115,70 @@ bool AdapterTracy::isConnected() {
return tracy::s_profiler.IsConnected();
}


struct CallstackStruct {
intercept::types::auto_array<std::pair<intercept::types::r_string, uint32_t>> data;
};

void DestructCallstackStruct(void* data) {
auto str = (CallstackStruct*)data;

str->data.clear();
delete str;
}

void AdapterTracy::sendCallstack(intercept::types::auto_array<std::pair<intercept::types::r_string, uint32_t>>& cs) {
if (cs.size() > 63)
cs.resize(63);

uint32_t depth = cs.size();

const char* func[64];
uint32_t fsz[64];
uint32_t ssz[64];
uint32_t spaceNeeded = 4; // cnt

uint32_t cnt = 0;
for (auto& [file, line] : cs) {
func[cnt] = file.c_str();
fsz[cnt] = uint32_t(strlen(func[cnt]));
ssz[cnt] = uint32_t(file.length());
spaceNeeded += fsz[cnt] + ssz[cnt];
cnt++;
}

spaceNeeded += cnt * (4 + 4 + 4); // source line, function string length, source string length

auto ptr = (char*)tracy::tracy_malloc(spaceNeeded + 4);
auto dst = ptr;
memcpy(dst, &spaceNeeded, 4); dst += 4;
memcpy(dst, &cnt, 4); dst += 4;


cnt = 0;
for (auto& [file, line] : cs) {
memcpy(dst, &line, 4); dst += 4;
memcpy(dst, fsz + cnt, 4); dst += 4;
memcpy(dst, func[cnt], fsz[cnt]); dst += fsz[cnt];
memcpy(dst, ssz + cnt, 4); dst += 4;
memcpy(dst, file.c_str(), ssz[cnt]), dst += ssz[cnt];
cnt++;
}
assert(dst - ptr == spaceNeeded + 4);

tracy::Magic magic;
auto token = tracy::GetToken();
auto& tail = token->get_tail_index();
auto item = token->enqueue_begin(magic);
tracy::MemWrite(&item->hdr.type, tracy::QueueType::CallstackArma);
tracy::MemWrite(&item->callstackAlloc.ptr, (uint64_t)ptr);
auto str = new CallstackStruct();
str->data = cs;
tracy::MemWrite(&item->callstackAlloc.nativePtr, (uint64_t)str);
tail.store(magic + 1, std::memory_order_release);

}

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

Expand Down
1 change: 1 addition & 0 deletions src/AdapterTracy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AdapterTracy final : public ProfilerAdapter
std::shared_ptr<ScopeInfo> createScopeStatic(const char* name, const char* filename, uint32_t fileline) const;
static bool isConnected();

static void sendCallstack(intercept::types::auto_array<std::pair<intercept::types::r_string, uint32_t>>& cs);
private:
static void ensureReady();
using scopeCacheKey = std::tuple<intercept::types::r_string, intercept::types::r_string,uint32_t>;
Expand Down
2 changes: 2 additions & 0 deletions src/ArmaScriptProfiler.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
<ClInclude Include="EngineProfiling.h" />
<ClInclude Include="FAllocHook.h" />
<ClInclude Include="HookManager.hpp" />
<ClInclude Include="NetworkProfiler.hpp" />
<ClInclude Include="ProfilerAdapter.hpp" />
<ClInclude Include="scriptProfiler.hpp" />
<ClInclude Include="SignalSlot.hpp" />
Expand Down Expand Up @@ -218,6 +219,7 @@
<ClCompile Include="FAllocHook.cpp" />
<ClCompile Include="HookManager.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="NetworkProfiler.cpp" />
<ClCompile Include="scriptProfiler.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/ArmaScriptProfiler.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ClInclude Include="FAllocHook.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="NetworkProfiler.hpp">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="scriptProfiler.cpp">
Expand Down Expand Up @@ -194,6 +197,9 @@
<ClCompile Include="FAllocHook.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="NetworkProfiler.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<MASM Include="hooks.asm">
Expand Down
Loading

0 comments on commit cfbebc6

Please sign in to comment.