Skip to content

Commit a7d4c4b

Browse files
committed
Separate hook start methods, simple generic start with defaults. Declare separate start methods in script export.
1 parent 784d700 commit a7d4c4b

File tree

3 files changed

+79
-41
lines changed

3 files changed

+79
-41
lines changed

src/xrScriptEngine/ScriptEngineScript.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ SCRIPT_EXPORT(CScriptEngine, (),
183183
{
184184
GEnv.ScriptEngine->m_profiler->start(hook_type);
185185
}),
186+
def("start_sampling_mode", +[](u32 sampling_interval)
187+
{
188+
GEnv.ScriptEngine->m_profiler->startSamplingMode(sampling_interval);
189+
}),
190+
def("start_hook_mode", +[](u32 stack_depth)
191+
{
192+
GEnv.ScriptEngine->m_profiler->startHookMode(stack_depth);
193+
}),
186194
def("stop", +[]()
187195
{
188196
GEnv.ScriptEngine->m_profiler->stop();

src/xrScriptEngine/script_profiler.cpp

+63-36
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ CScriptProfiler::CScriptProfiler(CScriptEngine* engine)
77
R_ASSERT(engine != NULL);
88

99
m_engine = engine;
10-
1110
m_active = false;
1211
m_profiler_type = CScriptProfilerType::None;
1312
m_sampling_profile_interval = PROFILE_SAMPLING_INTERVAL_DEFAULT;
14-
m_hook_profile_level = PROFILE_HOOK_LEVEL_DEFAULT;
13+
m_hook_profile_depth = PROFILE_HOOK_DEPTH_DEFAULT;
1514

1615
if (strstr(Core.Params, ARGUMENT_PROFILER_DEFAULT))
1716
start();
@@ -28,63 +27,88 @@ CScriptProfiler::~CScriptProfiler()
2827

2928
void CScriptProfiler::start(CScriptProfilerType profiler_type)
3029
{
31-
if (m_active)
30+
switch (profiler_type)
3231
{
33-
Msg("[P] Tried to start already active profiler, operation ignored");
34-
return;
32+
case CScriptProfilerType::Hook:
33+
startHookMode(PROFILE_HOOK_DEPTH_DEFAULT);
34+
return;
35+
case CScriptProfilerType::Sampling:
36+
startSamplingMode(PROFILE_SAMPLING_INTERVAL_DEFAULT);
37+
return;
38+
case CScriptProfilerType::None:
39+
Msg("[P] Tried to start none type profiler");
40+
return;
41+
default: NODEFAULT;
3542
}
43+
}
3644

37-
if (profiler_type == CScriptProfilerType::None)
45+
void CScriptProfiler::startHookMode(u32 stack_depth)
46+
{
47+
if (m_active)
3848
{
39-
Msg("[P] Tried to start none type profiler");
49+
Msg("[P] Tried to start already active profiler, operation ignored");
4050
return;
4151
}
4252

4353
if (!lua())
4454
{
45-
Msg("[P] Activating profiler on lua engine start, waiting init");
55+
Msg("[P] Activating hook profiler on lua engine start, waiting init");
4656

47-
m_profiler_type = profiler_type;
57+
m_profiler_type = CScriptProfilerType::Hook;
4858
m_active = true;
4959

5060
return;
5161
}
5262

53-
switch (profiler_type)
63+
clamp(stack_depth, 0u, PROFILE_SAMPLING_INTERVAL_MAX);
64+
65+
if (!attachLuaHook())
5466
{
55-
case CScriptProfilerType::Hook:
56-
if (!attachLuaHook())
57-
{
58-
Msg("[P] Cannot start scripts hook profiler, hook was not set properly");
59-
return;
60-
}
67+
Msg("[P] Cannot start scripts hook profiler, hook was not set properly");
68+
return;
69+
}
6170

62-
Msg("[P] Starting scripts hook profiler");
71+
Msg("[P] Starting scripts hook profiler, depth: %d", stack_depth);
6372

64-
m_hook_profiling_portions.clear();
65-
m_profiler_type = profiler_type;
66-
m_active = true;
73+
m_hook_profile_depth = stack_depth;
74+
m_hook_profiling_portions.clear();
75+
m_profiler_type = CScriptProfilerType::Hook;
76+
m_active = true;
77+
}
6778

68-
return;
69-
case CScriptProfilerType::Sampling:
79+
void CScriptProfiler::startSamplingMode(u32 sampling_interval)
80+
{
81+
if (m_active)
7082
{
71-
if (!luaIsJitProfilerDefined(lua()))
72-
{
73-
Msg("[P] Cannot start scripts sampling profiler, jit.profiler module is not defined");
74-
return;
75-
}
83+
Msg("[P] Tried to start already active profiler, operation ignored");
84+
return;
85+
}
7686

77-
Msg("[P] Starting scripts sampling profiler, interval: %d", m_sampling_profile_interval);
78-
luaJitSamplingProfilerAttach(this, m_sampling_profile_interval);
87+
if (!lua())
88+
{
89+
Msg("[P] Activating sampling profiler on lua engine start, waiting init");
7990

80-
m_profiler_type = profiler_type;
91+
m_profiler_type = CScriptProfilerType::Sampling;
8192
m_active = true;
8293

83-
return;
94+
return;
8495
}
8596

86-
default: NODEFAULT;
97+
if (!luaIsJitProfilerDefined(lua()))
98+
{
99+
Msg("[P] Cannot start scripts sampling profiler, jit.profiler module is not defined");
100+
return;
87101
}
102+
103+
clamp(sampling_interval, 1u, PROFILE_SAMPLING_INTERVAL_MAX);
104+
105+
Msg("[P] Starting scripts sampling profiler, interval: %d", sampling_interval);
106+
107+
luaJitSamplingProfilerAttach(this, sampling_interval);
108+
109+
m_sampling_profile_interval = sampling_interval;
110+
m_profiler_type = CScriptProfilerType::Sampling;
111+
m_active = true;
88112
}
89113

90114
void CScriptProfiler::stop()
@@ -380,8 +404,11 @@ void CScriptProfiler::onLuaHookCall(lua_State* L, lua_Debug* dbg)
380404
lua_Debug parent_stack_info;
381405
lua_Debug stack_info;
382406

407+
// todo: Implement dynamic depth.
408+
// todo: Implement dynamic depth.
409+
383410
// Check higher level of stack.
384-
if (m_hook_profile_level > 0)
411+
if (m_hook_profile_depth > 0)
385412
{
386413
if (!lua_getstack(L, 1, &parent_stack_info))
387414
{
@@ -401,15 +428,15 @@ void CScriptProfiler::onLuaHookCall(lua_State* L, lua_Debug* dbg)
401428
string512 buffer;
402429

403430
auto name = stack_info.name ? stack_info.name : "?";
404-
auto parent_name = m_hook_profile_level > 0 && parent_stack_info.name ? parent_stack_info.name : "?";
431+
auto parent_name = m_hook_profile_depth > 0 && parent_stack_info.name ? parent_stack_info.name : "?";
405432
auto short_src = stack_info.short_src;
406433
auto line_defined = stack_info.linedefined;
407434

408435
if (!stack_info.name && line_defined == 0)
409436
name = "lua-script-body";
410437

411438
// Include date from higher stack levels.
412-
if (m_hook_profile_level > 0)
439+
if (m_hook_profile_depth > 0)
413440
xr_sprintf(buffer, "%s [%d] - %s @ %s", name, line_defined, parent_name, short_src);
414441
else
415442
xr_sprintf(buffer, "%s [%d] @ %s", name, line_defined, parent_name, short_src);
@@ -511,7 +538,7 @@ void CScriptProfiler::luaJitSamplingProfilerAttach(CScriptProfiler* profiler, u3
511538
xr_itoa(interval, buffer + 3, 10);
512539

513540
luaJitProfilerStart(
514-
profiler->lua(), "fli",
541+
profiler->lua(), buffer,
515542
[](void* data, lua_State* L, int samples, int vmstate) {
516543
CScriptProfiler* profiler = static_cast<CScriptProfiler*>(data);
517544
string2048 buffer;

src/xrScriptEngine/script_profiler.hpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,26 @@ class XRSCRIPTENGINE_API CScriptProfiler
7676
using Duration = Clock::duration;
7777

7878
private:
79+
// List of commnad line args for startup profuler attach:
7980
constexpr static cpcstr ARGUMENT_PROFILER_DEFAULT = "-lua_profiler";
8081
constexpr static cpcstr ARGUMENT_PROFILER_HOOK = "-lua_hook_profiler";
8182
constexpr static cpcstr ARGUMENT_PROFILER_SAMPLING = "-lua_sampling_profiler";
8283

8384
static const u32 PROFILE_ENTRIES_LOG_LIMIT = 128;
84-
static const u32 PROFILE_HOOK_LEVEL_DEFAULT = 1;
85+
static const u32 PROFILE_HOOK_DEPTH_DEFAULT = 1;
86+
static const u32 PROFILE_HOOK_DEPTH_MAX = 1;
8587
static const u32 PROFILE_SAMPLING_INTERVAL_DEFAULT = 10;
8688
static const u32 PROFILE_SAMPLING_INTERVAL_MAX = 1000;
8789

8890
CScriptEngine* m_engine;
89-
90-
bool m_active;
9191
CScriptProfilerType m_profiler_type;
92+
bool m_active;
9293

9394
/*
94-
* Profiling level - number of stacks to check before each function call.
95+
* Profiling depth - number of stacks to summarise function call trace.
9596
* Helps validating results of same functions called from different places vs totals by specific function.
9697
*/
97-
u32 m_hook_profile_level;
98+
u32 m_hook_profile_depth;
9899
/*
99100
* Sampling interval for JIT based profiler.
100101
* Value should be set in ms and defaults to 10ms.
@@ -110,6 +111,8 @@ class XRSCRIPTENGINE_API CScriptProfiler
110111
bool isActive() const { return m_active; };
111112

112113
void start(CScriptProfilerType profiler_type = CScriptProfilerType::Hook);
114+
void startSamplingMode(u32 sampling_interval);
115+
void startHookMode(u32 stack_depth);
113116
void stop();
114117
void reset();
115118

0 commit comments

Comments
 (0)