@@ -9,18 +9,22 @@ CScriptProfiler::CScriptProfiler(CScriptEngine* engine)
9
9
m_engine = engine;
10
10
11
11
m_active = false ;
12
- m_hook_profile_level = 1 ;
13
12
m_profiler_type = CScriptProfilerType::None;
13
+ m_sampling_profile_interval = PROFILE_SAMPLING_INTERVAL_DEFAULT;
14
+ m_hook_profile_level = PROFILE_HOOK_LEVEL_DEFAULT;
14
15
15
- if (strstr (Core.Params , " -lua_profiler " ))
16
+ if (strstr (Core.Params , ARGUMENT_PROFILER_DEFAULT ))
16
17
start ();
17
- else if (strstr (Core.Params , " -lua_hook_profiler " ))
18
+ else if (strstr (Core.Params , ARGUMENT_PROFILER_HOOK ))
18
19
start (CScriptProfilerType::Hook);
19
- else if (strstr (Core.Params , " -lua_sampling_profiler " ))
20
+ else if (strstr (Core.Params , ARGUMENT_PROFILER_SAMPLING ))
20
21
start (CScriptProfilerType::Sampling);
21
22
}
22
23
23
- CScriptProfiler::~CScriptProfiler () {}
24
+ CScriptProfiler::~CScriptProfiler ()
25
+ {
26
+ m_engine = nullptr ;
27
+ }
24
28
25
29
void CScriptProfiler::start (CScriptProfilerType profiler_type)
26
30
{
@@ -70,8 +74,8 @@ void CScriptProfiler::start(CScriptProfilerType profiler_type)
70
74
return ;
71
75
}
72
76
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 );
75
79
76
80
m_profiler_type = profiler_type;
77
81
m_active = true ;
@@ -254,13 +258,60 @@ void CScriptProfiler::logSamplingReport()
254
258
255
259
void CScriptProfiler::saveReport ()
256
260
{
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" );
258
282
259
283
// todo;
260
284
// todo;
261
285
// todo;
262
286
}
263
287
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
+
264
315
/*
265
316
* @returns whether profiling lua hook was/is attached to current VM context
266
317
*/
@@ -284,10 +335,6 @@ void CScriptProfiler::onDispose(lua_State* L)
284
335
285
336
void CScriptProfiler::onReinit (lua_State* L)
286
337
{
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
-
291
338
if (!m_active)
292
339
return ;
293
340
@@ -315,13 +362,13 @@ void CScriptProfiler::onReinit(lua_State* L)
315
362
return ;
316
363
}
317
364
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 );
320
367
321
368
return ;
322
369
}
323
370
324
- default : NODEFAULT;
371
+ default : NODEFAULT;
325
372
}
326
373
}
327
374
@@ -458,8 +505,11 @@ bool CScriptProfiler::luaIsJitProfilerDefined(lua_State* L)
458
505
* Attach sampling profiling hooks.
459
506
* With provided period report samples and store information in profiler for further reporting.
460
507
*/
461
- void CScriptProfiler::luaJitSamplingProfilerAttach (CScriptProfiler* profiler)
508
+ void CScriptProfiler::luaJitSamplingProfilerAttach (CScriptProfiler* profiler, u32 interval )
462
509
{
510
+ string32 buffer = " fli" ;
511
+ xr_itoa (interval, buffer + 3 , 10 );
512
+
463
513
luaJitProfilerStart (
464
514
profiler->lua (), " fli" ,
465
515
[](void * data, lua_State* L, int samples, int vmstate) {
0 commit comments