diff mbox

[03/12] qemu-timer: more clock functions

Message ID 1317141111-27701-4-git-send-email-pbonzini@redhat.com
State New
Headers show

Commit Message

Paolo Bonzini Sept. 27, 2011, 4:31 p.m. UTC
These will be used when moving icount accounting to cpus.c.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qemu-timer.c |   25 +++++++++++++++++++++++++
 qemu-timer.h |    3 +++
 2 files changed, 28 insertions(+), 0 deletions(-)

Comments

Lluís Vilanova Sept. 30, 2011, 10:52 a.m. UTC | #1
Paolo Bonzini writes:

> These will be used when moving icount accounting to cpus.c.

I have something related to this kind of refactoring. While trying to understand
all the timing facilities in QEMU, I wrote some (unfinished) patches that try to
disentangle much of the code in qemu-timer into two new files:

- qemu-htime: Provides routines related to time in the host.
- qemu-vtime: Provides routines related to time in the guest.

These patches also try to sanitize some routine names by making their domain and
units explicit (e.g., get_clock becomes qemu_htime_nsec and cpu_get_ticks
becomes qemu_vtime_tsc). The patches also make the frequency explicit (adds
qemu_htime_freq and qemu_vtime_freq).

The result should provide code that makes it very clear on which time domain and
frequency the code is operating on.

The routines in qemu-timer should be already moved into the corresponding
qemu-*time files (I no longer remember on which state I left it), but the
routine name sanitizing is not finished, mostly because I still had to clear out
some details about how the current deadline calculation works.

Does this kind of refactoring sound interesting?


Thanks,
   Lluis
Paolo Bonzini Sept. 30, 2011, 11:03 a.m. UTC | #2
On 09/30/2011 12:52 PM, Lluís Vilanova wrote:
> Paolo Bonzini writes:
>
>> These will be used when moving icount accounting to cpus.c.
>
> I have something related to this kind of refactoring. While trying to understand
> all the timing facilities in QEMU, I wrote some (unfinished) patches that try to
> disentangle much of the code in qemu-timer into two new files:
>
> - qemu-htime: Provides routines related to time in the host.
> - qemu-vtime: Provides routines related to time in the guest.
>
> These patches also try to sanitize some routine names by making their domain and
> units explicit (e.g., get_clock becomes qemu_htime_nsec and cpu_get_ticks
> becomes qemu_vtime_tsc).

That's almost unnecessary, get_clock should be just 
qemu_get_clock_ns(host_clock) or something like that.  The problem is 
that the clock names are impossible to remember. :)

However, making it clear from the name whether get_clock refers to the 
host_clock or rt_clock can be a useful cleanup.

> The patches also make the frequency explicit (adds
> qemu_htime_freq and qemu_vtime_freq).

The frequency of the clocks is now always explicit in the function names 
(using _ns or _ms).

> The routines in qemu-timer should be already moved into the corresponding
> qemu-*time files (I no longer remember on which state I left it), but the
> routine name sanitizing is not finished, mostly because I still had to clear out
> some details about how the current deadline calculation works.

I think after my series there is already a similar split, with 
qemu-timer.c doing everything except the guest clock, and cpus.c taking 
care of the guest clock.

I have a few more cleanups pending in this area, but I'll submit them 
probably after 1.0 since I have enough stuff on my plate already.

> Does this kind of refactoring sound interesting?

Renaming the host_clock and rt_clock to something more easily remembered 
is trivial, but still useful.

It is also useful to make a dummy vmclock (with primitives like "get", 
"set", "advance to the next deadline") available outside qemu proper for 
use in unit tests.  I'm not sure how close we are to unit testing of 
device models, but anything that makes us closer is worthwhile.

Paolo
Lluís Vilanova Sept. 30, 2011, 12:15 p.m. UTC | #3
Paolo Bonzini writes:

> On 09/30/2011 12:52 PM, Lluís Vilanova wrote:
>> Paolo Bonzini writes:
>> 
>>> These will be used when moving icount accounting to cpus.c.
>> 
>> I have something related to this kind of refactoring. While trying to understand
>> all the timing facilities in QEMU, I wrote some (unfinished) patches that try to
>> disentangle much of the code in qemu-timer into two new files:
>> 
>> - qemu-htime: Provides routines related to time in the host.
>> - qemu-vtime: Provides routines related to time in the guest.
>> 
>> These patches also try to sanitize some routine names by making their domain and
>> units explicit (e.g., get_clock becomes qemu_htime_nsec and cpu_get_ticks
>> becomes qemu_vtime_tsc).

> That's almost unnecessary, get_clock should be just
> qemu_get_clock_ns(host_clock) or something like that.  The problem is that the
> clock names are impossible to remember. :)

Yes, that was the main point of my refactoring :)


> However, making it clear from the name whether get_clock refers to the
> host_clock or rt_clock can be a useful cleanup.

Well, then maybe I will resume that work after 1.0.


Lluis
diff mbox

Patch

diff --git a/qemu-timer.c b/qemu-timer.c
index e2551f3..ebb5089 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -495,6 +495,31 @@  void qemu_clock_warp(QEMUClock *clock)
     }
 }
 
+int64_t qemu_clock_has_timers(QEMUClock *clock)
+{
+    return !!clock->active_timers;
+}
+
+int64_t qemu_clock_expired(QEMUClock *clock)
+{
+    return (clock->active_timers &&
+            clock->active_timers->expire_time < qemu_get_clock_ns(clock));
+}
+
+int64_t qemu_clock_deadline(QEMUClock *clock)
+{
+    /* To avoid problems with overflow limit this to 2^32.  */
+    int64_t delta = INT32_MAX;
+
+    if (clock->active_timers) {
+        delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
+    }
+    if (delta < 0) {
+        delta = 0;
+    }
+    return delta;
+}
+
 QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
                           QEMUTimerCB *cb, void *opaque)
 {
diff --git a/qemu-timer.h b/qemu-timer.h
index 0a43469..4578075 100644
--- a/qemu-timer.h
+++ b/qemu-timer.h
@@ -38,6 +38,9 @@  extern QEMUClock *vm_clock;
 extern QEMUClock *host_clock;
 
 int64_t qemu_get_clock_ns(QEMUClock *clock);
+int64_t qemu_clock_has_timers(QEMUClock *clock);
+int64_t qemu_clock_expired(QEMUClock *clock);
+int64_t qemu_clock_deadline(QEMUClock *clock);
 void qemu_clock_enable(QEMUClock *clock, int enabled);
 void qemu_clock_warp(QEMUClock *clock);