@@ -7,11 +7,10 @@ CScriptProfiler::CScriptProfiler(CScriptEngine* engine)
7
7
R_ASSERT (engine != NULL );
8
8
9
9
m_engine = engine;
10
-
11
10
m_active = false ;
12
11
m_profiler_type = CScriptProfilerType::None;
13
12
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 ;
15
14
16
15
if (strstr (Core.Params , ARGUMENT_PROFILER_DEFAULT))
17
16
start ();
@@ -28,63 +27,88 @@ CScriptProfiler::~CScriptProfiler()
28
27
29
28
void CScriptProfiler::start (CScriptProfilerType profiler_type)
30
29
{
31
- if (m_active )
30
+ switch (profiler_type )
32
31
{
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;
35
42
}
43
+ }
36
44
37
- if (profiler_type == CScriptProfilerType::None)
45
+ void CScriptProfiler::startHookMode (u32 stack_depth)
46
+ {
47
+ if (m_active)
38
48
{
39
- Msg (" [P] Tried to start none type profiler" );
49
+ Msg (" [P] Tried to start already active profiler, operation ignored " );
40
50
return ;
41
51
}
42
52
43
53
if (!lua ())
44
54
{
45
- Msg (" [P] Activating profiler on lua engine start, waiting init" );
55
+ Msg (" [P] Activating hook profiler on lua engine start, waiting init" );
46
56
47
- m_profiler_type = profiler_type ;
57
+ m_profiler_type = CScriptProfilerType::Hook ;
48
58
m_active = true ;
49
59
50
60
return ;
51
61
}
52
62
53
- switch (profiler_type)
63
+ clamp (stack_depth, 0u , PROFILE_SAMPLING_INTERVAL_MAX);
64
+
65
+ if (!attachLuaHook ())
54
66
{
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
+ }
61
70
62
- Msg (" [P] Starting scripts hook profiler" );
71
+ Msg (" [P] Starting scripts hook profiler, depth: %d " , stack_depth );
63
72
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
+ }
67
78
68
- return ;
69
- case CScriptProfilerType::Sampling:
79
+ void CScriptProfiler::startSamplingMode (u32 sampling_interval)
80
+ {
81
+ if (m_active)
70
82
{
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
+ }
76
86
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" );
79
90
80
- m_profiler_type = profiler_type ;
91
+ m_profiler_type = CScriptProfilerType::Sampling ;
81
92
m_active = true ;
82
93
83
- return ;
94
+ return ;
84
95
}
85
96
86
- default : NODEFAULT;
97
+ if (!luaIsJitProfilerDefined (lua ()))
98
+ {
99
+ Msg (" [P] Cannot start scripts sampling profiler, jit.profiler module is not defined" );
100
+ return ;
87
101
}
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 ;
88
112
}
89
113
90
114
void CScriptProfiler::stop ()
@@ -380,8 +404,11 @@ void CScriptProfiler::onLuaHookCall(lua_State* L, lua_Debug* dbg)
380
404
lua_Debug parent_stack_info;
381
405
lua_Debug stack_info;
382
406
407
+ // todo: Implement dynamic depth.
408
+ // todo: Implement dynamic depth.
409
+
383
410
// Check higher level of stack.
384
- if (m_hook_profile_level > 0 )
411
+ if (m_hook_profile_depth > 0 )
385
412
{
386
413
if (!lua_getstack (L, 1 , &parent_stack_info))
387
414
{
@@ -401,15 +428,15 @@ void CScriptProfiler::onLuaHookCall(lua_State* L, lua_Debug* dbg)
401
428
string512 buffer;
402
429
403
430
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 : " ?" ;
405
432
auto short_src = stack_info.short_src ;
406
433
auto line_defined = stack_info.linedefined ;
407
434
408
435
if (!stack_info.name && line_defined == 0 )
409
436
name = " lua-script-body" ;
410
437
411
438
// Include date from higher stack levels.
412
- if (m_hook_profile_level > 0 )
439
+ if (m_hook_profile_depth > 0 )
413
440
xr_sprintf (buffer, " %s [%d] - %s @ %s" , name, line_defined, parent_name, short_src);
414
441
else
415
442
xr_sprintf (buffer, " %s [%d] @ %s" , name, line_defined, parent_name, short_src);
@@ -511,7 +538,7 @@ void CScriptProfiler::luaJitSamplingProfilerAttach(CScriptProfiler* profiler, u3
511
538
xr_itoa (interval, buffer + 3 , 10 );
512
539
513
540
luaJitProfilerStart (
514
- profiler->lua (), " fli " ,
541
+ profiler->lua (), buffer ,
515
542
[](void * data, lua_State* L, int samples, int vmstate) {
516
543
CScriptProfiler* profiler = static_cast <CScriptProfiler*>(data);
517
544
string2048 buffer;
0 commit comments