summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkxl/trac-tim.lmt
blob: fcba7f1325a13a4d4fe03de242b59b0ae289e13d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
if not modules then modules = { } end modules ['trac-tim'] = {
    version   = 1.001,
    comment   = "companion to m-timing.tex",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local format, gsub = string.format, string.gsub
local concat, sort = table.concat, table.sort
local next, tonumber = next, tonumber

moduledata          = moduledata or { }
local progress      = moduledata.progress or { }
moduledata.progress = progress

local report_timing = logs.reporter("timing")

if not nodes then nodes = { } end -- when loaded in mtxrun

progress.defaultfilename = ((tex and tex.jobname) or "whatever") .. "-luatex-progress"

-- storage

function progress.store()
    nodes.snapshots.takesample()
end

function progress.save(name)
    local filename = (name or progress.defaultfilename) .. ".lut"
    report_timing("saving data in %a",filename)
    table.save(filename,nodes.snapshots.getsamples())
    nodes.snapshots.resetsamples()
end

-- conversion

local processed = { }

local function convert(name)
    name = name ~= "" and name or progress.defaultfilename
    if not processed[name] then
        local pages = 0
        local names = { }
        local top   = { }
        local bot   = { }
        local siz   = { }
        local paths = { }
        local data  = table.load(name .. ".lut")
        if data then
            pages = #data
            if pages > 1 then

                local factor = 100

                local function path(tag,subtag,tagname)
                    local tagname = tag .. ": " .. subtag
                    local b, t, s = nil, nil, { }
                    for k=1,#data do
                        local v = data[k][tag]
                        v = v and v[subtag]
                        if v then
                            if type(v) == "table" then
                                set[tagname] = tonumber(v.set)
                                v = tonumber(v.top)
                            else
                                v = tonumber(v)
                            end
                            if v then
                                if b then
                                    if v > t then t = v end
                                    if v < b then b = v end
                                else
                                    t = v
                                    b = v
                                end
                            else
                                v = 0
                            end
                        else
                            v = 0
                        end
                        s[k] = v
                    end
                    if not b then
                        -- safeguard against updates
                        b = 0
                        t = 0
                    end
                    top[tagname] = gsub(format("%.3f",t),"%.000$","")
                    bot[tagname] = gsub(format("%.3f",b),"%.000$","")
                    local delta = t - b
                    if delta == 0 then
                        delta = 1
                    else
                        delta = factor/delta
                    end
                    for k=1,#s do
                        s[k] = format("(%.3f,%.3f)",k,(s[k]-b)*delta)
                    end
                    paths[tagname] = concat(s,"--")
                    return tagname
                end

                local function collect(category)
                    if data[1][category] then
                        local keys = { }
                        for k=1,#data do
                            for k, v in next, data[k][category] do
                                keys[k] = true
                            end
                        end
                        for k=1,#data do
                            local m = data[k][category]
                            for k, v in next, keys do
                                if not m[k] then m[k] = 0 end
                            end
                        end
                        for k in next, keys do
                            names[#names+1] = path(category,k)
                        end
                    end
                end

                collect("nodes")
                collect("stock")
                collect("memories")
                collect("variables")
                collect("texvariables")
                collect("luavariables")
                collect("texcallbacks")
                collect("mpcallbacks")
                collect("backendcallbacks")

                pages = pages - 1 -- hm
            end
        end

        sort(names)

        processed[name] = {
            names = names,
            top   = top,
            bot   = bot,
            set   = set,
            pages = pages,
            paths = paths,
        }
    end
    return processed[name]
end

progress.convert = convert

function progress.set(name,tag)
    return convert(name).set[tag] or 0
end

function progress.bot(name,tag)
    return convert(name).bot[tag] or 0
end

function progress.top(name,tag)
    return convert(name).top[tag] or 0
end

function progress.pages(name,tag)
    return convert(name).pages or 0
end

function progress.path(name,tag)
    return convert(name).paths[tag] or "origin"
end

function progress.names(name)
    return convert(name).names or { }
end