Skip to content

feat: add Lua GC flags #5194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

feat: add Lua GC flags #5194

wants to merge 3 commits into from

Conversation

BorysTheDev
Copy link
Contributor

I've added 2 flags:

  • luagc - to tune lua gc
  • lua_mem_usage_force_gc - to do force GC

I have done some tests with lua_mem_usage_force_gc, and it shows quite good results to my mind, and it works fast.
Regarding the luagc flag, I've tried to use different parameters, according to https://www.lua.org/manual/5.4/manual.html#2.5.1 , and the results were insignificant, but with aggressive parameters performance can be dropped significantly

@BorysTheDev BorysTheDev requested review from romange and kostasrim May 28, 2025 10:25
@@ -1109,6 +1203,16 @@ Interpreter* InterpreterManager::Get() {
}

void InterpreterManager::Return(Interpreter* ir) {
static const uint64_t max_memory_usage = absl::GetFlag(FLAGS_lua_mem_usage_force_gc);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would imagine we would like to change these flags without restarting the process.
please add support to "CONFIG SET" as well.

@BorysTheDev BorysTheDev requested review from romange and adiholden May 30, 2025 11:00
static const uint64_t max_memory_usage = absl::GetFlag(FLAGS_lua_mem_usage_force_gc);
using namespace chrono;
++tl_stats().interpreter_return;
if (tl_stats().used_bytes > max_memory_usage) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the following scenario. We have a per thread limit of 100MB

  1. Lua1 allocates 1MB, does not trigger gc.
  2. .... Lua99 does not trigger GC and we have like 99MB returned.
  3. Now we have bunch of scripts created and returned - they allocate small blobs but at some point they cross 100MB so they being GC'ed and the lua memory usage goes around 100MB because the already returned scripts are not GCed.

To summarize - we have a shared per-thread threshold, but we perform GC on lua we currently return that can have only 10KB of allocated memory and most of the allocations are kept in the dormant interperters.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not that I have a proposal, but lets introduce a variable that tracks gc collected memory by counting LUA_GCCOUNT before and after RunGC. In fact, RunGC could return the amount of memory it released.
This way we will be able to estimate GC performance rate.

@@ -821,6 +821,8 @@ void Service::Init(util::AcceptServer* acceptor, std::vector<facade::Listener*>

config_registry.RegisterMutable("pipeline_squash");

config_registry.RegisterMutable("lua_mem_usage_force_gc");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's won't cut it and you have not tested that it works.
you used static const uint64_t max_memory_usage = ... so it will be initialised only once

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants