Received: from eggs.gnu.org ([2001:4830:134:3::10]:52694) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZCn3d-00021E-Me for qemu-devel@nongnu.org; Wed, 08 Jul 2015 06:58:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZCn3c-0007f2-Re for qemu-devel@nongnu.org; Wed, 08 Jul 2015 06:58:29 -0400 Date: Wed, 8 Jul 2015 11:58:22 +0100 From: Stefan Hajnoczi Message-ID: <20150708105822.GC25442@stefanha-thinkpad.redhat.com> References: <1435670385-625-1-git-send-email-famz@redhat.com> <1435670385-625-5-git-send-email-famz@redhat.com> <20150707150850.GG28673@stefanha-thinkpad.redhat.com> <20150708010127.GB10382@ad.nay.redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="WplhKdTI2c8ulnbP" Content-Disposition: inline In-Reply-To: <20150708010127.GB10382@ad.nay.redhat.com> Subject: Re: [Qemu-devel] [PATCH RFC 4/4] aio-posix: Use epoll in aio_poll List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Fam Zheng Cc: Kevin Wolf , pbonzini@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org --WplhKdTI2c8ulnbP Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jul 08, 2015 at 09:01:27AM +0800, Fam Zheng wrote: > On Tue, 07/07 16:08, Stefan Hajnoczi wrote: > > > +#define EPOLL_BATCH 128 > > > +static bool aio_poll_epoll(AioContext *ctx, bool blocking) > > > +{ > > > + AioHandler *node; > > > + bool was_dispatching; > > > + int i, ret; > > > + bool progress; > > > + int64_t timeout; > > > + struct epoll_event events[EPOLL_BATCH]; > > > + > > > + aio_context_acquire(ctx); > > > + was_dispatching =3D ctx->dispatching; > > > + progress =3D false; > > > + > > > + /* aio_notify can avoid the expensive event_notifier_set if > > > + * everything (file descriptors, bottom halves, timers) will > > > + * be re-evaluated before the next blocking poll(). This is > > > + * already true when aio_poll is called with blocking =3D=3D fal= se; > > > + * if blocking =3D=3D true, it is only true after poll() returns. > > > + * > > > + * If we're in a nested event loop, ctx->dispatching might be tr= ue. > > > + * In that case we can restore it just before returning, but we > > > + * have to clear it now. > > > + */ > > > + aio_set_dispatching(ctx, !blocking); > > > + > > > + ctx->walking_handlers++; > > > + > > > + timeout =3D blocking ? aio_compute_timeout(ctx) : 0; > > > + > > > + if (timeout > 0) { > > > + timeout =3D DIV_ROUND_UP(timeout, 1000000); > > > + } > >=20 > > I think you already posted the timerfd code in an earlier series. Why > > degrade to millisecond precision? It needs to be fixed up anyway if the > > main loop uses aio_poll() in the future. >=20 > Because of a little complication: timeout here is always -1 for iothread,= and > what is interesting is that -1 actually requires an explicit >=20 > timerfd_settime(timerfd, flags, &(struct itimerspec){{0, 0}}, NULL) >=20 > to disable timerfd for this aio_poll(), which costs somethings. Passing -= 1 to > epoll_wait() without this doesn't work because the timerfd is already add= ed to > the epollfd and may have an unexpected timeout set before. >=20 > Of course we can cache the state and optimize, but I've not reasoned abou= t what > if another thread happens to call aio_poll() when we're in epoll_wait(), = for > example when the first aio_poll() has a positive timeout but the second o= ne has > -1. I'm not sure I understand the threads scenario since aio_poll_epoll() has a big aio_context_acquire()/release() region that protects it, but I guess the nested aio_poll() case is similar. Care needs to be taken so the extra timerfd state is consistent. The optimization can be added later unless the timerfd_settime() syscall is so expensive that it defeats the advantage of epoll(). --WplhKdTI2c8ulnbP Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJVnQJOAAoJEJykq7OBq3PIZeAIAKQ4yA2KXFFqqkSYdY0f9QA5 8Dh1t2aMyXKUgHxDTfJk4lGhz1uG4jgiZGBy8hj0dkKHY31s+UfMYqt+ti6lMHMc EREDSuwHEqiNymneu02RYOI2HkI036MJpS4KQ/y1juS+QmzAM7YWenQ3p6wmWod7 d1rgTwjLivTUbqYsYi3be9xIaZHSlvXp4prsX9JHQ/GHXiCVME5Ax0XmE41wc9QO 0iyYP3bdY7/8g2DNkKC8VJaJorbdRC7TT2DJzCcZ2NSOJ39TQo5Ud1xG8P7N633F y5LlhFUlCgYWh0peDHxW4sg8KHxLwJz9Lg9P7xtc5tBu/fYXh3MjiF3c4SsJrJA= =SW09 -----END PGP SIGNATURE----- --WplhKdTI2c8ulnbP--