[Tarantool-patches] [PATCH luajit v2 3/3] memprof: substitute long proto names with aliases

Mikhail Shishatskiy m.shishatskiy at tarantool.org
Thu Dec 2 14:03:29 MSK 2021


Sometimes a loaded chunk name can be multiline (actually, it
is the Lua code itself). In order not to burden memprof parser
output with big multiline names, aliases were introduced.

The chunk name is replaced by `function_alias_N` (where N is a unique id)
to be displayed in the allocation events report. All the aliases are
printed in the end of parser's output under the header "ALIASES".

Because of changes mentioned above, the API of <utils/symtab.lua>
changed: now symtab has additional `alias` assotiative table for
storing aliases: one can get alias string by sym_chunk key and sym_chunk
by alias index. The humanizer module now can display aliases with the
new function <aliases>.

Follows up tarantool/tarantool#5815
---

Issue: https://github.com/tarantool/tarantool/issues/5815
Branch: https://github.com/tarantool/luajit/tree/shishqa/gh-5815-enrich-symtab-when-prototype-is-allocated-v2
Tarantool branch: https://github.com/tarantool/tarantool/tree/shishqa/gh-5815-enrich-symtab-when-prototype-is-allocated

 tools/memprof.lua          |  1 +
 tools/memprof/humanize.lua | 14 ++++++++++++++
 tools/utils/symtab.lua     | 17 +++++++++++++----
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/tools/memprof.lua b/tools/memprof.lua
index 18b44fdd..cf66dd9e 100644
--- a/tools/memprof.lua
+++ b/tools/memprof.lua
@@ -106,6 +106,7 @@ local function dump(inputfile)
   end
   local dheap = process.form_heap_delta(events, symbols)
   view.leak_info(dheap)
+  view.aliases(symbols)
   os.exit(0)
 end
 
diff --git a/tools/memprof/humanize.lua b/tools/memprof/humanize.lua
index 7771005d..d77c7132 100644
--- a/tools/memprof/humanize.lua
+++ b/tools/memprof/humanize.lua
@@ -81,4 +81,18 @@ function M.leak_info(dheap)
   print("")
 end
 
+function M.aliases(symbols)
+  if #symbols.alias == 0 then return end
+  print("ALIASES:")
+  for _, source in ipairs(symbols.alias) do
+    print(symbols.alias[source]..":")
+    local lineno = 1
+    for line in source:gmatch("(.-)\n") do
+      print(tostring(lineno).."\t| "..line)
+      lineno = lineno + 1
+    end
+    print("~\n")
+  end
+end
+
 return M
diff --git a/tools/utils/symtab.lua b/tools/utils/symtab.lua
index 00bab03a..133a0fc7 100644
--- a/tools/utils/symtab.lua
+++ b/tools/utils/symtab.lua
@@ -46,6 +46,13 @@ function M.parse_sym_lfunc(reader, symtab)
     symtab.lfunc[sym_addr] = {}
   end
 
+  if sym_chunk:find('\n') and symtab.alias[sym_chunk] == nil then
+    table.insert(symtab.alias, sym_chunk)
+    symtab.alias[sym_chunk] = string_format(
+      "function_alias_%d", #symtab.alias
+    )
+  end
+
   table.insert(symtab.lfunc[sym_addr], {
     source = sym_chunk,
     linedefined = sym_line,
@@ -77,6 +84,7 @@ function M.parse(reader)
   local symtab = {
     lfunc = {},
     trace = {},
+    alias = {},
   }
   local magic = reader:read_octets(3)
   local version = reader:read_octets(1)
@@ -147,15 +155,16 @@ function M.demangle(symtab, loc)
   end
 
   local addr = loc.addr
+  local epoch = loc.epoch
 
   if addr == 0 then
     return "INTERNAL"
   end
 
-  if symtab.lfunc[addr] and symtab.lfunc[addr][loc.epoch] then
-    return string_format(
-      "%s:%d", symtab.lfunc[addr][loc.epoch].source, loc.line
-    )
+  if symtab.lfunc[addr] and symtab.lfunc[addr][epoch] then
+    local source = symtab.lfunc[addr][epoch].source
+    local alias = symtab.alias[source]
+    return string_format("%s:%d", alias or source, loc.line)
   end
 
   return string_format("CFUNC %#x", addr)
-- 
2.33.1



More information about the Tarantool-patches mailing list