perf annotate: Enable cross arch annotate
v4 cover perf annotate: Enable cross arch annotate
Perf can currently only support code navigation (branches and calls) in annotate when run on the same architecture where perf.data was recorded. But cross arch annotate is not supported.
This patchset enables cross arch annotate. Currently I've used x86 and arm instructions which are already available and adding support for powerpc as well. Adding support for other arch will be easy.
I've created this patch on top of acme/perf/core. And tested it with x86 and powerpc only.
Note for arm:
Few instructions were defined under #if __arm__ which I've used as a table for arm. But I'm not sure whether instruction defined outside of that also contains arm instructions. Apart from that, 'call__parse()' and 'move__parse()' contains #ifdef __arm__ directive. I've changed it to if (!strcmp(norm_arch, arm)). I don't have a arm machine to test these changes.
Example:
Record on powerpc:
$ ./perf record -a
Report -> Annotate on x86:
$ ./perf report -i perf.data.powerpc --vmlinux vmlinux.powerpc
Changes in v4:
- powerpc: Added support for branch instructions that includes 'ctr'
- __maybe_unused was misplaced at few location. Corrected it.
- Moved position of v3 last patch that define macro for each arch name
v3 link: https://lkml.org/lkml/2016/6/30/99
Naveen N. Rao (1):
perf annotate: add powerpc support
Ravi Bangoria (2):
perf: Define macro for normalized arch names
perf annotate: Enable cross arch annotate
tools/perf/arch/common.c | 36 ++--- tools/perf/arch/common.h | 11 ++ tools/perf/builtin-top.c | 2 +- tools/perf/ui/browsers/annotate.c | 3 +- tools/perf/ui/gtk/annotate.c | 2 +- tools/perf/util/annotate.c | 273 ++++++++++++++++++++++++++++++------- tools/perf/util/annotate.h | 6 +- tools/perf/util/unwind-libunwind.c | 4 +- 8 files changed, 265 insertions(+), 72 deletions(-)
--
2.5.5
PATCH v4 1/3 perf: Define macro for normalized arch names
Define macro for each normalized arch name and use them instead of using arch name as string
---
Changes in v4:
- Moved position of patch
tools/perf/arch/common.c | 36 ++++++++++++++++++------------------ tools/perf/arch/common.h | 11 +++++++++++ tools/perf/util/unwind-libunwind.c | 4 ++-- 3 files changed, 31 insertions(+), 20 deletions(-)
tools/perf/arch/common.c +18 -18
@@ -122,25 +122,25 @@ static int lookup_triplets(const char *const *triplets, const char *name)const char *normalize_arch (char *arch) {if (!strcmp (arch,"x86_64" )) -return "x86" ; +return NORM_X86 ;if (arch[0 ] =='i' && arch[2 ] =='8' && arch[3 ] =='6' ) -return "x86" ; +return NORM_X86 ;if (!strcmp (arch,"sun4u" ) || !strncmp (arch,"sparc" ,5 )) -return "sparc" ; +return NORM_SPARC ;if (!strcmp (arch,"aarch64" ) || !strcmp (arch,"arm64" )) -return "arm64" ; +return NORM_ARM64 ;if (!strncmp (arch,"arm" ,3 ) || !strcmp (arch,"sa110" )) -return "arm" ; +return NORM_ARM ;if (!strncmp (arch,"s390" ,4 )) -return "s390" ; +return NORM_S390 ;if (!strncmp (arch,"parisc" ,6 )) -return "parisc" ; +return NORM_PARISC ;if (!strncmp (arch,"powerpc" ,7 ) || !strncmp (arch,"ppc" ,3 )) -return "powerpc" ; +return NORM_POWERPC ;if (!strncmp (arch,"mips" ,4 )) -return "mips" ; +return NORM_MIPS ;if (!strncmp (arch,"sh" ,2 ) &&isdigit (arch[2 ])) -return "sh" ; +return NORM_SH ;return arch; }
@@ -180,21 +180,21 @@ static int perf_env__lookup_binutils_path(struct perf_env *env,zfree (&buf); } -if (!strcmp (arch,"arm" )) +if (!strcmp (arch,NORM_ARM )) path_list = arm_triplets; -else if (!strcmp (arch,"arm64" )) +else if (!strcmp (arch,NORM_ARM64 )) path_list = arm64_triplets; -else if (!strcmp (arch,"powerpc" )) +else if (!strcmp (arch,NORM_POWERPC )) path_list = powerpc_triplets; -else if (!strcmp (arch,"sh" )) +else if (!strcmp (arch,NORM_SH )) path_list = sh_triplets; -else if (!strcmp (arch,"s390" )) +else if (!strcmp (arch,NORM_S390 )) path_list = s390_triplets; -else if (!strcmp (arch,"sparc" )) +else if (!strcmp (arch,NORM_SPARC )) path_list = sparc_triplets; -else if (!strcmp (arch,"x86" )) +else if (!strcmp (arch,NORM_X86 )) path_list = x86_triplets; -else if (!strcmp (arch,"mips" )) +else if (!strcmp (arch,NORM_MIPS )) path_list = mips_triplets;else {ui__error ("binutils for %s not supported.\n" , arch);
tools/perf/arch/common.h +11 -0
@@ -5,6 +5,17 @@extern const char *objdump_path; +/* Macro for normalized arch names */ +#define NORM_X86 "x86" +#define NORM_SPARC "sparc" +#define NORM_ARM64 "arm64" +#define NORM_ARM "arm" +#define NORM_S390 "s390" +#define NORM_PARISC "parisc" +#define NORM_POWERPC "powerpc" +#define NORM_MIPS "mips" +#define NORM_SH "sh" +int perf_env__lookup_objdump (struct perf_env *env);const char *normalize_arch (char *arch);
tools/perf/util/unwind-libunwind.c +2 -2
@@ -40,10 +40,10 @@ int unwind__prepare_access(struct thread *thread, struct map *map, arch =normalize_arch (thread->mg->machine->env->arch); -if (!strcmp (arch,"x86" )) { +if (!strcmp (arch,NORM_X86 )) {if (dso_type !=DSO__TYPE_64BIT ) ops = x86_32_unwind_libunwind_ops; - }else if (!strcmp (arch,"arm64" ) || !strcmp (arch,"arm" )) { + }else if (!strcmp (arch,NORM_ARM64 ) || !strcmp (arch,NORM_ARM )) {if (dso_type ==DSO__TYPE_64BIT ) ops = arm64_unwind_libunwind_ops; }
Signature · 1 line
2.5.5
PATCH v4 2/3 perf annotate: Enable cross arch annotate
In reply to Ravi BangoriaChange current data structures and function to enable cross arch annotate.
Current implementation does not contain logic of record on one arch and annotating on other. This remote annotate is partially possible with current implementation for x86 (or may be arm as well) only. But, to make remote annotation work properly, all architecture instruction tables need to be included in the perf binary. And while annotating, look for instruction table where perf.data was recorded.
---
Changes in v4:
- __maybe_unused was misplaced at few location. Corrected it
tools/perf/builtin-top.c | 2 +- tools/perf/ui/browsers/annotate.c | 3 +- tools/perf/ui/gtk/annotate.c | 2 +- tools/perf/util/annotate.c | 134 ++++++++++++++++++++++++-------------- tools/perf/util/annotate.h | 5 +- 5 files changed, 93 insertions(+), 53 deletions(-)
tools/perf/builtin-top.c +1 -1
@@ -128,7 +128,7 @@ static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)return err; } - err =symbol__annotate (sym, map,0 ); + err =symbol__annotate (sym, map,0 ,NULL );if (err ==0 ) { out_assign: top->sym_filter_entry = he;
tools/perf/ui/browsers/annotate.c +2 -1
@@ -1050,7 +1050,8 @@ int symbol__tui_annotate(struct symbol *sym, struct map *map, (nr_pcnt -1 ); } -if (symbol__annotate (sym, map, sizeof_bdl) <0 ) { +if (symbol__annotate (sym, map, sizeof_bdl, +perf_evsel__env_arch (evsel)) <0 ) {ui__error ("%s" , ui_helpline__last_msg); goto out_free_offsets; }
tools/perf/ui/gtk/annotate.c +1 -1
@@ -166,7 +166,7 @@ static int symbol__gtk_annotate(struct symbol *sym, struct map *map,if (map->dso->annotate_warned)return -1 ; -if (symbol__annotate (sym, map,0 ) <0 ) { +if (symbol__annotate (sym, map,0 ,perf_evsel__env_arch (evsel)) <0 ) {ui__error ("%s" , ui_helpline__current);return -1 ; }
tools/perf/util/annotate.c +86 -48
289 lines not on this page · raw message
tools/perf/util/annotate.h +3 -2
@@ -36,7 +36,7 @@ struct ins_operands {struct ins_ops {void (*free)(struct ins_operands *ops); -int (*parse)(struct ins_operands *ops); +int (*parse)(struct ins_operands *ops,const char *norm_arch);int (*scnprintf)(struct ins *ins,char *bf,size_t size,struct ins_operands *ops); };
@@ -155,7 +155,8 @@ int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 addr);int symbol__alloc_hist (struct symbol *sym);void symbol__annotate_zero_histograms (struct symbol *sym); -int symbol__annotate (struct symbol *sym,struct map *map,size_t privsize); +int symbol__annotate (struct symbol *sym,struct map *map,size_t privsize, +char *arch);int symbol__annotate_init (struct map *map,struct symbol *sym); int symbol__annotate_printf(struct symbol *sym, struct map *map,
Signature · 1 line
2.5.5
PATCH v4 3/3 perf annotate: add powerpc support
In reply to Ravi BangoriaFrom: Naveen N. Rao <[email protected]>
Powerpc has long list of branch instructions and hardcoding them in table appears to be error-prone. So, add new function to find instruction instead of creating table. This function dynamically create table(list of 'struct ins'), and instead of creating object every time, first check if list already contain object for that instruction.
---
Chnages in v4:
- Added support for branch instructions that includes 'ctr'
tools/perf/util/annotate.c | 155 +++++++++++++++++++++++++++++++++++++++++++-- tools/perf/util/annotate.h | 3 +- 2 files changed, 150 insertions(+), 8 deletions(-)
tools/perf/util/annotate.c +148 -7
@@ -55,10 +55,15 @@ int ins__scnprintf(struct ins *ins, char *bf, size_t size,return ins__raw_scnprintf (ins, bf, size, ops); } -static int call__parse (struct ins_operands *ops,const char *norm_arch) +static int call__parse (char *ins_name,struct ins_operands *ops, +const char *norm_arch) {char *endptr, *tok, *name; +/* Special case for powerpc */ +if (!strcmp (norm_arch,NORM_POWERPC ) &&strstr (ins_name,"ctr" )) +return 0 ; + ops->target.addr =strtoull (ops->raw, &endptr,16 ); name =strchr (endptr,'<' );
@@ -117,7 +122,7 @@ bool ins__is_call(const struct ins *ins)return ins->ops == &call_ops; } -static int jump__parse (struct ins_operands *ops, +static int jump__parse (char *ins_name__maybe_unused ,struct ins_operands *ops,const char *norm_arch__maybe_unused ) {const char *s =strchr (ops->raw,'+' );
@@ -135,6 +140,13 @@ static int jump__parse(struct ins_operands *ops,static int jump__scnprintf (struct ins *ins,char *bf,size_t size,struct ins_operands *ops) { +/* +* Instructions that does not include target address in operand +* like 'bctr' for powerpc. +*/ +if (!ops->target.addr) +return scnprintf (bf, size,"%-6.6s" , ins->name); +return scnprintf (bf, size,"%-6.6s %" PRIx64, ins->name, ops->target.offset); }
@@ -173,7 +185,8 @@ static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)return 0 ; } -static int lock__parse (struct ins_operands *ops,const char *norm_arch) +static int lock__parse (char *ins_name,struct ins_operands *ops, +const char *norm_arch) {char *name;
@@ -194,7 +207,8 @@ static int lock__parse(struct ins_operands *ops, const char *norm_arch)return 0 ;if (ops->locked.ins->ops->parse && - ops->locked.ins->ops->parse (ops->locked.ops, norm_arch) <0 ) + ops->locked.ins->ops->parse (ins_name, + ops->locked.ops, norm_arch) <0 ) goto out_free_ops;return 0 ;
@@ -237,7 +251,8 @@ static struct ins_ops lock_ops = { .scnprintf = lock__scnprintf, }; -static int mov__parse (struct ins_operands *ops,const char *norm_arch) +static int mov__parse (char *ins_name__maybe_unused ,struct ins_operands *ops, +const char *norm_arch) {char *s =strchr (ops->raw,',' ), *target, *comment, prev;
@@ -304,7 +319,7 @@ static struct ins_ops mov_ops = { .scnprintf = mov__scnprintf, }; -static int dec__parse (struct ins_operands *ops, +static int dec__parse (char *ins_name__maybe_unused ,struct ins_operands *ops,const char *norm_arch__maybe_unused ) {char *target, *comment, *s, prev;
@@ -459,6 +474,11 @@ static struct ins instructions_arm[] = { { .name = "bne", .ops = &jump_ops, }, }; +struct instructions_powerpc { +struct ins *ins; +struct list_head list; +}; +static int ins__key_cmp (const void *name,const void *insp) {const struct ins *ins = insp;
@@ -474,6 +494,125 @@ static int ins__cmp(const void *a, const void *b)return strcmp (ia->name, ib->name); } +static struct ins *list_add__ins_powerpc (struct instructions_powerpc *head, +const char *name,struct ins_ops *ops) +{ +struct instructions_powerpc *ins_powerpc; +struct ins *ins; + + ins =zalloc (sizeof (struct ins )); +if (!ins) +return NULL ; + + ins_powerpc =zalloc (sizeof (struct instructions_powerpc )); +if (!ins_powerpc) + goto out_free_ins; + + ins->name =strdup (name); +if (!ins->name) + goto out_free_ins_power; + + ins->ops = ops; + ins_powerpc->ins = ins; +list_add_tail (&(ins_powerpc->list), &(head->list)); + +return ins; + +out_free_ins_power: +zfree (&ins_powerpc); +out_free_ins: +zfree (&ins); +return NULL ; +} + +static struct ins *list_search__ins_powerpc (struct instructions_powerpc *head, +const char *name) +{ +struct instructions_powerpc *pos; + +list_for_each_entry (pos, &head->list, list) { +if (!strcmp (pos->ins->name, name)) +return pos->ins; + } +return NULL ; +} + +static struct ins *ins__find_powerpc (const char *name) +{ +int i; +struct ins *ins; +struct ins_ops *ops; +static struct instructions_powerpc head; +static bool list_initialized; + +/* +* - Interested only if instruction starts with 'b'. +* - Few start with 'b', but aren't branch instructions. +* - Let's also ignore instructions involving 'tar' since target +* branch addresses for those can't be determined statically. +* (same for instruction involving 'ctr'. But they are very +* common so not filtering them.) +*/ + +if (name[0 ] !='b' || + !strncmp (name,"bcd" ,3 ) || + !strncmp (name,"brinc" ,5 ) || + !strncmp (name,"bper" ,4 ) || +strstr (name,"tar" )) +return NULL ; + +if (!list_initialized) { +INIT_LIST_HEAD (&head.list); + list_initialized = true; + } + +/* +* Return if we already have object of 'struct ins' for this instruction +*/ + ins =list_search__ins_powerpc (&head, name); +if (ins) +return ins; + + ops = &jump_ops; + + i =strlen (name) -1 ; +if (i <0 ) +return NULL ; + +/* ignore optional hints at the end of the instructions */ +if (name[i] =='+' || name[i] =='-' ) + i--; + +if (name[i] =='l' || (name[i] =='a' && name[i-1 ] =='l' )) { +/* +* if the instruction ends up with 'l' or 'la', then +* those are considered 'calls' since they update LR. +* ... except for 'bnl' which is branch if not less than +* and the absolute form of the same. +*/ +if (strcmp (name,"bnl" ) &&strcmp (name,"bnl+" ) && +strcmp (name,"bnl-" ) &&strcmp (name,"bnla" ) && +strcmp (name,"bnla+" ) &&strcmp (name,"bnla-" )) + ops = &call_ops; + } +if (name[i] =='r' && name[i-1 ] =='l' ) +/* +* instructions ending with 'lr' are considered to be +* return instructions +*/ + ops = &ret_ops; + +/* +* Add instruction to list so next time no need to +* allocate memory for it. +*/ + ins =list_add__ins_powerpc (&head, name, ops); +if (ins) +return ins; + +return NULL ; +} +static void ins__sort (struct ins *instructions,int nmemb) {qsort (instructions, nmemb,sizeof (struct ins ), ins__cmp);
@@ -509,6 +648,8 @@ static struct ins *ins__find(const char *name, const char *norm_arch) }else if (!strcmp (norm_arch,NORM_ARM )) { instructions = instructions_arm; nmemb =ARRAY_SIZE (instructions_arm); + }else if (!strcmp (norm_arch,NORM_POWERPC )) { +return ins__find_powerpc (name); }else {pr_err ("perf annotate not supported by %s arch\n" , norm_arch);return NULL ;
@@ -760,7 +901,7 @@ static void disasm_line__init_ins(struct disasm_line *dl, char *arch)return ;if (dl->ins->ops->parse && - dl->ins->ops->parse (&dl->ops, norm_arch) <0 ) + dl->ins->ops->parse (dl->name, &dl->ops, norm_arch) <0 ) dl->ins =NULL ; }
tools/perf/util/annotate.h +2 -1
@@ -36,7 +36,8 @@ struct ins_operands {struct ins_ops {void (*free)(struct ins_operands *ops); -int (*parse)(struct ins_operands *ops,const char *norm_arch); +int (*parse)(char *name,struct ins_operands *ops, +const char *norm_arch);int (*scnprintf)(struct ins *ins,char *bf,size_t size,struct ins_operands *ops); };
Signature · 1 line
2.5.5
Arnaldo, Michael,
I've tested this patchset on ppc64 BE and LE both. Please review this.
-Ravi
Quoted 54 lines · Ravi Bangoria
Perf can currently only support code navigation (branches and calls) in annotate when run on the same architecture where perf.data was recorded. But cross arch annotate is not supported.
This patchset enables cross arch annotate. Currently I've used x86 and arm instructions which are already available and adding support for powerpc as well. Adding support for other arch will be easy.
I've created this patch on top of acme/perf/core. And tested it with x86 and powerpc only.
Note for arm:
Few instructions were defined under #if__arm__which I've used as a table for arm. But I'm not sure whether instruction defined outside of that also contains arm instructions. Apart from that, 'call__parse()' and 'move__parse()' contains #ifdef__arm__directive. I've changed it to if (!strcmp(norm_arch, arm)). I don't have a arm machine to test these changes.Example:
Record on powerpc:
$ ./perf record -aReport -> Annotate on x86: $ ./perf report -i perf.data.powerpc --vmlinux vmlinux.powerpcChanges in v4:
- powerpc: Added support for branch instructions that includes 'ctr'
-__maybe_unusedwas misplaced at few location. Corrected it.
- Moved position of v3 last patch that define macro for each arch namev3 link: https://lkml.org/lkml/2016/6/30/99
Naveen N. Rao (1):
perf annotate: add powerpc supportRavi Bangoria (2):
perf: Define macro for normalized arch names
perf annotate: Enable cross arch annotatetools/perf/arch/common.c | 36 ++--- tools/perf/arch/common.h | 11 ++ tools/perf/builtin-top.c | 2 +- tools/perf/ui/browsers/annotate.c | 3 +- tools/perf/ui/gtk/annotate.c | 2 +- tools/perf/util/annotate.c | 273 ++++++++++++++++++++++++++++++------- tools/perf/util/annotate.h | 6 +- tools/perf/util/unwind-libunwind.c | 4 +- 8 files changed, 265 insertions(+), 72 deletions(-)--
2.5.5
I've sent v5 series for this. Please review it.
Thanks,
Ravi
Quoted 62 lines · Ravi Bangoria
Arnaldo, Michael,
I've tested this patchset on ppc64 BE and LE both. Please review this.
-Ravi
Quoted 54 lines · Ravi Bangoria
Perf can currently only support code navigation (branches and calls) in annotate when run on the same architecture where perf.data was recorded. But cross arch annotate is not supported.
This patchset enables cross arch annotate. Currently I've used x86 and arm instructions which are already available and adding support for powerpc as well. Adding support for other arch will be easy.
I've created this patch on top of acme/perf/core. And tested it with x86 and powerpc only.
Note for arm:
Few instructions were defined under #if__arm__which I've used as a table for arm. But I'm not sure whether instruction defined outside of that also contains arm instructions. Apart from that, 'call__parse()' and 'move__parse()' contains #ifdef__arm__directive. I've changed it to if (!strcmp(norm_arch, arm)). I don't have a arm machine to test these changes.Example:
Record on powerpc:
$ ./perf record -aReport -> Annotate on x86: $ ./perf report -i perf.data.powerpc --vmlinux vmlinux.powerpcChanges in v4:
- powerpc: Added support for branch instructions that includes 'ctr'
-__maybe_unusedwas misplaced at few location. Corrected it.
- Moved position of v3 last patch that define macro for each arch namev3 link: https://lkml.org/lkml/2016/6/30/99
Naveen N. Rao (1):
perf annotate: add powerpc supportRavi Bangoria (2):
perf: Define macro for normalized arch names
perf annotate: Enable cross arch annotatetools/perf/arch/common.c | 36 ++--- tools/perf/arch/common.h | 11 ++ tools/perf/builtin-top.c | 2 +- tools/perf/ui/browsers/annotate.c | 3 +- tools/perf/ui/gtk/annotate.c | 2 +- tools/perf/util/annotate.c | 273 ++++++++++++++++++++++++++++++------- tools/perf/util/annotate.h | 6 +- tools/perf/util/unwind-libunwind.c | 4 +- 8 files changed, 265 insertions(+), 72 deletions(-)Signature · 1 line
2.5.5