Skip to contentSkip to conversation

qemu_timer.c: add timer_deadline_ms() helper

PATCH Superseded Daniel Henrique Barboza 4 participants 4 messages Feb 25, 2021 – Mar 1, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu_timer.c: add timer_deadline_ms() helper

The pSeries machine is using QEMUTimer internals to return the timeout in seconds for a timer object, in hw/ppc/spapr.c, function spapr_drc_unplug_timeout_remaining_sec().

Create a helper in qemu-timer.c to retrieve the deadline for a QEMUTimer object, in ms, to avoid exposing timer internals to the PPC code.

---

This patch is applicable on top of David's ppc-for-6.0.

 hw/ppc/spapr_drc.c   |  5 ++---
 include/qemu/timer.h |  8 ++++++++
 util/qemu-timer.c    | 13 +++++++++++++
 3 files changed, 23 insertions(+), 3 deletions(-)
hw/ppc/spapr_drc.c +2 -3
index 8c4997d795..98b626acf9 100644
@@ -421,9 +421,8 @@ void spapr_drc_unplug_request(SpaprDrc *drc)
 
 int spapr_drc_unplug_timeout_remaining_sec(SpaprDrc *drc)
 {
-    if (drc->unplug_requested && timer_pending(drc->unplug_timeout_timer)) {
-        return (qemu_timeout_ns_to_ms(drc->unplug_timeout_timer->expire_time) -
-                qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)) / 1000;
+    if (drc->unplug_requested) {
+        return timer_deadline_ms(drc->unplug_timeout_timer) / 1000;
     }
 
     return 0;
include/qemu/timer.h +8 -0
index 1678238384..5e76e3f8c2 100644
@@ -795,6 +795,14 @@ static inline int64_t get_max_clock_jump(void)
     return 60 * NANOSECONDS_PER_SECOND;
 }
 
+/**
+ * timer_deadline_ms:
+ *
+ * Returns the remaining miliseconds for @timer to expire, or zero
+ * if the timer is no longer pending.
+ */
+int64_t timer_deadline_ms(QEMUTimer *timer);
+
 /*
  * Low level clock functions
  */
util/qemu-timer.c +13 -0
index 81c28af517..3a03a63daa 100644
@@ -243,6 +243,19 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
     return delta;
 }
 
+/*
+ * Returns the time remaining for the deadline, in ms.
+ */
+int64_t timer_deadline_ms(QEMUTimer *timer)
+{
+    if (timer_pending(timer)) {
+        return qemu_timeout_ns_to_ms(timer->expire_time) -
+               qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
+    }
+
+    return 0;
+}
+
 /* Calculate the soonest deadline across all timerlists attached
  * to the clock. This is used for the icount timeout so we
  * ignore whether or not the clock should be used in deadline
Signature · 1 line
2.29.2
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Daniel Henrique Barboza

The pSeries machine is using QEMUTimer internals to return the timeout in seconds for a timer object, in hw/ppc/spapr.c, function spapr_drc_unplug_timeout_remaining_sec().

Quoted 8 lines

Create a helper in qemu-timer.c to retrieve the deadline for a QEMUTimer object, in ms, to avoid exposing timer internals to the PPC code.

---

This patch is applicable on top of David's ppc-for-6.0.

Quoted 65 lines
  hw/ppc/spapr_drc.c   |  5 ++---
  include/qemu/timer.h |  8 ++++++++
  util/qemu-timer.c    | 13 +++++++++++++
  3 files changed, 23 insertions(+), 3 deletions(-)
hw/ppc/spapr_drc.c +2 -3
index 8c4997d795..98b626acf9 100644
@@ -421,9 +421,8 @@ void spapr_drc_unplug_request(SpaprDrc *drc)
 
  int spapr_drc_unplug_timeout_remaining_sec(SpaprDrc *drc)
  {
-    if (drc->unplug_requested && timer_pending(drc->unplug_timeout_timer)) {
-        return (qemu_timeout_ns_to_ms(drc->unplug_timeout_timer->expire_time) -
-                qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)) / 1000;
+    if (drc->unplug_requested) {
+        return timer_deadline_ms(drc->unplug_timeout_timer) / 1000;
      }
 
      return 0;
include/qemu/timer.h +8 -0
index 1678238384..5e76e3f8c2 100644
@@ -795,6 +795,14 @@ static inline int64_t get_max_clock_jump(void)
      return 60 * NANOSECONDS_PER_SECOND;
  }
 
+/**
+ * timer_deadline_ms:
+ *
+ * Returns the remaining miliseconds for @timer to expire, or zero
+ * if the timer is no longer pending.
+ */
+int64_t timer_deadline_ms(QEMUTimer *timer);
+
  /*
   * Low level clock functions
   */
util/qemu-timer.c +13 -0
index 81c28af517..3a03a63daa 100644
@@ -243,6 +243,19 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
      return delta;
  }
 
+/*
+ * Returns the time remaining for the deadline, in ms.
+ */
+int64_t timer_deadline_ms(QEMUTimer *timer)
+{
+    if (timer_pending(timer)) {
+        return qemu_timeout_ns_to_ms(timer->expire_time) -
+               qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
+    }
+
+    return 0;
+}
+
  /* Calculate the soonest deadline across all timerlists attached
   * to the clock. This is used for the icount timeout so we
   * ignore whether or not the clock should be used in deadline
#2 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Daniel Henrique Barboza

The pSeries machine is using QEMUTimer internals to return the timeout in seconds for a timer object, in hw/ppc/spapr.c, function spapr_drc_unplug_timeout_remaining_sec().

Create a helper in qemu-timer.c to retrieve the deadline for a QEMUTimer object, in ms, to avoid exposing timer internals to the PPC code.

Quoted 7 lines
Quoted 5 lines

---

This patch is applicable on top of David's ppc-for-6.0.

With Paolo's ack, I've taken this into the ppc-for-6.0 tree, hope that's ok.

Quoted 63 lines
Quoted 62 lines
  hw/ppc/spapr_drc.c   |  5 ++---
  include/qemu/timer.h |  8 ++++++++
  util/qemu-timer.c    | 13 +++++++++++++
  3 files changed, 23 insertions(+), 3 deletions(-)
hw/ppc/spapr_drc.c +2 -3
index 8c4997d795..98b626acf9 100644
@@ -421,9 +421,8 @@ void spapr_drc_unplug_request(SpaprDrc *drc)
  int spapr_drc_unplug_timeout_remaining_sec(SpaprDrc *drc)
  {
-    if (drc->unplug_requested && timer_pending(drc->unplug_timeout_timer)) {
-        return (qemu_timeout_ns_to_ms(drc->unplug_timeout_timer->expire_time) -
-                qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)) / 1000;
+    if (drc->unplug_requested) {
+        return timer_deadline_ms(drc->unplug_timeout_timer) / 1000;
      }
      return 0;
include/qemu/timer.h +8 -0
index 1678238384..5e76e3f8c2 100644
@@ -795,6 +795,14 @@ static inline int64_t get_max_clock_jump(void)
      return 60 * NANOSECONDS_PER_SECOND;
  }
+/**
+ * timer_deadline_ms:
+ *
+ * Returns the remaining miliseconds for @timer to expire, or zero
+ * if the timer is no longer pending.
+ */
+int64_t timer_deadline_ms(QEMUTimer *timer);
+
  /*
   * Low level clock functions
   */
util/qemu-timer.c +13 -0
index 81c28af517..3a03a63daa 100644
@@ -243,6 +243,19 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
      return delta;
  }
+/*
+ * Returns the time remaining for the deadline, in ms.
+ */
+int64_t timer_deadline_ms(QEMUTimer *timer)
+{
+    if (timer_pending(timer)) {
+        return qemu_timeout_ns_to_ms(timer->expire_time) -
+               qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
+    }
+
+    return 0;
+}
+
  /* Calculate the soonest deadline across all timerlists attached
   * to the clock. This is used for the icount timeout so we
   * ignore whether or not the clock should be used in deadline
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#3 lore raw
In reply to Daniel Henrique Barboza
Quoted 23 lines · On Thu, 25 Feb 2021 18:29:14 -0300 Daniel Henrique Barboza…

The pSeries machine is using QEMUTimer internals to return the timeout in seconds for a timer object, in hw/ppc/spapr.c, function spapr_drc_unplug_timeout_remaining_sec().

Create a helper in qemu-timer.c to retrieve the deadline for a QEMUTimer object, in ms, to avoid exposing timer internals to the PPC code.

---

This patch is applicable on top of David's ppc-for-6.0.

 hw/ppc/spapr_drc.c   |  5 ++---
 include/qemu/timer.h |  8 ++++++++
 util/qemu-timer.c    | 13 +++++++++++++
 3 files changed, 23 insertions(+), 3 deletions(-)
hw/ppc/spapr_drc.c +0 -0
index 8c4997d795..98b626acf9 100644

hw/ppc/spapr_drc.c void spapr_drc_unplug_request(SpaprDrc *drc) · line 426

Quoted 9 lines
hw/ppc/spapr_drc.c +2 -3
@@ -421,9 +421,8 @@ void spapr_drc_unplug_request(SpaprDrc *drc)
 
 int spapr_drc_unplug_timeout_remaining_sec(SpaprDrc *drc)
 {
-    if (drc->unplug_requested && timer_pending(drc->unplug_timeout_timer)) {
-        return (qemu_timeout_ns_to_ms(drc->unplug_timeout_timer->expire_time) -
-                qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL)) / 1000;
+    if (drc->unplug_requested) {
+        return timer_deadline_ms(drc->unplug_timeout_timer) / 1000;
     }

Nice improvement. This also removes a dependency on QEMU_CLOCK_VIRTUAL that we shouldn't need since we've already passed it to timer_new_ms() during realize.

It seems that spapr_drc_start_unplug_timeout_timer() ...

static void spapr_drc_start_unplug_timeout_timer(SpaprDrc *drc)

{
    SpaprDrcClass *drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc);

    if (drck->unplug_timeout_seconds != 0) {
        timer_mod(drc->unplug_timeout_timer,
                  qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
                  drck->unplug_timeout_seconds * 1000);
    }
}

... could be simplified as well with a timer_mod_deadline_ms() helper, but this can go to another patch.

Quoted 27 lines
hw/ppc/spapr_drc.c +0 -0
     return 0;
include/qemu/timer.h +8 -0
index 1678238384..5e76e3f8c2 100644
@@ -795,6 +795,14 @@ static inline int64_t get_max_clock_jump(void)
     return 60 * NANOSECONDS_PER_SECOND;
 }
 
+/**
+ * timer_deadline_ms:
+ *
+ * Returns the remaining miliseconds for @timer to expire, or zero
+ * if the timer is no longer pending.
+ */
+int64_t timer_deadline_ms(QEMUTimer *timer);
+
 /*
  * Low level clock functions
  */
util/qemu-timer.c +0 -0
index 81c28af517..3a03a63daa 100644
@@ -243,6 +243,19 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
     return delta;

util/qemu-timer.c int64_t timerlist_deadline_ns(QEMUTimerList *timer_list) · line 253

Quoted 10 lines
util/qemu-timer.c +8 -0
 }
 
+/*
+ * Returns the time remaining for the deadline, in ms.
+ */
+int64_t timer_deadline_ms(QEMUTimer *timer)
+{
+    if (timer_pending(timer)) {
+        return qemu_timeout_ns_to_ms(timer->expire_time) -
+               qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);

This is assuming that the timer belongs to the QEMU_CLOCK_VIRTUAL clock, which looks awkward for a presumably generic function.

Correct clock type can be accessed through ->timer_list :

	qemu_clock_get_ms(timer->timer_list->clock->type)
Quoted 8 lines
util/qemu-timer.c +5 -0
+    }
+
+    return 0;
+}
+
 /* Calculate the soonest deadline across all timerlists attached
  * to the clock. This is used for the icount timeout so we
  * ignore whether or not the clock should be used in deadline