[Tarantool-patches] [PATCH luajit 3/7] memprof: move symtab to a separate module

Maxim Kokryashkin max.kokryashkin at gmail.com
Wed Sep 8 20:50:46 MSK 2021


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          | 37 ++-----------------------------
 src/lj_memprof.h          | 36 ------------------------------
 src/lj_symtab.c           | 36 ++++++++++++++++++++++++++++++
 src/lj_symtab.h           | 46 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 88 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 50c23cae..0f3d888f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -60,6 +60,7 @@ make_source_list(SOURCES_UTILS
   SOURCES
     lj_alloc.c
     lj_char.c
+    lj_symtab.c
     lj_utils_leb128.c
     lj_vmmath.c
     lj_wbuf.c
diff --git a/src/Makefile.dep.original b/src/Makefile.dep.original
index da5abf0d..b7d82719 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        := <BYTE>
-** reserved       := <BYTE> <BYTE> <BYTE>
-** sym            := sym-lua | sym-final
-** sym-lua        := sym-header sym-addr sym-chunk sym-line
-** sym-header     := <BYTE>
-** sym-addr       := <ULEB128>
-** sym-chunk      := string
-** sym-line       := <ULEB128>
-** sym-final      := sym-header
-** string         := string-len string-payload
-** string-len     := <ULEB128>
-** string-payload := <BYTE> {string-len}
-**
-** <BYTE>   :  A single byte (no surprises here)
-** <ULEB128>:  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..93e19353
--- /dev/null
+++ b/src/lj_symtab.c
@@ -0,0 +1,36 @@
+#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..9ea2c949
--- /dev/null
+++ b/src/lj_symtab.h
@@ -0,0 +1,46 @@
+#ifndef LJ_SYMTAB
+#define LJ_SYMTAB
+
+#include "lj_wbuf.h"
+#include "lj_obj.h"
+
+/*
+** symtab format:
+**
+** symtab         := prologue sym*
+** prologue       := 'l' 'j' 's' version reserved
+** version        := <BYTE>
+** reserved       := <BYTE> <BYTE> <BYTE>
+** sym            := sym-lua | sym-final
+** sym-lua        := sym-header sym-addr sym-chunk sym-line
+** sym-header     := <BYTE>
+** sym-addr       := <ULEB128>
+** sym-chunk      := string
+** sym-line       := <ULEB128>
+** sym-final      := sym-header
+** string         := string-len string-payload
+** string-len     := <ULEB128>
+** string-payload := <BYTE> {string-len}
+**
+** <BYTE>   :  A single byte (no surprises here)
+** <ULEB128>:  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 LJS_CURRENT_VERSION 0x1
+
+#define SYMTAB_LFUNC ((uint8_t)0)
+#define SYMTAB_FINAL ((uint8_t)0x80)
+
+void lj_symtab_dump(struct lj_wbuf *out, const struct global_State *g);
+
+#endif
-- 
2.33.0



More information about the Tarantool-patches mailing list