Skip to contentSkip to conversation

accel/tcg: Consider cluster index in tb_lookup__cpu_state()

PATCH Applied Peter Maydell 6 participants 8 messages Feb 5, 2019 – Feb 6, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/tcg: Consider cluster index in tb_lookup__cpu_state()

In commit f7b78602fdc6c6e4be we added the CPU cluster number to the cflags field of the TB hash; this included adding it to the value kept in tb->cflags, since we pass that field directly into the hash calculation in some places. Unfortunately we forgot to check whether other parts of the code were doing comparisons against tb->cflags that would need to be updated.

It turns out that there is exactly one such place: the tb_lookup__cpu_state() function checks whether the TB it has found in the tb_jmp_cache has a tb->cflags matching the cf_mask that is passed in. The tb->cflags has the cluster_index in it but the cf_mask does not.

Hoist the "add cluster index to the cf_mask" code up from tb_htable_lookup() to tb_lookup__cpu_state() so it can be considered in the "did this TB match in the jmp cache" condition, as well as when we do the full hash lookup by physical PC, flags, etc. (tb_htable_lookup() is only called from tb_lookup__cpu_state(), so this change doesn't require any further knock-on changes.)

Fixes f7b78602fdc6c6e4be ("accel/tcg: Add cluster number to TCG TB hash")

---
Does anybody know why tb_lookup__cpu_state() has that odd double-underscore in the middle of its name?

Since the jmp_cache is per-vcpu we know that we're always going to match on the cluster_index, so the other option would be to leave the cluster_index bits out of the comparison, and leave the "fold in cluster index to cf_mask" code in tb_htable_lookup(). Or we could require the callers of tb_lookup__cpu_state() to all provide the cluster index, but that's more places to change, so I prefer this.

---
 include/exec/tb-lookup.h | 4 ++++
 accel/tcg/cpu-exec.c     | 3 ---
 2 files changed, 4 insertions(+), 3 deletions(-)
include/exec/tb-lookup.h +4 -0
index 492cb682894..26921b6dafd 100644
@@ -28,6 +28,10 @@ tb_lookup__cpu_state(CPUState *cpu, target_ulong *pc, target_ulong *cs_base,
     cpu_get_tb_cpu_state(env, pc, cs_base, flags);
     hash = tb_jmp_cache_hash_func(*pc);
     tb = atomic_rcu_read(&cpu->tb_jmp_cache[hash]);
+
+    cf_mask &= ~CF_CLUSTER_MASK;
+    cf_mask |= cpu->cluster_index << CF_CLUSTER_SHIFT;
+
     if (likely(tb &&
                tb->pc == *pc &&
                tb->cs_base == *cs_base &&
accel/tcg/cpu-exec.c +0 -3
index 7cf1292546f..60d87d5a19b 100644
@@ -325,9 +325,6 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
     struct tb_desc desc;
     uint32_t h;
 
-    cf_mask &= ~CF_CLUSTER_MASK;
-    cf_mask |= cpu->cluster_index << CF_CLUSTER_SHIFT;
-
     desc.env = (CPUArchState *)cpu->env_ptr;
     desc.cs_base = cs_base;
     desc.flags = flags;
Signature · 1 line
2.20.1
Tested-by on v1 1/1
#1 lore raw
Quoted 28 lines · Peter Maydell

In commit f7b78602fdc6c6e4be we added the CPU cluster number to the cflags field of the TB hash; this included adding it to the value kept in tb->cflags, since we pass that field directly into the hash calculation in some places. Unfortunately we forgot to check whether other parts of the code were doing comparisons against tb->cflags that would need to be updated.

It turns out that there is exactly one such place: the tb_lookup__cpu_state() function checks whether the TB it has found in the tb_jmp_cache has a tb->cflags matching the cf_mask that is passed in. The tb->cflags has the cluster_index in it but the cf_mask does not.

Hoist the "add cluster index to the cf_mask" code up from tb_htable_lookup() to tb_lookup__cpu_state() so it can be considered in the "did this TB match in the jmp cache" condition, as well as when we do the full hash lookup by physical PC, flags, etc. (tb_htable_lookup() is only called from tb_lookup__cpu_state(), so this change doesn't require any further knock-on changes.)

Fixes f7b78602fdc6c6e4be ("accel/tcg: Add cluster number to TCG TB hash")

---
Does anybody know why tb_lookup__cpu_state() has that odd double-underscore in the middle of its name?

Quoted 7 lines

Since the jmp_cache is per-vcpu we know that we're always going to match on the cluster_index, so the other option would be to leave the cluster_index bits out of the comparison, and leave the "fold in cluster index to cf_mask" code in tb_htable_lookup(). Or we could require the callers of tb_lookup__cpu_state() to all provide the cluster index, but that's more places to change, so I prefer this.

I can confirm the performance regression I experienced is fixed by this patch.

Tested-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 61 lines · Peter Maydell

In commit f7b78602fdc6c6e4be we added the CPU cluster number to the cflags field of the TB hash; this included adding it to the value kept in tb->cflags, since we pass that field directly into the hash calculation in some places. Unfortunately we forgot to check whether other parts of the code were doing comparisons against tb->cflags that would need to be updated.

It turns out that there is exactly one such place: the tb_lookup__cpu_state() function checks whether the TB it has found in the tb_jmp_cache has a tb->cflags matching the cf_mask that is passed in. The tb->cflags has the cluster_index in it but the cf_mask does not.

Hoist the "add cluster index to the cf_mask" code up from tb_htable_lookup() to tb_lookup__cpu_state() so it can be considered in the "did this TB match in the jmp cache" condition, as well as when we do the full hash lookup by physical PC, flags, etc. (tb_htable_lookup() is only called from tb_lookup__cpu_state(), so this change doesn't require any further knock-on changes.)

Fixes f7b78602fdc6c6e4be ("accel/tcg: Add cluster number to TCG TB hash")

---
Does anybody know why tb_lookup__cpu_state() has that odd double-underscore in the middle of its name?

Since the jmp_cache is per-vcpu we know that we're always going to match on the cluster_index, so the other option would be to leave the cluster_index bits out of the comparison, and leave the "fold in cluster index to cf_mask" code in tb_htable_lookup(). Or we could require the callers of tb_lookup__cpu_state() to all provide the cluster index, but that's more places to change, so I prefer this.

---
 include/exec/tb-lookup.h | 4 ++++
 accel/tcg/cpu-exec.c     | 3 ---
 2 files changed, 4 insertions(+), 3 deletions(-)
include/exec/tb-lookup.h +4 -0
index 492cb682894..26921b6dafd 100644
@@ -28,6 +28,10 @@ tb_lookup__cpu_state(CPUState *cpu, target_ulong *pc, target_ulong *cs_base,
     cpu_get_tb_cpu_state(env, pc, cs_base, flags);
     hash = tb_jmp_cache_hash_func(*pc);
     tb = atomic_rcu_read(&cpu->tb_jmp_cache[hash]);
+
+    cf_mask &= ~CF_CLUSTER_MASK;
+    cf_mask |= cpu->cluster_index << CF_CLUSTER_SHIFT;
+
     if (likely(tb &&
                tb->pc == *pc &&
                tb->cs_base == *cs_base &&
accel/tcg/cpu-exec.c +0 -0
index 7cf1292546f..60d87d5a19b 100644

accel/tcg/cpu-exec.c TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc, · line 330

Quoted 9 lines
accel/tcg/cpu-exec.c +0 -3
@@ -325,9 +325,6 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
     struct tb_desc desc;
     uint32_t h;
 
-    cf_mask &= ~CF_CLUSTER_MASK;
-    cf_mask |= cpu->cluster_index << CF_CLUSTER_SHIFT;
-
     desc.env = (CPUArchState *)cpu->env_ptr;
     desc.cs_base = cs_base;
     desc.flags = flags;

Looks good to me: without performing a detailed benchmark, with this patch applied the performance seems to be back to where it was before f7b78602fdc6c6e4be was merged.

ATB,

Mark.

#3 lore raw
Quoted 49 lines · Mark Cave-Ayland
Quoted 13 lines · Peter Maydell

In commit f7b78602fdc6c6e4be we added the CPU cluster number to the cflags field of the TB hash; this included adding it to the value kept in tb->cflags, since we pass that field directly into the hash calculation in some places. Unfortunately we forgot to check whether other parts of the code were doing comparisons against tb->cflags that would need to be updated.

It turns out that there is exactly one such place: the tb_lookup__cpu_state() function checks whether the TB it has found in the tb_jmp_cache has a tb->cflags matching the cf_mask that is passed in. The tb->cflags has the cluster_index in it but the cf_mask does not.

Quoted 8 lines

Hoist the "add cluster index to the cf_mask" code up from tb_htable_lookup() to tb_lookup__cpu_state() so it can be considered in the "did this TB match in the jmp cache" condition, as well as when we do the full hash lookup by physical PC, flags, etc. (tb_htable_lookup() is only called from tb_lookup__cpu_state(), so this change doesn't require any further knock-on changes.)

Fixes f7b78602fdc6c6e4be ("accel/tcg: Add cluster number to TCG TB

hash")

include/exec/tb-lookup.h tb_lookup__cpu_state(CPUState *cpu, target_ulong *pc,

Quoted 24 lines

---
Does anybody know why tb_lookup__cpu_state() has that odd double-underscore in the middle of its name?

Since the jmp_cache is per-vcpu we know that we're always going to match on the cluster_index, so the other option would be to leave the cluster_index bits out of the comparison, and leave the "fold in cluster index to cf_mask" code in tb_htable_lookup(). Or we could require the callers of tb_lookup__cpu_state() to all provide the cluster index, but that's more places to change, so I prefer this.

---
 include/exec/tb-lookup.h | 4 ++++
 accel/tcg/cpu-exec.c     | 3 ---
 2 files changed, 4 insertions(+), 3 deletions(-)
include/exec/tb-lookup.h +0 -0
index 492cb682894..26921b6dafd 100644
@@ -28,6 +28,10 @@ tb_lookup__cpu_state(CPUState *cpu, target_ulong *pc,

target_ulong *cs_base,

Quoted 28 lines

accel/tcg/cpu-exec.c TranslationBlock *tb_htable_lookup(CPUState *cpu,

Quoted 15 lines
include/exec/tb-lookup.h +4 -0
     cpu_get_tb_cpu_state(env, pc, cs_base, flags);
     hash = tb_jmp_cache_hash_func(*pc);
     tb = atomic_rcu_read(&cpu->tb_jmp_cache[hash]);
+
+    cf_mask &= ~CF_CLUSTER_MASK;
+    cf_mask |= cpu->cluster_index << CF_CLUSTER_SHIFT;
+
     if (likely(tb &&
                tb->pc == *pc &&
                tb->cs_base == *cs_base &&
accel/tcg/cpu-exec.c +0 -0
index 7cf1292546f..60d87d5a19b 100644
@@ -325,9 +325,6 @@ TranslationBlock *tb_htable_lookup(CPUState *cpu,

target_ulong pc,

Quoted 10 lines
accel/tcg/cpu-exec.c +0 -3
     struct tb_desc desc;
     uint32_t h;
 
-    cf_mask &= ~CF_CLUSTER_MASK;
-    cf_mask |= cpu->cluster_index << CF_CLUSTER_SHIFT;
-
     desc.env = (CPUArchState *)cpu->env_ptr;
     desc.cs_base = cs_base;
     desc.flags = flags;

Confirmed, both Mac OS 9.2 and OS X 10.4 running with qemu-system-ppc are back to their old performance levels.

Best, and thanks,
Howard

Reviewed-by on v1 1/1
#4 lore raw
In reply to Peter Maydell
Quoted 20 lines · Peter Maydell

In commit f7b78602fdc6c6e4be we added the CPU cluster number to the cflags field of the TB hash; this included adding it to the value kept in tb->cflags, since we pass that field directly into the hash calculation in some places. Unfortunately we forgot to check whether other parts of the code were doing comparisons against tb->cflags that would need to be updated.

It turns out that there is exactly one such place: the tb_lookup__cpu_state() function checks whether the TB it has found in the tb_jmp_cache has a tb->cflags matching the cf_mask that is passed in. The tb->cflags has the cluster_index in it but the cf_mask does not.

Hoist the "add cluster index to the cf_mask" code up from tb_htable_lookup() to tb_lookup__cpu_state() so it can be considered in the "did this TB match in the jmp cache" condition, as well as when we do the full hash lookup by physical PC, flags, etc. (tb_htable_lookup() is only called from tb_lookup__cpu_state(), so this change doesn't require any further knock-on changes.)

Quoted 4 lines
Fixes f7b78602fdc6c6e4be ("accel/tcg: Add cluster number to TCG TB hash")
Quoted 2 lines

Does anybody know why tb_lookup__cpu_state() has that odd double-underscore in the middle of its name?

I'm inclined to think typo... Emilio?

r~

#5 lore raw
Quoted 4 lines · Richard Henderson
Quoted 2 lines

Does anybody know why tb_lookup__cpu_state() has that odd double-underscore in the middle of its name?

I'm inclined to think typo... Emilio?

It's not a typo -- it's there to separate "tb lookup" and "cpu state" to avoid ambiguity when guessing what the function does based on its name.

Other projects, such as the kernel, make extensive use of double underscores for this purpose. In fact, I believe I picked up the habit from reading kernel code.

		E.
#6 lore raw
Quoted 10 lines · Emilio G. Cota
Quoted 4 lines · Richard Henderson
Quoted 2 lines

Does anybody know why tb_lookup__cpu_state() has that odd double-underscore in the middle of its name?

I'm inclined to think typo... Emilio?

It's not a typo -- it's there to separate "tb lookup" and "cpu state" to avoid ambiguity when guessing what the function does based on its name.

OK, I wouldn't have guessed that -- my assumption was "accidentally removed a name component with sed" or "just a typo", and I didn't assign any semantic meaning to the double underscore...

thanks
-- PMM

#7 lore raw
In reply to Peter Maydell
Quoted 20 lines · Peter Maydell

In commit f7b78602fdc6c6e4be we added the CPU cluster number to the cflags field of the TB hash; this included adding it to the value kept in tb->cflags, since we pass that field directly into the hash calculation in some places. Unfortunately we forgot to check whether other parts of the code were doing comparisons against tb->cflags that would need to be updated.

It turns out that there is exactly one such place: the tb_lookup__cpu_state() function checks whether the TB it has found in the tb_jmp_cache has a tb->cflags matching the cf_mask that is passed in. The tb->cflags has the cluster_index in it but the cf_mask does not.

Hoist the "add cluster index to the cf_mask" code up from tb_htable_lookup() to tb_lookup__cpu_state() so it can be considered in the "did this TB match in the jmp cache" condition, as well as when we do the full hash lookup by physical PC, flags, etc. (tb_htable_lookup() is only called from tb_lookup__cpu_state(), so this change doesn't require any further knock-on changes.)

Quoted 5 lines
Fixes f7b78602fdc6c6e4be ("accel/tcg: Add cluster number to TCG TB hash")

---

Queued, thanks.

r~