xref: /qemu/contrib/plugins/hotblocks.c (revision f07a5674cf97b8473e5d06d7b1df9b51e97d553f)
1 /*
2  * Copyright (C) 2019, Alex Bennée <alex.bennee@linaro.org>
3  *
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>
15 
16 #include <qemu-plugin.h>
17 
18 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
19 
20 static bool do_inline;
21 
22 /* Plugins need to take care of their own locking */
23 static GMutex lock;
24 static GHashTable *hotblocks;
25 static guint64 limit = 20;
26 
27 /*
28  * Counting Structure
29  *
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  * checking the number of instructions as well to help
33  * differentiate.
34  */
35 typedef struct {
36     uint64_t start_addr;
37     struct qemu_plugin_scoreboard *exec_count;
38     int trans_count;
39     unsigned long insns;
40 } ExecCount;
41 
42 static gint cmp_exec_count(gconstpointer a, gconstpointer b)
43 {
44     ExecCount *ea = (ExecCount *) a;
45     ExecCount *eb = (ExecCount *) b;
46     uint64_t count_a =
47         qemu_plugin_u64_sum(qemu_plugin_scoreboard_u64(ea->exec_count));
48     uint64_t count_b =
49         qemu_plugin_u64_sum(qemu_plugin_scoreboard_u64(eb->exec_count));
50     return count_a > count_b ? -1 : 1;
51 }
52 
53 static guint exec_count_hash(gconstpointer v)
54 {
55     const ExecCount *e = v;
56     return e->start_addr ^ e->insns;
57 }
58 
59 static gboolean exec_count_equal(gconstpointer v1, gconstpointer v2)
60 {
61     const ExecCount *ea = v1;
62     const ExecCount *eb = v2;
63     return (ea->start_addr == eb->start_addr) &&
64            (ea->insns == eb->insns);
65 }
66 
67 static void exec_count_free(gpointer key, gpointer value, gpointer user_data)
68 {
69     ExecCount *cnt = value;
70     qemu_plugin_scoreboard_free(cnt->exec_count);
71 }
72 
73 static void plugin_exit(qemu_plugin_id_t id, void *p)
74 {
75     g_autoptr(GString) report = g_string_new("collected ");
76     GList *counts, *it;
77     int i;
78 
79     g_string_append_printf(report, "%d entries in the hash table\n",
80                            g_hash_table_size(hotblocks));
81     counts = g_hash_table_get_values(hotblocks);
82     it = g_list_sort(counts, cmp_exec_count);
83 
84     if (it) {
85         g_string_append_printf(report, "pc, tcount, icount, ecount\n");
86 
87         for (i = 0; i < limit && it->next; i++, it = it->next) {
88             ExecCount *rec = (ExecCount *) it->data;
89             g_string_append_printf(
90                 report, "0x%016"PRIx64", %d, %ld, %"PRId64"\n",
91                 rec->start_addr, rec->trans_count,
92                 rec->insns,
93                 qemu_plugin_u64_sum(
94                     qemu_plugin_scoreboard_u64(rec->exec_count)));
95         }
96 
97         g_list_free(it);
98     }
99 
100     qemu_plugin_outs(report->str);
101 
102     g_hash_table_foreach(hotblocks, exec_count_free, NULL);
103     g_hash_table_destroy(hotblocks);
104 }
105 
106 static void plugin_init(void)
107 {
108     hotblocks = g_hash_table_new(exec_count_hash, exec_count_equal);
109 }
110 
111 static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
112 {
113     ExecCount *cnt = (ExecCount *)udata;
114     qemu_plugin_u64_add(qemu_plugin_scoreboard_u64(cnt->exec_count),
115                         cpu_index, 1);
116 }
117 
118 /*
119  * When do_inline we ask the plugin to increment the counter for us.
120  * Otherwise a helper is inserted which calls the vcpu_tb_exec
121  * callback.
122  */
123 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
124 {
125     ExecCount *cnt;
126     uint64_t pc = qemu_plugin_tb_vaddr(tb);
127     size_t insns = qemu_plugin_tb_n_insns(tb);
128 
129     g_mutex_lock(&lock);
130     {
131         ExecCount e;
132         e.start_addr = pc;
133         e.insns = insns;
134         cnt = (ExecCount *) g_hash_table_lookup(hotblocks, &e);
135     }
136 
137     if (cnt) {
138         cnt->trans_count++;
139     } else {
140         cnt = g_new0(ExecCount, 1);
141         cnt->start_addr = pc;
142         cnt->trans_count = 1;
143         cnt->insns = insns;
144         cnt->exec_count = qemu_plugin_scoreboard_new(sizeof(uint64_t));
145         g_hash_table_insert(hotblocks, cnt, cnt);
146     }
147 
148     g_mutex_unlock(&lock);
149 
150     if (do_inline) {
151         qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
152             tb, QEMU_PLUGIN_INLINE_ADD_U64,
153             qemu_plugin_scoreboard_u64(cnt->exec_count), 1);
154     } else {
155         qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec,
156                                              QEMU_PLUGIN_CB_NO_REGS,
157                                              (void *)cnt);
158     }
159 }
160 
161 QEMU_PLUGIN_EXPORT
162 int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
163                         int argc, char **argv)
164 {
165     for (int i = 0; i < argc; i++) {
166         char *opt = argv[i];
167         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
168         if (g_strcmp0(tokens[0], "inline") == 0) {
169             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
170                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
171                 return -1;
172             }
173         } else {
174             fprintf(stderr, "option parsing failed: %s\n", opt);
175             return -1;
176         }
177     }
178 
179     plugin_init();
180 
181     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
182     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
183     return 0;
184 }
185