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

perf cpumap: Add intersect function

The merge function gives the union of two cpu maps. Add an intersect
function which is necessary, for example, when intersecting a PMUs
supported CPUs with user requested.

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/20230526215410.2435674-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
237d41d4 6ac2230b

+80
+35
tools/lib/perf/cpumap.c
··· 402 402 perf_cpu_map__put(orig); 403 403 return merged; 404 404 } 405 + 406 + struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig, 407 + struct perf_cpu_map *other) 408 + { 409 + struct perf_cpu *tmp_cpus; 410 + int tmp_len; 411 + int i, j, k; 412 + struct perf_cpu_map *merged = NULL; 413 + 414 + if (perf_cpu_map__is_subset(other, orig)) 415 + return perf_cpu_map__get(orig); 416 + if (perf_cpu_map__is_subset(orig, other)) 417 + return perf_cpu_map__get(other); 418 + 419 + tmp_len = max(orig->nr, other->nr); 420 + tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu)); 421 + if (!tmp_cpus) 422 + return NULL; 423 + 424 + i = j = k = 0; 425 + while (i < orig->nr && j < other->nr) { 426 + if (orig->map[i].cpu < other->map[j].cpu) 427 + i++; 428 + else if (orig->map[i].cpu > other->map[j].cpu) 429 + j++; 430 + else { 431 + j++; 432 + tmp_cpus[k++] = orig->map[i++]; 433 + } 434 + } 435 + if (k) 436 + merged = cpu_map__trim_new(k, tmp_cpus); 437 + free(tmp_cpus); 438 + return merged; 439 + }
+2
tools/lib/perf/include/perf/cpumap.h
··· 25 25 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map); 26 26 LIBPERF_API struct perf_cpu_map *perf_cpu_map__merge(struct perf_cpu_map *orig, 27 27 struct perf_cpu_map *other); 28 + LIBPERF_API struct perf_cpu_map *perf_cpu_map__intersect(struct perf_cpu_map *orig, 29 + struct perf_cpu_map *other); 28 30 LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map); 29 31 LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx); 30 32 LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus);
+1
tools/perf/tests/builtin-test.c
··· 97 97 &suite__backward_ring_buffer, 98 98 &suite__cpu_map_print, 99 99 &suite__cpu_map_merge, 100 + &suite__cpu_map_intersect, 100 101 &suite__sdt_event, 101 102 &suite__is_printable_array, 102 103 &suite__bitmap_print,
+41
tools/perf/tests/cpumap.c
··· 171 171 return 0; 172 172 } 173 173 174 + static int __test__cpu_map_intersect(const char *lhs, const char *rhs, int nr, const char *expected) 175 + { 176 + struct perf_cpu_map *a = perf_cpu_map__new(lhs); 177 + struct perf_cpu_map *b = perf_cpu_map__new(rhs); 178 + struct perf_cpu_map *c = perf_cpu_map__intersect(a, b); 179 + char buf[100]; 180 + 181 + TEST_ASSERT_EQUAL("failed to intersect map: bad nr", perf_cpu_map__nr(c), nr); 182 + cpu_map__snprint(c, buf, sizeof(buf)); 183 + TEST_ASSERT_VAL("failed to intersect map: bad result", !strcmp(buf, expected)); 184 + perf_cpu_map__put(a); 185 + perf_cpu_map__put(b); 186 + perf_cpu_map__put(c); 187 + return 0; 188 + } 189 + 190 + static int test__cpu_map_intersect(struct test_suite *test __maybe_unused, 191 + int subtest __maybe_unused) 192 + { 193 + int ret; 194 + 195 + ret = __test__cpu_map_intersect("4,2,1", "4,5,7", 1, "4"); 196 + if (ret) 197 + return ret; 198 + ret = __test__cpu_map_intersect("1-8", "6-9", 3, "6-8"); 199 + if (ret) 200 + return ret; 201 + ret = __test__cpu_map_intersect("1-8,12-20", "6-9,15", 4, "6-8,15"); 202 + if (ret) 203 + return ret; 204 + ret = __test__cpu_map_intersect("4,2,1", "1", 1, "1"); 205 + if (ret) 206 + return ret; 207 + ret = __test__cpu_map_intersect("1", "4,2,1", 1, "1"); 208 + if (ret) 209 + return ret; 210 + ret = __test__cpu_map_intersect("1", "1", 1, "1"); 211 + return ret; 212 + } 213 + 174 214 DEFINE_SUITE("Synthesize cpu map", cpu_map_synthesize); 175 215 DEFINE_SUITE("Print cpu map", cpu_map_print); 176 216 DEFINE_SUITE("Merge cpu map", cpu_map_merge); 217 + DEFINE_SUITE("Intersect cpu map", cpu_map_intersect);
+1
tools/perf/tests/tests.h
··· 127 127 DECLARE_SUITE(backward_ring_buffer); 128 128 DECLARE_SUITE(cpu_map_print); 129 129 DECLARE_SUITE(cpu_map_merge); 130 + DECLARE_SUITE(cpu_map_intersect); 130 131 DECLARE_SUITE(sdt_event); 131 132 DECLARE_SUITE(is_printable_array); 132 133 DECLARE_SUITE(bitmap_print);