Received: from eggs.gnu.org ([2001:4830:134:3::10]:33706) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZEaZc-0002Pu-Fb for qemu-devel@nongnu.org; Mon, 13 Jul 2015 06:02:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZEaZb-0006Zx-Dm for qemu-devel@nongnu.org; Mon, 13 Jul 2015 06:02:56 -0400 Date: Mon, 13 Jul 2015 11:02:42 +0100 From: Stefan Hajnoczi Message-ID: <20150713100242.GA17927@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> <20150708105822.GC25442@stefanha-thinkpad.redhat.com> <20150710004644.GD31230@ad.nay.redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="6c2NcOVqGQ03X4Wi" Content-Disposition: inline In-Reply-To: <20150710004644.GD31230@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 --6c2NcOVqGQ03X4Wi Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jul 10, 2015 at 08:46:44AM +0800, Fam Zheng wrote: > On Wed, 07/08 11:58, Stefan Hajnoczi wrote: > > 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= false; > > > > > + * if blocking =3D=3D true, it is only true after poll() ret= urns. > > > > > + * > > > > > + * If we're in a nested event loop, ctx->dispatching might b= e true. > > > > > + * 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 i= f the > > > > main loop uses aio_poll() in the future. > > >=20 > > > Because of a little complication: timeout here is always -1 for iothr= ead, and > > > what is interesting is that -1 actually requires an explicit > > >=20 > > > timerfd_settime(timerfd, flags, &(struct itimerspec){{0, 0}}, NUL= L) > > >=20 > > > to disable timerfd for this aio_poll(), which costs somethings. Passi= ng -1 to > > > epoll_wait() without this doesn't work because the timerfd is already= added 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 = about 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 seco= nd one has > > > -1. > >=20 > > 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. >=20 > Nested aio_poll() has no racing on timerfd because the outer aio_poll()'s > epoll_wait() would have already returned at the point of the inner aio_po= ll(). >=20 > Threads are different with Paolo's "release AioContext around blocking > aio_poll()". Ah, I see! > >=20 > > The optimization can be added later unless the timerfd_settime() syscall > > is so expensive that it defeats the advantage of epoll(). >=20 > That's the plan, and must be done before it get used by main loop. I'd rather we merge correct code than fast code which violates the API. Let's do nanosecond precision now, as advertised by the function names, and optimize timerfd later. --6c2NcOVqGQ03X4Wi Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJVo4zCAAoJEJykq7OBq3PI8gwH/23F7uXTo5Ple6d1ulF7Xll3 +vJPxsEPNM8WfseP0IfavoeS3xupIyFFs+RgDC8DU9MeweDMHd0sn5KyKkKEJoCM Kc0Ao07JQzcm+vI/pPKBr4len0r/pgXqBYH3otwL7RVZa+XCbBeQkxi7Mp4+2iW7 73d3IrzstcswybpX58VysshTa41v84EZZQChEpQw87cAZn6V7G0rRR857YoImrZS rTcbupax0czIohRJmsvCclfnoCcmg9616RPqu5AaYqEXu3KEARsnH/sb3kKNJrKv 5KdD6tqn6kTGc6q91u6IiaVuPUp+1SyayE+wLoUXa++arTDUQ+wGcXe0aRMrmro= =SbEq -----END PGP SIGNATURE----- --6c2NcOVqGQ03X4Wi--