Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3428141rdh; Mon, 27 Nov 2023 14:11:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IHdUR65fH4rJiwRnH6Bzn6a7ko7caN5vLIz3nI+JveP7f7JadOEOAXY3BDLrG/SMBE5l5YJ X-Received: by 2002:a05:6808:1b25:b0:3b5:75ad:5b73 with SMTP id bx37-20020a0568081b2500b003b575ad5b73mr17922013oib.13.1701123099774; Mon, 27 Nov 2023 14:11:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123099; cv=none; d=google.com; s=arc-20160816; b=p9Gm+fpJFNXXMpX9v9A+HPXUBBzrFqBUt59LNA/EiOY80A7qP+VGXDd+KLG6Gc/uTl MQWJ0lz+dkYoEV0N1jDWSJq8ZV3fbXV9gDnkO6PyuZah7gN6zecN4Dn/wGskWXD5luet KOn0flX2QTwsfq+QJ/ErvtfivisaQ3P2B8Djqv/cz4ZWntPPtEHkMho1OayuXxOR33zQ R+ftohzRs70sWFsOeMnBMR6W1or8WGamds9sj9YNgi8LZ4wY3ibE/vnTICfP6tOpqPfh M6Xl14xEh3Rxu6ILxNtlsSIX8riszF2Lz8woq7kJVVMV4bLl1M0rkONf9F3crJRhd4yD BWwQ== 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=kx7LDUS3rY7lBdLFklcEVdSsIMEbcaAgKdOgd/PIIs0=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=WKXeqfrPrsyuSUKQm7jzztOnJjrqYgYE5b6UPuC1WZnU8eXe2iTySG5tkvw+BvFj9R /pYUgn2mXJEOQPwxIM5f3DaKggOrzq4kmbBRVNyJysrWIpG8MsI1NbLvaixkp8uqNhyA QhtPouFGwFhixrPZqwXBdwNXor2ClfKVj9fkqqVHiTVVa+LeXmMhQEeMgZy11EP4Gk6C 2xDagxgTIc4wAUYi0A6UlKw2GjwU4XhAKvRAunx+MeUzlwbRydvAtfKVxk2ylj5y/W/0 /OzSIzFjJZb3BUhiCfNgJGAhYrghlD7h3RZLBEazchzRgB8RpKckJYz/NveJrr4228q3 gKRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=iITNyCpb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id e15-20020a63db0f000000b0057d08dac75csi10269338pgg.517.2023.11.27.14.11.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:11:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=iITNyCpb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id DBAAA8087242; Mon, 27 Nov 2023 14:11:36 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233856AbjK0WK7 (ORCPT + 99 others); Mon, 27 Nov 2023 17:10:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233727AbjK0WKY (ORCPT ); Mon, 27 Nov 2023 17:10:24 -0500 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 CFC6619AA for ; Mon, 27 Nov 2023 14:10:06 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d064f9e2a1so18010797b3.1 for ; Mon, 27 Nov 2023 14:10:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123005; x=1701727805; 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=kx7LDUS3rY7lBdLFklcEVdSsIMEbcaAgKdOgd/PIIs0=; b=iITNyCpb6bqpFVm0KKOdbt3O5WJ2wmRho7nr+iO8qkwzge4HoVjJNoFCT6XG/0+reN PblzarBbgt8eNdL5UFxn+NS7PX4jqQmwMQcdVRdSk+1a/VXIxndWciVS9cWeTgV6gSE7 K1EooAsWV3qTLkwkt3jsED8GVyHRMxDBSPJc8eOWgj7/i/E3rc5PJe8ux5fkFfuZ4ozp zCt2h5NyeMe9WzBUzLLc74qywRub+YrjwE8hhsquXTOoUDOB2E96Q2uCLuwQZuXUnpBV PpObH8j+ijYa/qQk/n+MrXDb+yPXrjb6nWyJeVgyQIv2FyPFuTMC9PtTrI/30oCorg+C wakw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123005; x=1701727805; 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=kx7LDUS3rY7lBdLFklcEVdSsIMEbcaAgKdOgd/PIIs0=; b=HFEgDozG8S5RG6Zkk0cluIQdKKunJgiZBf4A+Q+onUDMAAFVtiwoA3rxWu4yBtLUJx Md6VLXjD8ny/w9URRANKd1w/WWdlLRZxpbEO/5rFnzMIo5bzQBof5jgrFPYxYtLc3mWt 5BZMtsfvPTQC2txRujsS5yt+qCu2bxvY5v/3CVp3Xs++hHsQcuo6RKEIzB3Q99362cPy fjL86YaFmN+maLxtgRu8R5hDHfc48h1jB9mOYeBVOWvJ9fdNH/LaMr41yAHwqlYRqvOP 3/SGq+MnZB2P9tpXI0ONDpssesmmMcGh6t1sH0+f2rehq3D6nPaKRckLHCD0zhJSHObJ uyvg== X-Gm-Message-State: AOJu0YyLutJm39ZYjTbzyqsLDfTY79nQm+SF1CF2vcCHfItHIOvQFGTV GehcxbGCsObVqzSUt7caFkwGY9oJipPX X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a81:ad23:0:b0:5be:a336:4a6 with SMTP id l35-20020a81ad23000000b005bea33604a6mr478251ywh.3.1701123005663; Mon, 27 Nov 2023 14:10:05 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:30 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-19-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 18/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 , 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, Guilherme Amadio 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,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 agentk.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 (agentk.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:11:37 -0800 (PST) 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.43.0.rc1.413.gea7ed67945-goog