These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / tools / perf / util / annotate.c
index 7f5bdfc..1dd1949 100644 (file)
@@ -473,17 +473,73 @@ int symbol__alloc_hist(struct symbol *sym)
        return 0;
 }
 
+/* The cycles histogram is lazily allocated. */
+static int symbol__alloc_hist_cycles(struct symbol *sym)
+{
+       struct annotation *notes = symbol__annotation(sym);
+       const size_t size = symbol__size(sym);
+
+       notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
+       if (notes->src->cycles_hist == NULL)
+               return -1;
+       return 0;
+}
+
 void symbol__annotate_zero_histograms(struct symbol *sym)
 {
        struct annotation *notes = symbol__annotation(sym);
 
        pthread_mutex_lock(&notes->lock);
-       if (notes->src != NULL)
+       if (notes->src != NULL) {
                memset(notes->src->histograms, 0,
                       notes->src->nr_histograms * notes->src->sizeof_sym_hist);
+               if (notes->src->cycles_hist)
+                       memset(notes->src->cycles_hist, 0,
+                               symbol__size(sym) * sizeof(struct cyc_hist));
+       }
        pthread_mutex_unlock(&notes->lock);
 }
 
+static int __symbol__account_cycles(struct annotation *notes,
+                                   u64 start,
+                                   unsigned offset, unsigned cycles,
+                                   unsigned have_start)
+{
+       struct cyc_hist *ch;
+
+       ch = notes->src->cycles_hist;
+       /*
+        * For now we can only account one basic block per
+        * final jump. But multiple could be overlapping.
+        * Always account the longest one. So when
+        * a shorter one has been already seen throw it away.
+        *
+        * We separately always account the full cycles.
+        */
+       ch[offset].num_aggr++;
+       ch[offset].cycles_aggr += cycles;
+
+       if (!have_start && ch[offset].have_start)
+               return 0;
+       if (ch[offset].num) {
+               if (have_start && (!ch[offset].have_start ||
+                                  ch[offset].start > start)) {
+                       ch[offset].have_start = 0;
+                       ch[offset].cycles = 0;
+                       ch[offset].num = 0;
+                       if (ch[offset].reset < 0xffff)
+                               ch[offset].reset++;
+               } else if (have_start &&
+                          ch[offset].start < start)
+                       return 0;
+       }
+       ch[offset].have_start = have_start;
+       ch[offset].start = start;
+       ch[offset].cycles += cycles;
+       ch[offset].num++;
+       return 0;
+}
+
 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
                                      struct annotation *notes, int evidx, u64 addr)
 {
@@ -492,8 +548,11 @@ static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
 
        pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
 
-       if (addr < sym->start || addr >= sym->end)
+       if (addr < sym->start || addr >= sym->end) {
+               pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
+                      __func__, __LINE__, sym->name, sym->start, addr, sym->end);
                return -ERANGE;
+       }
 
        offset = addr - sym->start;
        h = annotation__histogram(notes, evidx);
@@ -506,6 +565,21 @@ static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
        return 0;
 }
 
+static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
+{
+       struct annotation *notes = symbol__annotation(sym);
+
+       if (notes->src == NULL) {
+               if (symbol__alloc_hist(sym) < 0)
+                       return NULL;
+       }
+       if (!notes->src->cycles_hist && cycles) {
+               if (symbol__alloc_hist_cycles(sym) < 0)
+                       return NULL;
+       }
+       return notes;
+}
+
 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
                                    int evidx, u64 addr)
 {
@@ -513,14 +587,71 @@ static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
 
        if (sym == NULL)
                return 0;
+       notes = symbol__get_annotation(sym, false);
+       if (notes == NULL)
+               return -ENOMEM;
+       return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
+}
 
-       notes = symbol__annotation(sym);
-       if (notes->src == NULL) {
-               if (symbol__alloc_hist(sym) < 0)
-                       return -ENOMEM;
+static int symbol__account_cycles(u64 addr, u64 start,
+                                 struct symbol *sym, unsigned cycles)
+{
+       struct annotation *notes;
+       unsigned offset;
+
+       if (sym == NULL)
+               return 0;
+       notes = symbol__get_annotation(sym, true);
+       if (notes == NULL)
+               return -ENOMEM;
+       if (addr < sym->start || addr >= sym->end)
+               return -ERANGE;
+
+       if (start) {
+               if (start < sym->start || start >= sym->end)
+                       return -ERANGE;
+               if (start >= addr)
+                       start = 0;
        }
+       offset = addr - sym->start;
+       return __symbol__account_cycles(notes,
+                                       start ? start - sym->start : 0,
+                                       offset, cycles,
+                                       !!start);
+}
 
-       return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
+int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
+                                   struct addr_map_symbol *start,
+                                   unsigned cycles)
+{
+       u64 saddr = 0;
+       int err;
+
+       if (!cycles)
+               return 0;
+
+       /*
+        * Only set start when IPC can be computed. We can only
+        * compute it when the basic block is completely in a single
+        * function.
+        * Special case the case when the jump is elsewhere, but
+        * it starts on the function start.
+        */
+       if (start &&
+               (start->sym == ams->sym ||
+                (ams->sym &&
+                  start->addr == ams->sym->start + ams->map->start)))
+               saddr = start->al_addr;
+       if (saddr == 0)
+               pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
+                       ams->addr,
+                       start ? start->addr : 0,
+                       ams->sym ? ams->sym->start + ams->map->start : 0,
+                       saddr);
+       err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
+       if (err)
+               pr_debug2("account_cycles failed %d\n", err);
+       return err;
 }
 
 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
@@ -647,14 +778,15 @@ struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disa
 }
 
 double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
-                           s64 end, const char **path)
+                           s64 end, const char **path, u64 *nr_samples)
 {
        struct source_line *src_line = notes->src->lines;
        double percent = 0.0;
+       *nr_samples = 0;
 
        if (src_line) {
                size_t sizeof_src_line = sizeof(*src_line) +
-                               sizeof(src_line->p) * (src_line->nr_pcnt - 1);
+                               sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
 
                while (offset < end) {
                        src_line = (void *)notes->src->lines +
@@ -663,7 +795,8 @@ double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
                        if (*path == NULL)
                                *path = src_line->path;
 
-                       percent += src_line->p[evidx].percent;
+                       percent += src_line->samples[evidx].percent;
+                       *nr_samples += src_line->samples[evidx].nr;
                        offset++;
                }
        } else {
@@ -673,8 +806,10 @@ double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
                while (offset < end)
                        hits += h->addr[offset++];
 
-               if (h->sum)
+               if (h->sum) {
+                       *nr_samples = hits;
                        percent = 100.0 * hits / h->sum;
+               }
        }
 
        return percent;
@@ -689,8 +824,10 @@ static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 st
 
        if (dl->offset != -1) {
                const char *path = NULL;
+               u64 nr_samples;
                double percent, max_percent = 0.0;
                double *ppercents = &percent;
+               u64 *psamples = &nr_samples;
                int i, nr_percent = 1;
                const char *color;
                struct annotation *notes = symbol__annotation(sym);
@@ -703,8 +840,10 @@ static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 st
                if (perf_evsel__is_group_event(evsel)) {
                        nr_percent = evsel->nr_members;
                        ppercents = calloc(nr_percent, sizeof(double));
-                       if (ppercents == NULL)
+                       psamples = calloc(nr_percent, sizeof(u64));
+                       if (ppercents == NULL || psamples == NULL) {
                                return -1;
+                       }
                }
 
                for (i = 0; i < nr_percent; i++) {
@@ -712,9 +851,10 @@ static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 st
                                        notes->src->lines ? i : evsel->idx + i,
                                        offset,
                                        next ? next->offset : (s64) len,
-                                       &path);
+                                       &path, &nr_samples);
 
                        ppercents[i] = percent;
+                       psamples[i] = nr_samples;
                        if (percent > max_percent)
                                max_percent = percent;
                }
@@ -752,8 +892,14 @@ static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 st
 
                for (i = 0; i < nr_percent; i++) {
                        percent = ppercents[i];
+                       nr_samples = psamples[i];
                        color = get_percent_color(percent);
-                       color_fprintf(stdout, color, " %7.2f", percent);
+
+                       if (symbol_conf.show_total_period)
+                               color_fprintf(stdout, color, " %7" PRIu64,
+                                             nr_samples);
+                       else
+                               color_fprintf(stdout, color, " %7.2f", percent);
                }
 
                printf(" :      ");
@@ -763,6 +909,9 @@ static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 st
                if (ppercents != &percent)
                        free(ppercents);
 
+               if (psamples != &nr_samples)
+                       free(psamples);
+
        } else if (max_lines && printed >= max_lines)
                return 1;
        else {
@@ -935,6 +1084,7 @@ int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
        struct kcore_extract kce;
        bool delete_extract = false;
        int lineno = 0;
+       int nline;
 
        if (filename)
                symbol__join_symfs(symfs_filename, filename);
@@ -980,6 +1130,7 @@ fallback:
                dso->annotate_warned = 1;
                pr_err("Can't annotate %s:\n\n"
                       "No vmlinux file%s\nwas found in the path.\n\n"
+                      "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
                       "Please use:\n\n"
                       "  perf buildid-cache -vu vmlinux\n\n"
                       "or:\n\n"
@@ -1029,6 +1180,9 @@ fallback:
 
                ret = decompress_to_file(m.ext, symfs_filename, fd);
 
+               if (ret)
+                       pr_err("Cannot decompress %s %s\n", m.ext, symfs_filename);
+
                free(m.ext);
                close(fd);
 
@@ -1054,13 +1208,25 @@ fallback:
        pr_debug("Executing: %s\n", command);
 
        file = popen(command, "r");
-       if (!file)
+       if (!file) {
+               pr_err("Failure running %s\n", command);
+               /*
+                * If we were using debug info should retry with
+                * original binary.
+                */
                goto out_remove_tmp;
+       }
 
-       while (!feof(file))
+       nline = 0;
+       while (!feof(file)) {
                if (symbol__parse_objdump_line(sym, map, file, privsize,
                            &lineno) < 0)
                        break;
+               nline++;
+       }
+
+       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.
@@ -1096,7 +1262,7 @@ static void insert_source_line(struct rb_root *root, struct source_line *src_lin
                ret = strcmp(iter->path, src_line->path);
                if (ret == 0) {
                        for (i = 0; i < src_line->nr_pcnt; i++)
-                               iter->p[i].percent_sum += src_line->p[i].percent;
+                               iter->samples[i].percent_sum += src_line->samples[i].percent;
                        return;
                }
 
@@ -1107,7 +1273,7 @@ static void insert_source_line(struct rb_root *root, struct source_line *src_lin
        }
 
        for (i = 0; i < src_line->nr_pcnt; i++)
-               src_line->p[i].percent_sum = src_line->p[i].percent;
+               src_line->samples[i].percent_sum = src_line->samples[i].percent;
 
        rb_link_node(&src_line->node, parent, p);
        rb_insert_color(&src_line->node, root);
@@ -1118,9 +1284,9 @@ static int cmp_source_line(struct source_line *a, struct source_line *b)
        int i;
 
        for (i = 0; i < a->nr_pcnt; i++) {
-               if (a->p[i].percent_sum == b->p[i].percent_sum)
+               if (a->samples[i].percent_sum == b->samples[i].percent_sum)
                        continue;
-               return a->p[i].percent_sum > b->p[i].percent_sum;
+               return a->samples[i].percent_sum > b->samples[i].percent_sum;
        }
 
        return 0;
@@ -1172,7 +1338,7 @@ static void symbol__free_source_line(struct symbol *sym, int len)
        int i;
 
        sizeof_src_line = sizeof(*src_line) +
-                         (sizeof(src_line->p) * (src_line->nr_pcnt - 1));
+                         (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
 
        for (i = 0; i < len; i++) {
                free_srcline(src_line->path);
@@ -1204,7 +1370,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
                        h_sum += h->sum;
                }
                nr_pcnt = evsel->nr_members;
-               sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->p);
+               sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
        }
 
        if (!h_sum)
@@ -1224,10 +1390,10 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 
                for (k = 0; k < nr_pcnt; k++) {
                        h = annotation__histogram(notes, evidx + k);
-                       src_line->p[k].percent = 100.0 * h->addr[i] / h->sum;
+                       src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
 
-                       if (src_line->p[k].percent > percent_max)
-                               percent_max = src_line->p[k].percent;
+                       if (src_line->samples[k].percent > percent_max)
+                               percent_max = src_line->samples[k].percent;
                }
 
                if (percent_max <= 0.5)
@@ -1267,7 +1433,7 @@ static void print_summary(struct rb_root *root, const char *filename)
 
                src_line = rb_entry(node, struct source_line, node);
                for (i = 0; i < src_line->nr_pcnt; i++) {
-                       percent = src_line->p[i].percent_sum;
+                       percent = src_line->samples[i].percent_sum;
                        color = get_percent_color(percent);
                        color_fprintf(stdout, color, " %7.2f", percent);
 
@@ -1454,6 +1620,7 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
        len = symbol__size(sym);
 
        if (print_lines) {
+               srcline_full_filename = full_paths;
                symbol__get_source_line(sym, map, evsel, &source_line, len);
                print_summary(&source_line, dso->long_name);
        }