Skip to content

Commit 4598a0a

Browse files
longman88acmel
authored andcommitted
perf symbols: Improve DSO long names lookup speed with rbtree
With workload that spawns and destroys many threads and processes, it was found that perf-mem could took a long time to post-process the perf data after the target workload had completed its operation. The performance bottleneck was found to be the lookup and insertion of the new DSO structures (thousands of them in this case). In a dual-socket Ivy-Bridge E7-4890 v2 machine (30-core, 60-thread), the perf profile below shows what perf was doing after the profiled AIM7 shared workload completed: - 83.94% perf libc-2.11.3.so [.] __strcmp_sse42 - __strcmp_sse42 - 99.82% map__new machine__process_mmap_event perf_session_deliver_event perf_session__process_event __perf_session__process_events cmd_record cmd_mem run_builtin main __libc_start_main - 13.17% perf perf [.] __dsos__findnew __dsos__findnew map__new machine__process_mmap_event perf_session_deliver_event perf_session__process_event __perf_session__process_events cmd_record cmd_mem run_builtin main __libc_start_main So about 97% of CPU times were spent in the map__new() function trying to insert new DSO entry into the DSO linked list. The whole post-processing step took about 9 minutes. The DSO structures are currently searched linearly. So the total processing time will be proportional to n^2. To overcome this performance problem, the DSO code is modified to also put the DSO structures in a RB tree sorted by its long name in additional to being in a simple linked list. With this change, the processing time will become proportional to n*log(n) which will be much quicker for large n. However, the short name will still be searched using the old linear searching method. With that patch in place, the same perf-mem post-processing step took less than 30 seconds to complete. Signed-off-by: Waiman Long <Waiman.Long@hp.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Don Zickus <dzickus@redhat.com> Cc: Douglas Hatch <doug.hatch@hp.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Scott J Norton <scott.norton@hp.com> Link: http://lkml.kernel.org/r/1412098575-27863-3-git-send-email-Waiman.Long@hp.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 8fa7d87 commit 4598a0a

3 files changed

Lines changed: 71 additions & 5 deletions

File tree

‎tools/perf/util/dso.c‎

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,65 @@ struct dso *dso__kernel_findnew(struct machine *machine, const char *name,
653653
return dso;
654654
}
655655

656+
/*
657+
* Find a matching entry and/or link current entry to RB tree.
658+
* Either one of the dso or name parameter must be non-NULL or the
659+
* function will not work.
660+
*/
661+
static struct dso *dso__findlink_by_longname(struct rb_root *root,
662+
struct dso *dso, const char *name)
663+
{
664+
struct rb_node **p = &root->rb_node;
665+
struct rb_node *parent = NULL;
666+
667+
if (!name)
668+
name = dso->long_name;
669+
/*
670+
* Find node with the matching name
671+
*/
672+
while (*p) {
673+
struct dso *this = rb_entry(*p, struct dso, rb_node);
674+
int rc = strcmp(name, this->long_name);
675+
676+
parent = *p;
677+
if (rc == 0) {
678+
/*
679+
* In case the new DSO is a duplicate of an existing
680+
* one, print an one-time warning & put the new entry
681+
* at the end of the list of duplicates.
682+
*/
683+
if (!dso || (dso == this))
684+
return this; /* Find matching dso */
685+
/*
686+
* The core kernel DSOs may have duplicated long name.
687+
* In this case, the short name should be different.
688+
* Comparing the short names to differentiate the DSOs.
689+
*/
690+
rc = strcmp(dso->short_name, this->short_name);
691+
if (rc == 0) {
692+
pr_err("Duplicated dso name: %s\n", name);
693+
return NULL;
694+
}
695+
}
696+
if (rc < 0)
697+
p = &parent->rb_left;
698+
else
699+
p = &parent->rb_right;
700+
}
701+
if (dso) {
702+
/* Add new node and rebalance tree */
703+
rb_link_node(&dso->rb_node, parent, p);
704+
rb_insert_color(&dso->rb_node, root);
705+
}
706+
return NULL;
707+
}
708+
709+
static inline struct dso *
710+
dso__find_by_longname(const struct rb_root *root, const char *name)
711+
{
712+
return dso__findlink_by_longname((struct rb_root *)root, NULL, name);
713+
}
714+
656715
void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
657716
{
658717
if (name == NULL)
@@ -755,6 +814,7 @@ struct dso *dso__new(const char *name)
755814
dso->a2l_fails = 1;
756815
dso->kernel = DSO_TYPE_USER;
757816
dso->needs_swap = DSO_SWAP__UNSET;
817+
RB_CLEAR_NODE(&dso->rb_node);
758818
INIT_LIST_HEAD(&dso->node);
759819
INIT_LIST_HEAD(&dso->data.open_entry);
760820
}
@@ -765,6 +825,10 @@ struct dso *dso__new(const char *name)
765825
void dso__delete(struct dso *dso)
766826
{
767827
int i;
828+
829+
if (!RB_EMPTY_NODE(&dso->rb_node))
830+
pr_err("DSO %s is still in rbtree when being deleted!\n",
831+
dso->long_name);
768832
for (i = 0; i < MAP__NR_TYPES; ++i)
769833
symbols__delete(&dso->symbols[i]);
770834

@@ -854,6 +918,7 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
854918
void dsos__add(struct dsos *dsos, struct dso *dso)
855919
{
856920
list_add_tail(&dso->node, &dsos->head);
921+
dso__findlink_by_longname(&dsos->root, dso, NULL);
857922
}
858923

859924
struct dso *dsos__find(const struct dsos *dsos, const char *name,
@@ -867,10 +932,7 @@ struct dso *dsos__find(const struct dsos *dsos, const char *name,
867932
return pos;
868933
return NULL;
869934
}
870-
list_for_each_entry(pos, &dsos->head, node)
871-
if (strcmp(pos->long_name, name) == 0)
872-
return pos;
873-
return NULL;
935+
return dso__find_by_longname(&dsos->root, name);
874936
}
875937

876938
struct dso *__dsos__findnew(struct dsos *dsos, const char *name)

‎tools/perf/util/dso.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,17 @@ struct dso_cache {
9191
};
9292

9393
/*
94-
* DSOs are put into a list for fast iteration.
94+
* DSOs are put into both a list for fast iteration and rbtree for fast
95+
* long name lookup.
9596
*/
9697
struct dsos {
9798
struct list_head head;
99+
struct rb_root root; /* rbtree root sorted by long name */
98100
};
99101

100102
struct dso {
101103
struct list_head node;
104+
struct rb_node rb_node; /* rbtree node sorted by long name */
102105
struct rb_root symbols[MAP__NR_TYPES];
103106
struct rb_root symbol_names[MAP__NR_TYPES];
104107
void *a2l;

‎tools/perf/util/machine.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static void dsos__delete(struct dsos *dsos)
7777
struct dso *pos, *n;
7878

7979
list_for_each_entry_safe(pos, n, &dsos->head, node) {
80+
RB_CLEAR_NODE(&pos->rb_node);
8081
list_del(&pos->node);
8182
dso__delete(pos);
8283
}

0 commit comments

Comments
 (0)