[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v4 48/54] tests/plugin: add a hotblocks plugin
From: |
Alex Bennée |
Subject: |
[Qemu-devel] [PATCH v4 48/54] tests/plugin: add a hotblocks plugin |
Date: |
Wed, 31 Jul 2019 17:07:13 +0100 |
This is a simple plugin to track which translation blocks are call
most often. As we don't have a view of the internals of TCG we can
only work by the address of the start of the block so we also need to
tracks how often the address is translated.
As there will be multiple blocks starting at the same address. We can
try and work around this by futzing the value to feed to the hash with
the insn count.
Signed-off-by: Alex Bennée <address@hidden>
---
tests/plugin/Makefile | 1 +
tests/plugin/hotblocks.c | 146 +++++++++++++++++++++++++++++++++++++++
2 files changed, 147 insertions(+)
create mode 100644 tests/plugin/hotblocks.c
diff --git a/tests/plugin/Makefile b/tests/plugin/Makefile
index f9a3546ea32..e74940eaac5 100644
--- a/tests/plugin/Makefile
+++ b/tests/plugin/Makefile
@@ -10,6 +10,7 @@ NAMES += bb
NAMES += empty
NAMES += insn
NAMES += mem
+NAMES += hotblocks
SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES)))
diff --git a/tests/plugin/hotblocks.c b/tests/plugin/hotblocks.c
new file mode 100644
index 00000000000..3654afbc887
--- /dev/null
+++ b/tests/plugin/hotblocks.c
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2019, Alex Bennée <address@hidden>
+ *
+ * License: GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include <inttypes.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <glib.h>
+
+#include <qemu-plugin.h>
+
+static bool do_inline;
+static int stdout_fd;
+
+/* Plugins need to take care of their own locking */
+static GMutex lock;
+static GHashTable *hotblocks;
+static guint64 limit = 20;
+
+/*
+ * Counting Structure
+ *
+ * The internals of the TCG are not exposed to plugins so we can only
+ * get the starting PC for each block. We cheat this slightly by
+ * xor'ing the number of instructions to the hash to help
+ * differentiate.
+ */
+typedef struct {
+ uint64_t start_addr;
+ uint64_t exec_count;
+ int trans_count;
+ unsigned long insns;
+} ExecCount;
+
+static gint cmp_exec_count(gconstpointer a, gconstpointer b)
+{
+ ExecCount *ea = (ExecCount *) a;
+ ExecCount *eb = (ExecCount *) b;
+ return ea->exec_count > eb->exec_count ? -1 : 1;
+}
+
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+ GString *report = g_string_new("collected ");
+ GList *counts, *it;
+ int i;
+
+ g_mutex_lock(&lock);
+ g_string_append_printf(report, "%d entries in the hash table\n",
+ g_hash_table_size(hotblocks));
+ counts = g_hash_table_get_values(hotblocks);
+ it = g_list_sort(counts, cmp_exec_count);
+
+ g_string_append_printf(report, "pc, tcount, icount, ecount\n");
+
+ for (i = 0; i < limit && it->next; i++, it = it->next) {
+ ExecCount *rec = (ExecCount *) it->data;
+ g_string_append_printf(report, "%#016"PRIx64", %d, %ld, %"PRId64"\n",
+ rec->start_addr, rec->trans_count,
+ rec->insns, rec->exec_count);
+ }
+
+ g_mutex_unlock(&lock);
+ g_list_free(it);
+
+ dprintf(stdout_fd, "%s", report->str);
+ g_string_free(report, true);
+}
+
+static void plugin_init(void)
+{
+ hotblocks = g_hash_table_new(NULL, g_direct_equal);
+}
+
+static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
+{
+ ExecCount *cnt;
+ uint64_t hash = (uint64_t) udata;
+
+ g_mutex_lock(&lock);
+ cnt = (ExecCount *) g_hash_table_lookup(hotblocks, (gconstpointer) hash);
+ /* should always succeed */
+ g_assert(cnt);
+ cnt->exec_count++;
+ g_mutex_unlock(&lock);
+}
+
+/*
+ * When do_inline we ask the plugin to increment the counter for us.
+ * Otherwise a helper is inserted which calls the vcpu_tb_exec
+ * callback.
+ */
+static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
+{
+ ExecCount *cnt;
+ uint64_t pc = qemu_plugin_tb_vaddr(tb);
+ unsigned long insns = qemu_plugin_tb_n_insns(tb);
+ uint64_t hash = pc ^ insns;
+
+ g_mutex_lock(&lock);
+ cnt = (ExecCount *) g_hash_table_lookup(hotblocks, (gconstpointer) hash);
+ if (cnt) {
+ cnt->trans_count++;
+ } else {
+ cnt = g_new0(ExecCount, 1);
+ cnt->start_addr = pc;
+ cnt->trans_count = 1;
+ cnt->insns = insns;
+ g_hash_table_insert(hotblocks, (gpointer) hash, (gpointer) cnt);
+ }
+
+ g_mutex_unlock(&lock);
+
+ if (do_inline) {
+ qemu_plugin_register_vcpu_tb_exec_inline(tb,
QEMU_PLUGIN_INLINE_ADD_U64,
+ &cnt->exec_count, 1);
+ } else {
+ qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec,
+ QEMU_PLUGIN_CB_NO_REGS,
+ (void *)hash);
+ }
+}
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc,
+ char **argv)
+{
+ if (argc && strcmp(argv[0], "inline") == 0) {
+ do_inline = true;
+ }
+
+ /* to be used when in the exit hook */
+ stdout_fd = dup(STDOUT_FILENO);
+ assert(stdout_fd);
+
+ plugin_init();
+
+ qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
+ qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
+ return 0;
+}
--
2.20.1
- [Qemu-devel] [PATCH v4 23/54] tcg: let plugins instrument virtual memory accesses, (continued)
- [Qemu-devel] [PATCH v4 23/54] tcg: let plugins instrument virtual memory accesses, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 20/54] plugin-gen: add module for TCG-related code, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 17/54] cputlb: document get_page_addr_code, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 38/54] target/riscv: fetch code with translator_ld, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 36/54] target/m68k: fetch code with translator_ld, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 49/54] plugin: add qemu_plugin_insn_disas helper, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 52/54] accel/stubs: reduce headers from tcg-stub, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 47/54] tests/tcg: enable plugin testing, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 34/54] target/i386: fetch code with translator_ld, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 24/54] plugins: implement helpers for resolving hwaddr, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 48/54] tests/plugin: add a hotblocks plugin,
Alex Bennée <=
- [Qemu-devel] [PATCH v4 35/54] target/hppa: fetch code with translator_ld, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 46/54] tests/plugin: add sample plugins, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 26/54] *-user: notify plugin of exit, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 16/54] queue: add QTAILQ_REMOVE_SEVERAL, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 43/54] plugin: add API symbols to qemu-plugins.symbols, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 21/54] atomic_template: fix indentation in GEN_ATOMIC_HELPER, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 28/54] cpu: hook plugin vcpu events, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 37/54] target/alpha: fetch code with translator_ld, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 44/54] vl: support -plugin option, Alex Bennée, 2019/07/31
- [Qemu-devel] [PATCH v4 32/54] target/ppc: fetch code with translator_ld, Alex Bennée, 2019/07/31