Skip to contentSkip to conversation

TCG code quality tracking

PATCH In review Fei Wu 1 participant 11 messages Jun 28, 2023 – Jul 3, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Fei Wu

author
#0 lore raw

v16 cover TCG code quality tracking

v16
--
* rebase to latest commit 4329d049d (Jun 26)
* original patch 1 (remove CONFIG_PROFILER) has already upstreamed so is removed from here
* add async_safe_run_on_cpu back for info tb-list/tb
* add ram_addr support to monitor_disas and use it to disassemble guest code
* update the commit logs and documents
* small change to do_hmp_tbstats_safe() to avoid memory leaking

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

Fei Wu (4):
accel/tcg: add jit stats to TBStatistics
debug: add -d tb_stats to control TBStatistics
tb-stats: dump hot TBs at the end of the execution
docs: add tb-stats how to

Vanderson M. do Rosario (4):
accel/tcg: introduce TBStatistics structure
accel: collecting TB execution count
monitor: adding tb_stats hmp command
tb-stats: Adding info [tb-list|tb] commands to HMP

 MAINTAINERS                      |   1 +
 accel/tcg/cpu-exec.c             |   6 +
 accel/tcg/meson.build            |   1 +
 accel/tcg/monitor.c              | 216 ++++++++++++++++++
 accel/tcg/tb-context.h           |   1 +
 accel/tcg/tb-hash.h              |   7 +
 accel/tcg/tb-maint.c             |  20 ++
 accel/tcg/tb-stats.c             | 366 +++++++++++++++++++++++++++++++
 accel/tcg/tcg-runtime.c          |   1 +
 accel/tcg/translate-all.c        |  77 ++++++-
 accel/tcg/translator.c           |  31 +++
 disas/disas-mon.c                |  15 +-
 disas/disas.c                    |   2 +
 docs/devel/index-tcg.rst         |   1 +
 docs/devel/tcg-tbstats.rst       | 126 +++++++++++
 hmp-commands-info.hx             |  16 ++
 hmp-commands.hx                  |  16 ++
 include/disas/disas.h            |   8 +-
 include/exec/tb-stats-dump.h     |  21 ++
 include/exec/tb-stats-flags.h    |  29 +++
 include/exec/tb-stats.h          | 130 +++++++++++
 include/exec/translation-block.h |   3 +
 include/monitor/hmp.h            |   3 +
 include/qemu/log.h               |   1 +
 include/tcg/tcg-temp-internal.h  |   2 +
 include/tcg/tcg.h                |  10 +
 linux-user/exit.c                |   2 +
 monitor/hmp-cmds-target.c        |   3 +-
 softmmu/runstate.c               |   2 +
 stubs/meson.build                |   1 +
 stubs/tb-stats.c                 |  36 +++
 tcg/tcg.c                        |  47 +++-
 util/log.c                       |  26 +++
 33 files changed, 1217 insertions(+), 10 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 v16 1/9 accel/tcg: introduce TBStatistics structure

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             | 46 ++++++++++++++++++++++++++
 accel/tcg/translate-all.c        | 44 +++++++++++++++++++++++++
 include/exec/tb-stats-flags.h    | 19 +++++++++++
 include/exec/tb-stats.h          | 56 ++++++++++++++++++++++++++++++++
 include/exec/translation-block.h |  3 ++
 10 files changed, 197 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 e07746ac7d..f11c58f371 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/exec/helper*.h.inc
 F: include/exec/helper-info.c.inc
accel/tcg/meson.build +1 -0
index 166bef173b..239120c933 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 a0c61f25cd..638fe58270 100644
@@ -67,4 +67,11 @@ uint32_t tb_hash_func(tb_page_addr_t phys_pc, vaddr pc,
     return qemu_xxhash8(phys_pc, pc, flags2, flags, cf_mask);
 }
 
+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 3541419845..264bdd84b3 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;
@@ -59,6 +77,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 +46 -0

46 lines not on this page · raw message

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

62 lines not on this page · raw message

include/exec/tb-stats-flags.h new file +19 -0
new file mode 100644
index 0000000000..c994cf3b9d
@@ -0,0 +1,19 @@
+/*
+ * 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);
+bool tb_stats_collection_enabled(void);
+
+#endif
include/exec/tb-stats.h new file +56 -0

56 lines not on this page · raw message

include/exec/translation-block.h +3 -0
index 5119924927..6fd8c8bc48 100644
@@ -141,6 +141,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;
 };
 
 /* The alignment given to TranslationBlock during allocation. */
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v16 2/9 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            | 18 ++++++++++++++++++
 accel/tcg/tcg-runtime.c         |  1 +
 accel/tcg/translate-all.c       |  6 ++++--
 accel/tcg/translator.c          | 26 ++++++++++++++++++++++++++
 include/exec/tb-stats-flags.h   |  6 ++++++
 include/exec/tb-stats.h         |  7 +++++++
 include/tcg/tcg-temp-internal.h |  2 ++
 8 files changed, 70 insertions(+), 2 deletions(-)
accel/tcg/cpu-exec.c +6 -0
index ba1890a373..ba8ac61069 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 +18 -0
index 2b6be943ef..2b7321b548 100644
@@ -20,6 +20,7 @@ enum TBStatsStatus {
 };
 
 static enum TBStatsStatus tcg_collect_tb_stats;
+static uint32_t tbstats_flag;
 
 void init_tb_stats_htable(void)
 {
@@ -44,3 +45,20 @@ bool tb_stats_collection_enabled(void)
 {
     return tcg_collect_tb_stats == TB_STATS_RUNNING;
 }
+
+uint32_t get_tbstats_flag(void)
+{
+    return tbstats_flag;
+}
+
+void set_tbstats_flag(uint32_t flag)
+{
+    tbstats_flag = flag;
+}
+
+bool tb_stats_enabled(TranslationBlock *tb, uint32_t flag)
+{
+    return tb_stats_collection_enabled() &&
+           tb->tb_stats &&
+           (tbstats_flag & flag);
+}
accel/tcg/tcg-runtime.c +1 -0
index 9fa539ad3d..14d684c2ee 100644
@@ -27,6 +27,7 @@
 #include "exec/helper-proto-common.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 +4 -2
index 5f47b862d8..d7a35e0ea5 100644
@@ -388,9 +388,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, cflags & CF_PCREL ? 0 : pc,
                                     cs_base, flags);
     } else {
accel/tcg/translator.c +26 -0
index 0fd9efceba..4743fa7d01 100644
@@ -10,10 +10,12 @@
 #include "qemu/osdep.h"
 #include "qemu/log.h"
 #include "qemu/error-report.h"
+#include "tcg/tcg-temp-internal.h"
 #include "exec/exec-all.h"
 #include "exec/translator.h"
 #include "exec/translate-all.h"
 #include "exec/plugin-gen.h"
+#include "exec/tb-stats.h"
 #include "tcg/tcg-op-common.h"
 
 static void gen_io_start(void)
@@ -117,6 +119,29 @@ static void gen_tb_end(const TranslationBlock *tb, uint32_t cflags,
     }
 }
 
+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, vaddr dest)
 {
     /* Suppress goto_tb if requested. */
@@ -156,6 +181,7 @@ void translator_loop(CPUState *cpu, TranslationBlock *tb, int *max_insns,
 
     /* Start translating.  */
     icount_start_insn = gen_tb_start(cflags);
+    gen_tb_exec_count(tb);
     ops->tb_start(db, cpu);
     tcg_debug_assert(db->is_jmp == DISAS_NEXT);  /* no early exit */
 
include/exec/tb-stats-flags.h +6 -0
index c994cf3b9d..c0e63408e2 100644
@@ -11,9 +11,15 @@
 #ifndef TB_STATS_FLAGS
 #define TB_STATS_FLAGS
 
+#define TB_NONE_STATS (0)  /* no stats */
+#define TB_EXEC_STATS (1 << 0)
+
 /* TBStatistic collection controls */
 void enable_collect_tb_stats(void);
 void disable_collect_tb_stats(void);
 bool tb_stats_collection_enabled(void);
 
+uint32_t get_tbstats_flag(void);
+void set_tbstats_flag(uint32_t flag);
+
 #endif
include/exec/tb-stats.h +7 -0
index 8feb50e87d..a8abab269c 100644
@@ -47,10 +47,17 @@ struct TBStatistics {
     uint32_t flags;
     /* cs_base isn't included in the hash but we do check for matches */
     uint64_t cs_base;
+
+    /* Execution stats */
+    struct {
+        unsigned long normal;
+        unsigned long atomic;
+    } executions;
 };
 
 bool tb_stats_cmp(const void *ap, const void *bp);
 
 void init_tb_stats_htable(void);
+bool tb_stats_enabled(TranslationBlock *tb, uint32_t flag);
 
 #endif
include/tcg/tcg-temp-internal.h +2 -0
index dded2917e5..61ef055b92 100644
@@ -25,6 +25,8 @@
 #ifndef TCG_TEMP_INTERNAL_H
 #define TCG_TEMP_INTERNAL_H
 
+#include "tcg.h"
+
 /*
  * Allocation and freeing of EBB temps is reserved to TCG internals
  */
Signature · 1 line
2.25.1
author
#3 lore raw

PATCH v16 3/9 accel/tcg: add jit stats 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           |  6 +++
 accel/tcg/tb-stats.c          | 69 +++++++++++++++++++++++++++++++++++
 accel/tcg/translate-all.c     | 31 ++++++++++++++--
 accel/tcg/translator.c        |  5 +++
 include/exec/tb-stats-flags.h |  1 +
 include/exec/tb-stats.h       | 30 +++++++++++++++
 include/tcg/tcg.h             | 10 +++++
 tcg/tcg.c                     | 47 ++++++++++++++++++++++--
 8 files changed, 193 insertions(+), 6 deletions(-)
accel/tcg/monitor.c +6 -0
index d48de23999..567950a7ed 100644
@@ -16,6 +16,7 @@
 #include "sysemu/cpu-timers.h"
 #include "sysemu/tcg.h"
 #include "tcg/tcg.h"
+#include "exec/tb-stats.h"
 #include "internal.h"
 
 
@@ -70,6 +71,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");
accel/tcg/tb-stats.c +69 -0

81 lines not on this page · raw message

accel/tcg/translate-all.c +28 -3
index d7a35e0ea5..4f1d3b4612 100644
@@ -266,7 +266,7 @@ void page_init(void)
  */
 static int setjmp_gen_code(CPUArchState *env, TranslationBlock *tb,
                            vaddr pc, void *host_pc,
-                           int *max_insns, int64_t *ti)
+                           int *max_insns)
 {
     int ret = sigsetjmp(tcg_ctx->jmp_trans, 0);
     if (unlikely(ret != 0)) {
@@ -294,6 +294,8 @@ 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->tbs = g_ptr_array_new();
+    qemu_mutex_init(&new_stats->jit_stats_lock);
 
     /*
      * All initialisation must be complete before we insert into qht
@@ -307,6 +309,7 @@ static TBStatistics *tb_get_stats(tb_page_addr_t phys_pc, target_ulong pc,
          * If there is already a TBStatistic for this TB from a previous flush
          * then just make the new TB point to the older TBStatistic
          */
+        g_ptr_array_free(new_stats->tbs, true);
         g_free(new_stats);
         return existing_stats;
     } else {
@@ -325,7 +328,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;
-    int64_t ti;
     void *host_pc;
 
     assert_memory_lock();
@@ -399,7 +401,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
         tb->tb_stats = NULL;
     }
 
-    gen_code_size = setjmp_gen_code(env, tb, pc, host_pc, &max_insns, &ti);
+    gen_code_size = setjmp_gen_code(env, tb, pc, host_pc, &max_insns);
     if (unlikely(gen_code_size < 0)) {
         switch (gen_code_size) {
         case -1:
@@ -553,6 +555,29 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
         return tb;
     }
 
+    /*
+     * Collect JIT stats when enabled. We batch them all up here to
+     * avoid spamming the cache with atomic accesses
+     */
+    if (tb_stats_enabled(tb, TB_JIT_STATS)) {
+        TBStatistics *ts = tb->tb_stats;
+        qemu_mutex_lock(&ts->jit_stats_lock);
+
+        ts->code.num_tcg_ops_opt += tcg_ctx->nb_ops;
+        ts->code.in_len += tb->size;
+        ts->code.out_len += tb->tc.size;
+        ts->code.search_out_len += search_size;
+
+        ts->translations.total++;
+        if (tb_page_addr1(tb) != -1) {
+            ts->translations.spanning++;
+        }
+
+        g_ptr_array_add(ts->tbs, tb);
+
+        qemu_mutex_unlock(&ts->jit_stats_lock);
+    }
+
     /*
      * Insert TB into the corresponding region tree before publishing it
      * through QHT. Otherwise rewinding happened in the TB might fail to
accel/tcg/translator.c +5 -0
index 4743fa7d01..3995d45a66 100644
@@ -248,6 +248,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 +1 -0
index c0e63408e2..286ca93bcc 100644
@@ -13,6 +13,7 @@
 
 #define TB_NONE_STATS (0)  /* no stats */
 #define TB_EXEC_STATS (1 << 0)
+#define TB_JIT_STATS  (1 << 1)
 
 /* TBStatistic collection controls */
 void enable_collect_tb_stats(void);
include/exec/tb-stats.h +30 -0
index a8abab269c..7c445856c9 100644
@@ -53,6 +53,34 @@ struct TBStatistics {
         unsigned long normal;
         unsigned long atomic;
     } executions;
+
+    /* JIT Stats - protected by lock */
+    QemuMutex jit_stats_lock;
+
+    /* Sum of all operations for all translations */
+    struct {
+        unsigned num_guest_inst;
+        unsigned num_tcg_ops;
+        unsigned num_tcg_ops_opt;
+        unsigned spills;
+
+        unsigned temps;
+        unsigned deleted_ops;
+        unsigned in_len;
+        unsigned out_len;
+        unsigned search_out_len;
+    } code;
+
+    struct {
+        unsigned long total;
+        unsigned long spanning;
+    } translations;
+
+    /*
+     * All persistent (cached) TranslationBlocks using
+     * this TBStats structure. Has to be reset on a tb_flush.
+     */
+    GPtrArray *tbs;
 };
 
 bool tb_stats_cmp(const void *ap, const void *bp);
@@ -60,4 +88,6 @@ bool tb_stats_cmp(const void *ap, const void *bp);
 void init_tb_stats_htable(void);
 bool tb_stats_enabled(TranslationBlock *tb, uint32_t flag);
 
+void dump_jit_profile_info(GString *buf);
+
 #endif
include/tcg/tcg.h +10 -0
index 95541e9474..8c0857fe12 100644
@@ -478,6 +478,14 @@ static inline TCGRegSet output_pref(const TCGOp *op, unsigned i)
     return i < ARRAY_SIZE(op->output_pref) ? op->output_pref[i] : 0;
 }
 
+/*
+ * The TCGProfile structure holds data for the lifetime of the translator.
+ */
+typedef struct TCGProfile {
+    /* 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;
@@ -507,6 +515,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;
tcg/tcg.c +44 -3

92 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#4 lore raw

PATCH v16 4/9 monitor: adding tb_stats hmp command

In reply to Fei Wu

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

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

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           | 98 +++++++++++++++++++++++++++++++++++
 accel/tcg/tb-stats.c          | 17 ++++++
 hmp-commands.hx               | 16 ++++++
 include/exec/tb-stats-flags.h |  1 +
 include/exec/tb-stats.h       |  4 ++
 include/monitor/hmp.h         |  1 +
 6 files changed, 137 insertions(+)
accel/tcg/monitor.c +98 -0
index 567950a7ed..60b66f16ff 100644
@@ -11,14 +11,19 @@
 #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"
 #include "tcg/tcg.h"
 #include "exec/tb-stats.h"
+#include "exec/tb-flush.h"
 #include "internal.h"
+#include "tb-context.h"
 
+enum TbstatsCmd { TBS_CMD_START, TBS_CMD_STOP, TBS_CMD_STATUS };
 
 static void dump_drift_info(GString *buf)
 {
@@ -87,6 +92,99 @@ HumanReadableText *qmp_x_query_opcount(Error **errp)
     return human_readable_text_from_str(buf);
 }
 
+struct TbstatsCommand {
+    enum TbstatsCmd cmd;
+    uint32_t flag;
+    Monitor *mon;
+};
+
+static void do_hmp_tbstats_safe(CPUState *cpu, run_on_cpu_data icmd)
+{
+    struct TbstatsCommand *cmdinfo = icmd.host_ptr;
+    int cmd = cmdinfo->cmd;
+    uint32_t flag = cmdinfo->flag;
+    Monitor *mon = cmdinfo->mon;
+
+    switch (cmd) {
+    case TBS_CMD_START:
+        if (tb_stats_collection_enabled()) {
+            monitor_printf(mon, "TB information already being recorded\n");
+            break;
+        }
+
+        set_tbstats_flag(flag);
+        enable_collect_tb_stats();
+        tb_flush(cpu);
+        break;
+    case TBS_CMD_STOP:
+        if (tb_stats_collection_disabled()) {
+            monitor_printf(mon, "TB information not being recorded\n");
+            break;
+        }
+
+        /* Dissalloc all TBStatistics structures and stop creating new ones */
+        disable_collect_tb_stats();
+        clean_tbstats();
+        tb_flush(cpu);
+        break;
+    case TBS_CMD_STATUS:
+        if (tb_stats_collection_enabled()) {
+            uint32_t flag = get_tbstats_flag();
+            monitor_printf(mon, "tb_stats is enabled with flag:\n");
+            monitor_printf(mon, "    EXEC: %d\n", !!(flag & TB_EXEC_STATS));
+            monitor_printf(mon, "     JIT: %d\n", !!(flag & TB_JIT_STATS));
+        } else {
+            monitor_printf(mon, "tb_stats is disabled\n");
+        }
+        break;
+    default: /* INVALID */
+        g_assert_not_reached();
+        break;
+    }
+
+    g_free(cmdinfo);
+}
+
+void hmp_tbstats(Monitor *mon, const QDict *qdict)
+{
+    if (!tcg_enabled()) {
+        monitor_printf(mon, "Only available with accel=tcg\n");
+        return;
+    }
+
+    char *cmd = (char *) qdict_get_try_str(qdict, "command");
+    enum TbstatsCmd icmd = -1;
+
+    if (strcmp(cmd, "start") == 0) {
+        icmd = TBS_CMD_START;
+    } else if (strcmp(cmd, "stop") == 0) {
+        icmd = TBS_CMD_STOP;
+    } else if (strcmp(cmd, "status") == 0) {
+        icmd = TBS_CMD_STATUS;
+    } else {
+        monitor_printf(mon, "Invalid command\n");
+        return;
+    }
+
+    char *sflag = (char *) qdict_get_try_str(qdict, "flag");
+    uint32_t flag = TB_EXEC_STATS | TB_JIT_STATS;
+    if (sflag) {
+        if (strcmp(sflag, "jit") == 0) {
+            flag = TB_JIT_STATS;
+        } else if (strcmp(sflag, "exec") == 0) {
+            flag = TB_EXEC_STATS;
+        }
+    }
+
+    struct TbstatsCommand *tbscommand = g_new0(struct TbstatsCommand, 1);
+    tbscommand->cmd = icmd;
+    tbscommand->flag = flag;
+    tbscommand->mon = mon;
+    async_safe_run_on_cpu(first_cpu, do_hmp_tbstats_safe,
+                          RUN_ON_CPU_HOST_PTR(tbscommand));
+
+}
+
 static void hmp_tcg_register(void)
 {
     monitor_register_hmp_info_hrt("jit", qmp_x_query_jit);
accel/tcg/tb-stats.c +17 -0
index 11322359c7..c90dde37d0 100644
@@ -91,6 +91,18 @@ void dump_jit_profile_info(GString *buf)
     g_free(jpi);
 }
 
+static void free_tbstats(void *p, uint32_t hash, void *userp)
+{
+    g_free(p);
+}
+
+void clean_tbstats(void)
+{
+    /* remove all tb_stats */
+    qht_iter(&tb_ctx.tb_stats, free_tbstats, NULL);
+    qht_destroy(&tb_ctx.tb_stats);
+}
+
 void init_tb_stats_htable(void)
 {
     if (!tb_ctx.tb_stats.map && tb_stats_collection_enabled()) {
@@ -115,6 +127,11 @@ bool tb_stats_collection_enabled(void)
     return tcg_collect_tb_stats == TB_STATS_RUNNING;
 }
 
+bool tb_stats_collection_disabled(void)
+{
+    return tcg_collect_tb_stats == TB_STATS_STOPPED;
+}
+
 uint32_t get_tbstats_flag(void)
 {
     return tbstats_flag;
hmp-commands.hx +16 -0
index 2cbd0f77a0..639e9d9e53 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,flag:s?",
+        .params     = "command [flag]",
+        .help       = "Control tb statistics collection:"
+                        "tb_stats (start|stop|status) [all|jit|exec]",
+        .cmd        = hmp_tbstats,
+    },
+#endif
+
+SRST
+``tb_stats`` *command* *flag*
+  Control recording tb statistics
+ERST
+
     {
         .name       = "qom-list",
         .args_type  = "path:s?",
include/exec/tb-stats-flags.h +1 -0
index 286ca93bcc..d8b844be99 100644
@@ -19,6 +19,7 @@
 void enable_collect_tb_stats(void);
 void disable_collect_tb_stats(void);
 bool tb_stats_collection_enabled(void);
+bool tb_stats_collection_disabled(void);
 
 uint32_t get_tbstats_flag(void);
 void set_tbstats_flag(uint32_t flag);
include/exec/tb-stats.h +4 -0
index 7c445856c9..ef6e8b6388 100644
@@ -31,6 +31,8 @@
 #include "exec/tb-stats-flags.h"
 #include "tcg/tcg.h"
 
+enum SortBy { SORT_BY_HOTNESS, SORT_BY_HG /* Host/Guest */, SORT_BY_SPILLS };
+
 typedef struct TBStatistics TBStatistics;
 
 /*
@@ -90,4 +92,6 @@ bool tb_stats_enabled(TranslationBlock *tb, uint32_t flag);
 
 void dump_jit_profile_info(GString *buf);
 
+void clean_tbstats(void);
+
 #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
author
#5 lore raw

PATCH v16 5/9 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    | 18 ++++++++++++++++++
 include/exec/tb-stats.h |  8 ++++++++
 3 files changed, 27 insertions(+)
accel/tcg/tb-maint.c +1 -0
index 264bdd84b3..1ebe6fc60e 100644
@@ -762,6 +762,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 +18 -0
index c90dde37d0..7c7f700c89 100644
@@ -103,6 +103,24 @@ void clean_tbstats(void)
     qht_destroy(&tb_ctx.tb_stats);
 }
 
+/*
+ * We have to reset the tbs array on a tb_flush as those
+ * TranslationBlocks no longer exist.
+ */
+
+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 ef6e8b6388..cef177bc69 100644
@@ -94,4 +94,12 @@ void dump_jit_profile_info(GString *buf);
 
 void clean_tbstats(void);
 
+/**
+ * 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
author
#6 lore raw

PATCH v16 6/9 tb-stats: Adding info [tb-list|tb] commands to HMP

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 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.

Example of output:

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

TB id:0 | phys:0xa21f562e virt:0x0000000000000000 flags:0x00028010 0 inv/1
        | exec:6171503732/0 guest inst cov:94.77%
        | trans:1 ints: g:8 op:28 op_opt:24 spills:0
        | h/g (host bytes / guest insts): 37.000000
0xa21f562e:  00002797      auipc         a5,8192           # 0xa21f762e
0xa21f5632:  a2278793      addi          a5,a5,-1502
0xa21f5636:  639c          ld            a5,0(a5)
0xa21f5638:  00178713      addi          a4,a5,1
0xa21f563c:  00002797      auipc         a5,8192           # 0xa21f763c
0xa21f5640:  a1478793      addi          a5,a5,-1516
0xa21f5644:  e398          sd            a4,0(a5)
0xa21f5646:  b7e5          j             -24               # 0xa21f562e

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

Message-Id: <[email protected]>

[AJB: fix authorship, dropped coverset]
---
 accel/tcg/monitor.c       | 112 ++++++++++++++++++++++++
 accel/tcg/tb-stats.c      | 177 ++++++++++++++++++++++++++++++++++++++
 disas/disas-mon.c         |  15 +++-
 disas/disas.c             |   2 +
 hmp-commands-info.hx      |  16 ++++
 include/disas/disas.h     |   8 +-
 include/exec/tb-stats.h   |  25 ++++++
 include/monitor/hmp.h     |   2 +
 monitor/hmp-cmds-target.c |   3 +-
 9 files changed, 356 insertions(+), 4 deletions(-)
accel/tcg/monitor.c +112 -0
index 60b66f16ff..0ce5a1cb45 100644
@@ -8,18 +8,22 @@
 
 #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"
 #include "qapi/qmp/qdict.h"
 #include "monitor/monitor.h"
 #include "monitor/hmp.h"
+#include "monitor/hmp-target.h"
 #include "sysemu/cpus.h"
 #include "sysemu/cpu-timers.h"
 #include "sysemu/tcg.h"
 #include "tcg/tcg.h"
 #include "exec/tb-stats.h"
 #include "exec/tb-flush.h"
+#include "disas/disas.h"
+#include "tb-context.h"
 #include "internal.h"
 #include "tb-context.h"
 
@@ -185,6 +189,114 @@ void hmp_tbstats(Monitor *mon, const QDict *qdict)
 
 }
 
+struct tblist_dump_info {
+    int count;
+    int sortedby;
+    Monitor *mon;
+};
+
+static void do_dump_tblist_info_safe(CPUState *cpu, run_on_cpu_data info)
+{
+    struct tblist_dump_info *tbdi = info.host_ptr;
+    g_autoptr(GString) buf = g_string_new("");
+
+    dump_tblist_info(buf, tbdi->count, tbdi->sortedby);
+    monitor_printf(tbdi->mon, "%s", buf->str);
+
+    g_free(tbdi);
+}
+
+void hmp_info_tblist(Monitor *mon, const QDict *qdict)
+{
+    int number_int;
+    const char *sortedby_str = NULL;
+
+    if (!tcg_enabled()) {
+        monitor_printf(mon, "Only available with accel=tcg\n");
+        return;
+    }
+    if (!tb_ctx.tb_stats.map) {
+        monitor_printf(mon, "no TB information recorded\n");
+        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 {
+        monitor_printf(mon, "valid sort options are: hotness hg spills\n");
+        return;
+    }
+
+    struct tblist_dump_info *tbdi = g_new(struct tblist_dump_info, 1);
+    tbdi->count = number_int;
+    tbdi->sortedby = sortedby;
+    tbdi->mon = mon;
+    async_safe_run_on_cpu(first_cpu, do_dump_tblist_info_safe,
+                          RUN_ON_CPU_HOST_PTR(tbdi));
+}
+
+struct tb_dump_info {
+    int id;
+    Monitor *mon;
+};
+
+static void do_dump_tb_info_safe(CPUState *cpu, run_on_cpu_data info)
+{
+    struct tb_dump_info *tbdi = info.host_ptr;
+    int id = tbdi->id;
+    Monitor *mon = tbdi->mon;
+    g_autoptr(GString) buf = g_string_new("");
+
+    TBStatistics *tbs = get_tbstats_by_id(id);
+    if (tbs == NULL) {
+        monitor_printf(mon, "TB %d information is not recorded\n", id);
+        return;
+    }
+
+    monitor_printf(mon, "\n------------------------------\n\n");
+
+    int valid_tb_num = dump_tb_info(buf, tbs, id);
+    monitor_printf(mon, "%s", buf->str);
+
+    if (valid_tb_num > 0) {
+        for (int i = tbs->tbs->len - 1; i >= 0; --i) {
+            TranslationBlock *tb = g_ptr_array_index(tbs->tbs, i);
+            if (!(tb->cflags & CF_INVALID)) {
+                monitor_disas(mon, mon_get_cpu(mon), tbs->phys_pc, tb->icount,
+                              DISAS_GRA);
+                break;
+            }
+        }
+    }
+    monitor_printf(mon, "\n------------------------------\n\n");
+
+    g_free(tbdi);
+}
+
+void hmp_info_tb(Monitor *mon, const QDict *qdict)
+{
+    const int id = qdict_get_int(qdict, "id");
+
+    if (!tcg_enabled()) {
+        monitor_printf(mon, "Only available with accel=tcg\n");
+        return;
+    }
+
+    struct tb_dump_info *tbdi = g_new(struct tb_dump_info, 1);
+    tbdi->id = id;
+    tbdi->mon = mon;
+    async_safe_run_on_cpu(first_cpu, do_dump_tb_info_safe,
+                          RUN_ON_CPU_HOST_PTR(tbdi));
+}
+
 static void hmp_tcg_register(void)
 {
     monitor_register_hmp_info_hrt("jit", qmp_x_query_jit);
accel/tcg/tb-stats.c +177 -0

216 lines not on this page · raw message

disas/disas-mon.c +13 -2
index 48ac492c6c..e4125b4493 100644
@@ -23,9 +23,18 @@ physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
     return res == MEMTX_OK ? 0 : EIO;
 }
 
+static int
+ram_addr_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
+                     struct disassemble_info *info)
+{
+    void *p = qemu_map_ram_ptr(0, memaddr);
+    memcpy(myaddr, p, length);
+    return 0;
+}
+
 /* Disassembler for the monitor.  */
 void monitor_disas(Monitor *mon, CPUState *cpu, uint64_t pc,
-                   int nb_insn, bool is_physical)
+                   int nb_insn, int addr_kind)
 {
     int count, i;
     CPUDebug s;
@@ -35,8 +44,10 @@ void monitor_disas(Monitor *mon, CPUState *cpu, uint64_t pc,
     s.info.fprintf_func = disas_gstring_printf;
     s.info.stream = (FILE *)ds;  /* abuse this slot */
 
-    if (is_physical) {
+    if (addr_kind == DISAS_GPA) {
         s.info.read_memory_func = physical_read_memory;
+    } else if (addr_kind == DISAS_GRA) {
+        s.info.read_memory_func = ram_addr_read_memory;
     }
     s.info.buffer_vma = pc;
 
disas/disas.c +2 -0
index 0d2d06c2ec..3b19b51fca 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;
 
hmp-commands-info.hx +16 -0
index f5b37eb74a..ace1ecc3c5 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/disas/disas.h +7 -1
index 176775eff7..95854abca2 100644
@@ -1,12 +1,18 @@
 #ifndef QEMU_DISAS_H
 #define QEMU_DISAS_H
 
+enum {
+    DISAS_GVA = 0,
+    DISAS_GPA,
+    DISAS_GRA,  /* guest ram addr */
+};
+
 /* Disassemble this for me please... (debugging). */
 void disas(FILE *out, const void *code, size_t size);
 void target_disas(FILE *out, CPUState *cpu, uint64_t code, size_t size);
 
 void monitor_disas(Monitor *mon, CPUState *cpu, uint64_t pc,
-                   int nb_insn, bool is_physical);
+                   int nb_insn, int addr_kind);
 
 char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size);
 
include/exec/tb-stats.h +25 -0
index cef177bc69..0caffdf32b 100644
@@ -54,6 +54,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 */
@@ -89,6 +91,7 @@ bool tb_stats_cmp(const void *ap, const void *bp);
 
 void init_tb_stats_htable(void);
 bool tb_stats_enabled(TranslationBlock *tb, uint32_t flag);
+bool tbs_stats_enabled(struct TBStatistics *tbs, uint32_t flag);
 
 void dump_jit_profile_info(GString *buf);
 
@@ -102,4 +105,26 @@ void clean_tbstats(void);
  */
 void tbstats_reset_tbs(void);
 
+/**
+ * dump_tbs_info: report the hottest blocks
+ *
+ * @buf: output buffer
+ * @total: the limit of hotblocks
+ * @sort_by: property in which the dump will be sorted
+ *
+ * Report the hottest blocks to either the log or monitor
+ */
+void dump_tblist_info(GString *buf, int total, int sort_by);
+
+/**
+ * dump_tb_info: dump information about one TB
+ *
+ * @buf: output buffer
+ * @tbs: the tbs to dump
+ * @id: the display id of the block (from previous search)
+ */
+int dump_tb_info(GString *buf, TBStatistics *tbs, int id);
+
+TBStatistics *get_tbstats_by_id(int id);
+
 #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
monitor/hmp-cmds-target.c +2 -1
index 0d3e84d960..7d1433336d 100644
@@ -133,7 +133,8 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,
     }
 
     if (format == 'i') {
-        monitor_disas(mon, cs, addr, count, is_physical);
+        monitor_disas(mon, cs, addr, count,
+                      is_physical ? DISAS_GPA : DISAS_GVA);
         return;
     }
 
Signature · 1 line
2.25.1
author
#7 lore raw

PATCH v16 7/9 debug: add -d tb_stats to control TBStatistics

In reply to Fei Wu

Capture TBS at startup instead of an explicit 'tb_stats start' command.

---
 include/exec/tb-stats-flags.h |  1 +
 include/qemu/log.h            |  1 +
 stubs/meson.build             |  1 +
 stubs/tb-stats.c              | 32 ++++++++++++++++++++++++++++++++
 util/log.c                    | 18 ++++++++++++++++++
 5 files changed, 53 insertions(+)
 create mode 100644 stubs/tb-stats.c
include/exec/tb-stats-flags.h +1 -0
index d8b844be99..936bd35707 100644
@@ -14,6 +14,7 @@
 #define TB_NONE_STATS (0)  /* no stats */
 #define TB_EXEC_STATS (1 << 0)
 #define TB_JIT_STATS  (1 << 1)
+#define TB_ALL_STATS  (TB_EXEC_STATS | TB_JIT_STATS)
 
 /* TBStatistic collection controls */
 void enable_collect_tb_stats(void);
include/qemu/log.h +1 -0
index df59bfabcd..eca10f02a3 100644
@@ -36,6 +36,7 @@ bool qemu_log_separate(void);
 #define LOG_STRACE         (1 << 19)
 #define LOG_PER_THREAD     (1 << 20)
 #define CPU_LOG_TB_VPU     (1 << 21)
+#define CPU_LOG_TB_STATS   (1 << 22)
 
 /* 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 +32 -0
new file mode 100644
index 0000000000..38ee12313e
@@ -0,0 +1,32 @@
+/*
+ * 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;
+}
+
+void disable_collect_tb_stats(void)
+{
+    return;
+}
+
+bool tb_stats_collection_enabled(void)
+{
+    return false;
+}
+
+void set_tbstats_flag(uint32_t flag)
+{
+    return;
+}
util/log.c +18 -0
index def88a9402..59c781de36 100644
@@ -27,6 +27,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
@@ -497,6 +498,8 @@ const QEMULogItem qemu_log_items[] = {
       "open a separate log file per thread; filename must contain '%d'" },
     { CPU_LOG_TB_VPU, "vpu",
       "include VPU registers in the 'cpu' logging" },
+    { CPU_LOG_TB_STATS, "tb_stats_{all,jit,exec}",
+      "enable collection of TBs statistics at startup" },
     { 0, NULL, NULL },
 };
 
@@ -518,6 +521,21 @@ 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_")) {
+            char *p = *tmp + 9;
+            uint32_t flag = TB_NONE_STATS;
+            if (g_str_has_prefix(p, "all")) {
+                flag = TB_ALL_STATS;
+            } else if (g_str_has_prefix(p, "jit")) {
+                flag = TB_JIT_STATS;
+            } else if (g_str_has_prefix(p, "exec")) {
+                flag = TB_EXEC_STATS;
+            }
+            if (flag != TB_NONE_STATS) {
+                mask |= CPU_LOG_TB_STATS;
+                set_tbstats_flag(flag);
+                enable_collect_tb_stats();
+            }
         } else {
             for (item = qemu_log_items; item->mask != 0; item++) {
                 if (g_str_equal(*tmp, item->name)) {
Signature · 1 line
2.25.1
author
#8 lore raw

PATCH v16 8/9 tb-stats: dump hot TBs at the end of the execution

In reply to Fei Wu

Dump the hottest TBs if -d tb_stats_{all,jit,exec}[:dump_num_at_exit]

---
 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              |  4 ++++
 util/log.c                    | 12 ++++++++++--
 7 files changed, 61 insertions(+), 2 deletions(-)
 create mode 100644 include/exec/tb-stats-dump.h
accel/tcg/tb-stats.c +21 -0
index 73f0a25781..1b038b7b85 100644
@@ -17,6 +17,7 @@
 #include "qemu/log.h"
 
 #include "exec/tb-stats.h"
+#include "exec/tb-stats-dump.h"
 #include "tb-context.h"
 
 #include "internal.h"
@@ -29,6 +30,7 @@ enum TBStatsStatus {
 
 static enum TBStatsStatus tcg_collect_tb_stats;
 static uint32_t tbstats_flag;
+static int max_dump_tbs;
 
 static GPtrArray *last_search;
 
@@ -300,6 +302,20 @@ void dump_tblist_info(GString *buf, int total, int sort_by)
     }
 }
 
+/*
+ * Dump the final stats
+ */
+void tb_stats_dump(void)
+{
+    if (!tb_stats_collection_enabled()) {
+        return;
+    }
+
+    g_autoptr(GString) buf = g_string_new("");
+    dump_tblist_info(buf, max_dump_tbs, SORT_BY_HOTNESS);
+    qemu_printf("%s", buf->str);
+}
+
 void enable_collect_tb_stats(void)
 {
     tcg_collect_tb_stats = TB_STATS_RUNNING;
@@ -343,3 +359,8 @@ bool tbs_stats_enabled(struct TBStatistics *tbs, uint32_t flag)
     return tb_stats_collection_enabled() &&
            (tbstats_flag & flag);
 }
+
+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 936bd35707..52d0429eeb 100644
@@ -24,5 +24,6 @@ bool tb_stats_collection_disabled(void);
 
 uint32_t get_tbstats_flag(void);
 void set_tbstats_flag(uint32_t flag);
+void set_tbstats_max_tbs(int max);
 
 #endif
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 a9fbcf4862..74cc7430c6 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"
@@ -817,6 +818,7 @@ void qemu_cleanup(void)
     vm_shutdown();
     replay_finish();
 
+    tb_stats_dump();
     job_cancel_sync_all();
     bdrv_close_all();
 
stubs/tb-stats.c +4 -0
index 38ee12313e..78ae1aa217 100644
@@ -30,3 +30,7 @@ void set_tbstats_flag(uint32_t flag)
 {
     return;
 }
+
+void set_tbstats_max_tbs(int max) {
+    return;
+}
util/log.c +10 -2
index 59c781de36..3c15fcac01 100644
@@ -498,8 +498,9 @@ const QEMULogItem qemu_log_items[] = {
       "open a separate log file per thread; filename must contain '%d'" },
     { CPU_LOG_TB_VPU, "vpu",
       "include VPU registers in the 'cpu' logging" },
-    { CPU_LOG_TB_STATS, "tb_stats_{all,jit,exec}",
-      "enable collection of TBs statistics at startup" },
+    { CPU_LOG_TB_STATS, "tb_stats_{all,jit,exec}[:dump_num_at_exit]",
+      "enable collection of TBs statistics at startup "
+      "and dump at exit until given a limit" },
     { 0, NULL, NULL },
 };
 
@@ -526,16 +527,23 @@ int qemu_str_to_log_mask(const char *str)
             uint32_t flag = TB_NONE_STATS;
             if (g_str_has_prefix(p, "all")) {
                 flag = TB_ALL_STATS;
+                p += 3;
             } else if (g_str_has_prefix(p, "jit")) {
                 flag = TB_JIT_STATS;
+                p += 3;
             } else if (g_str_has_prefix(p, "exec")) {
                 flag = TB_EXEC_STATS;
+                p += 4;
             }
             if (flag != TB_NONE_STATS) {
                 mask |= CPU_LOG_TB_STATS;
                 set_tbstats_flag(flag);
                 enable_collect_tb_stats();
             }
+            if (p[0] == ':') {
+                int max_to_dump = atoi(p + 1);
+                set_tbstats_max_tbs(max_to_dump);
+            }
         } else {
             for (item = qemu_log_items; item->mask != 0; item++) {
                 if (g_str_equal(*tmp, item->name)) {
Signature · 1 line
2.25.1
author
#9 lore raw

PATCH v16 9/9 docs: add tb-stats how to

In reply to Fei Wu
---
 docs/devel/index-tcg.rst   |   1 +
 docs/devel/tcg-tbstats.rst | 126 +++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+)
 create mode 100644 docs/devel/tcg-tbstats.rst
docs/devel/index-tcg.rst +1 -0
index b44ff8b5a4..8d2832737f 100644
@@ -14,4 +14,5 @@ are only implementing things for HW accelerated hypervisors.
    multi-thread-tcg
    tcg-icount
    tcg-plugins
+   tcg-tbstats
    replay
docs/devel/tcg-tbstats.rst new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#10 lore raw
In reply to Fei Wu
Quoted 5 lines · Fei Wu

v16
--
* rebase to latest commit 4329d049d (Jun 26)
* original patch 1 (remove CONFIG_PROFILER) has already upstreamed so is removed from here

Hi Richard,

As CONFIG_PROFILER has been removed in upstream, could you please take a look again at this revision?

Thanks,
Fei.

Quoted 62 lines

* add async_safe_run_on_cpu back for info tb-list/tb
* add ram_addr support to monitor_disas and use it to disassemble guest code
* update the commit logs and documents
* small change to do_hmp_tbstats_safe() to avoid memory leaking

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

Fei Wu (4):
accel/tcg: add jit stats to TBStatistics
debug: add -d tb_stats to control TBStatistics
tb-stats: dump hot TBs at the end of the execution
docs: add tb-stats how to

Vanderson M. do Rosario (4):
accel/tcg: introduce TBStatistics structure
accel: collecting TB execution count
monitor: adding tb_stats hmp command
tb-stats: Adding info [tb-list|tb] commands to HMP

 MAINTAINERS                      |   1 +
 accel/tcg/cpu-exec.c             |   6 +
 accel/tcg/meson.build            |   1 +
 accel/tcg/monitor.c              | 216 ++++++++++++++++++
 accel/tcg/tb-context.h           |   1 +
 accel/tcg/tb-hash.h              |   7 +
 accel/tcg/tb-maint.c             |  20 ++
 accel/tcg/tb-stats.c             | 366 +++++++++++++++++++++++++++++++
 accel/tcg/tcg-runtime.c          |   1 +
 accel/tcg/translate-all.c        |  77 ++++++-
 accel/tcg/translator.c           |  31 +++
 disas/disas-mon.c                |  15 +-
 disas/disas.c                    |   2 +
 docs/devel/index-tcg.rst         |   1 +
 docs/devel/tcg-tbstats.rst       | 126 +++++++++++
 hmp-commands-info.hx             |  16 ++
 hmp-commands.hx                  |  16 ++
 include/disas/disas.h            |   8 +-
 include/exec/tb-stats-dump.h     |  21 ++
 include/exec/tb-stats-flags.h    |  29 +++
 include/exec/tb-stats.h          | 130 +++++++++++
 include/exec/translation-block.h |   3 +
 include/monitor/hmp.h            |   3 +
 include/qemu/log.h               |   1 +
 include/tcg/tcg-temp-internal.h  |   2 +
 include/tcg/tcg.h                |  10 +
 linux-user/exit.c                |   2 +
 monitor/hmp-cmds-target.c        |   3 +-
 softmmu/runstate.c               |   2 +
 stubs/meson.build                |   1 +
 stubs/tb-stats.c                 |  36 +++
 tcg/tcg.c                        |  47 +++-
 util/log.c                       |  26 +++
 33 files changed, 1217 insertions(+), 10 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