Skip to contentSkip to conversation

perf annotate: Fixes & Introduce --source-only and new source code view

PATCH New Taeung Song 1 participant 8 messages Mar 9, 2017 perf events

lore
Share & tools
lore
#0 lore raw

v3 cover perf annotate: Fixes & Introduce --source-only and new source code view

Hi,

Currently perf-annotate have several problems, limitaions and needs regaring line numbers and source code view.

- Wrong line numbers on perf-annotate (both stdio and TUI)
- Wrong sum of overhead(percent) matching source lines
- Limitaions because of a dependence of 'objdump -S'
- A need of source code only view with overhead for new view point for performance

So I'll handle them by this patchset.

First of all, fix several bugs regarding perf-annotate.
And Introduce --source-only option that show source code with overhead and the new source code view for TUI.

I think --source-only and the new source code view
can provide a new view point for performance on source code level (and I think the view is a precheck before asm level and more readble than asm+src mixed view.)

I'd appreciate it, if you give some feedback to me.

Thanks,
Taeung

v3:
- fix mistakes about a leak or missing handing exception of v2 (Namhyung)
- fix several bugs about perf-annotate
- keep "mixed" annotation and add new --source-only option
- remove hide_src_code config for TUI

v2:
- contains the new source code view (Namhyung)

P.S.
I'm making patches for 'fold/unfold parts of asm per a line' on the new source code view. If you agree the new source code view, I keep going to do!

Taeung Song (7):
perf annotate: Use build-id dir when reading link name perf annotate: Avoid division by zero when calculating percent perf annotate: Fix missing setting nr samples on source_line perf annotate: More exactly grep -v in symbol__disassemble() perf annotate: Get correct line numbers matched with addr perf annotate: Introduce --source-only option
perf annotate: Support the new source code view for TUI

 tools/perf/Documentation/perfconfig.example |   1 -
 tools/perf/builtin-annotate.c               |   2 +
 tools/perf/ui/browsers/annotate.c           | 238 ++++++++++++++------
 tools/perf/util/annotate.c                  | 328 +++++++++++++++++++++++++++-
 tools/perf/util/annotate.h                  |  31 ++-
 tools/perf/util/symbol.c                    |   1 +
 tools/perf/util/symbol.h                    |   1 +
 7 files changed, 518 insertions(+), 84 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v3 1/7 perf annotate: Use build-id dir when reading link name

In dso__disassemble_filename() when reading link name from a build-id file, it is failed each time because a build-id file gotten from dso__build_id_filename() is not symbolic link.

So use build-id directory path instead of a build-id file name.

For example, if build-id file name gotten from
dso__build_id_filename() is as below,

/root/.debug/.build-id/4f/75c7d197c951659d1c1b8b5fd49bcdf8f3f8b1/elf

instead of the above build-id file name,
use the build-id dir path that is a symbolic link as below.

/root/.debug/.build-id/4f/75c7d197c951659d1c1b8b5fd49bcdf8f3f8b1

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/util/annotate.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
tools/perf/util/annotate.c +9 -1
index 273f21f..fc91c6b 100644
@@ -1307,6 +1307,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 {
 	char linkname[PATH_MAX];
 	char *build_id_filename;
+	char *build_id_path = NULL;
 
 	if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
 	    !dso__is_kcore(dso))
@@ -1322,8 +1323,14 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 		goto fallback;
 	}
 
+	build_id_path = strdup(filename);
+	if (!build_id_path)
+		return -1;
+
+	dirname(build_id_path);
+
 	if (dso__is_kcore(dso) ||
-	    readlink(filename, linkname, sizeof(linkname)) < 0 ||
+	    readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
 	    strstr(linkname, DSO__NAME_KALLSYMS) ||
 	    access(filename, R_OK)) {
 fallback:
@@ -1335,6 +1342,7 @@ static int dso__disassemble_filename(struct dso *dso, char *filename, size_t fil
 		__symbol__join_symfs(filename, filename_size, dso->long_name);
 	}
 
+	free(build_id_path);
 	return 0;
 }
 
Signature · 1 line
2.7.4
#2 lore raw

PATCH v3 2/7 perf annotate: Avoid division by zero when calculating percent

In reply to Taeung Song

Currently perf-annotate with --print-line can print -nan(0x8000000000000) because of division by zero when calculating percent.

So if a sum of samples is zero, skip calculating percent.

Before:

    $ perf annotate --stdio -l

Sorted summary for file /home/taeung/workspace/a.out
----------------------------------------------

   32.89    -nan    7.04 a.c:38
   25.14    -nan    0.00 a.c:34
   16.26    -nan   56.34 a.c:31
   15.88    -nan    1.41 a.c:37
    5.67    -nan    0.00 a.c:39
    1.13    -nan   35.21 a.c:26
    0.95    -nan    0.00 a.c:44
    0.57    -nan    0.00 a.c:32
 Percent                 |      Source code & Disassembly of a.out for cycles (529 samples)
-----------------------------------------------------------------------------------------
                         :

...

 a.c:26    0.57    -nan    4.23 :         40081a:       mov    %edi,-0x24(%rbp)
 a.c:26    0.00    -nan    9.86 :         40081d:       mov    %rsi,-0x30(%rbp)

...

After:

    $ perf annotate --stdio -l

Sorted summary for file /home/taeung/workspace/a.out
----------------------------------------------

   32.89    0.00    7.04 a.c:38
   25.14    0.00    0.00 a.c:34
   16.26    0.00   56.34 a.c:31
   15.88    0.00    1.41 a.c:37
    5.67    0.00    0.00 a.c:39
    1.13    0.00   35.21 a.c:26
    0.95    0.00    0.00 a.c:44
    0.57    0.00    0.00 a.c:32
 Percent                 |      Source code & Disassembly of old for cycles (529 samples)
-----------------------------------------------------------------------------------------
                         :

...

a.c:26    0.57    0.00    4.23 :         40081a:       mov    %edi,-0x24(%rbp)
a.c:26    0.00    0.00    9.86 :         40081d:       mov    %rsi,-0x30(%rbp)

...

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/util/annotate.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/perf/util/annotate.c +7 -3
index fc91c6b..9bb43cd 100644
@@ -1665,11 +1665,15 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 		src_line->nr_pcnt = nr_pcnt;
 
 		for (k = 0; k < nr_pcnt; k++) {
+			double percent = 0.0;
+
 			h = annotation__histogram(notes, evidx + k);
-			src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
+			if (h->sum)
+				percent = 100.0 * h->addr[i] / h->sum;
 
-			if (src_line->samples[k].percent > percent_max)
-				percent_max = src_line->samples[k].percent;
+			if (percent > percent_max)
+				percent_max = percent;
+			src_line->samples[k].percent = percent;
 		}
 
 		if (percent_max <= 0.5)
Signature · 1 line
2.7.4
#3 lore raw

PATCH v3 3/7 perf annotate: Fix missing setting nr samples on source_line

In reply to Taeung Song

If using --show-total-period with -l, disasm__calc_percent() use a 'samples' array of source_line. But samples[evidx].nr is always zero so print 0 values as below.

Before:

    $ perf annotate --stdio -l --show-total-period
...
       0 :        400816:       push   %rbp
       0 :        400817:       mov    %rsp,%rbp
       0 :        40081a:       mov    %edi,-0x24(%rbp)
       0 :        40081d:       mov    %rsi,-0x30(%rbp)
       0 :        400821:       mov    -0x24(%rbp),%eax
       0 :        400824:       mov    -0x30(%rbp),%rdx
       0 :        400828:       mov    (%rdx),%esi
       0 :        40082a:       mov    $0x0,%edx
...

The reason is that in symbol__get_source_line() did't set number of samples into a memeber variable 'nr' of samples of source_line.

So fix it for correct number of samples
when using --show-total-period option as below.

After:

    $ perf annotate --stdio -l --show-total-period
...
       3 :        400816:       push   %rbp
       4 :        400817:       mov    %rsp,%rbp
       0 :        40081a:       mov    %edi,-0x24(%rbp)
       0 :        40081d:       mov    %rsi,-0x30(%rbp)
       1 :        400821:       mov    -0x24(%rbp),%eax
       2 :        400824:       mov    -0x30(%rbp),%rdx
       0 :        400828:       mov    (%rdx),%esi
       1 :        40082a:       mov    $0x0,%edx
...
Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/util/annotate.c | 6 ++++--
 tools/perf/util/annotate.h | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
tools/perf/util/annotate.c +4 -2
index 9bb43cd..63130ec 100644
@@ -1659,7 +1659,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++) {
-		u64 offset;
+		u64 offset, nr_samples;
 		double percent_max = 0.0;
 
 		src_line->nr_pcnt = nr_pcnt;
@@ -1668,12 +1668,14 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 			double percent = 0.0;
 
 			h = annotation__histogram(notes, evidx + k);
+			nr_samples = h->addr[i];
 			if (h->sum)
-				percent = 100.0 * h->addr[i] / h->sum;
+				percent = 100.0 * nr_samples / h->sum;
 
 			if (percent > percent_max)
 				percent_max = percent;
 			src_line->samples[k].percent = percent;
+			src_line->samples[k].nr = nr_samples;
 		}
 
 		if (percent_max <= 0.5)
tools/perf/util/annotate.h +1 -1
index 09776b5..948aa8e 100644
@@ -98,7 +98,7 @@ struct cyc_hist {
 struct source_line_samples {
 	double		percent;
 	double		percent_sum;
-	double          nr;
+	u64		nr;
 };
 
 struct source_line {
Signature · 1 line
2.7.4
#4 lore raw

PATCH v3 4/7 perf annotate: More exactly grep -v in symbol__disassemble()

In reply to Taeung Song

To remove the first line containing file name and file format as below,

    $ objdump -l -d -S -C /home/taeung/a.out | head -2
    /home/taeung/a.out:     file format elf64-x86-64

currently perf-annotate use objdump with grep -v "file name". But it cause a side effect eliminating filename:linenr of output of 'objdump -l' so fix it.

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/util/annotate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/annotate.c +1 -1
index 63130ec..e49eb7e 100644
@@ -1443,7 +1443,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_na
 	snprintf(command, sizeof(command),
 		 "%s %s%s --start-address=0x%016" PRIx64
 		 " --stop-address=0x%016" PRIx64
-		 " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
+		 " -l -d %s %s -C %s 2>/dev/null|grep -v %s: |expand",
 		 objdump_path ? objdump_path : "objdump",
 		 disassembler_style ? "-M " : "",
 		 disassembler_style ? disassembler_style : "",
Signature · 1 line
2.7.4
#5 lore raw

PATCH v3 5/7 perf annotate: Get correct line numbers matched with addr

In reply to Taeung Song

Currently perf-annotate show wrong line numbers.

For example,
Actual source code is as below

...
21 };
22
23 unsigned int limited_wgt;
24
25 unsigned int get_cond_maxprice(int wgt) 26 {
...

However, the output of perf-annotate is as below.

4 Disassembly of section .text:

  6   0000000000400966 <get_cond_maxprice>:
  7   get_cond_maxprice():
  26  };

28 unsigned int limited_wgt;

  30  unsigned int get_cond_maxprice(int wgt)
  31  {

So remove the wrong way counting line numbers and match correct line numbers to each addr of asm lines

Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/util/annotate.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/perf/util/annotate.c +1 -2
index e49eb7e..a50d949 100644
@@ -823,11 +823,11 @@ static struct disasm_line *disasm_line__new(s64 offset, char *line,
 	if (dl != NULL) {
 		dl->offset = offset;
 		dl->line = strdup(line);
-		dl->line_nr = line_nr;
 		if (dl->line == NULL)
 			goto out_delete;
 
 		if (offset != -1) {
+			dl->line_nr = line_nr;
 			if (disasm_line__parse(dl->line, &dl->ins.name, &dl->ops.raw) < 0)
 				goto out_free_line;
 
@@ -1207,7 +1207,6 @@ static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
 
 	dl = disasm_line__new(offset, parsed_line, privsize, *line_nr, arch, map);
 	free(line);
-	(*line_nr)++;
 
 	if (dl == NULL)
 		return -1;
Signature · 1 line
2.7.4
#6 lore raw

PATCH v3 6/7 perf annotate: Introduce --source-only option

In reply to Taeung Song

Currently there is an asm view that can show
parts of source code lines with --source option.

But I think it is nice to add the new view with --source-only into perf-annotate.
The option can show acutal source code per symbol(function) with overhead using new struct source_code.

I think this view can be more readable than previous source code view and be the new view point for performance for a precheck before assembly code level.

For example, if target symbol is 'hex2u64' of util/util.c

Before:

    $ perf annotate --stdio -s hex2u64
 Percent |      Source code & Disassembly of perf for cycles:ppp (42 samples)
-----------------------------------------------------------------------------
...
         :      int hex2u64(const char *ptr, u64 *long_val)
         :      {
    0.00 :        53feb3:       push   %rbp
    2.38 :        53feb4:       mov    %rsp,%rbp
    0.00 :        53feb7:       sub    $0x30,%rsp
    0.00 :        53febb:       callq  4248b0 <pthread_attr_setdetachstate@plt+0x10>
    0.00 :        53fec0:       mov    %rdi,-0x28(%rbp)
    0.00 :        53fec4:       mov    %rsi,-0x30(%rbp)
    0.00 :        53fec8:       mov    %fs:0x28,%rax
    0.00 :        53fed1:       mov    %rax,-0x8(%rbp)
    0.00 :        53fed5:       xor    %eax,%eax
         :              const char *p = ptr;
    2.38 :        53fed7:       mov    -0x28(%rbp),%rax
    0.00 :        53fedb:       mov    %rax,-0x10(%rbp)
...

After:

    $ perf annotate --source-only --stdio -s hex2u64
 Percent |      Source code of util.c for cycles:ppp (42 samples)
-----------------------------------------------------------------
    0.00 : 354   * While we find nice hex chars, build a long_val.
    0.00 : 355   * Return number of chars processed.
    0.00 : 356   */
    0.00 : 357  int hex2u64(const char *ptr, u64 *long_val)
    2.38 : 358  {
    2.38 : 359          const char *p = ptr;
    0.00 : 360          *long_val = 0;
    0.00 : 361
   30.95 : 362          while (*p) {
   23.81 : 363                  const int hex_val = hex(*p);
    0.00 : 364
   14.29 : 365                  if (hex_val < 0)
    0.00 : 366                          break;
    0.00 : 367
   26.19 : 368                  *long_val = (*long_val << 4) | hex_val;
    0.00 : 369                  p++;
    0.00 : 370          }
    0.00 : 371
    0.00 : 372          return p - ptr;
    0.00 : 373  }
Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/builtin-annotate.c     |   2 +
 tools/perf/ui/browsers/annotate.c |   5 -
 tools/perf/util/annotate.c        | 299 +++++++++++++++++++++++++++++++++++++-
 tools/perf/util/annotate.h        |  22 +++
 tools/perf/util/symbol.c          |   1 +
 tools/perf/util/symbol.h          |   1 +
 6 files changed, 321 insertions(+), 9 deletions(-)
tools/perf/builtin-annotate.c +2 -0
index 4f52d85..93365e1 100644
@@ -432,6 +432,8 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __maybe_unused)
 		     symbol__config_symfs),
 	OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
 		    "Interleave source code with assembly code (default)"),
+	OPT_BOOLEAN(0, "source-only", &symbol_conf.annotate_src_only,
+		    "Display source code for each symbol"),
 	OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
 		    "Display raw encoding of assembly instructions (default)"),
 	OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
tools/perf/ui/browsers/annotate.c +0 -5
index ba36aac..03b2012 100644
@@ -11,11 +11,6 @@
 #include "../../util/config.h"
 #include <pthread.h>
 
-struct disasm_line_samples {
-	double		percent;
-	u64		nr;
-};
-
 #define IPC_WIDTH 6
 #define CYCLES_WIDTH 6
 
tools/perf/util/annotate.c +295 -4

352 lines not on this page · raw message

tools/perf/util/annotate.h +22 -0
index 948aa8e..dd7ddae 100644
@@ -56,6 +56,11 @@ int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *
 
 struct annotation;
 
+struct disasm_line_samples {
+	double		percent;
+	u64		nr;
+};
+
 struct disasm_line {
 	struct list_head    node;
 	s64		    offset;
@@ -95,6 +100,21 @@ struct cyc_hist {
 	u16	reset;
 };
 
+struct code_line {
+	struct list_head    node;
+	int		    line_nr;
+	char		    *line;
+	int		    nr_matched_dl;
+	struct disasm_line  **matched_dl_arr;
+	struct disasm_line_samples *samples_sum;
+};
+
+struct source_code {
+	char		 *path;
+	int		 nr_events;
+	struct list_head lines;
+};
+
 struct source_line_samples {
 	double		percent;
 	double		percent_sum;
@@ -123,7 +143,9 @@ struct source_line {
  */
 struct annotated_source {
 	struct list_head   source;
+	struct source_code *code;
 	struct source_line *lines;
+	bool   		   has_src_code;
 	int    		   nr_histograms;
 	size_t		   sizeof_sym_hist;
 	struct cyc_hist	   *cycles_hist;
tools/perf/util/symbol.c +1 -0
index 70e389b..71228bd 100644
@@ -35,6 +35,7 @@ struct symbol_conf symbol_conf = {
 	.use_modules		= true,
 	.try_vmlinux_path	= true,
 	.annotate_src		= true,
+	.annotate_src_only	= false,
 	.demangle		= true,
 	.demangle_kernel	= false,
 	.cumulate_callchain	= true,
tools/perf/util/symbol.h +1 -0
index 6c358b7..190be1d 100644
@@ -108,6 +108,7 @@ struct symbol_conf {
 			kptr_restrict,
 			annotate_asm_raw,
 			annotate_src,
+			annotate_src_only,
 			event_group,
 			demangle,
 			demangle_kernel,
Signature · 1 line
2.7.4
#7 lore raw

PATCH v3 7/7 perf annotate: Support the new source code view for TUI

In reply to Taeung Song

To make source code view more readable,
add the new source code view for TUI
instead of confusing mixed source code & dissambly view.

This view can show source code per symbol(function). and it can be toggled by a 's' key.
And support 'k' and 't' key for line numbers and total period.

For example, if target symbol is 'hex2u64' of util/util.c

Before:

       |    Disassembly of section .text:
       |
       |    000000000053ef9e <hex2u64>:
       |    hex2u64():
       |    /*
       |     * While we find nice hex chars, build a long_val.
       |     * Return number of chars processed.
       |     */
       |    int hex2u64(const char *ptr, u64 *long_val)
       |    {
       |      push   %rbp
  1.79 |      mov    %rsp,%rbp
       |      sub    $0x30,%rsp

After:

       |     * While we find nice hex chars, build a long_val.
       |     * Return number of chars processed.
       |     */
       |    int hex2u64(const char *ptr, u64 *long_val)
  1.79 |    {
       |        const char *p = ptr;
       |        *long_val = 0;
       |
 28.57 |        while (*p) {
 30.36 |                const int hex_val = hex(*p);
       |
 12.50 |                if (hex_val < 0)
       |                        break;
       |
 23.21 |                *long_val = (*long_val << 4) | hex_val;
       |                p++;
       |        }
       |
  1.79 |        return p - ptr;
Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/Documentation/perfconfig.example |   1 -
 tools/perf/ui/browsers/annotate.c           | 233 ++++++++++++++++++++--------
 tools/perf/util/annotate.c                  |  10 +-
 tools/perf/util/annotate.h                  |   7 +
 4 files changed, 180 insertions(+), 71 deletions(-)
tools/perf/Documentation/perfconfig.example +0 -1
index 2b477c1..c9fc41b 100644
@@ -23,7 +23,6 @@
 [annotate]
 
 	# Defaults
-	hide_src_code = false
 	use_offset = true
 	jump_arrows = true
 	show_nr_jumps = false
tools/perf/ui/browsers/annotate.c +168 -65

335 lines not on this page · raw message

tools/perf/util/annotate.c +5 -5
index 7d1c7cc..d8a3a50 100644
@@ -1396,7 +1396,7 @@ static void code_lines__free(struct list_head *code_lines)
 	}
 }
 
-static int symbol__free_source_code(struct symbol *sym)
+int symbol__free_source_code(struct symbol *sym)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	struct source_code *code = notes->src->code;
@@ -1410,8 +1410,8 @@ static int symbol__free_source_code(struct symbol *sym)
 	return 0;
 }
 
-static void code_line__sum_samples(struct code_line *cl, struct disasm_line *dl,
-				   struct annotation *notes, struct perf_evsel *evsel)
+void code_line__sum_samples(struct code_line *cl, struct disasm_line *dl,
+			    struct annotation *notes, struct perf_evsel *evsel)
 {
 	int i;
 	u64 nr_samples;
@@ -1569,8 +1569,8 @@ static int source_code__collect(struct source_code *code,
 	return ret;
 }
 
-static int symbol__get_source_code(struct symbol *sym, struct map *map,
-				   struct perf_evsel *evsel)
+int symbol__get_source_code(struct symbol *sym, struct map *map,
+			    struct perf_evsel *evsel)
 {
 	struct annotation *notes = symbol__annotation(sym);
 	struct source_code *code;
tools/perf/util/annotate.h +7 -0
index dd7ddae..e125eca 100644
@@ -109,6 +109,9 @@ struct code_line {
 	struct disasm_line_samples *samples_sum;
 };
 
+void code_line__sum_samples(struct code_line *cl, struct disasm_line *dl,
+			    struct annotation *notes, struct perf_evsel *evsel);
+
 struct source_code {
 	char		 *path;
 	int		 nr_events;
@@ -180,6 +183,10 @@ 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__free_source_code(struct symbol *sym);
+int symbol__get_source_code(struct symbol *sym, struct map *map,
+			    struct perf_evsel *evsel);
+
 int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_name, size_t privsize);
 
 enum symbol_disassemble_errno {
Signature · 1 line
2.7.4