From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp3.mail.ru (smtp3.mail.ru [94.100.179.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 1AA594765E3 for ; Sun, 27 Dec 2020 14:54:26 +0300 (MSK) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) From: Sergey Ostanevich In-Reply-To: <6552b7249cd4f3e2782805f71de205700d5b531f.1608907726.git.skaplun@tarantool.org> Date: Sun, 27 Dec 2020 14:54:24 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: References: <6552b7249cd4f3e2782805f71de205700d5b531f.1608907726.git.skaplun@tarantool.org> Subject: Re: [Tarantool-patches] [PATCH luajit v2 6/7] misc: add Lua API for memory profiler List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Hi! Thanks for the patch! 2 nits below, LGTM after the fix. Sergos > On 25 Dec 2020, at 18:26, Sergey Kaplun wrote: >=20 > This patch introduces Lua API for LuaJIT memory profiler implemented = in > the scope of the previous patch. >=20 > Profiler returns some true value if started/stopped successfully, > returns nil on failure (plus an error message as a second result and a > system-dependent error code as a third result). > If LuaJIT build without memory profiler both return `false`. ^^^^^^ was built >=20 > have adjusted with two new errors > PROF_ISRUNNING/PROF_NOTRUNNING returned in case when profiler has > started/stopped already correspondingly. >=20 > Part of tarantool/tarantool#5442 > --- >=20 > Changes in v2: > - Added pushing of errno for ERR_PROF* and ERRMEM > - Added forgotten assert. >=20 > src/Makefile.dep | 5 +- > src/lib_misc.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++ > src/lj_errmsg.h | 6 ++ > 3 files changed, 176 insertions(+), 2 deletions(-) >=20 > diff --git a/src/Makefile.dep b/src/Makefile.dep > index 8ae14a5..c3d0977 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 lj_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..36fe29f 100644 > --- a/src/lib_misc.c > +++ b/src/lib_misc.c > @@ -8,13 +8,21 @@ > #define lib_misc_c > #define LUA_LIB >=20 > +#include > +#include > + > #include "lua.h" > #include "lmisclib.h" > +#include "lauxlib.h" >=20 > #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 "lj_memprof.h" >=20 > /* = ------------------------------------------------------------------------ = */ >=20 > @@ -67,8 +75,167 @@ LJLIB_CF(misc_getmetrics) >=20 > #include "lj_libdef.h" >=20 > +/* ----- 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 =3D ((struct memprof_ctx *)opt)->stream; > + const void * const buf_start =3D *buf_addr; > + const void *data =3D *buf_addr; > + size_t write_total =3D 0; > + > + lua_assert(len <=3D STREAM_BUFFER_SIZE); > + > + for (;;) { > + const size_t written =3D fwrite(data, 1, len, stream); > + > + if (LJ_UNLIKELY(written =3D=3D 0)) { > + /* Re-tries write in case of EINTR. */ > + if (errno =3D=3D EINTR) { > + errno =3D 0; > + continue; > + } > + break; > + } > + > + write_total +=3D written; > + > + if (write_total =3D=3D len) > + break; > + > + data =3D (uint8_t *)data + (ptrdiff_t)written; After incomplete write you=E2=80=99ll return to the fwrite() call with data pointer moved, but with len untouched -> you=E2=80=99ll read beyond the buffer. > + } > + lua_assert(write_total <=3D len); > + > + *buf_addr =3D 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 =3D opt; > + FILE *stream =3D ctx->stream; > + memprof_ctx_free(ctx, buf); > + return fclose(stream); > +} > + > +/* local started, err, errno =3D misc.memprof.start(fname) */ > +LJLIB_CF(misc_memprof_start) > +{ > + struct lua_Prof_options opt =3D {0}; > + struct memprof_ctx *ctx; > + const char *fname; > + int memprof_status; > + int started; > + > + fname =3D strdata(lj_lib_checkstr(L, 1)); > + > + ctx =3D lj_mem_new(L, sizeof(*ctx)); > + if (ctx =3D=3D NULL) > + goto errmem; > + > + opt.ctx =3D ctx; > + opt.writer =3D buffer_writer_default; > + opt.on_stop =3D on_stop_cb_default; > + opt.len =3D STREAM_BUFFER_SIZE; > + opt.buf =3D (uint8_t *)lj_mem_new(L, STREAM_BUFFER_SIZE); > + if (NULL =3D=3D opt.buf) { > + lj_mem_free(G(L), ctx, sizeof(*ctx)); > + goto errmem; > + } > + > + ctx->g =3D G(L); > + ctx->stream =3D fopen(fname, "wb"); > + > + if (ctx->stream =3D=3D NULL) { > + memprof_ctx_free(ctx, opt.buf); > + return luaL_fileresult(L, 0, fname); > + } > + > + memprof_status =3D lj_memprof_start(L, &opt); > + started =3D memprof_status =3D=3D PROFILE_SUCCESS; > + > + if (LJ_UNLIKELY(!started)) { > + fclose(ctx->stream); > + remove(fname); > + memprof_ctx_free(ctx, opt.buf); > + switch (memprof_status) { > + case PROFILE_ERRRUN: > + lua_pushnil(L); > + lua_pushstring(L, err2msg(LJ_ERR_PROF_ISRUNNING)); > + lua_pushinteger(L, EINVAL); > + return 3; > + case PROFILE_ERRIO: > + return luaL_fileresult(L, 0, fname); > + default: > + lua_assert(0); > + break; > + } > + } > + lua_pushboolean(L, started); > + > + return 1; > +errmem: > + lua_pushnil(L); > + lua_pushstring(L, err2msg(LJ_ERR_ERRMEM)); > + lua_pushinteger(L, ENOMEM); > + return 3; > +} > + > +/* local stopped, err, errno =3D misc.memprof.stop() */ > +LJLIB_CF(misc_memprof_stop) > +{ > + int status =3D lj_memprof_stop(); > + int stopped_successfully =3D status =3D=3D PROFILE_SUCCESS; > + if (!stopped_successfully) { > + switch (status) { > + case PROFILE_ERRRUN: > + lua_pushnil(L); > + lua_pushstring(L, err2msg(LJ_ERR_PROF_NOTRUNNING)); > + lua_pushinteger(L, EINVAL); > + return 3; > + case PROFILE_ERRIO: > + return luaL_fileresult(L, 0, NULL); > + default: > + lua_assert(0); > + 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 >=20 > +#if LJ_HASPROFILE || LJ_HASMEMPROF > +/* Profiler errors. */ > +ERRDEF(PROF_ISRUNNING, "profiler is running already") > +ERRDEF(PROF_NOTRUNNING, "profiler is not running") > +#endif > + > #undef ERRDEF >=20 > /* Detecting unused error messages: > --=20 > 2.28.0 >=20