Skip to content

Commit

Permalink
Implement an alternative way to pass parameters (#30)
Browse files Browse the repository at this point in the history
* init commit

* review changes

* remove unused extern var

* Apply suggestions from code review

Co-authored-by: Björn Dahlgren <[email protected]>

* removed json header, from scriptProfiler.hpp.
changed to pass file path out through data pointer for linux

* Dedmen's genius idea

* submodule update

* Update src/scriptProfiler.cpp

---------

Co-authored-by: Dedmen Miller <[email protected]>
Co-authored-by: Björn Dahlgren <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2023
1 parent 8e4bb6f commit 9efeca7
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
/src/Debug
/src/ArmaScriptProfiler.vcxproj.user
/src/Release
/out
*.biprivatekey

# CLion
Expand All @@ -50,4 +51,4 @@ cmake-build-*/
.hemtt/local
addons/*.pbo
keys/*
releases/*
releases/*
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "tracy"]
path = tracy
url = https://github.com/dedmen/tracy.git
[submodule "lib/json"]
path = lib/json
url = https://github.com/nlohmann/json.git
1 change: 1 addition & 0 deletions lib/json
Submodule json added at 5d2754
13 changes: 13 additions & 0 deletions parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profilerAdapter": "Tracy",
"profilerOutput": "",
"profilerEnableInstruction": true,
"profilerEnableEngine": false,
"profilerEngineThreads": false,
"profilerEngineDoFile": false,
"profilerEngineDoMem": false,
"profilerNoPaths": false,
"profilerNoInstrumentation": false,
"profilerEnableFAlloc": false,
"profilerEnableNetwork": true
}
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ cmake_minimum_required (VERSION 3.13)

set(INTERCEPT_CLIENT_PATH "${CMAKE_SOURCE_DIR}/intercept/src/client")
set(BROFILER_BASE_PATH "${CMAKE_SOURCE_DIR}/brofiler")
set(LIB_BASE_PATH "${CMAKE_SOURCE_DIR}/lib")

set(INTERCEPT_INCLUDE_PATH "${INTERCEPT_CLIENT_PATH}/headers" "${INTERCEPT_CLIENT_PATH}/headers/shared" "${INTERCEPT_CLIENT_PATH}/headers/client/" "${INTERCEPT_CLIENT_PATH}/headers/client/sqf")
set(BROFILER_INCLUDE_PATH "${BROFILER_BASE_PATH}/BrofilerCore" "${BROFILER_BASE_PATH}/ThirdParty/TaskScheduler/Scheduler/Include")
set(TRACY_INCLUDE_PATH "${CMAKE_SOURCE_DIR}/tracy")

set(JSON_INCLUDE_PATH "${LIB_BASE_PATH}/json/single_include")

if(USE_64BIT_BUILD)
set(INTERCEPT_PLUGIN_NAME "ArmaScriptProfiler_x64")
Expand Down Expand Up @@ -70,6 +71,7 @@ add_library( ${INTERCEPT_PLUGIN_NAME} SHARED ${library_sources} ${INTERCEPT_SOUR
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${INTERCEPT_INCLUDE_PATH} )
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${BROFILER_INCLUDE_PATH} )
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${TRACY_INCLUDE_PATH} )
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${JSON_INCLUDE_PATH} )

target_link_libraries (${INTERCEPT_PLUGIN_NAME} ${CMAKE_THREAD_LIBS_INIT})

Expand Down
91 changes: 89 additions & 2 deletions src/scriptProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#include <intercept.hpp>
#include <numeric>
#include <random>
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#ifndef __linux__
#include <Windows.h>
#else
#include <signal.h>
#include <fstream>
#include <limits.h>
#include <unistd.h>
#include <link.h>
#endif
#include "ProfilerAdapter.hpp"
#include "AdapterArmaDiag.hpp"
Expand All @@ -26,6 +31,7 @@ using namespace intercept;
using namespace std::chrono_literals;
std::chrono::high_resolution_clock::time_point startTime;
static sqf_script_type* GameDataProfileScope_type;
static nlohmann::json json;

scriptProfiler profiler{};
bool instructionLevelProfiling = false;
Expand Down Expand Up @@ -987,6 +993,25 @@ std::optional<std::string> getCommandLineParam(std::string_view needle) {
adapterStr = adapterStr.substr(0, adapterStr.length() - 1);
return adapterStr;
}

if (!json.empty() && json.contains(needle.substr(1))) {
switch (json[needle.substr(1)].type()) {
case nlohmann::json::value_t::boolean:
{
if (json[needle.substr(1)].get<bool>()) {
return "true";
} else {
return {};
}
}
case nlohmann::json::value_t::string:
{
return json[needle.substr(1)].get<std::string>();
}
default:
return {};
}
}
return {};
}

Expand Down Expand Up @@ -1306,9 +1331,72 @@ class GameInstructionNewExpression : public game_instruction {

#pragma endregion Instructions
#endif


#if __linux__
extern "C" int iterateCallback(struct dl_phdr_info* info, size_t size, void* data) {
std::filesystem::path sharedPath = info->dlpi_name;
if (sharedPath.filename() == "ArmaScriptProfiler_x64.so") {
*static_cast<std::filesystem::path*>(data) = info->dlpi_name;
return 0;
}
return 0;
}
#endif

std::filesystem::path getSharedObjectPath() {
#if __linux__
std::filesystem::path path;
dl_iterate_phdr(iterateCallback, &path);
return path;

#else
wchar_t buffer[MAX_PATH] = { 0 };
HMODULE handle = nullptr;
if(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&getSharedObjectPath), &handle) == 0) {
sqf::diag_log("getSharedObjectPath: GetModuleHandle failed");
return std::filesystem::path{};
}

DWORD len = GetModuleFileNameW(handle, buffer, MAX_PATH);
if (len == 0) {
return std::filesystem::path{};
}
return std::filesystem::path{buffer};
#endif
}

std::filesystem::path findConfigFilePath() {

std::filesystem::path path = getSharedObjectPath();
path = path.parent_path().parent_path();

std::filesystem::path dirName{ "config"sv };
std::filesystem::path fileName{ "parameters.json"sv };
path = path / dirName / fileName;

return path;
}

void scriptProfiler::preStart() {
sqf::diag_log("Arma Script Profiler preStart");

std::filesystem::path filePath = findConfigFilePath();

if (!filePath.empty() && std::filesystem::exists(filePath)) {
sqf::diag_log("ASP: Found a Configuration File"sv);
std::ifstream file(filePath);
try {
json = nlohmann::json::parse(file);
}
catch (nlohmann::json::exception& error) {
// log error, set parameterFile false, and continue by using getCommandLineParam
sqf::diag_log(error.what());

}
}

auto startAdapter = getCommandLineParam("-profilerAdapter"sv);

if (startAdapter) {
Expand All @@ -1319,7 +1407,6 @@ void scriptProfiler::preStart() {
auto chromeAdapter = std::make_shared<AdapterChrome>();
GProfilerAdapter = chromeAdapter;
sqf::diag_log("ASP: Selected Chrome Adapter"sv);

auto chromeOutput = getCommandLineParam("-profilerOutput"sv);
if (chromeOutput)
chromeAdapter->setTargetFile(*chromeOutput);
Expand Down

0 comments on commit 9efeca7

Please sign in to comment.