Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf cpumap: Add equal function

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>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
74c075ca 7d1b529f

+60
+21
tools/lib/perf/cpumap.c
··· 335 335 return perf_cpu_map__idx(cpus, cpu) != -1; 336 336 } 337 337 338 + bool perf_cpu_map__equal(const struct perf_cpu_map *lhs, const struct perf_cpu_map *rhs) 339 + { 340 + int nr; 341 + 342 + if (lhs == rhs) 343 + return true; 344 + 345 + if (!lhs || !rhs) 346 + return false; 347 + 348 + nr = __perf_cpu_map__nr(lhs); 349 + if (nr != __perf_cpu_map__nr(rhs)) 350 + return false; 351 + 352 + for (int idx = 0; idx < nr; idx++) { 353 + if (__perf_cpu_map__cpu(lhs, idx).cpu != __perf_cpu_map__cpu(rhs, idx).cpu) 354 + return false; 355 + } 356 + return true; 357 + } 358 + 338 359 struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map) 339 360 { 340 361 struct perf_cpu result = {
+2
tools/lib/perf/include/perf/cpumap.h
··· 33 33 LIBPERF_API bool perf_cpu_map__empty(const struct perf_cpu_map *map); 34 34 LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map); 35 35 LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu); 36 + LIBPERF_API bool perf_cpu_map__equal(const struct perf_cpu_map *lhs, 37 + const struct perf_cpu_map *rhs); 36 38 37 39 #define perf_cpu_map__for_each_cpu(cpu, idx, cpus) \ 38 40 for ((idx) = 0, (cpu) = perf_cpu_map__cpu(cpus, idx); \
+37
tools/perf/tests/cpumap.c
··· 211 211 return ret; 212 212 } 213 213 214 + static int test__cpu_map_equal(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 215 + { 216 + struct perf_cpu_map *any = perf_cpu_map__dummy_new(); 217 + struct perf_cpu_map *one = perf_cpu_map__new("1"); 218 + struct perf_cpu_map *two = perf_cpu_map__new("2"); 219 + struct perf_cpu_map *empty = perf_cpu_map__intersect(one, two); 220 + struct perf_cpu_map *pair = perf_cpu_map__new("1-2"); 221 + struct perf_cpu_map *tmp; 222 + struct perf_cpu_map *maps[] = {empty, any, one, two, pair}; 223 + 224 + for (size_t i = 0; i < ARRAY_SIZE(maps); i++) { 225 + /* Maps equal themself. */ 226 + TEST_ASSERT_VAL("equal", perf_cpu_map__equal(maps[i], maps[i])); 227 + for (size_t j = 0; j < ARRAY_SIZE(maps); j++) { 228 + /* Maps dont't equal each other. */ 229 + if (i == j) 230 + continue; 231 + TEST_ASSERT_VAL("not equal", !perf_cpu_map__equal(maps[i], maps[j])); 232 + } 233 + } 234 + 235 + /* Maps equal made maps. */ 236 + tmp = perf_cpu_map__merge(perf_cpu_map__get(one), two); 237 + TEST_ASSERT_VAL("pair", perf_cpu_map__equal(pair, tmp)); 238 + perf_cpu_map__put(tmp); 239 + 240 + tmp = perf_cpu_map__intersect(pair, one); 241 + TEST_ASSERT_VAL("one", perf_cpu_map__equal(one, tmp)); 242 + perf_cpu_map__put(tmp); 243 + 244 + for (size_t i = 0; i < ARRAY_SIZE(maps); i++) 245 + perf_cpu_map__put(maps[i]); 246 + 247 + return TEST_OK; 248 + } 249 + 214 250 static struct test_case tests__cpu_map[] = { 215 251 TEST_CASE("Synthesize cpu map", cpu_map_synthesize), 216 252 TEST_CASE("Print cpu map", cpu_map_print), 217 253 TEST_CASE("Merge cpu map", cpu_map_merge), 218 254 TEST_CASE("Intersect cpu map", cpu_map_intersect), 255 + TEST_CASE("Equal cpu map", cpu_map_equal), 219 256 { .name = NULL, } 220 257 }; 221 258