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 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
37 struct qemu_plugin_scoreboard
*exec_count
;
42 static gint
cmp_exec_count(gconstpointer a
, gconstpointer b
)
44 ExecCount
*ea
= (ExecCount
*) a
;
45 ExecCount
*eb
= (ExecCount
*) b
;
47 qemu_plugin_u64_sum(qemu_plugin_scoreboard_u64(ea
->exec_count
));
49 qemu_plugin_u64_sum(qemu_plugin_scoreboard_u64(eb
->exec_count
));
50 return count_a
> count_b
? -1 : 1;
53 static void exec_count_free(gpointer key
, gpointer value
, gpointer user_data
)
55 ExecCount
*cnt
= value
;
56 qemu_plugin_scoreboard_free(cnt
->exec_count
);
59 static void plugin_exit(qemu_plugin_id_t id
, void *p
)
61 g_autoptr(GString
) report
= g_string_new("collected ");
65 g_string_append_printf(report
, "%d entries in the hash table\n",
66 g_hash_table_size(hotblocks
));
67 counts
= g_hash_table_get_values(hotblocks
);
68 it
= g_list_sort(counts
, cmp_exec_count
);
71 g_string_append_printf(report
, "pc, tcount, icount, ecount\n");
73 for (i
= 0; i
< limit
&& it
->next
; i
++, it
= it
->next
) {
74 ExecCount
*rec
= (ExecCount
*) it
->data
;
75 g_string_append_printf(
76 report
, "0x%016"PRIx64
", %d, %ld, %"PRId64
"\n",
77 rec
->start_addr
, rec
->trans_count
,
80 qemu_plugin_scoreboard_u64(rec
->exec_count
)));
86 qemu_plugin_outs(report
->str
);
88 g_hash_table_foreach(hotblocks
, exec_count_free
, NULL
);
89 g_hash_table_destroy(hotblocks
);
92 static void plugin_init(void)
94 hotblocks
= g_hash_table_new(NULL
, g_direct_equal
);
97 static void vcpu_tb_exec(unsigned int cpu_index
, void *udata
)
99 ExecCount
*cnt
= (ExecCount
*)udata
;
100 qemu_plugin_u64_add(qemu_plugin_scoreboard_u64(cnt
->exec_count
),
105 * When do_inline we ask the plugin to increment the counter for us.
106 * Otherwise a helper is inserted which calls the vcpu_tb_exec
109 static void vcpu_tb_trans(qemu_plugin_id_t id
, struct qemu_plugin_tb
*tb
)
112 uint64_t pc
= qemu_plugin_tb_vaddr(tb
);
113 size_t insns
= qemu_plugin_tb_n_insns(tb
);
114 uint64_t hash
= pc
^ insns
;
117 cnt
= (ExecCount
*) g_hash_table_lookup(hotblocks
, (gconstpointer
) hash
);
121 cnt
= g_new0(ExecCount
, 1);
122 cnt
->start_addr
= pc
;
123 cnt
->trans_count
= 1;
125 cnt
->exec_count
= qemu_plugin_scoreboard_new(sizeof(uint64_t));
126 g_hash_table_insert(hotblocks
, (gpointer
) hash
, (gpointer
) cnt
);
129 g_mutex_unlock(&lock
);
132 qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
133 tb
, QEMU_PLUGIN_INLINE_ADD_U64
,
134 qemu_plugin_scoreboard_u64(cnt
->exec_count
), 1);
136 qemu_plugin_register_vcpu_tb_exec_cb(tb
, vcpu_tb_exec
,
137 QEMU_PLUGIN_CB_NO_REGS
,
143 int qemu_plugin_install(qemu_plugin_id_t id
, const qemu_info_t
*info
,
144 int argc
, char **argv
)
146 for (int i
= 0; i
< argc
; i
++) {
148 g_auto(GStrv
) tokens
= g_strsplit(opt
, "=", 2);
149 if (g_strcmp0(tokens
[0], "inline") == 0) {
150 if (!qemu_plugin_bool_parse(tokens
[0], tokens
[1], &do_inline
)) {
151 fprintf(stderr
, "boolean argument parsing failed: %s\n", opt
);
155 fprintf(stderr
, "option parsing failed: %s\n", opt
);
162 qemu_plugin_register_vcpu_tb_trans_cb(id
, vcpu_tb_trans
);
163 qemu_plugin_register_atexit_cb(id
, plugin_exit
, NULL
);