From patchwork Thu Nov 2 17:57:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 161021 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp534626vqu; Thu, 2 Nov 2023 11:00:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHevvKpS5r+AXvC/hsVxR/p4CZn1uEj75ZpctI398bemzZPcKy0mlqRuuSpX30OU3+yzfyJ X-Received: by 2002:a05:6358:cc25:b0:168:d382:1446 with SMTP id gx37-20020a056358cc2500b00168d3821446mr18676581rwb.11.1698948045851; Thu, 02 Nov 2023 11:00:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698948045; cv=none; d=google.com; s=arc-20160816; b=UyEY1GfNR0qyFuvcXqespH8EDmdZ59/SHo38Ddc+rhzyj6NRM67CInA3hX5yW9hsbH fvPebtXO53ppnHNYYUvTv6t9l2mbevDp3ZjtnbzfxGnmepz4JMIDSA65s3pJzUWVF1gi laBfb1dYtOFHeJZ8sSdxhw8202gadhCpJkuPBDrB1jz1cu7AoQ1bqGp3rCl/gDGZ8Ejh 58GSOzGRrbL8wmbty8T65h1HqTHqLK3IMqXPb1mFf97gFAuTp5NwXDfB/nDUPYQbGpJX CEJhxPgaOVVkIJJ0hfxY7XWIWSBW9e8E5fLvDRIUmOFzwWAcPNIU4oj1e118rmtyK2kd OTfQ== 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 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id bn10-20020a056a02030a00b00585a5e9a965si77289pgb.161.2023.11.02.11.00.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 11:00:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; 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 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id ADB88820D5D7; Thu, 2 Nov 2023 11:00:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346182AbjKBSAB (ORCPT + 35 others); Thu, 2 Nov 2023 14:00:01 -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 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,T_SCC_BODY_TEXT_LINE, 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Thu, 02 Nov 2023 11:00:35 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781476145606377805 X-GMAIL-MSGID: 1781476145606377805 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 {