qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Jan Kiszka <jan.kiszka@web.de>
Cc: Anthony Liguori <aliguori@us.ibm.com>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 2/2] [RFC] time: refactor QEMU timer to use GHRTimer
Date: Tue, 23 Aug 2011 10:12:05 +0200	[thread overview]
Message-ID: <4E5360D5.5050207@redhat.com> (raw)
In-Reply-To: <4E52BBF2.3040102@web.de>

On 08/22/2011 10:28 PM, Jan Kiszka wrote:
>   - QEMU_CLOCK_VIRTUAL: Without -icount, same as above, but stops when
>     the guest is stopped. The offset to compensate for stopped
>     times is based on TSC, not sure why. With -icount, things get more
>     complicated, Paolo had some nice explanations for the details.

The TSC is actually out of the picture.  However, it is easy to get 
confused, because the same code handles stopping both the vm_clock and 
the TSC when the guest is paused.  cpu_get_ticks handles the TSC, 
cpu_get_clock handles the clock:

Removing some "uninteresting" details, you have:

/* return the host CPU cycle counter and handle stop/restart */
int64_t cpu_get_ticks(void)
{
     if (!vm_running) {
         return timers_state.cpu_ticks_offset;
     } else {
         int64_t ticks;
         ticks = cpu_get_real_ticks();
         return ticks + timers_state.cpu_ticks_offset;
     }
}

/* return the host CPU monotonic timer and handle stop/restart */
static int64_t cpu_get_clock(void)
{
     int64_t ti;
     if (!vm_running) {
         return timers_state.cpu_clock_offset;
     } else {
         ti = get_clock();
         return ti + timers_state.cpu_clock_offset;
     }
}

which use the same algorithm but with different base clocks (TSC vs. 
CLOCK_MONOTONIC).

With -icount, things get indeed more complicated.  I'll cover only the 
iothread case since all my attempts at understanding the non-iothread 
case failed. :)  The "-icount N" vm_clock has nanosecond resolution just 
like the normal vm_clock, but those are not real nanoseconds.  While the 
CPU is running, each instruction increments the vm_clock by 2^N 
nanoseconds (yes, this is completely bollocks for SMP. :).  When the CPU 
is not running, instead, the vm_clock follows CLOCK_MONOTONIC; the 
rationale is there in qemu-timer.c.

"-icount auto" is the same, except that we try to keep vm_clock roughly 
the same as CLOCK_MONOTONIC by oscillating the clock frequency between 
"-icount N" values that are more representative of the actual execution 
frequency.

On top of this, the VM has to do two things in icount mode. The first is 
to stop execution at the next vm_clock deadline, which means breaking 
translation blocks after executing the appropriate number of 
instructions; this is quite obvious.  The second is to stop execution of 
a TB after any MMIO instruction, in order to recalculate deadlines if 
necessary.  The latter is responsible for most of the icount black magic 
spread all over the tree.  However, it's not that bad: long term, it 
sounds at least plausible to reuse this machinery to run the CPU threads 
outside the iothread lock (and only take it when doing MMIO, just like 
KVM does).

Paolo

  parent reply	other threads:[~2011-08-23  8:53 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-22 19:21 [Qemu-devel] [PATCH 1/2] main: add high resolution GSource based timer Anthony Liguori
2011-08-22 19:21 ` [Qemu-devel] [PATCH 2/2] [RFC] time: refactor QEMU timer to use GHRTimer Anthony Liguori
2011-08-22 20:28   ` Jan Kiszka
2011-08-22 20:36     ` Anthony Liguori
2011-08-22 20:49       ` Jan Kiszka
2011-08-22 21:55         ` Anthony Liguori
2011-08-22 23:48           ` Jan Kiszka
2011-08-23  8:12     ` Paolo Bonzini [this message]
2011-08-23  9:07       ` Edgar E. Iglesias
2011-08-23  7:43   ` Paolo Bonzini
2011-08-23 12:33     ` Anthony Liguori
2011-08-23 12:44       ` Paolo Bonzini
2011-08-22 19:26 ` [Qemu-devel] [PATCH 1/2] main: add high resolution GSource based timer Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E5360D5.5050207@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=aliguori@us.ibm.com \
    --cc=jan.kiszka@web.de \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).