From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932829AbdCIRpM (ORCPT ); Thu, 9 Mar 2017 12:45:12 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:33734 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932472AbdCIRpI (ORCPT ); Thu, 9 Mar 2017 12:45:08 -0500 From: Taeung Song To: Arnaldo Carvalho de Melo Cc: linux-kernel@vger.kernel.org, Jiri Olsa , Namhyung Kim , Ingo Molnar , Peter Zijlstra , Wang Nan , Masami Hiramatsu , Taeung Song , Jiri Olsa Subject: [PATCH v3 6/7] perf annotate: Introduce --source-only option Date: Fri, 10 Mar 2017 02:35:40 +0900 Message-Id: <1489080941-3965-7-git-send-email-treeze.taeung@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1489080941-3965-1-git-send-email-treeze.taeung@gmail.com> References: <1489080941-3965-1-git-send-email-treeze.taeung@gmail.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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 Cc: Jiri Olsa Signed-off-by: Taeung Song --- 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(-) diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c index 4f52d85..93365e1 100644 --- a/tools/perf/builtin-annotate.c +++ b/tools/perf/builtin-annotate.c @@ -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", diff --git a/tools/perf/ui/browsers/annotate.c b/tools/perf/ui/browsers/annotate.c index ba36aac..03b2012 100644 --- a/tools/perf/ui/browsers/annotate.c +++ b/tools/perf/ui/browsers/annotate.c @@ -11,11 +11,6 @@ #include "../../util/config.h" #include -struct disasm_line_samples { - double percent; - u64 nr; -}; - #define IPC_WIDTH 6 #define CYCLES_WIDTH 6 diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index a50d949..7d1c7cc 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1357,6 +1357,273 @@ static const char *annotate__norm_arch(const char *arch_name) return normalize_arch((char *)arch_name); } +static int parse_srcline(char *srcline, char **path, int *line_nr) +{ + char *sep; + + if (srcline == NULL || !strcmp(srcline, SRCLINE_UNKNOWN)) + return -1; + + sep = strchr(srcline, ':'); + if (sep) { + *sep = '\0'; + *path = srcline; + *line_nr = strtoul(++sep, NULL, 0); + } else + return -1; + + return 0; +} + +static void code_line__free(struct code_line *cl) +{ + zfree(&cl->line); + zfree(&cl->matched_dl_arr); + zfree(&cl->samples_sum); + free(cl); +} + +static void code_lines__free(struct list_head *code_lines) +{ + struct code_line *pos, *tmp; + + if (list_empty(code_lines)) + return; + + list_for_each_entry_safe(pos, tmp, code_lines, node) { + list_del_init(&pos->node); + code_line__free(pos); + } +} + +static int symbol__free_source_code(struct symbol *sym) +{ + struct annotation *notes = symbol__annotation(sym); + struct source_code *code = notes->src->code; + + if (code == NULL) + return -1; + + code_lines__free(&code->lines); + zfree(&code->path); + zfree(¬es->src->code); + return 0; +} + +static 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; + struct sym_hist *h; + struct source_code *code = notes->src->code; + + for (i = 0; i < code->nr_events; i++) { + double percent = 0.0; + + h = annotation__histogram(notes, evsel->idx + i); + nr_samples = h->addr[dl->offset]; + if (h->sum) + percent = 100.0 * nr_samples / h->sum; + + cl->samples_sum[i].percent += percent; + cl->samples_sum[i].nr += nr_samples; + } +} + +static void source_code__print(struct code_line *cl, int nr_events, + struct annotation *notes, struct perf_evsel *evsel) +{ + int i; + const char *color; + double percent, max_percent = 0.0; + + for (i = 0; i < cl->nr_matched_dl; i++) { + code_line__sum_samples(cl, cl->matched_dl_arr[i], notes, evsel); + } + + for (i = 0; i < nr_events; i++) { + percent = cl->samples_sum[i].percent; + color = get_percent_color(percent); + if (max_percent < percent) + max_percent = percent; + + if (symbol_conf.show_total_period) + color_fprintf(stdout, color, " %7" PRIu64, + cl->samples_sum[i].nr); + else + color_fprintf(stdout, color, " %7.2f", percent); + } + color = get_percent_color(max_percent); + color_fprintf(stdout, color, " : %d %s\n", + cl->line_nr, cl->line); +} + +static int code_line__match_with_dl(struct code_line *cl, struct list_head *disasm_lines) +{ + int nr_dl = 0; + struct disasm_line *pos, **tmp, **matched_dl_arr; + size_t allocated = sizeof(struct disasm_line **); + + matched_dl_arr = zalloc(allocated); + if (!matched_dl_arr) + return -1; + + list_for_each_entry(pos, disasm_lines, node) { + if (pos->line_nr == cl->line_nr) { + if (nr_dl > 0) { + tmp = realloc(matched_dl_arr, allocated * (nr_dl + 1)); + if (!tmp) { + free(matched_dl_arr); + return -1; + } + matched_dl_arr = tmp; + } + matched_dl_arr[nr_dl++] = pos; + } + } + + cl->matched_dl_arr = matched_dl_arr; + cl->nr_matched_dl = nr_dl; + return 0; +} + +static struct code_line *code_line__new(char *line, int linenr, int nr_events) +{ + struct code_line *cl = zalloc(sizeof(*cl)); + + if (!cl) + return NULL; + + cl->line_nr = linenr; + cl->line = strdup(line); + cl->samples_sum = + zalloc(sizeof(struct disasm_line_samples) * nr_events); + if (!cl->samples_sum) + zfree(&cl); + + return cl; +} + +static int source_code__collect(struct source_code *code, + struct annotation *notes, + char *path, char* d_name, + int start_linenr, int end_linenr) +{ + int ret = -1, linenr = 0; + char *line = NULL, *parsed_line; + size_t len; + FILE *file; + struct stat srcfile_st, objfile_st; + struct code_line *cl; + + if (stat(path, &srcfile_st) < 0) + return -1; + else { + if (stat(d_name, &objfile_st) < 0) + return -1; + if (srcfile_st.st_mtime > objfile_st.st_mtime) { + pr_err("Source file is more recent than when recording" + ": %s\n", path); + pr_err(" (try 'perf record' again after recompiling" + " the source file or with 'perf buildid-cache -r')\n"); + return -1; + } + } + + file = fopen(path, "r"); + if (!file) + return -1; + + if (srcline_full_filename) + code->path = strdup(path); + else + code->path = strdup(basename(path)); + + INIT_LIST_HEAD(&code->lines); + while (!feof(file)) { + if (getline(&line, &len, file) < 0) + goto out_err; + + if (++linenr < start_linenr) + continue; + + parsed_line = rtrim(line); + cl = code_line__new(parsed_line, linenr, code->nr_events); + if (!cl) + goto out_err; + if (code_line__match_with_dl(cl, ¬es->src->source) < 0) + goto out_err; + + list_add_tail(&cl->node, &code->lines); + if (linenr == end_linenr) { + ret = 0; + goto out; + } + } +out_err: + code_lines__free(&code->lines); +out: + free(line); + fclose(file); + return ret; +} + +static 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; + bool tmp; + u64 start = map__rip_2objdump(map, sym->start); + u64 end = map__rip_2objdump(map, sym->end - 1); + int start_linenr, end_linenr, ret = -1; + char *path, *start_srcline = NULL, *end_srcline = NULL; + char *d_name = map->dso->symsrc_filename; + + if (!d_name) + return -1; + + tmp = srcline_full_filename; + srcline_full_filename = true; + start_srcline = get_srcline(map->dso, start, NULL, false); + end_srcline = get_srcline(map->dso, end, NULL, false); + srcline_full_filename = tmp; + + if (parse_srcline(start_srcline, &path, &start_linenr) < 0) + goto out; + if (parse_srcline(end_srcline, &path, &end_linenr) < 0) + goto out; + + code = zalloc(sizeof(struct source_code)); + if (code == NULL) + goto out; + + if (perf_evsel__is_group_event(evsel)) + code->nr_events = evsel->nr_members; + else + code->nr_events = 1; + + /* To read a function header for the sym */ + if (start_linenr > 4) + start_linenr -= 4; + else + start_linenr = 1; + + if (source_code__collect(code, notes, path, d_name, + start_linenr, end_linenr) < 0) { + zfree(&code); + goto out; + } + + ret = 0; + notes->src->code = code; +out: + free_srcline(start_srcline); + free_srcline(end_srcline); + return ret; +} + int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_name, size_t privsize) { struct dso *dso = map->dso; @@ -1497,7 +1764,6 @@ int symbol__disassemble(struct symbol *sym, struct map *map, const char *arch_na if (nline == 0) pr_err("No output from %s\n", command); - /* * kallsyms does not have symbol sizes so there may a nop at the end. * Remove it. @@ -1755,6 +2021,7 @@ int symbol__annotate_printf(struct symbol *sym, struct map *map, struct sym_hist *h = annotation__histogram(notes, evsel->idx); struct disasm_line *pos, *queue = NULL; u64 start = map__rip_2objdump(map, sym->start); + bool src_code_only = false; int printed = 2, queue_len = 0; int more = 0; u64 len; @@ -1775,8 +2042,14 @@ int symbol__annotate_printf(struct symbol *sym, struct map *map, if (perf_evsel__is_group_event(evsel)) width *= evsel->nr_members; - graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples)\n", - width, width, "Percent", d_filename, evsel_name, h->sum); + if (symbol_conf.annotate_src_only && notes->src->has_src_code) + src_code_only = true; + + graph_dotted_len = printf(" %-*.*s| %s of %s for %s (%" PRIu64 " samples)\n", + width, width, "Percent", + src_code_only ? "Source code" : "Source code & Disassembly", + src_code_only ? notes->src->code->path : d_filename, + evsel_name, h->sum); printf("%-*.*s----\n", graph_dotted_len, graph_dotted_len, graph_dotted_line); @@ -1784,6 +2057,16 @@ int symbol__annotate_printf(struct symbol *sym, struct map *map, if (verbose > 0) symbol__annotate_hits(sym, evsel); + if (src_code_only) { + struct source_code *code = notes->src->code; + struct code_line *cl; + + list_for_each_entry(cl, &code->lines, node) + source_code__print(cl, code->nr_events, notes, evsel); + + goto out; + } + list_for_each_entry(pos, ¬es->src->source, node) { if (context && queue == NULL) { queue = pos; @@ -1820,7 +2103,8 @@ int symbol__annotate_printf(struct symbol *sym, struct map *map, break; } } - +out: + printf("\n"); free(filename); return more; @@ -1890,6 +2174,7 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map, bool full_paths, int min_pcnt, int max_lines) { struct dso *dso = map->dso; + struct annotation *notes = symbol__annotation(sym); struct rb_root source_line = RB_ROOT; u64 len; @@ -1904,11 +2189,17 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map, print_summary(&source_line, dso->long_name); } + if (symbol_conf.annotate_src_only && + symbol__get_source_code(sym, map, evsel) == 0) + notes->src->has_src_code = true; + symbol__annotate_printf(sym, map, evsel, full_paths, min_pcnt, max_lines, 0); if (print_lines) symbol__free_source_line(sym, len); + if (notes->src->has_src_code) + symbol__free_source_code(sym); disasm__purge(&symbol__annotation(sym)->src->source); return 0; diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 948aa8e..dd7ddae 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -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; diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 70e389b..71228bd 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -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, diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 6c358b7..190be1d 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -108,6 +108,7 @@ struct symbol_conf { kptr_restrict, annotate_asm_raw, annotate_src, + annotate_src_only, event_group, demangle, demangle_kernel, -- 2.7.4