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.
16 #include <qemu-plugin.h>
18 static bool do_inline
;
20 /* Plugins need to take care of their own locking */
22 static GHashTable
*hotblocks
;
23 static guint64 limit
= 20;
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
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 ");
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
);
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
);
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
)
84 uint64_t hash
= (uint64_t) udata
;
87 cnt
= (ExecCount
*) g_hash_table_lookup(hotblocks
, (gconstpointer
) hash
);
88 /* should always succeed */
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
99 static void vcpu_tb_trans(qemu_plugin_id_t id
, struct qemu_plugin_tb
*tb
)
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
;
107 cnt
= (ExecCount
*) g_hash_table_lookup(hotblocks
, (gconstpointer
) hash
);
111 cnt
= g_new0(ExecCount
, 1);
112 cnt
->start_addr
= pc
;
113 cnt
->trans_count
= 1;
115 g_hash_table_insert(hotblocks
, (gpointer
) hash
, (gpointer
) cnt
);
118 g_mutex_unlock(&lock
);
121 qemu_plugin_register_vcpu_tb_exec_inline(tb
, QEMU_PLUGIN_INLINE_ADD_U64
,
122 &cnt
->exec_count
, 1);
124 qemu_plugin_register_vcpu_tb_exec_cb(tb
, vcpu_tb_exec
,
125 QEMU_PLUGIN_CB_NO_REGS
,
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) {
140 qemu_plugin_register_vcpu_tb_trans_cb(id
, vcpu_tb_trans
);
141 qemu_plugin_register_atexit_cb(id
, plugin_exit
, NULL
);