perf cpumap: Add equal function
authorIan Rogers <irogers@google.com>
Sat, 27 May 2023 07:21:38 +0000 (00:21 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sat, 27 May 2023 12:36:44 +0000 (09:36 -0300)
Equality is a useful property to compare after merging and
intersecting maps.

Reviewed-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ali Saidi <alisaidi@amazon.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kang Minchul <tegongkang@gmail.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Ming Wang <wangming01@loongson.cn>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Rob Herring <robh@kernel.org>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20230527072210.2900565-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/perf/cpumap.c
tools/lib/perf/include/perf/cpumap.h
tools/perf/tests/cpumap.c

index ec3f4ac..98d7cb2 100644 (file)
@@ -335,6 +335,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 (__perf_cpu_map__cpu(lhs, idx).cpu != __perf_cpu_map__cpu(rhs, idx).cpu)
+                       return false;
+       }
+       return true;
+}
+
 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map)
 {
        struct perf_cpu result = {
index b4c9a82..cedfc26 100644 (file)
@@ -33,6 +33,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);   \
index 8380569..7730fc2 100644 (file)
@@ -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, }
 };