Skip to contentSkip to conversation

Clean and improve time related helpers

PATCH Applied Aurelien Jarno 2 participants 7 messages May 18, 2015 – May 25, 2015

lore
Share & tools
lore
#0 lore raw

cover Clean and improve time related helpers

This patch series cleans and improves the time-of-day clock and CPU timer. Most notably it implements the STPT instruction, which allow the Linux kernel to do time accouting (so top reports correct values).

Aurelien Jarno (5):
target-s390x: add a tod2time function
target-s390x: simplify SCKC helper
target-s390x: streamline STCK helper
target-s390x: implement STCKC helper
target-s390x: implement STPT helper

 target-s390x/cpu.h         |  5 +++++
 target-s390x/misc_helper.c | 32 +++++++++++++++-----------------
 2 files changed, 20 insertions(+), 17 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/5 target-s390x: add a tod2time function

Add a tod2time function similar to the time2tod one, instead of open coding the conversion.

Cc Alexander Graf <[email protected]>
Cc Richard Henderson <[email protected]>
---
 target-s390x/cpu.h         | 5 +++++
 target-s390x/misc_helper.c | 4 ++--
 2 files changed, 7 insertions(+), 2 deletions(-)
target-s390x/cpu.h +5 -0
index 8135dda..28aa39d 100644
@@ -970,6 +970,11 @@ static inline uint64_t time2tod(uint64_t ns) {
     return (ns << 9) / 125;
 }
 
+/* Converts s390's clock format to ns */
+static inline uint64_t tod2time(uint64_t t) {
+    return (t * 125) >> 9;
+}
+
 static inline void cpu_inject_ext(S390CPU *cpu, uint32_t code, uint32_t param,
                                   uint64_t param64)
 {
target-s390x/misc_helper.c +2 -2
index e1007fa..230bafd 100644
@@ -294,7 +294,7 @@ void HELPER(sckc)(CPUS390XState *env, uint64_t time)
     /* difference between now and then */
     time -= clock_value(env);
     /* nanoseconds */
-    time = (time * 125) >> 9;
+    time = tod2time(time);
 
     timer_mod(env->tod_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time);
 }
@@ -314,7 +314,7 @@ void HELPER(spt)(CPUS390XState *env, uint64_t time)
     }
 
     /* nanoseconds */
-    time = (time * 125) >> 9;
+    time = tod2time(time);
 
     timer_mod(env->cpu_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time);
 }
Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/5 target-s390x: simplify SCKC helper

In reply to Aurelien Jarno

The clock comparator and the QEMU timer work the same way, triggering at a given time, they just differ by the origin and the scale. It is therefore possible to go from one to another without using the current clock value. This spares two calls to qemu_clock_get_ns, which probably return slightly different values, possibly reducing the accuracy.

Cc Alexander Graf <[email protected]>
Cc Richard Henderson <[email protected]>
---
 target-s390x/misc_helper.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target-s390x/misc_helper.c +4 -3
index 230bafd..120807f 100644
@@ -291,12 +291,13 @@ void HELPER(sckc)(CPUS390XState *env, uint64_t time)
         return;
     }
 
-    /* difference between now and then */
-    time -= clock_value(env);
+    /* difference between origins */
+    time -= env->tod_offset;
+
     /* nanoseconds */
     time = tod2time(time);
 
-    timer_mod(env->tod_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time);
+    timer_mod(env->tod_timer, env->tod_basetime + time);
 }
 
 /* Store Clock Comparator */
Signature · 1 line
2.1.4
#3 lore raw

PATCH 3/5 target-s390x: streamline STCK helper

In reply to Aurelien Jarno

Now that clock_value is only used in one place, we can inline it in the STCK helper.

Cc Alexander Graf <[email protected]>
Cc Richard Henderson <[email protected]>
---
 target-s390x/misc_helper.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
target-s390x/misc_helper.c +2 -7
index 120807f..fa105fa 100644
@@ -268,7 +268,8 @@ void HELPER(spx)(CPUS390XState *env, uint64_t a1)
     tlb_flush_page(cs, TARGET_PAGE_SIZE);
 }
 
-static inline uint64_t clock_value(CPUS390XState *env)
+/* Store Clock */
+uint64_t HELPER(stck)(CPUS390XState *env)
 {
     uint64_t time;
 
@@ -278,12 +279,6 @@ static inline uint64_t clock_value(CPUS390XState *env)
     return time;
 }
 
-/* Store Clock */
-uint64_t HELPER(stck)(CPUS390XState *env)
-{
-    return clock_value(env);
-}
-
 /* Set Clock Comparator */
 void HELPER(sckc)(CPUS390XState *env, uint64_t time)
 {
Signature · 1 line
2.1.4
#4 lore raw

PATCH 4/5 target-s390x: implement STCKC helper

In reply to Aurelien Jarno

The STCKC instruction just returns the last written clock comparator value and KVM already provides the corresponding variable.

Cc Alexander Graf <[email protected]>
Cc Richard Henderson <[email protected]>
---
 target-s390x/misc_helper.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target-s390x/misc_helper.c +3 -2
index fa105fa..57aee95 100644
@@ -286,6 +286,8 @@ void HELPER(sckc)(CPUS390XState *env, uint64_t time)
         return;
     }
 
+    env->ckc = time;
+
     /* difference between origins */
     time -= env->tod_offset;
 
@@ -298,8 +300,7 @@ void HELPER(sckc)(CPUS390XState *env, uint64_t time)
 /* Store Clock Comparator */
 uint64_t HELPER(stckc)(CPUS390XState *env)
 {
-    /* XXX implement */
-    return 0;
+    return env->ckc;
 }
 
 /* Set CPU Timer */
Signature · 1 line
2.1.4
#5 lore raw

PATCH 5/5 target-s390x: implement STPT helper

In reply to Aurelien Jarno

Save the timer target value in the SPT helper, so that the STPT helper can compute the remaining time.

This allow the Linux kernel to correctly do time accounting.

Cc Alexander Graf <[email protected]>
Cc Richard Henderson <[email protected]>
---
 target-s390x/misc_helper.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target-s390x/misc_helper.c +4 -3
index 57aee95..3ec7268 100644
@@ -313,14 +313,15 @@ void HELPER(spt)(CPUS390XState *env, uint64_t time)
     /* nanoseconds */
     time = tod2time(time);
 
-    timer_mod(env->cpu_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time);
+    env->cputm = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + time;
+
+    timer_mod(env->cpu_timer, env->cputm);
 }
 
 /* Store CPU Timer */
 uint64_t HELPER(stpt)(CPUS390XState *env)
 {
-    /* XXX implement */
-    return 0;
+    return time2tod(env->cputm - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
 }
 
 /* Store System Information */
Signature · 1 line
2.1.4
#6 lore raw
In reply to Aurelien Jarno
Quoted 3 lines · Aurelien Jarno

This patch series cleans and improves the time-of-day clock and CPU timer. Most notably it implements the STPT instruction, which allow the Linux kernel to do time accouting (so top reports correct values).

Thanks, applied all to s390-next.

Alex