From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 7902D6ECE3; Mon, 27 Jun 2022 19:22:22 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7902D6ECE3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1656346942; bh=PZzrugTa66mRZQsAKmy8xSYcbfiJ0JCSQZMmYLhA1cg=; h=In-Reply-To:Date:References:To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=LwwJJonJNg9CUV9jOOJ8+nzbojNNb9tdx57DDUeCsUw/NeGkanafvY+QQCRetMZU6 d+KEXZ/JlaW4nYYdOUKT3OKNknTST+I0rpcKsC3be351tBdGY0AEzJuzJ8cdyKgZoj UtFoSiT7vnp0me/Ds07SsN6RBaPc1OVpSVm/F3Ss= Received: from smtp55.i.mail.ru (smtp55.i.mail.ru [217.69.128.35]) (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 020F56ECE3 for ; Mon, 27 Jun 2022 19:22:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 020F56ECE3 Received: by smtp55.i.mail.ru with esmtpa (envelope-from ) id 1o5rVF-0001UV-Df; Mon, 27 Jun 2022 19:22:21 +0300 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.100.31\)) In-Reply-To: <20220606133528.13355-1-skaplun@tarantool.org> Date: Mon, 27 Jun 2022 19:22:20 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: References: <20220606133528.13355-1-skaplun@tarantool.org> To: Sergey Kaplun X-Mailer: Apple Mail (2.3696.100.31) X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD921FF253C2DCA64328D82274A16F3B1206881FAF42C11B61F00894C459B0CD1B9381287BD830C09F6E8AB70439490F6FBD1E836DB9696AB707CBF1C18CED4D92F X-8FC586DF: 6EFBBC1D9D64D975 X-C1DE0DAB: 0D63561A33F958A59F4E491809E24951DE4B7384766298D933184A3A93E1CC52D59269BC5F550898D99A6476B3ADF6B4886A5961035A09600383DAD389E261318FB05168BE4CE3AF X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34C974B02B4EA30DFBB2F6D6E61CD62CDEF66BB1364F4259CC0703DD02A75B322D1EE9D263638C0FA61D7E09C32AA3244CF75480B941D46BA4D7FA2BF1E163B1661DD47778AE04E04DFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj8L65zF8kk4ciEo5NbQpNOQ== X-Mailru-Sender: 11C2EC085EDE56FA38FD4C59F7EFE407A55767CE746464EC5E1D99CA0E7395642DC8CD00009A478A19381EE24192DF5555834048F03EF5D4C9A814A92B2E3B1BA4250FC3964EA4964198E0F3ECE9B5443453F38A29522196 X-Mras: OK Subject: Re: [Tarantool-patches] [PATCH luajit] Fix interaction between profiler hooks and finalizers. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: sergos via Tarantool-patches Reply-To: sergos Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi! Thanks for the patch! LGTM with minor updates to the message. Sergos > On 6 Jun 2022, at 16:35, Sergey Kaplun wrote: >=20 > From: Mike Pall >=20 > Thanks to Julien Desgats. >=20 > (cherry picked from commit 92fa45f9eb5d9aa732c6b972a85c26120d7612b4) >=20 > Embedded LuaJIT profiler uses the following approach: > * Create trigger on timer signals, which set `HOOK_PROFILE` flag on > `g->hookmask` (if not set already) and update all BC instructions > dispatch. ??? plural, perhaps dispatch places? > * When any of instruction is executed, callback set by user on = profiler a > initialize is called. After the dispatch table updated again to > the old static values, and flags are reset. > * `HOOK_PROFILE` flag will set again and dispatch will updated at the = next > tick of the timer. >=20 > Before call to __gc finalizer the old hookmask is saved to be restored a for the future = use. > later. When the second step is happening on call of __gc metamethod a a > `HOOK_PROFILE` flag is reset. But right after that the old hook the > disposition (with `HOOK_PROFILE`) is restored, when we leaving > `gc_call_finalizer()` without updating the dispatch table, so the > profiler callback is never called again. >=20 > This patch updates dispatch table right after hooks manipulation in > `gc_call_finalizer()` and prevent ticks influence inside finalizer > calls. >=20 > Sergey Kaplun: > * added the description and the test for the problem >=20 > Part of tarantool/tarantool#6548 > --- >=20 > Side note: PR has 2 extra commits because the tarantool/luajit master > (tarantool branch) isn't bumped in the tarantool yet. >=20 > Branch: = https://github.com/tarantool/luajit/tree/skaplun/lj-512-profiler-hook-fina= lizers-full-ci > Tarantool PR: https://github.com/tarantool/tarantool/pull/7237 > Related issues: > * https://github.com/LuaJIT/LuaJIT/issues/512 > * https://github.com/tarantool/tarantool/issues/6548 >=20 >=20 > src/lj_gc.c | 2 ++ > src/lj_obj.h | 3 +- > src/lj_profile.c | 2 +- > .../lj-512-profiler-hook-finalizers.test.lua | 31 +++++++++++++++++++ > 4 files changed, 36 insertions(+), 2 deletions(-) > create mode 100644 = test/tarantool-tests/lj-512-profiler-hook-finalizers.test.lua >=20 > diff --git a/src/lj_gc.c b/src/lj_gc.c > index 7f0ec896..29387a48 100644 > --- a/src/lj_gc.c > +++ b/src/lj_gc.c > @@ -493,6 +493,7 @@ static void gc_call_finalizer(global_State *g, = lua_State *L, > TValue *top; > lj_trace_abort(g); > hook_entergc(g); /* Disable hooks and new traces during __gc. */ > + if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g); > g->gc.threshold =3D LJ_MAX_MEM; /* Prevent GC steps. */ > top =3D L->top; > copyTV(L, top++, mo); > @@ -501,6 +502,7 @@ static void gc_call_finalizer(global_State *g, = lua_State *L, > L->top =3D top+1; > errcode =3D lj_vm_pcall(L, top, 1+0, -1); /* Stack: |mo|o| -> | */ > hook_restore(g, oldh); > + if (LJ_HASPROFILE && (oldh & HOOK_PROFILE)) lj_dispatch_update(g); > g->gc.threshold =3D oldt; /* Restore GC threshold. */ > if (errcode) > lj_err_throw(L, errcode); /* Propagate errors. */ > diff --git a/src/lj_obj.h b/src/lj_obj.h > index 197d735a..d1451c3a 100644 > --- a/src/lj_obj.h > +++ b/src/lj_obj.h > @@ -692,7 +692,8 @@ typedef struct global_State { > #define HOOK_PROFILE 0x80 > #define hook_active(g) ((g)->hookmask & HOOK_ACTIVE) > #define hook_enter(g) ((g)->hookmask |=3D HOOK_ACTIVE) > -#define hook_entergc(g) ((g)->hookmask |=3D = (HOOK_ACTIVE|HOOK_GC)) > +#define hook_entergc(g) \ > + ((g)->hookmask =3D ((g)->hookmask | (HOOK_ACTIVE|HOOK_GC)) & = ~HOOK_PROFILE) > #define hook_vmevent(g) ((g)->hookmask |=3D = (HOOK_ACTIVE|HOOK_VMEVENT)) > #define hook_leave(g) ((g)->hookmask &=3D ~HOOK_ACTIVE) > #define hook_save(g) ((g)->hookmask & ~HOOK_EVENTMASK) > diff --git a/src/lj_profile.c b/src/lj_profile.c > index 4412d68b..896d8f46 100644 > --- a/src/lj_profile.c > +++ b/src/lj_profile.c > @@ -106,7 +106,7 @@ static void profile_trigger(ProfileState *ps) > profile_lock(ps); > ps->samples++; /* Always increment number of samples. */ > mask =3D g->hookmask; > - if (!(mask & (HOOK_PROFILE|HOOK_VMEVENT))) { /* Set profile hook. = */ > + if (!(mask & (HOOK_PROFILE|HOOK_VMEVENT|HOOK_GC))) { /* Set = profile hook. */ > int st =3D g->vmstate; > ps->vmstate =3D st >=3D 0 ? 'N' : > st =3D=3D ~LJ_VMST_INTERP ? 'I' : > diff --git = a/test/tarantool-tests/lj-512-profiler-hook-finalizers.test.lua = b/test/tarantool-tests/lj-512-profiler-hook-finalizers.test.lua > new file mode 100644 > index 00000000..4b6aabb7 > --- /dev/null > +++ b/test/tarantool-tests/lj-512-profiler-hook-finalizers.test.lua > @@ -0,0 +1,31 @@ > +local tap =3D require('tap') > +local profile =3D require('jit.profile') > + > +local test =3D tap.test('lj-512-profiler-hook-finalizers') > +test:plan(1) > + > +-- Sampling interval in ms. > +local INTERVAL =3D 10 > + > +local nsamples =3D 0 > +profile.start('li' .. tostring(INTERVAL), function() > + nsamples =3D nsamples + 1 > +end) > + > +local start =3D os.clock() > +for _ =3D 1, 1e6 do > + getmetatable(newproxy(true)).__gc =3D function() end > +end > +local finish =3D os.clock() > + > +profile.stop() > + > +-- XXX: The bug is occured as stopping of callbacks invocation, > +-- when a new tick strikes inside `gc_call_finalizer()`. > +-- The amount of successfull callbacks isn't stable (2-15). > +-- So, assume that amount of profiling samples should be at least > +-- more than 0.5 intervals of time during sampling. > +test:ok(nsamples >=3D 0.5 * (finish - start) * 1e3 / INTERVAL, > + 'profiler sampling') > + > +os.exit(test:check() and 0 or 1) > --=20 > 2.34.1 >=20