Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 784d700

Browse files
committedJan 17, 2025
Const expressions for args. Use interval param in sampling profiler.
1 parent 251d7fd commit 784d700

File tree

2 files changed

+89
-31
lines changed

2 files changed

+89
-31
lines changed
 

‎src/xrScriptEngine/script_profiler.cpp

+66-16
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ CScriptProfiler::CScriptProfiler(CScriptEngine* engine)
99
m_engine = engine;
1010

1111
m_active = false;
12-
m_hook_profile_level = 1;
1312
m_profiler_type = CScriptProfilerType::None;
13+
m_sampling_profile_interval = PROFILE_SAMPLING_INTERVAL_DEFAULT;
14+
m_hook_profile_level = PROFILE_HOOK_LEVEL_DEFAULT;
1415

15-
if (strstr(Core.Params, "-lua_profiler"))
16+
if (strstr(Core.Params, ARGUMENT_PROFILER_DEFAULT))
1617
start();
17-
else if (strstr(Core.Params, "-lua_hook_profiler"))
18+
else if (strstr(Core.Params, ARGUMENT_PROFILER_HOOK))
1819
start(CScriptProfilerType::Hook);
19-
else if (strstr(Core.Params, "-lua_sampling_profiler"))
20+
else if (strstr(Core.Params, ARGUMENT_PROFILER_SAMPLING))
2021
start(CScriptProfilerType::Sampling);
2122
}
2223

23-
CScriptProfiler::~CScriptProfiler() {}
24+
CScriptProfiler::~CScriptProfiler()
25+
{
26+
m_engine = nullptr;
27+
}
2428

2529
void CScriptProfiler::start(CScriptProfilerType profiler_type)
2630
{
@@ -70,8 +74,8 @@ void CScriptProfiler::start(CScriptProfilerType profiler_type)
7074
return;
7175
}
7276

73-
Msg("[P] Starting scripts sampling profiler");
74-
luaJitSamplingProfilerAttach(this);
77+
Msg("[P] Starting scripts sampling profiler, interval: %d", m_sampling_profile_interval);
78+
luaJitSamplingProfilerAttach(this, m_sampling_profile_interval);
7579

7680
m_profiler_type = profiler_type;
7781
m_active = true;
@@ -254,13 +258,60 @@ void CScriptProfiler::logSamplingReport()
254258

255259
void CScriptProfiler::saveReport()
256260
{
257-
Log("[P] Saving profiler report");
261+
switch (m_profiler_type)
262+
{
263+
case CScriptProfilerType::Hook:
264+
return saveHookReport();
265+
case CScriptProfilerType::Sampling:
266+
return saveSamplingReport();
267+
default:
268+
Msg("[P] No active profiling data to save report");
269+
return;
270+
}
271+
}
272+
273+
void CScriptProfiler::saveHookReport()
274+
{
275+
if (m_hook_profiling_portions.empty())
276+
{
277+
Msg("[P] Nothing to report for hook profiler, data is missing");
278+
return;
279+
}
280+
281+
Log("[P] Saving hook profiler report");
258282

259283
// todo;
260284
// todo;
261285
// todo;
262286
}
263287

288+
void CScriptProfiler::saveSamplingReport()
289+
{
290+
if (m_sampling_profiling_log.empty())
291+
{
292+
Msg("[P] Nothing to report for sampling profiler, data is missing");
293+
return;
294+
}
295+
296+
string_path log_file_name;
297+
strconcat(sizeof(log_file_name), log_file_name, Core.ApplicationName, "_", Core.UserName, "_sampling_profile.perf");
298+
FS.update_path(log_file_name, "$logs$", log_file_name);
299+
300+
Msg("[P] Saving sampling report to %s", log_file_name);
301+
302+
IWriter* F = FS.w_open(log_file_name);
303+
304+
if (F)
305+
{
306+
for (auto &it : m_sampling_profiling_log)
307+
{
308+
F->w_string(*it);
309+
}
310+
311+
FS.w_close(F);
312+
}
313+
}
314+
264315
/*
265316
* @returns whether profiling lua hook was/is attached to current VM context
266317
*/
@@ -284,10 +335,6 @@ void CScriptProfiler::onDispose(lua_State* L)
284335

285336
void CScriptProfiler::onReinit(lua_State* L)
286337
{
287-
// todo: Should we get old ref and detach profiler / hook?
288-
// todo: Should we get old ref and detach profiler / hook?
289-
// todo: Should we get old ref and detach profiler / hook?
290-
291338
if (!m_active)
292339
return;
293340

@@ -315,13 +362,13 @@ void CScriptProfiler::onReinit(lua_State* L)
315362
return;
316363
}
317364

318-
Msg("[P] Re-init scripts sampling profiler, attach handler");
319-
luaJitSamplingProfilerAttach(this);
365+
Msg("[P] Re-init scripts sampling profiler - attach handler, interval: %d", m_sampling_profile_interval);
366+
luaJitSamplingProfilerAttach(this, m_sampling_profile_interval);
320367

321368
return;
322369
}
323370

324-
default: NODEFAULT;
371+
default: NODEFAULT;
325372
}
326373
}
327374

@@ -458,8 +505,11 @@ bool CScriptProfiler::luaIsJitProfilerDefined(lua_State* L)
458505
* Attach sampling profiling hooks.
459506
* With provided period report samples and store information in profiler for further reporting.
460507
*/
461-
void CScriptProfiler::luaJitSamplingProfilerAttach(CScriptProfiler* profiler)
508+
void CScriptProfiler::luaJitSamplingProfilerAttach(CScriptProfiler* profiler, u32 interval)
462509
{
510+
string32 buffer = "fli";
511+
xr_itoa(interval, buffer + 3, 10);
512+
463513
luaJitProfilerStart(
464514
profiler->lua(), "fli",
465515
[](void* data, lua_State* L, int samples, int vmstate) {

‎src/xrScriptEngine/script_profiler.hpp

+23-15
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,30 @@ class XRSCRIPTENGINE_API CScriptProfiler
7676
using Duration = Clock::duration;
7777

7878
private:
79+
constexpr static cpcstr ARGUMENT_PROFILER_DEFAULT = "-lua_profiler";
80+
constexpr static cpcstr ARGUMENT_PROFILER_HOOK = "-lua_hook_profiler";
81+
constexpr static cpcstr ARGUMENT_PROFILER_SAMPLING = "-lua_sampling_profiler";
82+
7983
static const u32 PROFILE_ENTRIES_LOG_LIMIT = 128;
84+
static const u32 PROFILE_HOOK_LEVEL_DEFAULT = 1;
85+
static const u32 PROFILE_SAMPLING_INTERVAL_DEFAULT = 10;
86+
static const u32 PROFILE_SAMPLING_INTERVAL_MAX = 1000;
8087

8188
CScriptEngine* m_engine;
8289

8390
bool m_active;
84-
85-
// todo: Add period parameter for sampling profiler.
86-
87-
// Profiling level - number of stacks to check before each function call.
88-
// Helps validating results of same functions called from different places vs totals by specific function.
89-
// todo: Handle level / provide level properly.
90-
u8 m_hook_profile_level;
9191
CScriptProfilerType m_profiler_type;
92-
// todo: Profiler-type specific naming for containers.
93-
// todo: Profiler-type specific naming for containers.
94-
// todo: Profiler-type specific naming for containers.
92+
93+
/*
94+
* Profiling level - number of stacks to check before each function call.
95+
* Helps validating results of same functions called from different places vs totals by specific function.
96+
*/
97+
u32 m_hook_profile_level;
98+
/*
99+
* Sampling interval for JIT based profiler.
100+
* Value should be set in ms and defaults to 10ms.
101+
*/
102+
u32 m_sampling_profile_interval;
95103
xr_unordered_map<shared_str, CScriptProfilerHookPortion> m_hook_profiling_portions;
96104
xr_vector<shared_str> m_sampling_profiling_log;
97105

@@ -109,20 +117,20 @@ class XRSCRIPTENGINE_API CScriptProfiler
109117
void logHookReport();
110118
void logSamplingReport();
111119
void saveReport();
112-
// todo: Save sampling report.
113-
// todo: Save hook.
120+
void saveHookReport();
121+
void saveSamplingReport();
114122

115-
bool attachLuaHook();
116123
void onReinit(lua_State* L);
117124
void onDispose(lua_State* L);
118125
void onLuaHookCall(lua_State* L, lua_Debug* dbg);
119126

120127
private:
121128
lua_State* lua() const;
129+
bool attachLuaHook();
122130

123-
static int luaMemoryUsed(lua_State* L);
131+
static int luaMemoryUsed(lua_State* L);
124132
static bool luaIsJitProfilerDefined(lua_State* L);
125-
static void luaJitSamplingProfilerAttach(CScriptProfiler* profiler);
133+
static void luaJitSamplingProfilerAttach(CScriptProfiler* profiler, u32 interval);
126134
static void luaJitProfilerStart(lua_State* L, cpcstr mode, luaJIT_profile_callback callback, void* data);
127135
static void luaJitProfilerStop(lua_State* L);
128136
static std::pair<cpcstr, size_t> luaJitProfilerDump(lua_State* L, cpcstr format, int depth);

0 commit comments

Comments
 (0)
Please sign in to comment.