lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Roberto Ierusalimschy wrote:
> How do you draw those amazing graphs?

I just added a function to print a counter plus the three key variables
to stdout (in C, to avoid influencing the GC). This function (gcstats)
is then called on every iteration of the linked list test from John Skaller:

local listlen, count = tonumber(arg[1]), tonumber(arg[2])

local function mklist(n, tail)
  if n == 0 then return tail end;
  local head = {}
  local t = tail
  t2 = mklist(n - 1, t)
  local node = {} = t2 = n
  return node

collectgarbage("setincmode", 0)     -- change this to 1 for incremental mode

local t = {}
local j = 0

for i=1,count do
  gcstats()                         -- print GC statistics to stdout
  j = j + 1
  if j > 100 then j = 0 end
  t[j] = mklist(listlen, nil)

Then I run

  lua linklist.lua 50 2500 >gc.dat

and use gnuplot to draw the graph:

set grid
set yrange [ 0:6000 ]

plot "gc.dat" u 1:2 t "total" w l, \
     "gc.dat" u 1:3 t "threshold" w l, \
     "gc.dat" u 1:4 t "estimate" w l

pause -1 "Press return"

The yrange is there just so all graphs have the same scale. Remove this
line to get autoscaling.

Adapting this approach to other scenarios requires printing the GC stats
in regular intervals (e.g. with a separate thread).

Interactively watching the GC stats is feasible, too. John suggested gscope.
LibVNCServer may be another alternative. But this is a lot more work if you
want to do it right (e.g. allow watching apps on remote servers).