Skip to content

Commit 99150a1

Browse files
Jin Yaoacmel
authored andcommitted
perf diff: Use hists to manage basic blocks per symbol
The hist__account_cycles() can account cycles per basic block. The basic block information is saved in cycles_hist structure. This patch processes each symbol, get basic blocks from cycles_hist and add the basic block entries to a new hists (in 'struct block_hist'). Using a hists is because we need to compare, sort and print the basic blocks later. v6: --- Since 'ops' argument is removed from hists__add_entry_block, update the code accordingly. No functional change. v5: --- Since now we still carry block_info in 'struct hist_entry' we don't need to use our own new/free ops for hist entries. And the block_info is released in hist_entry__delete. v3: --- 1. In v2, we put block stuffs in 'struct hist_entry', but it's not a good design. In v3, we create a new 'struct block_hist' and cast the 'struct hist_entry' to 'struct block_hist' in some places, which can avoid adding new stuffs in 'struct hist_entry'. 2. abs() -> labs(), in block_cycles_diff_cmp(). v2: --- v1 adds the basic block entries to per data-file hists but v2 adds the basic block entries to per symbol hists. That is to keep current perf-diff format. Will show the result in next patches. Signed-off-by: Jin Yao <yao.jin@linux.intel.com> Reviewed-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Jin Yao <yao.jin@intel.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1561713784-30533-5-git-send-email-yao.jin@linux.intel.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 30d8155 commit 99150a1

3 files changed

Lines changed: 202 additions & 3 deletions

File tree

‎tools/perf/builtin-diff.c‎

Lines changed: 187 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "util/data.h"
2121
#include "util/config.h"
2222
#include "util/time-utils.h"
23+
#include "util/annotate.h"
2324

2425
#include <errno.h>
2526
#include <inttypes.h>
@@ -87,11 +88,14 @@ static s64 compute_wdiff_w2;
8788
static const char *cpu_list;
8889
static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
8990

91+
static struct addr_location dummy_al;
92+
9093
enum {
9194
COMPUTE_DELTA,
9295
COMPUTE_RATIO,
9396
COMPUTE_WEIGHTED_DIFF,
9497
COMPUTE_DELTA_ABS,
98+
COMPUTE_CYCLES,
9599
COMPUTE_MAX,
96100
};
97101

@@ -100,6 +104,7 @@ const char *compute_names[COMPUTE_MAX] = {
100104
[COMPUTE_DELTA_ABS] = "delta-abs",
101105
[COMPUTE_RATIO] = "ratio",
102106
[COMPUTE_WEIGHTED_DIFF] = "wdiff",
107+
[COMPUTE_CYCLES] = "cycles",
103108
};
104109

105110
static int compute = COMPUTE_DELTA_ABS;
@@ -234,6 +239,8 @@ static int setup_compute(const struct option *opt, const char *str,
234239
for (i = 0; i < COMPUTE_MAX; i++)
235240
if (!strcmp(cstr, compute_names[i])) {
236241
*cp = i;
242+
if (i == COMPUTE_CYCLES)
243+
break;
237244
return setup_compute_opt(option);
238245
}
239246

@@ -336,6 +343,31 @@ static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
336343
return -1;
337344
}
338345

346+
static void *block_hist_zalloc(size_t size)
347+
{
348+
struct block_hist *bh;
349+
350+
bh = zalloc(size + sizeof(*bh));
351+
if (!bh)
352+
return NULL;
353+
354+
return &bh->he;
355+
}
356+
357+
static void block_hist_free(void *he)
358+
{
359+
struct block_hist *bh;
360+
361+
bh = container_of(he, struct block_hist, he);
362+
hists__delete_entries(&bh->block_hists);
363+
free(bh);
364+
}
365+
366+
struct hist_entry_ops block_hist_ops = {
367+
.new = block_hist_zalloc,
368+
.free = block_hist_free,
369+
};
370+
339371
static int diff__process_sample_event(struct perf_tool *tool,
340372
union perf_event *event,
341373
struct perf_sample *sample,
@@ -363,9 +395,22 @@ static int diff__process_sample_event(struct perf_tool *tool,
363395
goto out_put;
364396
}
365397

366-
if (!hists__add_entry(hists, &al, NULL, NULL, NULL, sample, true)) {
367-
pr_warning("problem incrementing symbol period, skipping event\n");
368-
goto out_put;
398+
if (compute != COMPUTE_CYCLES) {
399+
if (!hists__add_entry(hists, &al, NULL, NULL, NULL, sample,
400+
true)) {
401+
pr_warning("problem incrementing symbol period, "
402+
"skipping event\n");
403+
goto out_put;
404+
}
405+
} else {
406+
if (!hists__add_entry_ops(hists, &block_hist_ops, &al, NULL,
407+
NULL, NULL, sample, true)) {
408+
pr_warning("problem incrementing symbol period, "
409+
"skipping event\n");
410+
goto out_put;
411+
}
412+
413+
hist__account_cycles(sample->branch_stack, &al, sample, false);
369414
}
370415

371416
/*
@@ -475,6 +520,127 @@ static void hists__baseline_only(struct hists *hists)
475520
}
476521
}
477522

523+
static int64_t block_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
524+
struct hist_entry *left, struct hist_entry *right)
525+
{
526+
struct block_info *bi_l = left->block_info;
527+
struct block_info *bi_r = right->block_info;
528+
int cmp;
529+
530+
if (!bi_l->sym || !bi_r->sym) {
531+
if (!bi_l->sym && !bi_r->sym)
532+
return 0;
533+
else if (!bi_l->sym)
534+
return -1;
535+
else
536+
return 1;
537+
}
538+
539+
if (bi_l->sym == bi_r->sym) {
540+
if (bi_l->start == bi_r->start) {
541+
if (bi_l->end == bi_r->end)
542+
return 0;
543+
else
544+
return (int64_t)(bi_r->end - bi_l->end);
545+
} else
546+
return (int64_t)(bi_r->start - bi_l->start);
547+
} else {
548+
cmp = strcmp(bi_l->sym->name, bi_r->sym->name);
549+
return cmp;
550+
}
551+
552+
if (bi_l->sym->start != bi_r->sym->start)
553+
return (int64_t)(bi_r->sym->start - bi_l->sym->start);
554+
555+
return (int64_t)(bi_r->sym->end - bi_l->sym->end);
556+
}
557+
558+
static int64_t block_cycles_diff_cmp(struct hist_entry *left,
559+
struct hist_entry *right)
560+
{
561+
bool pairs_left = hist_entry__has_pairs(left);
562+
bool pairs_right = hist_entry__has_pairs(right);
563+
s64 l, r;
564+
565+
if (!pairs_left && !pairs_right)
566+
return 0;
567+
568+
l = labs(left->diff.cycles);
569+
r = labs(right->diff.cycles);
570+
return r - l;
571+
}
572+
573+
static int64_t block_sort(struct perf_hpp_fmt *fmt __maybe_unused,
574+
struct hist_entry *left, struct hist_entry *right)
575+
{
576+
return block_cycles_diff_cmp(right, left);
577+
}
578+
579+
static void init_block_hist(struct block_hist *bh)
580+
{
581+
__hists__init(&bh->block_hists, &bh->block_list);
582+
perf_hpp_list__init(&bh->block_list);
583+
584+
INIT_LIST_HEAD(&bh->block_fmt.list);
585+
INIT_LIST_HEAD(&bh->block_fmt.sort_list);
586+
bh->block_fmt.cmp = block_cmp;
587+
bh->block_fmt.sort = block_sort;
588+
perf_hpp_list__register_sort_field(&bh->block_list,
589+
&bh->block_fmt);
590+
bh->valid = true;
591+
}
592+
593+
static void init_block_info(struct block_info *bi, struct symbol *sym,
594+
struct cyc_hist *ch, int offset)
595+
{
596+
bi->sym = sym;
597+
bi->start = ch->start;
598+
bi->end = offset;
599+
bi->cycles = ch->cycles;
600+
bi->cycles_aggr = ch->cycles_aggr;
601+
bi->num = ch->num;
602+
bi->num_aggr = ch->num_aggr;
603+
}
604+
605+
static int process_block_per_sym(struct hist_entry *he)
606+
{
607+
struct annotation *notes;
608+
struct cyc_hist *ch;
609+
struct block_hist *bh;
610+
611+
if (!he->ms.map || !he->ms.sym)
612+
return 0;
613+
614+
notes = symbol__annotation(he->ms.sym);
615+
if (!notes || !notes->src || !notes->src->cycles_hist)
616+
return 0;
617+
618+
bh = container_of(he, struct block_hist, he);
619+
init_block_hist(bh);
620+
621+
ch = notes->src->cycles_hist;
622+
for (unsigned int i = 0; i < symbol__size(he->ms.sym); i++) {
623+
if (ch[i].num_aggr) {
624+
struct block_info *bi;
625+
struct hist_entry *he_block;
626+
627+
bi = block_info__new();
628+
if (!bi)
629+
return -1;
630+
631+
init_block_info(bi, he->ms.sym, &ch[i], i);
632+
he_block = hists__add_entry_block(&bh->block_hists,
633+
&dummy_al, bi);
634+
if (!he_block) {
635+
block_info__put(bi);
636+
return -1;
637+
}
638+
}
639+
}
640+
641+
return 0;
642+
}
643+
478644
static void hists__precompute(struct hists *hists)
479645
{
480646
struct rb_root_cached *root;
@@ -494,6 +660,9 @@ static void hists__precompute(struct hists *hists)
494660
he = rb_entry(next, struct hist_entry, rb_node_in);
495661
next = rb_next(&he->rb_node_in);
496662

663+
if (compute == COMPUTE_CYCLES)
664+
process_block_per_sym(he);
665+
497666
data__for_each_file_new(i, d) {
498667
pair = get_pair_data(he, d);
499668
if (!pair)
@@ -510,6 +679,9 @@ static void hists__precompute(struct hists *hists)
510679
case COMPUTE_WEIGHTED_DIFF:
511680
compute_wdiff(he, pair);
512681
break;
682+
case COMPUTE_CYCLES:
683+
process_block_per_sym(pair);
684+
break;
513685
default:
514686
BUG_ON(1);
515687
}
@@ -1411,6 +1583,13 @@ static int ui_init(void)
14111583
case COMPUTE_DELTA_ABS:
14121584
fmt->sort = hist_entry__cmp_delta_abs_idx;
14131585
break;
1586+
case COMPUTE_CYCLES:
1587+
/*
1588+
* Should set since 'fmt->sort' is called without
1589+
* checking valid during sorting
1590+
*/
1591+
fmt->sort = hist_entry__cmp_nop;
1592+
break;
14141593
default:
14151594
BUG_ON(1);
14161595
}
@@ -1507,6 +1686,8 @@ int cmd_diff(int argc, const char **argv)
15071686
if (quiet)
15081687
perf_quiet_option();
15091688

1689+
symbol__annotation_init();
1690+
15101691
if (symbol__init(NULL) < 0)
15111692
return -1;
15121693

@@ -1516,6 +1697,9 @@ int cmd_diff(int argc, const char **argv)
15161697
if (check_file_brstack() < 0)
15171698
return -1;
15181699

1700+
if (compute == COMPUTE_CYCLES && !pdiff.has_br_stack)
1701+
return -1;
1702+
15191703
if (ui_init() < 0)
15201704
return -1;
15211705

‎tools/perf/util/hist.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,9 @@ void hist_entry__delete(struct hist_entry *he)
12291229
mem_info__zput(he->mem_info);
12301230
}
12311231

1232+
if (he->block_info)
1233+
block_info__zput(he->block_info);
1234+
12321235
zfree(&he->res_samples);
12331236
zfree(&he->stat_acc);
12341237
free_srcline(he->srcline);

‎tools/perf/util/sort.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ struct hist_entry_diff {
7979

8080
/* HISTC_WEIGHTED_DIFF */
8181
s64 wdiff;
82+
83+
/* PERF_HPP_DIFF__CYCLES */
84+
s64 cycles;
8285
};
8386
};
8487

@@ -286,6 +289,15 @@ struct sort_entry {
286289
u8 se_width_idx;
287290
};
288291

292+
struct block_hist {
293+
struct hists block_hists;
294+
struct perf_hpp_list block_list;
295+
struct perf_hpp_fmt block_fmt;
296+
int block_idx;
297+
bool valid;
298+
struct hist_entry he;
299+
};
300+
289301
extern struct sort_entry sort_thread;
290302
extern struct list_head hist_entry__sort_list;
291303

0 commit comments

Comments
 (0)