From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 5FB628BCDDC; Mon, 4 Dec 2023 16:26:42 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5FB628BCDDC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1701696402; bh=mofpjcViI7biAFPdtBGZ9+m8xSvKPZbKMmmDs2o50VA=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ekPnZ4dCmNiJd1D3czNZ6MNNlSyNHCkkO4yJyya8xWij749sGBOkJUBSB0GivyMan dfMVJT86mPGdolU1etEAuUztBaogiYJOB0pyKOkwk3WEyCr3419Z+dkncS4mX7c0MV +3wJYIaz83NfhiXcM1T1JF5l2p3PNoXUAMhMPEWI= Received: from mail-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 19DE88BCDDA for ; Mon, 4 Dec 2023 16:25:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 19DE88BCDDA Received: by mail-lj1-f176.google.com with SMTP id 38308e7fff4ca-2c9eca5bbaeso24565081fa.3 for ; Mon, 04 Dec 2023 05:25:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701696317; x=1702301117; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Iurosd5Ity010hqk4MHPxuhS4GUMLPCuSE0aueVZuZE=; b=tbyIpBsah7p36rzumfzN676ZLXuzVlUuddFF5pcF2qLkNrM03vOJuMiUpjEUsfsb4U 5EUVbvfngvhwoKIofOgvQ2I+2Aw0EI1fCl4DsufLVSaeWtlviZXhVwHg9tdpGCHkTgjq caXhHBjf2i0vUUEKntsFWFz2EeM278EHQN027U+tJfhRU/htxS8sewxcFq29IQzhjq5Z DzOTKttnUDH+LeMYkQQ5JMOyiqe3T5uV6Yu0ys/61HSlLyl+mCG8xCd/DckTMPoydvHh d3PNbD4Zl0NmEnMt+/384/X1wzRo+BPCkpod3jXGU4oqI9UnXJ7RZ2XW3zKAs8ffxYO5 83qg== X-Gm-Message-State: AOJu0Yxl38Pb1IFVu95yixLEtRX6uDg1DFj8QdJHG5W/nWoJIACuJYZE 9wRf3p7mICinuBnzl+tZODI8OzenXj18ow== X-Google-Smtp-Source: AGHT+IGSzXhE1spT/qzsdy0JdA1vtcJYwcS3JkIdaFP97bVg91d4Dq7SM/64DhwYefybzAeA6B9a6g== X-Received: by 2002:a2e:9790:0:b0:2c9:ffe9:23cb with SMTP id y16-20020a2e9790000000b002c9ffe923cbmr401943lji.96.1701696316921; Mon, 04 Dec 2023 05:25:16 -0800 (PST) Received: from fckxorg.mail.msk ([2a00:1148:b0ba:16:a3e8:bdc1:dbed:dbc8]) by smtp.gmail.com with ESMTPSA id x1-20020a2e9dc1000000b002ca09599e28sm196597ljj.107.2023.12.04.05.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 05:25:16 -0800 (PST) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Mon, 4 Dec 2023 16:25:01 +0300 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit 3/4] memprof: introduce the `--human-readable` option X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 | 72 +++++++++++++++++++ tools/memprof.lua | 21 ++++-- tools/memprof/humanize.lua | 46 +++++++++--- 3 files changed, 122 insertions(+), 17 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..0a579cec --- /dev/null +++ b/test/tarantool-tests/gh-5994-memprof-human-readable.test.lua @@ -0,0 +1,72 @@ +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, +}) +test:plan(5) + +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. +} + +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 e23bbf24..acadbc17 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 @@ -39,9 +45,12 @@ Supported options are: 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 +110,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, symbols) + if not config.leak_only then + view.profile_info(events, config) end - local dheap = process.form_heap_delta(events, symbols) - view.leak_info(dheap) + local dheap = process.form_heap_delta(events) + view.leak_info(dheap, config) view.aliases(symbols) -- XXX: The second argument is required to properly close Lua -- universe (i.e. invoke before exiting). diff --git a/tools/memprof/humanize.lua b/tools/memprof/humanize.lua index 5b289ce3..a5fdabf7 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 i = 1 + + while bytes >= 1024 and i < #units do + bytes = bytes / 1024 + i = i + 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[i]) +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.43.0