hwprofile.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2020, Alex Bennée <alex.bennee@linaro.org>
  3. *
  4. * HW Profile - breakdown access patterns for IO to devices
  5. *
  6. * License: GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include <inttypes.h>
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <inttypes.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <glib.h>
  17. #include <qemu-plugin.h>
  18. QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
  19. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  20. typedef struct {
  21. uint64_t cpu_read;
  22. uint64_t cpu_write;
  23. uint64_t reads;
  24. uint64_t writes;
  25. } IOCounts;
  26. typedef struct {
  27. uint64_t off_or_pc;
  28. IOCounts counts;
  29. } IOLocationCounts;
  30. typedef struct {
  31. const char *name;
  32. uint64_t base;
  33. IOCounts totals;
  34. GHashTable *detail;
  35. } DeviceCounts;
  36. static GMutex lock;
  37. static GHashTable *devices;
  38. static struct qemu_plugin_scoreboard *source_pc_scoreboard;
  39. static qemu_plugin_u64 source_pc;
  40. /* track the access pattern to a piece of HW */
  41. static bool pattern;
  42. /* track the source address of access to HW */
  43. static bool source;
  44. /* track only matched regions of HW */
  45. static bool check_match;
  46. static gchar **matches;
  47. static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW;
  48. static inline bool track_reads(void)
  49. {
  50. return rw == QEMU_PLUGIN_MEM_RW || rw == QEMU_PLUGIN_MEM_R;
  51. }
  52. static inline bool track_writes(void)
  53. {
  54. return rw == QEMU_PLUGIN_MEM_RW || rw == QEMU_PLUGIN_MEM_W;
  55. }
  56. static void plugin_init(void)
  57. {
  58. devices = g_hash_table_new(NULL, NULL);
  59. }
  60. static gint sort_cmp(gconstpointer a, gconstpointer b)
  61. {
  62. DeviceCounts *ea = (DeviceCounts *) a;
  63. DeviceCounts *eb = (DeviceCounts *) b;
  64. return ea->totals.reads + ea->totals.writes >
  65. eb->totals.reads + eb->totals.writes ? -1 : 1;
  66. }
  67. static gint sort_loc(gconstpointer a, gconstpointer b)
  68. {
  69. IOLocationCounts *ea = (IOLocationCounts *) a;
  70. IOLocationCounts *eb = (IOLocationCounts *) b;
  71. return ea->off_or_pc > eb->off_or_pc;
  72. }
  73. static void fmt_iocount_record(GString *s, IOCounts *rec)
  74. {
  75. if (track_reads()) {
  76. g_string_append_printf(s, ", %"PRIx64", %"PRId64,
  77. rec->cpu_read, rec->reads);
  78. }
  79. if (track_writes()) {
  80. g_string_append_printf(s, ", %"PRIx64", %"PRId64,
  81. rec->cpu_write, rec->writes);
  82. }
  83. }
  84. static void fmt_dev_record(GString *s, DeviceCounts *rec)
  85. {
  86. g_string_append_printf(s, "%s, 0x%"PRIx64,
  87. rec->name, rec->base);
  88. fmt_iocount_record(s, &rec->totals);
  89. g_string_append_c(s, '\n');
  90. }
  91. static void plugin_exit(qemu_plugin_id_t id, void *p)
  92. {
  93. g_autoptr(GString) report = g_string_new("");
  94. GList *counts;
  95. if (!(pattern || source)) {
  96. g_string_printf(report, "Device, Address");
  97. if (track_reads()) {
  98. g_string_append_printf(report, ", RCPUs, Reads");
  99. }
  100. if (track_writes()) {
  101. g_string_append_printf(report, ", WCPUs, Writes");
  102. }
  103. g_string_append_c(report, '\n');
  104. }
  105. counts = g_hash_table_get_values(devices);
  106. if (counts && g_list_next(counts)) {
  107. GList *it;
  108. it = g_list_sort(counts, sort_cmp);
  109. while (it) {
  110. DeviceCounts *rec = (DeviceCounts *) it->data;
  111. if (rec->detail) {
  112. GList *accesses = g_hash_table_get_values(rec->detail);
  113. GList *io_it = g_list_sort(accesses, sort_loc);
  114. const char *prefix = pattern ? "off" : "pc";
  115. g_string_append_printf(report, "%s @ 0x%"PRIx64"\n",
  116. rec->name, rec->base);
  117. while (io_it) {
  118. IOLocationCounts *loc = (IOLocationCounts *) io_it->data;
  119. g_string_append_printf(report, " %s:%08"PRIx64,
  120. prefix, loc->off_or_pc);
  121. fmt_iocount_record(report, &loc->counts);
  122. g_string_append_c(report, '\n');
  123. io_it = io_it->next;
  124. }
  125. } else {
  126. fmt_dev_record(report, rec);
  127. }
  128. it = it->next;
  129. };
  130. g_list_free(it);
  131. }
  132. qemu_plugin_outs(report->str);
  133. }
  134. static DeviceCounts *new_count(const char *name, uint64_t base)
  135. {
  136. DeviceCounts *count = g_new0(DeviceCounts, 1);
  137. count->name = name;
  138. count->base = base;
  139. if (pattern || source) {
  140. count->detail = g_hash_table_new(g_int64_hash, g_int64_equal);
  141. }
  142. g_hash_table_insert(devices, (gpointer) name, count);
  143. return count;
  144. }
  145. static IOLocationCounts *new_location(GHashTable *table, uint64_t off_or_pc)
  146. {
  147. IOLocationCounts *loc = g_new0(IOLocationCounts, 1);
  148. loc->off_or_pc = off_or_pc;
  149. g_hash_table_insert(table, &loc->off_or_pc, loc);
  150. return loc;
  151. }
  152. static void hwprofile_match_hit(DeviceCounts *rec, uint64_t off)
  153. {
  154. g_autoptr(GString) report = g_string_new("hwprofile: match @ offset");
  155. g_string_append_printf(report, "%"PRIx64", previous hits\n", off);
  156. fmt_dev_record(report, rec);
  157. qemu_plugin_outs(report->str);
  158. }
  159. static void inc_count(IOCounts *count, bool is_write, unsigned int cpu_index)
  160. {
  161. if (is_write) {
  162. count->writes++;
  163. count->cpu_write |= (1 << cpu_index);
  164. } else {
  165. count->reads++;
  166. count->cpu_read |= (1 << cpu_index);
  167. }
  168. }
  169. static void vcpu_haddr(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo,
  170. uint64_t vaddr, void *udata)
  171. {
  172. struct qemu_plugin_hwaddr *hwaddr = qemu_plugin_get_hwaddr(meminfo, vaddr);
  173. if (!hwaddr || !qemu_plugin_hwaddr_is_io(hwaddr)) {
  174. return;
  175. } else {
  176. const char *name = qemu_plugin_hwaddr_device_name(hwaddr);
  177. uint64_t off = qemu_plugin_hwaddr_phys_addr(hwaddr);
  178. bool is_write = qemu_plugin_mem_is_store(meminfo);
  179. DeviceCounts *counts;
  180. g_mutex_lock(&lock);
  181. counts = (DeviceCounts *) g_hash_table_lookup(devices, name);
  182. if (!counts) {
  183. uint64_t base = vaddr - off;
  184. counts = new_count(name, base);
  185. }
  186. if (check_match) {
  187. if (g_strv_contains((const char * const *)matches, counts->name)) {
  188. hwprofile_match_hit(counts, off);
  189. inc_count(&counts->totals, is_write, cpu_index);
  190. }
  191. } else {
  192. inc_count(&counts->totals, is_write, cpu_index);
  193. }
  194. /* either track offsets or source of access */
  195. if (source) {
  196. off = qemu_plugin_u64_get(source_pc, cpu_index);
  197. }
  198. if (pattern || source) {
  199. IOLocationCounts *io_count = g_hash_table_lookup(counts->detail,
  200. &off);
  201. if (!io_count) {
  202. io_count = new_location(counts->detail, off);
  203. }
  204. inc_count(&io_count->counts, is_write, cpu_index);
  205. }
  206. g_mutex_unlock(&lock);
  207. }
  208. }
  209. static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
  210. {
  211. size_t n = qemu_plugin_tb_n_insns(tb);
  212. size_t i;
  213. for (i = 0; i < n; i++) {
  214. struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
  215. if (source) {
  216. uint64_t pc = qemu_plugin_insn_vaddr(insn);
  217. qemu_plugin_register_vcpu_mem_inline_per_vcpu(
  218. insn, rw, QEMU_PLUGIN_INLINE_STORE_U64,
  219. source_pc, pc);
  220. }
  221. qemu_plugin_register_vcpu_mem_cb(insn, vcpu_haddr,
  222. QEMU_PLUGIN_CB_NO_REGS, rw, NULL);
  223. }
  224. }
  225. QEMU_PLUGIN_EXPORT
  226. int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
  227. int argc, char **argv)
  228. {
  229. int i;
  230. g_autoptr(GString) matches_raw = g_string_new("");
  231. for (i = 0; i < argc; i++) {
  232. char *opt = argv[i];
  233. g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
  234. if (g_strcmp0(tokens[0], "track") == 0) {
  235. if (g_strcmp0(tokens[1], "read") == 0) {
  236. rw = QEMU_PLUGIN_MEM_R;
  237. } else if (g_strcmp0(tokens[1], "write") == 0) {
  238. rw = QEMU_PLUGIN_MEM_W;
  239. } else {
  240. fprintf(stderr, "invalid value for track: %s\n", tokens[1]);
  241. return -1;
  242. }
  243. } else if (g_strcmp0(tokens[0], "pattern") == 0) {
  244. if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &pattern)) {
  245. fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
  246. return -1;
  247. }
  248. } else if (g_strcmp0(tokens[0], "source") == 0) {
  249. if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &source)) {
  250. fprintf(stderr, "boolean argument parsing failed: %s\n", opt);
  251. return -1;
  252. }
  253. } else if (g_strcmp0(tokens[0], "match") == 0) {
  254. check_match = true;
  255. g_string_append_printf(matches_raw, "%s,", tokens[1]);
  256. } else {
  257. fprintf(stderr, "option parsing failed: %s\n", opt);
  258. return -1;
  259. }
  260. }
  261. if (check_match) {
  262. matches = g_strsplit(matches_raw->str, ",", -1);
  263. }
  264. if (source && pattern) {
  265. fprintf(stderr, "can only currently track either source or pattern.\n");
  266. return -1;
  267. }
  268. if (!info->system_emulation) {
  269. fprintf(stderr, "hwprofile: plugin only useful for system emulation\n");
  270. return -1;
  271. }
  272. if (source) {
  273. source_pc_scoreboard = qemu_plugin_scoreboard_new(sizeof(uint64_t));
  274. source_pc = qemu_plugin_scoreboard_u64(source_pc_scoreboard);
  275. }
  276. plugin_init();
  277. qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
  278. qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
  279. return 0;
  280. }