| 1 | /* |
| 2 | * Copyright (C) 2019, Alex Bennée <alex.bennee@linaro.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 <inttypes.h> |
| 11 | #include <string.h> |
| 12 | #include <unistd.h> |
| 13 | #include <stdio.h> |
| 14 | #include <glib.h> |
| 15 | |
| 16 | #include <qemu-plugin.h> |
| 17 | |
| 18 | QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION; |
| 19 | |
| 20 | static bool do_inline; |
| 21 | |
| 22 | /* Plugins need to take care of their own locking */ |
| 23 | static GMutex lock; |
| 24 | static GHashTable *hotblocks; |
| 25 | static guint64 limit = 20; |
| 26 | |
| 27 | /* |
| 28 | * Counting Structure |
| 29 | * |
| 30 | * The internals of the TCG are not exposed to plugins so we can only |
| 31 | * get the starting PC for each block. We cheat this slightly by |
| 32 | * checking the number of instructions as well to help |
| 33 | * differentiate. |
| 34 | */ |
| 35 | typedef struct { |
| 36 | uint64_t start_addr; |
| 37 | struct qemu_plugin_scoreboard *exec_count; |
| 38 | int trans_count; |
| 39 | unsigned long insns; |
| 40 | } ExecCount; |
| 41 | |
| 42 | static gint cmp_exec_count(gconstpointer a, gconstpointer b, gpointer d) |
| 43 | { |
| 44 | ExecCount *ea = (ExecCount *) a; |
| 45 | ExecCount *eb = (ExecCount *) b; |
| 46 | uint64_t count_a = |
| 47 | qemu_plugin_u64_sum(qemu_plugin_scoreboard_u64(ea->exec_count)); |
| 48 | uint64_t count_b = |
| 49 | qemu_plugin_u64_sum(qemu_plugin_scoreboard_u64(eb->exec_count)); |
| 50 | return count_a > count_b ? -1 : 1; |
| 51 | } |
| 52 | |
| 53 | static guint exec_count_hash(gconstpointer v) |
| 54 | { |
| 55 | const ExecCount *e = v; |
| 56 | return e->start_addr ^ e->insns; |
| 57 | } |
| 58 | |
| 59 | static gboolean exec_count_equal(gconstpointer v1, gconstpointer v2) |
| 60 | { |
| 61 | const ExecCount *ea = v1; |
| 62 | const ExecCount *eb = v2; |
| 63 | return (ea->start_addr == eb->start_addr) && |
| 64 | (ea->insns == eb->insns); |
| 65 | } |
| 66 | |
| 67 | static void exec_count_free(gpointer key, gpointer value, gpointer user_data) |
| 68 | { |
| 69 | ExecCount *cnt = value; |
| 70 | qemu_plugin_scoreboard_free(cnt->exec_count); |
| 71 | } |
| 72 | |
| 73 | static void plugin_exit(void *p) |
| 74 | { |
| 75 | g_autoptr(GString) report = g_string_new("collected "); |
| 76 | GList *counts, *sorted_counts, *it; |
| 77 | int i; |
| 78 | |
| 79 | g_string_append_printf(report, "%d entries in the hash table\n", |
| 80 | g_hash_table_size(hotblocks)); |
| 81 | counts = g_hash_table_get_values(hotblocks); |
| 82 | sorted_counts = g_list_sort_with_data(counts, cmp_exec_count, NULL); |
| 83 | |
| 84 | if (sorted_counts) { |
| 85 | g_string_append_printf(report, "pc, tcount, icount, ecount\n"); |
| 86 | |
| 87 | for (i = 0, it = sorted_counts; (limit == 0 || i < limit) && it; |
| 88 | i++, it = it->next) { |
| 89 | ExecCount *rec = (ExecCount *) it->data; |
| 90 | g_string_append_printf( |
| 91 | report, "0x%016"PRIx64", %d, %ld, %"PRIu64"\n", |
| 92 | rec->start_addr, rec->trans_count, |
| 93 | rec->insns, |
| 94 | qemu_plugin_u64_sum( |
| 95 | qemu_plugin_scoreboard_u64(rec->exec_count))); |
| 96 | } |
| 97 | |
| 98 | g_list_free(sorted_counts); |
| 99 | } |
| 100 | |
| 101 | qemu_plugin_outs(report->str); |
| 102 | |
| 103 | g_hash_table_foreach(hotblocks, exec_count_free, NULL); |
| 104 | g_hash_table_destroy(hotblocks); |
| 105 | } |
| 106 | |
| 107 | static void plugin_init(void) |
| 108 | { |
| 109 | hotblocks = g_hash_table_new(exec_count_hash, exec_count_equal); |
| 110 | } |
| 111 | |
| 112 | static void vcpu_tb_exec(unsigned int cpu_index, void *udata) |
| 113 | { |
| 114 | ExecCount *cnt = (ExecCount *)udata; |
| 115 | qemu_plugin_u64_add(qemu_plugin_scoreboard_u64(cnt->exec_count), |
| 116 | cpu_index, 1); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * When do_inline we ask the plugin to increment the counter for us. |
| 121 | * Otherwise a helper is inserted which calls the vcpu_tb_exec |
| 122 | * callback. |
| 123 | */ |
| 124 | static void vcpu_tb_trans(struct qemu_plugin_tb *tb, void *userdata) |
| 125 | { |
| 126 | ExecCount *cnt; |
| 127 | uint64_t pc = qemu_plugin_tb_vaddr(tb); |
| 128 | size_t insns = qemu_plugin_tb_n_insns(tb); |
| 129 | |
| 130 | g_mutex_lock(&lock); |
| 131 | { |
| 132 | ExecCount e; |
| 133 | e.start_addr = pc; |
| 134 | e.insns = insns; |
| 135 | cnt = (ExecCount *) g_hash_table_lookup(hotblocks, &e); |
| 136 | } |
| 137 | |
| 138 | if (cnt) { |
| 139 | cnt->trans_count++; |
| 140 | } else { |
| 141 | cnt = g_new0(ExecCount, 1); |
| 142 | cnt->start_addr = pc; |
| 143 | cnt->trans_count = 1; |
| 144 | cnt->insns = insns; |
| 145 | cnt->exec_count = qemu_plugin_scoreboard_new(sizeof(uint64_t)); |
| 146 | g_hash_table_insert(hotblocks, cnt, cnt); |
| 147 | } |
| 148 | |
| 149 | g_mutex_unlock(&lock); |
| 150 | |
| 151 | if (do_inline) { |
| 152 | qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( |
| 153 | tb, QEMU_PLUGIN_INLINE_ADD_U64, |
| 154 | qemu_plugin_scoreboard_u64(cnt->exec_count), 1); |
| 155 | } else { |
| 156 | qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec, |
| 157 | QEMU_PLUGIN_CB_NO_REGS, |
| 158 | (void *)cnt); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | QEMU_PLUGIN_EXPORT |
| 163 | int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, |
| 164 | int argc, char **argv) |
| 165 | { |
| 166 | for (int i = 0; i < argc; i++) { |
| 167 | char *opt = argv[i]; |
| 168 | g_auto(GStrv) tokens = g_strsplit(opt, "=", 2); |
| 169 | if (g_strcmp0(tokens[0], "inline") == 0) { |
| 170 | if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &do_inline)) { |
| 171 | fprintf(stderr, "boolean argument parsing failed: %s\n", opt); |
| 172 | return -1; |
| 173 | } |
| 174 | } else if (g_strcmp0(tokens[0], "limit") == 0) { |
| 175 | char *endptr = NULL; |
| 176 | limit = g_ascii_strtoull(tokens[1], &endptr, 10); |
| 177 | if (endptr == tokens[1] || *endptr != '\0') { |
| 178 | fprintf(stderr, "unsigned integer parsing failed: %s\n", opt); |
| 179 | return -1; |
| 180 | } |
| 181 | } else { |
| 182 | fprintf(stderr, "option parsing failed: %s\n", opt); |
| 183 | return -1; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | plugin_init(); |
| 188 | |
| 189 | qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans, NULL); |
| 190 | qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); |
| 191 | return 0; |
| 192 | } |