Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp554023rdb; Thu, 2 Nov 2023 11:00:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGRAGh3AEYm4l42yaDLz2S3JYOjokGAe6sQAn417kg1whJhJAGu6GWUZDhq216aTQCfYPvE X-Received: by 2002:a05:6a21:7906:b0:181:bc16:48f0 with SMTP id bg6-20020a056a21790600b00181bc1648f0mr1659880pzc.32.1698948008856; Thu, 02 Nov 2023 11:00:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948008; cv=none; d=google.com; s=arc-20160816; b=OoSDhYNNTXNb2OUewm6XzWQyUh3I6/SVXyjzrWbxBEekk7jsDvbqEs3WX8obkM2Yd7 jx6T0upeD2lrqx2wvxpavDSmeKS7EvPvzd4ypQgYcsKtWj2oQeUbwi3/RGQh5hG9xJn0 f2QXWJirYLH+ZQDJ8HdR70dVFSq90A8JH6I/srkT/MchZs1G0SbnZeQy5mkOdoHpm52d JiH8vzsG6l4VcG3z5sZXLPaEhHeVv/7fJFryMVZeqkiVN/M+NZJ7HkVyptE2f5BGnXKF M6ZB4upJbp0P3P5jTMJ65bcSrSXo53OZeTEICrm5lhfswwZStDtDk90EkPDrzj7eIpU+ FThw== 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=5jQubpFxVympzrNhfbd67emKxgsvnRdgi7XbNym7SkQ=; fh=z1KcSqUpYQ9oC4uLSeIkhAYTnJ2bvP0QbNW2xqV5NqA=; b=EocSm/NCvK0JbTtwrcVZnnQlmE5LkCHJNpjTWCzUbsDEIWW5VtE73J+ONyr8CIw6vJ E/lPmAjwRT9BnwyeNUvVRUKx+0IYHYXnAMWsKMiRxyhC+gKoq1Flc7X+aB1NcfUyJ8um CkqfwUQJHdJFWlyxY65u5uIzV5s0vIJehtcqVBRI2xabho39/vaXTfm+ezUcDvDeAODm pxSAQ9USec8T0IGZ++siJesZw5szx4HuRLpmAhPPUeSV4a6CyUaCLfJLF9JanpiUiHau YpsRydMuGX7jRwq/X2fDNVD0qUKA4SKrX9iQ+0hh/PPsEjaT9npnnj6/le+ZMeaZ/uvk epgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PiJ9YCaT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id u22-20020aa78496000000b006be15fece6csi59778pfn.360.2023.11.02.11.00.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:00:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=PiJ9YCaT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 51B0B82B6F74; Thu, 2 Nov 2023 11:00:07 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345648AbjKBSAA (ORCPT + 99 others); Thu, 2 Nov 2023 14:00:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345900AbjKBR7K (ORCPT ); Thu, 2 Nov 2023 13:59:10 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DC451734 for ; Thu, 2 Nov 2023 10:58:45 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5afa071d100so25595437b3.1 for ; Thu, 02 Nov 2023 10:58:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698947924; x=1699552724; 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=5jQubpFxVympzrNhfbd67emKxgsvnRdgi7XbNym7SkQ=; b=PiJ9YCaTy7qZAZM4R7B5ef+vNqEekvkb0PV+/Zonba/2GeU/eC5XYED6Uwp0rGcspH CkZxHb2k+qVkMglWdOTjVho7KZmerCTmiea6k9ulrd7LI/79YOWz0KdyT5JtSHwLP7Go dKUpcDGkE9ye+Rg70ItVUsW6m0FhW6qTPe1dYYMcPXRR8ucqvfe3DoJYzgkIKiYGmp1H 0qgiIpdvY06IMH8mD3vEeaKxDSaiqUaBnUmPkXnAXsvx6kDe2hrnVWy43TD7FG57yDi9 tFj6D7wxRDuS4zFC31dRBepJ8sO11fJlgs50IdM1L0FadSRL4veY4bpqA4LvV6MkVFZs Z2ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698947924; x=1699552724; 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=5jQubpFxVympzrNhfbd67emKxgsvnRdgi7XbNym7SkQ=; b=hG8mBGhb2x1UmtpP3fmPNC/grJm6xzsRvjzPJfD/zptRCR2KZk0ayrE93yTzD9efmK 1xeyYYez50U42RZfg9VTG4QqReem4zH2NeeOWVqImgbafmXQb6yYrfNUXO8lFTcdztsV HqPR1j1ifDe8RjE+vde5PBSKsYR+1IC3BKBj5Ah89Nne1KXIKo532ta9pa7Fv4yyc9qH zs22oIjatMKbtTmxPqq5Jjgc9kOJVk5Fas8tos+0ukTg0AuaWguK29P69i1Pfwxlmy6A LMsp2l79Hqck7cck2MwNI6Trgh2Lfwms0emsZDw+9PYYmeNWDGQDefDS9hmTgwf560DF q3rw== X-Gm-Message-State: AOJu0Yx+VX5yGIEOgc3y+N/2U1RYfJoiKcugzyXE2x92xZfUgPKZJSuL 810v53COVWcWwDhfQw4CCE3XJenWzxs7 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:bb34:df9c:836c:afca]) (user=irogers job=sendgmr) by 2002:a25:ade4:0:b0:d9a:ff08:e090 with SMTP id d36-20020a25ade4000000b00d9aff08e090mr346879ybe.5.1698947924611; Thu, 02 Nov 2023 10:58:44 -0700 (PDT) Date: Thu, 2 Nov 2023 10:57:03 -0700 In-Reply-To: <20231102175735.2272696-1-irogers@google.com> Message-Id: <20231102175735.2272696-22-irogers@google.com> Mime-Version: 1.0 References: <20231102175735.2272696-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Subject: [PATCH v4 21/53] 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 , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:00:07 -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 b6986a81aa6d..3d47b5c5528b 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.869.gea05f2083d-goog