[PATCH 17/35] perf annotate: Add symbol__calc_percent function

From: Jiri Olsa
Date: Wed Oct 11 2017 - 11:02:40 EST


Adding symbol__calc_percent function, that calculates
annotation data for symbol and put the data in the
struct annotation_line::samples array.

Link: http://lkml.kernel.org/n/tip-9blzqeni0t3t3e90z4jxjf8d@xxxxxxxxxxxxxx
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/util/annotate.c | 65 +++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 64 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 1b5c7d0a53e8..cfebd1678dc8 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -1618,6 +1618,65 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
goto out_remove_tmp;
}

+static void calc_percent(struct sym_hist *hist,
+ struct annotation_data *sample,
+ s64 offset, s64 end)
+{
+ unsigned int hits = 0;
+ u64 period = 0;
+
+ while (offset < end) {
+ hits += hist->addr[offset].nr_samples;
+ period += hist->addr[offset].period;
+ ++offset;
+ }
+
+ if (hist->nr_samples) {
+ sample->he.period = period;
+ sample->he.nr_samples = hits;
+ sample->percent = 100.0 * hits / hist->nr_samples;
+ }
+}
+
+static int annotation__calc_percent(struct annotation *notes,
+ struct perf_evsel *evsel, s64 len)
+{
+ struct annotation_line *al, *next;
+
+ pthread_mutex_lock(&notes->lock);
+
+ list_for_each_entry(al, &notes->src->source, node) {
+ s64 end;
+ int i;
+
+ if (al->offset == -1)
+ continue;
+
+ next = annotation_line__next(al, &notes->src->source);
+ end = next ? next->offset : len;
+
+ for (i = 0; i < al->samples_nr; i++) {
+ struct annotation_data *sample;
+ struct sym_hist *hist;
+
+ hist = annotation__histogram(notes, evsel->idx + i);
+ sample = &al->samples[i];
+
+ calc_percent(hist, sample, al->offset, end);
+ }
+ }
+
+ pthread_mutex_unlock(&notes->lock);
+ return 0;
+}
+
+static int symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel)
+{
+ struct annotation *notes = symbol__annotation(sym);
+
+ return annotation__calc_percent(notes, evsel, symbol__size(sym));
+}
+
int symbol__annotate(struct symbol *sym, struct map *map,
struct perf_evsel *evsel, size_t privsize,
struct arch **parch, char *cpuid)
@@ -1653,7 +1712,11 @@ int symbol__annotate(struct symbol *sym, struct map *map,
}
}

- return symbol__disassemble(sym, &args);
+ err = symbol__disassemble(sym, &args);
+ if (err)
+ return err;
+
+ return symbol__calc_percent(sym, evsel);
}

static void insert_source_line(struct rb_root *root, struct source_line *src_line)
--
2.13.6