4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
16 #include <qemu-plugin.h>
18 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
20 static bool do_inline;
22 /* Plugins need to take care of their own locking */
24 static GHashTable *hotblocks;
25 static guint64 limit = 20;
30 * The internals of the TCG are not exposed to plugins so we can only
31 * get the starting PC for each block. We cheat this slightly by
32 * xor'ing the number of instructions to the hash to help
42 static gint cmp_exec_count(gconstpointer a, gconstpointer b)
44 ExecCount *ea = (ExecCount *) a;
45 ExecCount *eb = (ExecCount *) b;
46 return ea->exec_count > eb->exec_count ? -1 : 1;
49 static void plugin_exit(qemu_plugin_id_t id, void *p)
51 g_autoptr(GString) report = g_string_new("collected ");
56 g_string_append_printf(report, "%d entries in the hash table\n",
57 g_hash_table_size(hotblocks));
58 counts = g_hash_table_get_values(hotblocks);
59 it = g_list_sort(counts, cmp_exec_count);
62 g_string_append_printf(report, "pc, tcount, icount, ecount\n");
64 for (i = 0; i < limit && it->next; i++, it = it->next) {
65 ExecCount *rec = (ExecCount *) it->data;
66 g_string_append_printf(report, "%#016"PRIx64", %d, %ld, %"PRId64"\n",
67 rec->start_addr, rec->trans_count,
68 rec->insns, rec->exec_count);
72 g_mutex_unlock(&lock);
75 qemu_plugin_outs(report->str);
78 static void plugin_init(void)
80 hotblocks = g_hash_table_new(NULL, g_direct_equal);
83 static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
86 uint64_t hash = (uint64_t) udata;
89 cnt = (ExecCount *) g_hash_table_lookup(hotblocks, (gconstpointer) hash);
90 /* should always succeed */
93 g_mutex_unlock(&lock);
97 * When do_inline we ask the plugin to increment the counter for us.
98 * Otherwise a helper is inserted which calls the vcpu_tb_exec
101 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
104 uint64_t pc = qemu_plugin_tb_vaddr(tb);
105 unsigned long insns = qemu_plugin_tb_n_insns(tb);
106 uint64_t hash = pc ^ insns;
109 cnt = (ExecCount *) g_hash_table_lookup(hotblocks, (gconstpointer) hash);
113 cnt = g_new0(ExecCount, 1);
114 cnt->start_addr = pc;
115 cnt->trans_count = 1;
117 g_hash_table_insert(hotblocks, (gpointer) hash, (gpointer) cnt);
120 g_mutex_unlock(&lock);
123 qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64,
124 &cnt->exec_count, 1);
126 qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec,
127 QEMU_PLUGIN_CB_NO_REGS,
133 int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
134 int argc, char **argv)
136 if (argc && strcmp(argv[0], "inline") == 0) {
142 qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
143 qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);