Skip to content

Commit

Permalink
profiling mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jackhumbert committed May 19, 2023
1 parent af92d25 commit e543685
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ find_program(ZOLTAN_CLANG_EXE NAMES zoltan-clang.exe PATHS "${MOD_TOOLS_DIR}" CA
find_program(CYBERPUNK_2077_EXE NAMES Cyberpunk2077.exe PATHS "${CYBERPUNK_2077_GAME_DIR}/bin/x64" CACHE DOC "Cyberpunk2077.exe Executable File")

configure_red4ext(src/red4ext)
configure_red4ext_addresses(Main.cpp Addresses.hpp)
configure_red4ext_addresses(Addresses.hpp)

find_package(Detours)
find_package(Spdlog)
Expand Down
2 changes: 1 addition & 1 deletion deps/cyberpunk_cmake
38 changes: 23 additions & 15 deletions src/red4ext/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <thread>
#include <winnt.h>
#include <winuser.h>
#include "RED4ext/CNamePool.hpp"
#include "RED4ext/ISerializable.hpp"
#include "RED4ext/InstanceType.hpp"
#include "RED4ext/RTTISystem.hpp"
Expand Down Expand Up @@ -130,15 +131,22 @@ REGISTER_HOOK(void __fastcall, Breakpoint, RED4ext::IScriptable *context, RED4ex
Breakpoint_Original(context, stackFrame, a3, a4);
}

// #define CTD_HELPER_PROFILING
#define CTD_HELPER_PROFILING
int numberOfProcessors = 4;

void LogFunctionCall(RED4ext::IScriptable *context, RED4ext::CStackFrame *stackFrame, RED4ext::CBaseFunction *func) {

wchar_t * thread_name;
HRESULT hr = GetThreadDescription(GetCurrentThread(), &thread_name);
std::wstring ws(thread_name);
auto thread = std::string(ws.begin(), ws.end());

#ifdef CTD_HELPER_PROFILING
auto profiler = CyberpunkMod::Profiler();
RED4ext::CNamePool::Add(thread.c_str());
auto profiler = CyberpunkMod::Profiler(thread.c_str(), 5);
#endif

auto invoke = reinterpret_cast<RED4ext::Instr::Invoke *>(stackFrame->code);
wchar_t * thread_name;
HRESULT hr = GetThreadDescription(GetCurrentThread(), &thread_name);
auto call = CallPair();
call.line = invoke->lineNumber;
call.self.func = *reinterpret_cast<BaseFunction*>(func);
Expand All @@ -155,24 +163,22 @@ void LogFunctionCall(RED4ext::IScriptable *context, RED4ext::CStackFrame *stackF
// call.self.contextType->ToString(context, call.self.contextString);
// call.parent.contextString = call.self.contextString;
// }
}
std::wstring ws(thread_name);
auto thread = std::string(ws.begin(), ws.end());
};

std::lock_guard<std::mutex> lock(queueLock);
lastThread = thread;
if (funcCallQueues.find(thread) == funcCallQueues.end()) {
funcCallQueues[thread] = std::queue<CallPair>();
{
std::lock_guard<std::mutex> lock(queueLock);
lastThread = thread;
}
funcCallQueues[thread].emplace(call);
while (funcCallQueues[thread].size() > MAX_CALLS) {
funcCallQueues[thread].pop();
auto& queue = funcCallQueues[thread];
queue.emplace(call);
while (queue.size() > MAX_CALLS) {
queue.pop();
}

#ifdef CTD_HELPER_PROFILING
auto avg = profiler.End();
if (avg != 0) {
spdlog::info("Average microseconds for CTD Helper: {}", avg);
spdlog::info("1s of execution in {:<15}: {:7}us", profiler.m_tracker.ToString(), avg);
}
#endif
}
Expand Down Expand Up @@ -534,6 +540,8 @@ RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle, RED4ext::

ModModuleFactory::GetInstance().Load(aSdk, aHandle);

numberOfProcessors = std::thread::hardware_concurrency();

break;
}
case RED4ext::EMainReason::Unload: {
Expand Down

0 comments on commit e543685

Please sign in to comment.