Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1669897lqa; Mon, 29 Apr 2024 15:57:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU14mfU7gtQ4WW/bpSIbyPCnNDANXK8YxQrRbKwhqR07h/0uRS9hrZJCsQ9/MSd7ePfdvwnY+/yx4p1eWUVPnOT2pnzobcInghR1ed0qw== X-Google-Smtp-Source: AGHT+IEJMjJqudOiGbsciizlewbcsBvUQy9OCuSu/mbpl3293UHZdCLW+P33aYXUYtahkx5ksRq7 X-Received: by 2002:a50:f690:0:b0:571:e293:a8de with SMTP id d16-20020a50f690000000b00571e293a8demr790806edn.20.1714431468360; Mon, 29 Apr 2024 15:57:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714431468; cv=pass; d=google.com; s=arc-20160816; b=yRqjcYlnYbisJhoPSKw/JTlIYKSqJDGxh3XAGQEa9/0JtHa2zKNyrsgldviSx/c8xe fm590MF7T9m+4jBmLEBQZkMEqxdZw7lpuIaBA317jrS/WX0V9H1/vxtpATK4rPJXyiez 0OQ7LCKNOdjTyx/skAB5cJzc8xhClsUmHZR3nSal15boDKQ8ExV8RRynjA5ybY5Wqs0G f0XpaAgz6FLmMFhXBmmQpmolk5j+/Z+E014uH9C5ucXwiGLXfumRGhNXFcbgz29O+u/x lpVPifmAZWJmPK0rKidCQpv4CiIp1heWJLHXZ4jclOz+7sjx25Db8HigQxoyn5lgkOZs 6pvA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=mpTg/ifm/BEPHqKx6Aw2YbqWMbGK25br2pYO21+I3Fk=; fh=fHhbvXgCev/wpffLVpfaAi8pPyv7hrTyhVPgsZ7HNrs=; b=brbeFgFoYwk3f7ZDjUnpy5N5dxeq9ITHiWUazmDhktvE1/cn66Xyusx4FMWm5+NOVw ESm6kO0JWsJ+IEnF62F3tBH8HSPi/lqw53GhJDxihXAiYpTgucP82P+WDxhaTD0x3kyu /v3WPkNUaczes+FYJSyhbWXoK94uZTWR3NQJ5HY0Yy4rx/O25lYjnTGjWrPumnj/KkIn gfWH/sdIiDKnIMOzGt60H8U9xcAWMwD5wc/QLnS4UEDvoxzOb/Oq8Vsu9fuXZyTzHS19 4iURllDBmKc0mLhPXuh/ImDzTBTXg3jCzeCVIvCykONZKV4XeDP7DqRmWoDyhaeCoWZN cQ0A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FUinoZIO; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163117-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163117-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id h12-20020a50cdcc000000b00571c0ae2ce1si13751230edj.620.2024.04.29.15.57.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Apr 2024 15:57:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163117-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=@kernel.org header.s=k20201202 header.b=FUinoZIO; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163117-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163117-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 0EE7F1F21E6C for ; Mon, 29 Apr 2024 22:57:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 152325A4FF; Mon, 29 Apr 2024 22:57:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FUinoZIO" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C2EF1E888; Mon, 29 Apr 2024 22:57:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714431460; cv=none; b=tV2H1OfK50FMZm0Fn4HsytjLCJ4y61Zj2hve1sjsDM7PZfAITYks3965PhHQO2HdFWKeBBzvnuzEP0rFJ/3127ssV/r1pMkiBYesn1Rt3NX3wW/mU72FSTodIjsxVadNqSX9cpXcTU4ryEDUkvlhyTAKaFsmYyfsM3G7uB54xN0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714431460; c=relaxed/simple; bh=WIYeIyuZMEic4wS0TYk6SanQeD6+R01+KLorHb/LgME=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=d5GDpLqgkWn7dDi7pZVXtTeNYu5N5vLDcjMPFHX2aWRJJpgBNIQsM6aMH23nFw93FEy0bleWqyXA4G+8KHkMnXnBeQfPe2HWTxz8ZkSE2ErwqGHTMUEdvjehvLwW3Sr8a3RMlNz/DbNJ2QuGgynu5oNt1WnQ03LHRA+PuIoyeZ4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FUinoZIO; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66D4BC113CD; Mon, 29 Apr 2024 22:57:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714431459; bh=WIYeIyuZMEic4wS0TYk6SanQeD6+R01+KLorHb/LgME=; h=From:To:Cc:Subject:Date:From; b=FUinoZIOtEWv0U80gEnwGYCB2S4xw8/q9RAT8i2Tr5YapZIIaH9P6tRkDa78gHKAO xPkgYHLhBYnvtqn5uHSDJTsTM/usXe9tDi3vNE6C7/x1Aom5HghDXF89ttbFvJmKeE wjUODox83ePCmtnwHxsxt4Yx3fDiID5RwOd4vswRZHiCutwsRP1MWqkIc54S3NBqLR FEBgt7u3cDh9gNCW1GuFp8mpmZR0njgO/yHE/cUY2NolHLkFaQlICG5wOb/MUtm768 hjAJSIIVops1kh7wnbMwUffHVSC8PHSyt07J+hJekB+RvJAlRY3QdocClZFyR3CgDt UQ7qoP7MIFEJQ== From: Namhyung Kim To: Arnaldo Carvalho de Melo , Ian Rogers , Kan Liang Cc: Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Subject: [PATCH] perf maps: Remove check_invariants() from maps__lock() Date: Mon, 29 Apr 2024 15:57:38 -0700 Message-ID: <20240429225738.1491791-1-namhyung@kernel.org> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit I found that the debug build was a lot slowed down by the maps lock code since it checks the invariants whenever it gets the pointer to the lock. This means it checks twice the invariants before and after the access. Instead, let's move the checking code within the lock area but after any modification and remove it from the read paths. This would remove (more than) the half of the maps lock overhead. The time for perf report with a huge data file (200k+ of MMAP2 events). Non-debug Before After --------- -------- -------- 2m 43s 6m 45s 4m 21s Signed-off-by: Namhyung Kim --- tools/perf/util/maps.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 725300896f38..61eb742d91e3 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -211,11 +211,6 @@ void maps__set_unwind_libunwind_ops(struct maps *maps, const struct unwind_libun static struct rw_semaphore *maps__lock(struct maps *maps) { - /* - * When the lock is acquired or released the maps invariants should - * hold. - */ - check_invariants(maps); return &RC_CHK_ACCESS(maps)->lock; } @@ -358,6 +353,7 @@ static int map__strcmp(const void *a, const void *b) static int maps__sort_by_name(struct maps *maps) { int err = 0; + down_write(maps__lock(maps)); if (!maps__maps_by_name_sorted(maps)) { struct map **maps_by_name = maps__maps_by_name(maps); @@ -384,6 +380,7 @@ static int maps__sort_by_name(struct maps *maps) maps__set_maps_by_name_sorted(maps, true); } } + check_invariants(maps); up_write(maps__lock(maps)); return err; } @@ -502,6 +499,7 @@ int maps__insert(struct maps *maps, struct map *map) down_write(maps__lock(maps)); ret = __maps__insert(maps, map); + check_invariants(maps); up_write(maps__lock(maps)); return ret; } @@ -536,6 +534,7 @@ void maps__remove(struct maps *maps, struct map *map) { down_write(maps__lock(maps)); __maps__remove(maps, map); + check_invariants(maps); up_write(maps__lock(maps)); } @@ -602,6 +601,7 @@ void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data else i++; } + check_invariants(maps); up_write(maps__lock(maps)); } @@ -942,6 +942,8 @@ int maps__copy_from(struct maps *dest, struct maps *parent) map__put(new); } } + check_invariants(dest); + up_read(maps__lock(parent)); up_write(maps__lock(dest)); return err; @@ -1097,6 +1099,7 @@ void maps__fixup_end(struct maps *maps) map__set_end(maps_by_address[n - 1], ~0ULL); RC_CHK_ACCESS(maps)->ends_broken = false; + check_invariants(maps); up_write(maps__lock(maps)); } @@ -1147,6 +1150,8 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map) map__start(kmaps_maps_by_address[first_after_]) >= map__end(new_map)) { /* No overlap so regular insert suffices. */ int ret = __maps__insert(kmaps, new_map); + + check_invariants(kmaps); up_write(maps__lock(kmaps)); return ret; } @@ -1184,6 +1189,7 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map) map__zput(kmaps_maps_by_address[i]); free(kmaps_maps_by_address); + check_invariants(kmaps); up_write(maps__lock(kmaps)); return 0; } -- 2.44.0.769.g3c40516874-goog