some TCG fixes
cover some TCG fixes
The first patch we've seen before -- I'm taking it from the atomic interrupt_request series.
The other three patches are related to TCG profiling. One of them is a build fix that I suspect has gone unnoticed due to its dependence on CONFIG_PROFILER.
The series is checkpatch-clean. You can fetch it from:
https://github.com/cota/qemu/tree/tcg-profile
Thanks,
Emilio
PATCH 1/4 tcg: access cpu->icount_decr.u16.high with atomics
Consistently access u16.high with atomics to avoid undefined behaviour in MTTCG.
Note that icount_decr.u16.low is only used in icount mode, so regular accesses to it are OK.
--- accel/tcg/tcg-all.c | 2 +- accel/tcg/translate-all.c | 2 +- qom/cpu.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
accel/tcg/tcg-all.c +1 -1
@@ -51,7 +51,7 @@ static void tcg_handle_interrupt(CPUState *cpu, int mask)if (!qemu_cpu_is_self (cpu)) {qemu_cpu_kick (cpu); }else { - cpu->icount_decr.u16.high =-1 ; +atomic_set (&cpu->icount_decr.u16.high,-1 );if (use_icount && !cpu->can_do_io && (mask & ~old_mask) !=0 ) {
accel/tcg/translate-all.c +1 -1
@@ -2341,7 +2341,7 @@ void cpu_interrupt(CPUState *cpu, int mask) {g_assert (qemu_mutex_iothread_locked ()); cpu->interrupt_request |= mask; - cpu->icount_decr.u16.high =-1 ; +atomic_set (&cpu->icount_decr.u16.high,-1 ); }/*
qom/cpu.c +1 -1
@@ -266,7 +266,7 @@ static void cpu_common_reset(CPUState *cpu) cpu->mem_io_pc =0 ; cpu->mem_io_vaddr =0 ; cpu->icount_extra =0 ; - cpu->icount_decr.u32 =0 ; +atomic_set (&cpu->icount_decr.u32,0 ); cpu->can_do_io =1 ; cpu->exception_index =-1 ; cpu->crash_occurred = false;
Signature · 1 line
2.17.1
PATCH 2/4 tcg: fix use of uninitialized variable under CONFIG_PROFILER
In reply to Emilio G. CotaWe forgot to initialize n in commit 15fa08f845 ("tcg: Dynamically allocate TCGOps", 2017-12-29).
--- tcg/tcg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/tcg.c +1 -1
@@ -3430,7 +3430,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)#ifdef CONFIG_PROFILER { -int n; +int n =0 ; QTAILQ_FOREACH(op, &s->ops, link) { n++;
Signature · 1 line
2.17.1
Re: [Qemu-devel] [PATCH 2/4] tcg: fix use of uninitialized variable under CONFIG_PROFILER
Quoted 4 lines · Emilio G. Cota
We forgot to initialize n in commit
15fa08f845("tcg: Dynamically allocate TCGOps", 2017-12-29).Signed-off-by Emilio G. Cota <[email protected]>
Quoted 18 lines
--- tcg/tcg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tcg/tcg.c +1
-1@@ -3430,7 +3430,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)#ifdef CONFIG_PROFILER { -int n; +int n =0 ; QTAILQ_FOREACH(op, &s->ops, link) { n++;
PATCH 3/4 tcg: plug holes in struct TCGProfile
In reply to Emilio G. CotaThis plugs two 4-byte holes in 64-bit.
--- tcg/tcg.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/tcg.h +1 -1
@@ -633,8 +633,8 @@ typedef struct TCGProfile {int64_t tb_count;int64_t op_count;/* total insn count */ int op_count_max;/* max insn per TB */ -int64_t temp_count;int temp_count_max; +int64_t temp_count;int64_t del_op_count;int64_t code_in_len;int64_t code_out_len;
Signature · 1 line
2.17.1
PATCH 4/4 tcg: distribute tcg_time into TCG contexts
In reply to Emilio G. CotaWhen we implemented per-vCPU TCG contexts, we forgot to also distribute the tcg_time counter, which has remained as a global accessed without any serialization, leading to potentially missed counts.
Fix it by distributing the field over the TCG contexts, embedding it into TCGProfile with a field called "cpu_exec_time", which is more descriptive than "tcg_time". Add a function to query this value directly, and for completeness, fill in the field in tcg_profile_snapshot, even though its callers do not use it.
--- include/qemu/timer.h | 1 - tcg/tcg.h | 2 ++ cpus.c | 3 ++- monitor.c | 13 ++++++++++--- tcg/tcg.c | 23 +++++++++++++++++++++++ 5 files changed, 37 insertions(+), 5 deletions(-)
include/qemu/timer.h +0 -1
@@ -1046,7 +1046,6 @@ static inline int64_t profile_getclock(void) return get_clock(); } -extern int64_t tcg_time;extern int64_t dev_time;#endif
tcg/tcg.h +2 -0
@@ -629,6 +629,7 @@ typedef struct TCGOp {QEMU_BUILD_BUG_ON (NB_OPS > (1 <<8 )); typedef struct TCGProfile { + int64_t cpu_exec_time; int64_t tb_count1; int64_t tb_count; int64_t op_count; /* total insn count */
@@ -1002,6 +1003,7 @@ int tcg_check_temp_count(void);#define tcg_check_temp_count () 0#endif +int64_t tcg_cpu_exec_time (void );void tcg_dump_info (FILE *f,fprintf_function cpu_fprintf);void tcg_dump_op_count (FILE *f,fprintf_function cpu_fprintf);
cpus.c +2 -1
@@ -1425,7 +1425,8 @@ static int tcg_cpu_exec(CPUState *cpu) ret =cpu_exec (cpu);cpu_exec_end (cpu);#ifdef CONFIG_PROFILER - tcg_time +=profile_getclock () - ti; +atomic_set (&tcg_ctx->prof.cpu_exec_time, + tcg_ctx->prof.cpu_exec_time +profile_getclock () - ti);#endif return ret; }
monitor.c +10 -3
@@ -83,6 +83,7 @@#include "sysemu/cpus.h" #include "sysemu/iothread.h" #include "qemu/cutils.h" +#include "tcg/tcg.h" #if defined(TARGET_S390X )#include "hw/s390x/storage-keys.h"
@@ -1966,16 +1967,22 @@ static void hmp_info_numa(Monitor *mon, const QDict *qdict)#ifdef CONFIG_PROFILER -int64_t tcg_time;int64_t dev_time;static void hmp_info_profile (Monitor *mon,const QDict *qdict) { +static int64_t last_cpu_exec_time; +int64_t cpu_exec_time; +int64_t delta; + + cpu_exec_time =tcg_cpu_exec_time (); + delta = cpu_exec_time - last_cpu_exec_time; +monitor_printf (mon,"async time %" PRId64" (%0.3f)\n" , dev_time, dev_time / (double )NANOSECONDS_PER_SECOND );monitor_printf (mon,"qemu time %" PRId64" (%0.3f)\n" , - tcg_time, tcg_time / (double )NANOSECONDS_PER_SECOND ); - tcg_time =0 ; + delta, delta / (double )NANOSECONDS_PER_SECOND ); + last_cpu_exec_time = cpu_exec_time; dev_time =0 ; }#else
tcg/tcg.c +23 -0
@@ -30,6 +30,7 @@/* Define to jump the ELF file used to communicate with GDB. */ #undef DEBUG_JIT +#include "qemu/error-report.h" #include "qemu/cutils.h" #include "qemu/host-utils.h" #include "qemu/timer.h"
@@ -3361,6 +3362,7 @@ void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)const TCGProfile *orig = &s->prof;if (counters) { +PROF_ADD (prof, orig, cpu_exec_time);PROF_ADD (prof, orig, tb_count1);PROF_ADD (prof, orig, tb_count);PROF_ADD (prof, orig, op_count);
@@ -3412,11 +3414,32 @@ void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf) prof.table_op_count[i]); } } + +int64_t tcg_cpu_exec_time (void ) +{ +unsigned int n_ctxs =atomic_read (&n_tcg_ctxs); +unsigned int i; +int64_t ret =0 ; + +for (i =0 ; i < n_ctxs; i++) { +const TCGContext *s =atomic_read (&tcg_ctxs[i]); +const TCGProfile *prof = &s->prof; + + ret +=atomic_read (&prof->cpu_exec_time); + } +return ret; +}#else void tcg_dump_op_count (FILE *f,fprintf_function cpu_fprintf) {cpu_fprintf (f,"[TCG profiler not compiled]\n" ); } + +int64_t tcg_cpu_exec_time (void ) +{ +error_report ("%s: TCG profiler not compiled" , __func__); +exit (EXIT_FAILURE ); +}#endif
Signature · 1 line
2.17.1
Richard Henderson
AppliedQuoted 9 lines · Emilio G. Cota
The first patch we've seen before -- I'm taking it from the atomic
interrupt_requestseries.The other three patches are related to TCG profiling. One of them is a build fix that I suspect has gone unnoticed due to its dependence on
CONFIG_PROFILER.The series is checkpatch-clean. You can fetch it from:
https://github.com/cota/qemu/tree/tcg-profile
Queued, thanks.
r~