diff options
Diffstat (limited to 'tex/context/base/trac-inf.lua')
-rw-r--r-- | tex/context/base/trac-inf.lua | 164 |
1 files changed, 76 insertions, 88 deletions
diff --git a/tex/context/base/trac-inf.lua b/tex/context/base/trac-inf.lua index 72f03675a..4e9280d50 100644 --- a/tex/context/base/trac-inf.lua +++ b/tex/context/base/trac-inf.lua @@ -6,7 +6,13 @@ if not modules then modules = { } end modules ['trac-inf'] = { license = "see context related readme files" } +-- As we want to protect the global tables, we no longer store the timing +-- in the tables themselves but in a hidden timers table so that we don't +-- get warnings about assignments. This is more efficient than using rawset +-- and rawget. + local format = string.format +local clock = os.gettimeofday or os.clock -- should go in environment local statusinfo, n, registered = { }, 0, { } @@ -15,91 +21,81 @@ statistics = statistics or { } statistics.enable = true statistics.threshold = 0.05 --- timing functions - -local clock = os.gettimeofday or os.clock - -local notimer +local timers = { } -function statistics.hastimer(instance) - return instance and instance.starttime +local function hastiming(instance) + return instance and timers[instance] end -function statistics.resettiming(instance) - if not instance then - notimer = { timing = 0, loadtime = 0 } - else - instance.timing, instance.loadtime = 0, 0 - end +local function resettiming(instance) + timers[instance or "notimer"] = { timing = 0, loadtime = 0 } end -function statistics.starttiming(instance) - if not instance then - notimer = { } - instance = notimer +local function starttiming(instance) + local timer = timers[instance or "notimer"] + if not timer then + timer = { } + timers[instance or "notimer"] = timer end - local it = instance.timing + local it = timer.timing if not it then it = 0 end if it == 0 then - instance.starttime = clock() - if not instance.loadtime then - instance.loadtime = 0 + timer.starttime = clock() + if not timer.loadtime then + timer.loadtime = 0 end - else ---~ logs.report("system","nested timing (%s)",tostring(instance)) end - instance.timing = it + 1 + timer.timing = it + 1 end -function statistics.stoptiming(instance, report) - if not instance then - instance = notimer - end - if instance then - local it = instance.timing - if it > 1 then - instance.timing = it - 1 - else - local starttime = instance.starttime - if starttime then - local stoptime = clock() - local loadtime = stoptime - starttime - instance.stoptime = stoptime - instance.loadtime = instance.loadtime + loadtime - if report then - statistics.report("load time %0.3f",loadtime) - end - instance.timing = 0 - return loadtime +local function stoptiming(instance, report) + local timer = timers[instance or "notimer"] + local it = timer.timing + if it > 1 then + timer.timing = it - 1 + else + local starttime = timer.starttime + if starttime then + local stoptime = clock() + local loadtime = stoptime - starttime + timer.stoptime = stoptime + timer.loadtime = timer.loadtime + loadtime + if report then + statistics.report("load time %0.3f",loadtime) end + timer.timing = 0 + return loadtime end end return 0 end -function statistics.elapsedtime(instance) - if not instance then - instance = notimer - end - return format("%0.3f",(instance and instance.loadtime) or 0) +local function elapsedtime(instance) + local timer = timers[instance or "notimer"] + return format("%0.3f",timer and timer.loadtime or 0) end -function statistics.elapsedindeed(instance) - if not instance then - instance = notimer - end - local t = (instance and instance.loadtime) or 0 - return t > statistics.threshold +local function elapsedindeed(instance) + local timer = timers[instance or "notimer"] + return (timer and timer.loadtime or 0) > statistics.threshold end -function statistics.elapsedseconds(instance,rest) -- returns nil if 0 seconds - if statistics.elapsedindeed(instance) then - return format("%s seconds %s", statistics.elapsedtime(instance),rest or "") +local function elapsedseconds(instance,rest) -- returns nil if 0 seconds + if elapsedindeed(instance) then + return format("%s seconds %s", elapsedtime(instance),rest or "") end end +statistics.hastiming = hastiming +statistics.resettiming = resettiming +statistics.starttiming = starttiming +statistics.stoptiming = stoptiming +statistics.elapsedtime = elapsedtime +statistics.elapsedindeed = elapsedindeed +statistics.elapsedseconds = elapsedseconds + -- general function function statistics.register(tag,fnc) @@ -128,7 +124,6 @@ function statistics.show(reporter) end) register("current memory usage", statistics.memused) register("runtime",statistics.runtime) --- -- for i=1,#statusinfo do local s = statusinfo[i] local r = s[2]() @@ -142,7 +137,13 @@ function statistics.show(reporter) end function statistics.show_job_stat(tag,data,n) - texio.write_nl(format("%-15s: %s - %s","mkiv lua stats",tag:rpadd(n," "),data)) + if type(data) == "table" then + for i=1,#data do + statistics.show_job_stat(tag,data[i],n) + end + else + texio.write_nl(format("%-15s: %s - %s","mkiv lua stats",tag:rpadd(n," "),data)) + end end function statistics.memused() -- no math.round yet -) @@ -150,48 +151,35 @@ function statistics.memused() -- no math.round yet -) return format("%s MB (ctx: %s MB)",round(collectgarbage("count")/1000), round(status.luastate_bytes/1000000)) end -if statistics.runtime then - -- already loaded and set -elseif luatex and luatex.starttime then - statistics.starttime = luatex.starttime - statistics.loadtime = 0 - statistics.timing = 0 -else - statistics.starttiming(statistics) -end +starttiming(statistics) -function statistics.runtime() - statistics.stoptiming(statistics) - return statistics.formatruntime(statistics.elapsedtime(statistics)) +function statistics.formatruntime(runtime) -- indirect so it can be overloaded and + return format("%s seconds", runtime) -- indeed that happens in cure-uti.lua end -function statistics.formatruntime(runtime) - return format("%s seconds", statistics.elapsedtime(statistics)) +function statistics.runtime() + stoptiming(statistics) + return statistics.formatruntime(elapsedtime(statistics)) end function statistics.timed(action,report) - local timer = { } report = report or logs.simple - statistics.starttiming(timer) + starttiming("run") action() - statistics.stoptiming(timer) - report("total runtime: %s",statistics.elapsedtime(timer)) + stoptiming("run") + report("total runtime: %s",elapsedtime("run")) end -- where, not really the best spot for this: commands = commands or { } -local timer - -function commands.resettimer() - statistics.resettiming(timer) - statistics.starttiming(timer) +function commands.resettimer(name) + resettiming(name or "whatever") + starttiming(name or "whatever") end -function commands.elapsedtime() - statistics.stoptiming(timer) - tex.sprint(statistics.elapsedtime(timer)) +function commands.elapsedtime(name) + stoptiming(name or "whatever") + tex.sprint(elapsedtime(name or "whatever")) end - -commands.resettimer() |