From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41842) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bWm6n-0006cR-Fm for qemu-devel@nongnu.org; Mon, 08 Aug 2016 11:04:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bWm6k-0003xS-0B for qemu-devel@nongnu.org; Mon, 08 Aug 2016 11:04:53 -0400 Received: from mail-wm0-x233.google.com ([2a00:1450:400c:c09::233]:38240) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bWm6j-0003wz-Kx for qemu-devel@nongnu.org; Mon, 08 Aug 2016 11:04:49 -0400 Received: by mail-wm0-x233.google.com with SMTP id o80so145206419wme.1 for ; Mon, 08 Aug 2016 08:04:49 -0700 (PDT) Date: Mon, 8 Aug 2016 16:04:46 +0100 From: Stefan Hajnoczi Message-ID: <20160808150446.GB25553@stefanha-x1.localdomain> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="i0/AhcQY5QxfSsSZ" Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] Fwd: Understanding Qemu Block List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Gadre Nayan Cc: qemu-devel@nongnu.org --i0/AhcQY5QxfSsSZ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jul 27, 2016 at 05:49:48PM +0530, Gadre Nayan wrote: > I have recently started working on QEMU and it seems a daunting task > to say the least, hence I wanted some head-start. I have browsed the > source but I still need to bring closer some dots so I can start > working on some experimental changes. I will have to ask further > questions based on my new improved understanding, I think my questions > could be compiled into a single FAQ list :) >=20 > I wanted to understand AIOContext in Block, Coroutines in Block. How > are they related to IOThreads and vCPU threads. >=20 > Currently My understanding is as follows (kindly validate as well) >=20 > 1. There is 1 vCPU thread per CPU core that I specify in the command argu= ments. vcpu threads are also used for hyperthreads, not just cores. Basically each logical CPU that guest code runs on is a vcpu thread. > so eg: ./qemu-system-x86_64 -smp 4 > should create 4 vCPU threads ? Yes > These vCPU threads are going to run the Guest Code, They may also han= dle the > exit conditions and dispatch the MMIO request. >=20 > So, who handles the MMIO request and its Completion, is it the IOThre= ads ? IOThread is a thread that runs an epoll(2) event loop. That means it can process file descriptor activity and timers. Many hardware register accesses are handled in the vcpu thread - that vcpu is unable to execute guest code during this time. The ioeventfd mechanism is used by some virtio devices, however, to handle virtqueue notifies in the IOThread. This allows the vcpu can continue executing guest code while an IOThread performs device emulation. So the answer is that device emulation mostly happens in vcpu thread context but also in QEMU main loop or IOThread context. (The QEMU main loop is similar to IOThread but there is always one QEMU main loop thread. It doesn't use the IOThread code and isn't considered an IOThread, mainly for historical reasons.) > 2. Lets say the Guest Code has decided to write something on a disk file = on the > Host, the guest Should then dispatch the write operation and some > IOThread will > do the IO. How does the IO thread report back the completion of > this operation. Typically by raising an interrupt. The kvm.ko kernel module can inject the interrupt into a running vcpu. > And how do the AIOcontext and Coroutines play a role in this operatio= n. IOThread is the thread. AIOContext is the event loop code (epoll() loop, file descriptor monitoring, etc). Coroutines are just a convenience to avoid callback hell and making code easier to read, but they don't change the event-driven architecture of QEMU. > I know the use of co-routines but I would like to understand > through some code > snippets how they interact with the block layer. There are plently of examples in block.c and block/*. Basically the device emulation code calls blk_aio_preadv()/blk_aio_pwritev()/etc. Because the block layer code is complex and would involve many many callbacks, block.c executes the I/O request in a coroutine. This allows async I/O to appear like a regular blocking I/O operation (in preadv()/pwritev() style) without spaghetti code (aka callback hell). Stefan --i0/AhcQY5QxfSsSZ Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQEcBAEBAgAGBQJXqJ+OAAoJEJykq7OBq3PIS74H/Az5OVX7N7f3Dc3Bku62c4Qr 8PRq0LwlMtQ8rBenrCWJlO3yScBQkCLNmwAwabM8BN4lUyYESyXqpbHQL1K+M4WT C67bAj3WwFIMaxfOz/UtyYu6fSk7czievh8TzqdyhxBS6YVsOp2/f8r9kU8D1ZqT lpHcGHcfL3D13zaD7oMo6+eJZEqol2CeffUowtz8+S0Baos1WARJ825iW3VBPmyg gFl2yuFzO/saAKRvEUdxwmVygWI8kZaG6z5ZDNrG5N6yhWoYUXd2QDcZPHowoONJ 4jeBP+8iReBVuzyc/xMZsmUO43/YnMBMKP3m0zcPLofZ7+mdK62fjQxfCWqOFnI= =3G/6 -----END PGP SIGNATURE----- --i0/AhcQY5QxfSsSZ--