perf maps: Refactor maps__fixup_overlappings()
authorIan Rogers <irogers@google.com>
Thu, 7 Dec 2023 01:16:50 +0000 (17:16 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 20 Dec 2023 17:53:41 +0000 (14:53 -0300)
Rename to maps__fixup_overlap_and_insert() as the given mapping is
always inserted. Factor out first_ending_after() as a utility
function. Minor variable name changes. Switch to using debug_file()
rather than passing a debug FILE*.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Colin Ian King <colin.i.king@gmail.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Guilherme Amadio <amadio@gentoo.org>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Li Dong <lidong@vivo.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Ming Wang <wangming01@loongson.cn>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Vincent Whitchurch <vincent.whitchurch@axis.com>
Cc: Wenyu Liu <liuwenyu7@huawei.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20231207011722.1220634-17-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/maps.c
tools/perf/util/maps.h
tools/perf/util/thread.c

index f13fd3a9686b1b7d83c19ebd81e84e16e4044549..94a97923527dfc2c54498a47a72f37b236713428 100644 (file)
@@ -334,20 +334,16 @@ size_t maps__fprintf(struct maps *maps, FILE *fp)
        return args.printed;
 }
 
-int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
+/*
+ * Find first map where end > map->start.
+ * Same as find_vma() in kernel.
+ */
+static struct rb_node *first_ending_after(struct maps *maps, const struct map *map)
 {
        struct rb_root *root;
        struct rb_node *next, *first;
-       int err = 0;
-
-       down_write(maps__lock(maps));
 
        root = maps__entries(maps);
-
-       /*
-        * Find first map where end > map->start.
-        * Same as find_vma() in kernel.
-        */
        next = root->rb_node;
        first = NULL;
        while (next) {
@@ -361,8 +357,23 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
                } else
                        next = next->rb_right;
        }
+       return first;
+}
+
+/*
+ * Adds new to maps, if new overlaps existing entries then the existing maps are
+ * adjusted or removed so that new fits without overlapping any entries.
+ */
+int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new)
+{
+
+       struct rb_node *next;
+       int err = 0;
+       FILE *fp = debug_file();
+
+       down_write(maps__lock(maps));
 
-       next = first;
+       next = first_ending_after(maps, new);
        while (next && !err) {
                struct map_rb_node *pos = rb_entry(next, struct map_rb_node, rb_node);
                next = rb_next(&pos->rb_node);
@@ -371,27 +382,27 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
                 * Stop if current map starts after map->end.
                 * Maps are ordered by start: next will not overlap for sure.
                 */
-               if (map__start(pos->map) >= map__end(map))
+               if (map__start(pos->map) >= map__end(new))
                        break;
 
                if (verbose >= 2) {
 
                        if (use_browser) {
                                pr_debug("overlapping maps in %s (disable tui for more info)\n",
-                                        map__dso(map)->name);
+                                        map__dso(new)->name);
                        } else {
-                               fputs("overlapping maps:\n", fp);
-                               map__fprintf(map, fp);
+                               pr_debug("overlapping maps:\n");
+                               map__fprintf(new, fp);
                                map__fprintf(pos->map, fp);
                        }
                }
 
-               rb_erase_init(&pos->rb_node, root);
+               rb_erase_init(&pos->rb_node, maps__entries(maps));
                /*
                 * Now check if we need to create new maps for areas not
                 * overlapped by the new map:
                 */
-               if (map__start(map) > map__start(pos->map)) {
+               if (map__start(new) > map__start(pos->map)) {
                        struct map *before = map__clone(pos->map);
 
                        if (before == NULL) {
@@ -399,7 +410,7 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
                                goto put_map;
                        }
 
-                       map__set_end(before, map__start(map));
+                       map__set_end(before, map__start(new));
                        err = __maps__insert(maps, before);
                        if (err) {
                                map__put(before);
@@ -411,7 +422,7 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
                        map__put(before);
                }
 
-               if (map__end(map) < map__end(pos->map)) {
+               if (map__end(new) < map__end(pos->map)) {
                        struct map *after = map__clone(pos->map);
 
                        if (after == NULL) {
@@ -419,10 +430,10 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
                                goto put_map;
                        }
 
-                       map__set_start(after, map__end(map));
-                       map__add_pgoff(after, map__end(map) - map__start(pos->map));
-                       assert(map__map_ip(pos->map, map__end(map)) ==
-                               map__map_ip(after, map__end(map)));
+                       map__set_start(after, map__end(new));
+                       map__add_pgoff(after, map__end(new) - map__start(pos->map));
+                       assert(map__map_ip(pos->map, map__end(new)) ==
+                               map__map_ip(after, map__end(new)));
                        err = __maps__insert(maps, after);
                        if (err) {
                                map__put(after);
@@ -436,6 +447,8 @@ put_map:
                map__put(pos->map);
                free(pos);
        }
+       /* Add the map. */
+       err = __maps__insert(maps, new);
        up_write(maps__lock(maps));
        return err;
 }
index b94ad5c8fea743817c84bafe6b95594a1f55ea47..62e94d443c029465a78e3abbb98e8c1203f69e94 100644 (file)
@@ -133,7 +133,7 @@ struct addr_map_symbol;
 
 int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams);
 
-int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp);
+int maps__fixup_overlap_and_insert(struct maps *maps, struct map *new);
 
 struct map *maps__find_by_name(struct maps *maps, const char *name);
 
index b6986a81aa6dfa80a42a5d69d8c5c3956a685686..3d47b5c5528b2617d4fbb98e7f7eaf495dc19005 100644 (file)
@@ -345,8 +345,7 @@ int thread__insert_map(struct thread *thread, struct map *map)
        if (ret)
                return ret;
 
-       maps__fixup_overlappings(thread__maps(thread), map, stderr);
-       return maps__insert(thread__maps(thread), map);
+       return maps__fixup_overlap_and_insert(thread__maps(thread), map);
 }
 
 struct thread__prepare_access_maps_cb_args {