tests/plugin: add a hotblocks plugin
[qemu/ar7.git] / tests / plugin / hotblocks.c
blob1bd183849a120bf609eca1068f44bf93546d2646
1 /*
2 * Copyright (C) 2019, Alex Bennée <alex.bennee@linaro.org>
4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7 #include <inttypes.h>
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <inttypes.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <glib.h>
16 #include <qemu-plugin.h>
18 static bool do_inline;
20 /* Plugins need to take care of their own locking */
21 static GMutex lock;
22 static GHashTable *hotblocks;
23 static guint64 limit = 20;
26 * Counting Structure
28 * The internals of the TCG are not exposed to plugins so we can only
29 * get the starting PC for each block. We cheat this slightly by
30 * xor'ing the number of instructions to the hash to help
31 * differentiate.
33 typedef struct {
34 uint64_t start_addr;
35 uint64_t exec_count;
36 int trans_count;
37 unsigned long insns;
38 } ExecCount;
40 static gint cmp_exec_count(gconstpointer a, gconstpointer b)
42 ExecCount *ea = (ExecCount *) a;
43 ExecCount *eb = (ExecCount *) b;
44 return ea->exec_count > eb->exec_count ? -1 : 1;
47 static void plugin_exit(qemu_plugin_id_t id, void *p)
49 g_autoptr(GString) report = g_string_new("collected ");
50 GList *counts, *it;
51 int i;
53 g_mutex_lock(&lock);
54 g_string_append_printf(report, "%d entries in the hash table\n",
55 g_hash_table_size(hotblocks));
56 counts = g_hash_table_get_values(hotblocks);
57 it = g_list_sort(counts, cmp_exec_count);
59 if (it) {
60 g_string_append_printf(report, "pc, tcount, icount, ecount\n");
62 for (i = 0; i < limit && it->next; i++, it = it->next) {
63 ExecCount *rec = (ExecCount *) it->data;
64 g_string_append_printf(report, "%#016"PRIx64", %d, %ld, %"PRId64"\n",
65 rec->start_addr, rec->trans_count,
66 rec->insns, rec->exec_count);
69 g_list_free(it);
70 g_mutex_unlock(&lock);
73 qemu_plugin_outs(report->str);
76 static void plugin_init(void)
78 hotblocks = g_hash_table_new(NULL, g_direct_equal);
81 static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
83 ExecCount *cnt;
84 uint64_t hash = (uint64_t) udata;
86 g_mutex_lock(&lock);
87 cnt = (ExecCount *) g_hash_table_lookup(hotblocks, (gconstpointer) hash);
88 /* should always succeed */
89 g_assert(cnt);
90 cnt->exec_count++;
91 g_mutex_unlock(&lock);
95 * When do_inline we ask the plugin to increment the counter for us.
96 * Otherwise a helper is inserted which calls the vcpu_tb_exec
97 * callback.
99 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
101 ExecCount *cnt;
102 uint64_t pc = qemu_plugin_tb_vaddr(tb);
103 unsigned long insns = qemu_plugin_tb_n_insns(tb);
104 uint64_t hash = pc ^ insns;
106 g_mutex_lock(&lock);
107 cnt = (ExecCount *) g_hash_table_lookup(hotblocks, (gconstpointer) hash);
108 if (cnt) {
109 cnt->trans_count++;
110 } else {
111 cnt = g_new0(ExecCount, 1);
112 cnt->start_addr = pc;
113 cnt->trans_count = 1;
114 cnt->insns = insns;
115 g_hash_table_insert(hotblocks, (gpointer) hash, (gpointer) cnt);
118 g_mutex_unlock(&lock);
120 if (do_inline) {
121 qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64,
122 &cnt->exec_count, 1);
123 } else {
124 qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec,
125 QEMU_PLUGIN_CB_NO_REGS,
126 (void *)hash);
130 QEMU_PLUGIN_EXPORT
131 int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
132 int argc, char **argv)
134 if (argc && strcmp(argv[0], "inline") == 0) {
135 do_inline = true;
138 plugin_init();
140 qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
141 qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
142 return 0;