From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp46.i.mail.ru (smtp46.i.mail.ru [94.100.177.106]) (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 3BBA24765E3 for ; Sun, 27 Dec 2020 16:27:45 +0300 (MSK) From: Sergey Ostanevich Message-Id: <799CAEFC-C0C2-49B2-A695-0E2AE4AA55AD@tarantool.org> Content-Type: multipart/alternative; boundary="Apple-Mail=_D4F3ACBC-C47F-417E-86FD-E19A3CC1A84F" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Date: Sun, 27 Dec 2020 16:27:43 +0300 In-Reply-To: <20201227115405.GA14702@root> References: <036ac4034b714d9c3338246fd1bb3b373ef94b64.1608907726.git.skaplun@tarantool.org> <308C8690-5434-4E09-8BA1-91A3F39F1318@tarantool.org> <20201227115405.GA14702@root> Subject: Re: [Tarantool-patches] [PATCH luajit v2 5/7] core: introduce 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 --Apple-Mail=_D4F3ACBC-C47F-417E-86FD-E19A3CC1A84F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii LGTM Sergos > On 27 Dec 2020, at 14:54, Sergey Kaplun wrote: >=20 > Hi, Sergos! >=20 > Thanks for the review! >=20 > On 27.12.20, Sergey Ostanevich wrote: >> Hi! >>=20 >> Thanks for the patch! Just one question below, looks good otherwise. >>=20 >> Sergos >>=20 >>> diff --git a/src/lj_memprof.c b/src/lj_memprof.c >>> new file mode 100644 >>> index 0000000..e0df057 >>> --- /dev/null >>> +++ b/src/lj_memprof.c >> >>> +static int memprof_stop(const struct lua_State *L) >>> +{ >>> + struct memprof *mp =3D &memprof; >>> + struct alloc *oalloc =3D &mp->orig_alloc; >>> + struct lj_wbuf *out =3D &mp->out; >>> + int return_status =3D PROFILE_SUCCESS; >>> + int saved_errno =3D 0; >>> + struct lua_State *main_L; >>> + int cb_status; >>> + >>> + memprof_lock(); >>> + >>> + if (mp->state =3D=3D MPS_HALT) { >>> + errno =3D mp->saved_errno; >>> + mp->state =3D MPS_IDLE >>> + memprof_unlock(); >>> + return PROFILE_ERRIO; >>> + } >>> + >>> + if (mp->state !=3D MPS_PROFILE) { >>> + memprof_unlock(); >>> + return PROFILE_ERRRUN; >>> + } >>> + >>=20 >>> + if (L !=3D NULL && mp->g !=3D G(L)) { >>> + memprof_unlock(); >>> + return PROFILE_ERR; >>> + } >>> + >>> + mp->state =3D MPS_IDLE; >>> + >>> + lua_assert(mp->g !=3D NULL); >>> + main_L =3D mainthread(mp->g); >>> + >>> + lua_assert(memprof_allocf =3D=3D lua_getallocf(main_L, NULL)); >>> + lua_assert(oalloc->allocf !=3D NULL); >>> + lua_assert(oalloc->state !=3D NULL); >>> + lua_setallocf(main_L, oalloc->allocf, oalloc->state); >>> + >>> + if (LJ_UNLIKELY(lj_wbuf_test_flag(out, STREAM_STOP))) { >>> + lua_assert(lj_wbuf_test_flag(out, STREAM_ERR_IO)); >>> + mp->state =3D MPS_HALT; >>> + /* on_stop call may change errno value. */ >>> + mp->saved_errno =3D lj_wbuf_errno(out); >>> + /* Ignore possible errors. mp->opt.buf =3D=3D NULL here. */ >>> + mp->opt.on_stop(mp->opt.ctx, mp->opt.buf); >>> + lj_wbuf_terminate(out); >>> + memprof_unlock(); >>> + return PROFILE_ERRIO; >>> + } >>> + lj_wbuf_addbyte(out, LJM_EPILOGUE_HEADER); >>> + >>> + lj_wbuf_flush(out); >>> + >>> + cb_status =3D mp->opt.on_stop(mp->opt.ctx, mp->opt.buf); >>> + if (LJ_UNLIKELY(lj_wbuf_test_flag(out, STREAM_ERR_IO) || = cb_status !=3D 0)) { >>> + saved_errno =3D lj_wbuf_errno(out); >>=20 >> Previous return of PROFILE_ERRIO causes MPS_HALT. Should it be the = same? >=20 > Yes, you're right! Previous MPS_HALT is redundant -- profiler anyway > should stop (IDLE) immediately, there is no reason to set HALT there. > Thanks! Fixed! >=20 > See the iterative diff below. Branch is force-pushed. >=20 >>=20 >>> + return_status =3D PROFILE_ERRIO; >>> + } >>> + >>> + lj_wbuf_terminate(out); >>> + >>> + memprof_unlock(); >>> + errno =3D saved_errno; >>> + return return_status; >>> +} >>=20 >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > diff --git a/src/lj_memprof.c b/src/lj_memprof.c > index e0df057..998cbea 100644 > --- a/src/lj_memprof.c > +++ b/src/lj_memprof.c > @@ -354,13 +354,13 @@ static int memprof_stop(const struct lua_State = *L) >=20 > if (LJ_UNLIKELY(lj_wbuf_test_flag(out, STREAM_STOP))) { > lua_assert(lj_wbuf_test_flag(out, STREAM_ERR_IO)); > - mp->state =3D MPS_HALT; > /* on_stop call may change errno value. */ > - mp->saved_errno =3D lj_wbuf_errno(out); > + saved_errno =3D lj_wbuf_errno(out); > /* Ignore possible errors. mp->opt.buf =3D=3D NULL here. */ > mp->opt.on_stop(mp->opt.ctx, mp->opt.buf); > lj_wbuf_terminate(out); > memprof_unlock(); > + errno =3D saved_errno; > return PROFILE_ERRIO; > } > lj_wbuf_addbyte(out, LJM_EPILOGUE_HEADER); > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > --=20 > Best regards, > Sergey Kaplun --Apple-Mail=_D4F3ACBC-C47F-417E-86FD-E19A3CC1A84F Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii LGTM

Sergos

On 27 Dec 2020, at 14:54, = Sergey Kaplun <skaplun@tarantool.org> wrote:

Hi, = Sergos!

Thanks for = the review!

On 27.12.20, = Sergey Ostanevich wrote:
Hi!

Thanks= for the patch! Just one question below, looks good otherwise.

Sergos

diff --git a/src/lj_memprof.c = b/src/lj_memprof.c
new file mode 100644
index = 0000000..e0df057
--- /dev/null
+++ = b/src/lj_memprof.c
<snipped>
+static int = memprof_stop(const struct lua_State *L)
+{
+ =  struct memprof *mp =3D &memprof;
+  struct = alloc *oalloc =3D &mp->orig_alloc;
+  struct = lj_wbuf *out =3D &mp->out;
+  int = return_status =3D PROFILE_SUCCESS;
+  int saved_errno = =3D 0;
+  struct lua_State *main_L;
+ =  int cb_status;
+
+ =  memprof_lock();
+
+  if = (mp->state =3D=3D MPS_HALT) {
+    errno = =3D mp->saved_errno;
+    mp->state =3D= MPS_IDLE
+    memprof_unlock();
+    return PROFILE_ERRIO;
+ =  }
+
+  if (mp->state !=3D = MPS_PROFILE) {
+    memprof_unlock();
+    return PROFILE_ERRRUN;
+ =  }
+

+  if (L !=3D NULL = && mp->g !=3D G(L)) {
+ =    memprof_unlock();
+ =    return PROFILE_ERR;
+  }
+
+  mp->state =3D MPS_IDLE;
+
+  lua_assert(mp->g !=3D NULL);
+  main_L =3D mainthread(mp->g);
+
+  lua_assert(memprof_allocf =3D=3D = lua_getallocf(main_L, NULL));
+ =  lua_assert(oalloc->allocf !=3D NULL);
+ =  lua_assert(oalloc->state !=3D NULL);
+ =  lua_setallocf(main_L, oalloc->allocf, oalloc->state);
+
+  if = (LJ_UNLIKELY(lj_wbuf_test_flag(out, STREAM_STOP))) {
+ =    lua_assert(lj_wbuf_test_flag(out, STREAM_ERR_IO));
+    mp->state =3D MPS_HALT;
+ =    /* on_stop call may change errno value. */
+    mp->saved_errno =3D = lj_wbuf_errno(out);
+    /* Ignore possible = errors. mp->opt.buf =3D=3D NULL here. */
+ =    mp->opt.on_stop(mp->opt.ctx, mp->opt.buf);
+    lj_wbuf_terminate(out);
+ =    memprof_unlock();
+ =    return PROFILE_ERRIO;
+  }
+  lj_wbuf_addbyte(out, LJM_EPILOGUE_HEADER);
+
+  lj_wbuf_flush(out);
+
+  cb_status =3D mp->opt.on_stop(mp->opt.ctx, = mp->opt.buf);
+  if = (LJ_UNLIKELY(lj_wbuf_test_flag(out, STREAM_ERR_IO) || cb_status !=3D 0)) = {
+    saved_errno =3D = lj_wbuf_errno(out);

Previous = return of PROFILE_ERRIO causes MPS_HALT. Should it be the same?

Yes, you're right! Previous MPS_HALT is redundant -- profiler = anyway
should stop = (IDLE) immediately, there is no reason to set HALT there.
Thanks! = Fixed!

See the = iterative diff below. Branch is force-pushed.


+ =    return_status =3D PROFILE_ERRIO;
+ =  }
+
+  lj_wbuf_terminate(out);
+
+  memprof_unlock();
+ =  errno =3D saved_errno;
+  return = return_status;
+}


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
diff --git = a/src/lj_memprof.c b/src/lj_memprof.c
index e0df057..998cbea 100644
--- a/src/lj_memprof.c
+++ b/src/lj_memprof.c
@@ -354,13 +354,13 @@ static int memprof_stop(const struct = lua_State *L)

  if = (LJ_UNLIKELY(lj_wbuf_test_flag(out, STREAM_STOP))) {
    lua_assert(lj_wbuf_test_flag(out, = STREAM_ERR_IO));
-    mp->state =3D MPS_HALT;
    /* on_stop call may change errno = value. */
- =    mp->saved_errno =3D lj_wbuf_errno(out);
+ =    saved_errno =3D lj_wbuf_errno(out);
    /* Ignore possible errors. = mp->opt.buf =3D=3D NULL here. */
    mp->opt.on_stop(mp->opt.ctx, = mp->opt.buf);
    lj_wbuf_terminate(out);
    memprof_unlock();
+ =    errno =3D saved_errno;
    return PROFILE_ERRIO;
  }
  lj_wbuf_addbyte(out, = LJM_EPILOGUE_HEADER);
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

-- 
Best = regards,
Sergey = Kaplun

= --Apple-Mail=_D4F3ACBC-C47F-417E-86FD-E19A3CC1A84F--