qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: fei2.wu@intel.com,
	"Vanderson M. do Rosario" <vandersonmr2@gmail.com>,
	"Alex Bennée" <alex.bennee@linaro.org>
Subject: [PATCH v17 15/16] accel/tcg: Add info [tb-list|tb] commands to HMP
Date: Tue,  3 Oct 2023 11:30:57 -0700	[thread overview]
Message-ID: <20231003183058.1639121-16-richard.henderson@linaro.org> (raw)
In-Reply-To: <20231003183058.1639121-1-richard.henderson@linaro.org>

From: "Vanderson M. do Rosario" <vandersonmr2@gmail.com>

These commands allow the exploration of TBs generated by the TCG.
Understand which one hotter, with more guest/host instructions,
and examine the 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

Signed-off-by: Vanderson M. do Rosario <vandersonmr2@gmail.com>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Fei Wu <fei2.wu@intel.com>
[rth: Split out of a larger patch]
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 accel/tcg/tb-context.h |  2 +
 accel/tcg/monitor.c    | 91 ++++++++++++++++++++++++++++++++++++++++++
 accel/tcg/tb-stats.c   |  2 +
 hmp-commands-info.hx   | 14 +++++++
 4 files changed, 109 insertions(+)

diff --git a/accel/tcg/tb-context.h b/accel/tcg/tb-context.h
index 4b1abe392b..29d87200b6 100644
--- a/accel/tcg/tb-context.h
+++ b/accel/tcg/tb-context.h
@@ -35,6 +35,8 @@ struct TBContext {
     /* statistics */
     unsigned tb_flush_count;
     unsigned tb_phys_invalidate_count;
+
+    GPtrArray *last_search;
 };
 
 extern TBContext tb_ctx;
diff --git a/accel/tcg/monitor.c b/accel/tcg/monitor.c
index 370fea883c..1be3218715 100644
--- a/accel/tcg/monitor.c
+++ b/accel/tcg/monitor.c
@@ -15,12 +15,14 @@
 #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 "tcg/tb-stats.h"
 #include "exec/tb-flush.h"
+#include "disas/disas.h"
 #include "internal-common.h"
 #include "tb-context.h"
 
@@ -303,10 +305,99 @@ static void hmp_tbstats(Monitor *mon, const QDict *qdict)
                           RUN_ON_CPU_HOST_INT(flags));
 }
 
+static void hmp_info_tblist(Monitor *mon, const QDict *qdict)
+{
+    int max;
+    const char *sortedby_str;
+    GCompareFunc sort;
+    GPtrArray *array;
+
+    if (!tcg_enabled()) {
+        monitor_printf(mon, "Only available with accel=tcg\n");
+        return;
+    }
+    if (!tb_stats_enabled) {
+        monitor_printf(mon, "TB statistics not being recorded\n");
+        return;
+    }
+
+    max = qdict_get_try_int(qdict, "number", 10);
+    sortedby_str = qdict_get_try_str(qdict, "sortedby");
+
+    if (sortedby_str == NULL || g_str_equal(sortedby_str, "hotness")) {
+        sort = tb_stats_sort_by_coverage;
+    } else if (g_str_equal(sortedby_str, "hg")) {
+        sort = tb_stats_sort_by_hg;
+    } else if (g_str_equal(sortedby_str, "spills")) {
+        sort = tb_stats_sort_by_spills;
+    } else {
+        monitor_printf(mon, "Sort options are: hotness, hg, spills\n");
+        return;
+    }
+
+    g_ptr_array_unref(tb_ctx.last_search);
+    tb_ctx.last_search = NULL;
+
+    array = tb_stats_collect(max, sort);
+    max = array->len;
+    if (max == 0) {
+        monitor_printf(mon, "No TB statistics collected\n");
+        g_ptr_array_free(array, true);
+        return;
+    }
+
+    for (int i = 0; i < max; ++i) {
+        TBStatistics *s = g_ptr_array_index(array, i);
+        g_autoptr(GString) buf = tb_stats_dump(s, i);
+        monitor_puts(mon, buf->str);
+    }
+
+    /* Remember for the next "info tb" */
+    tb_ctx.last_search = array;
+}
+
+static void hmp_info_tb(Monitor *mon, const QDict *qdict)
+{
+    GPtrArray *array;
+    int id;
+
+    if (!tcg_enabled()) {
+        monitor_printf(mon, "Only available with accel=tcg\n");
+        return;
+    }
+
+    array = g_ptr_array_ref(tb_ctx.last_search);
+    if (!array) {
+        monitor_printf(mon, "No TB statistics collected\n");
+        return;
+    }
+
+    id = qdict_get_int(qdict, "id");
+    if (id < array->len) {
+        TBStatistics *s = g_ptr_array_index(array, id);
+        g_autoptr(GString) buf = tb_stats_dump(s, id);
+        monitor_puts(mon, buf->str);
+
+        for (int i = s->tbs->len - 1; i >= 0; --i) {
+            TranslationBlock *tb = g_ptr_array_index(s->tbs, i);
+            if (!(tb->cflags & CF_INVALID)) {
+                monitor_disas(mon, mon_get_cpu(mon), s->phys_pc,
+                              tb->icount, MON_DISAS_GRA);
+            }
+        }
+    } else {
+        monitor_printf(mon, "TB %d information not recorded\n", id);
+    }
+
+    g_ptr_array_unref(array);
+}
+
 static void hmp_tcg_register(void)
 {
     monitor_register_hmp_info_hrt("jit", qmp_x_query_jit);
     monitor_register_hmp_info_hrt("opcount", qmp_x_query_opcount);
     monitor_register_hmp("tb_stats", false, hmp_tbstats);
+    monitor_register_hmp("tb-list", true, hmp_info_tblist);
+    monitor_register_hmp("tb", true, hmp_info_tb);
 }
 type_init(hmp_tcg_register);
diff --git a/accel/tcg/tb-stats.c b/accel/tcg/tb-stats.c
index b2c9445b67..0f84c14a88 100644
--- a/accel/tcg/tb-stats.c
+++ b/accel/tcg/tb-stats.c
@@ -43,6 +43,8 @@ void tb_stats_init(uint32_t flags)
                      CODE_GEN_HTABLE_SIZE, QHT_MODE_AUTO_RESIZE);
         }
     } else {
+        g_ptr_array_unref(tb_ctx.last_search);
+        tb_ctx.last_search = NULL;
         qht_iter(&tb_ctx.stats, tb_stats_free, NULL);
         qht_destroy(&tb_ctx.stats);
     }
diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
index f5b37eb74a..8e9b64cf7f 100644
--- a/hmp-commands-info.hx
+++ b/hmp-commands-info.hx
@@ -262,6 +262,20 @@ 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",
+    },
+    {
+        .name       = "tb",
+        .args_type  = "id:i",
+        .params     = "id",
+        .help       = "show information about one translated block by id,"
+                      "from the result of a previous \"info tb-list\"",
+    },
 #endif
 
 SRST
-- 
2.34.1



  parent reply	other threads:[~2023-10-03 18:33 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-03 18:30 [PATCH v17 00/16] TCG code quality tracking Richard Henderson
2023-10-03 18:30 ` [PATCH v17 01/16] accel/tcg: Move HMP info jit and info opcount code Richard Henderson
2023-10-10 12:09   ` Philippe Mathieu-Daudé
2023-10-15 12:58   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 02/16] tcg: Record orig_nb_ops TCGContext Richard Henderson
2023-10-15 12:57   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 03/16] tcg: Record nb_deleted_ops in TCGContext Richard Henderson
2023-10-15 12:58   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 04/16] tcg: Record nb_spills " Richard Henderson
2023-10-15 12:59   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 05/16] accel/tcg: Add TBStatistics structure Richard Henderson
2023-10-16 14:38   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 06/16] accel/tcg: Collect TB execution statistics Richard Henderson
2023-10-03 18:30 ` [PATCH v17 07/16] accel/tcg: Collect TB jit statistics Richard Henderson
2023-10-10 12:13   ` Philippe Mathieu-Daudé
2023-10-03 18:30 ` [PATCH v17 08/16] accel/tcg: Add tb_stats hmp command Richard Henderson
2023-10-03 18:30 ` [PATCH v17 09/16] util/log: Add Error argument to qemu_str_to_log_mask Richard Henderson
2023-10-10 12:55   ` Markus Armbruster
2023-10-15 18:55     ` Richard Henderson
2023-10-03 18:30 ` [PATCH v17 10/16] util/log: Add -d tb_stats Richard Henderson
2023-10-10 12:34   ` Philippe Mathieu-Daudé
2023-10-15 19:53     ` Richard Henderson
2023-10-03 18:30 ` [PATCH v17 11/16] accel/tcg: Add tb_stats_collect and tb_stats_dump Richard Henderson
2023-10-16 14:48   ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 12/16] softmmu: Export qemu_ram_ptr_length Richard Henderson
2023-10-10 12:31   ` Philippe Mathieu-Daudé
2023-10-03 18:30 ` [PATCH v17 13/16] disas: Allow monitor_disas to read from ram_addr_t Richard Henderson
2023-10-10 12:46   ` Philippe Mathieu-Daudé
2023-10-15 19:21     ` Alex Bennée
2023-10-03 18:30 ` [PATCH v17 14/16] monitor: Change MonitorDec.get_value return type to int64_t Richard Henderson
2023-10-16 14:59   ` Alex Bennée
2023-10-16 15:43   ` Alex Bennée
2023-10-03 18:30 ` Richard Henderson [this message]
2023-10-16 15:02   ` [PATCH v17 15/16] accel/tcg: Add info [tb-list|tb] commands to HMP Alex Bennée
2023-10-03 18:30 ` [PATCH v17 16/16] accel/tcg: Dump hot TBs at the end of the execution Richard Henderson
2023-10-10 12:36   ` Philippe Mathieu-Daudé
2023-10-10 13:23     ` Alex Bennée
2024-11-14  9:28 ` [PATCH v17 00/16] TCG code quality tracking Nikita Shubin
2025-01-21 10:22   ` Chinmay Rath

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231003183058.1639121-16-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=alex.bennee@linaro.org \
    --cc=fei2.wu@intel.com \
    --cc=qemu-devel@nongnu.org \
    --cc=vandersonmr2@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).