Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp814582lqt; Thu, 6 Jun 2024 21:55:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXZ32S3icOXkPYajOu92Oyfq8f4qW6t4YaXjuHSuxw87jCHu508s3jxQ6YwRosgiOFMas9lYMQwCQ7mJNlorRsan/5PQmqfZPDvAJGENA== X-Google-Smtp-Source: AGHT+IG11yezPLoyQISRxYDgqYVBDqz09S3+iT/EUrji6nssYWXIsp4rs3+0nzA5s/nIUy82RLvZ X-Received: by 2002:a17:906:5803:b0:a6e:372c:cc5e with SMTP id a640c23a62f3a-a6e372cd0c8mr10460466b.61.1717736126461; Thu, 06 Jun 2024 21:55:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717736126; cv=pass; d=google.com; s=arc-20160816; b=yfWkQP2/uBIHola2lCiaDpgCjUh4D99BSPiTNIzuoYXKXJME4ixtqxdxY8YdYMiWEM sbo9hdLrSk6N1kB0YVEKgdD99/yZzqfG6ofWNjX9XRB2nA6tqz5MxhnYUy+Ou5897ZfV b8CxIZFVw1clEyzYFWCJk0UrVtMs8esNxcKXjQsnyPm3aqWYjZAt5vRpawVJTNY8AwEt XnyPPA/2rWvywZA6AGJK+cJKiV5t62owSLHgtdYO/8KZaUAa59CpiFfD8zZ8wpiNiYRp sJLdaOK5XnuH+eMzkXbbyLysOfBxcfs5K40HxoGRXFU2bqAbz/xZMzHVe6c7mZObTYHD FrRg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=vUQKwpPhBYleEuurbsht3zzd+kMQUMmu39PgM4r1UfU=; fh=YlDOFLcCQeJB8AE8DrcRhgCBxRvnOo2kQ4LXqhSShyY=; b=XLLoiLJq7E4+Q8xSEfzOXhLSnNJ0Omo8QgtrjMwVfTCVuMfpHK5vJgcVfk6Nb8vkGv 1Q8+k1BKDBUnOezRl6oo5D6hYCOFChh5rDJkiiTF/EOhHkVzk8cLdB9toF9aj6aAvzoT 5V7Z+3lzwcRpShakqz/tMFTXLeV93R/p3fQbI5ju0rapLNQEnABRy/BWGLE4CKwnb/5y uPlf+yXH0r/VDjM4YNyfYrfuM5X8/ZgEYd77OmSLatFw7AypJIjvZJdDz/bWIiRirV7h DWmaWuRO4pJJPfmk+LSr9cTcosKIZvAXu9D+Hc0OhNsQuEu8Du3gHDazOo9rBtnXxVXM toBw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ue60vCKl; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-205393-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205393-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a6c80588413si144134866b.197.2024.06.06.21.55.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 21:55:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-205393-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ue60vCKl; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-205393-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-205393-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 01B441F26035 for ; Fri, 7 Jun 2024 04:55:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8BB2816D33C; Fri, 7 Jun 2024 04:31:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ue60vCKl" Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E47B14F128 for ; Fri, 7 Jun 2024 04:31:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734704; cv=none; b=LR4kZ5RvAZA/GUKlPYap0Aj1uNnX3pQGKWeDyi1/nJvc1/X8T1lhad2vOuM+PGn+CwRv4/d0dOElvvn+lM8iWWtM2rtOILMkLvf21IcIOu3lP3YgJAaVrIkiVRAnTfvhTjR0xGW4LGKmhkzFEkDLIlj+PRe65kxj2cbax4Yxi8Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717734704; c=relaxed/simple; bh=m+FO5prOuAQRG/TGAPSbvyZvd0BG/DyRE8f9rNYUEZ0=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=s3Ncu6IHiEixhFFub/QK+MJ3VLb4bE9sPXcb48ugunPrnq7U17ZahwnsMqFR1Lnu8cni6EU7fhxevy70uearvfQmDcQv5yw81QLeSNKRJu2DJsUMuocE8NGdhauhT/HG9nCfDeLi1Ic4hSdsFfIRRI6IWD9fya2I9GhM9ahKR6U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ue60vCKl; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1ee5f3123d8so60295ad.1 for ; Thu, 06 Jun 2024 21:31:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717734701; x=1718339501; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=vUQKwpPhBYleEuurbsht3zzd+kMQUMmu39PgM4r1UfU=; b=Ue60vCKlT8fsHBn+sSbaPvUPdAqJX/CUvqdRxg77iQy6f/bmNqQ7rF7zqplgGfsbGw ehMorhMI1q5YRGCJnG7HD/EwTRoqwmi+cv9tL36HB3Y8Say8kPWkVnlwdYj9dGmVbMFh p80LaL8zQRIQZd7m94vOC0nuw7uIZ9idZzNxq6G+HrmAA4s5tIiliWO7yOZRiO6ydwTw 32HfvOWJbIliqjEkd1EmasU2SrUjak3Otg2Ob4PSRClO0w7t2CHw2bWIUJA4ZEkPku8Z WM2EsZaiNEZj6jI9RBOxv5aXe5xUt7mqhsCliU4Zf6xoXJ5zNLiJAInXxcEGMJJvRihS QlMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717734701; x=1718339501; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vUQKwpPhBYleEuurbsht3zzd+kMQUMmu39PgM4r1UfU=; b=aFJJZzdoBiEiMb7m5tGy+TJnx7Z7WNH/1zikJYI07YKI9p3vaPpscJMqssmubrbQVS +aLvTcReMv9wDJfHsmWH+IwhbeGzYzsgxdFR5Vou3DAApZ/GqdbV/kScy+gZpYxh+czB JR00/454LH/KXPIGZErxWESJwWw7G1inrbnYhr0b+xnY2jlARXsirkGnKVYwjW87eOSb ASS860hZ0ie7JoeCTqzV8FlnYozCsRhMvfaswDLd21ioO8TEaJX5vlyx5p0p0bSbYH37 qCK4L2mIjMSv2XwY5OtNw11yB+OA6QZP8IxVdrODlGdDnEYxCqkUiqnfYw0tqTy4cNKg 5tZQ== X-Forwarded-Encrypted: i=1; AJvYcCVAkIwefgciTcZFLYII0StIFTBmDk+gCan3eRbb/dW1l9X8uBT/hpeayh265x0ONc+SmGcIQvZb0JWbn5S9tzec7+iwdQ3p/dVTVt4w X-Gm-Message-State: AOJu0YzkCC5ePAYXZu1pCAjkEqY34yjKvjOAVffHcBHfKmPkBJUrfx35 +ZPiqyy2TAI3+1BdZ4CvsvaCt4wFRsPwyXY3sI+Lwl/phqt8nqGMOw1AD7tikhlOVJDEjw/T+62 gDMZa4cJAJMiV5YIt/WrDF/HP4PX5lkVsRsjB X-Received: by 2002:a17:902:c402:b0:1e5:62:7a89 with SMTP id d9443c01a7336-1f6ba64b814mr6213245ad.18.1717734700474; Thu, 06 Jun 2024 21:31:40 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240521165109.708593-1-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Thu, 6 Jun 2024 21:31:28 -0700 Message-ID: Subject: Re: [PATCH v1 0/3] Fix and improve __maps__fixup_overlap_and_insert To: James Clark Cc: "Steinar H . Gunderson" , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jun 6, 2024 at 3:56=E2=80=AFAM James Clark wr= ote: > > > > On 21/05/2024 17:51, Ian Rogers wrote: > > Fix latent unlikely bugs in __maps__fixup_overlap_and_insert. > > > > Improve __maps__fixup_overlap_and_insert's performance 21x in the case > > of overlapping mmaps. sesse@google.com reported slowness opening > > perf.data files from chromium where the files contained a large number > > of overlapping mappings. Improve this case primarily by avoiding > > unnecessary sorting. > > > > Unscientific timing data processing a perf.data file with overlapping > > mmap events from chromium: > > > > Before: > > real 0m9.856s > > user 0m9.637s > > sys 0m0.204s > > > > After: > > real 0m0.675s > > user 0m0.454s > > sys 0m0.196s > > > > Tested with address/leak sanitizer, invariant checks and validating > > the before and after output are identical. > > > > Ian Rogers (3): > > perf maps: Fix use after free in __maps__fixup_overlap_and_insert > > perf maps: Reduce sorting for overlapping mappings > > perf maps: Add/use a sorted insert for fixup overlap and insert > > > > tools/perf/util/maps.c | 113 +++++++++++++++++++++++++++++++++-------- > > 1 file changed, 92 insertions(+), 21 deletions(-) > > > > Reviewed-by: James Clark > > I'm wondering if there is any point in the non sorted insert any more? > > Maps could be always either sorted by name or sorted by address and > insert() is always a sorted/fixup-overlaps insert depending on the sort > style of the list. One of the motivations for the sorted array, instead of an rbtree, was to simplify reference count checking. We're in much better shape with that these days, I think the worst "memory leak" is the dsos holding onto a dso and its symbols indefinitely, instead of removing older unused dsos. It'd be hard to go back to an rb tree with reference counting. For the rbtree insert it was O(log N), the sorted insert these changes add is O(N) and the regular insert without sorting is O(1). A common case from scanning /proc/pid/maps is that when map entries are inserted they are inserted in order. The O(1) insert checks that and maintains that the maps are sorted. https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tr= ee/tools/perf/util/maps.c?h=3Dperf-tools-next#n469 For the synthesized maps we should be able to insert N map entries in O(N). The sorted insert would be similar as the memmoves would always copy 0 elements. So I can see an argument for keeping the array always sorted. For the perf.data files we commonly process synthesized mmaps dominate. In the case for this patch, JIT data dominated, with frequent overlapping mapping changes. I guess the biggest hurdle to just always keeping things sorted is the mental hurdle of ignoring the worst insert complexity, which should never apply given how the maps are commonly built. Thanks, Ian