xref: /openbmc/qemu/tests/tcg/plugins/inline.c (revision 1b4c136b)
1 /*
2  * Copyright (C) 2023, Pierrick Bouvier <pierrick.bouvier@linaro.org>
3  *
4  * Demonstrates and tests usage of inline ops.
5  *
6  * License: GNU GPL, version 2 or later.
7  *   See the COPYING file in the top-level directory.
8  */
9 
10 #include <glib.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 
14 #include <qemu-plugin.h>
15 
16 typedef struct {
17     uint64_t count_tb;
18     uint64_t count_tb_inline;
19     uint64_t count_insn;
20     uint64_t count_insn_inline;
21     uint64_t count_mem;
22     uint64_t count_mem_inline;
23     uint64_t tb_cond_num_trigger;
24     uint64_t tb_cond_track_count;
25     uint64_t insn_cond_num_trigger;
26     uint64_t insn_cond_track_count;
27 } CPUCount;
28 
29 static const uint64_t cond_trigger_limit = 100;
30 
31 typedef struct {
32     uint64_t data_insn;
33     uint64_t data_tb;
34     uint64_t data_mem;
35 } CPUData;
36 
37 static struct qemu_plugin_scoreboard *counts;
38 static qemu_plugin_u64 count_tb;
39 static qemu_plugin_u64 count_tb_inline;
40 static qemu_plugin_u64 count_insn;
41 static qemu_plugin_u64 count_insn_inline;
42 static qemu_plugin_u64 count_mem;
43 static qemu_plugin_u64 count_mem_inline;
44 static qemu_plugin_u64 tb_cond_num_trigger;
45 static qemu_plugin_u64 tb_cond_track_count;
46 static qemu_plugin_u64 insn_cond_num_trigger;
47 static qemu_plugin_u64 insn_cond_track_count;
48 static struct qemu_plugin_scoreboard *data;
49 static qemu_plugin_u64 data_insn;
50 static qemu_plugin_u64 data_tb;
51 static qemu_plugin_u64 data_mem;
52 
53 static uint64_t global_count_tb;
54 static uint64_t global_count_insn;
55 static uint64_t global_count_mem;
56 static unsigned int max_cpu_index;
57 static GMutex tb_lock;
58 static GMutex insn_lock;
59 static GMutex mem_lock;
60 
61 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
62 
stats_insn(void)63 static void stats_insn(void)
64 {
65     const uint64_t expected = global_count_insn;
66     const uint64_t per_vcpu = qemu_plugin_u64_sum(count_insn);
67     const uint64_t inl_per_vcpu =
68         qemu_plugin_u64_sum(count_insn_inline);
69     const uint64_t cond_num_trigger =
70         qemu_plugin_u64_sum(insn_cond_num_trigger);
71     const uint64_t cond_track_left = qemu_plugin_u64_sum(insn_cond_track_count);
72     const uint64_t conditional =
73         cond_num_trigger * cond_trigger_limit + cond_track_left;
74     g_autoptr(GString) stats = g_string_new("");
75     g_string_append_printf(stats, "insn: %" PRIu64 "\n", expected);
76     g_string_append_printf(stats, "insn: %" PRIu64 " (per vcpu)\n", per_vcpu);
77     g_string_append_printf(stats, "insn: %" PRIu64 " (per vcpu inline)\n", inl_per_vcpu);
78     g_string_append_printf(stats, "insn: %" PRIu64 " (cond cb)\n", conditional);
79     qemu_plugin_outs(stats->str);
80     g_assert(expected > 0);
81     g_assert(per_vcpu == expected);
82     g_assert(inl_per_vcpu == expected);
83     g_assert(conditional == expected);
84 }
85 
stats_tb(void)86 static void stats_tb(void)
87 {
88     const uint64_t expected = global_count_tb;
89     const uint64_t per_vcpu = qemu_plugin_u64_sum(count_tb);
90     const uint64_t inl_per_vcpu =
91         qemu_plugin_u64_sum(count_tb_inline);
92     const uint64_t cond_num_trigger = qemu_plugin_u64_sum(tb_cond_num_trigger);
93     const uint64_t cond_track_left = qemu_plugin_u64_sum(tb_cond_track_count);
94     const uint64_t conditional =
95         cond_num_trigger * cond_trigger_limit + cond_track_left;
96     g_autoptr(GString) stats = g_string_new("");
97     g_string_append_printf(stats, "tb: %" PRIu64 "\n", expected);
98     g_string_append_printf(stats, "tb: %" PRIu64 " (per vcpu)\n", per_vcpu);
99     g_string_append_printf(stats, "tb: %" PRIu64 " (per vcpu inline)\n", inl_per_vcpu);
100     g_string_append_printf(stats, "tb: %" PRIu64 " (conditional cb)\n", conditional);
101     qemu_plugin_outs(stats->str);
102     g_assert(expected > 0);
103     g_assert(per_vcpu == expected);
104     g_assert(inl_per_vcpu == expected);
105     g_assert(conditional == expected);
106 }
107 
stats_mem(void)108 static void stats_mem(void)
109 {
110     const uint64_t expected = global_count_mem;
111     const uint64_t per_vcpu = qemu_plugin_u64_sum(count_mem);
112     const uint64_t inl_per_vcpu =
113         qemu_plugin_u64_sum(count_mem_inline);
114     g_autoptr(GString) stats = g_string_new("");
115     g_string_append_printf(stats, "mem: %" PRIu64 "\n", expected);
116     g_string_append_printf(stats, "mem: %" PRIu64 " (per vcpu)\n", per_vcpu);
117     g_string_append_printf(stats, "mem: %" PRIu64 " (per vcpu inline)\n", inl_per_vcpu);
118     qemu_plugin_outs(stats->str);
119     g_assert(expected > 0);
120     g_assert(per_vcpu == expected);
121     g_assert(inl_per_vcpu == expected);
122 }
123 
plugin_exit(qemu_plugin_id_t id,void * udata)124 static void plugin_exit(qemu_plugin_id_t id, void *udata)
125 {
126     const unsigned int num_cpus = qemu_plugin_num_vcpus();
127     g_autoptr(GString) stats = g_string_new("");
128     g_assert(num_cpus == max_cpu_index + 1);
129 
130     for (int i = 0; i < num_cpus ; ++i) {
131         const uint64_t tb = qemu_plugin_u64_get(count_tb, i);
132         const uint64_t tb_inline = qemu_plugin_u64_get(count_tb_inline, i);
133         const uint64_t insn = qemu_plugin_u64_get(count_insn, i);
134         const uint64_t insn_inline = qemu_plugin_u64_get(count_insn_inline, i);
135         const uint64_t mem = qemu_plugin_u64_get(count_mem, i);
136         const uint64_t mem_inline = qemu_plugin_u64_get(count_mem_inline, i);
137         const uint64_t tb_cond_trigger =
138             qemu_plugin_u64_get(tb_cond_num_trigger, i);
139         const uint64_t tb_cond_left =
140             qemu_plugin_u64_get(tb_cond_track_count, i);
141         const uint64_t insn_cond_trigger =
142             qemu_plugin_u64_get(insn_cond_num_trigger, i);
143         const uint64_t insn_cond_left =
144             qemu_plugin_u64_get(insn_cond_track_count, i);
145         g_string_printf(stats, "cpu %d: tb (%" PRIu64 ", %" PRIu64
146                         ", %" PRIu64 " * %" PRIu64 " + %" PRIu64
147                         ") | "
148                         "insn (%" PRIu64 ", %" PRIu64
149                         ", %" PRIu64 " * %" PRIu64 " + %" PRIu64
150                         ") | "
151                         "mem (%" PRIu64 ", %" PRIu64 ")"
152                         "\n",
153                         i,
154                         tb, tb_inline,
155                         tb_cond_trigger, cond_trigger_limit, tb_cond_left,
156                         insn, insn_inline,
157                         insn_cond_trigger, cond_trigger_limit, insn_cond_left,
158                         mem, mem_inline);
159         qemu_plugin_outs(stats->str);
160         g_assert(tb == tb_inline);
161         g_assert(insn == insn_inline);
162         g_assert(mem == mem_inline);
163         g_assert(tb_cond_trigger == tb / cond_trigger_limit);
164         g_assert(tb_cond_left == tb % cond_trigger_limit);
165         g_assert(insn_cond_trigger == insn / cond_trigger_limit);
166         g_assert(insn_cond_left == insn % cond_trigger_limit);
167     }
168 
169     stats_tb();
170     stats_insn();
171     stats_mem();
172 
173     qemu_plugin_scoreboard_free(counts);
174     qemu_plugin_scoreboard_free(data);
175 }
176 
vcpu_tb_exec(unsigned int cpu_index,void * udata)177 static void vcpu_tb_exec(unsigned int cpu_index, void *udata)
178 {
179     qemu_plugin_u64_add(count_tb, cpu_index, 1);
180     g_assert(qemu_plugin_u64_get(data_tb, cpu_index) == (uintptr_t) udata);
181     g_mutex_lock(&tb_lock);
182     max_cpu_index = MAX(max_cpu_index, cpu_index);
183     global_count_tb++;
184     g_mutex_unlock(&tb_lock);
185 }
186 
vcpu_tb_cond_exec(unsigned int cpu_index,void * udata)187 static void vcpu_tb_cond_exec(unsigned int cpu_index, void *udata)
188 {
189     g_assert(qemu_plugin_u64_get(tb_cond_track_count, cpu_index) ==
190              cond_trigger_limit);
191     g_assert(qemu_plugin_u64_get(data_tb, cpu_index) == (uintptr_t) udata);
192     qemu_plugin_u64_set(tb_cond_track_count, cpu_index, 0);
193     qemu_plugin_u64_add(tb_cond_num_trigger, cpu_index, 1);
194 }
195 
vcpu_insn_cond_exec(unsigned int cpu_index,void * udata)196 static void vcpu_insn_cond_exec(unsigned int cpu_index, void *udata)
197 {
198     g_assert(qemu_plugin_u64_get(insn_cond_track_count, cpu_index) ==
199              cond_trigger_limit);
200     g_assert(qemu_plugin_u64_get(data_insn, cpu_index) == (uintptr_t) udata);
201     qemu_plugin_u64_set(insn_cond_track_count, cpu_index, 0);
202     qemu_plugin_u64_add(insn_cond_num_trigger, cpu_index, 1);
203 }
204 
vcpu_insn_exec(unsigned int cpu_index,void * udata)205 static void vcpu_insn_exec(unsigned int cpu_index, void *udata)
206 {
207     qemu_plugin_u64_add(count_insn, cpu_index, 1);
208     g_assert(qemu_plugin_u64_get(data_insn, cpu_index) == (uintptr_t) udata);
209     g_mutex_lock(&insn_lock);
210     global_count_insn++;
211     g_mutex_unlock(&insn_lock);
212 }
213 
vcpu_mem_access(unsigned int cpu_index,qemu_plugin_meminfo_t info,uint64_t vaddr,void * udata)214 static void vcpu_mem_access(unsigned int cpu_index,
215                             qemu_plugin_meminfo_t info,
216                             uint64_t vaddr,
217                             void *udata)
218 {
219     qemu_plugin_u64_add(count_mem, cpu_index, 1);
220     g_assert(qemu_plugin_u64_get(data_mem, cpu_index) == (uintptr_t) udata);
221     g_mutex_lock(&mem_lock);
222     global_count_mem++;
223     g_mutex_unlock(&mem_lock);
224 }
225 
vcpu_tb_trans(qemu_plugin_id_t id,struct qemu_plugin_tb * tb)226 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
227 {
228     void *tb_store = tb;
229     qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
230         tb, QEMU_PLUGIN_INLINE_STORE_U64, data_tb, (uintptr_t) tb_store);
231     qemu_plugin_register_vcpu_tb_exec_cb(
232         tb, vcpu_tb_exec, QEMU_PLUGIN_CB_NO_REGS, tb_store);
233     qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
234         tb, QEMU_PLUGIN_INLINE_ADD_U64, count_tb_inline, 1);
235 
236     qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu(
237         tb, QEMU_PLUGIN_INLINE_ADD_U64, tb_cond_track_count, 1);
238     qemu_plugin_register_vcpu_tb_exec_cond_cb(
239         tb, vcpu_tb_cond_exec, QEMU_PLUGIN_CB_NO_REGS,
240         QEMU_PLUGIN_COND_EQ, tb_cond_track_count, cond_trigger_limit, tb_store);
241 
242     for (int idx = 0; idx < qemu_plugin_tb_n_insns(tb); ++idx) {
243         struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, idx);
244         void *insn_store = insn;
245         void *mem_store = (char *)insn_store + 0xff;
246 
247         qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
248             insn, QEMU_PLUGIN_INLINE_STORE_U64, data_insn,
249             (uintptr_t) insn_store);
250         qemu_plugin_register_vcpu_insn_exec_cb(
251             insn, vcpu_insn_exec, QEMU_PLUGIN_CB_NO_REGS, insn_store);
252         qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
253             insn, QEMU_PLUGIN_INLINE_ADD_U64, count_insn_inline, 1);
254 
255         qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
256             insn, QEMU_PLUGIN_INLINE_ADD_U64, insn_cond_track_count, 1);
257         qemu_plugin_register_vcpu_insn_exec_cond_cb(
258             insn, vcpu_insn_cond_exec, QEMU_PLUGIN_CB_NO_REGS,
259             QEMU_PLUGIN_COND_EQ, insn_cond_track_count, cond_trigger_limit,
260             insn_store);
261 
262         qemu_plugin_register_vcpu_mem_inline_per_vcpu(
263             insn, QEMU_PLUGIN_MEM_RW,
264             QEMU_PLUGIN_INLINE_STORE_U64,
265             data_mem, (uintptr_t) mem_store);
266         qemu_plugin_register_vcpu_mem_cb(insn, &vcpu_mem_access,
267                                          QEMU_PLUGIN_CB_NO_REGS,
268                                          QEMU_PLUGIN_MEM_RW, mem_store);
269         qemu_plugin_register_vcpu_mem_inline_per_vcpu(
270             insn, QEMU_PLUGIN_MEM_RW,
271             QEMU_PLUGIN_INLINE_ADD_U64,
272             count_mem_inline, 1);
273     }
274 }
275 
276 QEMU_PLUGIN_EXPORT
qemu_plugin_install(qemu_plugin_id_t id,const qemu_info_t * info,int argc,char ** argv)277 int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
278                         int argc, char **argv)
279 {
280     counts = qemu_plugin_scoreboard_new(sizeof(CPUCount));
281     count_tb = qemu_plugin_scoreboard_u64_in_struct(
282         counts, CPUCount, count_tb);
283     count_insn = qemu_plugin_scoreboard_u64_in_struct(
284         counts, CPUCount, count_insn);
285     count_mem = qemu_plugin_scoreboard_u64_in_struct(
286         counts, CPUCount, count_mem);
287     count_tb_inline = qemu_plugin_scoreboard_u64_in_struct(
288         counts, CPUCount, count_tb_inline);
289     count_insn_inline = qemu_plugin_scoreboard_u64_in_struct(
290         counts, CPUCount, count_insn_inline);
291     count_mem_inline = qemu_plugin_scoreboard_u64_in_struct(
292         counts, CPUCount, count_mem_inline);
293     tb_cond_num_trigger = qemu_plugin_scoreboard_u64_in_struct(
294         counts, CPUCount, tb_cond_num_trigger);
295     tb_cond_track_count = qemu_plugin_scoreboard_u64_in_struct(
296         counts, CPUCount, tb_cond_track_count);
297     insn_cond_num_trigger = qemu_plugin_scoreboard_u64_in_struct(
298         counts, CPUCount, insn_cond_num_trigger);
299     insn_cond_track_count = qemu_plugin_scoreboard_u64_in_struct(
300         counts, CPUCount, insn_cond_track_count);
301     data = qemu_plugin_scoreboard_new(sizeof(CPUData));
302     data_insn = qemu_plugin_scoreboard_u64_in_struct(data, CPUData, data_insn);
303     data_tb = qemu_plugin_scoreboard_u64_in_struct(data, CPUData, data_tb);
304     data_mem = qemu_plugin_scoreboard_u64_in_struct(data, CPUData, data_mem);
305 
306     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
307     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
308 
309     return 0;
310 }
311