Attribute Lua VM C-function CPU time back to Lua source lines.
perf can show that luaH_getshortstr costs 13% CPU, but not which Lua lines contribute that 13%. vLua samples only while a chosen Lua VM C function is on CPU, then attributes those samples to Lua source locations with full call stacks.
- Precise: SIGPROF sampling + PC-range filter — capture Lua stacks only when the target C function is executing
- Lightweight: ~0.03% overhead at 100Hz — safe to leave on in production
- Safe: signal-handler parses stacks in place; ring buffer stores value types only; SIGSEGV trampoline covers hot-reload races
- Compatible: writes pLua-format
.profiles (CPU v2 metadata) for pprof / FlameGraph - Flexible: target C function name is a parameter — profile any Lua VM internal
- Multi-state: each
lua_Statehas its own profiler session; stopping one does not stop others
- Lua 5.3.x only. Headers and the test interpreter are vendored Lua 5.3.6 private VM structs (
lobject.h/lstate.h). The host process must use a matching 5.3.x layout. Lua 5.4 is out of scope and is rejected atstart(). - Linux is the supported platform (SIGPROF /
ITIMER_PROF+ ELF symbol lookup). CI runs onubuntu-latest. Other Unix systems are untested. There is no Windows port.
CMake extracts dep/lua-5.3.6.tar.gz automatically.
./build.sh
# or:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build buildOutputs:
bin/libvlua.so— sampler shared librarybin/vlua— profile converter (if Go is available)build/lua/lua53— matching Lua 5.3.6 interpreter for tests
ctest --test-dir build --output-on-failure
# converter tests (also part of ctest when Go is available):
cd tools && go testRegistered tests include attribution ranking (test_getstr, test_vlua), function-name recovery (test_funcname), session APIs and errors (test_api), two-lua_State independence (unit_test), and hot-reload safety (including a stress run). A crash in the stress test is a failure.
test/bench_vlua.lua is an optional local overhead experiment and is not in CI (the numbers are statistical, not a pass/fail gate):
cd test
../build/lua/lua53 bench_vlua.lualocal v = require "libvlua"
-- Arg 1: Lua VM C function to sample
-- Arg 2: output .pro file (pLua-compatible, CPU v2)
-- Arg 3 (optional): sample interval in milliseconds (default 10)
v.start("luaH_getshortstr", "call.pro")
-- or: v.start("luaH_getshortstr", "call.pro", 5)
do_some_thing()
-- Stop sampling; returns a text hotspot summary
local text, err = v.stop()
print(text)
-- Session helpers (per lua_State)
local st = v.status() -- running, target, output, interval_ms, samples, ticks, hits, backend, config
v.set_config({ cpu_interval_ms = 5 }) -- default for later start() calls
local cfg = v.get_config()
v.close() -- stop if needed and drop the sessionstart() returns 0 on success, or nil, error on failure (target C function not found, stripped symbols, Lua version / VM layout mismatch, already running, conflicting SIGPROF interval, and similar). stop() returns the text report, or nil, error.
Each lua_State has its own session. You can profile two VMs at once; they must use the same sample interval because ITIMER_PROF is process-wide. Sessions are not OS-thread safe: call the APIs and run the VM on the same thread (same limitation as pLua). The SIGPROF handler never calls Lua APIs, allocates, or takes locks.
The default interval is 10 ms (100 Hz), overridable with v.start(..., interval_ms), v.set_config, or the VLUA_CPU_INTERVAL_MS environment variable.
Inject with hookso
lrealstart / lrealstop signatures are unchanged:
# a) Get lua_State* (e.g. first arg of lua_settop in xxx.so)
./hookso arg $PID xxx.so lua_settop 1
# => 123456
# b) Load libvlua.so
./hookso dlopen $PID ./libvlua.so
# c) Start: lrealstart(L, "luaH_getshortstr", "./call.pro")
./hookso call $PID libvlua.so lrealstart i=123456 s="luaH_getshortstr" s="./call.pro"
# d) Stop: lrealstop(L)
./hookso call $PID libvlua.so lrealstop i=123456The tools/ directory converts .pro profile data into FlameGraph SVGs, call graphs, and modern pprof proto files.
- FlameGraph:
show.shfetchesflamegraph.plfrom GitHub if not found locally or inPATH. On CentOS/RHEL you may needyum install perl-open. - Graphviz (call graph PNG):
sudo apt install graphviz/sudo yum install graphviz - pprof (call graph DOT/PNG):
go install github.com/google/pprof@latest(thegoogle-perftoolsapt package was removed on newer Ubuntu)
cd tools
./show.sh ../test
# or invoke the converter directly:
../bin/vlua -i ../test/getstr.pro -proto getstr.pb.gz -pprof getstr.prof -flame getstr.fl
go tool pprof -top getstr.pb.gzThis generates:
<name>.fl: Folded stack traces<name>.svg: Interactive SVG flame graph<name>.prof: Legacy symbolized pprof profile<name>.pb.gz: gzipprofile.protofor moderngo tool pprof<name>.dot/<name>.png: Graphviz call graph (if pprof and graphviz are installed)
show.sh looks for bin/vlua (built by CMake) or builds tools/vlua via Go on demand. The converter has no third-party Go dependencies. Legacy (pre-v2) .pro files still parse; -pprof and -flame work on them. -proto requires a v2 CPU profile (vLua writes these by default).
Simulated table get-by-string hotspots (nested player.role.battle.stat.kill chains), sampling luaH_getshortstr:
Top hotspots (source:line -> count, pct of analysable)
count pct location
152 31.67% @test_getstr.lua:24 -- calc_damage: player.role.battle.weapon.damage = ...
131 27.29% @test_getstr.lua:36 -- update_kill: player.role.battle.stat.kill = ...
63 13.12% @test_getstr.lua:49 -- update_pos: player.role.pos.y = ...
57 11.88% @test_getstr.lua:48 -- update_pos: player.role.pos.x = ...
...
- On optimized Lua builds, tiny helpers such as
luaH_getshortstrare often inlined intoluaH_get/luaH_getstr. Ifstart()succeeds but the report has 0 hits, sample the caller that still has a symbol (nm <binary> | grep luaH_get). CI attribution tests useluaH_getfor that reason. - Do not strip the host binary — static symbols like
luaH_getshortstrlive in.symtab
Verify:nm <binary> | grep luaH_getshortstr - Safe during hot-reload (value-type samples + pointer checks + SIGSEGV trampoline)
- A session is bound to the
lua_State*passed tostart()(coroutine threads are separate states) - Headers / test interpreter are Lua 5.3.6; Lua 5.4 is not supported
lua-family-bucket · pLua · hookso
MIT
