Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun <skaplun@tarantool.org>
To: Igor Munkin <imun@tarantool.org>,
	Sergey Ostanevich <sergos@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH luajit v1 09/11] misc: add Lua API for memory profiler
Date: Wed, 16 Dec 2020 22:13:44 +0300	[thread overview]
Message-ID: <174b6bcbb0651777a919f8f9ecee38ccb8415694.1608142899.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1608142899.git.skaplun@tarantool.org>

This patch introduces Lua API for LuaJIT memory profiler implemented in
the scope of the previous patch.

Profiler returns `true` if start successfully, returns nil on failure
(plus an error message as a second result and a system-dependent error
code as a third result) and some true value on success.
If LuaJIT build without memory profiler both return `false`.

<lj_errmsg.h> have adjusted with two new errors
PROF_ISRUNNING/PROF_NOTRUNNING returned in case when profiler has
started/stopped already correspondingly.

Part of tarantool/tarantool#5442
---
 src/Makefile.dep |   5 +-
 src/lib_misc.c   | 165 +++++++++++++++++++++++++++++++++++++++++++++++
 src/lj_errmsg.h  |   6 ++
 3 files changed, 174 insertions(+), 2 deletions(-)

diff --git a/src/Makefile.dep b/src/Makefile.dep
index c41fdcf..510b5e5 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -29,8 +29,9 @@ lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
  lj_vm.h lj_vmevent.h lj_lib.h luajit.h lj_libdef.h
 lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
  lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_libdef.h
-lib_misc.o: lib_misc.c lua.h luaconf.h lmisclib.h lj_obj.h lj_def.h lj_arch.h \
- lj_str.h lj_tab.h lj_lib.h lj_libdef.h
+lib_misc.o: lib_misc.c lua.h luaconf.h lmisclib.h lauxlib.h lj_obj.h \
+ lj_def.h lj_arch.h lj_str.h lj_tab.h lj_lib.h lj_gc.h lj_err.h \
+ lj_errmsg.h profile/ljp_memprof.h lj_libdef.h
 lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
  lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_lib.h \
  lj_libdef.h
diff --git a/src/lib_misc.c b/src/lib_misc.c
index 6f7b9a9..d3b5ab4 100644
--- a/src/lib_misc.c
+++ b/src/lib_misc.c
@@ -8,13 +8,21 @@
 #define lib_misc_c
 #define LUA_LIB
 
+#include <stdio.h>
+#include <errno.h>
+
 #include "lua.h"
 #include "lmisclib.h"
+#include "lauxlib.h"
 
 #include "lj_obj.h"
 #include "lj_str.h"
 #include "lj_tab.h"
 #include "lj_lib.h"
+#include "lj_gc.h"
+#include "lj_err.h"
+
+#include "profile/ljp_memprof.h"
 
 /* ------------------------------------------------------------------------ */
 
@@ -67,8 +75,165 @@ LJLIB_CF(misc_getmetrics)
 
 #include "lj_libdef.h"
 
+/* ----- misc.memprof module ---------------------------------------------- */
+
+#define LJLIB_MODULE_misc_memprof
+
+/*
+** Yep, 8Mb. Tuned in order not to bother the platform with too often flushes.
+*/
+#define STREAM_BUFFER_SIZE (8 * 1024 * 1024)
+
+/* Structure given as ctx to memprof writer and on_stop callback. */
+struct memprof_ctx {
+  /* Output file stream for data. */
+  FILE *stream;
+  /* Profiled global_State for lj_mem_free at on_stop callback. */
+  global_State *g;
+};
+
+static LJ_AINLINE void memprof_ctx_free(struct memprof_ctx *ctx, uint8_t *buf)
+{
+  lj_mem_free(ctx->g, buf, STREAM_BUFFER_SIZE);
+  lj_mem_free(ctx->g, ctx, sizeof(*ctx));
+}
+
+/* Default buffer writer function. Just call fwrite to corresponding FILE. */
+static size_t buffer_writer_default(const void **buf_addr, size_t len,
+				    void *opt)
+{
+  FILE *stream = ((struct memprof_ctx *)opt)->stream;
+  const void * const buf_start = *buf_addr;
+  const void *data = *buf_addr;
+  size_t write_total = 0;
+
+  lua_assert(len <= STREAM_BUFFER_SIZE);
+
+  for (;;) {
+    const size_t written = fwrite(data, 1, len, stream);
+
+    if (LJ_UNLIKELY(written == 0)) {
+      /* Re-tries write in case of EINTR. */
+      if (errno == EINTR) {
+	errno = 0;
+	continue;
+      }
+      break;
+    }
+
+    write_total += written;
+
+    if (write_total == len)
+      break;
+
+    data = (uint8_t *)data + (ptrdiff_t)written;
+  }
+  lua_assert(write_total <= len);
+
+  *buf_addr = buf_start;
+  return write_total;
+}
+
+/* Default on stop callback. Just close corresponding stream. */
+static int on_stop_cb_default(void *opt, uint8_t *buf)
+{
+  struct memprof_ctx *ctx = opt;
+  FILE *stream = ctx->stream;
+  memprof_ctx_free(ctx, buf);
+  return fclose(stream);
+}
+
+/* local started, err, errno = misc.memprof.start(fname) */
+LJLIB_CF(misc_memprof_start)
+{
+  struct luam_Prof_options opt = {0};
+  struct memprof_ctx *ctx;
+  const char *fname;
+  int memprof_status;
+  int started;
+
+  fname = strdata(lj_lib_checkstr(L, 1));
+
+  ctx = lj_mem_new(L, sizeof(*ctx));
+  if (ctx == NULL)
+    goto errmem;
+
+  opt.ctx = ctx;
+  opt.writer = buffer_writer_default;
+  opt.on_stop = on_stop_cb_default;
+  opt.len = STREAM_BUFFER_SIZE;
+  opt.buf = (uint8_t *)lj_mem_new(L, STREAM_BUFFER_SIZE);
+  if (NULL == opt.buf) {
+    lj_mem_free(G(L), ctx, sizeof(*ctx));
+    goto errmem;
+  }
+
+  ctx->g = G(L);
+  ctx->stream = fopen(fname, "wb");
+
+  if (ctx->stream == NULL) {
+    memprof_ctx_free(ctx, opt.buf);
+    return luaL_fileresult(L, 0, fname);
+  }
+
+  memprof_status = ljp_memprof_start(L, &opt);
+  started = memprof_status == LUAM_PROFILE_SUCCESS;
+
+  if (LJ_UNLIKELY(!started)) {
+    fclose(ctx->stream);
+    remove(fname);
+    memprof_ctx_free(ctx, opt.buf);
+    switch (memprof_status) {
+    case LUAM_PROFILE_ERRRUN:
+      lua_pushnil(L);
+      setstrV(L, L->top++, lj_err_str(L, LJ_ERR_PROF_ISRUNNING));
+      return 2;
+    case LUAM_PROFILE_ERRMEM:
+      /* Unreachable for now. */
+      goto errmem;
+    case LUAM_PROFILE_ERRIO:
+      return luaL_fileresult(L, 0, fname);
+    default:
+      break;
+    }
+  }
+  lua_pushboolean(L, started);
+
+  return 1;
+errmem:
+  lua_pushnil(L);
+  setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));
+  return 2;
+}
+
+/* local stopped, err, errno = misc.memprof.stop() */
+LJLIB_CF(misc_memprof_stop)
+{
+  int status = ljp_memprof_stop();
+  int stopped_successfully = status == LUAM_PROFILE_SUCCESS;
+  if (!stopped_successfully) {
+    switch (status) {
+    case LUAM_PROFILE_ERRRUN:
+      lua_pushnil(L);
+      setstrV(L, L->top++, lj_err_str(L, LJ_ERR_PROF_NOTRUNNING));
+      return 2;
+    case LUAM_PROFILE_ERRIO:
+      return luaL_fileresult(L, 0, NULL);
+    default:
+      break;
+    }
+  }
+  lua_pushboolean(L, stopped_successfully);
+  return 1;
+}
+
+#include "lj_libdef.h"
+
+/* ------------------------------------------------------------------------ */
+
 LUALIB_API int luaopen_misc(struct lua_State *L)
 {
   LJ_LIB_REG(L, LUAM_MISCLIBNAME, misc);
+  LJ_LIB_REG(L, LUAM_MISCLIBNAME ".memprof", misc_memprof);
   return 1;
 }
diff --git a/src/lj_errmsg.h b/src/lj_errmsg.h
index de7b867..6816da2 100644
--- a/src/lj_errmsg.h
+++ b/src/lj_errmsg.h
@@ -185,6 +185,12 @@ ERRDEF(FFI_NYIPACKBIT,	"NYI: packed bit fields")
 ERRDEF(FFI_NYICALL,	"NYI: cannot call this C function (yet)")
 #endif
 
+#if LJ_HASPROFILE || LJ_HASMEMPROF
+/* Profiler errors. */
+ERRDEF(PROF_ISRUNNING,	"profiler is running already")
+ERRDEF(PROF_NOTRUNNING,	"profiler is not running")
+#endif
+
 #undef ERRDEF
 
 /* Detecting unused error messages:
-- 
2.28.0

  parent reply	other threads:[~2020-12-16 19:14 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-16 19:13 [Tarantool-patches] [PATCH luajit v1 00/11] LuaJIT " Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 01/11] build: add src dir in building Sergey Kaplun
2020-12-20 21:27   ` Igor Munkin
2020-12-23 18:20     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 02/11] utils: introduce leb128 reader and writer Sergey Kaplun
2020-12-20 22:44   ` Igor Munkin
2020-12-23 22:34     ` Sergey Kaplun
2020-12-24  9:11       ` Igor Munkin
2020-12-25  8:46         ` Sergey Kaplun
2020-12-23 16:50   ` Sergey Ostanevich
2020-12-23 22:36     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 03/11] profile: introduce profiler writing module Sergey Kaplun
2020-12-21  9:24   ` Igor Munkin
2020-12-24  6:46     ` Sergey Kaplun
2020-12-24 15:45       ` Sergey Ostanevich
2020-12-24 21:20         ` Sergey Kaplun
2020-12-25  9:37           ` Igor Munkin
2020-12-25 10:13             ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 04/11] profile: introduce symtab write module Sergey Kaplun
2020-12-21 10:30   ` Igor Munkin
2020-12-24  7:00     ` Sergey Kaplun
2020-12-24  9:36       ` Igor Munkin
2020-12-25  8:45         ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 05/11] vm: introduce LFUNC and FFUNC vmstates Sergey Kaplun
2020-12-25 11:07   ` Sergey Ostanevich
2020-12-25 11:23     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 06/11] core: introduce new mem_L field Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 07/11] debug: move debug_frameline to public module API Sergey Kaplun
2020-12-20 22:46   ` Igor Munkin
2020-12-24  6:50     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 08/11] profile: introduce memory profiler Sergey Kaplun
2020-12-16 19:13 ` Sergey Kaplun [this message]
2020-12-24 16:32   ` [Tarantool-patches] [PATCH luajit v1 09/11] misc: add Lua API for " Sergey Ostanevich
2020-12-24 21:25     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 10/11] tools: introduce tools directory Sergey Kaplun
2020-12-20 22:46   ` Igor Munkin
2020-12-24  6:47     ` Sergey Kaplun
2020-12-16 19:13 ` [Tarantool-patches] [PATCH luajit v1 11/11] profile: introduce profile parser Sergey Kaplun
2020-12-24 23:09   ` Igor Munkin
2020-12-25  8:41     ` Sergey Kaplun
2020-12-21 10:43 ` [Tarantool-patches] [PATCH luajit v1 00/11] LuaJIT memory profiler Igor Munkin
2020-12-24  7:02   ` Sergey Kaplun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=174b6bcbb0651777a919f8f9ecee38ccb8415694.1608142899.git.skaplun@tarantool.org \
    --to=skaplun@tarantool.org \
    --cc=imun@tarantool.org \
    --cc=sergos@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit v1 09/11] misc: add Lua API for memory profiler' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox