Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2185789rda; Tue, 24 Oct 2023 15:26:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3/HMFblJcc7kOO+KM3YuogBZH1nB0SqPLiaWnCpQPPI+POspNiuiawHuRTu3R7haf+Wne X-Received: by 2002:a17:902:c401:b0:1bc:5924:2da2 with SMTP id k1-20020a170902c40100b001bc59242da2mr15573378plk.56.1698186411487; Tue, 24 Oct 2023 15:26:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186411; cv=none; d=google.com; s=arc-20160816; b=Jl1uDAMjg7kwvAnBhm2HYsTMau/Vf2yACWounEGT8SZqjAoL9+LLSBAe1+ro9isHdm /BllS9c/StqjL+EogdVBHmJXdskbrXP71gm2QXcZchVZ8MNnRPg30gQvW7BkTyDNN/Da QfJiiDATc80oPvs84FtByQQHrNMCs+FtzWyX+/m6CnU1VPnE5PrdA7W/EcshVKYtxi2d hsaNWn5Qulq2V60x/IqGv7OUZ6qkg1P02qN2wzzvEvs8xfuOj42lbW20YXHP+Sh4PBLn 2l2e2FNH21YQrKmSvYeDq8zs4y7Ra7o87HaRZBcZy/S/QstP99+PQkuK9RDQozUewfNy raQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=8rha7reTjPPM+nzq3LN9RJmw1LRSN6MIopdCvqO71wM=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=l33aCWULAsb93IohBzQf+2cZViJS1h01+6JRoOFFPmTJQYnBvHqvmlHaiwUcdOawXb invMg0EvbRrAUnQFaooJ29PyHyV7y036NWXhNfVGQAj1fvE4RB2BqDsj3EQ9fWIddiN3 oKOcTLaGTMkfaYq4ZK8SfsSmdIhMMZdkMZEpHEI4MFkztA1eO5GzigRnt40ekjq3BbYK BGpVin/gRM2C83P9pH8b/Q1AvCvWTQdVeC8+oVhNZ5ynwBIvqDBivHjRjNr/FguJ544r IEUPT/ZEpNZUzT4rml9cBpxIOSlOI1pcKETuTO2ziSZZXHXODrv2CpqUxGYz4bo9MBPO vQ7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ost0D5Om; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id p6-20020a170902e74600b001c752a540cdsi9116593plf.145.2023.10.24.15.26.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:26:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ost0D5Om; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 6277780219C5; Tue, 24 Oct 2023 15:26:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344525AbjJXW0g (ORCPT + 99 others); Tue, 24 Oct 2023 18:26:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344518AbjJXWZ6 (ORCPT ); Tue, 24 Oct 2023 18:25:58 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A7BF19BA for ; Tue, 24 Oct 2023 15:25:24 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a8ee6a1801so63563787b3.3 for ; Tue, 24 Oct 2023 15:25:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186323; x=1698791123; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=8rha7reTjPPM+nzq3LN9RJmw1LRSN6MIopdCvqO71wM=; b=Ost0D5OmJOXGh20juf/FD6edHdPvqW/aaozKCA/p7a9Xmt2BL/grhJR1BT1OlnmYkR O1WK7OAbpP165tAHj8f120CT5kA/i8q+nEZRWKoFCc04uD4HghMXSAlPjaQ0OGTPTGYQ oCpIbKcHxFMgp/7H4V/igcOpjmZRz+cQym5q2czRrAHJ/cKAe3ieEmILlVVnKuYVb5um Wq3w1dZ56F5U4hjKxlPqNG7zbr6NV3lWFZJ9dIJMteDATZCd8shzvxMIGBc7jZdWL5Xe Z56LLihOfT4C//k3ursYKY7vVXWApYSBDaeEnq1HEo14yGiHZiq0es1bH52DJizT70Tj Y5YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186323; x=1698791123; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8rha7reTjPPM+nzq3LN9RJmw1LRSN6MIopdCvqO71wM=; b=C/618W/tOuDnhw0oYTT7ELlu++gHuRn7WfaqMQqLgzhwiBvIjWZzmuk+wAI0FyGc7U YsY/ImBfjSOLU/XDloL4hhKu6c2/BRbTOpRdXXjRr0silAAAls7yV+aYjMn9Z/tWMUf8 EtBLDsE3mu7Liu3jBD99xD3DiF2AHgTHMqwnbR5RFy0kn51szfQIPkEbUjObbgd8k2Br 7uFIdBrRg4UabsFb/DTH5gJ7ylEkl6NzO20ssf72oCQ0f9PeQz8G6MjlrT3S5qjlZwY3 epPHpKXgWM99pT9GGltHFUNvp86J3hdO5pmGFEKlNdTiXEtVarq7Yk0BcQSFUe7jzrJO f4/w== X-Gm-Message-State: AOJu0YyjP/ZVmkoSH0qwp5MecYE0dn5vp76sDnTj/YsGHIeKfuIVjUHs 9ZeIX+UvXiz73oeOciLpMuXKJaHa/Oo3 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a25:d05:0:b0:d9a:5b63:a682 with SMTP id 5-20020a250d05000000b00d9a5b63a682mr239523ybn.13.1698186322987; Tue, 24 Oct 2023 15:25:22 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:34 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-32-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 31/50] perf maps: Refactor maps__fixup_overlappings From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:26:47 -0700 (PDT) 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 --- tools/perf/util/maps.c | 62 ++++++++++++++++++++++++---------------- tools/perf/util/maps.h | 2 +- tools/perf/util/thread.c | 3 +- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index f13fd3a9686b..40df08dd9bf3 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -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 > new->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,22 @@ 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; + + 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 +381,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); - map__fprintf(pos->map, fp); + pr_debug("overlapping maps:\n"); + map__fprintf(new, debug_file()); + map__fprintf(pos->map, debug_file()); } } - 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 +409,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); @@ -407,11 +417,11 @@ int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) } if (verbose >= 2 && !use_browser) - map__fprintf(before, fp); + map__fprintf(before, debug_file()); 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,23 +429,25 @@ 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); goto put_map; } if (verbose >= 2 && !use_browser) - map__fprintf(after, fp); + map__fprintf(after, debug_file()); map__put(after); } put_map: map__put(pos->map); free(pos); } + /* Add the map. */ + err = __maps__insert(maps, new); up_write(maps__lock(maps)); return err; } diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index b94ad5c8fea7..62e94d443c02 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -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); diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 2b938ccbfe3e..cab818af6787 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -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 { -- 2.42.0.758.gaed0368e0e-goog