perf top: Fix event group with more than two events
authorNamhyung Kim <namhyung@kernel.org>
Tue, 27 Aug 2019 23:15:55 +0000 (08:15 +0900)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 28 Aug 2019 21:15:03 +0000 (18:15 -0300)
The event group feature links relevant hist entries among events so that
they can be displayed together.  During the link process, each hist
entry in non-leader events is connected to a hist entry in the leader
event.  This is done in order of events specified in the command line so
it assumes that events are linked in the order.

But 'perf top' can break the assumption since it does the link process
multiple times.  For example, a hist entry can be in the third event
only at first so it's linked after the leader.  Some time later, second
event has a hist entry for it and it'll be linked after the entry of the
third event.

This makes the code compilicated to deal with such unordered entries.
This patch simply unlink all the entries after it's printed so that they
can assume the correct order after the repeated link process.  Also it'd
be easy to deal with decaying old entries IMHO.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Reported-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/20190827231555.121411-2-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-top.c
tools/perf/util/hist.c
tools/perf/util/hist.h

index 104dbb1095c511c03f95b3565da481a4e88d0f47..c3f95440e99cd713dfc23c7c157ad807d09bf11e 100644 (file)
@@ -273,6 +273,12 @@ static void perf_top__resort_hists(struct perf_top *t)
        evlist__for_each_entry(evlist, pos) {
                struct hists *hists = evsel__hists(pos);
 
+               /*
+                * unlink existing entries so that they can be linked
+                * in a correct order in hists__match() below.
+                */
+               hists__unlink(hists);
+
                if (evlist->enabled) {
                        if (t->zero) {
                                hists__delete_entries(hists);
index 33702675073c10da5ff35aaf4da03cd36893c023..e0b149673a88b56b8e949b452144f730cf6836cc 100644 (file)
@@ -2439,7 +2439,7 @@ void hists__match(struct hists *leader, struct hists *other)
 {
        struct rb_root_cached *root;
        struct rb_node *nd;
-       struct hist_entry *pos, *pair, *pos_pair, *tmp_pair;
+       struct hist_entry *pos, *pair;
 
        if (symbol_conf.report_hierarchy) {
                /* hierarchy report always collapses entries */
@@ -2456,24 +2456,8 @@ void hists__match(struct hists *leader, struct hists *other)
                pos  = rb_entry(nd, struct hist_entry, rb_node_in);
                pair = hists__find_entry(other, pos);
 
-               if (pair && list_empty(&pair->pairs.node)) {
-                       list_for_each_entry_safe(pos_pair, tmp_pair, &pos->pairs.head, pairs.node) {
-                               if (pos_pair->hists == other) {
-                                       /*
-                                        * XXX maybe decayed entries can appear
-                                        * here?  but then we would have use
-                                        * after free, as decayed entries are
-                                        * freed see hists__delete_entry
-                                        */
-                                       BUG_ON(!pos_pair->dummy);
-                                       list_del_init(&pos_pair->pairs.node);
-                                       hist_entry__delete(pos_pair);
-                                       break;
-                               }
-                       }
-
+               if (pair)
                        hist_entry__add_pair(pair, pos);
-               }
        }
 }
 
@@ -2558,6 +2542,25 @@ int hists__link(struct hists *leader, struct hists *other)
        return 0;
 }
 
+int hists__unlink(struct hists *hists)
+{
+       struct rb_root_cached *root;
+       struct rb_node *nd;
+       struct hist_entry *pos;
+
+       if (hists__has(hists, need_collapse))
+               root = &hists->entries_collapsed;
+       else
+               root = hists->entries_in;
+
+       for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
+               pos = rb_entry(nd, struct hist_entry, rb_node_in);
+               list_del_init(&pos->pairs.node);
+       }
+
+       return 0;
+}
+
 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
                          struct perf_sample *sample, bool nonany_branch_mode)
 {
index 83d5fc15429c7081bf7fdeb29898a6bb6c768bb9..7b9267ebebeb0af82d8390fb5510fe1aefd6c70c 100644 (file)
@@ -217,6 +217,7 @@ void hists__calc_col_len(struct hists *hists, struct hist_entry *he);
 
 void hists__match(struct hists *leader, struct hists *other);
 int hists__link(struct hists *leader, struct hists *other);
+int hists__unlink(struct hists *hists);
 
 struct hists_evsel {
        struct evsel evsel;