| 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 |
local jit = require("jit") |
| 44 |
local profile = require("jit.profile") |
| 45 |
local vmdef = require("jit.vmdef") |
| 46 |
local math = math |
| 47 |
local pairs, ipairs, tonumber, floor = pairs, ipairs, tonumber, math.floor |
| 48 |
local sort, format = table.sort, string.format |
| 49 |
local stdout = io.stdout |
| 50 |
local zone |
| 51 |
|
| 52 |
|
| 53 |
local out |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
local prof_ud |
| 58 |
local prof_states, prof_split, prof_min, prof_raw, prof_fmt, prof_depth |
| 59 |
local prof_ann, prof_count1, prof_count2, prof_samples |
| 60 |
|
| 61 |
local map_vmmode = { |
| 62 |
N = "Compiled", |
| 63 |
I = "Interpreted", |
| 64 |
C = "C code", |
| 65 |
G = "Garbage Collector", |
| 66 |
J = "JIT Compiler", |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
local function prof_cb(th, samples, vmmode) |
| 71 |
prof_samples = prof_samples + samples |
| 72 |
local key_stack, key_stack2, key_state |
| 73 |
|
| 74 |
if prof_states then |
| 75 |
if prof_states == "v" then |
| 76 |
key_state = map_vmmode[vmmode] or vmmode |
| 77 |
else |
| 78 |
key_state = zone:get() or "(none)" |
| 79 |
end |
| 80 |
end |
| 81 |
if prof_fmt then |
| 82 |
key_stack = profile.dumpstack(th, prof_fmt, prof_depth) |
| 83 |
key_stack = key_stack:gsub("%[builtin#(%d+)%]", function(x) |
| 84 |
return vmdef.ffnames[tonumber(x)] |
| 85 |
end) |
| 86 |
if prof_split == 2 then |
| 87 |
local k1, k2 = key_stack:match("(.-) [<>] (.*)") |
| 88 |
if k2 then key_stack, key_stack2 = k1, k2 end |
| 89 |
elseif prof_split == 3 then |
| 90 |
key_stack2 = profile.dumpstack(th, "l", 1) |
| 91 |
end |
| 92 |
end |
| 93 |
|
| 94 |
local k1, k2 |
| 95 |
if prof_split == 1 then |
| 96 |
if key_state then |
| 97 |
k1 = key_state |
| 98 |
if key_stack then k2 = key_stack end |
| 99 |
end |
| 100 |
elseif key_stack then |
| 101 |
k1 = key_stack |
| 102 |
if key_stack2 then k2 = key_stack2 elseif key_state then k2 = key_state end |
| 103 |
end |
| 104 |
|
| 105 |
if k1 then |
| 106 |
local t1 = prof_count1 |
| 107 |
t1[k1] = (t1[k1] or 0) + samples |
| 108 |
if k2 then |
| 109 |
local t2 = prof_count2 |
| 110 |
local t3 = t2[k1] |
| 111 |
if not t3 then t3 = {}; t2[k1] = t3 end |
| 112 |
t3[k2] = (t3[k2] or 0) + samples |
| 113 |
end |
| 114 |
end |
| 115 |
end |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
local function prof_top(count1, count2, samples, indent) |
| 121 |
local t, n = {}, 0 |
| 122 |
for k in pairs(count1) do |
| 123 |
n = n + 1 |
| 124 |
t[n] = k |
| 125 |
end |
| 126 |
sort(t, function(a, b) return count1[a] > count1[b] end) |
| 127 |
for i=1,n do |
| 128 |
local k = t[i] |
| 129 |
local v = count1[k] |
| 130 |
local pct = floor(v*100/samples + 0.5) |
| 131 |
if pct < prof_min then break end |
| 132 |
if not prof_raw then |
| 133 |
out:write(format("%s%2d%% %s\n", indent, pct, k)) |
| 134 |
elseif prof_raw == "r" then |
| 135 |
out:write(format("%s%5d %s\n", indent, v, k)) |
| 136 |
else |
| 137 |
out:write(format("%s %d\n", k, v)) |
| 138 |
end |
| 139 |
if count2 then |
| 140 |
local r = count2[k] |
| 141 |
if r then |
| 142 |
prof_top(r, nil, v, (prof_split == 3 or prof_split == 1) and " -- " or |
| 143 |
(prof_depth < 0 and " -> " or " <- ")) |
| 144 |
end |
| 145 |
end |
| 146 |
end |
| 147 |
end |
| 148 |
|
| 149 |
|
| 150 |
local function prof_annotate(count1, samples) |
| 151 |
local files = {} |
| 152 |
local ms = 0 |
| 153 |
for k, v in pairs(count1) do |
| 154 |
local pct = floor(v*100/samples + 0.5) |
| 155 |
ms = math.max(ms, v) |
| 156 |
if pct >= prof_min then |
| 157 |
local file, line = k:match("^(.*):(%d+)$") |
| 158 |
if not file then file = k; line = 0 end |
| 159 |
local fl = files[file] |
| 160 |
if not fl then fl = {}; files[file] = fl; files[#files+1] = file end |
| 161 |
line = tonumber(line) |
| 162 |
fl[line] = prof_raw and v or pct |
| 163 |
end |
| 164 |
end |
| 165 |
sort(files) |
| 166 |
local fmtv, fmtn = " %3d%% | %s\n", " | %s\n" |
| 167 |
if prof_raw then |
| 168 |
local n = math.max(5, math.ceil(math.log10(ms))) |
| 169 |
fmtv = "%"..n.."d | %s\n" |
| 170 |
fmtn = (" "):rep(n).." | %s\n" |
| 171 |
end |
| 172 |
local ann = prof_ann |
| 173 |
for _, file in ipairs(files) do |
| 174 |
local f0 = file:byte() |
| 175 |
if f0 == 40 or f0 == 91 then |
| 176 |
out:write(format("\n====== %s ======\n[Cannot annotate non-file]\n", file)) |
| 177 |
break |
| 178 |
end |
| 179 |
local fp, err = io.open(file) |
| 180 |
if not fp then |
| 181 |
out:write(format("====== ERROR: %s: %s\n", file, err)) |
| 182 |
break |
| 183 |
end |
| 184 |
out:write(format("\n====== %s ======\n", file)) |
| 185 |
local fl = files[file] |
| 186 |
local n, show = 1, false |
| 187 |
if ann ~= 0 then |
| 188 |
for i=1,ann do |
| 189 |
if fl[i] then show = true; out:write("@@ 1 @@\n"); break end |
| 190 |
end |
| 191 |
end |
| 192 |
for line in fp:lines() do |
| 193 |
if line:byte() == 27 then |
| 194 |
out:write("[Cannot annotate bytecode file]\n") |
| 195 |
break |
| 196 |
end |
| 197 |
local v = fl[n] |
| 198 |
if ann ~= 0 then |
| 199 |
local v2 = fl[n+ann] |
| 200 |
if show then |
| 201 |
if v2 then show = n+ann elseif v then show = n |
| 202 |
elseif show+ann < n then show = false end |
| 203 |
elseif v2 then |
| 204 |
show = n+ann |
| 205 |
out:write(format("@@ %d @@\n", n)) |
| 206 |
end |
| 207 |
if not show then goto next end |
| 208 |
end |
| 209 |
if v then |
| 210 |
out:write(format(fmtv, v, line)) |
| 211 |
else |
| 212 |
out:write(format(fmtn, line)) |
| 213 |
end |
| 214 |
::next:: |
| 215 |
n = n + 1 |
| 216 |
end |
| 217 |
fp:close() |
| 218 |
end |
| 219 |
end |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
local function prof_finish() |
| 225 |
if prof_ud then |
| 226 |
profile.stop() |
| 227 |
local samples = prof_samples |
| 228 |
if samples == 0 then |
| 229 |
if prof_raw ~= true then out:write("[No samples collected]\n") end |
| 230 |
elseif prof_ann then |
| 231 |
prof_annotate(prof_count1, samples) |
| 232 |
else |
| 233 |
prof_top(prof_count1, prof_count2, samples, "") |
| 234 |
end |
| 235 |
prof_count1 = nil |
| 236 |
prof_count2 = nil |
| 237 |
prof_ud = nil |
| 238 |
if out ~= stdout then out:close() end |
| 239 |
end |
| 240 |
end |
| 241 |
|
| 242 |
|
| 243 |
local function prof_start(mode) |
| 244 |
local interval = "" |
| 245 |
mode = mode:gsub("i%d*", function(s) interval = s; return "" end) |
| 246 |
prof_min = 3 |
| 247 |
mode = mode:gsub("m(%d+)", function(s) prof_min = tonumber(s); return "" end) |
| 248 |
prof_depth = 1 |
| 249 |
mode = mode:gsub("%-?%d+", function(s) prof_depth = tonumber(s); return "" end) |
| 250 |
local m = {} |
| 251 |
for c in mode:gmatch(".") do m[c] = c end |
| 252 |
prof_states = m.z or m.v |
| 253 |
if prof_states == "z" then zone = require("jit.zone") end |
| 254 |
local scope = m.l or m.f or m.F or (prof_states and "" or "f") |
| 255 |
local flags = (m.p or "") |
| 256 |
prof_raw = m.r |
| 257 |
if m.s then |
| 258 |
prof_split = 2 |
| 259 |
if prof_depth == -1 or m["-"] then prof_depth = -2 |
| 260 |
elseif prof_depth == 1 then prof_depth = 2 end |
| 261 |
elseif mode:find("[fF].*l") then |
| 262 |
scope = "l" |
| 263 |
prof_split = 3 |
| 264 |
else |
| 265 |
prof_split = (scope == "" or mode:find("[zv].*[lfF]")) and 1 or 0 |
| 266 |
end |
| 267 |
prof_ann = m.A and 0 or (m.a and 3) |
| 268 |
if prof_ann then |
| 269 |
scope = "l" |
| 270 |
prof_fmt = "pl" |
| 271 |
prof_split = 0 |
| 272 |
prof_depth = 1 |
| 273 |
elseif m.G and scope ~= "" then |
| 274 |
prof_fmt = flags..scope.."Z;" |
| 275 |
prof_depth = -100 |
| 276 |
prof_raw = true |
| 277 |
prof_min = 0 |
| 278 |
elseif scope == "" then |
| 279 |
prof_fmt = false |
| 280 |
else |
| 281 |
local sc = prof_split == 3 and m.f or m.F or scope |
| 282 |
prof_fmt = flags..sc..(prof_depth >= 0 and "Z < " or "Z > ") |
| 283 |
end |
| 284 |
prof_count1 = {} |
| 285 |
prof_count2 = {} |
| 286 |
prof_samples = 0 |
| 287 |
profile.start(scope:lower()..interval, prof_cb) |
| 288 |
prof_ud = newproxy(true) |
| 289 |
getmetatable(prof_ud).__gc = prof_finish |
| 290 |
end |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
local function start(mode, outfile) |
| 295 |
if not outfile then outfile = os.getenv("LUAJIT_PROFILEFILE") end |
| 296 |
if outfile then |
| 297 |
out = outfile == "-" and stdout or assert(io.open(outfile, "w")) |
| 298 |
else |
| 299 |
out = stdout |
| 300 |
end |
| 301 |
prof_start(mode or "f") |
| 302 |
end |
| 303 |
|
| 304 |
|
| 305 |
return { |
| 306 |
start = start, |
| 307 |
stop = prof_finish |
| 308 |
} |
| 309 |
|