[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v1 2/8] cpu: cache CPUClass in CPUState for hot code paths
From: |
Alex Bennée |
Subject: |
[PATCH v1 2/8] cpu: cache CPUClass in CPUState for hot code paths |
Date: |
Thu, 11 Aug 2022 16:14:07 +0100 |
The class cast checkers are quite expensive and always on (unlike the
dynamic case who's checks are gated by CONFIG_QOM_CAST_DEBUG). To
avoid the overhead of repeatedly checking something which should never
change we cache the CPUClass reference for use in the hot code paths.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
include/hw/core/cpu.h | 9 +++++++++
cpu.c | 9 ++++-----
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index 500503da13..1a7e1a9380 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -51,6 +51,13 @@ typedef int (*WriteCoreDumpFunction)(const void *buf, size_t
size,
*/
#define CPU(obj) ((CPUState *)(obj))
+/*
+ * The class checkers bring in CPU_GET_CLASS() which is potentially
+ * expensive given the eventual call to
+ * object_class_dynamic_cast_assert(). Because of this the CPUState
+ * has a cached value for the class in cs->cc which is set up in
+ * cpu_exec_realizefn() for use in hot code paths.
+ */
typedef struct CPUClass CPUClass;
DECLARE_CLASS_CHECKERS(CPUClass, CPU,
TYPE_CPU)
@@ -317,6 +324,8 @@ struct qemu_work_item;
struct CPUState {
/*< private >*/
DeviceState parent_obj;
+ /* cache to avoid expensive CPU_GET_CLASS */
+ CPUClass *cc;
/*< public >*/
int nr_cores;
diff --git a/cpu.c b/cpu.c
index 584ac78baf..14365e36f3 100644
--- a/cpu.c
+++ b/cpu.c
@@ -131,9 +131,8 @@ const VMStateDescription vmstate_cpu_common = {
void cpu_exec_realizefn(CPUState *cpu, Error **errp)
{
-#ifndef CONFIG_USER_ONLY
- CPUClass *cc = CPU_GET_CLASS(cpu);
-#endif
+ /* cache the cpu class for the hotpath */
+ cpu->cc = CPU_GET_CLASS(cpu);
cpu_list_add(cpu);
if (!accel_cpu_realizefn(cpu, errp)) {
@@ -151,8 +150,8 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
vmstate_register(NULL, cpu->cpu_index, &vmstate_cpu_common, cpu);
}
- if (cc->sysemu_ops->legacy_vmsd != NULL) {
- vmstate_register(NULL, cpu->cpu_index, cc->sysemu_ops->legacy_vmsd,
cpu);
+ if (cpu->cc->sysemu_ops->legacy_vmsd != NULL) {
+ vmstate_register(NULL, cpu->cpu_index,
cpu->cc->sysemu_ops->legacy_vmsd, cpu);
}
#endif /* CONFIG_USER_ONLY */
}
--
2.30.2
- [PATCH for 7.1 v1 0/8] memory leaks and speed tweaks, Alex Bennée, 2022/08/11
- [PATCH v1 4/8] cputlb: used cached CPUClass in our hot-paths, Alex Bennée, 2022/08/11
- [PATCH v1 3/8] hw/core/cpu-sysemu: used cached class in cpu_asidx_from_attrs, Alex Bennée, 2022/08/11
- [PATCH v1 7/8] tests/avocado: apply a band aid to aspeed-evb login, Alex Bennée, 2022/08/11
- [PATCH v1 2/8] cpu: cache CPUClass in CPUState for hot code paths,
Alex Bennée <=
- [PATCH v1 5/8] ssi: cache SSIPeripheralClass to avoid GET_CLASS(), Alex Bennée, 2022/08/11
- [PATCH v1 1/8] linux-user: un-parent OBJECT(cpu) when closing thread, Alex Bennée, 2022/08/11
- [PATCH v1 6/8] tests/avocado: add timeout to the aspeed tests, Alex Bennée, 2022/08/11
- [PATCH v1 8/8] accel/tcg: remove trace_vcpu_dstate TB checking, Alex Bennée, 2022/08/11