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

Mikhail Shishatskiy m.shishatskiy at tarantool.org
Sat Aug 21 15:50:01 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 and `alias_count` counter to store number of 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
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 | 35 +++++++++++++++++++++++++++++++++++
 tools/utils/symtab.lua     | 14 +++++++++++++-
 3 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/tools/memprof.lua b/tools/memprof.lua
index 496a0d78..52e09fd9 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)
   view.leak_info(dheap)
+  view.aliases(symbols)
   os.exit(0)
 end
 
diff --git a/tools/memprof/humanize.lua b/tools/memprof/humanize.lua
index 0360362d..72e71080 100644
--- a/tools/memprof/humanize.lua
+++ b/tools/memprof/humanize.lua
@@ -5,6 +5,9 @@
 
 local symtab = require "utils.symtab"
 
+-- Assume that loaded string chunks have less than 9999 lines.
+local ALIAS_MAX_LINE_NUMBER_LEN = 4
+
 local M = {}
 
 function M.render(events)
@@ -98,4 +101,36 @@ function M.describe_location(symbols, loc)
   return symtab.demangle(symbols, loc)
 end
 
+local function get_aliases(symbols)
+  local aliases = {}
+  for source, alias in pairs(symbols.alias) do
+    table.insert(aliases, { alias, source })
+  end
+  table.sort(aliases, function(a, b)
+    return a[1] < b[1]
+  end)
+  return aliases
+end
+
+local function format_source_prefix(lineno)
+  local line_str = tostring(lineno)
+  local line_str_len = line_str:len()
+  return line_str..string.rep(" ", ALIAS_MAX_LINE_NUMBER_LEN - line_str_len)
+end
+
+function M.aliases(symbols)
+  if symbols.alias_count == 0 then return end
+  print("ALIASES:")
+  local aliases = get_aliases(symbols)
+  for _, alias in ipairs(aliases) do
+    print(alias[1]..":")
+    local lineno = 1
+    for line in alias[2]:gmatch("(.-)\n") do
+      print(format_source_prefix(lineno)..'|'..line)
+      lineno = lineno + 1
+    end
+    print("~\n")
+  end
+end
+
 return M
diff --git a/tools/utils/symtab.lua b/tools/utils/symtab.lua
index 601c9563..88fdb42e 100644
--- a/tools/utils/symtab.lua
+++ b/tools/utils/symtab.lua
@@ -58,6 +58,8 @@ function M.parse(reader)
   local symtab = {
     lfunc = {},
     trace = {},
+    alias = {},
+    alias_count = 0,
   }
   local magic = reader:read_octets(3)
   local version = reader:read_octets(1)
@@ -102,7 +104,17 @@ local function demangle_lfunc(symtab, loc)
   if addr == 0 then
     return "INTERNAL"
   elseif symtab.lfunc[addr] then
-    return string_format("%s:%d", symtab.lfunc[loc.addr].source, loc.line)
+    local name = symtab.lfunc[addr].source
+    if name:find('\n') == nil then
+      return string_format("%s:%d", name, loc.line)
+    end
+    if not symtab.alias[name] then
+      symtab.alias[name] = string_format(
+        "function_alias_%d", symtab.alias_count
+      )
+      symtab.alias_count = symtab.alias_count + 1
+    end
+    return string_format("%s:%d", symtab.alias[name], loc.line)
   end
   return string_format("CFUNC %#x", addr)
 end
-- 
2.32.0



More information about the Tarantool-patches mailing list