Commit 27d0fd410c3cee00ece2e55f4354a7a9ec1a6a6a

Authored by Arnaldo Carvalho de Melo
Committed by Ingo Molnar
1 parent 60c1baf124

strlist: Introduce strlist__entry and strlist__nr_entries methods

The strlist__entry method allows accessing strlists like an
array, will be used in the 'perf report' to access the first
entry.

We now keep the nr_entries so that we can check if we have just
one entry, will be used in 'perf report' to improve the output
by showing just at the top when we have just, say, one DSO.

While at it use nr_entries to optimize strlist__is_empty by not
using the far more costly rb_first based implementation.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
LKML-Reference: <1247325517-12272-2-git-send-email-acme@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

Showing 2 changed files with 27 additions and 4 deletions Side-by-side Diff

tools/perf/util/strlist.c
... ... @@ -64,6 +64,7 @@
64 64  
65 65 rb_link_node(&sn->rb_node, parent, p);
66 66 rb_insert_color(&sn->rb_node, &self->entries);
  67 + ++self->nr_entries;
67 68  
68 69 return 0;
69 70 }
... ... @@ -155,8 +156,9 @@
155 156 struct strlist *self = malloc(sizeof(*self));
156 157  
157 158 if (self != NULL) {
158   - self->entries = RB_ROOT;
159   - self->dupstr = dupstr;
  159 + self->entries = RB_ROOT;
  160 + self->dupstr = dupstr;
  161 + self->nr_entries = 0;
160 162 if (slist && strlist__parse_list(self, slist) != 0)
161 163 goto out_error;
162 164 }
... ... @@ -181,5 +183,19 @@
181 183 self->entries = RB_ROOT;
182 184 free(self);
183 185 }
  186 +}
  187 +
  188 +struct str_node *strlist__entry(const struct strlist *self, unsigned int idx)
  189 +{
  190 + struct rb_node *nd;
  191 +
  192 + for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  193 + struct str_node *pos = rb_entry(nd, struct str_node, rb_node);
  194 +
  195 + if (!idx--)
  196 + return pos;
  197 + }
  198 +
  199 + return NULL;
184 200 }
tools/perf/util/strlist.h
... ... @@ -11,7 +11,8 @@
11 11  
12 12 struct strlist {
13 13 struct rb_root entries;
14   - bool dupstr;
  14 + unsigned int nr_entries;
  15 + bool dupstr;
15 16 };
16 17  
17 18 struct strlist *strlist__new(bool dupstr, const char *slist);
18 19  
... ... @@ -21,11 +22,17 @@
21 22 int strlist__load(struct strlist *self, const char *filename);
22 23 int strlist__add(struct strlist *self, const char *str);
23 24  
  25 +struct str_node *strlist__entry(const struct strlist *self, unsigned int idx);
24 26 bool strlist__has_entry(struct strlist *self, const char *entry);
25 27  
26 28 static inline bool strlist__empty(const struct strlist *self)
27 29 {
28   - return rb_first(&self->entries) == NULL;
  30 + return self->nr_entries == 0;
  31 +}
  32 +
  33 +static inline unsigned int strlist__nr_entries(const struct strlist *self)
  34 +{
  35 + return self->nr_entries;
29 36 }
30 37  
31 38 int strlist__parse_list(struct strlist *self, const char *s);