Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mikhail Shishatskiy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, imun@tarantool.org,
	skaplun@tarantool.org
Subject: [Tarantool-patches] [PATCH luajit v1 3/4] memprof: substitute long proto names with aliases
Date: Sat, 21 Aug 2021 19:50:01 +0700	[thread overview]
Message-ID: <20210821125002.408132-4-m.shishatskiy@tarantool.org> (raw)
In-Reply-To: <20210821125002.408132-1-m.shishatskiy@tarantool.org>

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


  parent reply	other threads:[~2021-08-21 12:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-21 12:49 [Tarantool-patches] [PATCH luajit v1 0/4] memprof: enrich symtab when meeting new prototype Mikhail Shishatskiy via Tarantool-patches
2021-08-21 12:49 ` [Tarantool-patches] [PATCH luajit v1 1/4] " Mikhail Shishatskiy via Tarantool-patches
2021-08-24 14:16   ` Mikhail Shishatskiy via Tarantool-patches
2021-08-21 12:50 ` [Tarantool-patches] [PATCH luajit v1 2/4] memprof: demangle symbols on the spot Mikhail Shishatskiy via Tarantool-patches
2021-08-21 12:50 ` Mikhail Shishatskiy via Tarantool-patches [this message]
2021-08-21 12:50 ` [Tarantool-patches] [PATCH luajit v1 4/4] luajit: change order of modules Mikhail Shishatskiy via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210821125002.408132-4-m.shishatskiy@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imun@tarantool.org \
    --cc=m.shishatskiy@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit v1 3/4] memprof: substitute long proto names with aliases' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox