Skip to contentSkip to conversation

TCG code quality tracking

PATCH Superseded Fei Wu 5 participants 47 messages May 30, 2023 – Jun 12, 2023 Arm/Amlogic Meson SoC support

v14 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Fei Wu

author
#0 lore raw

v14 cover TCG code quality tracking

v14
---
* cleanup TCGProfile in patch 04

Alex Bennée (1):
tb-stats: reset the tracked TBs on a tb_flush

Fei Wu (3):
accel/tcg: remove CONFIG_PROFILER accel/tcg: add jit stats and time to TBStatistics
docs: add tb-stats how to

Vanderson M. do Rosario (6):
accel/tcg: introduce TBStatistics structure
accel: collecting TB execution count
debug: add -d tb_stats to control TBStatistics collection:
monitor: adding tb_stats hmp command
Adding info [tb-list|tb] commands to HMP (WIP)
tb-stats: dump hot TBs at the end of the execution

 MAINTAINERS                   |   1 +
 accel/tcg/cpu-exec.c          |   6 +
 accel/tcg/meson.build         |   1 +
 accel/tcg/monitor.c           | 122 +++++-
 accel/tcg/tb-context.h        |   1 +
 accel/tcg/tb-hash.h           |   7 +
 accel/tcg/tb-maint.c          |  20 +
 accel/tcg/tb-stats.c          | 692 ++++++++++++++++++++++++++++++++++
 accel/tcg/tcg-accel-ops.c     |  15 +-
 accel/tcg/tcg-runtime.c       |   1 +
 accel/tcg/translate-all.c     | 143 +++++--
 accel/tcg/translator.c        |  30 ++
 disas/disas.c                 |  26 +-
 docs/devel/tcg-tbstats.rst    | 129 +++++++
 hmp-commands-info.hx          |  16 +
 hmp-commands.hx               |  16 +
 include/exec/exec-all.h       |   3 +
 include/exec/gen-icount.h     |   2 +
 include/exec/tb-stats-dump.h  |  21 ++
 include/exec/tb-stats-flags.h |  34 ++
 include/exec/tb-stats.h       | 164 ++++++++
 include/monitor/hmp.h         |   3 +
 include/qemu/log-for-trace.h  |   6 +-
 include/qemu/log.h            |   3 +
 include/qemu/timer.h          |   5 +-
 include/tcg/tcg.h             |  41 +-
 linux-user/exit.c             |   2 +
 meson.build                   |   2 -
 meson_options.txt             |   2 -
 scripts/meson-buildoptions.sh |   3 -
 softmmu/runstate.c            |  10 +-
 stubs/meson.build             |   1 +
 stubs/tb-stats.c              |  32 ++
 tcg/tcg.c                     | 230 +++--------
 tests/qtest/qmp-cmd-test.c    |   2 +-
 util/log.c                    | 103 ++++-
 36 files changed, 1601 insertions(+), 294 deletions(-)
 create mode 100644 accel/tcg/tb-stats.c
 create mode 100644 docs/devel/tcg-tbstats.rst
 create mode 100644 include/exec/tb-stats-dump.h
 create mode 100644 include/exec/tb-stats-flags.h
 create mode 100644 include/exec/tb-stats.h
 create mode 100644 stubs/tb-stats.c
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v14 1/10 accel/tcg: remove CONFIG_PROFILER

TBStats will be introduced to replace CONFIG_PROFILER totally, here remove all CONFIG_PROFILER related stuffs first.

---
 accel/tcg/monitor.c           |  25 ----
 accel/tcg/tcg-accel-ops.c     |  10 --
 accel/tcg/translate-all.c     |  33 ------
 include/qemu/timer.h          |   9 --
 include/tcg/tcg.h             |  25 ----
 meson.build                   |   2 -
 meson_options.txt             |   2 -
 scripts/meson-buildoptions.sh |   3 -
 softmmu/runstate.c            |   9 --
 tcg/tcg.c                     | 208 ----------------------------------
 tests/qtest/qmp-cmd-test.c    |   3 -
 11 files changed, 329 deletions(-)
accel/tcg/monitor.c +0 -25
index 92fce580f1..e903dd1d2e 100644
@@ -80,36 +80,11 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
     return human_readable_text_from_str(buf);
 }
 
-#ifdef CONFIG_PROFILER
-
-int64_t dev_time;
-
-HumanReadableText *qmp_x_query_profile(Error **errp)
-{
-    g_autoptr(GString) buf = g_string_new("");
-    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;
-
-    g_string_append_printf(buf, "async time  %" PRId64 " (%0.3f)\n",
-                           dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
-    g_string_append_printf(buf, "qemu time   %" PRId64 " (%0.3f)\n",
-                           delta, delta / (double)NANOSECONDS_PER_SECOND);
-    last_cpu_exec_time = cpu_exec_time;
-    dev_time = 0;
-
-    return human_readable_text_from_str(buf);
-}
-#else
 HumanReadableText *qmp_x_query_profile(Error **errp)
 {
     error_setg(errp, "Internal profiler not compiled");
     return NULL;
 }
-#endif
 
 static void hmp_tcg_register(void)
 {
accel/tcg/tcg-accel-ops.c +0 -10
index 58c8e64096..3973591508 100644
@@ -70,20 +70,10 @@ void tcg_cpus_destroy(CPUState *cpu)
 int tcg_cpus_exec(CPUState *cpu)
 {
     int ret;
-#ifdef CONFIG_PROFILER
-    int64_t ti;
-#endif
     assert(tcg_enabled());
-#ifdef CONFIG_PROFILER
-    ti = profile_getclock();
-#endif
     cpu_exec_start(cpu);
     ret = cpu_exec(cpu);
     cpu_exec_end(cpu);
-#ifdef CONFIG_PROFILER
-    qatomic_set(&tcg_ctx->prof.cpu_exec_time,
-                tcg_ctx->prof.cpu_exec_time + profile_getclock() - ti);
-#endif
     return ret;
 }
 
accel/tcg/translate-all.c +0 -33
index c87648b99e..4e035e0f79 100644
@@ -203,10 +203,6 @@ void cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb,
                                uintptr_t host_pc)
 {
     uint64_t data[TARGET_INSN_START_WORDS];
-#ifdef CONFIG_PROFILER
-    TCGProfile *prof = &tcg_ctx->prof;
-    int64_t ti = profile_getclock();
-#endif
     int insns_left = cpu_unwind_data_from_tb(tb, host_pc, data);
 
     if (insns_left < 0) {
@@ -223,12 +219,6 @@ void cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb,
     }
 
     cpu->cc->tcg_ops->restore_state_to_opc(cpu, tb, data);
-
-#ifdef CONFIG_PROFILER
-    qatomic_set(&prof->restore_time,
-                prof->restore_time + profile_getclock() - ti);
-    qatomic_set(&prof->restore_count, prof->restore_count + 1);
-#endif
 }
 
 bool cpu_restore_state(CPUState *cpu, uintptr_t host_pc)
@@ -291,13 +281,6 @@ static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb,
     tcg_ctx->cpu = NULL;
     *max_insns = tb->icount;
 
-#ifdef CONFIG_PROFILER
-    qatomic_set(&tcg_ctx->prof.tb_count, tcg_ctx->prof.tb_count + 1);
-    qatomic_set(&tcg_ctx->prof.interm_time,
-                tcg_ctx->prof.interm_time + profile_getclock() - *ti);
-    *ti = profile_getclock();
-#endif
-
     return tcg_gen_code(tcg_ctx, tb, pc);
 }
 
@@ -311,9 +294,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
     tb_page_addr_t phys_pc;
     tcg_insn_unit *gen_code_buf;
     int gen_code_size, search_size, max_insns;
-#ifdef CONFIG_PROFILER
-    TCGProfile *prof = &tcg_ctx->prof;
-#endif
     int64_t ti;
     void *host_pc;
 
@@ -365,12 +345,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
 
  tb_overflow:
 
-#ifdef CONFIG_PROFILER
-    /* includes aborted translations because of exceptions */
-    qatomic_set(&prof->tb_count1, prof->tb_count1 + 1);
-    ti = profile_getclock();
-#endif
-
     trace_translate_block(tb, pc, tb->tc.ptr);
 
     gen_code_size = setjmp_gen_code(env, tb, pc, host_pc, &max_insns, &ti);
@@ -425,13 +399,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
      */
     perf_report_code(pc, tb, tcg_splitwx_to_rx(gen_code_buf));
 
-#ifdef CONFIG_PROFILER
-    qatomic_set(&prof->code_time, prof->code_time + profile_getclock() - ti);
-    qatomic_set(&prof->code_in_len, prof->code_in_len + tb->size);
-    qatomic_set(&prof->code_out_len, prof->code_out_len + gen_code_size);
-    qatomic_set(&prof->search_out_len, prof->search_out_len + search_size);
-#endif
-
     if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM) &&
         qemu_log_in_addr_range(pc)) {
         FILE *logfile = qemu_log_trylock();
include/qemu/timer.h +0 -9
index ee071e07d1..9a91cb1248 100644
@@ -989,13 +989,4 @@ static inline int64_t cpu_get_host_ticks(void)
 }
 #endif
 
-#ifdef CONFIG_PROFILER
-static inline int64_t profile_getclock(void)
-{
-    return get_clock();
-}
-
-extern int64_t dev_time;
-#endif
-
 #endif
include/tcg/tcg.h +0 -25
index 072c35f7f5..083cbd6580 100644
@@ -522,27 +522,6 @@ static inline TCGRegSet output_pref(const TCGOp *op, unsigned i)
     return i < ARRAY_SIZE(op->output_pref) ? op->output_pref[i] : 0;
 }
 
-typedef struct TCGProfile {
-    int64_t cpu_exec_time;
-    int64_t tb_count1;
-    int64_t tb_count;
-    int64_t op_count; /* total insn count */
-    int op_count_max; /* max insn per TB */
-    int temp_count_max;
-    int64_t temp_count;
-    int64_t del_op_count;
-    int64_t code_in_len;
-    int64_t code_out_len;
-    int64_t search_out_len;
-    int64_t interm_time;
-    int64_t code_time;
-    int64_t la_time;
-    int64_t opt_time;
-    int64_t restore_count;
-    int64_t restore_time;
-    int64_t table_op_count[NB_OPS];
-} TCGProfile;
-
 struct TCGContext {
     uint8_t *pool_cur, *pool_end;
     TCGPool *pool_first, *pool_current, *pool_first_large;
@@ -569,10 +548,6 @@ struct TCGContext {
     tcg_insn_unit *code_buf;      /* pointer for start of tb */
     tcg_insn_unit *code_ptr;      /* pointer for running end of tb */
 
-#ifdef CONFIG_PROFILER
-    TCGProfile prof;
-#endif
-
 #ifdef CONFIG_DEBUG_TCG
     int goto_tb_issue_mask;
     const TCGOpcode *vecop_list;
meson.build +0 -2
index 2d48aa1e2e..f08280bc8b 100644
@@ -2141,7 +2141,6 @@ if numa.found()
                                        dependencies: numa))
 endif
 config_host_data.set('CONFIG_OPENGL', opengl.found())
-config_host_data.set('CONFIG_PROFILER', get_option('profiler'))
 config_host_data.set('CONFIG_RBD', rbd.found())
 config_host_data.set('CONFIG_RDMA', rdma.found())
 config_host_data.set('CONFIG_SAFESTACK', get_option('safe_stack'))
@@ -4110,7 +4109,6 @@ if 'objc' in all_languages
   summary_info += {'QEMU_OBJCFLAGS':    ' '.join(qemu_common_flags)}
 endif
 summary_info += {'QEMU_LDFLAGS':      ' '.join(qemu_ldflags)}
-summary_info += {'profiler':          get_option('profiler')}
 summary_info += {'link-time optimization (LTO)': get_option('b_lto')}
 summary_info += {'PIE':               get_option('b_pie')}
 summary_info += {'static build':      config_host.has_key('CONFIG_STATIC')}
meson_options.txt +0 -2
index 90237389e2..bbb5c7e886 100644
@@ -345,8 +345,6 @@ option('qom_cast_debug', type: 'boolean', value: true,
 option('gprof', type: 'boolean', value: false,
        description: 'QEMU profiling with gprof',
        deprecated: true)
-option('profiler', type: 'boolean', value: false,
-       description: 'profiler support')
 option('slirp_smbd', type : 'feature', value : 'auto',
        description: 'use smbd (at path --smbd=*) in slirp networking')
 
scripts/meson-buildoptions.sh +0 -3
index 5714fd93d9..7dd5709ef4 100644
@@ -39,7 +39,6 @@ meson_options_help() {
   printf "%s\n" '                           jemalloc/system/tcmalloc)'
   printf "%s\n" '  --enable-module-upgrades try to load modules from alternate paths for'
   printf "%s\n" '                           upgrades'
-  printf "%s\n" '  --enable-profiler        profiler support'
   printf "%s\n" '  --enable-rng-none        dummy RNG, avoid using /dev/(u)random and'
   printf "%s\n" '                           getrandom()'
   printf "%s\n" '  --enable-safe-stack      SafeStack Stack Smash Protection (requires'
@@ -401,8 +400,6 @@ _meson_option_parse() {
     --with-pkgversion=*) quote_sh "-Dpkgversion=$2" ;;
     --enable-png) printf "%s" -Dpng=enabled ;;
     --disable-png) printf "%s" -Dpng=disabled ;;
-    --enable-profiler) printf "%s" -Dprofiler=true ;;
-    --disable-profiler) printf "%s" -Dprofiler=false ;;
     --enable-pvrdma) printf "%s" -Dpvrdma=enabled ;;
     --disable-pvrdma) printf "%s" -Dpvrdma=disabled ;;
     --enable-qcow1) printf "%s" -Dqcow1=enabled ;;
softmmu/runstate.c +0 -9
index 2f2396c819..bd50062ed0 100644
@@ -728,18 +728,9 @@ static bool main_loop_should_exit(int *status)
 int qemu_main_loop(void)
 {
     int status = EXIT_SUCCESS;
-#ifdef CONFIG_PROFILER
-    int64_t ti;
-#endif
 
     while (!main_loop_should_exit(&status)) {
-#ifdef CONFIG_PROFILER
-        ti = profile_getclock();
-#endif
         main_loop_wait(false);
-#ifdef CONFIG_PROFILER
-        dev_time += profile_getclock() - ti;
-#endif
     }
 
     return status;
tcg/tcg.c +0 -208

262 lines not on this page · raw message

tests/qtest/qmp-cmd-test.c +0 -3
index a58de48d2a..73a670e8fa 100644
@@ -46,9 +46,6 @@ static int query_error_class(const char *cmd)
         { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
         { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
         { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
-#ifndef CONFIG_PROFILER
-        { "x-query-profile", ERROR_CLASS_GENERIC_ERROR },
-#endif
         /* Only valid with a USB bus added */
         { "x-query-usb", ERROR_CLASS_GENERIC_ERROR },
         /* Only valid with accel=tcg */
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v14 2/10 accel/tcg: introduce TBStatistics structure

In reply to Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

To store statistics for each TB, we created a TBStatistics structure which is linked with the TBs. TBStatistics can stay alive after tb_flush and be relinked to a regenerated TB. So the statistics can be accumulated even through flushes.

The goal is to have all present and future qemu/tcg statistics and meta-data stored in this new structure.

Message-Id: <[email protected]>

[AJB: fix git author, review comments]
---
 MAINTAINERS                   |  1 +
 accel/tcg/meson.build         |  1 +
 accel/tcg/tb-context.h        |  1 +
 accel/tcg/tb-hash.h           |  7 +++++
 accel/tcg/tb-maint.c          | 19 ++++++++++++
 accel/tcg/tb-stats.c          | 58 +++++++++++++++++++++++++++++++++++
 accel/tcg/translate-all.c     | 43 ++++++++++++++++++++++++++
 include/exec/exec-all.h       |  3 ++
 include/exec/tb-stats-flags.h | 21 +++++++++++++
 include/exec/tb-stats.h       | 56 +++++++++++++++++++++++++++++++++
 10 files changed, 210 insertions(+)
 create mode 100644 accel/tcg/tb-stats.c
 create mode 100644 include/exec/tb-stats-flags.h
 create mode 100644 include/exec/tb-stats.h
MAINTAINERS +1 -0
index 4b025a7b63..d72ecd12b4 100644
@@ -153,6 +153,7 @@ F: include/exec/cpu*.h
 F: include/exec/exec-all.h
 F: include/exec/tb-flush.h
 F: include/exec/target_long.h
+F: include/exec/tb-stats*.h
 F: include/exec/helper*.h
 F: include/sysemu/cpus.h
 F: include/sysemu/tcg.h
accel/tcg/meson.build +1 -0
index aeb20a6ef0..9263bdde11 100644
@@ -4,6 +4,7 @@ tcg_ss.add(files(
   'cpu-exec-common.c',
   'cpu-exec.c',
   'tb-maint.c',
+  'tb-stats.c',
   'tcg-runtime-gvec.c',
   'tcg-runtime.c',
   'translate-all.c',
accel/tcg/tb-context.h +1 -0
index cac62d9749..d7910d586b 100644
@@ -35,6 +35,7 @@ struct TBContext {
     /* statistics */
     unsigned tb_flush_count;
     unsigned tb_phys_invalidate_count;
+    struct qht tb_stats;
 };
 
 extern TBContext tb_ctx;
accel/tcg/tb-hash.h +7 -0
index 83dc610e4c..87d657a1c6 100644
@@ -67,4 +67,11 @@ uint32_t tb_hash_func(tb_page_addr_t phys_pc, target_ulong pc, uint32_t flags,
     return qemu_xxhash7(phys_pc, pc, flags, cf_mask, trace_vcpu_dstate);
 }
 
+static inline
+uint32_t tb_stats_hash_func(tb_page_addr_t phys_pc, target_ulong pc,
+                            uint32_t flags)
+{
+    return qemu_xxhash5(phys_pc, pc, flags);
+}
+
 #endif
accel/tcg/tb-maint.c +19 -0
index 991746f80f..0980fca358 100644
@@ -24,6 +24,7 @@
 #include "exec/log.h"
 #include "exec/exec-all.h"
 #include "exec/tb-flush.h"
+#include "exec/tb-stats.h"
 #include "exec/translate-all.h"
 #include "sysemu/tcg.h"
 #include "tcg/tcg.h"
@@ -41,6 +42,23 @@
 #define TB_FOR_EACH_JMP(head_tb, tb, n)                                 \
     TB_FOR_EACH_TAGGED((head_tb)->jmp_list_head, tb, n, jmp_list_next)
 
+/*
+ * This is the more or less the same compare as tb_cmp(), but the
+ * data persists over tb_flush. We also aggregate the various
+ * variations of cflags under one record and ignore the details of
+ * page overlap (although we can count it).
+ */
+bool tb_stats_cmp(const void *ap, const void *bp)
+{
+    const TBStatistics *a = ap;
+    const TBStatistics *b = bp;
+
+    return a->phys_pc == b->phys_pc &&
+        a->pc == b->pc &&
+        a->cs_base == b->cs_base &&
+        a->flags == b->flags;
+}
+
 static bool tb_cmp(const void *ap, const void *bp)
 {
     const TranslationBlock *a = ap;
@@ -60,6 +78,7 @@ void tb_htable_init(void)
     unsigned int mode = QHT_MODE_AUTO_RESIZE;
 
     qht_init(&tb_ctx.htable, tb_cmp, CODE_GEN_HTABLE_SIZE, mode);
+    init_tb_stats_htable();
 }
 
 typedef struct PageDesc PageDesc;
accel/tcg/tb-stats.c new file +58 -0

58 lines not on this page · raw message

accel/tcg/translate-all.c +43 -0

61 lines not on this page · raw message

include/exec/exec-all.h +3 -0
index 4d2b151986..f8a80f63d3 100644
@@ -608,6 +608,9 @@ struct TranslationBlock {
     uintptr_t jmp_list_head;
     uintptr_t jmp_list_next[2];
     uintptr_t jmp_dest[2];
+
+    /* Pointer to a struct where statistics from the TB is stored */
+    struct TBStatistics *tb_stats;
 };
 
 /* Hide the qatomic_read to make code a little easier on the eyes */
include/exec/tb-stats-flags.h new file +21 -0
new file mode 100644
index 0000000000..87ee3d902e
@@ -0,0 +1,21 @@
+/*
+ * QEMU System Emulator, Code Quality Monitor System
+ *
+ * We define the flags and control bits here to avoid complications of
+ * including TCG/CPU information in common code.
+ *
+ * Copyright (c) 2019 Vanderson M. do Rosario <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef TB_STATS_FLAGS
+#define TB_STATS_FLAGS
+
+/* TBStatistic collection controls */
+void enable_collect_tb_stats(void);
+void disable_collect_tb_stats(void);
+void pause_collect_tb_stats(void);
+bool tb_stats_collection_enabled(void);
+bool tb_stats_collection_paused(void);
+
+#endif
include/exec/tb-stats.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v14 02/10] accel/tcg: introduce TBStatistics structure

accel/tcg/translate-all.c TranslationBlock *tb_gen_code(CPUState *cpu, · line 388

Quoted 7 lines · Fei Wu
accel/tcg/translate-all.c +7 -0
+    /*
+     * We want to fetch the stats structure before we start code
+     * generation so we can count interesting things about this
+     * generation.
+     */
+    if (tb_stats_collection_enabled()) {
+        tb->tb_stats = tb_get_stats(phys_pc, pc, cs_base, flags);

If cflags & CF_PCREL, then 'pc' should not be cached or matched. Using 'phys_pc' will suffice, so pass 0 in that case.

r~

author
#4 lore raw

Re: [PATCH v14 02/10] accel/tcg: introduce TBStatistics structure

Quoted 9 lines · Richard Henderson
Quoted 7 lines · Fei Wu

+    /*
+     * We want to fetch the stats structure before we start code +     * generation so we can count interesting things about this +     * generation.
+     */
+    if (tb_stats_collection_enabled()) {
+        tb->tb_stats = tb_get_stats(phys_pc, pc, cs_base, flags);

Quoted 3 lines

If cflags & CF_PCREL, then 'pc' should not be cached or matched. Using 'phys_pc' will suffice, so pass 0 in that case.

OK, tb_get_stats(phys_pc, (cflags & CF_PCREL ? 0 : pc), cs_base, flags);

btw, is it possible to drop 'pc' even w/o CF_PCREL setting in cflags? Two TBs with different 'pc' but same 'phys_pc', are they the same?

Thanks,
Fei.

Quoted 2 lines

r~

#5 lore raw

Re: [PATCH v14 02/10] accel/tcg: introduce TBStatistics structure

Quoted 10 lines · Fei
Quoted 9 lines · Richard Henderson
Quoted 7 lines · Fei Wu

+    /*
+     * We want to fetch the stats structure before we start code +     * generation so we can count interesting things about this +     * generation.
+     */
+    if (tb_stats_collection_enabled()) {
+        tb->tb_stats = tb_get_stats(phys_pc, pc, cs_base, flags);

Quoted 7 lines
Quoted 3 lines

If cflags & CF_PCREL, then 'pc' should not be cached or matched. Using 'phys_pc' will suffice, so pass 0 in that case.

OK, tb_get_stats(phys_pc, (cflags & CF_PCREL ? 0 : pc), cs_base, flags);

btw, is it possible to drop 'pc' even w/o CF_PCREL setting in cflags? Two TBs with different 'pc' but same 'phys_pc', are they the same?

For the purpose of statistics, yes, plausibly.

Knowing how many different translations of the same bit of libc.so, for a guest that does not support CF_PCREL, could be revealing. In any case, you can get back to the virtual addresses via tb_stats->tbs[i]->pc, so long as tb_stats->tb[i].cflags & CF_PCREL is not set.

r~

#6 lore raw

Re: [PATCH v14 02/10] accel/tcg: introduce TBStatistics structure

In reply to Fei Wu

accel/tcg/tb-stats.c line 22

Quoted 7 lines · Fei Wu
accel/tcg/tb-stats.c new file +7 -0
+/* TBStatistic collection controls */
+enum TBStatsStatus {
+    TB_STATS_DISABLED = 0,
+    TB_STATS_RUNNING,
+    TB_STATS_PAUSED,
+    TB_STATS_STOPPED
+};

I don't see what PAUSED or STOPPED actually do.
As far as I can see, stats are either being collected or not: a boolean.

r~

author
#7 lore raw

Re: [PATCH v14 02/10] accel/tcg: introduce TBStatistics structure

Quoted 12 lines · Richard Henderson

accel/tcg/tb-stats.c line 22

Quoted 7 lines · Fei Wu
accel/tcg/tb-stats.c new file +7 -0
+/* TBStatistic collection controls */
+enum TBStatsStatus {
+    TB_STATS_DISABLED = 0,
+    TB_STATS_RUNNING,
+    TB_STATS_PAUSED,
+    TB_STATS_STOPPED
+};

I don't see what PAUSED or STOPPED actually do.
As far as I can see, stats are either being collected or not: a boolean.

If STOPPED, clean_tbstats() gets called, all the tbstats history is destroyed, but it's not for PAUSED.

Thanks,
Fei.

Quoted 2 lines

r~

#8 lore raw

Re: [PATCH v14 02/10] accel/tcg: introduce TBStatistics structure

Quoted 13 lines · Fei
Quoted 12 lines · Richard Henderson

accel/tcg/tb-stats.c line 22

Quoted 7 lines · Fei Wu
accel/tcg/tb-stats.c new file +7 -0
+/* TBStatistic collection controls */
+enum TBStatsStatus {
+    TB_STATS_DISABLED = 0,
+    TB_STATS_RUNNING,
+    TB_STATS_PAUSED,
+    TB_STATS_STOPPED
+};

I don't see what PAUSED or STOPPED actually do.
As far as I can see, stats are either being collected or not: a boolean.

Quoted 2 lines

If STOPPED, clean_tbstats() gets called, all the tbstats history is destroyed, but it's not for PAUSED.

But it doesn't PAUSE everything either, since (1) code is built into each tb, and (2) each tb->tb_stats->stats_enabled neither changed. Indeed, tb_stats_collection_enabled() is only checked in a fraction of the places stats are collected.

r~

author
#9 lore raw

Re: [PATCH v14 02/10] accel/tcg: introduce TBStatistics structure

Quoted 14 lines · Richard Henderson
Quoted 13 lines · Fei
Quoted 12 lines · Richard Henderson

accel/tcg/tb-stats.c line 22

Quoted 7 lines · Fei Wu
accel/tcg/tb-stats.c new file +7 -0
+/* TBStatistic collection controls */
+enum TBStatsStatus {
+    TB_STATS_DISABLED = 0,
+    TB_STATS_RUNNING,
+    TB_STATS_PAUSED,
+    TB_STATS_STOPPED
+};

I don't see what PAUSED or STOPPED actually do.
As far as I can see, stats are either being collected or not: a boolean.

Quoted 8 lines
Quoted 2 lines

If STOPPED, clean_tbstats() gets called, all the tbstats history is destroyed, but it's not for PAUSED.

But it doesn't PAUSE everything either, since (1) code is built into each tb, and (2) each tb->tb_stats->stats_enabled neither changed. Indeed, tb_stats_collection_enabled() is only checked in a fraction of the places stats are collected.

tbs are always flushed at the end of hmp tb-stats cmd by tb_flush(), stats_enabled can be changed e.g. during pause/filter cmd. It should be a bug if stats are collected w/o tb_stats_collection_enabled().

Thanks,
Fei.

Quoted 3 lines

r~

author
#10 lore raw

PATCH v14 3/10 accel: collecting TB execution count

In reply to Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

If a TB has a TBS (TBStatistics) with the TB_EXEC_STATS enabled, then we instrument the start code of this TB to atomically count the number of times it is executed. We count both the number of "normal" executions and atomic executions of a TB.

The execution count of the TB is stored in its respective TBS.

All TBStatistics are created by default with the flags from default_tbstats_flag.

[Richard Henderson created the inline gen_tb_exec_count]

Message-Id: <[email protected]>

[AJB: Fix author]
---
 accel/tcg/cpu-exec.c          |  6 ++++++
 accel/tcg/tb-stats.c          |  6 ++++++
 accel/tcg/tcg-runtime.c       |  1 +
 accel/tcg/translate-all.c     |  7 +++++--
 accel/tcg/translator.c        | 25 +++++++++++++++++++++++++
 include/exec/gen-icount.h     |  1 +
 include/exec/tb-stats-flags.h |  5 +++++
 include/exec/tb-stats.h       | 13 +++++++++++++
 8 files changed, 62 insertions(+), 2 deletions(-)
accel/tcg/cpu-exec.c +6 -0
index 0e741960da..c0d8f26237 100644
@@ -25,6 +25,7 @@
 #include "trace.h"
 #include "disas/disas.h"
 #include "exec/exec-all.h"
+#include "exec/tb-stats.h"
 #include "tcg/tcg.h"
 #include "qemu/atomic.h"
 #include "qemu/rcu.h"
@@ -562,7 +563,12 @@ void cpu_exec_step_atomic(CPUState *cpu)
             mmap_unlock();
         }
 
+        if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
+            tb->tb_stats->executions.atomic++;
+        }
+
         cpu_exec_enter(cpu);
+
         /* execute the generated code */
         trace_exec_tb(tb, pc);
         cpu_tb_exec(cpu, tb, &tb_exit);
accel/tcg/tb-stats.c +6 -0
index f988bd8a31..143a52ef5c 100644
@@ -22,6 +22,7 @@ enum TBStatsStatus {
 };
 
 static enum TBStatsStatus tcg_collect_tb_stats;
+static uint32_t default_tbstats_flag;
 
 void init_tb_stats_htable(void)
 {
@@ -56,3 +57,8 @@ bool tb_stats_collection_paused(void)
 {
     return tcg_collect_tb_stats == TB_STATS_PAUSED;
 }
+
+uint32_t get_default_tbstats_flag(void)
+{
+    return default_tbstats_flag;
+}
accel/tcg/tcg-runtime.c +1 -0
index e4e030043f..11c6192064 100644
@@ -27,6 +27,7 @@
 #include "exec/helper-proto.h"
 #include "exec/cpu_ldst.h"
 #include "exec/exec-all.h"
+#include "exec/tb-stats.h"
 #include "disas/disas.h"
 #include "exec/log.h"
 #include "tcg/tcg.h"
accel/tcg/translate-all.c +5 -2
index bb9483785e..dadf49954f 100644
@@ -295,6 +295,7 @@ static TBStatistics *tb_get_stats(tb_page_addr_t phys_pc, target_ulong pc,
     new_stats->pc = pc;
     new_stats->cs_base = cs_base;
     new_stats->flags = flags;
+    new_stats->stats_enabled = get_default_tbstats_flag();
 
     /*
      * All initialisation must be complete before we insert into qht
@@ -382,9 +383,11 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
     /*
      * We want to fetch the stats structure before we start code
      * generation so we can count interesting things about this
-     * generation.
+     * generation. If dfilter is in effect we will only collect stats
+     * for the specified range.
      */
-    if (tb_stats_collection_enabled()) {
+    if (tb_stats_collection_enabled() &&
+        qemu_log_in_addr_range(tb->pc)) {
         tb->tb_stats = tb_get_stats(phys_pc, pc, cs_base, flags);
     } else {
         tb->tb_stats = NULL;
accel/tcg/translator.c +25 -0
index 6120ef2a92..80ffbfb455 100644
@@ -11,6 +11,7 @@
 #include "qemu/error-report.h"
 #include "tcg/tcg.h"
 #include "tcg/tcg-op.h"
+#include "tcg/tcg-temp-internal.h"
 #include "exec/exec-all.h"
 #include "exec/gen-icount.h"
 #include "exec/log.h"
@@ -18,6 +19,29 @@
 #include "exec/plugin-gen.h"
 #include "exec/replay-core.h"
 
+static void gen_tb_exec_count(TranslationBlock *tb)
+{
+    if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
+        TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
+
+        tcg_gen_movi_ptr(ptr, (intptr_t)&tb->tb_stats->executions.normal);
+        if (sizeof(tb->tb_stats->executions.normal) == 4) {
+            TCGv_i32 t = tcg_temp_ebb_new_i32();
+            tcg_gen_ld_i32(t, ptr, 0);
+            tcg_gen_addi_i32(t, t, 1);
+            tcg_gen_st_i32(t, ptr, 0);
+            tcg_temp_free_i32(t);
+        } else {
+            TCGv_i64 t = tcg_temp_ebb_new_i64();
+            tcg_gen_ld_i64(t, ptr, 0);
+            tcg_gen_addi_i64(t, t, 1);
+            tcg_gen_st_i64(t, ptr, 0);
+            tcg_temp_free_i64(t);
+        }
+        tcg_temp_free_ptr(ptr);
+    }
+}
+
 bool translator_use_goto_tb(DisasContextBase *db, target_ulong dest)
 {
     /* Suppress goto_tb if requested. */
@@ -56,6 +80,7 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
 
     /* Start translating.  */
     gen_tb_start(db->tb);
+    gen_tb_exec_count(tb);
     ops->tb_start(db, cpu);
     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
 
include/exec/gen-icount.h +1 -0
index f6de79a6b4..20e7835ff0 100644
@@ -2,6 +2,7 @@
 #define GEN_ICOUNT_H
 
 #include "exec/exec-all.h"
+#include "exec/tb-stats.h"
 
 /* Helpers for instruction counting code generation.  */
 
include/exec/tb-stats-flags.h +5 -0
index 87ee3d902e..fa71eb6f0c 100644
@@ -11,6 +11,9 @@
 #ifndef TB_STATS_FLAGS
 #define TB_STATS_FLAGS
 
+#define TB_NOTHING    (1 << 0)
+#define TB_EXEC_STATS (1 << 1)
+
 /* TBStatistic collection controls */
 void enable_collect_tb_stats(void);
 void disable_collect_tb_stats(void);
@@ -18,4 +21,6 @@ void pause_collect_tb_stats(void);
 bool tb_stats_collection_enabled(void);
 bool tb_stats_collection_paused(void);
 
+uint32_t get_default_tbstats_flag(void);
+
 #endif
include/exec/tb-stats.h +13 -0
index b519465665..eb1fa92a4e 100644
@@ -31,6 +31,9 @@
 #include "exec/tb-stats-flags.h"
 #include "tcg/tcg.h"
 
+#define tb_stats_enabled(tb, JIT_STATS) \
+    (tb && tb->tb_stats && (tb->tb_stats->stats_enabled & JIT_STATS))
+
 typedef struct TBStatistics TBStatistics;
 
 /*
@@ -47,6 +50,16 @@ struct TBStatistics {
     uint32_t     flags;
     /* cs_base isn't included in the hash but we do check for matches */
     target_ulong cs_base;
+
+    /* which stats are enabled for this TBStats */
+    uint32_t stats_enabled;
+
+    /* Execution stats */
+    struct {
+        unsigned long normal;
+        unsigned long atomic;
+    } executions;
+
 };
 
 bool tb_stats_cmp(const void *ap, const void *bp);
Signature · 1 line
2.25.1
#11 lore raw

Re: [PATCH v14 03/10] accel: collecting TB execution count

Quoted 70 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

If a TB has a TBS (TBStatistics) with the TB_EXEC_STATS enabled, then we instrument the start code of this TB to atomically count the number of times it is executed. We count both the number of "normal" executions and atomic executions of a TB.

The execution count of the TB is stored in its respective TBS.

All TBStatistics are created by default with the flags from default_tbstats_flag.

[Richard Henderson created the inline gen_tb_exec_count]

Message-Id: <[email protected]>

[AJB: Fix author]
---
  accel/tcg/cpu-exec.c          |  6 ++++++
  accel/tcg/tb-stats.c          |  6 ++++++
  accel/tcg/tcg-runtime.c       |  1 +
  accel/tcg/translate-all.c     |  7 +++++--
  accel/tcg/translator.c        | 25 +++++++++++++++++++++++++
  include/exec/gen-icount.h     |  1 +
  include/exec/tb-stats-flags.h |  5 +++++
  include/exec/tb-stats.h       | 13 +++++++++++++
  8 files changed, 62 insertions(+), 2 deletions(-)
accel/tcg/cpu-exec.c +6 -0
index 0e741960da..c0d8f26237 100644
@@ -25,6 +25,7 @@
  #include "trace.h"
  #include "disas/disas.h"
  #include "exec/exec-all.h"
+#include "exec/tb-stats.h"
  #include "tcg/tcg.h"
  #include "qemu/atomic.h"
  #include "qemu/rcu.h"
@@ -562,7 +563,12 @@ void cpu_exec_step_atomic(CPUState *cpu)
              mmap_unlock();
          }
 
+        if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
+            tb->tb_stats->executions.atomic++;
+        }
+
          cpu_exec_enter(cpu);
+
          /* execute the generated code */
          trace_exec_tb(tb, pc);
          cpu_tb_exec(cpu, tb, &tb_exit);
accel/tcg/tb-stats.c +1 -0
index f988bd8a31..143a52ef5c 100644
@@ -22,6 +22,7 @@ enum TBStatsStatus {
  };
 
  static enum TBStatsStatus tcg_collect_tb_stats;
+static uint32_t default_tbstats_flag;
 
  void init_tb_stats_htable(void)
  {

accel/tcg/tb-stats.c bool tb_stats_collection_paused(void) · line 64

Quoted 8 lines
accel/tcg/tb-stats.c +5 -0
@@ -56,3 +57,8 @@ bool tb_stats_collection_paused(void)
  {
      return tcg_collect_tb_stats == TB_STATS_PAUSED;
  }
+
+uint32_t get_default_tbstats_flag(void)
+{
+    return default_tbstats_flag;
+}

What is the purpose of this function, instead of a global variable? What is the meaning of 'default' in its name?

accel/tcg/tb-stats.c static TBStatistics *tb_get_stats(tb_page_addr_t phys_pc, target_ulong pc, · line 298

Quoted 5 lines
accel/tcg/tb-stats.c +1 -0
@@ -295,6 +295,7 @@ static TBStatistics *tb_get_stats(tb_page_addr_t phys_pc, target_ulong pc,
      new_stats->pc = pc;
      new_stats->cs_base = cs_base;
      new_stats->flags = flags;
+    new_stats->stats_enabled = get_default_tbstats_flag();

Is this merely to record how we have generated a given TB? What is the purpose of this flag over the global variable?

include/exec/tb-stats-flags.h line 15

Quoted 10 lines
include/exec/tb-stats-flags.h +2 -0
index 87ee3d902e..fa71eb6f0c 100644
@@ -11,6 +11,9 @@
  #ifndef TB_STATS_FLAGS
  #define TB_STATS_FLAGS
 
+#define TB_NOTHING    (1 << 0)
+#define TB_EXEC_STATS (1 << 1)

Why is NOTHING a non-zero flag?

include/exec/tb-stats.h line 35

Quoted 8 lines
include/exec/tb-stats.h +2 -0
@@ -31,6 +31,9 @@
  #include "exec/tb-stats-flags.h"
  #include "tcg/tcg.h"
 
+#define tb_stats_enabled(tb, JIT_STATS) \
+    (tb && tb->tb_stats && (tb->tb_stats->stats_enabled & JIT_STATS))

Inline function, though again, why stats_enabled vs global variable?

r~

author
#12 lore raw

Re: [PATCH v14 03/10] accel: collecting TB execution count

Quoted 71 lines · Richard Henderson
Quoted 70 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

If a TB has a TBS (TBStatistics) with the TB_EXEC_STATS enabled, then we instrument the start code of this TB to atomically count the number of times it is executed. We count both the number of "normal" executions and atomic executions of a TB.

The execution count of the TB is stored in its respective TBS.

All TBStatistics are created by default with the flags from default_tbstats_flag.

[Richard Henderson created the inline gen_tb_exec_count]

Message-Id: <[email protected]>

[AJB: Fix author]

---
  accel/tcg/cpu-exec.c          |  6 ++++++ accel/tcg/tb-stats.c          |  6 ++++++ accel/tcg/tcg-runtime.c       |  1 + accel/tcg/translate-all.c     |  7 +++++-- accel/tcg/translator.c        | 25 +++++++++++++++++++++++++ include/exec/gen-icount.h     |  1 + include/exec/tb-stats-flags.h |  5 +++++ include/exec/tb-stats.h       | 13 +++++++++++++

  8 files changed, 62 insertions(+), 2 deletions(-)
accel/tcg/cpu-exec.c +5 -0
index 0e741960da..c0d8f26237 100644
@@ -25,6 +25,7 @@
   #include "trace.h"
   #include "disas/disas.h"
   #include "exec/exec-all.h"
+#include "exec/tb-stats.h"
   #include "tcg/tcg.h"
   #include "qemu/atomic.h"
   #include "qemu/rcu.h"
@@ -562,7 +563,12 @@ void cpu_exec_step_atomic(CPUState *cpu)
               mmap_unlock();
           }
   +        if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
+            tb->tb_stats->executions.atomic++;
+        }
+
           cpu_exec_enter(cpu);
+
           /* execute the generated code */
           trace_exec_tb(tb, pc);
           cpu_tb_exec(cpu, tb, &tb_exit);
accel/tcg/tb-stats.c +1 -0
index f988bd8a31..143a52ef5c 100644
@@ -22,6 +22,7 @@ enum TBStatsStatus {
   };
     static enum TBStatsStatus tcg_collect_tb_stats;
+static uint32_t default_tbstats_flag;
     void init_tb_stats_htable(void)
   {
@@ -56,3 +57,8 @@ bool tb_stats_collection_paused(void)
   {
       return tcg_collect_tb_stats == TB_STATS_PAUSED;
   }
Quoted 9 lines

accel/tcg/tb-stats.c bool tb_stats_collection_paused(void) · line 64

Quoted 5 lines
accel/tcg/tb-stats.c +5 -0
+
+uint32_t get_default_tbstats_flag(void)
+{
+    return default_tbstats_flag;
+}

What is the purpose of this function, instead of a global variable? What is the meaning of 'default' in its name?

tbs have their specific settings, e.g. after 'filter' cmd:
* the last_search tbs has their stats_enabled kept
* tbs not in the list sets their flag to TB_PAUSED

I guess 'default' means the flag for creating new tbstats.

Quoted 10 lines

accel/tcg/tb-stats.c static TBStatistics *tb_get_stats(tb_page_addr_t · line 299

Quoted 6 lines
accel/tcg/tb-stats.c +1 -0
@@ -295,6 +295,7 @@ static TBStatistics *tb_get_stats(tb_page_addr_t
 phys_pc, target_ulong pc,
       new_stats->pc = pc;
       new_stats->cs_base = cs_base;
       new_stats->flags = flags;
+    new_stats->stats_enabled = get_default_tbstats_flag();

Is this merely to record how we have generated a given TB? What is the purpose of this flag over the global variable?

see above.

Quoted 4 lines
Quoted 4 lines
include/exec/tb-stats-flags.h +0 -1
index 87ee3d902e..fa71eb6f0c 100644
--- a/include/exec/tb-stats-flags.h
Quoted 9 lines

include/exec/tb-stats-flags.h line 14

Quoted 6 lines

+++ b/include/exec/tb-stats-flags.h

include/exec/tb-stats-flags.h +1 -0
@@ -11,6 +11,9 @@
   #ifndef TB_STATS_FLAGS
   #define TB_STATS_FLAGS
   +#define TB_NOTHING    (1 << 0)
+#define TB_EXEC_STATS (1 << 1)

Why is NOTHING a non-zero flag?

yes, it might looks better. But there is no correctness issue either as it checks if the specific bit is enabled during collecting stats.

Quoted 10 lines

include/exec/tb-stats.h line 34

Quoted 7 lines
include/exec/tb-stats.h +1 -0
@@ -31,6 +31,9 @@
   #include "exec/tb-stats-flags.h"
   #include "tcg/tcg.h"
   +#define tb_stats_enabled(tb, JIT_STATS) \
+    (tb && tb->tb_stats && (tb->tb_stats->stats_enabled & JIT_STATS))

Inline function, though again, why stats_enabled vs global variable?

will inline it.

Thanks,
Fei.

Quoted 2 lines

r~

#13 lore raw

Re: [PATCH v14 03/10] accel: collecting TB execution count

Quoted 81 lines · Fei
Quoted 71 lines · Richard Henderson
Quoted 70 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

If a TB has a TBS (TBStatistics) with the TB_EXEC_STATS enabled, then we instrument the start code of this TB to atomically count the number of times it is executed. We count both the number of "normal" executions and atomic executions of a TB.

The execution count of the TB is stored in its respective TBS.

All TBStatistics are created by default with the flags from default_tbstats_flag.

[Richard Henderson created the inline gen_tb_exec_count]

Message-Id: <[email protected]>

[AJB: Fix author]
---
   accel/tcg/cpu-exec.c          |  6 ++++++
   accel/tcg/tb-stats.c          |  6 ++++++
   accel/tcg/tcg-runtime.c       |  1 +
   accel/tcg/translate-all.c     |  7 +++++--
   accel/tcg/translator.c        | 25 +++++++++++++++++++++++++
   include/exec/gen-icount.h     |  1 +
   include/exec/tb-stats-flags.h |  5 +++++
   include/exec/tb-stats.h       | 13 +++++++++++++
   8 files changed, 62 insertions(+), 2 deletions(-)
accel/tcg/cpu-exec.c +5 -0
index 0e741960da..c0d8f26237 100644
@@ -25,6 +25,7 @@
   #include "trace.h"
   #include "disas/disas.h"
   #include "exec/exec-all.h"
+#include "exec/tb-stats.h"
   #include "tcg/tcg.h"
   #include "qemu/atomic.h"
   #include "qemu/rcu.h"
@@ -562,7 +563,12 @@ void cpu_exec_step_atomic(CPUState *cpu)
               mmap_unlock();
           }
   +        if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
+            tb->tb_stats->executions.atomic++;
+        }
+
           cpu_exec_enter(cpu);
+
           /* execute the generated code */
           trace_exec_tb(tb, pc);
           cpu_tb_exec(cpu, tb, &tb_exit);
accel/tcg/tb-stats.c +1 -0
index f988bd8a31..143a52ef5c 100644
@@ -22,6 +22,7 @@ enum TBStatsStatus {
   };
     static enum TBStatsStatus tcg_collect_tb_stats;
+static uint32_t default_tbstats_flag;
     void init_tb_stats_htable(void)
   {
@@ -56,3 +57,8 @@ bool tb_stats_collection_paused(void)
   {
       return tcg_collect_tb_stats == TB_STATS_PAUSED;
   }
Quoted 9 lines

accel/tcg/tb-stats.c bool tb_stats_collection_paused(void) · line 64

Quoted 5 lines
accel/tcg/tb-stats.c +5 -0
+
+uint32_t get_default_tbstats_flag(void)
+{
+    return default_tbstats_flag;
+}

What is the purpose of this function, instead of a global variable? What is the meaning of 'default' in its name?

Quoted 3 lines

tbs have their specific settings, e.g. after 'filter' cmd:
* the last_search tbs has their stats_enabled kept
* tbs not in the list sets their flag to TB_PAUSED

How does this affect anything at all?

We are not *checking* the tb->tb_stats->stats_enabled bit except at code generation time, not code execution time. Therefore nothing ever reads the TB_PAUSED bit (or, correspondingly, the clearing of the other bits). The setting of the bit is permanent.

Quoted 2 lines

yes, it might looks better. But there is no correctness issue either as it checks if the specific bit is enabled during collecting stats.

No, it does not. See above.

r~

author
#14 lore raw

Re: [PATCH v14 03/10] accel: collecting TB execution count

Quoted 86 lines · Richard Henderson
Quoted 81 lines · Fei
Quoted 71 lines · Richard Henderson
Quoted 70 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

If a TB has a TBS (TBStatistics) with the TB_EXEC_STATS enabled, then we instrument the start code of this TB to atomically count the number of times it is executed. We count both the number of "normal" executions and atomic executions of a TB.

The execution count of the TB is stored in its respective TBS.

All TBStatistics are created by default with the flags from default_tbstats_flag.

[Richard Henderson created the inline gen_tb_exec_count]

Message-Id: <[email protected]>

[AJB: Fix author]

---
   accel/tcg/cpu-exec.c          |  6 ++++++ accel/tcg/tb-stats.c          |  6 ++++++ accel/tcg/tcg-runtime.c       |  1 + accel/tcg/translate-all.c     |  7 +++++-- accel/tcg/translator.c        | 25 +++++++++++++++++++++++++ include/exec/gen-icount.h     |  1 + include/exec/tb-stats-flags.h |  5 +++++ include/exec/tb-stats.h       | 13 +++++++++++++

   8 files changed, 62 insertions(+), 2 deletions(-)
accel/tcg/cpu-exec.c +5 -0
index 0e741960da..c0d8f26237 100644
@@ -25,6 +25,7 @@
    #include "trace.h"
    #include "disas/disas.h"
    #include "exec/exec-all.h"
+#include "exec/tb-stats.h"
    #include "tcg/tcg.h"
    #include "qemu/atomic.h"
    #include "qemu/rcu.h"
@@ -562,7 +563,12 @@ void cpu_exec_step_atomic(CPUState *cpu)
                mmap_unlock();
            }
    +        if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
+            tb->tb_stats->executions.atomic++;
+        }
+
            cpu_exec_enter(cpu);
+
            /* execute the generated code */
            trace_exec_tb(tb, pc);
            cpu_tb_exec(cpu, tb, &tb_exit);
accel/tcg/tb-stats.c +1 -0
index f988bd8a31..143a52ef5c 100644
@@ -22,6 +22,7 @@ enum TBStatsStatus {
    };
      static enum TBStatsStatus tcg_collect_tb_stats;
+static uint32_t default_tbstats_flag;
      void init_tb_stats_htable(void)
    {
@@ -56,3 +57,8 @@ bool tb_stats_collection_paused(void)
    {
        return tcg_collect_tb_stats == TB_STATS_PAUSED;
    }
Quoted 9 lines

accel/tcg/tb-stats.c bool tb_stats_collection_paused(void) · line 64

Quoted 5 lines
accel/tcg/tb-stats.c +5 -0
+
+uint32_t get_default_tbstats_flag(void)
+{
+    return default_tbstats_flag;
+}

What is the purpose of this function, instead of a global variable? What is the meaning of 'default' in its name?

Quoted 3 lines

tbs have their specific settings, e.g. after 'filter' cmd:
* the last_search tbs has their stats_enabled kept
* tbs not in the list sets their flag to TB_PAUSED

Quoted 7 lines

How does this affect anything at all?

We are not *checking* the tb->tb_stats->stats_enabled bit except at code generation time, not code execution time.  Therefore nothing ever reads the TB_PAUSED bit (or, correspondingly, the clearing of the other bits).  The setting of the bit is permanent.

At dump time, it does check stats_enabled e.g. in dump_tb_header(). So the question is whether FILTER is necessary at all? If not, we can remove FILTER together with PAUSE, and only keep START & STOP in hmp cmd.

Thanks,
Fei.

Quoted 7 lines
Quoted 2 lines

yes, it might looks better. But there is no correctness issue either as it checks if the specific bit is enabled during collecting stats.

No, it does not.  See above.

r~

#15 lore raw

Re: [PATCH v14 03/10] accel: collecting TB execution count

Quoted 8 lines · Fei
Quoted 5 lines

We are not *checking* the tb->tb_stats->stats_enabled bit except at code generation time, not code execution time.  Therefore nothing ever reads the TB_PAUSED bit (or, correspondingly, the clearing of the other bits).  The setting of the bit is permanent.

At dump time, it does check stats_enabled e.g. in dump_tb_header(). So the question is whether FILTER is necessary at all? If not, we can remove FILTER together with PAUSE, and only keep START & STOP in hmp cmd.

Let's start simpler and remove FILTER and PAUSE.

r~

author
#16 lore raw

PATCH v14 4/10 accel/tcg: add jit stats and time to TBStatistics

In reply to Fei Wu

This collects all the statistics for TBStatistics, not only for the whole emulation but for each TB.

---
 accel/tcg/monitor.c           |  20 ++++-
 accel/tcg/tb-stats.c          | 146 ++++++++++++++++++++++++++++++++++
 accel/tcg/tcg-accel-ops.c     |   7 ++
 accel/tcg/translate-all.c     |  70 +++++++++++++++-
 accel/tcg/translator.c        |   7 +-
 include/exec/tb-stats-flags.h |   2 +
 include/exec/tb-stats.h       |  46 +++++++++++
 include/qemu/timer.h          |   6 ++
 include/tcg/tcg.h             |  28 ++++++-
 softmmu/runstate.c            |   9 +++
 tcg/tcg.c                     |  88 ++++++++++++++++++--
 tests/qtest/qmp-cmd-test.c    |   3 +
 12 files changed, 417 insertions(+), 15 deletions(-)
accel/tcg/monitor.c +19 -1
index e903dd1d2e..2bc87f2642 100644
@@ -15,6 +15,7 @@
 #include "sysemu/cpus.h"
 #include "sysemu/cpu-timers.h"
 #include "sysemu/tcg.h"
+#include "exec/tb-stats.h"
 #include "internal.h"
 
 
@@ -69,6 +70,11 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
 {
     g_autoptr(GString) buf = g_string_new("");
 
+    if (!tb_stats_collection_enabled()) {
+        error_setg(errp, "TB information not being recorded.");
+        return NULL;
+    }
+
     if (!tcg_enabled()) {
         error_setg(errp,
                    "Opcode count information is only available with accel=tcg");
@@ -80,11 +86,23 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
     return human_readable_text_from_str(buf);
 }
 
+#ifdef CONFIG_TCG
+HumanReadableText *qmp_x_query_profile(Error **errp)
+{
+    g_autoptr(GString) buf = g_string_new("");
+
+    dump_jit_exec_time_info(dev_time, buf);
+    dev_time = 0;
+
+    return human_readable_text_from_str(buf);
+}
+#else
 HumanReadableText *qmp_x_query_profile(Error **errp)
 {
-    error_setg(errp, "Internal profiler not compiled");
+    error_setg(errp, "TCG should be enabled!");
     return NULL;
 }
+#endif
 
 static void hmp_tcg_register(void)
 {
accel/tcg/tb-stats.c +146 -0

158 lines not on this page · raw message

accel/tcg/tcg-accel-ops.c +7 -0
index 3973591508..749ad182f2 100644
@@ -70,10 +70,17 @@ void tcg_cpus_destroy(CPUState *cpu)
 int tcg_cpus_exec(CPUState *cpu)
 {
     int ret;
+    uint64_t ti;
+
     assert(tcg_enabled());
+    ti = profile_getclock();
+
     cpu_exec_start(cpu);
     ret = cpu_exec(cpu);
     cpu_exec_end(cpu);
+
+    qatomic_add(&tcg_ctx->prof.cpu_exec_time, profile_getclock() - ti);
+
     return ret;
 }
 
accel/tcg/translate-all.c +66 -4

135 lines not on this page · raw message

accel/tcg/translator.c +6 -1
index 80ffbfb455..a60a92091b 100644
@@ -19,7 +19,7 @@
 #include "exec/plugin-gen.h"
 #include "exec/replay-core.h"
 
-static void gen_tb_exec_count(TranslationBlock *tb)
+static inline void gen_tb_exec_count(TranslationBlock *tb)
 {
     if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
         TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
@@ -147,6 +147,11 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
     tb->size = db->pc_next - db->pc_first;
     tb->icount = db->num_insns;
 
+    /* Save number of guest instructions for TB_JIT_STATS */
+    if (tb_stats_enabled(tb, TB_JIT_STATS)) {
+        tb->tb_stats->code.num_guest_inst += db->num_insns;
+    }
+
     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
         && qemu_log_in_addr_range(db->pc_first)) {
         FILE *logfile = qemu_log_trylock();
include/exec/tb-stats-flags.h +2 -0
index fa71eb6f0c..f29eff7576 100644
@@ -13,6 +13,8 @@
 
 #define TB_NOTHING    (1 << 0)
 #define TB_EXEC_STATS (1 << 1)
+#define TB_JIT_STATS  (1 << 2)
+#define TB_JIT_TIME   (1 << 3)
 
 /* TBStatistic collection controls */
 void enable_collect_tb_stats(void);
include/exec/tb-stats.h +46 -0

62 lines not on this page · raw message

include/qemu/timer.h +6 -0
index 9a91cb1248..ad0da18a5f 100644
@@ -989,4 +989,10 @@ static inline int64_t cpu_get_host_ticks(void)
 }
 #endif
 
+static inline int64_t profile_getclock(void)
+{
+    return get_clock();
+}
+
+extern uint64_t dev_time;
 #endif
include/tcg/tcg.h +26 -2
index 083cbd6580..85bf6fb78e 100644
@@ -522,6 +522,26 @@ static inline TCGRegSet output_pref(const TCGOp *op, unsigned i)
     return i < ARRAY_SIZE(op->output_pref) ? op->output_pref[i] : 0;
 }
 
+/* Timestamps during translation  */
+typedef struct TCGTime {
+    uint64_t start;
+    uint64_t ir_done;
+    uint64_t opt_done;
+    uint64_t la_done;
+    uint64_t code_done;
+} TCGTime;
+
+/*
+ * The TCGProfile structure holds data for the lifetime of the translator.
+ */
+typedef struct TCGProfile {
+    /* exec time of this vcpu */
+    int64_t cpu_exec_time;
+
+    /* Lifetime count of TCGOps per TCGContext when tb_stats enabled */
+    size_t table_op_count[NB_OPS];
+} TCGProfile;
+
 struct TCGContext {
     uint8_t *pool_cur, *pool_end;
     TCGPool *pool_first, *pool_current, *pool_first_large;
@@ -548,6 +568,8 @@ struct TCGContext {
     tcg_insn_unit *code_buf;      /* pointer for start of tb */
     tcg_insn_unit *code_ptr;      /* pointer for running end of tb */
 
+    TCGProfile prof;
+
 #ifdef CONFIG_DEBUG_TCG
     int goto_tb_issue_mask;
     const TCGOpcode *vecop_list;
@@ -608,6 +630,7 @@ struct TCGContext {
 
     /* Exit to translator on overflow. */
     sigjmp_buf jmp_trans;
+    TranslationBlock *current_tb;
 };
 
 static inline bool temp_readonly(TCGTemp *ts)
@@ -827,7 +850,8 @@ void tcg_register_thread(void);
 void tcg_prologue_init(TCGContext *s);
 void tcg_func_start(TCGContext *s);
 
-int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start);
+int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start,
+                 TCGTime *tcg_time);
 
 void tb_target_set_jmp_target(const TranslationBlock *, int,
                               uintptr_t, uintptr_t);
@@ -885,7 +909,7 @@ static inline TCGv_ptr tcg_temp_new_ptr(void)
     return temp_tcgv_ptr(t);
 }
 
-int64_t tcg_cpu_exec_time(void);
+uint64_t tcg_cpu_exec_time(void);
 void tcg_dump_info(GString *buf);
 void tcg_dump_op_count(GString *buf);
 
softmmu/runstate.c +9 -0
index bd50062ed0..37390799f1 100644
@@ -728,9 +728,18 @@ static bool main_loop_should_exit(int *status)
 int qemu_main_loop(void)
 {
     int status = EXIT_SUCCESS;
+#ifdef CONFIG_TCG
+    uint64_t ti;
+#endif
 
     while (!main_loop_should_exit(&status)) {
+#ifdef CONFIG_TCG
+        ti = profile_getclock();
+#endif
         main_loop_wait(false);
+#ifdef CONFIG_TCG
+        dev_time += profile_getclock() - ti;
+#endif
     }
 
     return status;
tcg/tcg.c +81 -7

154 lines not on this page · raw message

tests/qtest/qmp-cmd-test.c +3 -0
index 73a670e8fa..749aafe4da 100644
@@ -46,6 +46,9 @@ static int query_error_class(const char *cmd)
         { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
         { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
         { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
+#ifndef CONFIG_TCG
+        { "x-query-profile", ERROR_CLASS_GENERIC_ERROR },
+#endif
         /* Only valid with a USB bus added */
         { "x-query-usb", ERROR_CLASS_GENERIC_ERROR },
         /* Only valid with accel=tcg */
Signature · 1 line
2.25.1
#17 lore raw

Re: [PATCH v14 04/10] accel/tcg: add jit stats and time to TBStatistics

Quoted 34 lines · Fei Wu

This collects all the statistics for TBStatistics, not only for the whole emulation but for each TB.

---
 accel/tcg/monitor.c           |  20 ++++-
 accel/tcg/tb-stats.c          | 146 ++++++++++++++++++++++++++++++++++
 accel/tcg/tcg-accel-ops.c     |   7 ++
 accel/tcg/translate-all.c     |  70 +++++++++++++++-
 accel/tcg/translator.c        |   7 +-
 include/exec/tb-stats-flags.h |   2 +
 include/exec/tb-stats.h       |  46 +++++++++++
 include/qemu/timer.h          |   6 ++
 include/tcg/tcg.h             |  28 ++++++-
 softmmu/runstate.c            |   9 +++
 tcg/tcg.c                     |  88 ++++++++++++++++++--
 tests/qtest/qmp-cmd-test.c    |   3 +
 12 files changed, 417 insertions(+), 15 deletions(-)
accel/tcg/monitor.c +1 -0
index e903dd1d2e..2bc87f2642 100644
@@ -15,6 +15,7 @@
 #include "sysemu/cpus.h"
 #include "sysemu/cpu-timers.h"
 #include "sysemu/tcg.h"
+#include "exec/tb-stats.h"
 #include "internal.h"
 
 

accel/tcg/monitor.c HumanReadableText *qmp_x_query_opcount(Error **errp) · line 74

Quoted 5 lines
accel/tcg/monitor.c +2 -0
@@ -69,6 +70,11 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
 {
     g_autoptr(GString) buf = g_string_new("");
 
+    if (!tb_stats_collection_enabled()) {
+        error_setg(errp, "TB information not being recorded.");

From error_setg()'s contract in include/qapi/error.h:

* The resulting message should be a single phrase, with no newline or
* trailing punctuation.

Please drop the period. Same elsewhere, not flagging it again there.

Quoted 18 lines
accel/tcg/monitor.c +11 -0
+        return NULL;
+    }
+
     if (!tcg_enabled()) {
         error_setg(errp,
                    "Opcode count information is only available with accel=tcg");
@@ -80,11 +86,23 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
     return human_readable_text_from_str(buf);
 }
 
+#ifdef CONFIG_TCG
+HumanReadableText *qmp_x_query_profile(Error **errp)
+{
+    g_autoptr(GString) buf = g_string_new("");
+
+    dump_jit_exec_time_info(dev_time, buf);
+    dev_time = 0;
+

accel/tcg/monitor.c HumanReadableText *qmp_x_query_opcount(Error **errp) · line 105

Quoted 10 lines
accel/tcg/monitor.c +5 -1
+    return human_readable_text_from_str(buf);
+}
+#else
 HumanReadableText *qmp_x_query_profile(Error **errp)
 {
-    error_setg(errp, "Internal profiler not compiled");
+    error_setg(errp, "TCG should be enabled!");
     return NULL;
 }
+#endif

machine.json has

##
# @x-query-profile:
#
# Query TCG profiling information
#
# Features:
#
# @unstable: This command is meant for debugging. #
# Returns: profile information
#
# Since: 6.2
##
{ 'command': 'x-query-profile',

     'returns': 'HumanReadableText',
     'if': 'CONFIG_TCG',
     'features': [ 'unstable' ] }

Not changed in this series.

Note the command is conditional on CONFIG_TCG, i.e. code generated for it is #if defined(CONFIG_TCG).

The only other use is in hmp-commands-info.hx, and it is also guarded by CONFIG_TCG.

Therefore, your #else is unreachable. You can delete it along with...

Quoted 3 lines
accel/tcg/monitor.c +0 -0
 static void hmp_tcg_register(void)
 {

[...]

tests/qtest/qmp-cmd-test.c static int query_error_class(const char *cmd) · line 51

Quoted 11 lines
tests/qtest/qmp-cmd-test.c +3 -0
index 73a670e8fa..749aafe4da 100644
@@ -46,6 +46,9 @@ static int query_error_class(const char *cmd)
         { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
         { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
         { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
+#ifndef CONFIG_TCG
+        { "x-query-profile", ERROR_CLASS_GENERIC_ERROR },
+#endif

... this entry.

Quoted 3 lines
tests/qtest/qmp-cmd-test.c +0 -0
         /* Only valid with a USB bus added */
         { "x-query-usb", ERROR_CLASS_GENERIC_ERROR },
         /* Only valid with accel=tcg */
author
#18 lore raw

Re: [PATCH v14 04/10] accel/tcg: add jit stats and time to TBStatistics

Quoted 44 lines · Markus Armbruster
Quoted 34 lines · Fei Wu

This collects all the statistics for TBStatistics, not only for the whole emulation but for each TB.

---
 accel/tcg/monitor.c           |  20 ++++-
 accel/tcg/tb-stats.c          | 146 ++++++++++++++++++++++++++++++++++
 accel/tcg/tcg-accel-ops.c     |   7 ++
 accel/tcg/translate-all.c     |  70 +++++++++++++++-
 accel/tcg/translator.c        |   7 +-
 include/exec/tb-stats-flags.h |   2 +
 include/exec/tb-stats.h       |  46 +++++++++++
 include/qemu/timer.h          |   6 ++
 include/tcg/tcg.h             |  28 ++++++-
 softmmu/runstate.c            |   9 +++
 tcg/tcg.c                     |  88 ++++++++++++++++++--
 tests/qtest/qmp-cmd-test.c    |   3 +
 12 files changed, 417 insertions(+), 15 deletions(-)
accel/tcg/monitor.c +1 -0
index e903dd1d2e..2bc87f2642 100644
@@ -15,6 +15,7 @@
 #include "sysemu/cpus.h"
 #include "sysemu/cpu-timers.h"
 #include "sysemu/tcg.h"
+#include "exec/tb-stats.h"
 #include "internal.h"
 
 

accel/tcg/monitor.c HumanReadableText *qmp_x_query_opcount(Error **errp) · line 74

Quoted 5 lines
accel/tcg/monitor.c +2 -0
@@ -69,6 +70,11 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
 {
     g_autoptr(GString) buf = g_string_new("");
 
+    if (!tb_stats_collection_enabled()) {
+        error_setg(errp, "TB information not being recorded.");

From error_setg()'s contract in include/qapi/error.h:

Quoted 5 lines

* The resulting message should be a single phrase, with no newline or
* trailing punctuation.

Please drop the period. Same elsewhere, not flagging it again there.

Got it, will do.

Quoted 51 lines
Quoted 18 lines
accel/tcg/monitor.c +11 -0
+        return NULL;
+    }
+
     if (!tcg_enabled()) {
         error_setg(errp,
                    "Opcode count information is only available with accel=tcg");
@@ -80,11 +86,23 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
     return human_readable_text_from_str(buf);
 }
 
+#ifdef CONFIG_TCG
+HumanReadableText *qmp_x_query_profile(Error **errp)
+{
+    g_autoptr(GString) buf = g_string_new("");
+
+    dump_jit_exec_time_info(dev_time, buf);
+    dev_time = 0;
+

accel/tcg/monitor.c HumanReadableText *qmp_x_query_opcount(Error **errp) · line 105

Quoted 10 lines
accel/tcg/monitor.c +5 -1
+    return human_readable_text_from_str(buf);
+}
+#else
 HumanReadableText *qmp_x_query_profile(Error **errp)
 {
-    error_setg(errp, "Internal profiler not compiled");
+    error_setg(errp, "TCG should be enabled!");
     return NULL;
 }
+#endif

machine.json has

   ##
   # @x-query-profile:
   #
   # Query TCG profiling information
   #
   # Features:
   #
   # @unstable: This command is meant for debugging.
   #
   # Returns: profile information
   #
   # Since: 6.2
   ##
   { 'command': 'x-query-profile',
     'returns': 'HumanReadableText',
     'if': 'CONFIG_TCG',
     'features': [ 'unstable' ] }

Not changed in this series.

Quoted 8 lines

Note the command is conditional on CONFIG_TCG, i.e. code generated for it is #if defined(CONFIG_TCG).

The only other use is in hmp-commands-info.hx, and it is also guarded by CONFIG_TCG.

Therefore, your #else is unreachable. You can delete it along with...

OK, so qmp_x_query_profile won't get called #ifndef CONFIG_TCG, I will remove it.

Thanks,
Fei.

Quoted 24 lines
Quoted 3 lines
accel/tcg/monitor.c +0 -0
 static void hmp_tcg_register(void)
 {

[...]

tests/qtest/qmp-cmd-test.c static int query_error_class(const char *cmd) · line 51

Quoted 11 lines
tests/qtest/qmp-cmd-test.c +3 -0
index 73a670e8fa..749aafe4da 100644
@@ -46,6 +46,9 @@ static int query_error_class(const char *cmd)
         { "query-balloon", ERROR_CLASS_DEVICE_NOT_ACTIVE },
         { "query-hotpluggable-cpus", ERROR_CLASS_GENERIC_ERROR },
         { "query-vm-generation-id", ERROR_CLASS_GENERIC_ERROR },
+#ifndef CONFIG_TCG
+        { "x-query-profile", ERROR_CLASS_GENERIC_ERROR },
+#endif

... this entry.

Quoted 3 lines
tests/qtest/qmp-cmd-test.c +0 -0
         /* Only valid with a USB bus added */
         { "x-query-usb", ERROR_CLASS_GENERIC_ERROR },
         /* Only valid with accel=tcg */
#19 lore raw

Re: [PATCH v14 04/10] accel/tcg: add jit stats and time to TBStatistics

In reply to Fei Wu
Quoted 22 lines · Fei Wu
accel/tcg/tb-stats.c +22 -0
+static void collect_jit_profile_info(void *p, uint32_t hash, void *userp)
+{
+    struct jit_profile_info *jpi = userp;
+    TBStatistics *tbs = p;
+
+    jpi->translations += tbs->translations.total;
+    jpi->ops += tbs->code.num_tcg_ops;
+    if (stat_per_translation(tbs, code.num_tcg_ops) > jpi->ops_max) {
+        jpi->ops_max = stat_per_translation(tbs, code.num_tcg_ops);
+    }
+    jpi->del_ops += tbs->code.deleted_ops;
+    jpi->temps += tbs->code.temps;
+    if (stat_per_translation(tbs, code.temps) > jpi->temps_max) {
+        jpi->temps_max = stat_per_translation(tbs, code.temps);
+    }
+    jpi->host += tbs->code.out_len;
+    jpi->guest += tbs->code.in_len;
+    jpi->search_data += tbs->code.search_out_len;
+
+    jpi->interm_time += stat_per_translation(tbs, gen_times.ir);
+    jpi->opt_time += stat_per_translation(tbs, gen_times.ir_opt);
+    jpi->la_time += stat_per_translation(tbs, gen_times.la);

accel/tcg/tb-stats.c enum TBStatsStatus { · line 86

Quoted 10 lines
accel/tcg/tb-stats.c +10 -0
+    jpi->code_time += stat_per_translation(tbs, gen_times.code);
+
+    /*
+     * The restore time covers how long we have spent restoring state
+     * from a given TB (e.g. recovering from a fault). It is therefor
+     * not related to the number of translations we have done.
+     */
+    jpi->restore_time += tbs->tb_restore_time;
+    jpi->restore_count += tbs->tb_restore_count;
+}

Why do sums of averages (stats_per_translation) instead of accumulating the complete total and dividing by jpi->translations?

Quoted 7 lines
accel/tcg/tb-stats.c +7 -0
+void dump_jit_exec_time_info(uint64_t dev_time, GString *buf)
+{
+    static uint64_t last_cpu_exec_time;
+    uint64_t cpu_exec_time;
+    uint64_t delta;
+
+    cpu_exec_time = tcg_cpu_exec_time();

accel/tcg/tb-stats.c enum TBStatsStatus { · line 104

Quoted 10 lines
accel/tcg/tb-stats.c +10 -0
+    delta = cpu_exec_time - last_cpu_exec_time;
+
+    g_string_append_printf(buf, "async time  %" PRId64 " (%0.3f)\n",
+                           dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
+    g_string_append_printf(buf, "qemu time   %" PRId64 " (%0.3f)\n",
+                           delta, delta / (double)NANOSECONDS_PER_SECOND);
+    last_cpu_exec_time = cpu_exec_time;
+}
+
+/* dump JIT statisticis using TCGProfile and TBStats */

"statistics"

Quoted 8 lines
accel/tcg/tcg-accel-ops.c +0 -0
index 3973591508..749ad182f2 100644
@@ -70,10 +70,17 @@ void tcg_cpus_destroy(CPUState *cpu)
  int tcg_cpus_exec(CPUState *cpu)
  {
      int ret;

accel/tcg/tcg-accel-ops.c void tcg_cpus_destroy(CPUState *cpu) · line 82

Quoted 10 lines
accel/tcg/tcg-accel-ops.c +6 -0
+    uint64_t ti;
+
      assert(tcg_enabled());
+    ti = profile_getclock();
+
      cpu_exec_start(cpu);
      ret = cpu_exec(cpu);
      cpu_exec_end(cpu);
+
+    qatomic_add(&tcg_ctx->prof.cpu_exec_time, profile_getclock() - ti);

You can't qatomic_add a 64-bit value on a 32-bit host. Nor is tcg_ctx a good place to put this.

If you want to accumulate per-cpu data, put it on the cpu where there's no chance of racing with anyone.

Finally, I suspect that this isn't even where you want to accumulate time for execution as separate from translation. You'd to that in cpu_exec_enter/cpu_exec_exit.

accel/tcg/tcg-accel-ops.c static TBStatistics *tb_get_stats(tb_page_addr_t phys_pc, target_ulong pc, · line 318

Quoted 5 lines
accel/tcg/tcg-accel-ops.c +1 -0
@@ -296,6 +315,8 @@ static TBStatistics *tb_get_stats(tb_page_addr_t phys_pc, target_ulong pc,
      new_stats->cs_base = cs_base;
      new_stats->flags = flags;
      new_stats->stats_enabled = get_default_tbstats_flag();
+    new_stats->tbs = g_ptr_array_sized_new(4);

Why default to 4? Is that just a guess, or are we really recomputing tbs that frequently?
Is there a good reason not to use g_ptr_array_new()?

accel/tcg/translator.c line 22

Quoted 10 lines
accel/tcg/translator.c +1 -1
index 80ffbfb455..a60a92091b 100644
@@ -19,7 +19,7 @@
  #include "exec/plugin-gen.h"
  #include "exec/replay-core.h"
 
-static void gen_tb_exec_count(TranslationBlock *tb)
+static inline void gen_tb_exec_count(TranslationBlock *tb)

Why?

accel/tcg/translator.c void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, · line 153

Quoted 11 lines
accel/tcg/translator.c +4 -0
  {
      if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
          TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
@@ -147,6 +147,11 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
      tb->size = db->pc_next - db->pc_first;
      tb->icount = db->num_insns;
 
+    /* Save number of guest instructions for TB_JIT_STATS */
+    if (tb_stats_enabled(tb, TB_JIT_STATS)) {
+        tb->tb_stats->code.num_guest_inst += db->num_insns;
+    }

Why do this here, as opposed to the block in tb_gen_code?

accel/tcg/translator.c void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns, · line 155

Quoted 2 lines
accel/tcg/translator.c +2 -0
+#define stat_per_translation(stat, name) \
+    (stat->translations.total ? stat->name / stat->translations.total : 0)

Not a fan of this macro, and as per above, the reason for doing the division here is questionable.

include/qemu/timer.h static inline int64_t cpu_get_host_ticks(void) · line 995

Quoted 12 lines
include/qemu/timer.h +4 -0
index 9a91cb1248..ad0da18a5f 100644
@@ -989,4 +989,10 @@ static inline int64_t cpu_get_host_ticks(void)
  }
  #endif
 
+static inline int64_t profile_getclock(void)
+{
+    return get_clock();
+}

Why? You use get_clock directly most places.

include/qemu/timer.h static inline int64_t cpu_get_host_ticks(void)

Quoted 8 lines
include/qemu/timer.h +8 -0
+/* Timestamps during translation  */
+typedef struct TCGTime {
+    uint64_t start;
+    uint64_t ir_done;
+    uint64_t opt_done;
+    uint64_t la_done;
+    uint64_t code_done;
+} TCGTime;

int64_t would match the result of get_clock().

include/qemu/timer.h

Quoted 7 lines
include/qemu/timer.h +7 -0
+
+/*
+ * The TCGProfile structure holds data for the lifetime of the translator.
+ */
+typedef struct TCGProfile {
+    /* exec time of this vcpu */
+    int64_t cpu_exec_time;

TCGContext is not per-cpu, and therefore TCGProfile does not correspond either.

include/qemu/timer.h struct TCGContext { · line 633

Quoted 5 lines
include/qemu/timer.h +1 -0
@@ -608,6 +630,7 @@ struct TCGContext {
 
      /* Exit to translator on overflow. */
      sigjmp_buf jmp_trans;
+    TranslationBlock *current_tb;

TCGContext.gen_tb already exists.

include/qemu/timer.h struct TCGContext { · line 634

Quoted 2 lines
include/qemu/timer.h +1 -1
-int64_t tcg_cpu_exec_time(void);
+uint64_t tcg_cpu_exec_time(void);

Why? (Also, probably wants removing, per above.)

Quoted 9 lines
softmmu/runstate.c +3 -0
@@ -728,9 +728,18 @@ static bool main_loop_should_exit(int *status)
  int qemu_main_loop(void)
  {
      int status = EXIT_SUCCESS;
+#ifdef CONFIG_TCG
+    uint64_t ti;
+#endif

softmmu/runstate.c static bool main_loop_should_exit(int *status) · line 743

Quoted 10 lines
softmmu/runstate.c +6 -0
 
      while (!main_loop_should_exit(&status)) {
+#ifdef CONFIG_TCG
+        ti = profile_getclock();
+#endif
          main_loop_wait(false);
+#ifdef CONFIG_TCG
+        dev_time += profile_getclock() - ti;
+#endif
      }

What is this intending to collect? Because I don't think it measures anything. Certainly nothing related to TCG, CPUs or even devices.

softmmu/runstate.c static bool main_loop_should_exit(int *status)

Quoted 4 lines
softmmu/runstate.c +4 -0
+    /* ? won't this end up op_opt - op = del_op_count ? */
+    if (tb_stats_enabled(s->gen_tb, TB_JIT_STATS)) {
+        s->gen_tb->tb_stats->code.deleted_ops++;
+    }

Not quite. We can emit new ops as well. But how useful this is on its own is debatable.

softmmu/runstate.c

Quoted 5 lines
softmmu/runstate.c +5 -0
+/* avoid copy/paste errors */
+#define PROF_ADD(to, from, field)                       \
+    do {                                                \
+        (to)->field += qatomic_read(&((from)->field));  \
+    } while (0)

It is only used twice.
In addition, you can't use qatomic_read of a 64-bit variable on a 32-bit host. You really really need to build e.g. i386.

softmmu/runstate.c int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start) · line 5927

Quoted 6 lines
softmmu/runstate.c +1 -0
@@ -5879,6 +5923,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)
          }
      }
 
+
  #ifdef CONFIG_DEBUG_TCG

Stray.

r~

author
#20 lore raw

Re: [PATCH v14 04/10] accel/tcg: add jit stats and time to TBStatistics

Quoted 29 lines · Richard Henderson
Quoted 28 lines · Fei Wu

+static void collect_jit_profile_info(void *p, uint32_t hash, void *userp)

accel/tcg/tb-stats.c +26 -0
+{
+    struct jit_profile_info *jpi = userp;
+    TBStatistics *tbs = p;
+
+    jpi->translations += tbs->translations.total;
+    jpi->ops += tbs->code.num_tcg_ops;
+    if (stat_per_translation(tbs, code.num_tcg_ops) > jpi->ops_max) {
+        jpi->ops_max = stat_per_translation(tbs, code.num_tcg_ops);
+    }
+    jpi->del_ops += tbs->code.deleted_ops;
+    jpi->temps += tbs->code.temps;
+    if (stat_per_translation(tbs, code.temps) > jpi->temps_max) {
+        jpi->temps_max = stat_per_translation(tbs, code.temps);
+    }
+    jpi->host += tbs->code.out_len;
+    jpi->guest += tbs->code.in_len;
+    jpi->search_data += tbs->code.search_out_len;
+
+    jpi->interm_time += stat_per_translation(tbs, gen_times.ir);
+    jpi->opt_time += stat_per_translation(tbs, gen_times.ir_opt);
+    jpi->la_time += stat_per_translation(tbs, gen_times.la);
+    jpi->code_time += stat_per_translation(tbs, gen_times.code);
+
+    /*
+     * The restore time covers how long we have spent restoring state
+     * from a given TB (e.g. recovering from a fault). It is therefor
Quoted 9 lines
Quoted 5 lines

+     * not related to the number of translations we have done. +     */
+    jpi->restore_time += tbs->tb_restore_time;
+    jpi->restore_count += tbs->tb_restore_count;
+}

Why do sums of averages (stats_per_translation) instead of accumulating the complete total and dividing by jpi->translations?

There has some inconsistency.

Quoted 13 lines
Quoted 13 lines
accel/tcg/tb-stats.c +11 -0
+void dump_jit_exec_time_info(uint64_t dev_time, GString *buf)
+{
+    static uint64_t last_cpu_exec_time;
+    uint64_t cpu_exec_time;
+    uint64_t delta;
+
+    cpu_exec_time = tcg_cpu_exec_time();
+    delta = cpu_exec_time - last_cpu_exec_time;
+
+    g_string_append_printf(buf, "async time  %" PRId64 " (%0.3f)\n",
+                           dev_time, dev_time /

(double)NANOSECONDS_PER_SECOND);
+    g_string_append_printf(buf, "qemu time   %" PRId64 " (%0.3f)\n",

Quoted 9 lines

accel/tcg/tb-stats.c enum TBStatsStatus { · line 104

Quoted 6 lines

+                           delta, delta /
(double)NANOSECONDS_PER_SECOND);
+    last_cpu_exec_time = cpu_exec_time;

accel/tcg/tb-stats.c +3 -0
+}
+
+/* dump JIT statisticis using TCGProfile and TBStats */

"statistics"

ok.

Quoted 12 lines
Quoted 12 lines
accel/tcg/tcg-accel-ops.c +3 -0
index 3973591508..749ad182f2 100644
@@ -70,10 +70,17 @@ void tcg_cpus_destroy(CPUState *cpu)
   int tcg_cpus_exec(CPUState *cpu)
   {
       int ret;
+    uint64_t ti;
+
       assert(tcg_enabled());
+    ti = profile_getclock();
Quoted 8 lines

accel/tcg/tcg-accel-ops.c void tcg_cpus_destroy(CPUState *cpu) · line 82

Quoted 6 lines
accel/tcg/tcg-accel-ops.c +3 -0
+
       cpu_exec_start(cpu);
       ret = cpu_exec(cpu);
       cpu_exec_end(cpu);
+
+    qatomic_add(&tcg_ctx->prof.cpu_exec_time, profile_getclock() - ti);

You can't qatomic_add a 64-bit value on a 32-bit host.

Right, I only changed the counters to size_t, didn't fix time part. Is it possible to support it with some kind of locks on 32-bit as a generic fallback solution? After all, 32-bit host seems not popular, it might be sacrifice performance a little bit.

For me, the times 'dev_time' and 'cpu_exec_time' is only for the developer to get a sense of how large part of time spent on such as "ir" and "code". Alex mentioned in another thread that it's arguable if they are useful. If not, we can remove it.

Quoted 9 lines

Nor is tcg_ctx a good place to put this.

If you want to accumulate per-cpu data, put it on the cpu where there's no chance of racing with anyone.

Finally, I suspect that this isn't even where you want to accumulate time for execution as separate from translation.  You'd to that in cpu_exec_enter/cpu_exec_exit.

Quoted 10 lines

accel/tcg/tcg-accel-ops.c static TBStatistics *tb_get_stats(tb_page_addr_t · line 319

Quoted 6 lines
accel/tcg/tcg-accel-ops.c +1 -0
@@ -296,6 +315,8 @@ static TBStatistics *tb_get_stats(tb_page_addr_t
 phys_pc, target_ulong pc,
       new_stats->cs_base = cs_base;
       new_stats->flags = flags;
       new_stats->stats_enabled = get_default_tbstats_flag();
+    new_stats->tbs = g_ptr_array_sized_new(4);

Why default to 4?  Is that just a guess, or are we really recomputing tbs that frequently?  Is there a good reason not to use g_ptr_array_new()?

Is this the same question in 2019 :) I cannot find the original link right now. I will try to find it again later.

Quoted 12 lines

accel/tcg/translator.c line 22

Quoted 9 lines
accel/tcg/translator.c +1 -0
index 80ffbfb455..a60a92091b 100644
@@ -19,7 +19,7 @@
   #include "exec/plugin-gen.h"
   #include "exec/replay-core.h"
   -static void gen_tb_exec_count(TranslationBlock *tb)
+static inline void gen_tb_exec_count(TranslationBlock *tb)

Why?

will remove.

Quoted 5 lines
Quoted 5 lines
accel/tcg/translator.c +0 -0
   {
       if (tb_stats_enabled(tb, TB_EXEC_STATS)) {
           TCGv_ptr ptr = tcg_temp_ebb_new_ptr();
@@ -147,6 +147,11 @@ void translator_loop(CPUState *cpu,
 TranslationBlock *tb, int *max_insns,
Quoted 9 lines
Quoted 6 lines
accel/tcg/translator.c +0 -0
       tb->size = db->pc_next - db->pc_first;
       tb->icount = db->num_insns;

  +    /* Save number of guest instructions for TB_JIT_STATS */ +    if (tb_stats_enabled(tb, TB_JIT_STATS)) {
+        tb->tb_stats->code.num_guest_inst += db->num_insns; +    }

Why do this here, as opposed to the block in tb_gen_code?

close to scene of action?

Quoted 7 lines
Quoted 3 lines

+#define stat_per_translation(stat, name) \
+    (stat->translations.total ? stat->name / stat->translations.total : 0)

Not a fan of this macro, and as per above, the reason for doing the division here is questionable.

Quoted 14 lines

include/qemu/timer.h static inline int64_t cpu_get_host_ticks(void) · line 994

Quoted 11 lines
include/qemu/timer.h +3 -0
index 9a91cb1248..ad0da18a5f 100644
@@ -989,4 +989,10 @@ static inline int64_t cpu_get_host_ticks(void)
   }
   #endif
   +static inline int64_t profile_getclock(void)
+{
+    return get_clock();
+}

Why?  You use get_clock directly most places.

for the ones still in TCGProfile, but I think it's okay to remove.

Quoted 11 lines

include/qemu/timer.h static inline int64_t cpu_get_host_ticks(void)

Quoted 8 lines
include/qemu/timer.h +8 -0
+/* Timestamps during translation  */
+typedef struct TCGTime {
+    uint64_t start;
+    uint64_t ir_done;
+    uint64_t opt_done;
+    uint64_t la_done;
+    uint64_t code_done;
+} TCGTime;

int64_t would match the result of get_clock().

OK. btw, there are still several occurrences of u64 for time in the existing code.

Quoted 12 lines
Quoted 8 lines
include/qemu/timer.h +3 -0
+
+/*
+ * The TCGProfile structure holds data for the lifetime of the

translator.
+ */
+typedef struct TCGProfile {
+    /* exec time of this vcpu */
+    int64_t cpu_exec_time;

TCGContext is not per-cpu, and therefore TCGProfile does not correspond either.

Quoted 7 lines

include/qemu/timer.h struct TCGContext { · line 632

Quoted 4 lines
include/qemu/timer.h +1 -0
@@ -608,6 +630,7 @@ struct TCGContext {
         /* Exit to translator on overflow. */
       sigjmp_buf jmp_trans;
+    TranslationBlock *current_tb;

TCGContext.gen_tb already exists.

no user actually, will remove it.

Quoted 5 lines

include/qemu/timer.h struct TCGContext { · line 633

Quoted 2 lines
include/qemu/timer.h +1 -1
-int64_t tcg_cpu_exec_time(void);
+uint64_t tcg_cpu_exec_time(void);

Why?  (Also, probably wants removing, per above.)

The original patch mentioned 'make it an uint64_t as we won't be dealing in negative numbers.'

Quoted 13 lines
Quoted 13 lines
softmmu/runstate.c +6 -0
@@ -728,9 +728,18 @@ static bool main_loop_should_exit(int *status)
   int qemu_main_loop(void)
   {
       int status = EXIT_SUCCESS;
+#ifdef CONFIG_TCG
+    uint64_t ti;
+#endif
         while (!main_loop_should_exit(&status)) {
+#ifdef CONFIG_TCG
+        ti = profile_getclock();
+#endif
Quoted 9 lines

softmmu/runstate.c static bool main_loop_should_exit(int *status) · line 743

Quoted 5 lines
softmmu/runstate.c +3 -0
           main_loop_wait(false);
+#ifdef CONFIG_TCG
+        dev_time += profile_getclock() - ti;
+#endif
       }

What is this intending to collect?  Because I don't think it measures anything.  Certainly nothing related to TCG, CPUs or even devices.

It's exported to hmp cmd in dump_jit_exec_time_info() together with cpu_exec_time.

Quoted 8 lines

softmmu/runstate.c static bool main_loop_should_exit(int *status)

Quoted 4 lines
softmmu/runstate.c +4 -0
+    /* ? won't this end up op_opt - op = del_op_count ? */
+    if (tb_stats_enabled(s->gen_tb, TB_JIT_STATS)) {
+        s->gen_tb->tb_stats->code.deleted_ops++;
+    }

Not quite.  We can emit new ops as well.  But how useful this is on its own is debatable.

okay, so I will remove the comment.

Quoted 11 lines

softmmu/runstate.c

Quoted 5 lines
softmmu/runstate.c +5 -0
+/* avoid copy/paste errors */
+#define PROF_ADD(to, from, field)                       \
+    do {                                                \
+        (to)->field += qatomic_read(&((from)->field));  \
+    } while (0)

It is only used twice.
In addition, you can't use qatomic_read of a 64-bit variable on a 32-bit host.
You really really need to build e.g. i386.

Quoted 9 lines
Quoted 6 lines
softmmu/runstate.c +0 -0
@@ -5879,6 +5923,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock
 *tb, uint64_t pc_start)
           }
       }

  +
  #ifdef CONFIG_DEBUG_TCG

Stray.

will fix.

Thanks,
Fei.

Quoted 2 lines

r~

#21 lore raw

Re: [PATCH v14 04/10] accel/tcg: add jit stats and time to TBStatistics

Quoted 7 lines · Fei
Quoted 5 lines

include/tcg/tcg.h static inline TCGv_ptr tcg_temp_new_ptr(void) · line 912

Quoted 2 lines
include/tcg/tcg.h +1 -1
-int64_t tcg_cpu_exec_time(void);
+uint64_t tcg_cpu_exec_time(void);

Why?  (Also, probably wants removing, per above.)

The original patch mentioned 'make it an uint64_t as we won't be dealing in negative numbers.'

The signed vs unsigned thing is something that should be handled throughout everything that handles times, not adjusted here and there by only profiling.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
softmmu/runstate.c +6 -0
@@ -728,9 +728,18 @@ static bool main_loop_should_exit(int *status)
   int qemu_main_loop(void)
   {
       int status = EXIT_SUCCESS;
+#ifdef CONFIG_TCG
+    uint64_t ti;
+#endif
         while (!main_loop_should_exit(&status)) {
+#ifdef CONFIG_TCG
+        ti = profile_getclock();
+#endif
Quoted 9 lines

softmmu/runstate.c static bool main_loop_should_exit(int *status) · line 743

Quoted 5 lines
softmmu/runstate.c +3 -0
           main_loop_wait(false);
+#ifdef CONFIG_TCG
+        dev_time += profile_getclock() - ti;
+#endif
       }

What is this intending to collect?  Because I don't think it measures anything.  Certainly nothing related to TCG, CPUs or even devices.

Quoted 2 lines

It's exported to hmp cmd in dump_jit_exec_time_info() together with cpu_exec_time.

That doesn't answer my question: What do you think it measures?

r~

#22 lore raw

Re: [PATCH v14 04/10] accel/tcg: add jit stats and time to TBStatistics

In reply to Wu, Fei
Quoted 10 lines · Fei
Quoted 4 lines · Richard Henderson
Quoted 1 line · Fei Wu

+    qatomic_add(&tcg_ctx->prof.cpu_exec_time, profile_getclock() - ti);

You can't qatomic_add a 64-bit value on a 32-bit host.

Right, I only changed the counters to size_t, didn't fix time part. Is it possible to support it with some kind of locks on 32-bit as a generic fallback solution? After all, 32-bit host seems not popular, it might be sacrifice performance a little bit.

We have

int64_t qatomic_read_i64(const int64_t *ptr); uint64_t qatomic_read_u64(const uint64_t *ptr); void qatomic_set_i64(int64_t *ptr, int64_t val); void qatomic_set_u64(uint64_t *ptr, uint64_t val);

to support 32-bit hosts. You must also use

typedef int64_t aligned_int64_t __attribute__((aligned(8))); typedef uint64_t aligned_uint64_t __attribute__((aligned(8)));

r~

author
#23 lore raw

PATCH v14 5/10 debug: add -d tb_stats to control TBStatistics collection:

In reply to Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

-d tb_stats[[,level=(+all+jit+exec+time)][,dump_limit=<number>]]

"dump_limit" is used to limit the number of dumped TBStats in linux-user mode.

[all+jit+exec+time] control the profilling level used by the TBStats. Can be used as follow:

-d tb_stats
-d tb_stats,level=jit+time
-d tb_stats,dump_limit=15
...

Message-Id: <[email protected]>

[AJB: fix authorship, reword title]
---
 accel/tcg/tb-stats.c          |  5 +++++
 include/exec/gen-icount.h     |  1 +
 include/exec/tb-stats-flags.h |  3 +++
 include/exec/tb-stats.h       |  2 ++
 include/qemu/log.h            |  1 +
 stubs/meson.build             |  1 +
 stubs/tb-stats.c              | 27 +++++++++++++++++++++++++
 util/log.c                    | 37 +++++++++++++++++++++++++++++++++++
 8 files changed, 77 insertions(+)
 create mode 100644 stubs/tb-stats.c
accel/tcg/tb-stats.c +5 -0
index 78a3104c7f..68ac7d3f73 100644
@@ -208,3 +208,8 @@ uint32_t get_default_tbstats_flag(void)
 {
     return default_tbstats_flag;
 }
+
+void set_default_tbstats_flag(uint32_t flags)
+{
+    default_tbstats_flag = flags;
+}
include/exec/gen-icount.h +1 -0
index 20e7835ff0..4372817951 100644
@@ -3,6 +3,7 @@
 
 #include "exec/exec-all.h"
 #include "exec/tb-stats.h"
+#include "tb-stats-flags.h"
 
 /* Helpers for instruction counting code generation.  */
 
include/exec/tb-stats-flags.h +3 -0
index f29eff7576..04adaee8d9 100644
@@ -15,6 +15,7 @@
 #define TB_EXEC_STATS (1 << 1)
 #define TB_JIT_STATS  (1 << 2)
 #define TB_JIT_TIME   (1 << 3)
+#define TB_ALL_STATS  (TB_EXEC_STATS | TB_JIT_STATS | TB_JIT_TIME)
 
 /* TBStatistic collection controls */
 void enable_collect_tb_stats(void);
@@ -24,5 +25,7 @@ bool tb_stats_collection_enabled(void);
 bool tb_stats_collection_paused(void);
 
 uint32_t get_default_tbstats_flag(void);
+void set_default_tbstats_flag(uint32_t);
+void set_tbstats_flags(uint32_t flags);
 
 #endif
include/exec/tb-stats.h +2 -0
index d93d42e085..72585c448a 100644
@@ -31,6 +31,8 @@
 #include "exec/tb-stats-flags.h"
 #include "tcg/tcg.h"
 
+#include "exec/tb-stats-flags.h"
+
 #define tb_stats_enabled(tb, JIT_STATS) \
     (tb && tb->tb_stats && (tb->tb_stats->stats_enabled & JIT_STATS))
 
include/qemu/log.h +1 -0
index c5643d8dd5..6f3b8091cd 100644
@@ -35,6 +35,7 @@ bool qemu_log_separate(void);
 /* LOG_STRACE is used for user-mode strace logging. */
 #define LOG_STRACE         (1 << 19)
 #define LOG_PER_THREAD     (1 << 20)
+#define CPU_LOG_TB_STATS   (1 << 21)
 
 /* Lock/unlock output. */
 
stubs/meson.build +1 -0
index a56645e2f7..e926649d40 100644
@@ -65,3 +65,4 @@ else
 endif
 stub_ss.add(files('semihost-all.c'))
 stub_ss.add(when: 'CONFIG_VFIO_USER_SERVER', if_false: files('vfio-user-obj.c'))
+stub_ss.add(files('tb-stats.c'))
stubs/tb-stats.c new file +27 -0
new file mode 100644
index 0000000000..d212c2a1fa
@@ -0,0 +1,27 @@
+/*
+ * TB Stats Stubs
+ *
+ * Copyright (c) 2019
+ * Written by Alex Bennée <[email protected]>
+ *
+ * This code is licensed under the GNU GPL v2, or later.
+ */
+
+
+#include "qemu/osdep.h"
+#include "exec/tb-stats-flags.h"
+
+void enable_collect_tb_stats(void)
+{
+    return;
+}
+
+bool tb_stats_collection_enabled(void)
+{
+    return false;
+}
+
+void set_default_tbstats_flag(uint32_t flags)
+{
+    return;
+}
util/log.c +37 -0
index 53b4f6c58e..7ae471d97c 100644
@@ -19,6 +19,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/log.h"
+#include "qemu/qemu-print.h"
 #include "qemu/range.h"
 #include "qemu/error-report.h"
 #include "qapi/error.h"
@@ -27,6 +28,7 @@
 #include "qemu/thread.h"
 #include "qemu/lockable.h"
 #include "qemu/rcu.h"
+#include "exec/tb-stats-flags.h"
 #ifdef CONFIG_LINUX
 #include <sys/syscall.h>
 #endif
@@ -47,6 +49,7 @@ static __thread Notifier qemu_log_thread_cleanup_notifier;
 int qemu_loglevel;
 static bool log_per_thread;
 static GArray *debug_regions;
+int32_t max_num_hot_tbs_to_dump;
 
 /* Returns true if qemu_log() will really write somewhere. */
 bool qemu_log_enabled(void)
@@ -495,6 +498,10 @@ const QEMULogItem qemu_log_items[] = {
       "log every user-mode syscall, its input, and its result" },
     { LOG_PER_THREAD, "tid",
       "open a separate log file per thread; filename must contain '%d'" },
+    { CPU_LOG_TB_STATS,
+      "tb_stats[[,level=(+all+jit+exec+time)][,dump_limit=<number>]]",
+      "enable collection of TBs statistics"
+      "(and dump until given a limit if in user mode).\n" },
     { 0, NULL, NULL },
 };
 
@@ -516,6 +523,36 @@ int qemu_str_to_log_mask(const char *str)
             trace_enable_events((*tmp) + 6);
             mask |= LOG_TRACE;
 #endif
+        } else if (g_str_has_prefix(*tmp, "tb_stats")) {
+            mask |= CPU_LOG_TB_STATS;
+            set_default_tbstats_flag(TB_ALL_STATS);
+            enable_collect_tb_stats();
+        } else if (tb_stats_collection_enabled() &&
+                   g_str_has_prefix(*tmp, "dump_limit=")) {
+            max_num_hot_tbs_to_dump = atoi((*tmp) + 11);
+        } else if (tb_stats_collection_enabled() &&
+                   g_str_has_prefix(*tmp, "level=")) {
+            uint32_t flags = 0;
+            char **level_parts = g_strsplit(*tmp + 6, "+", 0);
+            char **level_tmp;
+            for (level_tmp = level_parts; level_tmp && *level_tmp;
+                    level_tmp++) {
+                if (g_str_equal(*level_tmp, "jit")) {
+                    flags |= TB_JIT_STATS;
+                } else if (g_str_equal(*level_tmp, "exec")) {
+                    flags |= TB_EXEC_STATS;
+                } else if (g_str_equal(*level_tmp, "time")) {
+                    flags |= TB_JIT_TIME;
+                } else if (g_str_equal(*level_tmp, "all")) {
+                    flags |= TB_ALL_STATS;
+                } else {
+                    /* FIXME: set errp */
+                    fprintf(stderr, "no option level=%s, valid options are:"
+                            "all, jit, exec or/and time\n", *level_tmp);
+                    exit(1);
+                }
+                set_default_tbstats_flag(flags);
+            }
         } else {
             for (item = qemu_log_items; item->mask != 0; item++) {
                 if (g_str_equal(*tmp, item->name)) {
Signature · 1 line
2.25.1
#24 lore raw

Re: [PATCH v14 05/10] debug: add -d tb_stats to control TBStatistics collection:

Quoted 6 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

-d tb_stats[[,level=(+all+jit+exec+time)][,dump_limit=<number>]]

"dump_limit" is used to limit the number of dumped TBStats in linux-user mode.

Why is user-mode special?

Review

Quoted 6 lines

[all+jit+exec+time] control the profilling level used by the TBStats. Can be used as follow:

(quoted diff) +0 -2
-d tb_stats
-d tb_stats,level=jit+time

Comma is already used to separate different -d options. You should not overload that.

"level" doesn't make sense for things that aren't hierarchical.

What's wrong with tb-stats-{all,jit,time,exec}?

r~

author
#25 lore raw

Re: [PATCH v14 05/10] debug: add -d tb_stats to control TBStatistics collection:

Quoted 10 lines · Richard Henderson
Quoted 6 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

  -d tb_stats[[,level=(+all+jit+exec+time)][,dump_limit=<number>]]

"dump_limit" is used to limit the number of dumped TBStats in linux-user mode.

Why is user-mode special?

Should be an issue in comment, not in code.

Quoted 10 lines

Review

Quoted 6 lines

[all+jit+exec+time] control the profilling level used by the TBStats. Can be used as follow:

(quoted diff) +0 -2
-d tb_stats
-d tb_stats,level=jit+time

Comma is already used to separate different -d options. You should not overload that.

Quoted 4 lines

"level" doesn't make sense for things that aren't hierarchical.

What's wrong with tb-stats-{all,jit,time,exec}?

Let me try.

Thanks,
Fei.

Quoted 2 lines

r~

author
#26 lore raw

PATCH v14 6/10 monitor: adding tb_stats hmp command

In reply to Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

Adding tb_stats [start|pause|stop|filter] command to hmp. This allows controlling the collection of statistics. It is also possible to set the level of collection:
all, jit, or exec.

tb_stats filter allow to only collect statistics for the TB in the last_search list.

The goal of this command is to allow the dynamic exploration of the TCG behavior and quality. Therefore, for now, a corresponding QMP command is not worthwhile.

Message-Id: <[email protected]>
Message-Id: <[email protected]>

[AJB: fix authorship]
---
 accel/tcg/monitor.c           |  45 +++++++++++++
 accel/tcg/tb-stats.c          | 121 +++++++++++++++++++++++++++++++++-
 hmp-commands.hx               |  16 +++++
 include/exec/tb-stats-flags.h |   2 +
 include/exec/tb-stats.h       |  10 +++
 include/monitor/hmp.h         |   1 +
 6 files changed, 192 insertions(+), 3 deletions(-)
accel/tcg/monitor.c +45 -0
index 2bc87f2642..2e00f10267 100644
@@ -11,7 +11,9 @@
 #include "qapi/error.h"
 #include "qapi/type-helpers.h"
 #include "qapi/qapi-commands-machine.h"
+#include "qapi/qmp/qdict.h"
 #include "monitor/monitor.h"
+#include "monitor/hmp.h"
 #include "sysemu/cpus.h"
 #include "sysemu/cpu-timers.h"
 #include "sysemu/tcg.h"
@@ -87,6 +89,49 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
 }
 
 #ifdef CONFIG_TCG
+void hmp_tbstats(Monitor *mon, const QDict *qdict)
+{
+    if (!tcg_enabled()) {
+        error_report("TB information is only available with accel=tcg");
+        return;
+    }
+
+    char *cmd = (char *) qdict_get_try_str(qdict, "command");
+    enum TbstatsCmd icmd = -1;
+
+    if (strcmp(cmd, "start") == 0) {
+        icmd = START;
+    } else if (strcmp(cmd, "pause") == 0) {
+        icmd = PAUSE;
+    } else if (strcmp(cmd, "stop") == 0) {
+        icmd = STOP;
+    } else if (strcmp(cmd, "filter") == 0) {
+        icmd = FILTER;
+    } else {
+        error_report("invalid command!");
+        return;
+    }
+
+    char *slevel = (char *) qdict_get_try_str(qdict, "level");
+    uint32_t level = TB_EXEC_STATS | TB_JIT_STATS | TB_JIT_TIME;
+    if (slevel) {
+        if (strcmp(slevel, "jit") == 0) {
+            level = TB_JIT_STATS;
+        } else if (strcmp(slevel, "exec") == 0) {
+            level = TB_EXEC_STATS;
+        } else if (strcmp(slevel, "time") == 0) {
+            level = TB_JIT_TIME;
+        }
+    }
+
+    struct TbstatsCommand *tbscommand = g_new0(struct TbstatsCommand, 1);
+    tbscommand->cmd = icmd;
+    tbscommand->level = level;
+    async_safe_run_on_cpu(first_cpu, do_hmp_tbstats_safe,
+                          RUN_ON_CPU_HOST_PTR(tbscommand));
+
+}
+
 HumanReadableText *qmp_x_query_profile(Error **errp)
 {
     g_autoptr(GString) buf = g_string_new("");
accel/tcg/tb-stats.c +118 -3

160 lines not on this page · raw message

hmp-commands.hx +16 -0
index 2cbd0f77a0..9a40215d34 100644
@@ -1670,6 +1670,22 @@ SRST
   Executes a qemu-io command on the given block device.
 ERST
 
+#if defined(CONFIG_TCG)
+    {
+        .name       = "tb_stats",
+        .args_type  = "command:s,level:s?",
+        .params     = "command [stats_level]",
+        .help       = "Control tb statistics collection:"
+                        "tb_stats (start|pause|stop|filter) [all|jit_stats|exec_stats]",
+        .cmd        = hmp_tbstats,
+    },
+#endif
+
+SRST
+``tb_stats`` *command* *stats_level*
+  Control recording tb statistics
+ERST
+
     {
         .name       = "qom-list",
         .args_type  = "path:s?",
include/exec/tb-stats-flags.h +2 -0
index 04adaee8d9..a3897c99b1 100644
@@ -16,12 +16,14 @@
 #define TB_JIT_STATS  (1 << 2)
 #define TB_JIT_TIME   (1 << 3)
 #define TB_ALL_STATS  (TB_EXEC_STATS | TB_JIT_STATS | TB_JIT_TIME)
+#define TB_PAUSED     (1 << 4)
 
 /* TBStatistic collection controls */
 void enable_collect_tb_stats(void);
 void disable_collect_tb_stats(void);
 void pause_collect_tb_stats(void);
 bool tb_stats_collection_enabled(void);
+bool tb_stats_collection_disabled(void);
 bool tb_stats_collection_paused(void);
 
 uint32_t get_default_tbstats_flag(void);
include/exec/tb-stats.h +10 -0
index 72585c448a..4bb343870b 100644
@@ -33,6 +33,9 @@
 
 #include "exec/tb-stats-flags.h"
 
+enum SortBy { SORT_BY_HOTNESS, SORT_BY_HG /* Host/Guest */, SORT_BY_SPILLS };
+enum TbstatsCmd { START, PAUSE, STOP, FILTER };
+
 #define tb_stats_enabled(tb, JIT_STATS) \
     (tb && tb->tb_stats && (tb->tb_stats->stats_enabled & JIT_STATS))
 
@@ -114,4 +117,11 @@ void init_tb_stats_htable(void);
 void dump_jit_profile_info(TCGProfile *s, GString *buf);
 void dump_jit_exec_time_info(uint64_t dev_time, GString *buf);
 
+struct TbstatsCommand {
+    enum TbstatsCmd cmd;
+    uint32_t level;
+};
+
+void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd);
+
 #endif
include/monitor/hmp.h +1 -0
index 13f9a2dedb..2e7f141754 100644
@@ -181,5 +181,6 @@ void hmp_ioport_write(Monitor *mon, const QDict *qdict);
 void hmp_boot_set(Monitor *mon, const QDict *qdict);
 void hmp_info_mtree(Monitor *mon, const QDict *qdict);
 void hmp_info_cryptodev(Monitor *mon, const QDict *qdict);
+void hmp_tbstats(Monitor *mon, const QDict *qdict);
 
 #endif
Signature · 1 line
2.25.1
#27 lore raw

Re: [PATCH v14 06/10] monitor: adding tb_stats hmp command

Quoted 14 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

Adding tb_stats [start|pause|stop|filter] command to hmp. This allows controlling the collection of statistics. It is also possible to set the level of collection:
all, jit, or exec.

tb_stats filter allow to only collect statistics for the TB in the last_search list.

The goal of this command is to allow the dynamic exploration of the TCG behavior and quality. Therefore, for now, a corresponding QMP command is not worthwhile.

Quoted 8 lines

Message-Id: <[email protected]>
Message-Id: <[email protected]>

[AJB: fix authorship]

---

I still don't see what pause does.

Quoted 9 lines
accel/tcg/tb-stats.c +1 -0
index 68ac7d3f73..805e1fc74d 100644
@@ -16,18 +16,20 @@
  #include "qemu/timer.h"
 
  #include "exec/tb-stats.h"
+#include "exec/tb-flush.h"

accel/tcg/tb-stats.c line 27

Quoted 10 lines
accel/tcg/tb-stats.c +1 -2
  #include "tb-context.h"
 
  /* TBStatistic collection controls */
  enum TBStatsStatus {
      TB_STATS_DISABLED = 0,
      TB_STATS_RUNNING,
-    TB_STATS_PAUSED,
-    TB_STATS_STOPPED
+    TB_STATS_PAUSED
  };

Why?

Quoted 96 lines
accel/tcg/tb-stats.c +86 -0
  static enum TBStatsStatus tcg_collect_tb_stats;
  static uint32_t default_tbstats_flag;
+/* only accessed in safe work */
+static GList *last_search;
 
  uint64_t dev_time;
 
@@ -170,6 +172,101 @@ void dump_jit_profile_info(TCGProfile *s, GString *buf)
      g_free(jpi);
  }
 
+static void free_tbstats(void *p, uint32_t hash, void *userp)
+{
+    g_free(p);
+}
+
+static void clean_tbstats(void)
+{
+    /* remove all tb_stats */
+    qht_iter(&tb_ctx.tb_stats, free_tbstats, NULL);
+    qht_destroy(&tb_ctx.tb_stats);
+}
+
+void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd)
+{
+    struct TbstatsCommand *cmdinfo = icmd.host_ptr;
+    int cmd = cmdinfo->cmd;
+    uint32_t level = cmdinfo->level;
+
+    switch (cmd) {
+    case START:
+        if (tb_stats_collection_enabled()) {
+            qemu_printf("TB information already being recorded");
+            return;
+        } else if (tb_stats_collection_paused()) {
+            set_tbstats_flags(level);
+        } else {
+            qht_init(&tb_ctx.tb_stats, tb_stats_cmp, CODE_GEN_HTABLE_SIZE,
+                     QHT_MODE_AUTO_RESIZE);
+        }
+
+        set_default_tbstats_flag(level);
+        enable_collect_tb_stats();
+        tb_flush(cpu);
+        break;
+    case PAUSE:
+        if (!tb_stats_collection_enabled()) {
+            qemu_printf("TB information not being recorded");
+            return;
+        }
+
+        /*
+         * Continue to create TBStatistic structures but stop collecting
+         * statistics
+         */
+        pause_collect_tb_stats();
+        set_default_tbstats_flag(TB_NOTHING);
+        set_tbstats_flags(TB_PAUSED);
+        tb_flush(cpu);
+        break;
+    case STOP:
+        if (tb_stats_collection_disabled()) {
+            qemu_printf("TB information not being recorded");
+            return;
+        }
+
+        /* Dissalloc all TBStatistics structures and stop creating new ones */
+        disable_collect_tb_stats();
+        clean_tbstats();
+        tb_flush(cpu);
+        break;
+    case FILTER:
+        if (!tb_stats_collection_enabled()) {
+            qemu_printf("TB information not being recorded");
+            return;
+        }
+        if (!last_search) {
+            qemu_printf(
+                    "no search on record! execute info tbs before filtering!");
+            return;
+        }
+
+        set_default_tbstats_flag(TB_NOTHING);
+
+        /*
+         * Set all tbstats as paused, then return only the ones from last_search
+         */
+        pause_collect_tb_stats();
+        set_tbstats_flags(TB_PAUSED);
+
+        for (GList *iter = last_search; iter; iter = g_list_next(iter)) {
+            TBStatistics *tbs = iter->data;
+            tbs->stats_enabled = level;
+        }
+

accel/tcg/tb-stats.c void dump_jit_profile_info(TCGProfile *s, GString *buf) · line 268

Quoted 10 lines
accel/tcg/tb-stats.c +10 -0
+        tb_flush(cpu);
+
+        break;
+    default: /* INVALID */
+        g_assert_not_reached();
+        break;
+    }
+
+    g_free(cmdinfo);
+}

Why isn't all of this in monitor.c?
It's not used or usable from user-only mode.

accel/tcg/tb-stats.c bool tb_stats_collection_enabled(void) · line 320

Quoted 5 lines
accel/tcg/tb-stats.c +5 -0
+void set_tbstats_flags(uint32_t flag)
+{
+    /* iterate over tbstats setting their flag as TB_NOTHING */
+    qht_iter(&tb_ctx.tb_stats, reset_tbstats_flag, &flag);
+}

Again, I question why a global variable is not more appropriate.

r~

author
#28 lore raw

Re: [PATCH v14 06/10] monitor: adding tb_stats hmp command

Quoted 25 lines · Richard Henderson
Quoted 14 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

Adding tb_stats [start|pause|stop|filter] command to hmp. This allows controlling the collection of statistics. It is also possible to set the level of collection:
all, jit, or exec.

tb_stats filter allow to only collect statistics for the TB in the last_search list.

The goal of this command is to allow the dynamic exploration of the TCG behavior and quality. Therefore, for now, a corresponding QMP command is not worthwhile.

Quoted 8 lines

Message-Id: <[email protected]>
Message-Id: <[email protected]>

[AJB: fix authorship]

---

Quoted 2 lines

I still don't see what pause does.

maybe it's necessary to discuss the user scenario, I suppose the original design is for this case:
* start
* pause - there are some interesting stats we want to keep
* not interested timeline
* start again - continue to investigate the interesting ones

Quoted 11 lines
Quoted 11 lines
accel/tcg/tb-stats.c +1 -0
index 68ac7d3f73..805e1fc74d 100644
@@ -16,18 +16,20 @@
   #include "qemu/timer.h"
     #include "exec/tb-stats.h"
+#include "exec/tb-flush.h"
   #include "tb-context.h"
     /* TBStatistic collection controls */
   enum TBStatsStatus {
Quoted 9 lines

accel/tcg/tb-stats.c line 27

Quoted 6 lines
accel/tcg/tb-stats.c +1 -2
       TB_STATS_DISABLED = 0,
       TB_STATS_RUNNING,
-    TB_STATS_PAUSED,
-    TB_STATS_STOPPED
+    TB_STATS_PAUSED
   };

Why?

STOPPED is the same as DISABLED.

Quoted 103 lines
Quoted 103 lines
accel/tcg/tb-stats.c +28 -0
     static enum TBStatsStatus tcg_collect_tb_stats;
   static uint32_t default_tbstats_flag;
+/* only accessed in safe work */
+static GList *last_search;
     uint64_t dev_time;
   @@ -170,6 +172,101 @@ void dump_jit_profile_info(TCGProfile *s,
 GString *buf)
       g_free(jpi);
   }
   +static void free_tbstats(void *p, uint32_t hash, void *userp)
+{
+    g_free(p);
+}
+
+static void clean_tbstats(void)
+{
+    /* remove all tb_stats */
+    qht_iter(&tb_ctx.tb_stats, free_tbstats, NULL);
+    qht_destroy(&tb_ctx.tb_stats);
+}
+
+void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd)
+{
+    struct TbstatsCommand *cmdinfo = icmd.host_ptr;
+    int cmd = cmdinfo->cmd;
+    uint32_t level = cmdinfo->level;
+
+    switch (cmd) {
+    case START:
+        if (tb_stats_collection_enabled()) {
+            qemu_printf("TB information already being recorded");
+            return;
+        } else if (tb_stats_collection_paused()) {
+            set_tbstats_flags(level);
+        } else {
+            qht_init(&tb_ctx.tb_stats, tb_stats_cmp,

CODE_GEN_HTABLE_SIZE,
+                     QHT_MODE_AUTO_RESIZE);
+        }

accel/tcg/tb-stats.c +27 -0
+
+        set_default_tbstats_flag(level);
+        enable_collect_tb_stats();
+        tb_flush(cpu);
+        break;
+    case PAUSE:
+        if (!tb_stats_collection_enabled()) {
+            qemu_printf("TB information not being recorded");
+            return;
+        }
+
+        /*
+         * Continue to create TBStatistic structures but stop collecting
+         * statistics
+         */
+        pause_collect_tb_stats();
+        set_default_tbstats_flag(TB_NOTHING);
+        set_tbstats_flags(TB_PAUSED);
+        tb_flush(cpu);
+        break;
+    case STOP:
+        if (tb_stats_collection_disabled()) {
+            qemu_printf("TB information not being recorded");
+            return;
+        }
+
+        /* Dissalloc all TBStatistics structures and stop creating

new ones */
+        disable_collect_tb_stats();
+        clean_tbstats();
+        tb_flush(cpu);
+        break;
+    case FILTER:
+        if (!tb_stats_collection_enabled()) {
+            qemu_printf("TB information not being recorded"); +            return;
+        }
+        if (!last_search) {
+            qemu_printf(
+                    "no search on record! execute info tbs before filtering!");
+            return;
+        }

accel/tcg/tb-stats.c +5 -0
+
+        set_default_tbstats_flag(TB_NOTHING);
+
+        /*
+         * Set all tbstats as paused, then return only the ones from

last_search
+         */
+        pause_collect_tb_stats();
+        set_tbstats_flags(TB_PAUSED);
+
+        for (GList *iter = last_search; iter; iter = g_list_next(iter)) {
+            TBStatistics *tbs = iter->data; +            tbs->stats_enabled = level;
+        }

accel/tcg/tb-stats.c +6 -0
+
+        tb_flush(cpu);
+
+        break;
+    default: /* INVALID */
+        g_assert_not_reached();
Quoted 9 lines

accel/tcg/tb-stats.c void dump_jit_profile_info(TCGProfile *s, GString *buf) · line 268

Quoted 5 lines

+        break;
+    }

accel/tcg/tb-stats.c +3 -0
+
+    g_free(cmdinfo);
+}

Why isn't all of this in monitor.c?
It's not used or usable from user-only mode.

closer to tb-stats, or closer to monitor? It seems to me monitor.c only contains the shim layer of qmp/hmp, the real stuffs which are large enough are put together with their logic part, e.g. dump_exec_info().

Thanks,
Fei.

Quoted 10 lines

accel/tcg/tb-stats.c bool tb_stats_collection_enabled(void) · line 320

Quoted 5 lines
accel/tcg/tb-stats.c +5 -0
+void set_tbstats_flags(uint32_t flag)
+{
+    /* iterate over tbstats setting their flag as TB_NOTHING */
+    qht_iter(&tb_ctx.tb_stats, reset_tbstats_flag, &flag);
+}

Again, I question why a global variable is not more appropriate.

r~

#29 lore raw

Re: [PATCH v14 06/10] monitor: adding tb_stats hmp command

Quoted 26 lines · Fei
Quoted 25 lines · Richard Henderson
Quoted 22 lines · Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

Adding tb_stats [start|pause|stop|filter] command to hmp. This allows controlling the collection of statistics. It is also possible to set the level of collection:
all, jit, or exec.

tb_stats filter allow to only collect statistics for the TB in the last_search list.

The goal of this command is to allow the dynamic exploration of the TCG behavior and quality. Therefore, for now, a corresponding QMP command is not worthwhile.

Message-Id: <[email protected]>
Message-Id: <[email protected]>

[AJB: fix authorship]

---

Quoted 8 lines
Quoted 2 lines

I still don't see what pause does.

maybe it's necessary to discuss the user scenario, I suppose the original design is for this case:
* start
* pause - there are some interesting stats we want to keep
* not interested timeline
* start again - continue to investigate the interesting ones

This use case seems very complicated. Perhaps start with something simpler.

Quoted 11 lines
Quoted 10 lines
Quoted 7 lines

  enum TBStatsStatus {
      TB_STATS_DISABLED = 0,
      TB_STATS_RUNNING,
-    TB_STATS_PAUSED,
-    TB_STATS_STOPPED
+    TB_STATS_PAUSED
  };

Why?

STOPPED is the same as DISABLED.

Then this should be squashed back to patch 2, so that it is never added.

Quoted 6 lines
Quoted 3 lines

Why isn't all of this in monitor.c?
It's not used or usable from user-only mode.

closer to tb-stats, or closer to monitor? It seems to me monitor.c only contains the shim layer of qmp/hmp, the real stuffs which are large enough are put together with their logic part, e.g. dump_exec_info().

I mean accel/tcg/monitor.c, which is built only when there exists a monitor.

r~

author
#30 lore raw

PATCH v14 7/10 tb-stats: reset the tracked TBs on a tb_flush

In reply to Fei Wu

From: Alex Bennée <[email protected]>

We keep track of translations but can only do so up until the translation cache is flushed. At that point we really have no idea if we can re-create a translation because all the active tracking information has been reset.

---
 accel/tcg/tb-maint.c    |  1 +
 accel/tcg/tb-stats.c    | 19 +++++++++++++++++++
 include/exec/tb-stats.h |  8 ++++++++
 3 files changed, 28 insertions(+)
accel/tcg/tb-maint.c +1 -0
index 0980fca358..11ff0ddd90 100644
@@ -763,6 +763,7 @@ static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count)
     qht_reset_size(&tb_ctx.htable, CODE_GEN_HTABLE_SIZE);
     tb_remove_all();
 
+    tbstats_reset_tbs();
     tcg_region_reset_all();
     /* XXX: flush processor icache at this point if cache flush is expensive */
     qatomic_inc(&tb_ctx.tb_flush_count);
accel/tcg/tb-stats.c +19 -0
index 805e1fc74d..139f049ffc 100644
@@ -267,6 +267,25 @@ void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd)
     g_free(cmdinfo);
 }
 
+/*
+ * We have to reset the tbs array on a tb_flush as those
+ * TranslationBlocks no longer exist and we no loner know if the
+ * current mapping is still valid.
+ */
+
+static void reset_tbs_array(void *p, uint32_t hash, void *userp)
+{
+    TBStatistics *tbs = p;
+    g_ptr_array_set_size(tbs->tbs, 0);
+}
+
+void tbstats_reset_tbs(void)
+{
+    if (tb_ctx.tb_stats.map) {
+        qht_iter(&tb_ctx.tb_stats, reset_tbs_array, NULL);
+    }
+}
+
 void init_tb_stats_htable(void)
 {
     if (!tb_ctx.tb_stats.map && tb_stats_collection_enabled()) {
include/exec/tb-stats.h +8 -0
index 4bb343870b..30b788f7b2 100644
@@ -124,4 +124,12 @@ struct TbstatsCommand {
 
 void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd);
 
+/**
+ * tbstats_reset_tbs: reset the linked array of TBs
+ *
+ * Reset the list of tbs for a given array. Should be called from
+ * safe work during tb_flush.
+ */
+void tbstats_reset_tbs(void);
+
 #endif
Signature · 1 line
2.25.1
#31 lore raw

Re: [PATCH v14 07/10] tb-stats: reset the tracked TBs on a tb_flush

accel/tcg/tb-stats.c void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd) · line 274

Quoted 13 lines · Fei Wu
accel/tcg/tb-stats.c +5 -0
index 805e1fc74d..139f049ffc 100644
@@ -267,6 +267,25 @@ void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd)
      g_free(cmdinfo);
  }
 
+/*
+ * We have to reset the tbs array on a tb_flush as those
+ * TranslationBlocks no longer exist and we no loner know if the
+ * current mapping is still valid.
+ */

The "and we no longer..." part is irrelevant: that's what phys_pc checks. But the TranslationBlocks no longer exist, so that is sufficient.

r~

author
#32 lore raw

Re: [PATCH v14 07/10] tb-stats: reset the tracked TBs on a tb_flush

Quoted 18 lines · Richard Henderson
Quoted 8 lines · Fei Wu
accel/tcg/tb-stats.c +0 -0
index 805e1fc74d..139f049ffc 100644
@@ -267,6 +267,25 @@ void do_hmp_tbstats_safe(CPUState *cpu,
 run_on_cpu_data icmd)
       g_free(cmdinfo);
   }
Quoted 5 lines

  +/*
+ * We have to reset the tbs array on a tb_flush as those
+ * TranslationBlocks no longer exist and we no loner know if the
+ * current mapping is still valid.
+ */

The "and we no longer..." part is irrelevant: that's what phys_pc checks. But the TranslationBlocks no longer exist, so that is sufficient.

Will remove this part from comment.

Thanks,
Fei.

Quoted 2 lines

r~

author
#33 lore raw

PATCH v14 8/10 Adding info [tb-list|tb] commands to HMP (WIP)

In reply to Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

These commands allow the exploration of TBs generated by the TCG. Understand which one hotter, with more guest/host instructions... and examine their guest, host and IR code.

The goal of this command is to allow the dynamic exploration of TCG behavior and code quality. Therefore, for now, a corresponding QMP command is not worthwhile.

[AJB: WIP - we still can't be safely sure a translation will succeed]

Example of output:

TB id:1 | phys:0x34d54 virt:0x0000000000034d54 flags:0x0000f0

	| exec:4828932/0 guest inst cov:16.38%
	| trans:1 ints: g:3 op:82 op_opt:34 spills:3
	| h/g (host bytes / guest insts): 90.666664
	| time to gen at 2.4GHz => code:3150.83(ns) IR:712.08(ns)
	| targets: 0x0000000000034d5e (id:11), 0x0000000000034d0d (id:2)

TB id:2 | phys:0x34d0d virt:0x0000000000034d0d flags:0x0000f0

	| exec:4825842/0 guest inst cov:21.82%
	| trans:1 ints: g:4 op:80 op_opt:38 spills:2
	| h/g (host bytes / guest insts): 84.000000
	| time to gen at 2.4GHz => code:3362.92(ns) IR:793.75(ns)
	| targets: 0x0000000000034d19 (id:12), 0x0000000000034d54 (id:1)

TB id:2 | phys:0x34d0d virt:0x0000000000034d0d flags:0x0000f0

	| exec:6956495/0  guest inst cov:21.82%
	| trans:2 ints: g:2 op:40 op_opt:19 spills:1
	| h/g (host bytes / guest insts): 84.000000
	| time to gen at 2.4GHz => code:3130.83(ns) IR:722.50(ns)
	| targets: 0x0000000000034d19 (id:12), 0x0000000000034d54 (id:1)

----------------
IN:

0x00034d0d:  89 de                    movl     %ebx, %esi
0x00034d0f:  26 8b 0e                 movl     %es:(%esi), %ecx
0x00034d12:  26 f6 46 08 80           testb    $0x80, %es:8(%esi)
0x00034d17:  75 3b                    jne      0x34d54

------------------------------

TB id:1 | phys:0x34d54 virt:0x0000000000034d54 flags:0x0000f0

	| exec:5202686/0 guest inst cov:11.28%
	| trans:1 ints: g:3 op:82 op_opt:34 spills:3
	| h/g (host bytes / guest insts): 90.666664
	| time to gen at 2.4GHz => code:2793.75(ns) IR:614.58(ns)
	| targets: 0x0000000000034d5e (id:3), 0x0000000000034d0d (id:2)

TB id:2 | phys:0x34d0d virt:0x0000000000034d0d flags:0x0000f0

	| exec:5199468/0 guest inst cov:15.03%
	| trans:1 ints: g:4 op:80 op_opt:38 spills:2
	| h/g (host bytes / guest insts): 84.000000
	| time to gen at 2.4GHz => code:2958.75(ns) IR:719.58(ns)
	| targets: 0x0000000000034d19 (id:4), 0x0000000000034d54 (id:1)

------------------------------
2 TBs to reach 25% of guest inst exec coverage
Total of guest insts exec: 138346727

------------------------------

Message-Id: <[email protected]>

[AJB: fix authorship, dropped coverset]
---
 accel/tcg/monitor.c          |  54 ++++++
 accel/tcg/tb-stats.c         | 322 +++++++++++++++++++++++++++++++++++
 disas/disas.c                |  26 ++-
 hmp-commands-info.hx         |  16 ++
 include/exec/tb-stats.h      |  33 +++-
 include/monitor/hmp.h        |   2 +
 include/qemu/log-for-trace.h |   6 +-
 include/qemu/log.h           |   2 +
 util/log.c                   |  66 +++++--
 9 files changed, 508 insertions(+), 19 deletions(-)
accel/tcg/monitor.c +54 -0
index 2e00f10267..a1780c5920 100644
@@ -8,6 +8,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/accel.h"
+#include "qemu/log.h"
 #include "qapi/error.h"
 #include "qapi/type-helpers.h"
 #include "qapi/qapi-commands-machine.h"
@@ -18,6 +19,7 @@
 #include "sysemu/cpu-timers.h"
 #include "sysemu/tcg.h"
 #include "exec/tb-stats.h"
+#include "tb-context.h"
 #include "internal.h"
 
 
@@ -132,6 +134,58 @@ void hmp_tbstats(Monitor *mon, const QDict *qdict)
 
 }
 
+void hmp_info_tblist(Monitor *mon, const QDict *qdict)
+{
+    int number_int;
+    const char *sortedby_str = NULL;
+    if (!tcg_enabled()) {
+        error_report("TB information is only available with accel=tcg");
+        return;
+    }
+    if (!tb_ctx.tb_stats.map) {
+        error_report("no TB information recorded");
+        return;
+    }
+
+    number_int = qdict_get_try_int(qdict, "number", 10);
+    sortedby_str = qdict_get_try_str(qdict, "sortedby");
+
+    int sortedby = SORT_BY_HOTNESS;
+    if (sortedby_str == NULL || strcmp(sortedby_str, "hotness") == 0) {
+        sortedby = SORT_BY_HOTNESS;
+    } else if (strcmp(sortedby_str, "hg") == 0) {
+        sortedby = SORT_BY_HG;
+    } else if (strcmp(sortedby_str, "spills") == 0) {
+        sortedby = SORT_BY_SPILLS;
+    } else {
+        error_report("valid sort options are: hotness hg spills");
+        return;
+    }
+
+    dump_tbs_info(number_int, sortedby, true);
+}
+
+void hmp_info_tb(Monitor *mon, const QDict *qdict)
+{
+    const int id = qdict_get_int(qdict, "id");
+    const char *flags = qdict_get_try_str(qdict, "flags");
+    int mask;
+
+    if (!tcg_enabled()) {
+        error_report("TB information is only available with accel=tcg");
+        return;
+    }
+
+    mask = flags ? qemu_str_to_log_mask(flags) : CPU_LOG_TB_IN_ASM;
+
+    if (!mask) {
+        error_report("Unable to parse log flags, see 'help log'");
+        return;
+    }
+
+    dump_tb_info(id, mask, true);
+}
+
 HumanReadableText *qmp_x_query_profile(Error **errp)
 {
     g_autoptr(GString) buf = g_string_new("");
accel/tcg/tb-stats.c +322 -0

350 lines not on this page · raw message

disas/disas.c +23 -3
index 0d2d06c2ec..0f31733646 100644
@@ -8,6 +8,8 @@
 #include "hw/core/cpu.h"
 #include "exec/memory.h"
 
+#include "qemu/log-for-trace.h"
+
 /* Filled in by elfload.c.  Simplistic, but will do for now. */
 struct syminfo *syminfos = NULL;
 
@@ -199,6 +201,24 @@ static void initialize_debug_host(CPUDebug *s)
 #endif
 }
 
+static int
+__attribute__((format(printf, 2, 3)))
+fprintf_log(FILE *a, const char *b, ...)
+{
+    va_list ap;
+    va_start(ap, b);
+
+    if (!to_string) {
+        vfprintf(a, b, ap);
+    } else {
+        qemu_vlog(b, ap);
+    }
+
+    va_end(ap);
+
+    return 1;
+}
+
 /* Disassemble this for me please... (debugging).  */
 void target_disas(FILE *out, CPUState *cpu, uint64_t code, size_t size)
 {
@@ -207,7 +227,7 @@ void target_disas(FILE *out, CPUState *cpu, uint64_t code, size_t size)
     CPUDebug s;
 
     disas_initialize_debug_target(&s, cpu);
-    s.info.fprintf_func = fprintf;
+    s.info.fprintf_func = fprintf_log;
     s.info.stream = out;
     s.info.buffer_vma = code;
     s.info.buffer_length = size;
@@ -221,9 +241,9 @@ void target_disas(FILE *out, CPUState *cpu, uint64_t code, size_t size)
     }
 
     for (pc = code; size > 0; pc += count, size -= count) {
-        fprintf(out, "0x%08" PRIx64 ":  ", pc);
+        fprintf_log(out, "0x%08" PRIx64 ":  ", pc);
         count = s.info.print_insn(pc, &s.info);
-        fprintf(out, "\n");
+        fprintf_log(out, "\n");
         if (count < 0) {
             break;
         }
hmp-commands-info.hx +16 -0
index 47d63d26db..01d9658aea 100644
@@ -262,6 +262,22 @@ ERST
         .params     = "",
         .help       = "show dynamic compiler info",
     },
+    {
+        .name       = "tb-list",
+        .args_type  = "number:i?,sortedby:s?",
+        .params     = "[number sortedby]",
+        .help       = "show a [number] translated blocks sorted by [sortedby]"
+                      "sortedby opts: hotness hg spills",
+        .cmd        = hmp_info_tblist,
+    },
+    {
+        .name       = "tb",
+        .args_type  = "id:i,flags:s?",
+        .params     = "id [flag1,flag2,...]",
+        .help       = "show information about one translated block by id."
+                      "dump flags can be used to set dump code level: out_asm in_asm op",
+        .cmd        = hmp_info_tb,
+    },
 #endif
 
 SRST
include/exec/tb-stats.h +31 -2
index 30b788f7b2..f2a6b09da9 100644
@@ -36,8 +36,11 @@
 enum SortBy { SORT_BY_HOTNESS, SORT_BY_HG /* Host/Guest */, SORT_BY_SPILLS };
 enum TbstatsCmd { START, PAUSE, STOP, FILTER };
 
+#define tbs_stats_enabled(tbs, JIT_STATS) \
+    (tbs && (tbs->stats_enabled & JIT_STATS))
+
 #define tb_stats_enabled(tb, JIT_STATS) \
-    (tb && tb->tb_stats && (tb->tb_stats->stats_enabled & JIT_STATS))
+    (tb && tb->tb_stats && tbs_stats_enabled(tb->tb_stats, JIT_STATS))
 
 #define stat_per_translation(stat, name) \
     (stat->translations.total ? stat->name / stat->translations.total : 0)
@@ -66,6 +69,8 @@ struct TBStatistics {
     struct {
         unsigned long normal;
         unsigned long atomic;
+        /* filled only when dumping x% cover set */
+        uint16_t coverage;
     } executions;
 
     /* JIT Stats - protected by lock */
@@ -88,7 +93,6 @@ struct TBStatistics {
 
     struct {
         unsigned long total;
-        unsigned long uncached;
         unsigned long spanning;
     } translations;
 
@@ -108,6 +112,9 @@ struct TBStatistics {
         uint64_t la;
         uint64_t code;
     } gen_times;
+
+    /* HMP information - used for referring to previous search */
+    int display_id;
 };
 
 bool tb_stats_cmp(const void *ap, const void *bp);
@@ -132,4 +139,26 @@ void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd);
  */
 void tbstats_reset_tbs(void);
 
+/**
+ * dump_tbs_info: report the hottest blocks
+ *
+ * @count: the limit of hotblocks
+ * @sort_by: property in which the dump will be sorted
+ * @use_monitor: redirect output to monitor
+ *
+ * Report the hottest blocks to either the log or monitor
+ */
+void dump_tbs_info(int count, int sort_by, bool use_monitor);
+
+/**
+ * dump_tb_info: dump information about one TB
+ *
+ * @id: the display id of the block (from previous search)
+ * @mask: the temporary logging mask
+ * @Use_monitor: redirect output to monitor
+ *
+ * Re-run a translation of a block at addr for the purposes of debug output
+ */
+void dump_tb_info(int id, int log_mask, bool use_monitor);
+
 #endif
include/monitor/hmp.h +2 -0
index 2e7f141754..acdd6f1561 100644
@@ -182,5 +182,7 @@ void hmp_boot_set(Monitor *mon, const QDict *qdict);
 void hmp_info_mtree(Monitor *mon, const QDict *qdict);
 void hmp_info_cryptodev(Monitor *mon, const QDict *qdict);
 void hmp_tbstats(Monitor *mon, const QDict *qdict);
+void hmp_info_tblist(Monitor *mon, const QDict *qdict);
+void hmp_info_tb(Monitor *mon, const QDict *qdict);
 
 #endif
include/qemu/log-for-trace.h +5 -1
index d47c9cd446..5d0afc56c7 100644
@@ -20,6 +20,9 @@
 
 /* Private global variable, don't use */
 extern int qemu_loglevel;
+extern bool to_string;
+
+extern int32_t max_num_hot_tbs_to_dump;
 
 #define LOG_TRACE          (1 << 15)
 
@@ -30,6 +33,7 @@ static inline bool qemu_loglevel_mask(int mask)
 }
 
 /* main logging function */
-void G_GNUC_PRINTF(1, 2) qemu_log(const char *fmt, ...);
+int G_GNUC_PRINTF(1, 2) qemu_log(const char *fmt, ...);
+int G_GNUC_PRINTF(1, 0) qemu_vlog(const char *fmt, va_list va);
 
 #endif
include/qemu/log.h +2 -0
index 6f3b8091cd..26b33151f3 100644
@@ -85,6 +85,8 @@ extern const QEMULogItem qemu_log_items[];
 bool qemu_set_log(int log_flags, Error **errp);
 bool qemu_set_log_filename(const char *filename, Error **errp);
 bool qemu_set_log_filename_flags(const char *name, int flags, Error **errp);
+void qemu_log_to_monitor(bool enable);
+void qemu_log_to_string(bool enable, GString *s);
 void qemu_set_dfilter_ranges(const char *ranges, Error **errp);
 bool qemu_log_in_addr_range(uint64_t addr);
 int qemu_str_to_log_mask(const char *str);
util/log.c +53 -13

96 lines not on this page · raw message

Signature · 1 line
2.25.1
#34 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 4 lines · Fei Wu
accel/tcg/tb-stats.c +4 -0
+static void do_dump_tbs_info(int total, int sort_by)
+{
+    id = 1;
+    GList *i;

accel/tcg/tb-stats.c void init_tb_stats_htable(void) · line 454

Quoted 10 lines
accel/tcg/tb-stats.c +10 -0
+    int count = total;
+
+    g_list_free(last_search);
+    last_search = NULL;
+
+    qht_iter(&tb_ctx.tb_stats, collect_tb_stats, NULL);
+
+    last_search = g_list_sort_with_data(last_search, inverse_sort_tbs,
+                                        &sort_by);
+

Why are you sorting on a list and not an array?
Intuitively, sorting a list of 1 million elements seems like the wrong choice.

Why did you put all the comparisons in one inverse_sort_tbs function, and require examining sort_by? Better would be N sorting functions where sort_by is evaluated once before starting the sort.

Quoted 22 lines

+++ b/disas/disas.c

disas/disas.c +10 -0
@@ -8,6 +8,8 @@
 #include "hw/core/cpu.h"
 #include "exec/memory.h"
 
+#include "qemu/log-for-trace.h"
+
 /* Filled in by elfload.c.  Simplistic, but will do for now. */
 struct syminfo *syminfos = NULL;
 
@@ -199,6 +201,24 @@ static void initialize_debug_host(CPUDebug *s)
 #endif
 }
 
+static int
+__attribute__((format(printf, 2, 3)))
+fprintf_log(FILE *a, const char *b, ...)
+{
+    va_list ap;
+    va_start(ap, b);
+
+    if (!to_string) {

disas/disas.c static void initialize_debug_host(CPUDebug *s) · line 221

Quoted 10 lines
disas/disas.c +10 -0
+        vfprintf(a, b, ap);
+    } else {
+        qemu_vlog(b, ap);
+    }
+
+    va_end(ap);
+
+    return 1;
+}
+

Not need on this either. Global variable being checked on each callback, instead of selecting the proper callback earlier -- preferably without the global variable.

Did you really need something different than monitor_disas? You almost certainly want to read physical memory and not virtual anyway.

util/log.c valid_filename_template(const char *filename, bool per_thread, Error **errp) · line 245

Quoted 10 lines
util/log.c +10 -0
+void qemu_log_to_monitor(bool enable)
+{
+    to_monitor = enable;
+}
+
+void qemu_log_to_string(bool enable, GString *s)
+{
+    to_string = enable;
+    string = s;
+}

What are these for, and why do you need them?
Why would to_string ever be anything other than (string != NULL)? Why are you using such very generic names for global variables?

r~

author
#35 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 62 lines · Richard Henderson
Quoted 4 lines · Fei Wu
accel/tcg/tb-stats.c +4 -0
+static void do_dump_tbs_info(int total, int sort_by)
+{
+    id = 1;
+    GList *i;

accel/tcg/tb-stats.c void init_tb_stats_htable(void) · line 454

Quoted 10 lines
accel/tcg/tb-stats.c +10 -0
+    int count = total;
+
+    g_list_free(last_search);
+    last_search = NULL;
+
+    qht_iter(&tb_ctx.tb_stats, collect_tb_stats, NULL);
+
+    last_search = g_list_sort_with_data(last_search, inverse_sort_tbs,
+                                        &sort_by);
+

Why are you sorting on a list and not an array? Intuitively, sorting a list of 1 million elements seems like the wrong choice.

Why did you put all the comparisons in one inverse_sort_tbs function, and require examining sort_by?  Better would be N sorting functions where sort_by is evaluated once before starting the sort.

Quoted 22 lines

+++ b/disas/disas.c

disas/disas.c +0 -0
@@ -8,6 +8,8 @@
  #include "hw/core/cpu.h"
  #include "exec/memory.h"

disas/disas.c +2 -0
+#include "qemu/log-for-trace.h"
+

 /* Filled in by elfload.c.  Simplistic, but will do for now. */ struct syminfo *syminfos = NULL;

(quoted diff) +0 -0
@@ -199,6 +201,24 @@ static void initialize_debug_host(CPUDebug *s)
  #endif
  }

disas/disas.c +8 -0
+static int
+__attribute__((format(printf, 2, 3)))
+fprintf_log(FILE *a, const char *b, ...)
+{
+    va_list ap;
+    va_start(ap, b);
+
+    if (!to_string) {

disas/disas.c static void initialize_debug_host(CPUDebug *s) · line 221

Quoted 10 lines
disas/disas.c +10 -0
+        vfprintf(a, b, ap);
+    } else {
+        qemu_vlog(b, ap);
+    }
+
+    va_end(ap);
+
+    return 1;
+}
+

Not need on this either.  Global variable being checked on each callback, instead of selecting the proper callback earlier -- preferably without the global variable.

Quoted 3 lines

Did you really need something different than monitor_disas?  You almost certainly want to read physical memory and not virtual anyway.

This makes me think the necessity of 'info tb', the primary extra info it adds is guest instruction, which can be gotten from 'x/i' w/o calling tb_gen_code.

(qemu) info tb 1
------------------------------

TB id:1 | phys:0x79bc86 virt:0xffffffff8059bc86 flags:0x01024001 0 inv/1
        | exec:56962444/0 guest inst cov:0.61%
        | trans:1 ints: g:8 op:27 op_opt:22 spills:0
        | h/g (host bytes / guest insts): 26.000000
        | time to gen at 2.4GHz => code:747.08(ns) IR:477.92(ns)

----------------
IN:
Priv: 1; Virt: 0

0xffffffff8059bc86:  00000013          nop
0xffffffff8059bc8a:  00000013          nop
0xffffffff8059bc8e:  00000013          nop
0xffffffff8059bc92:  00000013          nop
0xffffffff8059bc96:  1141              addi                    sp,sp,-16
0xffffffff8059bc98:  e422              sd                      s0,8(sp)
0xffffffff8059bc9a:  0800              addi                    s0,sp,16
0xffffffff8059bc9c:  c0102573          rdtime                  a0

------------------------------

(qemu) x/8i 0xffffffff8059bc86
x/8i 0xffffffff8059bc86

0xffffffff8059bc86:  00000013          nop
0xffffffff8059bc8a:  00000013          nop
0xffffffff8059bc8e:  00000013          nop
0xffffffff8059bc92:  00000013          nop
0xffffffff8059bc96:  1141              addi                    sp,sp,-16
0xffffffff8059bc98:  e422              sd                      s0,8(sp)
0xffffffff8059bc9a:  0800              addi                    s0,sp,16
0xffffffff8059bc9c:  c0102573          rdtime                  a0

Thanks,
Fei.

Quoted 17 lines

util/log.c valid_filename_template(const char *filename, bool per_thread, Error **errp) · line 245

Quoted 10 lines
util/log.c +10 -0
+void qemu_log_to_monitor(bool enable)
+{
+    to_monitor = enable;
+}
+
+void qemu_log_to_string(bool enable, GString *s)
+{
+    to_string = enable;
+    string = s;
+}

What are these for, and why do you need them?
Why would to_string ever be anything other than (string != NULL)? Why are you using such very generic names for global variables?

r~

author
#36 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 63 lines · Fei
Quoted 62 lines · Richard Henderson
Quoted 4 lines · Fei Wu
accel/tcg/tb-stats.c +4 -0
+static void do_dump_tbs_info(int total, int sort_by)
+{
+    id = 1;
+    GList *i;

accel/tcg/tb-stats.c void init_tb_stats_htable(void) · line 454

Quoted 10 lines
accel/tcg/tb-stats.c +10 -0
+    int count = total;
+
+    g_list_free(last_search);
+    last_search = NULL;
+
+    qht_iter(&tb_ctx.tb_stats, collect_tb_stats, NULL);
+
+    last_search = g_list_sort_with_data(last_search, inverse_sort_tbs,
+                                        &sort_by);
+

Why are you sorting on a list and not an array? Intuitively, sorting a list of 1 million elements seems like the wrong choice.

Why did you put all the comparisons in one inverse_sort_tbs function, and require examining sort_by?  Better would be N sorting functions where sort_by is evaluated once before starting the sort.

Quoted 22 lines

+++ b/disas/disas.c

disas/disas.c +0 -0
@@ -8,6 +8,8 @@
  #include "hw/core/cpu.h"
  #include "exec/memory.h"

disas/disas.c +2 -0
+#include "qemu/log-for-trace.h"
+

 /* Filled in by elfload.c.  Simplistic, but will do for now. */ struct syminfo *syminfos = NULL;

(quoted diff) +0 -0
@@ -199,6 +201,24 @@ static void initialize_debug_host(CPUDebug *s)
  #endif
  }

disas/disas.c +8 -0
+static int
+__attribute__((format(printf, 2, 3)))
+fprintf_log(FILE *a, const char *b, ...)
+{
+    va_list ap;
+    va_start(ap, b);
+
+    if (!to_string) {

disas/disas.c static void initialize_debug_host(CPUDebug *s) · line 221

Quoted 10 lines
disas/disas.c +10 -0
+        vfprintf(a, b, ap);
+    } else {
+        qemu_vlog(b, ap);
+    }
+
+    va_end(ap);
+
+    return 1;
+}
+

Not need on this either.  Global variable being checked on each callback, instead of selecting the proper callback earlier -- preferably without the global variable.

Quoted 3 lines
Quoted 3 lines

Did you really need something different than monitor_disas?  You almost certainly want to read physical memory and not virtual anyway.

'info tb' has extra flag to specify dump out_asm, in_asm & op, so there looks more user cases than monitor_disas, I think it's nice to have, but not strongly required, usually we have a general idea if we get the IN ops, plus the other counters such as g/op/... in the output.

Right now I am inclined to switch to monitor_disas, I found it causes guest kernel panic with the selective virt addr, it happens to the original v10 too.

------------------------------

TB id:1998 | phys:0x9c5f5ce2 virt:0x00ffffff8ff8ece2 flags:0x00024018 0 inv/1

        | exec:68/0 guest inst cov:0.00%
        | trans:1 ints: g:1 op:17 op_opt:16 spills:0
        | h/g (host bytes / guest insts): 129.000000
        | time to gen at 2.4GHz => code:1960.42(ns) IR:915.83(ns)

could not gen code for this TB (no longer mapped?)
------------------------------

[   98.213363] Unable to handle kernel paging request at virtual address
00ffffff8ff8ece2
[   98.219153] Oops [#1]
[   98.219299] Modules linked in: binfmt_misc nls_ascii nls_cp437 vfat

fat cfg80211 rfkill drm fuse drm_panel_orientation_quirks configfs i2c_core ip_tables x_tables autofs4 ext4 crc32c_generic crc16 mbcache jbd2 virtio_net net_failover virtio_blk failover virtio_mmio virtio virtio_ring

[   98.220832] CPU: 1 PID: 0 Comm: swapper/1 Not tainted 6.2.0+ #1
[   98.221122] Hardware name: riscv-virtio,qemu (DT)
[   98.221330] epc : arch_cpu_idle+0x1e/0x28
[   98.221985]  ra : default_idle_call+0x3c/0xb8
[   98.222150] epc : ffffffff80004398 ra : ffffffff808a0bc2 sp :
ff2000000068bf40
[   98.222339]  gp : ffffffff815688e8 tp : ff6000007ffcc240 t0 :
ff2000000064bad8
[   98.222562]  t1 : 0000000000000001 t2 : ffffffff80e02ed0 s0 :
ff2000000068bf50
[   98.222780]  s1 : 0000000000000001 a0 : 00000000000154ac a1 :
ff6000027d0f3000
[   98.223001]  a2 : ff6000007ffcc240 a3 : 0000000000000001 a4 :
ff6000027d0f3000
[   98.223217]  a5 : ff600001fdd5d500 a6 : 4000000000000000 a7 :
0000000000000000
[   98.223427]  s2 : ffffffff8156a220 s3 : ffffffff8156a418 s4 :
ffffffff80c6ba18
[   98.223636]  s5 : ffffffff815a9588 s6 : 0000000000000000 s7 :
0000000000000000
[   98.223853]  s8 : fffffffffffffffe s9 : 000000008003d6a8 s10:
0000000000000000
[   98.224065]  s11: 0000000000000000 t3 : ffffffff8156a438 t4 :
0000000000000000
[   98.224272]  t5 : 0000000000000000 t6 : 0000000000000000
[   98.224436] status: 0000000200000100 badaddr: 00ffffff8ff8ece2 cause:
000000000000000c
[   98.224778] [<ffffffff80004398>] arch_cpu_idle+0x1e/0x28
[   98.226393] ---[ end trace 0000000000000000 ]---
[   98.226770] Kernel panic - not syncing: Attempted to kill the idle task!
[   98.227188] SMP: stopping secondary CPUs
[   98.227980] ---[ end Kernel panic - not syncing: Attempted to kill

the idle task! ]---

Thanks,
Fei.

Quoted 60 lines

This makes me think the necessity of 'info tb', the primary extra info it adds is guest instruction, which can be gotten from 'x/i' w/o calling tb_gen_code.

(qemu) info tb 1
------------------------------

TB id:1 | phys:0x79bc86 virt:0xffffffff8059bc86 flags:0x01024001 0 inv/1
        | exec:56962444/0 guest inst cov:0.61%
        | trans:1 ints: g:8 op:27 op_opt:22 spills:0
        | h/g (host bytes / guest insts): 26.000000
        | time to gen at 2.4GHz => code:747.08(ns) IR:477.92(ns)

----------------
IN:
Priv: 1; Virt: 0

0xffffffff8059bc86:  00000013          nop
0xffffffff8059bc8a:  00000013          nop
0xffffffff8059bc8e:  00000013          nop
0xffffffff8059bc92:  00000013          nop
0xffffffff8059bc96:  1141              addi                    sp,sp,-16
0xffffffff8059bc98:  e422              sd                      s0,8(sp)
0xffffffff8059bc9a:  0800              addi                    s0,sp,16
0xffffffff8059bc9c:  c0102573          rdtime                  a0

------------------------------

(qemu) x/8i 0xffffffff8059bc86
x/8i 0xffffffff8059bc86

0xffffffff8059bc86:  00000013          nop
0xffffffff8059bc8a:  00000013          nop
0xffffffff8059bc8e:  00000013          nop
0xffffffff8059bc92:  00000013          nop
0xffffffff8059bc96:  1141              addi                    sp,sp,-16
0xffffffff8059bc98:  e422              sd                      s0,8(sp)
0xffffffff8059bc9a:  0800              addi                    s0,sp,16
0xffffffff8059bc9c:  c0102573          rdtime                  a0

Thanks,
Fei.

Quoted 17 lines

util/log.c valid_filename_template(const char *filename, bool per_thread, Error **errp) · line 245

Quoted 10 lines
util/log.c +10 -0
+void qemu_log_to_monitor(bool enable)
+{
+    to_monitor = enable;
+}
+
+void qemu_log_to_string(bool enable, GString *s)
+{
+    to_string = enable;
+    string = s;
+}

What are these for, and why do you need them?
Why would to_string ever be anything other than (string != NULL)? Why are you using such very generic names for global variables?

r~

author
#37 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

In reply to Richard Henderson
Quoted 23 lines · Richard Henderson
Quoted 8 lines
disas/disas.c +8 -0
+static int
+__attribute__((format(printf, 2, 3)))
+fprintf_log(FILE *a, const char *b, ...)
+{
+    va_list ap;
+    va_start(ap, b);
+
+    if (!to_string) {

disas/disas.c static void initialize_debug_host(CPUDebug *s) · line 221

Quoted 10 lines
disas/disas.c +10 -0
+        vfprintf(a, b, ap);
+    } else {
+        qemu_vlog(b, ap);
+    }
+
+    va_end(ap);
+
+    return 1;
+}
+

Not need on this either.  Global variable being checked on each callback, instead of selecting the proper callback earlier -- preferably without the global variable.

Quoted 3 lines

Did you really need something different than monitor_disas?  You almost certainly want to read physical memory and not virtual anyway.

Yes, it's usually okay for kernel address, but cannot re-gen the code for userspace virtual address (guest kernel panic on riscv guest). I tried monitor_disas() but it failed to disas too:

    monitor_disas(mon, mon_get_cpu(mon), tbs->phys_pc, num_inst, true);

How to use this function correctly?

Thanks,
Fei.

Quoted 17 lines

util/log.c valid_filename_template(const char *filename, bool per_thread, Error **errp) · line 245

Quoted 10 lines
util/log.c +10 -0
+void qemu_log_to_monitor(bool enable)
+{
+    to_monitor = enable;
+}
+
+void qemu_log_to_string(bool enable, GString *s)
+{
+    to_string = enable;
+    string = s;
+}

What are these for, and why do you need them?
Why would to_string ever be anything other than (string != NULL)? Why are you using such very generic names for global variables?

r~

author
#38 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 27 lines · Fei
Quoted 23 lines · Richard Henderson
Quoted 8 lines
disas/disas.c +8 -0
+static int
+__attribute__((format(printf, 2, 3)))
+fprintf_log(FILE *a, const char *b, ...)
+{
+    va_list ap;
+    va_start(ap, b);
+
+    if (!to_string) {

disas/disas.c static void initialize_debug_host(CPUDebug *s) · line 221

Quoted 10 lines
disas/disas.c +10 -0
+        vfprintf(a, b, ap);
+    } else {
+        qemu_vlog(b, ap);
+    }
+
+    va_end(ap);
+
+    return 1;
+}
+

Not need on this either.  Global variable being checked on each callback, instead of selecting the proper callback earlier -- preferably without the global variable.

Quoted 3 lines

Did you really need something different than monitor_disas?  You almost certainly want to read physical memory and not virtual anyway.

Quoted 7 lines

Yes, it's usually okay for kernel address, but cannot re-gen the code for userspace virtual address (guest kernel panic on riscv guest). I tried monitor_disas() but it failed to disas too:

    monitor_disas(mon, mon_get_cpu(mon), tbs->phys_pc, num_inst, true);

How to use this function correctly?

'phys_pc' in tbs is returned by get_page_addr_code_hostp(), which is not guest phys address actually, but ram_addr_t instead, so it's always wrong for monitor_disas. After some dirty changes, tbs can record the guest pa. Now we can disas both kernel and user space code. But still, no code is regenerated, disas in 'info tb' is just a convenient way to 'xp'.

Is there any existing function to convert ram_addr_t to guest pa?

(qemu) info tb-list 2

TB id:0 | phys:0x11b97762e virt:0x00aaaaaab76c262e flags:0x00024010 0 inv/1
        | exec:4447539979/0 guest inst cov:69.06%
        | trans:1 ints: g:8 op:28 op_opt:23 spills:0
        | h/g (host bytes / guest insts): 37.000000
TB id:1 | phys:0x8063474e virt:0xffffffff8043474e flags:0x01024001 0 inv/1
        | exec:131719290/0 guest inst cov:2.38%
        | trans:1 ints: g:9 op:37 op_opt:35 spills:0
        | h/g (host bytes / guest insts): 51.555557

(qemu) info tb 0
------------------------------

TB id:0 | phys:0x11b97762e virt:0x00aaaaaab76c262e flags:0x00024010 0 inv/1
        | exec:5841751800/0 guest inst cov:69.06%
        | trans:1 ints: g:8 op:28 op_opt:23 spills:0
        | h/g (host bytes / guest insts): 37.000000

0x11b97762e: 00002797 auipc a5,8192

       # 0x11b97962e
0x11b977632:  a2278793          addi                    a5,a5,-1502
0x11b977636:  639c              ld                      a5,0(a5)
0x11b977638:  00178713          addi                    a4,a5,1
0x11b97763c:  00002797          auipc                   a5,8192
       # 0x11b97963c
0x11b977640:  a1478793          addi                    a5,a5,-1516
0x11b977644:  e398              sd                      a4,0(a5)
0x11b977646:  b7e5              j                       -24
       # 0x11b97762e

------------------------------

(qemu) xp/8i 0x11b97762e
0x11b97762e: 00002797 auipc a5,8192

       # 0x11b97962e
0x11b977632:  a2278793          addi                    a5,a5,-1502
0x11b977636:  639c              ld                      a5,0(a5)
0x11b977638:  00178713          addi                    a4,a5,1
0x11b97763c:  00002797          auipc                   a5,8192
       # 0x11b97963c
0x11b977640:  a1478793          addi                    a5,a5,-1516
0x11b977644:  e398              sd                      a4,0(a5)
0x11b977646:  b7e5              j                       -24
       # 0x11b97762e

(qemu)

(qemu) info tb 1
------------------------------

TB id:1 | phys:0x8063474e virt:0xffffffff8043474e flags:0x01024001 0 inv/1
        | exec:131719290/0 guest inst cov:2.38%
        | trans:1 ints: g:9 op:37 op_opt:35 spills:0
        | h/g (host bytes / guest insts): 51.555557
0x8063474e:  00194a83          lbu                     s5,1(s2)
0x80634752:  00094803          lbu                     a6,0(s2)
0x80634756:  0b09              addi                    s6,s6,2
0x80634758:  008a9a9b          slliw                   s5,s5,8
0x8063475c:  01586833          or                      a6,a6,s5
0x80634760:  ff0b1f23          sh                      a6,-2(s6)
0x80634764:  1c7d              addi                    s8,s8,-1
0x80634766:  0909              addi                    s2,s2,2
0x80634768:  fe0c13e3          bnez                    s8,-26
      # 0x8063474e

------------------------------

(qemu) xp/9i 0x8063474e

0x8063474e:  00194a83          lbu                     s5,1(s2)
0x80634752:  00094803          lbu                     a6,0(s2)
0x80634756:  0b09              addi                    s6,s6,2
0x80634758:  008a9a9b          slliw                   s5,s5,8
0x8063475c:  01586833          or                      a6,a6,s5
0x80634760:  ff0b1f23          sh                      a6,-2(s6)
0x80634764:  1c7d              addi                    s8,s8,-1
0x80634766:  0909              addi                    s2,s2,2
0x80634768:  fe0c13e3          bnez                    s8,-26
      # 0x8063474e

Thanks,
Fei.

Quoted 21 lines

Thanks,
Fei.

Quoted 17 lines

util/log.c valid_filename_template(const char *filename, bool per_thread, Error **errp) · line 245

Quoted 10 lines
util/log.c +10 -0
+void qemu_log_to_monitor(bool enable)
+{
+    to_monitor = enable;
+}
+
+void qemu_log_to_string(bool enable, GString *s)
+{
+    to_string = enable;
+    string = s;
+}

What are these for, and why do you need them?
Why would to_string ever be anything other than (string != NULL)? Why are you using such very generic names for global variables?

r~

#39 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 13 lines · Fei
Quoted 11 lines · Fei
Quoted 3 lines · Richard Henderson

Did you really need something different than monitor_disas? You almost certainly want to read physical memory and not virtual anyway.

Yes, it's usually okay for kernel address, but cannot re-gen the code for userspace virtual address (guest kernel panic on riscv guest). I tried monitor_disas() but it failed to disas too:

    monitor_disas(mon, mon_get_cpu(mon), tbs->phys_pc, num_inst, true);

How to use this function correctly?

Quoted 7 lines

'phys_pc' in tbs is returned by get_page_addr_code_hostp(), which is not guest phys address actually, but ram_addr_t instead, so it's always wrong for monitor_disas. After some dirty changes, tbs can record the guest pa. Now we can disas both kernel and user space code. But still, no code is regenerated, disas in 'info tb' is just a convenient way to 'xp'.

Is there any existing function to convert ram_addr_t to guest pa?

Such a function would not be well-defined, because a block of RAM as specified by a ram_addr_t could be present at (aliased to) multiple guest physical addresses, or even currently not mapped to any guest physical address at all. And it could be present at different physical addresses for different vCPUs.

thanks
-- PMM

#40 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 20 lines · * Peter Maydell ([email protected]) wrote:
Quoted 19 lines · Fei
Quoted 11 lines · Fei
Quoted 3 lines · Richard Henderson

Did you really need something different than monitor_disas? You almost certainly want to read physical memory and not virtual anyway.

Yes, it's usually okay for kernel address, but cannot re-gen the code for userspace virtual address (guest kernel panic on riscv guest). I tried monitor_disas() but it failed to disas too:

    monitor_disas(mon, mon_get_cpu(mon), tbs->phys_pc, num_inst, true);

How to use this function correctly?

'phys_pc' in tbs is returned by get_page_addr_code_hostp(), which is not guest phys address actually, but ram_addr_t instead, so it's always wrong for monitor_disas. After some dirty changes, tbs can record the guest pa. Now we can disas both kernel and user space code. But still, no code is regenerated, disas in 'info tb' is just a convenient way to 'xp'.

Quoted 7 lines
Quoted 1 line

Is there any existing function to convert ram_addr_t to guest pa?

Such a function would not be well-defined, because a block of RAM as specified by a ram_addr_t could be present at (aliased to) multiple guest physical addresses, or even currently not mapped to any guest physical address at all. And it could be present at different physical addresses for different vCPUs.

True, but isn't there a similar mechanism for when an MCE happens in the host memory?

Dave

Quoted 2 lines

thanks
-- PMM

Signature · 4 lines
 -----Open up your eyes, open up your mind, open up your code -------   
/ Dr. David Alan Gilbert    |       Running GNU/Linux       | Happy  \ 
\        dave @ treblig.org |                               | In Hex /
 \ _________________________|_____ http://www.treblig.org   |_______/
#41 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 24 lines · Dr. David Alan Gilbert
Quoted 22 lines · * Peter Maydell ([email protected]) wrote:
Quoted 20 lines · Fei
Quoted 11 lines · Fei
Quoted 3 lines · Richard Henderson

Did you really need something different than monitor_disas? You almost certainly want to read physical memory and not virtual anyway.

Yes, it's usually okay for kernel address, but cannot re-gen the code for userspace virtual address (guest kernel panic on riscv guest). I tried monitor_disas() but it failed to disas too:

    monitor_disas(mon, mon_get_cpu(mon), tbs->phys_pc, num_inst, true);

How to use this function correctly?

'phys_pc' in tbs is returned by get_page_addr_code_hostp(), which is not guest phys address actually, but ram_addr_t instead, so it's always wrong for monitor_disas. After some dirty changes, tbs can record the guest pa. Now we can disas both kernel and user space code. But still, no code is regenerated, disas in 'info tb' is just a convenient way to 'xp'.

Is there any existing function to convert ram_addr_t to guest pa?

Quoted 8 lines
Quoted 5 lines

Such a function would not be well-defined, because a block of RAM as specified by a ram_addr_t could be present at (aliased to) multiple guest physical addresses, or even currently not mapped to any guest physical address at all. And it could be present at different physical addresses for different vCPUs.

True, but isn't there a similar mechanism for when an MCE happens in the host memory?

That goes via kvm_physical_memory_addr_from_host(), which looks up the host address in the KVMSlot array (and just picks the first hit). It won't work for TCG.

thanks
-- PMM

author
#42 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

In reply to Peter Maydell
Quoted 20 lines · Peter Maydell
Quoted 19 lines · Fei
Quoted 11 lines · Fei
Quoted 3 lines · Richard Henderson

Did you really need something different than monitor_disas? You almost certainly want to read physical memory and not virtual anyway.

Yes, it's usually okay for kernel address, but cannot re-gen the code for userspace virtual address (guest kernel panic on riscv guest). I tried monitor_disas() but it failed to disas too:

    monitor_disas(mon, mon_get_cpu(mon), tbs->phys_pc, num_inst, true);

How to use this function correctly?

'phys_pc' in tbs is returned by get_page_addr_code_hostp(), which is not guest phys address actually, but ram_addr_t instead, so it's always wrong for monitor_disas. After some dirty changes, tbs can record the guest pa. Now we can disas both kernel and user space code. But still, no code is regenerated, disas in 'info tb' is just a convenient way to 'xp'.

Quoted 8 lines
Quoted 1 line

Is there any existing function to convert ram_addr_t to guest pa?

Such a function would not be well-defined, because a block of RAM as specified by a ram_addr_t could be present at (aliased to) multiple guest physical addresses, or even currently not mapped to any guest physical address at all. And it could be present at different physical addresses for different vCPUs.

Thank you, Peter. What's the scenario of the last different physical addresses for different vCPUs?

For this specific case, I found I don't have to convert ram_addr_t to gpa, the real requirement is converting ram_addr_t to hva, this one seems well defined using qemu_map_ram_ptr(0, ram_addr) ?

Thanks,
Fei.

Quoted 2 lines

thanks
-- PMM

#43 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 5 lines · Fei
Quoted 3 lines · Peter Maydell
Quoted 1 line · Fei

Is there any existing function to convert ram_addr_t to guest pa?

Quoted 8 lines
Quoted 6 lines

Such a function would not be well-defined, because a block of RAM as specified by a ram_addr_t could be present at (aliased to) multiple guest physical addresses, or even currently not mapped to any guest physical address at all. And it could be present at different physical addresses for different vCPUs.

Thank you, Peter. What's the scenario of the last different physical addresses for different vCPUs?

You can have a board with two different CPUs, where one of them has a RAM MemoryRegion that it puts at address A in its address space, and the other puts it at address B. This is most likely with 'heterogenous' configurations where you have an application processor A and a board-support processor B. (I don't know if we actually have any board models like that currently, but it's logically possible.)

Quoted 3 lines

For this specific case, I found I don't have to convert ram_addr_t to gpa, the real requirement is converting ram_addr_t to hva, this one seems well defined using qemu_map_ram_ptr(0, ram_addr) ?

That does work for ram_addr_t to hva, but
but note the warning on the comment above that function:
you need to be in an RCU critical section to avoid some other thread coming along and de-allocating the RAM under your feet.

(Also it's tempting to remove that support for passing in 0 as the RAMBlock, because every other use in the codebase seems to pass in a RAMBlock pointer.)

thanks
-- PMM

author
#44 lore raw

Re: [PATCH v14 08/10] Adding info [tb-list|tb] commands to HMP (WIP)

Quoted 27 lines · Peter Maydell
Quoted 5 lines · Fei
Quoted 3 lines · Peter Maydell
Quoted 1 line · Fei

Is there any existing function to convert ram_addr_t to guest pa?

Quoted 8 lines
Quoted 6 lines

Such a function would not be well-defined, because a block of RAM as specified by a ram_addr_t could be present at (aliased to) multiple guest physical addresses, or even currently not mapped to any guest physical address at all. And it could be present at different physical addresses for different vCPUs.

Thank you, Peter. What's the scenario of the last different physical addresses for different vCPUs?

You can have a board with two different CPUs, where one of them has a RAM MemoryRegion that it puts at address A in its address space, and the other puts it at address B. This is most likely with 'heterogenous' configurations where you have an application processor A and a board-support processor B. (I don't know if we actually have any board models like that currently, but it's logically possible.)

Quoted 3 lines

For this specific case, I found I don't have to convert ram_addr_t to gpa, the real requirement is converting ram_addr_t to hva, this one seems well defined using qemu_map_ram_ptr(0, ram_addr) ?

Quoted 9 lines

That does work for ram_addr_t to hva, but
but note the warning on the comment above that function:
you need to be in an RCU critical section to avoid some other thread coming along and de-allocating the RAM under your feet.

(Also it's tempting to remove that support for passing in 0 as the RAMBlock, because every other use in the codebase seems to pass in a RAMBlock pointer.)

Got it, thank you very much.

Fei.

Quoted 2 lines

thanks
-- PMM

author
#45 lore raw

PATCH v14 9/10 tb-stats: dump hot TBs at the end of the execution

In reply to Fei Wu

From: "Vanderson M. do Rosario" <[email protected]>

Dump the hottest TBs if -d tb_stats,dump_limit=N is used.

Example of output for the 3 hottest TBs:

TB id:1 | phys:0x34d54 virt:0x0000000000034d54 flags:0x0000f0
        | exec:4828932/0 guest inst cov:16.38%
        | trans:1 ints: g:3 op:82 op_opt:34 spills:3
        | h/g (host bytes / guest insts): 90.666664
        | time to gen at 2.4GHz => code:3150.83(ns) IR:712.08(ns)
        | targets: 0x0000000000034d5e (id:11), 0x0000000000034d0d (id:2)
TB id:2 | phys:0x34d0d virt:0x0000000000034d0d flags:0x0000f0
        | exec:4825842/0 guest inst cov:21.82%
        | trans:1 ints: g:4 op:80 op_opt:38 spills:2
        | h/g (host bytes / guest insts): 84.000000
        | time to gen at 2.4GHz => code:3362.92(ns) IR:793.75(ns)
        | targets: 0x0000000000034d19 (id:12), 0x0000000000034d54 (id:1)
TB id:3 | phys:0xec1c1 virt:0x00000000000ec1c1 flags:0x0000b0
        | exec:872032/0 guest inst cov:1.97%
        | trans:1 ints: g:2 op:56 op_opt:26 spills:1
        | h/g (host bytes / guest insts): 68.000000
        | time to gen at 2.4GHz => code:1692.08(ns) IR:473.75(ns)
        | targets: 0x00000000000ec1c5 (id:4), 0x00000000000ec1cb (id:13)

Message-Id: <[email protected]>

[AJB: fix authorship, ad softmmu support]
---
 accel/tcg/tb-stats.c          | 21 +++++++++++++++++++++
 include/exec/tb-stats-dump.h  | 21 +++++++++++++++++++++
 include/exec/tb-stats-flags.h |  1 +
 linux-user/exit.c             |  2 ++
 softmmu/runstate.c            |  2 ++
 stubs/tb-stats.c              |  5 +++++
 util/log.c                    |  4 ++--
 7 files changed, 54 insertions(+), 2 deletions(-)
 create mode 100644 include/exec/tb-stats-dump.h
accel/tcg/tb-stats.c +21 -0
index 4b358cb421..d77891492a 100644
@@ -19,6 +19,7 @@
 
 #include "exec/tb-stats.h"
 #include "exec/tb-flush.h"
+#include "exec/tb-stats-dump.h"
 #include "tb-context.h"
 
 #include "internal.h"
@@ -32,6 +33,7 @@ enum TBStatsStatus {
 
 static enum TBStatsStatus tcg_collect_tb_stats;
 static uint32_t default_tbstats_flag;
+static int max_dump_tbs;
 /* only accessed in safe work */
 static GList *last_search;
 
@@ -616,6 +618,20 @@ void dump_tb_info(int id, int log_mask, bool use_monitor)
 }
 
 
+/*
+ * Dump the final stats
+ */
+void tb_stats_dump(void)
+{
+    if (!tb_stats_collection_enabled()) {
+        return;
+    }
+
+    dump_tbs_info(max_dump_tbs, SORT_BY_HOTNESS, false);
+}
+
+/* TBStatistic collection controls */
+
 void enable_collect_tb_stats(void)
 {
     tcg_collect_tb_stats = TB_STATS_RUNNING;
@@ -669,3 +685,8 @@ void set_default_tbstats_flag(uint32_t flags)
 {
     default_tbstats_flag = flags;
 }
+
+void set_tbstats_max_tbs(int max)
+{
+    max_dump_tbs = max;
+}
include/exec/tb-stats-dump.h new file +21 -0
new file mode 100644
index 0000000000..197c6148e9
@@ -0,0 +1,21 @@
+/*
+ * TB Stats common dump functions across sysemu/linux-user
+ *
+ * Copyright (c) 2019 Linaro
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#ifndef _TB_STATS_DUMP_H_
+#define _TB_STATS_DUMP_H_
+
+/**
+ * tb_stats_dump: dump final tb_stats at end of execution
+ */
+#ifdef CONFIG_TCG
+void tb_stats_dump(void);
+#else
+static inline void tb_stats_dump(void) { /* do nothing */ };
+#endif
+
+#endif /* _TB_STATS_DUMP_H_ */
include/exec/tb-stats-flags.h +1 -0
index a3897c99b1..484a4c9c68 100644
@@ -26,6 +26,7 @@ bool tb_stats_collection_enabled(void);
 bool tb_stats_collection_disabled(void);
 bool tb_stats_collection_paused(void);
 
+void set_tbstats_max_tbs(int max);
 uint32_t get_default_tbstats_flag(void);
 void set_default_tbstats_flag(uint32_t);
 void set_tbstats_flags(uint32_t flags);
linux-user/exit.c +2 -0
index 3017d28a3c..4fd23bcf60 100644
@@ -25,6 +25,7 @@
 #ifdef CONFIG_GPROF
 #include <sys/gmon.h>
 #endif
+#include "exec/tb-stats-dump.h"
 
 #ifdef CONFIG_GCOV
 extern void __gcov_dump(void);
@@ -41,4 +42,5 @@ void preexit_cleanup(CPUArchState *env, int code)
         gdb_exit(code);
         qemu_plugin_user_exit();
         perf_exit();
+        tb_stats_dump();
 }
softmmu/runstate.c +2 -0
index 37390799f1..b5ceb55ffd 100644
@@ -30,6 +30,7 @@
 #include "crypto/cipher.h"
 #include "crypto/init.h"
 #include "exec/cpu-common.h"
+#include "exec/tb-stats-dump.h"
 #include "gdbstub/syscalls.h"
 #include "hw/boards.h"
 #include "migration/misc.h"
@@ -827,6 +828,7 @@ void qemu_cleanup(void)
     vm_shutdown();
     replay_finish();
 
+    tb_stats_dump();
     job_cancel_sync_all();
     bdrv_close_all();
 
stubs/tb-stats.c +5 -0
index d212c2a1fa..a3e1406b88 100644
@@ -21,6 +21,11 @@ bool tb_stats_collection_enabled(void)
     return false;
 }
 
+void set_tbstats_max_tbs(int max)
+{
+    return;
+}
+
 void set_default_tbstats_flag(uint32_t flags)
 {
     return;
util/log.c +2 -2
index 6477eb5a5f..d159ca6916 100644
@@ -49,7 +49,6 @@ static __thread Notifier qemu_log_thread_cleanup_notifier;
 int qemu_loglevel;
 static bool log_per_thread;
 static GArray *debug_regions;
-int32_t max_num_hot_tbs_to_dump;
 static bool to_monitor;
 bool to_string;
 
@@ -568,7 +567,8 @@ int qemu_str_to_log_mask(const char *str)
             enable_collect_tb_stats();
         } else if (tb_stats_collection_enabled() &&
                    g_str_has_prefix(*tmp, "dump_limit=")) {
-            max_num_hot_tbs_to_dump = atoi((*tmp) + 11);
+            int hot_tbs = atoi((*tmp) + 11);
+            set_tbstats_max_tbs(hot_tbs);
         } else if (tb_stats_collection_enabled() &&
                    g_str_has_prefix(*tmp, "level=")) {
             uint32_t flags = 0;
Signature · 1 line
2.25.1
author
#46 lore raw

PATCH v14 10/10 docs: add tb-stats how to

In reply to Fei Wu
---
 docs/devel/tcg-tbstats.rst | 129 +++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
 create mode 100644 docs/devel/tcg-tbstats.rst
docs/devel/tcg-tbstats.rst new file +129 -0
new file mode 100644
index 0000000000..bfba222e96
@@ -0,0 +1,129 @@
+============
+TBStatistics
+============
+
+What is TBStatistics
+====================
+
+TBStatistics (tb_stats) is a tool to gather various internal information of TCG
+during binary translation, this allows us to identify such as hottest TBs,
+guest to host instruction translation ratio, number of spills during register
+allocation and more.
+
+
+How to use TBStatistics
+=======================
+
+1. HMP interface
+----------------
+
+TBStatistics provides HMP interface, you can try the following examples after
+connecting to the monitor.
+
+* First check the help info::
+
+    (qemu) help tb_stats
+    tb_stats command [stats_level] -- Control tb statistics collection:tb_stats (start|pause|stop|filter) [all|jit_stats|exec_stats]
+
+    (qemu) help info tb-list
+    info tb-list [number sortedby] -- show a [number] translated blocks sorted by [sortedby]sortedby opts: hotness hg spills
+
+    (qemu) help info tb
+    info tb id [flag1,flag2,...] -- show information about one translated block by id.dump flags can be used to set dump code level: out_asm in_asm op
+
+* Enable TBStatistics::
+
+    (qemu) tb_stats start all
+    (qemu)
+
+* Get interested TB list::
+
+    (qemu) info tb-list 2
+    TB id:1 | phys:0x79bca0 virt:0xffffffff8059bca0 flags:0x01024001 0 inv/1
+            | exec:1464084/0 guest inst cov:0.15%
+            | trans:1 ints: g:3 op:16 op_opt:15 spills:0
+            | h/g (host bytes / guest insts): 64.000000
+            | time to gen at 2.4GHz => code:607.08(ns) IR:250.83(ns)
+
+    TB id:2 | phys:0x2adf0c virt:0xffffffff800adf0c flags:0x01024001 0 inv/1
+            | exec:1033298/0 guest inst cov:0.28%
+            | trans:1 ints: g:8 op:35 op_opt:33 spills:0
+            | h/g (host bytes / guest insts): 86.000000
+            | time to gen at 2.4GHz => code:1429.58(ns) IR:545.42(ns)
+
+* Dive into the specific TB::
+
+    (qemu) info tb 1 op
+    ------------------------------
+
+    TB id:1 | phys:0x79bca0 virt:0xffffffff8059bca0 flags:0x01024001 7 inv/19
+            | exec:2038349/0 guest inst cov:0.15%
+            | trans:19 ints: g:3 op:16 op_opt:15 spills:0
+            | h/g (host bytes / guest insts): 64.000000
+            | time to gen at 2.4GHz => code:133434.17(ns) IR:176988.33(ns)
+
+    OP:
+     ld_i32 loc1,env,$0xfffffffffffffff0
+     brcond_i32 loc1,$0x0,lt,$L0
+     mov_i64 loc3,$0x7f3c70b3a4e0
+     call inc_exec_freq,$0x1,$0,loc3
+
+     ---- ffffffff8059bca0 0000000000006422
+     add_i64 loc5,x2/sp,$0x8
+     qemu_ld_i64 x8/s0,loc5,un+leq,1
+
+     ---- ffffffff8059bca2 0000000000000000
+     add_i64 x2/sp,x2/sp,$0x10
+
+     ---- ffffffff8059bca4 0000000000000000
+     mov_i64 pc,x1/ra
+     and_i64 pc,pc,$0xfffffffffffffffe
+     call lookup_tb_ptr,$0x6,$1,tmp9,env
+     goto_ptr tmp9
+     set_label $L0
+     exit_tb $0x7f3e887a8043
+
+    ------------------------------
+
+* Stop TBStatistics after investigation, this will disable TBStatistics completely.::
+
+    (qemu) tb_stats stop
+    (qemu)
+
+* Alternatively, TBStatistics can be paused, the previous collected TBStatistics
+  are not cleared but there is no TBStatistics recorded for new TBs.::
+
+    (qemu) tb_stats pause
+    (qemu)
+
+* Definitely, TBStatistics can be restarted for another round of investigation.::
+
+    (qemu) tb_stats start all
+    (qemu)
+
+
+2. Dump at exit
+---------------
+
+New command line options have been added to enable dump TB information at exit:::
+
+    -d tb_stats[[,level=(+all+jit+exec+time)][,dump_limit=<number>]]
+
+e.g. starting qemu like this:::
+
+    -d tb_stats,dump_limit=2
+
+Qemu prints the following at exit:::
+
+    QEMU: Terminated
+    TB id:1 | phys:0x61be02 virt:0xffffffff8041be02 flags:0x01024001 0 inv/1
+            | exec:72739176/0 guest inst cov:20.22%
+            | trans:1 ints: g:9 op:35 op_opt:33 spills:0
+            | h/g (host bytes / guest insts): 51.555557
+            | time to gen at 2.4GHz => code:1065.42(ns) IR:554.17(ns)
+
+    TB id:2 | phys:0x61bc66 virt:0xffffffff8041bc66 flags:0x01024001 0 inv/1
+            | exec:25069507/0 guest inst cov:0.77%
+            | trans:1 ints: g:1 op:15 op_opt:14 spills:0
+            | h/g (host bytes / guest insts): 128.000000
+            | time to gen at 2.4GHz => code:312.50(ns) IR:152.08(ns)
Signature · 1 line
2.25.1