It'll manage maps using timestamp so that it can find correct
map/symbol for sample at a certain time.  With this API, it can
maintain overlapping maps in a map_groups.

Cc: Stephane Eranian <eran...@google.com>
Signed-off-by: Namhyung Kim <namhy...@kernel.org>
---
 tools/perf/util/map.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/map.h | 25 ++++++++++++++++++++
 2 files changed, 89 insertions(+)

diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c
index 2034127ac3f0..c0f8933f29f0 100644
--- a/tools/perf/util/map.c
+++ b/tools/perf/util/map.c
@@ -777,6 +777,41 @@ void maps__insert(struct maps *maps, struct map *map)
        pthread_rwlock_unlock(&maps->lock);
 }
 
+static void __maps__insert_by_time(struct maps *maps, struct map *map)
+{
+       struct rb_node **p = &maps->entries.rb_node;
+       struct rb_node *parent = NULL;
+       const u64 ip = map->start;
+       const u64 timestamp = map->timestamp;
+       struct map *m;
+
+       while (*p != NULL) {
+               parent = *p;
+               m = rb_entry(parent, struct map, rb_node);
+               if (ip < m->start)
+                       p = &(*p)->rb_left;
+               else if (ip > m->start)
+                       p = &(*p)->rb_right;
+               else if (timestamp > m->timestamp)
+                       p = &(*p)->rb_left;
+               else if (timestamp < m->timestamp)
+                       p = &(*p)->rb_right;
+               else
+                       BUG_ON(1);
+       }
+
+       rb_link_node(&map->rb_node, parent, p);
+       rb_insert_color(&map->rb_node, &maps->entries);
+       map__get(map);
+}
+
+void maps__insert_by_time(struct maps *maps, struct map *map)
+{
+       pthread_rwlock_wrlock(&maps->lock);
+       __maps__insert_by_time(maps, map);
+       pthread_rwlock_unlock(&maps->lock);
+}
+
 static void __maps__remove(struct maps *maps, struct map *map)
 {
        rb_erase_init(&map->rb_node, &maps->entries);
@@ -815,6 +850,35 @@ out:
        return m;
 }
 
+struct map *maps__find_by_time(struct maps *maps, u64 ip, u64 timestamp)
+{
+       struct rb_node **p;
+       struct rb_node *parent = NULL;
+       struct map *m;
+       struct map *best = NULL;
+
+       pthread_rwlock_rdlock(&maps->lock);
+
+       p = &maps->entries.rb_node;
+       while (*p != NULL) {
+               parent = *p;
+               m = rb_entry(parent, struct map, rb_node);
+               if (ip < m->start)
+                       p = &(*p)->rb_left;
+               else if (ip >= m->end)
+                       p = &(*p)->rb_right;
+               else if (timestamp >= m->timestamp) {
+                       if (!best || best->timestamp < m->timestamp)
+                               best = m;
+                       p = &(*p)->rb_left;
+               } else
+                       p = &(*p)->rb_right;
+       }
+
+       pthread_rwlock_unlock(&maps->lock);
+       return best;
+}
+
 struct map *maps__first(struct maps *maps)
 {
        struct rb_node *first = rb_first(&maps->entries);
diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h
index 858f700ea5a3..41a9a39f1027 100644
--- a/tools/perf/util/map.h
+++ b/tools/perf/util/map.h
@@ -10,6 +10,8 @@
 #include <stdbool.h>
 #include <linux/types.h>
 
+#include "perf.h"  /* for perf_has_index */
+
 enum map_type {
        MAP__FUNCTION = 0,
        MAP__VARIABLE,
@@ -191,8 +193,10 @@ void map__reloc_vmlinux(struct map *map);
 size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
                                  FILE *fp);
 void maps__insert(struct maps *maps, struct map *map);
+void maps__insert_by_time(struct maps *maps, struct map *map);
 void maps__remove(struct maps *maps, struct map *map);
 struct map *maps__find(struct maps *maps, u64 addr);
+struct map *maps__find_by_time(struct maps *maps, u64 addr, u64 timestamp);
 struct map *maps__first(struct maps *maps);
 struct map *map__next(struct map *map);
 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
@@ -212,6 +216,17 @@ static inline void map_groups__insert(struct map_groups 
*mg, struct map *map)
        map->groups = mg;
 }
 
+static inline void map_groups__insert_by_time(struct map_groups *mg,
+                                             struct map *map)
+{
+       if (perf_has_index)
+               maps__insert_by_time(&mg->maps[map->type], map);
+       else
+               maps__insert(&mg->maps[map->type], map);
+
+       map->groups = mg;
+}
+
 static inline void map_groups__remove(struct map_groups *mg, struct map *map)
 {
        maps__remove(&mg->maps[map->type], map);
@@ -223,6 +238,16 @@ static inline struct map *map_groups__find(struct 
map_groups *mg,
        return maps__find(&mg->maps[type], addr);
 }
 
+static inline struct map *map_groups__find_by_time(struct map_groups *mg,
+                                                  enum map_type type, u64 addr,
+                                                  u64 timestamp)
+{
+       if (!perf_has_index)
+               return maps__find(&mg->maps[type], addr);
+
+       return maps__find_by_time(&mg->maps[type], addr, timestamp);
+}
+
 static inline struct map *map_groups__first(struct map_groups *mg,
                                            enum map_type type)
 {
-- 
2.6.0

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to