Skip to content

Commit ed17bbc

Browse files
authored
Added option to collect Lua garbage until timeout expires (#1047)
The new console commands are lua_gc_method and lua_gc_timeout. Also, added LuaJIT memory and time consumption display in rs_stats
1 parent 8d12a8b commit ed17bbc

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

Externals/LuaJIT

src/xrGame/Level.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,8 @@ void CLevel::OnFrame()
567567
}
568568
else
569569
m_level_sound_manager->Update();
570-
}
571-
// defer LUA-GC-STEP
572-
if (!GEnv.isDedicatedServer)
573-
{
570+
571+
// defer LUA-GC-STEP
574572
if (g_mt_config.test(mtLUA_GC))
575573
{
576574
Device.seqParallel.push_back(fastdelegate::FastDelegate0<>(this, &CLevel::script_gc));
@@ -588,11 +586,35 @@ void CLevel::OnFrame()
588586
}
589587

590588
int psLUA_GCSTEP = 100; // 10
589+
int psLUA_GCTIMEOUT = 1000;
590+
591+
u32 ps_lua_gc_method = 1;
591592

592593
void CLevel::script_gc()
593594
{
594595
ZoneScoped;
595-
lua_gc(GEnv.ScriptEngine->lua(), LUA_GCSTEP, psLUA_GCSTEP);
596+
AIStats.LuaGC.Begin();
597+
598+
switch (ps_lua_gc_method)
599+
{
600+
case 0:
601+
break;
602+
603+
case 2:
604+
if (lua_gc(GEnv.ScriptEngine->lua(), LUA_GCTIMEOUT, psLUA_GCTIMEOUT) >= 0)
605+
break;
606+
// LUA_GCTIMEOUT is unsupported, fallback to LUA_GCSTEP
607+
[[fallthrough]];
608+
609+
default:
610+
ps_lua_gc_method = 1;
611+
612+
case 1:
613+
lua_gc(GEnv.ScriptEngine->lua(), LUA_GCSTEP, psLUA_GCSTEP);
614+
break;
615+
}
616+
617+
AIStats.LuaGC.End();
596618
}
597619

598620
#ifdef DEBUG_PRECISE_PATH
@@ -795,6 +817,7 @@ void CLevel::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)
795817
font.OutNext("AI vision: %2.2fms, %d", AIStats.Vis.result, AIStats.Vis.count);
796818
font.OutNext("- query: %2.2fms", AIStats.VisQuery.result);
797819
font.OutNext("- rayCast: %2.2fms", AIStats.VisRayTests.result);
820+
font.OutNext("LUA GC: %d Kb, %2.2fms", lua_gc(GEnv.ScriptEngine->lua(), LUA_GCCOUNT, 0), AIStats.LuaGC.result);
798821
AIStats.FrameStart();
799822
}
800823

src/xrGame/Level.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class CLevel : public IGame_Level, public IPureClient
7171
CStatTimer Vis; // visibility detection - total
7272
CStatTimer VisQuery; // visibility detection - portal traversal and frustum culling
7373
CStatTimer VisRayTests; // visibility detection - ray casting
74+
CStatTimer LuaGC; // LuaJIT Garbage collector
7475

7576
AIStatistics() { FrameStart(); }
7677
void FrameStart()
@@ -82,6 +83,7 @@ class CLevel : public IGame_Level, public IPureClient
8283
Vis.FrameStart();
8384
VisQuery.FrameStart();
8485
VisRayTests.FrameStart();
86+
LuaGC.FrameStart();
8587
}
8688

8789
void FrameEnd()
@@ -93,6 +95,7 @@ class CLevel : public IGame_Level, public IPureClient
9395
Vis.FrameEnd();
9496
VisQuery.FrameEnd();
9597
VisRayTests.FrameEnd();
98+
LuaGC.FrameEnd();
9699
}
97100
};
98101
AIStatistics AIStats;

src/xrGame/console_commands.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ ENGINE_API
7575
extern float psHUD_FOV;
7676
extern float psSqueezeVelocity;
7777
extern int psLUA_GCSTEP;
78+
extern int psLUA_GCTIMEOUT;
79+
extern u32 ps_lua_gc_method;
7880
extern int g_auto_ammo_unload;
7981

8082
extern int x_m_x;
@@ -147,6 +149,15 @@ enum E_COMMON_FLAGS
147149
flAiUseTorchDynamicLights = 1
148150
};
149151

152+
const xr_token lua_gc_method_token[] =
153+
{
154+
{ "gc_disable", 0 },
155+
{ "gc_step", 1 },
156+
{ "gc_timeout", 2 },
157+
{ "gc_full", 3 },
158+
{ nullptr, -1 }
159+
};
160+
150161
CUIOptConCom g_OptConCom;
151162

152163
static void full_memory_stats()
@@ -1452,6 +1463,35 @@ class CCC_TimeFactor : public IConsole_Command
14521463

14531464
#endif // MASTER_GOLD
14541465

1466+
class CCC_LuaGCMethod : public CCC_Token
1467+
{
1468+
public:
1469+
CCC_LuaGCMethod(pcstr name) : CCC_Token(name, &ps_lua_gc_method, lua_gc_method_token) {}
1470+
1471+
void Execute(pcstr args) override
1472+
{
1473+
const auto prev = *value;
1474+
CCC_Token::Execute(args);
1475+
1476+
switch (*value)
1477+
{
1478+
case 0:
1479+
lua_gc(GEnv.ScriptEngine->lua(), LUA_GCSTOP, 0);
1480+
break;
1481+
case 1:
1482+
case 2:
1483+
if (prev == 0)
1484+
lua_gc(GEnv.ScriptEngine->lua(), LUA_GCRESTART, 0);
1485+
break;
1486+
case 3:
1487+
// Perform a full garbage collection cycle and return to previous strategy.
1488+
lua_gc(GEnv.ScriptEngine->lua(), LUA_GCCOLLECT, 0);
1489+
*value = prev;
1490+
break;
1491+
}
1492+
}
1493+
};
1494+
14551495
#include "GamePersistent.h"
14561496

14571497
class CCC_MainMenu : public IConsole_Command
@@ -2077,8 +2117,11 @@ void CCC_RegisterCommands()
20772117
CMD3(CCC_Mask, "lua_debug", &g_LuaDebug, 1);
20782118
#endif // MASTER_GOLD
20792119

2080-
#ifdef DEBUG
2120+
CMD1(CCC_LuaGCMethod, "lua_gc_method");
20812121
CMD4(CCC_Integer, "lua_gcstep", &psLUA_GCSTEP, 1, 1000);
2122+
CMD4(CCC_Integer, "lua_gc_timeout", &psLUA_GCTIMEOUT, 1000, 16000);
2123+
2124+
#ifdef DEBUG
20822125
CMD3(CCC_Mask, "ai_debug", &psAI_Flags, aiDebug);
20832126
CMD3(CCC_Mask, "ai_dbg_brain", &psAI_Flags, aiBrain);
20842127
CMD3(CCC_Mask, "ai_dbg_motion", &psAI_Flags, aiMotion);

0 commit comments

Comments
 (0)