]> git.proxmox.com Git - mirror_qemu.git/blame - tests/plugin/mem.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / tests / plugin / mem.c
CommitLineData
671f760b
EC
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
3fb356cc
AB
17QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
18
4f8d8860
PB
19typedef struct {
20 uint64_t mem_count;
21 uint64_t io_count;
22} CPUCount;
23
24static struct qemu_plugin_scoreboard *counts;
25static qemu_plugin_u64 mem_count;
26static qemu_plugin_u64 io_count;
0eca92e2 27static bool do_inline, do_callback;
671f760b
EC
28static bool do_haddr;
29static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
30
31static void plugin_exit(qemu_plugin_id_t id, void *p)
32{
33 g_autoptr(GString) out = g_string_new("");
34
4f8d8860
PB
35 if (do_inline || do_callback) {
36 g_string_printf(out, "mem accesses: %" PRIu64 "\n",
37 qemu_plugin_u64_sum(mem_count));
0eca92e2 38 }
671f760b 39 if (do_haddr) {
4f8d8860
PB
40 g_string_append_printf(out, "io accesses: %" PRIu64 "\n",
41 qemu_plugin_u64_sum(io_count));
671f760b
EC
42 }
43 qemu_plugin_outs(out->str);
4f8d8860 44 qemu_plugin_scoreboard_free(counts);
671f760b
EC
45}
46
47static 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)) {
4f8d8860 54 qemu_plugin_u64_add(io_count, cpu_index, 1);
671f760b 55 } else {
4f8d8860 56 qemu_plugin_u64_add(mem_count, cpu_index, 1);
671f760b
EC
57 }
58 } else {
4f8d8860 59 qemu_plugin_u64_add(mem_count, cpu_index, 1);
671f760b
EC
60 }
61}
62
63static 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) {
4f8d8860
PB
72 qemu_plugin_register_vcpu_mem_inline_per_vcpu(
73 insn, rw,
74 QEMU_PLUGIN_INLINE_ADD_U64,
75 mem_count, 1);
0eca92e2
AB
76 }
77 if (do_callback) {
671f760b
EC
78 qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
79 QEMU_PLUGIN_CB_NO_REGS,
80 rw, NULL);
81 }
82 }
83}
84
85QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
86 const qemu_info_t *info,
87 int argc, char **argv)
88{
671f760b 89
5ae589fa
MM
90 for (int i = 0; i < argc; i++) {
91 char *opt = argv[i];
40258741 92 g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
5ae589fa
MM
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) {
671f760b 101 rw = QEMU_PLUGIN_MEM_R;
5ae589fa 102 } else if (g_strcmp0(tokens[1], "w") == 0) {
671f760b 103 rw = QEMU_PLUGIN_MEM_W;
5ae589fa
MM
104 } else if (g_strcmp0(tokens[1], "rw") == 0) {
105 rw = QEMU_PLUGIN_MEM_RW;
106 } else {
96420a30 107 fprintf(stderr, "invalid value for argument track: %s\n", opt);
5ae589fa
MM
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;
671f760b 119 }
0eca92e2 120 } else {
5ae589fa
MM
121 fprintf(stderr, "option parsing failed: %s\n", opt);
122 return -1;
671f760b
EC
123 }
124 }
125
4f8d8860
PB
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);
671f760b
EC
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}