xref: /qemu/tests/plugin/insn.c (revision b49f4755)
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 static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata)
50 {
51     unsigned int i = cpu_index % MAX_CPUS;
52     InstructionCount *c = &counts[i];
53 
54     c->insn_count++;
55 }
56 
57 static void vcpu_insn_matched_exec_before(unsigned int cpu_index, void *udata)
58 {
59     unsigned int i = cpu_index % MAX_CPUS;
60     Instruction *insn = (Instruction *) udata;
61     Match *match = insn->match;
62     g_autoptr(GString) ts = g_string_new("");
63 
64     insn->hits++;
65     g_string_append_printf(ts, "0x%" PRIx64 ", '%s', %"PRId64 " hits",
66                            insn->vaddr, insn->disas, insn->hits);
67 
68     uint64_t icount = counts[i].insn_count;
69     uint64_t delta = icount - match->last_hit[i];
70 
71     match->hits[i]++;
72     match->total_delta[i] += delta;
73 
74     g_string_append_printf(ts,
75                            ", %"PRId64" match hits, "
76                            "Δ+%"PRId64 " since last match,"
77                            " %"PRId64 " avg insns/match\n",
78                            match->hits[i], delta,
79                            match->total_delta[i] / match->hits[i]);
80 
81     match->last_hit[i] = icount;
82 
83     qemu_plugin_outs(ts->str);
84 
85     g_ptr_array_add(match->history[i], insn);
86 }
87 
88 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
89 {
90     size_t n = qemu_plugin_tb_n_insns(tb);
91     size_t i;
92 
93     for (i = 0; i < n; i++) {
94         struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
95 
96         if (do_inline) {
97             qemu_plugin_register_vcpu_insn_exec_inline(
98                 insn, QEMU_PLUGIN_INLINE_ADD_U64, &inline_insn_count, 1);
99         } else {
100             uint64_t vaddr = qemu_plugin_insn_vaddr(insn);
101             qemu_plugin_register_vcpu_insn_exec_cb(
102                 insn, vcpu_insn_exec_before, QEMU_PLUGIN_CB_NO_REGS,
103                 GUINT_TO_POINTER(vaddr));
104         }
105 
106         if (do_size) {
107             size_t sz = qemu_plugin_insn_size(insn);
108             if (sz > sizes->len) {
109                 g_array_set_size(sizes, sz);
110             }
111             unsigned long *cnt = &g_array_index(sizes, unsigned long, sz);
112             (*cnt)++;
113         }
114 
115         /*
116          * If we are tracking certain instructions we will need more
117          * information about the instruction which we also need to
118          * save if there is a hit.
119          */
120         if (matches) {
121             char *insn_disas = qemu_plugin_insn_disas(insn);
122             int j;
123             for (j = 0; j < matches->len; j++) {
124                 Match *m = &g_array_index(matches, Match, j);
125                 if (g_str_has_prefix(insn_disas, m->match_string)) {
126                     Instruction *rec = g_new0(Instruction, 1);
127                     rec->disas = g_strdup(insn_disas);
128                     rec->vaddr = qemu_plugin_insn_vaddr(insn);
129                     rec->match = m;
130                     qemu_plugin_register_vcpu_insn_exec_cb(
131                         insn, vcpu_insn_matched_exec_before,
132                         QEMU_PLUGIN_CB_NO_REGS, rec);
133                 }
134             }
135             g_free(insn_disas);
136         }
137     }
138 }
139 
140 static void plugin_exit(qemu_plugin_id_t id, void *p)
141 {
142     g_autoptr(GString) out = g_string_new(NULL);
143     int i;
144 
145     if (do_size) {
146         for (i = 0; i <= sizes->len; i++) {
147             unsigned long *cnt = &g_array_index(sizes, unsigned long, i);
148             if (*cnt) {
149                 g_string_append_printf(out,
150                                        "len %d bytes: %ld insns\n", i, *cnt);
151             }
152         }
153     } else if (do_inline) {
154         g_string_append_printf(out, "insns: %" PRIu64 "\n", inline_insn_count);
155     } else {
156         uint64_t total_insns = 0;
157         for (i = 0; i < MAX_CPUS; i++) {
158             InstructionCount *c = &counts[i];
159             if (c->insn_count) {
160                 g_string_append_printf(out, "cpu %d insns: %" PRIu64 "\n",
161                                        i, c->insn_count);
162                 total_insns += c->insn_count;
163             }
164         }
165         g_string_append_printf(out, "total insns: %" PRIu64 "\n",
166                                total_insns);
167     }
168     qemu_plugin_outs(out->str);
169 }
170 
171 
172 /* Add a match to the array of matches */
173 static void parse_match(char *match)
174 {
175     Match new_match = { .match_string = match };
176     int i;
177     for (i = 0; i < MAX_CPUS; i++) {
178         new_match.history[i] = g_ptr_array_new();
179     }
180     if (!matches) {
181         matches = g_array_new(false, true, sizeof(Match));
182     }
183     g_array_append_val(matches, new_match);
184 }
185 
186 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
187                                            const qemu_info_t *info,
188                                            int argc, char **argv)
189 {
190     for (int i = 0; i < argc; i++) {
191         char *opt = argv[i];
192         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
193         if (g_strcmp0(tokens[0], "inline") == 0) {
194             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
195                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
196                 return -1;
197             }
198         } else if (g_strcmp0(tokens[0], "sizes") == 0) {
199             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_size)) {
200                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
201                 return -1;
202             }
203         } else if (g_strcmp0(tokens[0], "match") == 0) {
204             parse_match(tokens[1]);
205         } else {
206             fprintf(stderr, "option parsing failed: %s\n", opt);
207             return -1;
208         }
209     }
210 
211     if (do_size) {
212         sizes = g_array_new(true, true, sizeof(unsigned long));
213     }
214 
215     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
216     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
217     return 0;
218 }
219