perf tools: Enhance and correct srcline behavior
Namhyung Kim
maintainercover perf tools: Enhance and correct srcline behavior
Hello,
This patchset tries to fix and enhance current srcline behavior.
Firstly it doesn't actually sort by srcline info but by ip. I suspect it was because of a performance reason to run external addr2line utility. It showed the srcline info after hist entries were collapsed. Thanks to Roberto, we now have internal implementation of addr2line using libbfd so can sort/compare by srcline of entries.
Second problem for me was it sometimes printed "??:0" and sometimes printed a raw ip value for unknown srcline info. I changed to print the former consistently.
While at it, I found some bugs/leaks in srcline handling. Patch 1-3 are fixes for those and can be merged separately.
You can get this series on my 'perf/srcline-v1' branch in my tree at:
git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git
Any comments are welcome, thanks.
Namhyung
Namhyung Kim (8):
perf sort: Fix a memory leak on srcline
perf annotate: Reuse path from the result of addr2line perf hists: Free srcline when freeing hist_entry perf tools: Factor out get/free_srcline()
perf tools: Do not try to call addr2line for non-binary files perf tools: Pass dso instead of dso_name to get_srcline() perf tools: Save failed result of get_srcline() perf tools: Fix srcline sort key behavior
Roberto Vitillo (1):
perf tools: Implement addr2line directly using libbfd
tools/perf/Makefile | 1 + tools/perf/config/Makefile | 4 + tools/perf/util/annotate.c | 33 +----- tools/perf/util/dso.c | 1 + tools/perf/util/dso.h | 2 + tools/perf/util/hist.c | 1 + tools/perf/util/sort.c | 58 ++++------ tools/perf/util/srcline.c | 265 +++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/util.h | 8 ++ 9 files changed, 308 insertions(+), 65 deletions(-) create mode 100644 tools/perf/util/srcline.c
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 1/9 perf sort: Fix a memory leak on srcline
From: Namhyung Kim <[email protected]>
In the hist_entry__srcline_snprintf(), path and self->srcline are pointing the same memory region, but they are doubly allocated.
--- tools/perf/util/sort.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
tools/perf/util/sort.c +1 -4
@@ -269,10 +269,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,if (!fp) goto out_ip; -if (getline (&path, &line_len, fp) <0 || !line_len) - goto out_ip; - self->srcline =strdup (path); -if (self->srcline ==NULL ) +if (getline (&self->srcline, &line_len, fp) <0 || !line_len) goto out_ip; nl =strchr (self->srcline,'\n' );
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 2/9 perf annotate: Reuse path from the result of addr2line
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
In the symbol__get_source_line(), path and src_line->path will have same value, but they were allocated separately, and leaks one. Just share path to src_line->path.
--- tools/perf/util/annotate.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
tools/perf/util/annotate.c +1 -5
@@ -1091,11 +1091,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,if (getline (&path, &line_len, fp) <0 || !line_len) goto next_close; - src_line->path =malloc (sizeof (char ) * line_len +1 ); -if (!src_line->path) - goto next_close; - -strcpy (src_line->path, path); + src_line->path = path;insert_source_line (&tmp_root, src_line); next_close:
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 3/9 perf hists: Free srcline when freeing hist_entry
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
We've been leaked srcline of hist_entry, it should be freed also.
--- tools/perf/util/hist.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/util/hist.c +1 -0
@@ -530,6 +530,7 @@ void hist_entry__free(struct hist_entry *he) {free (he->branch_info);free (he->mem_info); +free (he->srcline);free (he); }
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 4/9 perf tools: Factor out get/free_srcline()
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
Currently external addr2line tool is used for srcline sort key and annotate with srcline info. Separate the common code to prepare upcoming enhancements.
--- tools/perf/Makefile | 1 + tools/perf/util/annotate.c | 20 ++--------- tools/perf/util/hist.c | 2 +- tools/perf/util/sort.c | 17 ++-------- tools/perf/util/srcline.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/util.h | 6 ++++ 6 files changed, 97 insertions(+), 32 deletions(-) create mode 100644 tools/perf/util/srcline.c
tools/perf/Makefile +1 -0
@@ -360,6 +360,7 @@ LIB_OBJS += $(OUTPUT)util/rblist.o LIB_OBJS += $(OUTPUT)util/intlist.o LIB_OBJS += $(OUTPUT)util/vdso.o LIB_OBJS += $(OUTPUT)util/stat.o +LIB_OBJS += $(OUTPUT)util/srcline.o LIB_OBJS += $(OUTPUT)ui/setup.o LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/annotate.c +3 -17
@@ -1018,7 +1018,7 @@ static void symbol__free_source_line(struct symbol *sym, int len) (sizeof (src_line->p) * (src_line->nr_pcnt -1 ));for (i =0 ; i < len; i++) { -free (src_line->path); +free_srcline (src_line->path); src_line = (void *)src_line + sizeof_src_line; }
@@ -1035,7 +1035,6 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,u64 start;int i, k;int evidx = evsel->idx; -char cmd[PATH_MAX *2 ];struct source_line *src_line;struct annotation *notes =symbol__annotation (sym);struct sym_hist *h =annotation__histogram (notes, evidx);
@@ -1063,10 +1062,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map, start =map__rip_2objdump (map, sym->start);for (i =0 ; i < len; i++) { -char *path =NULL ; -size_t line_len;u64 offset; -FILE *fp;double percent_max =0.0 ; src_line->nr_pcnt = nr_pcnt;
@@ -1083,19 +1079,9 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map, goto next; offset = start + i; -sprintf (cmd,"addr2line -e %s %016" PRIx64, filename, offset); - fp =popen (cmd,"r" ); -if (!fp) - goto next; - -if (getline (&path, &line_len, fp) <0 || !line_len) - goto next_close; - - src_line->path = path; + src_line->path =get_srcline (filename, offset);insert_source_line (&tmp_root, src_line); - next_close: -pclose (fp); next: src_line = (void *)src_line + sizeof_src_line; }
@@ -1136,7 +1122,7 @@ static void print_summary(struct rb_root *root, const char *filename) path = src_line->path; color =get_percent_color (percent_max); -color_fprintf (stdout, color," %s" , path); +color_fprintf (stdout, color," %s\n" , path); node =rb_next (node); }
tools/perf/util/hist.c +1 -1
@@ -530,7 +530,7 @@ void hist_entry__free(struct hist_entry *he) {free (he->branch_info);free (he->mem_info); -free (he->srcline); +free_srcline (he->srcline);free (he); }
tools/perf/util/sort.c +3 -14
@@ -251,8 +251,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf, unsigned int width __maybe_unused) {FILE *fp =NULL ; -char cmd[PATH_MAX +2 ], *path = self->srcline, *nl; -size_t line_len; +char *path = self->srcline;if (path !=NULL ) goto out_path;
@@ -263,19 +262,9 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,if (!strncmp (self->ms.map->dso->long_name,"/tmp/perf-" ,10 )) goto out_ip; -snprintf (cmd,sizeof (cmd),"addr2line -e %s %016" PRIx64, - self->ms.map->dso->long_name, self->ip); - fp =popen (cmd,"r" ); -if (!fp) - goto out_ip; - -if (getline (&self->srcline, &line_len, fp) <0 || !line_len) - goto out_ip; + path =get_srcline (self->ms.map->dso->long_name, self->ip); + self->srcline = path; - nl =strchr (self->srcline,'\n' ); -if (nl !=NULL ) - *nl ='\0' ; - path = self->srcline; out_path:if (fp)pclose (fp);
tools/perf/util/srcline.c new file +83 -0
83 lines not on this page · raw message
tools/perf/util/util.h +6 -0
@@ -279,4 +279,10 @@ void dump_stack(void);extern unsigned int page_size;void get_term_dimensions (struct winsize *ws); + +#define SRCLINE_UNKNOWN ((char *) "??:0") + +char *get_srcline (const char *dso_name,unsigned long addr); +void free_srcline (char *srcline); +#endif /* GIT_COMPAT_UTIL_H */
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 5/9 perf tools: Do not try to call addr2line for non-binary files
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
No need to call addr2line since they don't have such information.
--- tools/perf/util/sort.c | 3 --- tools/perf/util/srcline.c | 11 +++++++++-- 2 files changed, 9 insertions(+), 5 deletions(-)
tools/perf/util/sort.c +0 -3
@@ -259,9 +259,6 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,if (!self->ms.map) goto out_ip; -if (!strncmp (self->ms.map->dso->long_name,"/tmp/perf-" ,10 )) - goto out_ip; - path =get_srcline (self->ms.map->dso->long_name, self->ip); self->srcline = path;
tools/perf/util/srcline.c +9 -2
@@ -57,11 +57,17 @@ char *get_srcline(const char *dso_name, unsigned long addr) {char *file;unsigned line; -char *srcline; +char *srcline =SRCLINE_UNKNOWN ;size_t size; +if (dso_name[0 ] =='[' ) + goto out; + +if (!strncmp (dso_name,"/tmp/perf-" ,10 )) + goto out; +if (!addr2line (dso_name, addr, &file, &line)) -return SRCLINE_UNKNOWN ; + goto out;/* just calculate actual length */ size =snprintf (NULL ,0 ,"%s:%u" , file, line) +1 ;
@@ -73,6 +79,7 @@ char *get_srcline(const char *dso_name, unsigned long addr) srcline =SRCLINE_UNKNOWN ;free (file); +out:return srcline; }
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 6/9 perf tools: Pass dso instead of dso_name to get_srcline()
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
This is a preparation of next change. No functional changes are intended.
--- tools/perf/util/annotate.c | 11 ++++------- tools/perf/util/dso.h | 1 + tools/perf/util/sort.c | 2 +- tools/perf/util/srcline.c | 4 +++- tools/perf/util/util.h | 4 +++- 5 files changed, 12 insertions(+), 10 deletions(-)
tools/perf/util/annotate.c +4 -7
@@ -1029,8 +1029,7 @@ static void symbol__free_source_line(struct symbol *sym, int len)/* Get the filename:line for the colored entries */ static int symbol__get_source_line (struct symbol *sym,struct map *map,struct perf_evsel *evsel, -struct rb_root *root,int len, -const char *filename) +struct rb_root *root,int len) {u64 start;int i, k;
@@ -1079,7 +1078,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map, goto next; offset = start + i; - src_line->path =get_srcline (filename, offset); + src_line->path =get_srcline (map->dso, offset);insert_source_line (&tmp_root, src_line); next:
@@ -1286,7 +1285,6 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map, bool full_paths, int min_pcnt, int max_lines) { struct dso *dso = map->dso; -const char *filename = dso->long_name;struct rb_root source_line =RB_ROOT ;u64 len;
@@ -1296,9 +1294,8 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map, len =symbol__size (sym);if (print_lines) { -symbol__get_source_line (sym, map, evsel, &source_line, - len, filename); -print_summary (&source_line, filename); +symbol__get_source_line (sym, map, evsel, &source_line, len); +print_summary (&source_line, dso->long_name); } symbol__annotate_printf(sym, map, evsel, full_paths,
tools/perf/util/dso.h +1 -0
@@ -5,6 +5,7 @@#include <linux/rbtree.h> #include "types.h" #include "map.h" +#include "build-id.h" enum dso_binary_type {DSO_BINARY_TYPE__KALLSYMS =0 ,
tools/perf/util/sort.c +1 -1
@@ -259,7 +259,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,if (!self->ms.map) goto out_ip; - path =get_srcline (self->ms.map->dso->long_name, self->ip); + path =get_srcline (self->ms.map->dso, self->ip); self->srcline = path; out_path:
tools/perf/util/srcline.c +3 -1
@@ -4,6 +4,7 @@#include <linux/kernel.h> +#include "util/dso.h" #include "util/util.h" #include "util/debug.h"
@@ -53,11 +54,12 @@ out:return ret; } -char *get_srcline (const char *dso_name,unsigned long addr) +char *get_srcline (struct dso *dso,unsigned long addr) {char *file;unsigned line;char *srcline =SRCLINE_UNKNOWN ; +char *dso_name = dso->long_name;size_t size;if (dso_name[0 ] =='[' )
tools/perf/util/util.h +3 -1
@@ -282,7 +282,9 @@ void get_term_dimensions(struct winsize *ws);#define SRCLINE_UNKNOWN ((char *) "??:0") -char *get_srcline (const char *dso_name,unsigned long addr); +struct dso ; + +char *get_srcline (struct dso *dso,unsigned long addr);void free_srcline (char *srcline);#endif /* GIT_COMPAT_UTIL_H */
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 7/9 perf tools: Save failed result of get_srcline()
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
Some dso's lack srcline info, so there's no point to keep trying on them. Just save failture status and skip them.
--- tools/perf/util/dso.c | 1 + tools/perf/util/dso.h | 1 + tools/perf/util/srcline.c | 10 ++++++++-- 3 files changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/dso.c +1 -0
@@ -421,6 +421,7 @@ struct dso *dso__new(const char *name) dso->loaded =0 ; dso->sorted_by_name =0 ; dso->has_build_id =0 ; + dso->has_srcline =1 ; dso->kernel =DSO_TYPE_USER ; dso->needs_swap =DSO_SWAP__UNSET ;INIT_LIST_HEAD (&dso->node);
tools/perf/util/dso.h +1 -0
@@ -79,6 +79,7 @@ struct dso {enum dso_binary_type data_type; u8 adjust_symbols:1; u8 has_build_id:1; + u8 has_srcline:1; u8 hit:1; u8 annotate_warned:1; u8 sname_alloc:1;
tools/perf/util/srcline.c +8 -2
@@ -58,10 +58,13 @@ char *get_srcline(struct dso *dso, unsigned long addr) {char *file;unsigned line; -char *srcline =SRCLINE_UNKNOWN ; +char *srcline;char *dso_name = dso->long_name;size_t size; +if (!dso->has_srcline) +return SRCLINE_UNKNOWN ; +if (dso_name[0 ] =='[' ) goto out;
@@ -81,8 +84,11 @@ char *get_srcline(struct dso *dso, unsigned long addr) srcline =SRCLINE_UNKNOWN ;free (file); -out:return srcline; + +out: + dso->has_srcline =0 ; +return SRCLINE_UNKNOWN ; }void free_srcline (char *srcline)
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 8/9 perf tools: Implement addr2line directly using libbfd
In reply to Namhyung KimFrom: Roberto Vitillo <[email protected]>
When the srcline sort key is used , the external addr2line utility needs to be run for each hist entry to get the srcline info. This can consume quite a time if one has a huge perf.data file.
So rather than executing the external utility, implement it internally and just call it. We can do it since we've linked with libbfd already.
--- tools/perf/config/Makefile | 4 ++ tools/perf/util/srcline.c | 167 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+)
tools/perf/config/Makefile +4 -0
@@ -396,6 +396,10 @@ else endif endif +ifndef ($(filter -lbfd,$(EXTLIBS)),) + CFLAGS += -DLIBBFD_SUPPORT +endif + ifndef NO_STRLCPY ifeq ($(call try-cc,$(SOURCE_STRLCPY),,-DHAVE_STRLCPY),y) CFLAGS += -DHAVE_STRLCPY
tools/perf/util/srcline.c +167 -0
179 lines not on this page · raw message
Signature · 1 line
1.7.11.7
Namhyung Kim
maintainerPATCH 9/9 perf tools: Fix srcline sort key behavior
In reply to Namhyung KimFrom: Namhyung Kim <[email protected]>
Currently the srcline sort key compares ip rather than srcline info. I guess this was due to a performance reason to run external addr2line utility. Now we have implemented the functionality inside, use the srcline info when comparing hist entries.
Also constantly print "??:0" string for unknown srcline rather than printing ip.
--- tools/perf/util/sort.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-)
tools/perf/util/sort.c +20 -21
@@ -243,33 +243,32 @@ struct sort_entry sort_sym = {static int64_t sort__srcline_cmp (struct hist_entry *left,struct hist_entry *right) { -return (int64_t )(right->ip - left->ip); +if (!left->srcline) { +if (!left->ms.map) + left->srcline =SRCLINE_UNKNOWN ; +else { +struct map *map = left->ms.map; + left->srcline =get_srcline (map->dso, +map__rip_2objdump (map, left->ip)); + } + } +if (!right->srcline) { +if (!right->ms.map) + right->srcline =SRCLINE_UNKNOWN ; +else { +struct map *map = right->ms.map; + right->srcline =get_srcline (map->dso, +map__rip_2objdump (map, right->ip)); + } + } +return strcmp (left->srcline, right->srcline); }static int hist_entry__srcline_snprintf (struct hist_entry *self,char *bf,size_t size,unsigned int width__maybe_unused ) { -FILE *fp =NULL ; -char *path = self->srcline; - -if (path !=NULL ) - goto out_path; - -if (!self->ms.map) - goto out_ip; - - path =get_srcline (self->ms.map->dso, self->ip); - self->srcline = path; - -out_path: -if (fp) -pclose (fp); -return repsep_snprintf (bf, size,"%s" , path); -out_ip: -if (fp) -pclose (fp); -return repsep_snprintf (bf, size,"%-#*llx" ,BITS_PER_LONG /4 , self->ip); +return repsep_snprintf (bf, size,"%s" , self->srcline); }struct sort_entry sort_srcline = {
Signature · 1 line
1.7.11.7
Peter Zijlstra
maintainerQuoted 9 lines · Namhyung Kim
Hello,
This patchset tries to fix and enhance current srcline behavior.
Firstly it doesn't actually sort by srcline info but by ip. I suspect it was because of a performance reason to run external addr2line utility. It showed the srcline info after hist entries were collapsed. Thanks to Roberto, we now have internal implementation of addr2line using libbfd so can sort/compare by srcline of entries.
Sweet..
Quoted 35 lines · Namhyung Kim
Hello,
This patchset tries to fix and enhance current srcline behavior.
Firstly it doesn't actually sort by srcline info but by ip. I suspect it was because of a performance reason to run external addr2line utility. It showed the srcline info after hist entries were collapsed. Thanks to Roberto, we now have internal implementation of addr2line using libbfd so can sort/compare by srcline of entries.
Second problem for me was it sometimes printed "??:0" and sometimes printed a raw ip value for unknown srcline info. I changed to print the former consistently.
While at it, I found some bugs/leaks in srcline handling. Patch 1-3 are fixes for those and can be merged separately.
You can get this series on my 'perf/srcline-v1' branch in my tree at:
git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.gitAny comments are welcome, thanks.
NamhyungNamhyung Kim (8):
perf sort: Fix a memory leak on srcline
perf annotate: Reuse path from the result of addr2line perf hists: Free srcline when freeinghist_entryperf tools: Factor out get/free_srcline()
perf tools: Do not try to call addr2line for non-binary files perf tools: Pass dso instead ofdso_nametoget_srcline()perf tools: Save failed result ofget_srcline()perf tools: Fix srcline sort key behavior
Quoted 3 lines
Roberto Vitillo (1):
perf tools: Implement addr2line directly using libbfd
nice..