target/riscv: Keep cycle overflow deadlines in virtual nanoseconds
PATCH 1/1 target/riscv: Keep cycle overflow deadlines in virtual nanoseconds
The cycle counter uses icount_get(), which already returns virtual time in nanoseconds. The PMU overflow helper applies icount_to_ns() to both cycle and instruction counters, so cycle overflow timers are delayed by the icount shift.
Pass the counter context to the helper and scale only instruction counts.
--- target/riscv/tcg/pmu.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
target/riscv/tcg/pmu.c +8 -4
@@ -281,12 +281,15 @@ static gboolean pmu_remove_event_map(gpointer key, gpointer value,return (GPOINTER_TO_UINT (value) ==GPOINTER_TO_UINT (udata)) ? true : false; } -static int64_t pmu_icount_ticks_to_ns (int64_t value) +static int64_t pmu_icount_ticks_to_ns (CPURISCVState *env,uint32_t ctr_idx, +int64_t value) {int64_t ret =0 ;if (icount_enabled ()) { - ret =icount_to_ns (value); +/* The cycle counter counts ns of virtual time, see icount_get() */ + ret =riscv_pmu_ctr_monitor_instructions (env, ctr_idx) ? +icount_to_ns (value) : value; }else { ret = (NANOSECONDS_PER_SECOND /RISCV_TIMEBASE_FREQ ) * value; }
@@ -456,8 +459,9 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { - overflow_ns =pmu_icount_ticks_to_ns ((int64_t )overflow_delta); - overflow_left =pmu_icount_ticks_to_ns (overflow_left) ; + overflow_ns =pmu_icount_ticks_to_ns (env, ctr_idx, + (int64_t )overflow_delta); + overflow_left =pmu_icount_ticks_to_ns (env, ctr_idx, overflow_left); }else {return -1 ; }
Signature · 1 line
2.55.0
Quoted 7 lines · Philippe Mathieu-Daudé
The cycle counter uses
icount_get(), which already returns virtual time in nanoseconds. The PMU overflow helper appliesicount_to_ns()to both cycle and instruction counters, so cycle overflow timers are delayed by the icount shift.Pass the counter context to the helper and scale only instruction counts.
Hi Philippe,
Patch 3 of my PMU series [1] includes this fix. Its aim is to keep counter accounting and overflow scheduling consistent for each fixed event.
It introduces riscv_pmu_read_fixed_source() so counter reads and privilege accounting use the same source. In non-icount mode, this switches both paths to VM-elapsed ticks, excluding time spent while the VM is stopped.
Overflow scheduling must respect that source's units. Under icount, cycles are already virtual nanoseconds, while instructions remain raw counts. In pmu_ticks_to_ns(), icount_to_ns() therefore converts only the number of instructions remaining before overflow to virtual nanoseconds. The cycle value needs no further conversion.
This covers the issue addressed by your patch. Patch 3 also includes a cycle-overflow regression test with icount shift=3.
Thanks,
Tiancheng
Quoted 40 lines
Fixes b2d7a7c7e4e30 ("target/riscv: Implement privilege mode filtering for cycle/instret")Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/riscv/tcg/pmu.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)target/riscv/tcg/pmu.c +8
-4@@ -281,12 +281,15 @@ static gboolean pmu_remove_event_map(gpointer key, gpointer value,return (GPOINTER_TO_UINT (value) ==GPOINTER_TO_UINT (udata)) ? true : false; } -static int64_t pmu_icount_ticks_to_ns (int64_t value) +static int64_t pmu_icount_ticks_to_ns (CPURISCVState *env,uint32_t ctr_idx, +int64_t value) {int64_t ret =0 ;if (icount_enabled ()) { - ret =icount_to_ns (value); +/* The cycle counter counts ns of virtual time, see icount_get() */ + ret =riscv_pmu_ctr_monitor_instructions (env, ctr_idx) ? +icount_to_ns (value) : value; }else { ret = (NANOSECONDS_PER_SECOND /RISCV_TIMEBASE_FREQ ) * value; }@@ -456,8 +459,9 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { - overflow_ns =pmu_icount_ticks_to_ns ((int64_t )overflow_delta); - overflow_left =pmu_icount_ticks_to_ns (overflow_left) ; + overflow_ns =pmu_icount_ticks_to_ns (env, ctr_idx, + (int64_t )overflow_delta); + overflow_left =pmu_icount_ticks_to_ns (env, ctr_idx, overflow_left); }else {return -1 ; }
Hi Tiancheng,
Quoted 7 lines · TianCheng TANG
Quoted 5 lines · Philippe Mathieu-Daudé
The cycle counter uses
icount_get(), which already returns virtual time in nanoseconds. The PMU overflow helper appliesicount_to_ns()to both cycle and instruction counters, so cycle overflow timers are delayed by the icount shift.
Quoted 5 lines
Quoted 2 lines
Pass the counter context to the helper and scale only instruction counts.
Hi Philippe,
Patch 3 of my PMU series [1] includes this fix.
Yes, I noticed your work while preparing mine for submission. I have the impression your series is too big to get merged as a fix for 11.2 so I sent mine hoping at least this part'd get fixed, it isn't invasive; still your series would need adapting rebase then.
Quoted 66 lines
Its aim is to keep
counter accounting and overflow scheduling consistent for each fixed event.It introduces
riscv_pmu_read_fixed_source()so counter reads and privilege accounting use the same source. In non-icount mode, this switches both paths to VM-elapsed ticks, excluding time spent while the VM is stopped.Overflow scheduling must respect that source's units. Under icount, cycles are already virtual nanoseconds, while instructions remain raw counts. In
pmu_ticks_to_ns(),icount_to_ns()therefore converts only the number of instructions remaining before overflow to virtual nanoseconds. The cycle value needs no further conversion.This covers the issue addressed by your patch. Patch 3 also includes a cycle-overflow regression test with icount shift=3.
Thanks,
Tiancheng[1] https://lore.kernel.org/qemu-devel/20261119-riscv-pmu-correctness- [email protected]/
Quoted 42 lines
Fixes b2d7a7c7e4e30 ("target/riscv: Implement privilege mode filtering for cycle/instret")Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
target/riscv/tcg/pmu.c| 12 ++++++++----1 file changed, 8 insertions(+), 4 deletions(-)target/riscv/tcg/pmu.c +3
-2@@ -281,12 +281,15 @@ static gboolean pmu_remove_event_map(gpointer key, gpointer value, return (GPOINTER_TO_UINT(value) == GPOINTER_TO_UINT(udata)) ? true : false; } -static int64_t pmu_icount_ticks_to_ns (int64_t value) +static int64_t pmu_icount_ticks_to_ns (CPURISCVState *env,uint32_t ctr_idx, + int64_t value) { int64_t ret = 0; if (icount_enabled()) { - ret = icount_to_ns(value); + /* The cycle counter counts ns of virtual time, see
icount_get()*/
+ ret =riscv_pmu_ctr_monitor_instructions(env, ctr_idx)? +icount_to_ns(value): value;
} else {
ret = (NANOSECONDS_PER_SECOND/RISCV_TIMEBASE_FREQ) * value; }target/riscv/tcg/pmu.c +0
-0@@ -456,8 +459,9 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)if (
riscv_pmu_ctr_monitor_cycles(env, ctr_idx)||riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { -overflow_ns= pmu_icount_ticks_to_ns((int64_t)overflow_delta); -overflow_left=pmu_icount_ticks_to_ns(overflow_left); +overflow_ns= pmu_icount_ticks_to_ns(env,ctr_idx, + (int64_t)overflow_delta); +overflow_left=pmu_icount_ticks_to_ns(env, ctr_idx, overflow_left);
} else {
return -1;
}
Hi Philippe,
Quoted 10 lines · Philippe Mathieu-Daudé
The cycle counter uses
icount_get(), which already returns virtual time in nanoseconds. The PMU overflow helper appliesicount_to_ns()to both cycle and instruction counters, so cycle overflow timers are delayed by the icount shift.Pass the counter context to the helper and scale only instruction counts.
Fixes b2d7a7c7e4e30 ("target/riscv: Implement privilege mode filtering for cycle/instret")Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 13 lines
--- target/riscv/tcg/pmu.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)target/riscv/tcg/pmu.c +0
-1@@ -281,12 +281,15 @@ static gboolean pmu_remove_event_map(gpointer key, gpointer value,return (GPOINTER_TO_UINT (value) ==GPOINTER_TO_UINT (udata)) ? true : false; } -static int64_t pmu_icount_ticks_to_ns (int64_t value)
target/riscv/tcg/pmu.c static gboolean pmu_remove_event_map(gpointer key, gpointer value, · line 292
Quoted 10 lines
target/riscv/tcg/pmu.c +5
-1+static int64_t pmu_icount_ticks_to_ns (CPURISCVState *env,uint32_t ctr_idx, +int64_t value) {int64_t ret =0 ;if (icount_enabled ()) { - ret =icount_to_ns (value); +/* The cycle counter counts ns of virtual time, see icount_get() */ + ret =riscv_pmu_ctr_monitor_instructions (env, ctr_idx) ? +icount_to_ns (value) : value;
As an optional cleanup, could we call riscv_pmu_ctr_monitor_instructions() once in riscv_pmu_setup_timer() and reuse the result for the fixed-event check and both conversions?
Thanks,
Tiancheng
Quoted 15 lines
target/riscv/tcg/pmu.c +3
-2}else { ret = (NANOSECONDS_PER_SECOND /RISCV_TIMEBASE_FREQ ) * value; }@@ -456,8 +459,9 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { - overflow_ns =pmu_icount_ticks_to_ns ((int64_t )overflow_delta); - overflow_left =pmu_icount_ticks_to_ns (overflow_left) ; + overflow_ns =pmu_icount_ticks_to_ns (env, ctr_idx, + (int64_t )overflow_delta); + overflow_left =pmu_icount_ticks_to_ns (env, ctr_idx, overflow_left); }else {return -1 ; }
Quoted 11 lines · Philippe Mathieu-Daudé
The cycle counter uses
icount_get(), which already returns virtual time in nanoseconds. The PMU overflow helper appliesicount_to_ns()to both cycle and instruction counters, so cycle overflow timers are delayed by the icount shift.Pass the counter context to the helper and scale only instruction counts.
Fixes b2d7a7c7e4e30 ("target/riscv: Implement privilege modefiltering for cycle/instret")
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 47 lines
---
target/riscv/tcg/pmu.c | 12 ++++++++----1 file changed, 8 insertions(+), 4 deletions(-)target/riscv/tcg/pmu.c +0
-0@@ -281,12 +281,15 @@ static gboolean pmu_remove_event_map(gpointer key, gpointer value,return (
GPOINTER_TO_UINT(value)==GPOINTER_TO_UINT(udata)) ? true : false;
}target/riscv/tcg/pmu.c +1
-1-static int64_t pmu_icount_ticks_to_ns (int64_t value) +static int64_t pmu_icount_ticks_to_ns(CPURISCVState *env, uint32_t
ctr_idx,
+int64_tvalue)
{
int64_tret = 0;
if (icount_enabled()) {
- ret =icount_to_ns(value);
+ /* The cycle counter counts ns of virtual time, seeicount_get()*/
+ ret =riscv_pmu_ctr_monitor_instructions(env, ctr_idx)? +icount_to_ns(value): value;
} else {
ret = (NANOSECONDS_PER_SECOND/RISCV_TIMEBASE_FREQ) * value;
}target/riscv/tcg/pmu.c +0
-0@@ -456,8 +459,9 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)
if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx)||riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { -overflow_ns= pmu_icount_ticks_to_ns((int64_t)overflow_delta);
-overflow_left=pmu_icount_ticks_to_ns(overflow_left); +overflow_ns= pmu_icount_ticks_to_ns(env,ctr_idx, +
(int64_t)overflow_delta);
+overflow_left=pmu_icount_ticks_to_ns(env, ctr_idx, overflow_left);
} else {
return -1;
}
Alistair Francis
AppliedQuoted 11 lines · Philippe Mathieu-Daudé
The cycle counter uses
icount_get(), which already returns virtual time in nanoseconds. The PMU overflow helper appliesicount_to_ns()to both cycle and instruction counters, so cycle overflow timers are delayed by the icount shift.Pass the counter context to the helper and scale only instruction counts.
Fixes b2d7a7c7e4e30 ("target/riscv: Implement privilege modefiltering for cycle/instret")
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 47 lines
---
target/riscv/tcg/pmu.c | 12 ++++++++----1 file changed, 8 insertions(+), 4 deletions(-)target/riscv/tcg/pmu.c +0
-0@@ -281,12 +281,15 @@ static gboolean pmu_remove_event_map(gpointer key, gpointer value,return (
GPOINTER_TO_UINT(value)==GPOINTER_TO_UINT(udata)) ? true : false;
}target/riscv/tcg/pmu.c +1
-1-static int64_t pmu_icount_ticks_to_ns (int64_t value) +static int64_t pmu_icount_ticks_to_ns(CPURISCVState *env, uint32_t
ctr_idx,
+int64_tvalue)
{
int64_tret = 0;
if (icount_enabled()) {
- ret =icount_to_ns(value);
+ /* The cycle counter counts ns of virtual time, seeicount_get()*/
+ ret =riscv_pmu_ctr_monitor_instructions(env, ctr_idx)? +icount_to_ns(value): value;
} else {
ret = (NANOSECONDS_PER_SECOND/RISCV_TIMEBASE_FREQ) * value;
}target/riscv/tcg/pmu.c +0
-0@@ -456,8 +459,9 @@ int riscv_pmu_setup_timer(CPURISCVState *env, uint64_t value, uint32_t ctr_idx)
if (riscv_pmu_ctr_monitor_cycles(env, ctr_idx)||riscv_pmu_ctr_monitor_instructions(env, ctr_idx)) { -overflow_ns= pmu_icount_ticks_to_ns((int64_t)overflow_delta);
-overflow_left=pmu_icount_ticks_to_ns(overflow_left); +overflow_ns= pmu_icount_ticks_to_ns(env,ctr_idx, +
(int64_t)overflow_delta);
+overflow_left=pmu_icount_ticks_to_ns(env, ctr_idx, overflow_left);
} else {
return -1;
}