xref: /openbmc/qemu/tests/tcg/plugins/mem.c (revision 1b4c136b)
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 typedef struct {
20     uint64_t mem_count;
21     uint64_t io_count;
22 } CPUCount;
23 
24 static struct qemu_plugin_scoreboard *counts;
25 static qemu_plugin_u64 mem_count;
26 static qemu_plugin_u64 io_count;
27 static bool do_inline, do_callback;
28 static bool do_haddr;
29 static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
30 
plugin_exit(qemu_plugin_id_t id,void * p)31 static void plugin_exit(qemu_plugin_id_t id, void *p)
32 {
33     g_autoptr(GString) out = g_string_new("");
34 
35     if (do_inline || do_callback) {
36         g_string_printf(out, "mem accesses: %" PRIu64 "\n",
37                         qemu_plugin_u64_sum(mem_count));
38     }
39     if (do_haddr) {
40         g_string_append_printf(out, "io accesses: %" PRIu64 "\n",
41                                qemu_plugin_u64_sum(io_count));
42     }
43     qemu_plugin_outs(out->str);
44     qemu_plugin_scoreboard_free(counts);
45 }
46 
vcpu_mem(unsigned int cpu_index,qemu_plugin_meminfo_t meminfo,uint64_t vaddr,void * udata)47 static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
48                      uint64_t vaddr, void *udata)
49 {
50     if (do_haddr) {
51         struct qemu_plugin_hwaddr *hwaddr;
52         hwaddr = qemu_plugin_get_hwaddr(meminfo, vaddr);
53         if (qemu_plugin_hwaddr_is_io(hwaddr)) {
54             qemu_plugin_u64_add(io_count, cpu_index, 1);
55         } else {
56             qemu_plugin_u64_add(mem_count, cpu_index, 1);
57         }
58     } else {
59         qemu_plugin_u64_add(mem_count, cpu_index, 1);
60     }
61 }
62 
vcpu_tb_trans(qemu_plugin_id_t id,struct qemu_plugin_tb * tb)63 static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
64 {
65     size_t n = qemu_plugin_tb_n_insns(tb);
66     size_t i;
67 
68     for (i = 0; i < n; i++) {
69         struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
70 
71         if (do_inline) {
72             qemu_plugin_register_vcpu_mem_inline_per_vcpu(
73                 insn, rw,
74                 QEMU_PLUGIN_INLINE_ADD_U64,
75                 mem_count, 1);
76         }
77         if (do_callback) {
78             qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
79                                              QEMU_PLUGIN_CB_NO_REGS,
80                                              rw, NULL);
81         }
82     }
83 }
84 
qemu_plugin_install(qemu_plugin_id_t id,const qemu_info_t * info,int argc,char ** argv)85 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
86                                            const qemu_info_t *info,
87                                            int argc, char **argv)
88 {
89 
90     for (int i = 0; i < argc; i++) {
91         char *opt = argv[i];
92         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
93 
94         if (g_strcmp0(tokens[0], "haddr") == 0) {
95             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_haddr)) {
96                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
97                 return -1;
98             }
99         } else if (g_strcmp0(tokens[0], "track") == 0) {
100             if (g_strcmp0(tokens[1], "r") == 0) {
101                 rw = QEMU_PLUGIN_MEM_R;
102             } else if (g_strcmp0(tokens[1], "w") == 0) {
103                 rw = QEMU_PLUGIN_MEM_W;
104             } else if (g_strcmp0(tokens[1], "rw") == 0) {
105                 rw = QEMU_PLUGIN_MEM_RW;
106             } else {
107                 fprintf(stderr, "invalid value for argument track: %s\n", opt);
108                 return -1;
109             }
110         } else if (g_strcmp0(tokens[0], "inline") == 0) {
111             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) {
112                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
113                 return -1;
114             }
115         } else if (g_strcmp0(tokens[0], "callback") == 0) {
116             if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_callback)) {
117                 fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
118                 return -1;
119             }
120         } else {
121             fprintf(stderr, "option parsing failed: %s\n", opt);
122             return -1;
123         }
124     }
125 
126     if (do_inline && do_callback) {
127         fprintf(stderr,
128                 "can't enable inline and callback counting at the same time\n");
129         return -1;
130     }
131 
132     counts = qemu_plugin_scoreboard_new(sizeof(CPUCount));
133     mem_count = qemu_plugin_scoreboard_u64_in_struct(
134         counts, CPUCount, mem_count);
135     io_count = qemu_plugin_scoreboard_u64_in_struct(counts, CPUCount, io_count);
136     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
137     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
138     return 0;
139 }
140