From patchwork Wed May 24 22:17:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 13254637 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 00040C77B7C for ; Wed, 24 May 2023 23:20:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version :Message-Id:In-Reply-To:Date:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PrzafBJ06vsyMoXC/ilYFiDIuhhleNtFNMlZKSGqgqo=; b=uGzR4HDyouCFus E/zHdkaIQ1TYIMOdBk/RQIqz1rRXTTnYeKQRtHoTMWZnzLAETL9QyNKPIZuLJXBXcSL98Y85yYK8L Df6+QczcT/HLMgUxoSCIwlL/vTLTeArMAuINquUp8zNuYDVEbcWJdeM86sRsKGbekFhxFBXZUxP1+ WAXSfml+tOv6rgQyqSaiWlpIaMO9+Tu0uRRegENa2PBtRobkntVaDZNkPZix+PH+hfQpTsdjauBNu dw+GutLv9yRRWKnyB3nFtyoAXWUKt8GmJGrHafdOzjTr4kzDTWv82LHpMvtBbUkjRMqP0IWEOZ+LF ejypUjcFWI1ho3AT0XYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q1xmE-00Ev6Z-0Q; Wed, 24 May 2023 23:20:18 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q1wod-00Emim-19 for linux-arm-kernel@lists.infradead.org; Wed, 24 May 2023 22:18:44 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-ba81b37d9d2so2863582276.3 for ; Wed, 24 May 2023 15:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684966722; x=1687558722; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=xAoaFyBJOeyjHfZvGABM9yDThQmXliV0lqnBHOLa6q0=; b=zlbh0StDHb0NXfGfXg/X3LfsmeDzOHLH1YC1qEfX6cRN7wrLa7RBRNYltOVYHQZznF Qj6tcRduEffZVSuvM+NkKpIzs2/Yn/1/v2OXQHBbzm/KyggfjUhzB3z07p5R7onauVA4 BzzwVCRgEtBeKzsW7mXUpaSM5tYjehgTBXO+RvfETWvvcrWvHyiCk1xmfjFA4tG9OXVy vqnu09Hp/mrjZd+mt28IXuvqkdyd+8dV2AlzFpGd4QAToETp/lnxRQrJ3dkeh5sSJRG6 pOrHodyD/8xM3NQnw0fnY6Ff97Ye2bV9guPTrRxmve80oAhJLFbO/Iao7HNgd2Gi80I0 y4sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684966722; x=1687558722; 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=xAoaFyBJOeyjHfZvGABM9yDThQmXliV0lqnBHOLa6q0=; b=h2eEGFM/MZSJ500ED2CWKVanSRu3/UGg6I4v5O2Jns//lUAJbc4g5Vibvaq6EVhIfP I2rxkDLgjg6KEsr/AnyYaOehgIvssi7Wi9U5JZnhnDunBBw+gn+GdYk22gG1WfZ7SNKu ujhMXB0AYgpWtbmrQG5lYOJMlObuCH7TE5B58emF1DJVJw9RM0Gu+/iCDmoW2bnTrAWa AMmyivBVD941prtiDmWlGjFwn/n3u4V6aP3ZZU8WA5gerxKlmdvT/UGr2bZyDxe46FHH w0LVmdcywW4w+V2wQOinCHFjpkyLU5/G/v6UhbB6TrehTk7m2BqBhCoOzPQzcKE7fZc0 G+Zw== X-Gm-Message-State: AC+VfDwdysyGYksmbgn2hQY6Z9KHNc46ELEKCDlsiob8fcfYiwYW8yJc 2rcXruF8QM2ZXOPFluVyUIx62TWIKAU9 X-Google-Smtp-Source: ACHHUZ67iw6irCTpD7UvqWqkTdeLj7Zvgy/Do6A19KOegEO5oEs6WH6+/P6qeq5gp6xRG1thiKj91Wt3B8s7 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:7aa7:3d2d:76ae:8e96]) (user=irogers job=sendgmr) by 2002:a25:3256:0:b0:b8f:53e1:64b with SMTP id y83-20020a253256000000b00b8f53e1064bmr570703yby.13.1684966721885; Wed, 24 May 2023 15:18:41 -0700 (PDT) Date: Wed, 24 May 2023 15:17:59 -0700 In-Reply-To: <20230524221831.1741381-1-irogers@google.com> Message-Id: <20230524221831.1741381-4-irogers@google.com> Mime-Version: 1.0 References: <20230524221831.1741381-1-irogers@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Subject: [PATCH v3 03/35] perf cpumap: Add equal function From: Ian Rogers To: Suzuki K Poulose , Mike Leach , Leo Yan , John Garry , Will Deacon , James Clark , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Kajol Jain , Jing Zhang , Kan Liang , Zhengjun Xing , Ravi Bangoria , Madhavan Srinivasan , Athira Rajeev , Ming Wang , Huacai Chen , Sandipan Das , Dmitrii Dolgov <9erthalion6@gmail.com>, Sean Christopherson , Ali Saidi , Rob Herring , Thomas Richter , Kang Minchul , linux-kernel@vger.kernel.org, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230524_151843_394801_67EFCFFB X-CRM114-Status: GOOD ( 16.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Equality is a useful property to compare after merging and intersecting maps. Signed-off-by: Ian Rogers --- tools/lib/perf/cpumap.c | 21 ++++++++++++++++ tools/lib/perf/include/perf/cpumap.h | 2 ++ tools/perf/tests/cpumap.c | 37 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c index d4f3a1a12522..48595a3ad69c 100644 --- a/tools/lib/perf/cpumap.c +++ b/tools/lib/perf/cpumap.c @@ -321,6 +321,27 @@ bool perf_cpu_map__has(const struct perf_cpu_map *cpus, struct perf_cpu cpu) return perf_cpu_map__idx(cpus, cpu) != -1; } +bool perf_cpu_map__equal(const struct perf_cpu_map *lhs, const struct perf_cpu_map *rhs) +{ + int nr; + + if (lhs == rhs) + return true; + + if (!lhs || !rhs) + return false; + + nr = perf_cpu_map__nr(lhs); + if (nr != perf_cpu_map__nr(rhs)) + return false; + + for (int idx = 0; idx < nr; idx++) { + if (RC_CHK_ACCESS(lhs)->map[idx].cpu != RC_CHK_ACCESS(rhs)->map[idx].cpu) + return false; + } + return true; +} + struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map) { struct perf_cpu result = { diff --git a/tools/lib/perf/include/perf/cpumap.h b/tools/lib/perf/include/perf/cpumap.h index 0466c4216fbb..d0ae9552f8e2 100644 --- a/tools/lib/perf/include/perf/cpumap.h +++ b/tools/lib/perf/include/perf/cpumap.h @@ -28,6 +28,8 @@ LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus); LIBPERF_API bool perf_cpu_map__empty(const struct perf_cpu_map *map); LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map); LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu); +LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs, + const struct perf_cpu_map *rhs); #define perf_cpu_map__for_each_cpu(cpu, idx, cpus) \ for ((idx) = 0, (cpu) = perf_cpu_map__cpu(cpus, idx); \ diff --git a/tools/perf/tests/cpumap.c b/tools/perf/tests/cpumap.c index 83805690c209..7730fc2ab40b 100644 --- a/tools/perf/tests/cpumap.c +++ b/tools/perf/tests/cpumap.c @@ -211,11 +211,48 @@ static int test__cpu_map_intersect(struct test_suite *test __maybe_unused, return ret; } +static int test__cpu_map_equal(struct test_suite *test __maybe_unused, int subtest __maybe_unused) +{ + struct perf_cpu_map *any = perf_cpu_map__dummy_new(); + struct perf_cpu_map *one = perf_cpu_map__new("1"); + struct perf_cpu_map *two = perf_cpu_map__new("2"); + struct perf_cpu_map *empty = perf_cpu_map__intersect(one, two); + struct perf_cpu_map *pair = perf_cpu_map__new("1-2"); + struct perf_cpu_map *tmp; + struct perf_cpu_map *maps[] = {empty, any, one, two, pair}; + + for (size_t i = 0; i < ARRAY_SIZE(maps); i++) { + /* Maps equal themself. */ + TEST_ASSERT_VAL("equal", perf_cpu_map__equal(maps[i], maps[i])); + for (size_t j = 0; j < ARRAY_SIZE(maps); j++) { + /* Maps dont't equal each other. */ + if (i == j) + continue; + TEST_ASSERT_VAL("not equal", !perf_cpu_map__equal(maps[i], maps[j])); + } + } + + /* Maps equal made maps. */ + tmp = perf_cpu_map__merge(perf_cpu_map__get(one), two); + TEST_ASSERT_VAL("pair", perf_cpu_map__equal(pair, tmp)); + perf_cpu_map__put(tmp); + + tmp = perf_cpu_map__intersect(pair, one); + TEST_ASSERT_VAL("one", perf_cpu_map__equal(one, tmp)); + perf_cpu_map__put(tmp); + + for (size_t i = 0; i < ARRAY_SIZE(maps); i++) + perf_cpu_map__put(maps[i]); + + return TEST_OK; +} + static struct test_case tests__cpu_map[] = { TEST_CASE("Synthesize cpu map", cpu_map_synthesize), TEST_CASE("Print cpu map", cpu_map_print), TEST_CASE("Merge cpu map", cpu_map_merge), TEST_CASE("Intersect cpu map", cpu_map_intersect), + TEST_CASE("Equal cpu map", cpu_map_equal), { .name = NULL, } };