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 04D0B6EC40; Thu, 23 Sep 2021 17:51:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 04D0B6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1632408696; bh=v+14zJFaEKs71/WWzNdkxsoejkfGp57mZ+sIioXic5Q=; 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=pgmtdX816CCxSGyNwPCzD/4fvzUMUoR8HpgrxBrf6Uye1yqs6kLfOI+LOfwulzXhv qoVgz8wsRcCz3XLkZ4mLSc6bme/WjlqfgmclZB2grQnrRrwGb9EBwzLd9jdF+/LbOv imOGUPI7Ds1oXrNJIRoAdASm4Cuakn2Dk5jXny/4= Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (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 D8EDD6EC40 for ; Thu, 23 Sep 2021 17:51:34 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D8EDD6EC40 Received: by mail-lf1-f52.google.com with SMTP id u8so27053579lff.9 for ; Thu, 23 Sep 2021 07:51:34 -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=hp4bneUImdYutvkBI/nIOAtgw6RqUFgATuQXYz+Sz/U=; b=BSFP628AGKS46wQ4vU8x9ewvTFj0LSOJP8AslzhBVhv9CBs50SL0vsjSRGaEOiwyvC xem46htzYSPvOUCutOKR1eSz2mD2WEHC/NvaIw6n7Poj1kIPR23Bg9DXJFtE4fM04o9N BDOIdb1EYH9ZwEqbWLpc1OxAwBOk4AZalt3HIB5vLsjPsvHZXUxYOGnGK5qxBP3x/ACe Ndh0TEKzz0+KDz2565b5K5b02d3GqHgULC82zqXRg9HHDxcs9vYwBLks3ejX19H/ujRf ImjSyCtXyA6rDNe93x4KM/1dZKpJQHoXcVcMaPI1tTII1nIWmaN2T7mQ6Bi9vGzLolH8 Fffg== X-Gm-Message-State: AOAM530wKYdaHCq+pZ/5275/tWuadKJnN6QvEfdPKK6g8jX0VWLAHBTe 36Jr+Z84Ym5STOPS/N297YZ0wbJOCraWXpe2 X-Google-Smtp-Source: ABdhPJyGrCbsYY9XhVRFfrqczu/NbtfU2CYWx6qhLzzRfV9aIT4Qca25kHJQ2u9g56V76ZBybkQwCA== X-Received: by 2002:a05:6512:1087:: with SMTP id j7mr4503931lfg.139.1632408690887; Thu, 23 Sep 2021 07:51:30 -0700 (PDT) Received: from localhost.localdomain ([93.175.28.3]) by smtp.gmail.com with ESMTPSA id f3sm477804lfk.229.2021.09.23.07.51.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Sep 2021 07:51:30 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Thu, 23 Sep 2021 17:51:22 +0300 Message-Id: <20210923145122.30020-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v2] 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 --- Changes in v2: - Fixed comments as per review by Sergey Also, I've checked the LJS version in the tarantool branch, and it is 1. Link: https://github.com/tarantool/luajit/blob/814625fc7208d97d74e1998a23697b9a099da4b0/src/lj_memprof.h#L19 src/CMakeLists.txt | 1 + src/Makefile.dep.original | 4 ++- src/lj_memprof.c | 37 ++------------------------ src/lj_memprof.h | 36 ------------------------- src/lj_symtab.c | 42 +++++++++++++++++++++++++++++ src/lj_symtab.h | 56 +++++++++++++++++++++++++++++++++++++++ src/ljamalg.c | 1 + 7 files changed, 105 insertions(+), 72 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 68f51706..9389de73 100644 --- a/src/Makefile.dep.original +++ b/src/Makefile.dep.original @@ -146,7 +146,7 @@ lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_gc.h lj_err.h lj_errmsg.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.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_def.h lj_wbuf.h lj_obj.h lj_frame.h lj_bc.h lj_debug.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 @@ -202,6 +202,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 2c1ef3b8..fba688bc 100644 --- a/src/lj_memprof.c +++ b/src/lj_memprof.c @@ -19,40 +19,7 @@ #include "lj_frame.h" #include "lj_debug.h" -/* --------------------------------- Symtab --------------------------------- */ - -static const unsigned char ljs_header[] = {'l', 'j', 's', LJS_CURRENT_VERSION, - 0x0, 0x0, 0x0}; - -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; - } - 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. */ @@ -249,7 +216,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 3417475d..33a9b869 100644 --- a/src/lj_memprof.h +++ b/src/lj_memprof.h @@ -16,42 +16,6 @@ #include "lj_def.h" #include "lj_wbuf.h" -#define LJS_CURRENT_VERSION 0x1 - -/* -** symtab format: -** -** symtab := prologue sym* -** prologue := 'l' 'j' 's' version reserved -** version := -** reserved := -** sym := sym-lua | sym-final -** sym-lua := sym-header sym-addr sym-chunk sym-line -** sym-header := -** sym-addr := -** sym-chunk := string -** sym-line := -** sym-final := sym-header -** 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_FINAL ((uint8_t)0x80) #define LJM_CURRENT_FORMAT_VERSION 0x01 diff --git a/src/lj_symtab.c b/src/lj_symtab.c new file mode 100644 index 00000000..5ca8ccec --- /dev/null +++ b/src/lj_symtab.c @@ -0,0 +1,42 @@ +/* +** 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}; + +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; + } + 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..4bbdef82 --- /dev/null +++ b/src/lj_symtab.h @@ -0,0 +1,56 @@ +/* +** 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" + +#define LJS_CURRENT_VERSION 0x1 + +/* +** symtab format: +** +** symtab := prologue sym* +** prologue := 'l' 'j' 's' version reserved +** version := +** reserved := +** sym := sym-lua | sym-final +** sym-lua := sym-header sym-addr sym-chunk sym-line +** sym-header := +** sym-addr := +** sym-chunk := string +** sym-line := +** sym-final := sym-header +** 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_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.33.0