From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng2.m.smailru.net (smtpng2.m.smailru.net [94.100.179.3]) (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 6AE9D4765E3 for ; Sun, 27 Dec 2020 21:58:19 +0300 (MSK) Date: Sun, 27 Dec 2020 21:58:13 +0300 From: Igor Munkin Message-ID: <20201227185813.GL5396@tarantool.org> References: <6552b7249cd4f3e2782805f71de205700d5b531f.1608907726.git.skaplun@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <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 Sergey, Thanks for the patch! Please consider the comments below. On 25.12.20, Sergey Kaplun wrote: > This patch introduces Lua API for LuaJIT memory profiler implemented in > the scope of the previous patch. > > Profiler returns some true value if started/stopped successfully, There is no "some" true value there is a sole one. Please fix it in both the patch and the RFC. > 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`. > > 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 > --- > > Changes in v2: > - Added pushing of errno for ERR_PROF* and ERRMEM > - Added forgotten assert. > > src/Makefile.dep | 5 +- > src/lib_misc.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++ > src/lj_errmsg.h | 6 ++ > 3 files changed, 176 insertions(+), 2 deletions(-) > > 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 > @@ -67,8 +75,167 @@ LJLIB_CF(misc_getmetrics) > +static LJ_AINLINE void memprof_ctx_free(struct memprof_ctx *ctx, uint8_t *buf) > +{ > + lj_mem_free(ctx->g, buf, STREAM_BUFFER_SIZE); Side note: This is odd that you free the buffer here, but the buffer itself is not a part of the memprof context. Let's return to this later. > + 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) > +{ > + if (LJ_UNLIKELY(written == 0)) { > + /* Re-tries write in case of EINTR. */ > + if (errno == EINTR) { Minor: It's better to use early return here. Feel free to ignore. > + errno = 0; > + continue; > + } > + break; If other error occurs, you need to pass the NULL to buf_addr, right? Otherwise, there is no guarantee everything is written to the file and profiling proceeds. > + } > + > +/* Default on stop callback. Just close corresponding stream. */ Typo: s/close corresponding/close the corresponding/. > +static int on_stop_cb_default(void *opt, uint8_t *buf) > +/* local started, err, errno = misc.memprof.start(fname) */ > +LJLIB_CF(misc_memprof_start) > +{ > + fname = strdata(lj_lib_checkstr(L, 1)); Minor: You can make this assignment alongside with the declaration. > + > + ctx = lj_mem_new(L, sizeof(*ctx)); > + if (ctx == NULL) This is a dead code: raises a LUA_ERRMEM. > + goto errmem; > + > + if (NULL == opt.buf) { This is a dead code: raises a LUA_ERRMEM. > + lj_mem_free(G(L), ctx, sizeof(*ctx)); > + goto errmem; > + } > + memprof_status = lj_memprof_start(L, &opt); > + started = memprof_status == PROFILE_SUCCESS; Trust me, you don't need this variable. */me making Jedi mind tricks here* > + > + if (LJ_UNLIKELY(!started)) { > + fclose(ctx->stream); > + remove(fname); Minor: I doubt we need to remove a file even if LuaJIT failed to start profiling. Leave the comment if this makes sense. Feel free to ignore. > + memprof_ctx_free(ctx, opt.buf); > + switch (memprof_status) { > + } > + } > + lua_pushboolean(L, started); Please, s/started/1/ since there is no another value here. > + > + return 1; > +} > + > +/* local stopped, err, errno = misc.memprof.stop() */ > +LJLIB_CF(misc_memprof_stop) > +{ > + int status = lj_memprof_stop(); > + int stopped_successfully = status == PROFILE_SUCCESS; Trust me, you don't need this variable. */me making Jedi mind tricks here* > + if (!stopped_successfully) { > + lua_pushboolean(L, stopped_successfully); Please, s/stopped_succesfully/1/ since there is no another value here. > + 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") > +#if LJ_HASPROFILE || LJ_HASMEMPROF Why did you also initialize them for profiler? > +/* Profiler errors. */ > -- > 2.28.0 > -- Best regards, IM