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 11/16] accel/tcg: Add tb_stats_collect and tb_stats_dump
Date: Tue,  3 Oct 2023 11:30:53 -0700	[thread overview]
Message-ID: <20231003183058.1639121-12-richard.henderson@linaro.org> (raw)
In-Reply-To: <20231003183058.1639121-1-richard.henderson@linaro.org>

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

These functions will be used together to output statistics.

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>
---
 include/tcg/tb-stats.h |  25 +++++++++
 accel/tcg/tb-stats.c   | 119 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+)

diff --git a/include/tcg/tb-stats.h b/include/tcg/tb-stats.h
index 1ec0d13eff..edee73b63b 100644
--- a/include/tcg/tb-stats.h
+++ b/include/tcg/tb-stats.h
@@ -129,4 +129,29 @@ void tb_stats_reset_tbs(void);
 TBStatistics *tb_stats_lookup(tb_page_addr_t phys_pc, vaddr pc,
                               uint32_t flags, uint64_t flags2);
 
+/**
+ * tb_stats_collect:
+ * @max: maximum number of results
+ * @sort: sort function
+ *
+ * Collect all TBStatistics and return the first @max items,
+ * as dictated by the sort criteria.
+ */
+GPtrArray *tb_stats_collect(unsigned max, GCompareFunc sort);
+
+/* Sort functions for tb_stats_collect. */
+gint tb_stats_sort_by_spills(gconstpointer, gconstpointer);
+gint tb_stats_sort_by_coverage(gconstpointer, gconstpointer);
+gint tb_stats_sort_by_hg(gconstpointer, gconstpointer);
+
+/**
+ * tb_stats_dump:
+ * @s: structure to dump
+ * @index: label to emit
+ *
+ * Return a string with the rendering of the data in @s;
+ * @index is included in the output.
+ */
+GString *tb_stats_dump(TBStatistics *s, unsigned index);
+
 #endif /* TCG_TB_STATS_H */
diff --git a/accel/tcg/tb-stats.c b/accel/tcg/tb-stats.c
index 424c9a90ec..b2c9445b67 100644
--- a/accel/tcg/tb-stats.c
+++ b/accel/tcg/tb-stats.c
@@ -83,3 +83,122 @@ TBStatistics *tb_stats_lookup(tb_page_addr_t phys_pc, vaddr pc,
     }
     return s;
 }
+
+static void tb_stats_collect_iter(void *p, uint32_t hash, void *u)
+{
+    g_ptr_array_add(u, p);
+}
+
+static void calculate_coverages(GPtrArray *array)
+{
+    double total_exec_count = 0;
+    guint i, n = array->len;
+
+    for (i = 0; i < n; ++i) {
+        TBStatistics *s = g_ptr_array_index(array, i);
+        double avg_insns = 1;
+        double exec_count;
+
+        if (s->translations.total) {
+            avg_insns = s->code.num_guest_inst / (double)s->translations.total;
+        }
+        exec_count = ((double)s->executions.atomic + s->executions.normal)
+                     / avg_insns;
+        s->executions.coverage = exec_count;
+        total_exec_count += exec_count;
+    }
+
+    for (i = 0; i < n; ++i) {
+        TBStatistics *s = g_ptr_array_index(array, i);
+        s->executions.coverage /= total_exec_count;
+    }
+}
+
+GPtrArray *tb_stats_collect(unsigned max, GCompareFunc sort)
+{
+    GPtrArray *array = g_ptr_array_new();
+
+    /*
+     * Collect all TBStatistics and sort.
+     * Note that coverage data requires both execution and jit collection.
+     */
+    qht_iter(&tb_ctx.stats, tb_stats_collect_iter, array);
+    calculate_coverages(array);
+    g_ptr_array_sort(array, sort);
+
+    /* Truncate to the first MAX entries. */
+    if (max < array->len) {
+        g_ptr_array_set_size(array, max);
+    }
+    return array;
+}
+
+gint tb_stats_sort_by_spills(gconstpointer p1, gconstpointer p2)
+{
+    const TBStatistics *s1 = *(TBStatistics **)p1;
+    const TBStatistics *s2 = *(TBStatistics **)p2;
+    double c1 = (double)s1->code.spills / s1->translations.total;
+    double c2 = (double)s2->code.spills / s2->translations.total;
+
+    return c1 < c2 ? 1 : c1 == c2 ? 0 : -1;
+}
+
+gint tb_stats_sort_by_coverage(gconstpointer p1, gconstpointer p2)
+{
+    const TBStatistics *s1 = *(TBStatistics **)p1;
+    const TBStatistics *s2 = *(TBStatistics **)p2;
+    double c1 = s1->executions.coverage;
+    double c2 = s2->executions.coverage;
+
+    return c1 < c2 ? 1 : c1 == c2 ? 0 : -1;
+}
+
+gint tb_stats_sort_by_hg(gconstpointer p1, gconstpointer p2)
+{
+    const TBStatistics *s1 = *(TBStatistics **)p1;
+    const TBStatistics *s2 = *(TBStatistics **)p2;
+    double c1 = (double)s1->code.out_len / s1->code.num_guest_inst;
+    double c2 = (double)s2->code.out_len / s2->code.num_guest_inst;
+
+    return c1 < c2 ? 1 : c1 == c2 ? 0 : -1;
+}
+
+GString *tb_stats_dump(TBStatistics *s, unsigned index)
+{
+    unsigned n = s->tbs->len;
+    unsigned invalid = 0;
+    GString *buf;
+
+    for (unsigned i = 0; i < n; ++i) {
+        TranslationBlock *tb = g_ptr_array_index(s->tbs, i);
+        if (tb->cflags & CF_INVALID) {
+            invalid += 1;
+        }
+    }
+
+    buf = g_string_new("");
+    g_string_append_printf(buf,
+        "TB id:%u | phys:0x" TB_PAGE_ADDR_FMT " virt=%" VADDR_PRIx
+        " flags:0x%08x invalid:%u/%u\n",
+        index, s->phys_pc, s->pc, s->flags, invalid, n - invalid);
+
+    if (tb_stats_enabled & TB_STATS_EXEC) {
+        g_string_append_printf(buf,
+            "\t| exec:%lu/%lu coverage:%.2f%%\n",
+            s->executions.normal, s->executions.atomic,
+            s->executions.coverage * 100);
+    }
+
+    if (tb_stats_enabled & TB_STATS_JIT) {
+        g_string_append_printf(buf,
+            "\t| trans:%lu inst: g:%lu op:%lu op_opt:%lu spills:%ld\n"
+            "\t| h/g (host bytes / guest insts): %f\n",
+            s->translations.total,
+            s->code.num_guest_inst / s->translations.total,
+            s->code.num_tcg_ops / s->translations.total,
+            s->code.num_tcg_ops_opt / s->translations.total,
+            s->code.spills / s->translations.total,
+            (double)s->code.out_len / s->code.num_guest_inst);
+    }
+    return buf;
+}
-- 
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 ` Richard Henderson [this message]
2023-10-16 14:48   ` [PATCH v17 11/16] accel/tcg: Add tb_stats_collect and tb_stats_dump 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 ` [PATCH v17 15/16] accel/tcg: Add info [tb-list|tb] commands to HMP Richard Henderson
2023-10-16 15:02   ` 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-12-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).