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 9F5626ECE8; Wed, 20 Apr 2022 15:59:41 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9F5626ECE8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1650459581; bh=wEe3QAE9z4s43Xh1DUEloZFYLiKUYfeN+lOOVg5/RQA=; 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=UIVdusm/0OrjyCBrmPxvwY7uzCOODQhKt+waAO6TgetsqjW5ZUjr6F7TJ8jQTaUkQ 4v++PkLItpBV/CTdc2PZBON5oTm/s5UPsfTdAYShu5TibuxXY+eCvYUmJ2aLrK+QRn Ovj/pZCjphgR7wPr1Q/p1aByv8JEHjd+TQSeONvE= Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (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 9BD2F6F176 for ; Wed, 20 Apr 2022 15:58:15 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9BD2F6F176 Received: by mail-lf1-f53.google.com with SMTP id t25so2839474lfg.7 for ; Wed, 20 Apr 2022 05:58:15 -0700 (PDT) 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=uDpK2Zzu6ur7dkDRV5lU2CJ4M3zZFtfcTN/yHsNHzVM=; b=ffXS7d0htbjVTmsUWmPHTAY0gT4k9PsIDCu3C1oUr6kHJBJ+6cWSn4NpymHOrsCM4g vJqdIa0G8nMqTNwiVlDiy/kBnLBy7Nle8+JhRB350sv5mH7e0cL8lboFi1sIfFeD6UoX sRZ0SiE7mUtcc+QWbSY0zRFkn1iyyiPWeBmhlQqQxiI2xkB/MrqOGx95NonmrF8Ypezy Bp6Oi0h0YpnLmHDhNdPx8AHejpL0iBTDHHJWhIlI7O1fUzlzSqZacHDEnRk+bDssul7b QLcbxDrvePmYYk+uBP9iJOK60hAtbzS/WZCDYcNy637rAq279yle4dDoD/X01b977rWN UmeA== X-Gm-Message-State: AOAM531hN/tBy3KP6dBUesGMOkeICQ6Tus7PqNW+9ukO3uGy+H302eu/ tVkgy1k7iBosB5GfqeqYUmeeXNuqaVS3uA== X-Google-Smtp-Source: ABdhPJzryTJW66af6rHGqvowTbIe5NE7EWzzY6VUdn6Hk9HIfTC3oNOc799UJq/1am4nVhVfuxldhg== X-Received: by 2002:ac2:4649:0:b0:46d:f73:777b with SMTP id s9-20020ac24649000000b0046d0f73777bmr13933686lfo.595.1650459494514; Wed, 20 Apr 2022 05:58:14 -0700 (PDT) Received: from localhost.localdomain ([93.175.28.57]) by smtp.gmail.com with ESMTPSA id o3-20020a198c03000000b00448b7b1780csm1824076lfd.63.2022.04.20.05.58.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 05:58:14 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Wed, 20 Apr 2022 15:57:59 +0300 Message-Id: <13a5af2d5dca208448cebb221288525486546999.1650459246.git.m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v5 3/7] memprof: move symtab to a separate module 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" Considering the symbol table format, it is obvious that it is suitable not only for memprof, but for other profiling modules too. This commit moves the symbol table to the separate module, so modules other than memprof will be able to access it. Part of tarantool/tarantool#781 --- src/CMakeLists.txt | 1 + src/Makefile.dep.original | 4 +- src/lj_memprof.c | 82 +------------------------------------- src/lj_memprof.h | 41 ------------------- src/lj_symtab.c | 84 +++++++++++++++++++++++++++++++++++++++ src/lj_symtab.h | 65 ++++++++++++++++++++++++++++++ src/ljamalg.c | 1 + 7 files changed, 156 insertions(+), 122 deletions(-) create mode 100644 src/lj_symtab.c create mode 100644 src/lj_symtab.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c92d78cc..b4ce407b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -70,6 +70,7 @@ make_source_list(SOURCES_PROFILER lj_memprof.c lj_profile.c lj_profile_timer.c + lj_symtab.c ) # Lua standard library + extensions by LuaJIT. diff --git a/src/Makefile.dep.original b/src/Makefile.dep.original index fc0fb5d2..efe39e84 100644 --- a/src/Makefile.dep.original +++ b/src/Makefile.dep.original @@ -147,7 +147,7 @@ lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_dispatch.h lj_bc.h lj_traceerr.h lj_vm.h lj_memprof.o: lj_memprof.c lj_arch.h lua.h luaconf.h lj_memprof.h \ lj_def.h lj_wbuf.h lj_obj.h lj_frame.h lj_bc.h lj_debug.h lj_dispatch.h \ - lj_jit.h lj_ir.h + lj_jit.h lj_ir.h lj_symtab.h lj_meta.o: lj_meta.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_meta.h lj_frame.h \ lj_bc.h lj_vm.h lj_strscan.h lj_strfmt.h lj_lib.h @@ -203,6 +203,8 @@ lj_strfmt_num.o: lj_strfmt_num.c lj_obj.h lua.h luaconf.h lj_def.h \ lj_arch.h lj_buf.h lj_gc.h lj_str.h lj_strfmt.h lj_strscan.o: lj_strscan.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_char.h lj_strscan.h +lj_symtab.o: lj_symtab.c lj_symtab.h lj_wbuf.h lj_def.h lua.h luaconf.h \ + lj_obj.h lj_arch.h lj_tab.o: lj_tab.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ lj_err.h lj_errmsg.h lj_tab.h lj_trace.o: lj_trace.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ diff --git a/src/lj_memprof.c b/src/lj_memprof.c index 2d779983..e6ebf009 100644 --- a/src/lj_memprof.c +++ b/src/lj_memprof.c @@ -19,86 +19,8 @@ #include "lj_frame.h" #include "lj_debug.h" -#if LJ_HASJIT -#include "lj_dispatch.h" -#endif - -/* --------------------------------- Symtab --------------------------------- */ - -static const unsigned char ljs_header[] = {'l', 'j', 's', LJS_CURRENT_VERSION, - 0x0, 0x0, 0x0}; - -#if LJ_HASJIT - -static void dump_symtab_trace(struct lj_wbuf *out, const GCtrace *trace) -{ - GCproto *pt = &gcref(trace->startpt)->pt; - BCLine lineno = 0; - - const BCIns *startpc = mref(trace->startpc, const BCIns); - lua_assert(startpc >= proto_bc(pt) && - startpc < proto_bc(pt) + pt->sizebc); - - lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); - - lj_wbuf_addbyte(out, SYMTAB_TRACE); - lj_wbuf_addu64(out, (uint64_t)trace->traceno); - lj_wbuf_addu64(out, (uint64_t)trace->mcode); - /* - ** The information about the prototype, associated with the - ** trace's start has already been dumped, as it is anchored - ** via the trace and is not collected while the trace is alive. - ** For this reason, we do not need to repeat dumping the chunk - ** name for the prototype. - */ - lj_wbuf_addu64(out, (uintptr_t)pt); - lj_wbuf_addu64(out, (uint64_t)lineno); -} - -#else - -static void dump_symtab_trace(struct lj_wbuf *out, const GCtrace *trace) -{ - UNUSED(out); - UNUSED(trace); - lua_assert(0); -} - -#endif - -static void dump_symtab(struct lj_wbuf *out, const struct global_State *g) -{ - const GCRef *iter = &g->gc.root; - const GCobj *o; - const size_t ljs_header_len = sizeof(ljs_header) / sizeof(ljs_header[0]); - - /* Write prologue. */ - lj_wbuf_addn(out, ljs_header, ljs_header_len); - - while ((o = gcref(*iter)) != NULL) { - switch (o->gch.gct) { - case (~LJ_TPROTO): { - const GCproto *pt = gco2pt(o); - lj_wbuf_addbyte(out, SYMTAB_LFUNC); - lj_wbuf_addu64(out, (uintptr_t)pt); - lj_wbuf_addstring(out, proto_chunknamestr(pt)); - lj_wbuf_addu64(out, (uint64_t)pt->firstline); - break; - } - case (~LJ_TTRACE): { - dump_symtab_trace(out, gco2trace(o)); - break; - } - default: - break; - } - iter = &o->gch.nextgc; - } - - lj_wbuf_addbyte(out, SYMTAB_FINAL); -} - /* ---------------------------- Memory profiler ----------------------------- */ +#include "lj_symtab.h" enum memprof_state { /* Memory profiler is not running. */ @@ -331,7 +253,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); + lj_symtab_dump(&mp->out, mp->g); /* 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 395fb429..7fe17af4 100644 --- a/src/lj_memprof.h +++ b/src/lj_memprof.h @@ -16,47 +16,6 @@ #include "lj_def.h" #include "lj_wbuf.h" -#define LJS_CURRENT_VERSION 0x2 - -/* -** symtab format: -** -** symtab := prologue sym* -** prologue := 'l' 'j' 's' version reserved -** version := -** reserved := -** sym := sym-lua | sym-trace | sym-final -** sym-lua := sym-header sym-addr sym-chunk sym-line -** sym-trace := sym-header trace-no trace-addr sym-addr sym-line -** sym-header := -** sym-addr := -** sym-chunk := string -** sym-line := -** sym-final := sym-header -** trace-no := -** trace-addr := -** string := string-len string-payload -** string-len := -** string-payload := {string-len} -** -** : A single byte (no surprises here) -** : Unsigned integer represented in ULEB128 encoding -** -** (Order of bits below is hi -> lo) -** -** version: [VVVVVVVV] -** * VVVVVVVV: Byte interpreted as a plain numeric version number -** -** sym-header: [FUUUUUTT] -** * TT : 2 bits for representing symbol type -** * UUUUU : 5 unused bits -** * F : 1 bit marking the end of the symtab (final symbol) -*/ - -#define SYMTAB_LFUNC ((uint8_t)0) -#define SYMTAB_TRACE ((uint8_t)1) -#define SYMTAB_FINAL ((uint8_t)0x80) - #define LJM_CURRENT_FORMAT_VERSION 0x02 /* diff --git a/src/lj_symtab.c b/src/lj_symtab.c new file mode 100644 index 00000000..0cd3e8fc --- /dev/null +++ b/src/lj_symtab.c @@ -0,0 +1,84 @@ +/* +** Implementation of symbol table for profilers. +** +** Major portions taken verbatim or adapted from the LuaVela. +** Copyright (C) 2015-2019 IPONWEB Ltd. +*/ + +#define lj_symtab_c +#define LUA_CORE + +#include "lj_symtab.h" + +static const unsigned char ljs_header[] = {'l', 'j', 's', LJS_CURRENT_VERSION, + 0x0, 0x0, 0x0}; + +#if LJ_HASJIT + +static void dump_symtab_trace(struct lj_wbuf *out, const GCtrace *trace) +{ + GCproto *pt = &gcref(trace->startpt)->pt; + BCLine lineno = 0; + + const BCIns *startpc = mref(trace->startpc, const BCIns); + lua_assert(startpc >= proto_bc(pt) && + startpc < proto_bc(pt) + pt->sizebc); + + lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); + + lj_wbuf_addbyte(out, SYMTAB_TRACE); + lj_wbuf_addu64(out, (uint64_t)trace->traceno); + lj_wbuf_addu64(out, (uint64_t)trace->mcode); + /* + ** The information about the prototype, associated with the + ** trace's start has already been dumped, as it is anchored + ** via the trace and is not collected while the trace is alive. + ** For this reason, we do not need to repeat dumping the chunk + ** name for the prototype. + */ + lj_wbuf_addu64(out, (uintptr_t)pt); + lj_wbuf_addu64(out, (uint64_t)lineno); +} + +#else + +static void dump_symtab_trace(struct lj_wbuf *out, const GCtrace *trace) +{ + UNUSED(out); + UNUSED(trace); + lua_assert(0); +} + +#endif + +void lj_symtab_dump(struct lj_wbuf *out, const struct global_State *g) +{ + const GCRef *iter = &g->gc.root; + const GCobj *o; + const size_t ljs_header_len = sizeof(ljs_header) / sizeof(ljs_header[0]); + + /* Write prologue. */ + lj_wbuf_addn(out, ljs_header, ljs_header_len); + + while ((o = gcref(*iter)) != NULL) { + switch (o->gch.gct) { + case (~LJ_TPROTO): { + const GCproto *pt = gco2pt(o); + lj_wbuf_addbyte(out, SYMTAB_LFUNC); + lj_wbuf_addu64(out, (uintptr_t)pt); + lj_wbuf_addstring(out, proto_chunknamestr(pt)); + lj_wbuf_addu64(out, (uint64_t)pt->firstline); + break; + } + case (~LJ_TTRACE): { + dump_symtab_trace(out, gco2trace(o)); + break; + } + default: + break; + } + iter = &o->gch.nextgc; + } + + lj_wbuf_addbyte(out, SYMTAB_FINAL); +} diff --git a/src/lj_symtab.h b/src/lj_symtab.h new file mode 100644 index 00000000..03384b1f --- /dev/null +++ b/src/lj_symtab.h @@ -0,0 +1,65 @@ +/* +** Symbol table for profilers. +** +** Major portions taken verbatim or adapted from the LuaVela. +** Copyright (C) 2015-2019 IPONWEB Ltd. +*/ + +#ifndef LJ_SYMTAB_H +#define LJ_SYMTAB_H + +#include "lj_wbuf.h" +#include "lj_obj.h" +#include "lj_debug.h" + +#if LJ_HASJIT +#include "lj_dispatch.h" +#endif + +#define LJS_CURRENT_VERSION 0x2 + +/* +** symtab format: +** +** symtab := prologue sym* +** prologue := 'l' 'j' 's' version reserved +** version := +** reserved := +** sym := sym-lua | sym-trace | sym-final +** sym-lua := sym-header sym-addr sym-chunk sym-line +** sym-trace := sym-header trace-no trace-addr sym-addr sym-line +** sym-header := +** sym-addr := +** sym-chunk := string +** sym-line := +** sym-final := sym-header +** trace-no := +** trace-addr := +** string := string-len string-payload +** string-len := +** string-payload := {string-len} +** +** : A single byte (no surprises here) +** : Unsigned integer represented in ULEB128 encoding +** +** (Order of bits below is hi -> lo) +** +** version: [VVVVVVVV] +** * VVVVVVVV: Byte interpreted as a plain numeric version number +** +** sym-header: [FUUUUUTT] +** * TT : 2 bits for representing symbol type +** * UUUUU : 5 unused bits +** * F : 1 bit marking the end of the symtab (final symbol) +*/ + +#define SYMTAB_LFUNC ((uint8_t)0) +#define SYMTAB_TRACE ((uint8_t)1) +#define SYMTAB_FINAL ((uint8_t)0x80) + +/* +** Dumps symbol table for Lua functions into a buffer +*/ +void lj_symtab_dump(struct lj_wbuf *out, const struct global_State *g); + +#endif diff --git a/src/ljamalg.c b/src/ljamalg.c index ce7a0d6c..6ad5289c 100644 --- a/src/ljamalg.c +++ b/src/ljamalg.c @@ -52,6 +52,7 @@ #include "lj_mapi.c" #include "lj_profile.c" #include "lj_profile_timer.c" +#include "lj_symtab.c" #include "lj_memprof.c" #include "lj_lex.c" #include "lj_parse.c" -- 2.35.1