TCG plugin libinso.so: Virtual address range count
cover TCG plugin libinso.so: Virtual address range count
From: Steffen Hirschmann <[email protected]>
I found it useful for the TCG plugin insn.c to be able to count only instructions in a certain virtual address range (e.g. single leaf function). It could be of interest to a broader audience, thus I am RFCing it here.
Steffen Hirschmann (1):
TCG insn.c: Implement counting specific addresses
docs/about/emulation.rst | 2 + tests/tcg/plugins/insn.c | 89 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+)
Signature · 1 line
2.39.5
RFC PATCH 1/1 TCG insn.c: Implement counting specific addresses
From: Steffen Hirschmann <[email protected]>
This commit implements counting of executed instruction within certain virtual address ranges via libinsn.so
--- docs/about/emulation.rst | 2 + tests/tcg/plugins/insn.c | 89 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+)
docs/about/emulation.rst +2 -0
@@ -336,6 +336,8 @@ Behaviour can be tweaked with the following arguments: - Give a summary of the instruction sizes for the execution * - match=<string> - Only instrument instructions matching the string prefix + * - vaddr=<start>+<count> + - Counts executed instructions in this virtual address range. <start> and <count> must be in base 16 The ``match`` option will show some basic stats including how many instructions have executed since the last execution. For
tests/tcg/plugins/insn.c +89 -0
131 lines not on this page · raw message
Signature · 1 line
2.39.5
Re: [PATCH RFC 1/1] TCG insn.c: Implement counting specific addresses
Quoted 4 lines · [email protected] writes:
From: Steffen Hirschmann <[email protected]>
This commit implements counting of executed instruction within certain virtual address ranges via libinsn.so
This seems reasonable. Do you have any specific use cases where this information is useful?
Quoted 75 lines
Signed-off-by Steffen Hirschmann <[email protected]>--- docs/about/emulation.rst | 2 + tests/tcg/plugins/insn.c | 89 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+)docs/about/emulation.rst +2
-0@@ -336,6 +336,8 @@ Behaviour can be tweaked with the following arguments: - Give a summary of the instruction sizes for the execution * - match=<string> - Only instrument instructions matching the string prefix + * - vaddr=<start>+<count> + - Counts executed instructions in this virtual address range. <start> and <count> must be in base 16 The ``match`` option will show some basic stats including how many instructions have executed since the last execution. Fortests/tcg/plugins/insn.c +30
-0@@ -48,6 +48,14 @@ static GHashTable *match_insn_records;static GMutex match_hash_lock; +typedef struct { +uint64_t start; +uint64_t end; +qemu_plugin_u64 hits;/* Number of insn executed in this range */ +}VaddrRange ; + +static GArray *vaddr_ranges; +static Instruction *get_insn_record (const char *disas,uint64_t vaddr,Match *m) { g_autofree char *str_hash = g_strdup_printf("%"PRIx64" %s", vaddr, disas);@@ -187,6 +195,15 @@ static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) }g_free (insn_disas); } + +for (int j =0 ; j < vaddr_ranges->len; j++) { +VaddrRange *var = &g_array_index (vaddr_ranges, VaddrRange, j); +uint64_t vaddr =qemu_plugin_insn_vaddr (insn); +if (var->start <= vaddr && vaddr < var->end) { +qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu ( + insn,QEMU_PLUGIN_INLINE_ADD_U64 , var->hits,1 ); + } + } } }@@ -246,6 +263,19 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)g_array_free (matches, TRUE);g_array_free (sizes, TRUE); + +for (i =0 ; i < vaddr_ranges->len; ++i) { +VaddrRange *var = &g_array_index (vaddr_ranges, VaddrRange, i); +uint64_t hits =qemu_plugin_u64_sum (var->hits); + +g_string_printf (out,"Vaddr range [0x%08lx, 0x%08lx): %" PRId64" hits\n" , + var->start, var->end, hits); +qemu_plugin_outs (out->str); +qemu_plugin_scoreboard_free (var->hits.score); + } + + +g_array_free (vaddr_ranges, TRUE); }
tests/tcg/plugins/insn.c static void parse_match(char *match) · line 293
Quoted 6 lines
tests/tcg/plugins/insn.c +3
-0@@ -258,6 +288,48 @@ static void parse_match(char *match)g_array_append_val (matches, new_match); } +static void parse_vaddr (const char *arg) +{ +char *vaddr =g_strdup (arg);
We can make this:
g_autofree char *vaddr = g_strdup(arg);
to save the g_free() later.
tests/tcg/plugins/insn.c static void parse_match(char *match) · line 301
Quoted 8 lines
tests/tcg/plugins/insn.c +8
-0+char *saveptr, *token1, *token2; +uint64_t start, len, end; + token1 =strtok_r (vaddr,"+" , &saveptr); + token2 =strtok_r (NULL ,"+" , &saveptr); +if (!token1 || !token2) { +fprintf (stderr,"vaddr usage: vaddr=start+count (both base 16!)\n" ); +exit (1 ); + }
rather than manually messing about with strtok_r how about:
uint64_t start = 0, len = 0;
g_auto(GStrv) tokens = g_strsplit(vaddr, "+", 2);
if (tokens[0] && tokens[2]) {
start = g_ascii_strtoull(tokens[0], NULL, 16);
len = g_ascii_strtoull(tokens[1], NULL, 16);
}
if ((start == 0 || start == GUINT64_MAX) ||
(len == 0 || len == GUINT64_MAX)) {
fprintf(stderr, "vaddr usage: vaddr=start+count (both base 16!)\n");
return false;
}Alternatively we already export qemu_plugin_bool_parse() so maybe we could consider re-factoring qemu_set_dfilter_ranges() to extract the parser and make it available as a helper?
tests/tcg/plugins/insn.c static void parse_match(char *match) · line 307
Quoted 6 lines
tests/tcg/plugins/insn.c +6
-0+ + start =g_ascii_strtoull (token1,NULL ,16 ); + len =g_ascii_strtoull (token2,NULL ,16 ); + end = start + len; + +g_free (vaddr);
see above
tests/tcg/plugins/insn.c static void parse_match(char *match) · line 311
Quoted 4 lines
tests/tcg/plugins/insn.c +4
-0+ +if (start ==0 || end ==0 ) { +fprintf (stderr,"vaddr usage: vaddr=start+count (both base 16!)\n" ); +exit (1 );
exit is rude for a plugin, we should fall back to QEMU by returning -1
tests/tcg/plugins/insn.c static void parse_match(char *match) · line 323
Quoted 12 lines
tests/tcg/plugins/insn.c +12
-0+ } + +if (UINT64_MAX - start < len) { +fprintf (stderr,"integer overflow in vaddr end address calculation." +" Specified vaddr=start+count incorrect.\n" ); +exit (1 ); + } + +g_autoptr (GString) out =g_string_new (NULL ); +g_string_printf (out,"Registering new Vaddr range" +" start=0x%08lx len=0x%08lx end=0x%08lx\n" , start, len, end); +qemu_plugin_outs (out->str);
Keep variable declarations at the top of a block, that said do we need to echo here?
Quoted 49 lines
tests/tcg/plugins/insn.c +26
-0+ +VaddrRange new_vaddrrange = { + .start = start, + .end = end, + .hits =qemu_plugin_scoreboard_u64 ( +qemu_plugin_scoreboard_new (sizeof (uint64_t ))) }; +g_array_append_val (vaddr_ranges, new_vaddrrange); +} + QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,const qemu_info_t *info,int argc,char **argv)@@ -265,6 +337,7 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, matches =g_array_new (false, true,sizeof (Match));/* null terminated so 0 is not a special case */ sizes =g_array_new (true, true,sizeof (unsigned long )); + vaddr_ranges =g_array_new (false, true,sizeof (VaddrRange));for (int i =0 ; i < argc; i++) {char *opt = argv[i];@@ -281,6 +354,8 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, } }else if (g_strcmp0 (tokens[0 ],"match" ) ==0 ) {parse_match (tokens[1 ]); + }else if (g_strcmp0 (tokens[0 ],"vaddr" ) ==0 ) { +parse_vaddr (tokens[1 ]); }else if (g_strcmp0 (tokens[0 ],"trace" ) ==0 ) {if (!qemu_plugin_bool_parse (tokens[0 ], tokens[1 ], &do_trace)) {fprintf (stderr,"boolean argument parsing failed: %s\n" , opt);@@ -292,6 +367,20 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, } } +/* Check for invalid parameter combinations */ +if (vaddr_ranges->len >0 ) { +if (matches->len >0 ) { +fprintf (stderr,"match=... and vaddr=... are incompatible." +" Use only one of them.\n" ); +return -1 ; + } + +if (!do_inline) { +fprintf (stderr,"vaddr=... is only supported in conjunction with inline.\n" ); +return -1 ; + } + } + insn_count =qemu_plugin_scoreboard_u64 (qemu_plugin_scoreboard_new (sizeof (uint64_t )));
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [PATCH RFC 1/1] TCG insn.c: Implement counting specific addresses
Sorry, my first message seems to not have made it out...
Thanks for the review, I posted a v2 patch to the list.
Quoted 2 lines · Alex Bennée
This seems reasonable. Do you have any specific use cases where this information is useful?
In an embedded application with Tensorflow-lite micro that uses CMSIS-NN as backend.
I optimized some compute kernels in the backend (think of matrix multiplication) for a Cortex-M core. Since these compute kernels are leaf functions (every helper is properly inlined), I used this patch to count the number of executed instructions within the kernels.
Greetings,
Steffen
RFC PATCH v2 1/1 TCG insn.c: Implement counting specific addresses
In reply to [email protected]From: Steffen Hirschmann <[email protected]>
This commit implements counting of executed instruction within certain virtual address ranges via libinsn.so
--- docs/about/emulation.rst | 2 + tests/tcg/plugins/insn.c | 91 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+)
docs/about/emulation.rst +2 -0
@@ -336,6 +336,8 @@ Behaviour can be tweaked with the following arguments: - Give a summary of the instruction sizes for the execution * - match=<string> - Only instrument instructions matching the string prefix + * - vaddr=<start>+<count> + - Counts executed instructions in this virtual address range. <start> and <count> must be in base 16 The ``match`` option will show some basic stats including how many instructions have executed since the last execution. For
tests/tcg/plugins/insn.c +91 -0
133 lines not on this page · raw message
Signature · 1 line
2.47.2