Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers
@ 2024-03-13 14:40 Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 1/6] build: purge sysprof.collapse module Maxim Kokryashkin via Tarantool-patches
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Maxim Kokryashkin via Tarantool-patches @ 2024-03-13 14:40 UTC (permalink / raw)
  To: tarantool-patches, skaplun, sergeyb

Changes in v3:
- Last patch is split into two parts: second part will go in another
  series.

Branch: https://github.com/tarantool/luajit/tree/fckxorg/profile-parsers-refactoring-p1
PR: https://github.com/tarantool/tarantool/pull/9438
Issues:
https://github.com/tarantool/tarantool/issues/5994
https://github.com/tarantool/tarantool/issues/9217


Maxim Kokryashkin (6):
  build: purge sysprof.collapse module
  build: fix tool components handling
  memprof: refactor `heap_chunk` data structure
  memprof: remove unused arguments
  memprof: introduce the `--human-readable` option
  profilers: introduce event reader module

 Makefile.original                             | 18 +++--
 .../gh-5994-memprof-human-readable.test.lua   | 73 +++++++++++++++++++
 tools/CMakeLists.txt                          | 14 ++--
 tools/memprof.lua                             | 22 ++++--
 tools/memprof/humanize.lua                    | 46 +++++++++---
 tools/memprof/parse.lua                       | 22 ++++--
 tools/sysprof/collapse.lua                    |  3 -
 tools/utils/evread.lua                        | 32 ++++++++
 8 files changed, 192 insertions(+), 38 deletions(-)
 create mode 100644 test/tarantool-tests/gh-5994-memprof-human-readable.test.lua
 delete mode 100755 tools/sysprof/collapse.lua
 create mode 100644 tools/utils/evread.lua

--
2.44.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Tarantool-patches] [PATCH luajit v3 1/6] build: purge sysprof.collapse module
  2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
@ 2024-03-13 14:40 ` Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 2/6] build: fix tool components handling Maxim Kokryashkin via Tarantool-patches
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Maxim Kokryashkin via Tarantool-patches @ 2024-03-13 14:40 UTC (permalink / raw)
  To: tarantool-patches, skaplun, sergeyb

This module became unused as a result of commit
tarantool/tarantool@e2851883cd4c23d41bd9aec23fad06fd10d39c45,
so it can be purged safely from the LuaJIT sources.

Part of tarantool/tarantool#8700
---
 tools/CMakeLists.txt       | 6 ------
 tools/sysprof/collapse.lua | 3 ---
 2 files changed, 9 deletions(-)
 delete mode 100755 tools/sysprof/collapse.lua

diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index b951f767..1dfc39e4 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -60,9 +60,6 @@ if(LUAJIT_DISABLE_SYSPROF)
 else()
   add_custom_target(tools-parse-sysprof EXCLUDE_FROM_ALL DEPENDS
     sysprof/parse.lua
-    # TODO: This line is not deleted only for the sake of integrational
-    # testing. It should be deleted in the next series.
-    sysprof/collapse.lua
     sysprof.lua
     utils/bufread.lua
     utils/symtab.lua
@@ -71,9 +68,6 @@ else()
 
   install(FILES
       ${CMAKE_CURRENT_SOURCE_DIR}/sysprof/parse.lua
-      # TODO: This line is not deleted only for the sake of integrational
-      # testing. It should be deleted in the next series.
-      ${CMAKE_CURRENT_SOURCE_DIR}/sysprof/collapse.lua
     DESTINATION ${LUAJIT_DATAROOTDIR}/sysprof
     PERMISSIONS
       OWNER_READ OWNER_WRITE
diff --git a/tools/sysprof/collapse.lua b/tools/sysprof/collapse.lua
deleted file mode 100755
index cac92f1a..00000000
--- a/tools/sysprof/collapse.lua
+++ /dev/null
@@ -1,3 +0,0 @@
--- FIXME: This file is not deleted only for the sake of
--- integrational testing. It should be deleted in the
--- next series.
-- 
2.44.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Tarantool-patches] [PATCH luajit v3 2/6] build: fix tool components handling
  2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 1/6] build: purge sysprof.collapse module Maxim Kokryashkin via Tarantool-patches
@ 2024-03-13 14:40 ` Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 3/6] memprof: refactor `heap_chunk` data structure Maxim Kokryashkin via Tarantool-patches
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Maxim Kokryashkin via Tarantool-patches @ 2024-03-13 14:40 UTC (permalink / raw)
  To: tarantool-patches, skaplun, sergeyb

Prior to this patch, memprof/process.lua wasn't added to the
dependency list as a part of the memprof parser sources. Also,
it wasn't installed into the system along with other memprof
sources, which breaks the profile parser.

Also, the sysprof parser sources weren't handled by the
Makefile.original at all. The same applies to utils/avl.lua.
This patch fixes that, so now it's possible to properly handle
sysprof's parser.

Part of tarantool/tarantool#5994
---
 Makefile.original    | 16 ++++++++++++----
 tools/CMakeLists.txt |  4 ++++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/Makefile.original b/Makefile.original
index e67b6aa8..d0834fe6 100644
--- a/Makefile.original
+++ b/Makefile.original
@@ -40,6 +40,7 @@ INSTALL_JITLIB= $(INSTALL_LJLIBD)/jit
 INSTALL_TOOLSLIB= $(INSTALL_LJLIBD)
 INSTALL_UTILSLIB= $(INSTALL_TOOLSLIB)/utils
 INSTALL_MEMPROFLIB= $(INSTALL_TOOLSLIB)/memprof
+INSTALL_SYSPROFLIB= $(INSTALL_TOOLSLIB)/sysprof
 INSTALL_LMODD= $(INSTALL_SHARE)/lua
 INSTALL_LMOD= $(INSTALL_LMODD)/$(ABIVER)
 INSTALL_CMODD= $(INSTALL_LIB)/lua
@@ -68,10 +69,12 @@ INSTALL_PC= $(INSTALL_PKGCONFIG)/$(INSTALL_PCNAME)
 
 INSTALL_DIRS= $(INSTALL_BIN) $(INSTALL_LIB) $(INSTALL_INC) $(INSTALL_MAN) \
   $(INSTALL_PKGCONFIG) $(INSTALL_JITLIB) $(INSTALL_LMOD) $(INSTALL_CMOD) \
-  $(INSTALL_UTILSLIB) $(INSTALL_MEMPROFLIB) $(INSTALL_TOOLSLIB)
+  $(INSTALL_UTILSLIB) $(INSTALL_MEMPROFLIB) $(INSTALL_SYSPROFLIB) \
+  $(INSTALL_TOOLSLIB)
 UNINSTALL_DIRS= $(INSTALL_JITLIB) $(INSTALL_LJLIBD) $(INSTALL_INC) \
   $(INSTALL_LMOD) $(INSTALL_LMODD) $(INSTALL_CMOD) $(INSTALL_CMODD) \
-  $(INSTALL_UTILSLIB) $(INSTALL_MEMPROFLIB) $(INSTALL_TOOLSLIB)
+  $(INSTALL_UTILSLIB) $(INSTALL_MEMPROFLIB) $(INSTALL_SYSPROFLIB) \
+  $(INSTALL_TOOLSLIB)
 
 RM= rm -f
 MKDIR= mkdir -p
@@ -95,8 +98,9 @@ FILES_JITLIB= bc.lua bcsave.lua dump.lua p.lua v.lua zone.lua \
 	      dis_arm64be.lua dis_ppc.lua dis_mips.lua dis_mipsel.lua \
 	      dis_mips64.lua dis_mips64el.lua vmdef.lua
 FILES_UTILSLIB= avl.lua bufread.lua symtab.lua
-FILES_MEMPROFLIB= parse.lua humanize.lua
-FILES_TOOLSLIB= memprof.lua
+FILES_MEMPROFLIB= humanize.lua parse.lua process.lua
+FILES_SYSPROFLIB= parse.lua
+FILES_TOOLSLIB= memprof.lua sysprof.lua
 
 ifeq (,$(findstring Windows,$(OS)))
   HOST_SYS:= $(shell uname -s)
@@ -140,6 +144,7 @@ install: $(INSTALL_DEP)
 	cd src/jit && $(INSTALL_F) $(FILES_JITLIB) $(INSTALL_JITLIB)
 	cd tools/utils && $(INSTALL_F) $(FILES_UTILSLIB) $(INSTALL_UTILSLIB)
 	cd tools/memprof && $(INSTALL_F) $(FILES_MEMPROFLIB) $(INSTALL_MEMPROFLIB)
+	cd tools/sysprof && $(INSTALL_F) $(FILES_SYSPROFLIB) $(INSTALL_SYSPROFLIB)
 	cd tools && $(INSTALL_F) $(FILES_TOOLSLIB) $(INSTALL_TOOLSLIB)
 	@echo "==== Successfully installed LuaJIT $(VERSION) to $(PREFIX) ===="
 	@echo ""
@@ -162,6 +167,9 @@ uninstall:
 	for file in $(FILES_MEMPROFLIB); do \
 	  $(UNINSTALL) $(INSTALL_MEMPROFLIB)/$$file; \
 	  done
+	for file in $(FILES_SYSPROFLIB); do \
+	  $(UNINSTALL) $(INSTALL_SYSPROFLIB)/$$file; \
+	  done
 	for file in $(FILES_TOOLSLIB); do \
 	  $(UNINSTALL) $(INSTALL_TOOLSLIB)/$$file; \
 	  done
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 1dfc39e4..a4adc15b 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -14,6 +14,7 @@ else()
   add_custom_target(tools-parse-memprof EXCLUDE_FROM_ALL DEPENDS
     memprof/humanize.lua
     memprof/parse.lua
+    memprof/process.lua
     memprof.lua
     utils/avl.lua
     utils/bufread.lua
@@ -24,6 +25,7 @@ else()
   install(FILES
       ${CMAKE_CURRENT_SOURCE_DIR}/memprof/humanize.lua
       ${CMAKE_CURRENT_SOURCE_DIR}/memprof/parse.lua
+      ${CMAKE_CURRENT_SOURCE_DIR}/memprof/process.lua
     DESTINATION ${LUAJIT_DATAROOTDIR}/memprof
     PERMISSIONS
       OWNER_READ OWNER_WRITE
@@ -61,6 +63,7 @@ else()
   add_custom_target(tools-parse-sysprof EXCLUDE_FROM_ALL DEPENDS
     sysprof/parse.lua
     sysprof.lua
+    utils/avl.lua
     utils/bufread.lua
     utils/symtab.lua
   )
@@ -76,6 +79,7 @@ else()
     COMPONENT tools-parse-sysprof
   )
   install(FILES
+      ${CMAKE_CURRENT_SOURCE_DIR}/utils/avl.lua
       ${CMAKE_CURRENT_SOURCE_DIR}/utils/bufread.lua
       ${CMAKE_CURRENT_SOURCE_DIR}/utils/symtab.lua
     DESTINATION ${LUAJIT_DATAROOTDIR}/utils
-- 
2.44.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Tarantool-patches] [PATCH luajit v3 3/6] memprof: refactor `heap_chunk` data structure
  2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 1/6] build: purge sysprof.collapse module Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 2/6] build: fix tool components handling Maxim Kokryashkin via Tarantool-patches
@ 2024-03-13 14:40 ` Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 4/6] memprof: remove unused arguments Maxim Kokryashkin via Tarantool-patches
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Maxim Kokryashkin via Tarantool-patches @ 2024-03-13 14:40 UTC (permalink / raw)
  To: tarantool-patches, skaplun, sergeyb

The memprof parser used to have the `heap_chunk` data structure
using numeric indices for its values, which is hardly readable
and maintainable. This patch replaces these numeric indices with
corresponding string keys.

Part of tarantool/tarantool#5994
---
 tools/memprof/parse.lua | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/tools/memprof/parse.lua b/tools/memprof/parse.lua
index 42a601ef..cc66a23e 100644
--- a/tools/memprof/parse.lua
+++ b/tools/memprof/parse.lua
@@ -45,22 +45,30 @@ local function new_event(loc)
   }
 end
 
+local function new_heap_chunk(size, id, loc)
+  return {
+    size = size,
+    id = id,
+    loc = loc,
+  }
+end
+
 local function link_to_previous(heap_chunk, e, nsize)
   -- Memory at this chunk was allocated before we start tracking.
   if heap_chunk then
     -- Save Lua code location (line) by address (id).
-    if not e.primary[heap_chunk[2]] then
-      e.primary[heap_chunk[2]] = {
-        loc = heap_chunk[3],
+    if not e.primary[heap_chunk.id] then
+      e.primary[heap_chunk.id] = {
+        loc = heap_chunk.loc,
         allocated = 0,
         freed = 0,
         count = 0,
       }
     end
     -- Save information about delta for memory heap.
-    local location_data = e.primary[heap_chunk[2]]
+    local location_data = e.primary[heap_chunk.id]
     location_data.allocated = location_data.allocated + nsize
-    location_data.freed = location_data.freed + heap_chunk[1]
+    location_data.freed = location_data.freed + heap_chunk.size
     location_data.count = location_data.count + 1
   end
 end
@@ -95,7 +103,7 @@ local function parse_alloc(reader, asource, events, heap, symbols)
   e.num = e.num + 1
   e.alloc = e.alloc + nsize
 
-  heap[naddr] = {nsize, id, loc}
+  heap[naddr] = new_heap_chunk(nsize, id, loc)
 end
 
 local function parse_realloc(reader, asource, events, heap, symbols)
@@ -117,7 +125,7 @@ local function parse_realloc(reader, asource, events, heap, symbols)
   link_to_previous(heap[oaddr], e, nsize)
 
   heap[oaddr] = nil
-  heap[naddr] = {nsize, id, loc}
+  heap[naddr] = new_heap_chunk(nsize, id, loc)
 end
 
 local function parse_free(reader, asource, events, heap, symbols)
-- 
2.44.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Tarantool-patches] [PATCH luajit v3 4/6] memprof: remove unused arguments
  2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
                   ` (2 preceding siblings ...)
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 3/6] memprof: refactor `heap_chunk` data structure Maxim Kokryashkin via Tarantool-patches
@ 2024-03-13 14:40 ` Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 5/6] memprof: introduce the `--human-readable` option Maxim Kokryashkin via Tarantool-patches
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Maxim Kokryashkin via Tarantool-patches @ 2024-03-13 14:40 UTC (permalink / raw)
  To: tarantool-patches, skaplun, sergeyb

Both `humanize.profile_info` and `process.form_heap_delta`
don't need the symtab, so the argument can be dropped.

Part of tarantool/tarantool#5994
---
 tools/memprof.lua | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/memprof.lua b/tools/memprof.lua
index e23bbf24..955a1327 100644
--- a/tools/memprof.lua
+++ b/tools/memprof.lua
@@ -102,9 +102,9 @@ local function dump(inputfile)
   local symbols = symtab.parse(reader)
   local events = memprof.parse(reader, symbols)
   if not leak_only then
-    view.profile_info(events, symbols)
+    view.profile_info(events)
   end
-  local dheap = process.form_heap_delta(events, symbols)
+  local dheap = process.form_heap_delta(events)
   view.leak_info(dheap)
   view.aliases(symbols)
   -- XXX: The second argument is required to properly close Lua
-- 
2.44.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Tarantool-patches] [PATCH luajit v3 5/6] memprof: introduce the `--human-readable` option
  2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
                   ` (3 preceding siblings ...)
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 4/6] memprof: remove unused arguments Maxim Kokryashkin via Tarantool-patches
@ 2024-03-13 14:40 ` Maxim Kokryashkin via Tarantool-patches
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 6/6] profilers: introduce event reader module Maxim Kokryashkin via Tarantool-patches
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Maxim Kokryashkin via Tarantool-patches @ 2024-03-13 14:40 UTC (permalink / raw)
  To: tarantool-patches, skaplun, sergeyb

Prior to this patch, memprof could report only the raw
amount of bytes, which is hard to read. This patch adds the
`--human-readable` CLI option to the memprof parser, so the
memory is reported in KiB, MiB, or GiB, depending on what's
the biggest reasonable unit.

This patch also refactors the options mechanism for parser,
so all of the options are passed into parser's modules as a
single config table instead of being handled individually.

Part of tarantool/tarantool#5994
---
 .../gh-5994-memprof-human-readable.test.lua   | 73 +++++++++++++++++++
 tools/memprof.lua                             | 20 +++--
 tools/memprof/humanize.lua                    | 46 +++++++++---
 3 files changed, 123 insertions(+), 16 deletions(-)
 create mode 100644 test/tarantool-tests/gh-5994-memprof-human-readable.test.lua

diff --git a/test/tarantool-tests/gh-5994-memprof-human-readable.test.lua b/test/tarantool-tests/gh-5994-memprof-human-readable.test.lua
new file mode 100644
index 00000000..e34291be
--- /dev/null
+++ b/test/tarantool-tests/gh-5994-memprof-human-readable.test.lua
@@ -0,0 +1,73 @@
+local tap = require('tap')
+local test = tap.test('gh-5994-memprof-human-readable'):skipcond({
+  ['Profile tools are implemented for x86_64 only'] = jit.arch ~= 'x86' and
+                                                      jit.arch ~= 'x64',
+  ['Profile tools are implemented for Linux only'] = jit.os ~= 'Linux',
+  -- XXX: Tarantool integration is required to run this test properly.
+  -- luacheck: no global
+  ['No profile tools CLI option integration'] = _TARANTOOL,
+})
+
+local utils = require('utils')
+local TMP_BINFILE_MEMPROF = utils.tools.profilename('memprofdata.tmp.bin')
+local PARSE_CMD = utils.exec.luacmd(arg) .. ' -tm '
+
+local function generate_output(bytes)
+  local res, err = misc.memprof.start(TMP_BINFILE_MEMPROF)
+  -- Should start successfully.
+  assert(res, err)
+
+  -- luacheck: no unused
+  local _ = string.rep('_', bytes)
+
+  res, err = misc.memprof.stop()
+  -- Should stop successfully.
+  assert(res, err)
+end
+
+local TEST_SET = {
+  {
+    bytes = 2049,
+    match = '%dB',
+    hr = false,
+    name = 'non-human-readable mode is correct'
+  },
+  {
+    bytes = 100,
+    match = '%dB',
+    hr = true,
+    name = 'human-readable mode: bytes'
+  },
+  {
+    bytes = 2560,
+    match = '%d+%.%d%dKiB',
+    hr = true,
+    name = 'human-readable mode: float'
+  },
+  {
+    bytes = 2048,
+    match = '%dKiB',
+    hr = true,
+    name = 'human-readable mode: KiB'
+  },
+  {
+    bytes = 1024 * 1024,
+    match = '%dMiB',
+    hr = true,
+    name = 'human-readable mode: MiB'
+  },
+  -- XXX: The test case for GiB is not implemented because it is
+  -- OOM-prone for non-GC64 builds.
+}
+
+test:plan(#TEST_SET)
+
+for _, params in ipairs(TEST_SET) do
+  generate_output(params.bytes)
+  local cmd = PARSE_CMD .. (params.hr and ' --human-readable ' or '')
+  local output = io.popen(cmd .. TMP_BINFILE_MEMPROF):read('*all')
+  test:like(output, params.match, params.name)
+end
+
+os.remove(TMP_BINFILE_MEMPROF)
+test:done(true)
diff --git a/tools/memprof.lua b/tools/memprof.lua
index 955a1327..537cb869 100644
--- a/tools/memprof.lua
+++ b/tools/memprof.lua
@@ -22,6 +22,12 @@ local match, gmatch = string.match, string.gmatch
 -- Program options.
 local opt_map = {}
 
+-- Default config for the memprof parser.
+local config = {
+  leak_only = false,
+  human_readable = false,
+}
+
 function opt_map.help()
   stdout:write [[
 luajit-parse-memprof - parser of the memory usage profile collected
@@ -34,14 +40,18 @@ luajit-parse-memprof [options] memprof.bin
 Supported options are:
 
   --help                            Show this help and exit
+  --human-readable                  Use KiB/MiB/GiB notation instead of bytes
   --leak-only                       Report only leaks information
 ]]
   os.exit(0)
 end
 
-local leak_only = false
 opt_map["leak-only"] = function()
-  leak_only = true
+  config.leak_only = true
+end
+
+opt_map["human-readable"] = function()
+  config.human_readable = true
 end
 
 -- Print error and exit with error status.
@@ -101,11 +111,11 @@ local function dump(inputfile)
   local reader = bufread.new(inputfile)
   local symbols = symtab.parse(reader)
   local events = memprof.parse(reader, symbols)
-  if not leak_only then
-    view.profile_info(events)
+  if not config.leak_only then
+    view.profile_info(events, config)
   end
   local dheap = process.form_heap_delta(events)
-  view.leak_info(dheap)
+  view.leak_info(dheap, config)
   view.aliases(symbols)
   -- XXX: The second argument is required to properly close Lua
   -- universe (i.e. invoke <lua_close> before exiting).
diff --git a/tools/memprof/humanize.lua b/tools/memprof/humanize.lua
index 5b289ce3..a0b1693a 100644
--- a/tools/memprof/humanize.lua
+++ b/tools/memprof/humanize.lua
@@ -5,7 +5,29 @@
 
 local M = {}
 
-function M.render(events)
+local function human_readable_bytes(bytes)
+  local units = {"B", "KiB", "MiB", "GiB"}
+  local magnitude = 1
+
+  while bytes >= 1024 and magnitude < #units do
+    bytes = bytes / 1024
+    magnitude = magnitude + 1
+  end
+  local is_int = math.floor(bytes) == bytes
+  local fmt = is_int and "%d%s" or "%.2f%s"
+
+  return string.format(fmt, bytes, units[magnitude])
+end
+
+local function format_bytes(bytes, config)
+  if config.human_readable then
+    return human_readable_bytes(bytes)
+  else
+    return string.format('%dB', bytes)
+  end
+end
+
+function M.render(events, config)
   local ids = {}
 
   for id, _ in pairs(events) do
@@ -18,11 +40,11 @@ function M.render(events)
 
   for i = 1, #ids do
     local event = events[ids[i]]
-    print(string.format("%s: %d events\t+%d bytes\t-%d bytes",
+    print(string.format("%s: %d events\t+%s\t-%s",
       event.loc,
       event.num,
-      event.alloc,
-      event.free
+      format_bytes(event.alloc, config),
+      format_bytes(event.free, config)
     ))
 
     local prim_loc = {}
@@ -40,21 +62,21 @@ function M.render(events)
   end
 end
 
-function M.profile_info(events)
+function M.profile_info(events, config)
   print("ALLOCATIONS")
-  M.render(events.alloc)
+  M.render(events.alloc, config)
   print("")
 
   print("REALLOCATIONS")
-  M.render(events.realloc)
+  M.render(events.realloc, config)
   print("")
 
   print("DEALLOCATIONS")
-  M.render(events.free)
+  M.render(events.free, config)
   print("")
 end
 
-function M.leak_info(dheap)
+function M.leak_info(dheap, config)
   local leaks = {}
   for line, info in pairs(dheap) do
     -- Report "INTERNAL" events inconsistencies for profiling
@@ -71,8 +93,10 @@ function M.leak_info(dheap)
   print("HEAP SUMMARY:")
   for _, l in pairs(leaks) do
     print(string.format(
-      "%s holds %d bytes: %d allocs, %d frees",
-      l.line, l.dbytes, dheap[l.line].nalloc,
+      "%s holds %s: %d allocs, %d frees",
+      l.line,
+      format_bytes(l.dbytes, config),
+      dheap[l.line].nalloc,
       dheap[l.line].nfree
     ))
   end
-- 
2.44.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Tarantool-patches] [PATCH luajit v3 6/6] profilers: introduce event reader module
  2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
                   ` (4 preceding siblings ...)
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 5/6] memprof: introduce the `--human-readable` option Maxim Kokryashkin via Tarantool-patches
@ 2024-03-13 14:40 ` Maxim Kokryashkin via Tarantool-patches
  2024-03-15  8:44 ` [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Sergey Kaplun via Tarantool-patches
  2024-03-20 15:08 ` Sergey Kaplun via Tarantool-patches
  7 siblings, 0 replies; 9+ messages in thread
From: Maxim Kokryashkin via Tarantool-patches @ 2024-03-13 14:40 UTC (permalink / raw)
  To: tarantool-patches, skaplun, sergeyb

There is no error-checking in profilers which results in raw Lua
errors being reported in cases of non-existing paths or corrupt
file structures. This patch adds a profiler-agnostic module for
event parsing, which is going to be used to introduce
user-friendly errors. It is impossible to enable it right now
because it should be included in Tarantool's build procedure
first.

Part of tarantool/tarantool#9217
Part of tarantool/tarantool#5994
---
 Makefile.original      |  2 +-
 tools/CMakeLists.txt   |  4 ++++
 tools/utils/evread.lua | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 tools/utils/evread.lua

diff --git a/Makefile.original b/Makefile.original
index d0834fe6..4a1e1d9d 100644
--- a/Makefile.original
+++ b/Makefile.original
@@ -97,7 +97,7 @@ FILES_JITLIB= bc.lua bcsave.lua dump.lua p.lua v.lua zone.lua \
 	      dis_x86.lua dis_x64.lua dis_arm.lua dis_arm64.lua \
 	      dis_arm64be.lua dis_ppc.lua dis_mips.lua dis_mipsel.lua \
 	      dis_mips64.lua dis_mips64el.lua vmdef.lua
-FILES_UTILSLIB= avl.lua bufread.lua symtab.lua
+FILES_UTILSLIB= avl.lua bufread.lua evread.lua symtab.lua
 FILES_MEMPROFLIB= humanize.lua parse.lua process.lua
 FILES_SYSPROFLIB= parse.lua
 FILES_TOOLSLIB= memprof.lua sysprof.lua
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index a4adc15b..695c079a 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -18,6 +18,7 @@ else()
     memprof.lua
     utils/avl.lua
     utils/bufread.lua
+    utils/evread.lua
     utils/symtab.lua
   )
   list(APPEND LUAJIT_TOOLS_DEPS tools-parse-memprof)
@@ -36,6 +37,7 @@ else()
   install(FILES
       ${CMAKE_CURRENT_SOURCE_DIR}/utils/avl.lua
       ${CMAKE_CURRENT_SOURCE_DIR}/utils/bufread.lua
+      ${CMAKE_CURRENT_SOURCE_DIR}/utils/evread.lua
       ${CMAKE_CURRENT_SOURCE_DIR}/utils/symtab.lua
     DESTINATION ${LUAJIT_DATAROOTDIR}/utils
     PERMISSIONS
@@ -65,6 +67,7 @@ else()
     sysprof.lua
     utils/avl.lua
     utils/bufread.lua
+    utils/evread.lua
     utils/symtab.lua
   )
   list(APPEND LUAJIT_TOOLS_DEPS tools-parse-sysprof)
@@ -81,6 +84,7 @@ else()
   install(FILES
       ${CMAKE_CURRENT_SOURCE_DIR}/utils/avl.lua
       ${CMAKE_CURRENT_SOURCE_DIR}/utils/bufread.lua
+      ${CMAKE_CURRENT_SOURCE_DIR}/utils/evread.lua
       ${CMAKE_CURRENT_SOURCE_DIR}/utils/symtab.lua
     DESTINATION ${LUAJIT_DATAROOTDIR}/utils
     PERMISSIONS
diff --git a/tools/utils/evread.lua b/tools/utils/evread.lua
new file mode 100644
index 00000000..394a4a03
--- /dev/null
+++ b/tools/utils/evread.lua
@@ -0,0 +1,32 @@
+local bufread = require('utils.bufread')
+local symtab = require('utils.symtab')
+
+local function make_error_handler(fmt, inputfile)
+  return function(err)
+    io.stderr:write(string.format(fmt, inputfile))
+    io.stderr:write(string.format('\n\t%s\n', err))
+    os.exit(1, true)
+  end
+end
+
+return function(parser, inputfile)
+  local _, reader = xpcall(
+    bufread.new,
+    make_error_handler('Failed to open %s.', inputfile),
+    inputfile
+  )
+
+  local _, symbols = xpcall(
+    symtab.parse,
+    make_error_handler('Failed to parse symtab from %s.', inputfile),
+    reader
+  )
+
+  local _, events = xpcall(
+    parser,
+    make_error_handler('Failed to parse profile data from %s.', inputfile),
+    reader,
+    symbols
+  )
+  return events, symbols
+end
-- 
2.44.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers
  2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
                   ` (5 preceding siblings ...)
  2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 6/6] profilers: introduce event reader module Maxim Kokryashkin via Tarantool-patches
@ 2024-03-15  8:44 ` Sergey Kaplun via Tarantool-patches
  2024-03-20 15:08 ` Sergey Kaplun via Tarantool-patches
  7 siblings, 0 replies; 9+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2024-03-15  8:44 UTC (permalink / raw)
  To: Maxim Kokryashkin; +Cc: tarantool-patches

Hi, Maxim!
Thanks for the fixes!
LGTM!

-- 
Best regards,
Sergey Kaplun

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers
  2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
                   ` (6 preceding siblings ...)
  2024-03-15  8:44 ` [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Sergey Kaplun via Tarantool-patches
@ 2024-03-20 15:08 ` Sergey Kaplun via Tarantool-patches
  7 siblings, 0 replies; 9+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2024-03-20 15:08 UTC (permalink / raw)
  To: Maxim Kokryashkin; +Cc: tarantool-patches

I've checked the patchset into all long-term branches in
tarantool/luajit and bumped a new version in master [1], release/3.0 [2]
and release/2.11 [3].

[1]: https://github.com/tarantool/tarantool/pull/9832
[2]: https://github.com/tarantool/tarantool/pull/9833
[3]: https://github.com/tarantool/tarantool/pull/9834

-- 
Best regards,
Sergey Kaplun

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-03-20 15:13 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-13 14:40 [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Maxim Kokryashkin via Tarantool-patches
2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 1/6] build: purge sysprof.collapse module Maxim Kokryashkin via Tarantool-patches
2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 2/6] build: fix tool components handling Maxim Kokryashkin via Tarantool-patches
2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 3/6] memprof: refactor `heap_chunk` data structure Maxim Kokryashkin via Tarantool-patches
2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 4/6] memprof: remove unused arguments Maxim Kokryashkin via Tarantool-patches
2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 5/6] memprof: introduce the `--human-readable` option Maxim Kokryashkin via Tarantool-patches
2024-03-13 14:40 ` [Tarantool-patches] [PATCH luajit v3 6/6] profilers: introduce event reader module Maxim Kokryashkin via Tarantool-patches
2024-03-15  8:44 ` [Tarantool-patches] [PATCH luajit v3 0/6] profilers: refactor parsers Sergey Kaplun via Tarantool-patches
2024-03-20 15:08 ` Sergey Kaplun via Tarantool-patches

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