xref: /qemu/tests/plugin/insn.c (revision 401e311f)
1 /*
2  * Copyright (C) 2018, Emilio G. Cota <cota@braap.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 <string.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <glib.h>
14 
15 #include <qemu-plugin.h>
16 
17 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
18 
19 #define MAX_CPUS 8 /* lets not go nuts */
20 
21 typedef struct {
22     uint64_t insn_count;
23 } InstructionCount;
24 
25 static InstructionCount counts[MAX_CPUS];
26 static uint64_t inline_insn_count;
27 
28 static bool do_inline;
29 static bool do_size;
30 static GArray *sizes;
31 
32 typedef struct {
33     char *match_string;
34     uint64_t hits[MAX_CPUS];
35     uint64_t last_hit[MAX_CPUS];
36     uint64_t total_delta[MAX_CPUS];
37     GPtrArray *history[MAX_CPUS];
38 } Match;
39 
40 static GArray *matches;
41 
42 typedef struct {
43     Match *match;
44     uint64_t vaddr;
45     uint64_t hits;
46     char *disas;
47 } Instruction;
48 
49 /*
50  * Initialise a new vcpu with reading the register list
51  */
52 static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index)
53 {
54     g_autoptr(GArray) reg_list = qemu_plugin_get_registers();
55     g_autoptr(GByteArray) reg_value = g_byte_array_new();
56 
57     if (reg_list) {
58         for (int i = 0; i < reg_list->len; i++) {
59             qemu_plugin_reg_descriptor *rd = &g_array_index(
60                 reg_list, qemu_plugin_reg_descriptor, i);
61             int count = qemu_plugin_read_register(rd->handle, reg_value);
62             g_assert(count > 0);
63         }
64     }
65 }
66 
67 
68 static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata)
69 {
70     unsigned int i = cpu_index % MAX_CPUS;
71     InstructionCount *c = &counts[i];
72 
73     c->insn_count++;
74 }
75 
76 static void vcpu_insn_matched_exec_before(unsigned int cpu_index, void *udata)
77 {
78     unsigned int i = cpu_index % MAX_CPUS;
79     Instruction *insn = (Instruction *) udata;
80     Match *match = insn->match;
81     g_autoptr(GString) ts = g_string_new("");
82 
83     insn->hits++;
84     g_string_append_printf(ts, "0x%" PRIx64 ", '%s', %"PRId64 " hits",
85                            insn->vaddr, insn->disas, insn->hits);
86 
87     uint64_t icount = counts[i].insn_count;
88     uint64_t delta = icount - match->last_hit[i];
89 
90     match->hits[i]++;
91     match->total_delta[i] += delta;
92 
93     g_string_append_printf(ts,
94                            ", %"PRId64" match hits, "
95                            "Δ+%"PRId64 " since last match,"
96                            " %"PRId64 " avg insns/match\n",
97                            match->hits[i], delta,
98                            match->total_delta[i] / match->hits[i]);
99 
100     match->last_hit[i] = icount;
101 
102     qemu_plugin_outs(ts->str);
103 
104     g_ptr_array_add(match->history[i], insn);
105 }
106 
107 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
108 {
109     size_t n = qemu_plugin_tb_n_insns(tb);
110     size_t i;
111 
112     for (i = 0; i < n; i++) {
113         struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
114 
115         if (do_inline) {
116             qemu_plugin_register_vcpu_insn_exec_inline(
117                 insn, QEMU_PLUGIN_INLINE_ADD_U64, &inline_insn_count, 1);
118         } else {
119             uint64_t vaddr = qemu_plugin_insn_vaddr(insn);
120             qemu_plugin_register_vcpu_insn_exec_cb(
121                 insn, vcpu_insn_exec_before, QEMU_PLUGIN_CB_NO_REGS,
122                 GUINT_TO_POINTER(vaddr));
123         }
124 
125         if (do_size) {
126             size_t sz = qemu_plugin_insn_size(insn);
127             if (sz > sizes->len) {
128                 g_array_set_size(sizes, sz);
129             }
130             unsigned long *cnt = &g_array_index(sizes, unsigned long, sz);
131             (*cnt)++;
132         }
133 
134         /*
135          * If we are tracking certain instructions we will need more
136          * information about the instruction which we also need to
137          * save if there is a hit.
138          */
139         if (matches) {
140             char *insn_disas = qemu_plugin_insn_disas(insn);
141             int j;
142             for (j = 0; j < matches->len; j++) {
143                 Match *m = &g_array_index(matches, Match, j);
144                 if (g_str_has_prefix(insn_disas, m->match_string)) {
145                     Instruction *rec = g_new0(Instruction, 1);
146                     rec->disas = g_strdup(insn_disas);
147                     rec->vaddr = qemu_plugin_insn_vaddr(insn);
148                     rec->match = m;
149                     qemu_plugin_register_vcpu_insn_exec_cb(
150                         insn, vcpu_insn_matched_exec_before,
151                         QEMU_PLUGIN_CB_NO_REGS, rec);
152                 }
153             }
154             g_free(insn_disas);
155         }
156     }
157 }
158 
159 static void plugin_exit(qemu_plugin_id_t id, void *p)
160 {
161     g_autoptr(GString) out = g_string_new(NULL);
162     int i;
163 
164     if (do_size) {
165         for (i = 0; i <= sizes->len; i++) {
166             unsigned long *cnt = &g_array_index(sizes, unsigned long, i);
167             if (*cnt) {
168                 g_string_append_printf(out,
169                                        "len %d bytes: %ld insns\n", i, *cnt);
170             }
171         }
172     } else if (do_inline) {
173         g_string_append_printf(out, "insns: %" PRIu64 "\n", inline_insn_count);
174     } else {
175         uint64_t total_insns = 0;
176         for (i = 0; i < MAX_CPUS; i++) {
177             InstructionCount *c = &counts[i];
178             if (c->insn_count) {
179                 g_string_append_printf(out, "cpu %d insns: %" PRIu64 "\n",
180                                        i, c->insn_count);
181                 total_insns += c->insn_count;
182             }
183         }
184         g_string_append_printf(out, "total insns: %" PRIu64 "\n",
185                                total_insns);
186     }
187     qemu_plugin_outs(out->str);
188 }
189 
190 
191 /* Add a match to the array of matches */
192 static void parse_match(char *match)
193 {
194     Match new_match = { .match_string = match };
195     int i;
196     for (i = 0; i < MAX_CPUS; i++) {
197         new_match.history[i] = g_ptr_array_new();
198     }
199     if (!matches) {
200         matches = g_array_new(false, true, sizeof(Match));
201     }
202     g_array_append_val(matches, new_match);
203 }
204 
205 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
206                                            const qemu_info_t *info,
207                                            int argc, char **argv)
208 {
209     for (int i = 0; i < argc; i++) {
210         char *opt = argv[i];
211         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
212         if (g_strcmp0(tokens[0], "inline") == 0) {
213             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
214                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
215                 return -1;
216             }
217         } else if (g_strcmp0(tokens[0], "sizes") == 0) {
218             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_size)) {
219                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
220                 return -1;
221             }
222         } else if (g_strcmp0(tokens[0], "match") == 0) {
223             parse_match(tokens[1]);
224         } else {
225             fprintf(stderr, "option parsing failed: %s\n", opt);
226             return -1;
227         }
228     }
229 
230     if (do_size) {
231         sizes = g_array_new(true, true, sizeof(unsigned long));
232     }
233 
234     /* Register init, translation block and exit callbacks */
235     qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
236     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
237     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
238     return 0;
239 }
240