techmeet.love/debug.lua

87 lines
2.6 KiB
Lua

F = {
-- two charts; one fine-grained and one coarse-grained
data1={}, -- fine-grained samples: memory footprint size
data2={}, -- coarse-grained samples: each is mean of X fine-grained samples
N1 = 300, -- # of fine-grained samples to show
N2 = 300, -- # of coarse-grained samples to show
X = 10, -- number of fine-grained samples that make up one coarse-grained sample; must be <= N1
previous_sample=0, -- time at which we took previous fine-grained sample
nsamples=0, -- running count of fine-grained samples taken since previous coarse-grained sample
}
function update_footprint()
if Current_time-F.previous_sample < 1 then
return
end
F.previous_sample = Current_time
assert(#F.data1 <= F.N1)
if #F.data1 == F.N1 then
table.remove(F.data1, 1)
end
table.insert(F.data1, collectgarbage('count'))
F.nsamples = F.nsamples+1
-- if we ever have an error in data collection we quickly lose it because this triggers
--assert(F.nsamples <= F.X)
-- don't trust nsamples beyond this point
if F.nsamples < F.X then
return
end
assert(#F.data2 <= F.N2)
if #F.data2 == F.N2 then
table.remove(F.data2, 1)
end
table.insert(F.data2, mean(F.data1, #F.data1-F.X+1, #F.data1))
F.nsamples = 0
end
function draw_debug()
if not Display_settings.show_debug then return end
App.color{r=1,g=1,b=1}
love.graphics.rectangle('fill', App.screen.width-900,App.screen.height-460, 900,460)
App.color{r=0.6,g=0.6,b=0.6}
love.graphics.rectangle('line', App.screen.width-895,App.screen.height-455, 890,450)
App.color{r=0,g=0,b=0}
draw_graph(F.data1, 'fast', App.screen.width-820,App.screen.height-445, 800,200, F.N1)
draw_graph(F.data2, 'slow', App.screen.width-820,App.screen.height-235, 800,200, F.N2)
end
function draw_graph(data, title, x,y, w,h, n)
love.graphics.line(x,y, x,y+h)
love.graphics.line(x,y+h, x+w,y+h)
love.graphics.print(title, x+w/2,y+h+5)
if #data == 0 then return end
local dx = w/n
local maxy = max(data)
maxy = math.floor(maxy*1.1/100)*100
love.graphics.print(tostring(maxy), x-70, y)
for i,val in ipairs(data) do
local xx = x + (i-1)*dx
local yy = y + (1 - val/maxy)*h
love.graphics.circle('fill', xx,yy, 2)
end
end
function sum(arr, lo, hi)
local result = 0
for i=lo,hi do
result = result + arr[i]
end
return result
end
function mean(arr, lo, hi)
return sum(arr, lo, hi)/(hi-lo+1)
end
function max(arr)
if #arr == 0 then return end
local result = arr[1]
for _,v in ipairs(arr) do
if v > result then
result = v
end
end
assert(result)
return result
end