Skip to content

Commit

Permalink
Added garbage collection counter, for pathological use cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Jul 7, 2009
1 parent afd3ae9 commit 2135bfc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lua_glue.c
Expand Up @@ -475,6 +475,11 @@ void MojoLua_collectGarbage(void)
int pre = 0;
int post = 0;

lua_getglobal(L, MOJOSETUP_NAMESPACE);
if (lua_istable(L, -1)) // namespace is sane?
set_integer(L, 0, "garbagecounter");
lua_pop(L, 1);

pre = (lua_gc(L, LUA_GCCOUNT, 0) * 1024) + lua_gc(L, LUA_GCCOUNTB, 0);
logDebug("Collecting garbage (currently using %0 bytes).", numstr(pre));
ticks = MojoPlatform_ticks();
Expand All @@ -487,7 +492,8 @@ void MojoLua_collectGarbage(void)


// You can trigger the garbage collector with more control in the standard
// Lua runtime, but this notes profiling and statistics via logDebug().
// Lua runtime, but this notes profiling and statistics via logDebug(),
// and resets MojoSetup.garbagecounter to zero.
static int luahook_collectgarbage(lua_State *L)
{
MojoLua_collectGarbage();
Expand Down
18 changes: 18 additions & 0 deletions scripts/mojosetup_init.lua
Expand Up @@ -21,6 +21,24 @@

local _ = MojoSetup.translate

-- Set up the garbage counter. Things that are done in possibly long-running
-- loops that create a lot of junk can optionally set a threshold and call
-- MojoSetup.incrementgarbagecounter() on each iteration. Whenever you hit
-- the threshold, garbage collection runs and resets the counter. This keeps
-- memory usage from spiraling out of control for pathological cases.
-- Note that the counter resets in MojoSetup.collectgarbage() itself, so you
-- don't have to worry about the counter accumulating and firing off extra
-- unnecessary collections if you happen to hit a loop at the wrong time.
MojoSetup.garbagecounter = 0
MojoSetup.garbagethreshold = 500

function MojoSetup.incrementgarbagecount()
MojoSetup.garbagecounter = MojoSetup.garbagecounter + 1
if MojoSetup.garbagecounter >= MojoSetup.garbagethreshold then
MojoSetup.collectgarbage()
end
end

-- Returns three elements: protocol, host, path
function MojoSetup.spliturl(url)
return string.match(url, "^(.+://)(.-)/(.*)")
Expand Down
1 change: 1 addition & 0 deletions scripts/mojosetup_mainline.lua
Expand Up @@ -419,6 +419,7 @@ local function install_file(dest, perms, writefn, desc, manifestkey)
end

MojoSetup.loginfo("Created file '" .. dest .. "'")
MojoSetup.incrementgarbagecount()
end


Expand Down

0 comments on commit 2135bfc

Please sign in to comment.