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 DD5A26E454; Fri, 4 Mar 2022 22:24:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DD5A26E454 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1646421861; bh=JRh97CF1eYRLgLw4z98OKwB7y7ZLyT78pvCvm7ovUwU=; 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=U/QBm4yuRIPa+ZyB11bywf7RvJJ1+l34wTlRd3RABCIRmGULPWCdJXNIB0UVfvA0C pNMPn1+eLTlX29rm71bCNGG8yPCy1ioCZyF6Hvit9+M8o9TYW9lyWm5bTyc2bBrKuh mu2HBXhz/is7945VTbaUGR5sXpK5NbRM+hg5wVR4= Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (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 523016E229 for ; Fri, 4 Mar 2022 22:22:29 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 523016E229 Received: by mail-lj1-f177.google.com with SMTP id v22so12306217ljh.7 for ; Fri, 04 Mar 2022 11:22:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iysRju9vUs5izwk91tpV/BoPxxjihfzo9UdA7E9DdNw=; b=6QfTNS/t1uRoZQUxII+h4IdaXuZ/1fZYchu8t69EY775t6lIS9OHb6oME0ye2RZOFZ Eb1rMjLPBHPbTw9788jO5Q6iCBW8tb6DY1liwkepcGbOJ/Qqay9eBJiB8Om0C6kYwiCv tClO0ABYnxhozsfcZrvxeyMfvUChIf63rQ9iQ/Hg1uk78UhS34gs2VxsqPMOMP864L8i x6/9vHbqAEGxV1sf7SshNSlS6OexnrE4dv5/p4/37J8oZxowT+BwqPjblWMcrIPIzADC 3jNlxMMso+j1uvvw3zXZBOMoVeBMj1R2el/hcgjeHXFtkxqtmy7sjSzlrcrXfE3zjEo+ 39cw== X-Gm-Message-State: AOAM532PrHqkxkQ1hAVaJAwdPlIbXIihISTLZ+Znf3Sk8prm2w7gr1Ns 5Hgw69ncVB4G5wGsodz3pi08sE9lD6Ey8tOpou8= X-Google-Smtp-Source: ABdhPJwumq5Tq3PQk1VexzIpFfN9z4qyfM6vcIBOyAODvXvt+aBhCrgIWcIhGIaivdaxRE+PO3zI6g== X-Received: by 2002:a05:651c:12c7:b0:23e:b8f9:15b2 with SMTP id 7-20020a05651c12c700b0023eb8f915b2mr96259lje.382.1646421748494; Fri, 04 Mar 2022 11:22:28 -0800 (PST) Received: from localhost.localdomain (109-252-140-49.dynamic.spd-mgts.ru. [109.252.140.49]) by smtp.gmail.com with ESMTPSA id u9-20020a056512128900b0044315b93df5sm1204687lfs.233.2022.03.04.11.22.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 11:22:28 -0800 (PST) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Fri, 4 Mar 2022 22:22:19 +0300 Message-Id: <20220304192219.1266071-3-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220304192219.1266071-1-m.kokryashkin@tarantool.org> References: <20220304192219.1266071-1-m.kokryashkin@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v5 2/2] memprof: enrich symtab with newly loaded symbols 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" This commit lets memprof extend its symtab when new C-symbols appear after dlopen. The following data is stored in event stream for each newly loaded symbol: | (AEVENT_SYMTAB | ASOURCE_CFUNC) | symbol address | symbol name | 1 byte 8 bytes magic number Resolves tarantool/tarantool#5813 --- src/lj_memprof.c | 70 +++++++++++++++++++++++++++++++++++------ src/lj_memprof.h | 8 ++++- tools/memprof.lua | 5 +++ tools/memprof/parse.lua | 19 +++++++++++ tools/utils/symtab.lua | 4 +++ 5 files changed, 96 insertions(+), 10 deletions(-) diff --git a/src/lj_memprof.c b/src/lj_memprof.c index 71c1da7f..2b609dfa 100644 --- a/src/lj_memprof.c +++ b/src/lj_memprof.c @@ -128,6 +128,11 @@ uint32_t ghashtab_size(ElfW(Addr) ghashtab) struct symbol_resolver_conf { struct lj_wbuf *buf; const uint8_t header; + + uint32_t cur_lib; + uint32_t lib_cnt_prev; + uint32_t to_dump_cnt; + uint32_t *lib_cnt; }; void write_c_symtab(ElfW(Sym*) sym, char *strtab, ElfW(Addr) so_addr, @@ -353,22 +358,46 @@ int resolve_symbolnames(struct dl_phdr_info *info, size_t info_size, void *data) const uint8_t header = conf->header; struct lj_wbuf *buf = conf->buf; - UNUSED(info_size); + const uint32_t lib_cnt_prev = *conf->lib_cnt; + uint32_t lib_cnt = 0; + + /* + ** Check that dlpi_adds and dlpi_subs fields are available. + ** Assertion was taken from the GLIBC tests: + ** https://code.woboq.org/userspace/glibc/elf/tst-dlmodcount.c.html#37 + */ + assert(info_size > offsetof(struct dl_phdr_info, dlpi_subs) + + sizeof(info->dlpi_subs)); + + lib_cnt = info->dlpi_adds - info->dlpi_subs; + conf->lib_cnt_prev = *conf->lib_cnt; - /* Skip vDSO library. */ + /* Skip vDSO library. */ if (info->dlpi_addr == getauxval(AT_SYSINFO_EHDR)) return 0; + if ((conf->to_dump_cnt = info->dlpi_adds - lib_cnt_prev) == 0) + /* No new libraries, stop resolver. */ + return 1; + + if (conf->cur_lib < lib_cnt - conf->to_dump_cnt) { + /* That lib is already dumped, skip it. */ + ++conf->cur_lib; + return 0; + } + /* ** Main way: try to open ELF and read SHT_SYMTAB, SHT_STRTAB and SHT_HASH ** sections from it. */ if (dump_sht_symtab(info->dlpi_name, buf, header, info->dlpi_addr) == 0) { + ++conf->cur_lib; return 0; } /* First fallback: dump functions only from PT_DYNAMIC segment. */ if(dump_dyn_symtab(info, header, buf) == 0) { + ++conf->cur_lib; return 0; } @@ -380,12 +409,13 @@ int resolve_symbolnames(struct dl_phdr_info *info, size_t info_size, void *data) lj_wbuf_addu64(buf, info->dlpi_addr); lj_wbuf_addstring(buf, info->dlpi_name); + ++conf->cur_lib; return 0; } #endif -static void dump_symtab(struct lj_wbuf *out, const struct global_State *g) +static void dump_symtab(struct lj_wbuf *out, const struct global_State *g, uint32_t *lib_cnt) { const GCRef *iter = &g->gc.root; const GCobj *o; @@ -395,7 +425,13 @@ static void dump_symtab(struct lj_wbuf *out, const struct global_State *g) struct symbol_resolver_conf conf = { out, SYMTAB_CFUNC, + 0, + *lib_cnt, + 0, + lib_cnt }; +#else + UNUSED(lib_cnt); #endif /* Write prologue. */ @@ -454,6 +490,7 @@ struct memprof { struct alloc orig_alloc; /* Original allocator. */ struct lj_memprof_options opt; /* Profiling options. */ int saved_errno; /* Saved errno when profiler deinstrumented. */ + uint32_t lib_cnt; /* Number of currently loaded libs. */ }; static struct memprof memprof = {0}; @@ -489,15 +526,30 @@ static void memprof_write_lfunc(struct lj_wbuf *out, uint8_t aevent, } static void memprof_write_cfunc(struct lj_wbuf *out, uint8_t aevent, - const GCfunc *fn) + const GCfunc *fn, uint32_t *lib_cnt) { +#if LUAJIT_OS != LUAJIT_OS_OSX + /* Check if there are any new libs. */ + struct symbol_resolver_conf conf = { + out, + AEVENT_SYMTAB | ASOURCE_CFUNC, + 0, + *lib_cnt, + 0, + lib_cnt + }; + dl_iterate_phdr(resolve_symbolnames, &conf); +#else + UNUSED(lib_cnt); +#endif + lj_wbuf_addbyte(out, aevent | ASOURCE_CFUNC); lj_wbuf_addu64(out, (uintptr_t)fn->c.f); } static void memprof_write_ffunc(struct lj_wbuf *out, uint8_t aevent, GCfunc *fn, struct lua_State *L, - cTValue *frame) + cTValue *frame, uint32_t *lib_cnt) { cTValue *pframe = frame_prev(frame); GCfunc *pfn = frame_func(pframe); @@ -510,7 +562,7 @@ static void memprof_write_ffunc(struct lj_wbuf *out, uint8_t aevent, if (pfn != NULL && isluafunc(pfn)) memprof_write_lfunc(out, aevent, pfn, L, frame); else - memprof_write_cfunc(out, aevent, fn); + memprof_write_cfunc(out, aevent, fn, lib_cnt); } static void memprof_write_func(struct memprof *mp, uint8_t aevent) @@ -523,9 +575,9 @@ static void memprof_write_func(struct memprof *mp, uint8_t aevent) if (isluafunc(fn)) memprof_write_lfunc(out, aevent, fn, L, NULL); else if (isffunc(fn)) - memprof_write_ffunc(out, aevent, fn, L, frame); + memprof_write_ffunc(out, aevent, fn, L, frame, &mp->lib_cnt); else if (iscfunc(fn)) - memprof_write_cfunc(out, aevent, fn); + memprof_write_cfunc(out, aevent, fn, &mp->lib_cnt); else lua_assert(0); } @@ -661,7 +713,7 @@ int lj_memprof_start(struct lua_State *L, const struct lj_memprof_options *opt) /* Init output. */ lj_wbuf_init(&mp->out, mp_opt->writer, mp_opt->ctx, mp_opt->buf, mp_opt->len); - dump_symtab(&mp->out, mp->g); + dump_symtab(&mp->out, mp->g, &mp->lib_cnt); /* Write prologue. */ lj_wbuf_addn(&mp->out, ljm_header, ljm_header_len); diff --git a/src/lj_memprof.h b/src/lj_memprof.h index 0327a205..ea8f2362 100644 --- a/src/lj_memprof.h +++ b/src/lj_memprof.h @@ -71,10 +71,11 @@ ** prologue := 'l' 'j' 'm' version reserved ** version := ** reserved := -** event := event-alloc | event-realloc | event-free +** event := event-alloc | event-realloc | event-free | event-symtab ** event-alloc := event-header loc? naddr nsize ** event-realloc := event-header loc? oaddr osize naddr nsize ** event-free := event-header loc? oaddr osize +** event-symtab := event-header sym-addr sym-name ** event-header := ** loc := loc-lua | loc-c | loc-trace ** loc-lua := sym-addr line-no @@ -88,7 +89,11 @@ ** naddr := ** osize := ** nsize := +** sym-name := string ** epilogue := event-header +** string := string-len string-payload +** string-len := +** string-payload := {string-len} ** ** : A single byte (no surprises here) ** : Unsigned integer represented in ULEB128 encoding @@ -107,6 +112,7 @@ */ /* Allocation events. */ +#define AEVENT_SYMTAB ((uint8_t)0) #define AEVENT_ALLOC ((uint8_t)1) #define AEVENT_FREE ((uint8_t)2) #define AEVENT_REALLOC ((uint8_t)(AEVENT_ALLOC | AEVENT_FREE)) diff --git a/tools/memprof.lua b/tools/memprof.lua index 18b44fdd..805b7e74 100644 --- a/tools/memprof.lua +++ b/tools/memprof.lua @@ -101,6 +101,11 @@ local function dump(inputfile) local reader = bufread.new(inputfile) local symbols = symtab.parse(reader) local events = memprof.parse(reader, symbols) + + for addr, event in pairs(events.symtab) do + symtab.add_cfunc(symbols, addr, event.name) + end + if not leak_only then view.profile_info(events, symbols) end diff --git a/tools/memprof/parse.lua b/tools/memprof/parse.lua index 47dbaee4..36343e4a 100644 --- a/tools/memprof/parse.lua +++ b/tools/memprof/parse.lua @@ -17,6 +17,7 @@ local LJM_CURRENT_VERSION = 0x02 local LJM_EPILOGUE_HEADER = 0x80 +local AEVENT_SYMTAB = 0 local AEVENT_ALLOC = 1 local AEVENT_FREE = 2 local AEVENT_REALLOC = 3 @@ -41,6 +42,7 @@ local function new_event(loc) free = 0, alloc = 0, primary = {}, + name = nil } end @@ -85,6 +87,21 @@ local function parse_location(reader, asource) return symtab.id(loc), loc end +local function parse_symtab(reader, asource, events, heap) + -- That instruction supresses unused variable warning + -- from luacheck. + local _ = asource or heap + + local id = reader:read_uleb128() + local name = reader:read_string() + + if not events[id] then + events[id] = new_event(0) + end + + events[id].name = name +end + local function parse_alloc(reader, asource, events, heap) local id, loc = parse_location(reader, asource) @@ -142,6 +159,7 @@ local function parse_free(reader, asource, events, heap) end local parsers = { + [AEVENT_SYMTAB] = {evname = "symtab", parse = parse_symtab}, [AEVENT_ALLOC] = {evname = "alloc", parse = parse_alloc}, [AEVENT_FREE] = {evname = "free", parse = parse_free}, [AEVENT_REALLOC] = {evname = "realloc", parse = parse_realloc}, @@ -182,6 +200,7 @@ function M.parse(reader) realloc = {}, free = {}, heap = {}, + symtab = {} } local magic = reader:read_octets(3) diff --git a/tools/utils/symtab.lua b/tools/utils/symtab.lua index aa66269c..75b0285b 100644 --- a/tools/utils/symtab.lua +++ b/tools/utils/symtab.lua @@ -158,4 +158,8 @@ function M.demangle(symtab, loc) return string_format("CFUNC %#x", addr) end +function M.add_cfunc(symtab, addr, name) + symtab.cfunc = avl.insert(symtab.cfunc, addr, {name = name}) +end + return M -- 2.35.1