]> git.proxmox.com Git - mirror_qemu.git/blame - tests/plugin/mem.c
.travis.yml: don't run make check with multiple jobs
[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
17static uint64_t mem_count;
18static uint64_t io_count;
19static bool do_inline;
20static bool do_haddr;
21static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
22
23static void plugin_exit(qemu_plugin_id_t id, void *p)
24{
25 g_autoptr(GString) out = g_string_new("");
26
27 g_string_printf(out, "mem accesses: %" PRIu64 "\n", mem_count);
28 if (do_haddr) {
29 g_string_append_printf(out, "io accesses: %" PRIu64 "\n", mem_count);
30 }
31 qemu_plugin_outs(out->str);
32}
33
34static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
35 uint64_t vaddr, void *udata)
36{
37 if (do_haddr) {
38 struct qemu_plugin_hwaddr *hwaddr;
39 hwaddr = qemu_plugin_get_hwaddr(meminfo, vaddr);
40 if (qemu_plugin_hwaddr_is_io(hwaddr)) {
41 io_count++;
42 } else {
43 mem_count++;
44 }
45 } else {
46 mem_count++;
47 }
48}
49
50static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
51{
52 size_t n = qemu_plugin_tb_n_insns(tb);
53 size_t i;
54
55 for (i = 0; i < n; i++) {
56 struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
57
58 if (do_inline) {
59 qemu_plugin_register_vcpu_mem_inline(insn, rw,
60 QEMU_PLUGIN_INLINE_ADD_U64,
61 &mem_count, 1);
62 } else {
63 qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem,
64 QEMU_PLUGIN_CB_NO_REGS,
65 rw, NULL);
66 }
67 }
68}
69
70QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
71 const qemu_info_t *info,
72 int argc, char **argv)
73{
74 if (argc) {
75 if (argc >= 3) {
76 if (!strcmp(argv[2], "haddr")) {
77 do_haddr = true;
78 }
79 }
80 if (argc >= 2) {
81 const char *str = argv[1];
82
83 if (!strcmp(str, "r")) {
84 rw = QEMU_PLUGIN_MEM_R;
85 } else if (!strcmp(str, "w")) {
86 rw = QEMU_PLUGIN_MEM_W;
87 }
88 }
89 if (!strcmp(argv[0], "inline")) {
90 do_inline = true;
91 }
92 }
93
94 qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
95 qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
96 return 0;
97}