Skip to contentSkip to conversation

target/riscv: Use a direct cast for better performance

PATCH Superseded Richard W.M. Jones 4 participants 6 messages Oct 8, 2023 – Oct 9, 2023 RISC-V

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/1 target/riscv: Use a direct cast for better performance

RISCV_CPU(cs) uses a checked cast. When QOM cast debugging is enabled this adds about 5% total overhead when emulating RV64 on x86-64 host.

Using a RISC-V guest with 16 vCPUs, 16 GB of guest RAM, virtio-blk disk. The guest has a copy of the qemu source tree. The test involves compiling the qemu source tree with 'make clean; time make -j16'.

Before making this change the compile step took 449 & 447 seconds over two consecutive runs.

After making this change, 428 & 422 seconds.

The saving is about 5%.

Thanks: Paolo Bonzini

---
 target/riscv/cpu_helper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +5 -1
index 3a02079290..479d9863ae 100644
@@ -66,7 +66,11 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc,
                           uint64_t *cs_base, uint32_t *pflags)
 {
     CPUState *cs = env_cpu(env);
-    RISCVCPU *cpu = RISCV_CPU(cs);
+    /*
+     * Using the checked cast RISCV_CPU(cs) imposes ~ 5% overhead when
+     * QOM cast debugging is enabled, so use a direct cast instead.
+     */
+    RISCVCPU *cpu = (RISCVCPU *)cs;
     RISCVExtStatus fs, vs;
     uint32_t flags = 0;
 
Signature · 1 line
2.41.0
#2 lore raw

Hi Richard,

Quoted 26 lines · Richard W.M. Jones

RISCV_CPU(cs) uses a checked cast. When QOM cast debugging is enabled this adds about 5% total overhead when emulating RV64 on x86-64 host.

Using a RISC-V guest with 16 vCPUs, 16 GB of guest RAM, virtio-blk disk. The guest has a copy of the qemu source tree. The test involves compiling the qemu source tree with 'make clean; time make -j16'.

Before making this change the compile step took 449 & 447 seconds over two consecutive runs.

After making this change, 428 & 422 seconds.

The saving is about 5%.

Thanks: Paolo Bonzini

---
  target/riscv/cpu_helper.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +0 -0
index 3a02079290..479d9863ae 100644

target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, · line 69

Quoted 4 lines
target/riscv/cpu_helper.c +0 -1
@@ -66,7 +66,11 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc,
                            uint64_t *cs_base, uint32_t *pflags)
  {
      CPUState *cs = env_cpu(env);
-    RISCVCPU *cpu = RISCV_CPU(cs);

You might want to use:

       RISCVCPU *cpu =  env_archcpu(env);

Other occurences in target/riscv/internals.h.

Quoted 8 lines
target/riscv/cpu_helper.c +5 -0
+    /*
+     * Using the checked cast RISCV_CPU(cs) imposes ~ 5% overhead when
+     * QOM cast debugging is enabled, so use a direct cast instead.
+     */
+    RISCVCPU *cpu = (RISCVCPU *)cs;
      RISCVExtStatus fs, vs;
      uint32_t flags = 0;
 
Reviewed-by on v2 1/1
#3 lore raw
In reply to Richard W.M. Jones
Quoted 26 lines · Richard W.M. Jones

RISCV_CPU(cs) uses a checked cast. When QOM cast debugging is enabled this adds about 5% total overhead when emulating RV64 on x86-64 host.

Using a RISC-V guest with 16 vCPUs, 16 GB of guest RAM, virtio-blk disk. The guest has a copy of the qemu source tree. The test involves compiling the qemu source tree with 'make clean; time make -j16'.

Before making this change the compile step took 449 & 447 seconds over two consecutive runs.

After making this change, 428 & 422 seconds.

The saving is about 5%.

Thanks: Paolo Bonzini

---
  target/riscv/cpu_helper.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +0 -0
index 3a02079290..479d9863ae 100644

target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, · line 73

Quoted 9 lines
target/riscv/cpu_helper.c +5 -1
@@ -66,7 +66,11 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc,
                            uint64_t *cs_base, uint32_t *pflags)
  {
      CPUState *cs = env_cpu(env);
-    RISCVCPU *cpu = RISCV_CPU(cs);
+    /*
+     * Using the checked cast RISCV_CPU(cs) imposes ~ 5% overhead when
+     * QOM cast debugging is enabled, so use a direct cast instead.
+     */
+    RISCVCPU *cpu = (RISCVCPU *)cs;

This function is very hot. Maybe we should cache the tbflags instead of calculate it here. Otherwise,

Zhiwei

Quoted 3 lines
target/riscv/cpu_helper.c +0 -0
      RISCVExtStatus fs, vs;
      uint32_t flags = 0;
 
#4 lore raw
Quoted 32 lines · LIU Zhiwei
Quoted 30 lines · Richard W.M. Jones

RISCV_CPU(cs) uses a checked cast. When QOM cast debugging is enabled this adds about 5% total overhead when emulating RV64 on x86-64 host.

Using a RISC-V guest with 16 vCPUs, 16 GB of guest RAM, virtio-blk disk. The guest has a copy of the qemu source tree. The test involves compiling the qemu source tree with 'make clean; time make -j16'.

Before making this change the compile step took 449 & 447 seconds over two consecutive runs.

After making this change, 428 & 422 seconds.

The saving is about 5%.

Thanks: Paolo Bonzini

---
 target/riscv/cpu_helper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +0 -1
index 3a02079290..479d9863ae 100644
@@ -66,7 +66,11 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc,
                           uint64_t *cs_base, uint32_t *pflags)
 {
     CPUState *cs = env_cpu(env);
-    RISCVCPU *cpu = RISCV_CPU(cs);
Quoted 8 lines

target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, · line 73

Quoted 5 lines
target/riscv/cpu_helper.c +5 -0
+    /*
+     * Using the checked cast RISCV_CPU(cs) imposes ~ 5% overhead when
+     * QOM cast debugging is enabled, so use a direct cast instead.
+     */
+    RISCVCPU *cpu = (RISCVCPU *)cs;

This function is very hot. Maybe we should cache the tbflags instead of calculate it here. Otherwise,

This function is indeed very hot, taking over 20% of total host time in my guest stress test.

How would we cache the flags? AIUI they simply depend on machine state and we must recalculate them either when the machine state changes (sprinkle "update_tbflags" everywhere) or here. If you have any suggestions I can try things.

Quoted 1 line

I posted a v3 based on Philippe's feedback.

Rich.

Quoted 4 lines

Zhiwei

Quoted 2 lines
target/riscv/cpu_helper.c +0 -0
     RISCVExtStatus fs, vs;
     uint32_t flags = 0;
Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
#5 lore raw
Quoted 39 lines · Richard W.M. Jones
Quoted 22 lines · LIU Zhiwei
Quoted 21 lines · Richard W.M. Jones

RISCV_CPU(cs) uses a checked cast. When QOM cast debugging is enabled this adds about 5% total overhead when emulating RV64 on x86-64 host.

Using a RISC-V guest with 16 vCPUs, 16 GB of guest RAM, virtio-blk disk. The guest has a copy of the qemu source tree. The test involves compiling the qemu source tree with 'make clean; time make -j16'.

Before making this change the compile step took 449 & 447 seconds over two consecutive runs.

After making this change, 428 & 422 seconds.

The saving is about 5%.

Thanks: Paolo Bonzini

---
  target/riscv/cpu_helper.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 5 lines
target/riscv/cpu_helper.c +0 -0
index 3a02079290..479d9863ae 100644

target/riscv/cpu_helper.c void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, · line 73

Quoted 9 lines
target/riscv/cpu_helper.c +5 -1
@@ -66,7 +66,11 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc,
                            uint64_t *cs_base, uint32_t *pflags)
  {
      CPUState *cs = env_cpu(env);
-    RISCVCPU *cpu = RISCV_CPU(cs);
+    /*
+     * Using the checked cast RISCV_CPU(cs) imposes ~ 5% overhead when
+     * QOM cast debugging is enabled, so use a direct cast instead.
+     */
+    RISCVCPU *cpu = (RISCVCPU *)cs;

This function is very hot. Maybe we should cache the tbflags instead of calculate it here. Otherwise,

Quoted 6 lines

This function is indeed very hot, taking over 20% of total host time in my guest stress test.

How would we cache the flags? AIUI they simply depend on machine state and we must recalculate them either when the machine state changes (sprinkle "update_tbflags" everywhere)

Yes, we should do in this way.

Quoted 2 lines

or here. If you have
any suggestions I can try things.

I think it exceeds this patch.

Quoted 3 lines
Quoted 2 lines

I posted a v3 based on Philippe's feedback.

OK.

Thanks
Zhiwei

Quoted 7 lines

Rich.

Quoted 4 lines

Zhiwei

Quoted 2 lines
target/riscv/cpu_helper.c +0 -0
      RISCVExtStatus fs, vs;
      uint32_t flags = 0;