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

Configure Feed

Select the types of activity you want to include in your feed.

libperf cpumap: Rename perf_cpu_map__default_new() to perf_cpu_map__new_online_cpus() and prefer sysfs

Rename perf_cpu_map__default_new() to perf_cpu_map__new_online_cpus() to
better indicate what the implementation does.

Read the online CPUs from /sys/devices/system/cpu/online first before
using sysconf() as it can't accurately configure holes in the CPU map.

If sysconf() is used, warn when the configured and online processors
disagree.

When reading from a file, if the read doesn't yield a CPU map then
return an empty map rather than the default online. This avoids
recursion but also better yields being able to detect failures.

Add more comments.

Reviewed-by: James Clark <james.clark@arm.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: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Andrew Jones <ajones@ventanamicro.com>
Cc: André Almeida <andrealmeid@igalia.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: Atish Patra <atishp@rivosinc.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Paran Lee <p4ranlee@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will@kernel.org>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Yang Li <yang.lee@linux.alibaba.com>
Cc: Yanteng Si <siyanteng@loongson.cn>
Cc: bpf@vger.kernel.org
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20231129060211.1890454-3-irogers@google.com
[ s/syfs/sysfs/g typo ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
8f60f870 48219b08

+51 -27
+35 -24
tools/lib/perf/cpumap.c
··· 9 9 #include <unistd.h> 10 10 #include <ctype.h> 11 11 #include <limits.h> 12 + #include "internal.h" 12 13 13 14 void perf_cpu_map__set_nr(struct perf_cpu_map *map, int nr_cpus) 14 15 { ··· 67 66 } 68 67 } 69 68 70 - static struct perf_cpu_map *cpu_map__default_new(void) 69 + static struct perf_cpu_map *cpu_map__new_sysconf(void) 71 70 { 72 71 struct perf_cpu_map *cpus; 73 - int nr_cpus; 72 + int nr_cpus, nr_cpus_conf; 74 73 75 74 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); 76 75 if (nr_cpus < 0) 77 76 return NULL; 77 + 78 + nr_cpus_conf = sysconf(_SC_NPROCESSORS_CONF); 79 + if (nr_cpus != nr_cpus_conf) { 80 + pr_warning("Number of online CPUs (%d) differs from the number configured (%d) the CPU map will only cover the first %d CPUs.", 81 + nr_cpus, nr_cpus_conf, nr_cpus); 82 + } 78 83 79 84 cpus = perf_cpu_map__alloc(nr_cpus); 80 85 if (cpus != NULL) { ··· 93 86 return cpus; 94 87 } 95 88 96 - struct perf_cpu_map *perf_cpu_map__default_new(void) 89 + static struct perf_cpu_map *cpu_map__new_sysfs_online(void) 97 90 { 98 - return cpu_map__default_new(); 91 + struct perf_cpu_map *cpus = NULL; 92 + FILE *onlnf; 93 + 94 + onlnf = fopen("/sys/devices/system/cpu/online", "r"); 95 + if (onlnf) { 96 + cpus = perf_cpu_map__read(onlnf); 97 + fclose(onlnf); 98 + } 99 + return cpus; 100 + } 101 + 102 + struct perf_cpu_map *perf_cpu_map__new_online_cpus(void) 103 + { 104 + struct perf_cpu_map *cpus = cpu_map__new_sysfs_online(); 105 + 106 + if (cpus) 107 + return cpus; 108 + 109 + return cpu_map__new_sysconf(); 99 110 } 100 111 101 112 ··· 205 180 206 181 if (nr_cpus > 0) 207 182 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus); 208 - else 209 - cpus = cpu_map__default_new(); 210 183 out_free_tmp: 211 184 free(tmp_cpus); 212 - return cpus; 213 - } 214 - 215 - static struct perf_cpu_map *cpu_map__read_all_cpu_map(void) 216 - { 217 - struct perf_cpu_map *cpus = NULL; 218 - FILE *onlnf; 219 - 220 - onlnf = fopen("/sys/devices/system/cpu/online", "r"); 221 - if (!onlnf) 222 - return cpu_map__default_new(); 223 - 224 - cpus = perf_cpu_map__read(onlnf); 225 - fclose(onlnf); 226 185 return cpus; 227 186 } 228 187 ··· 220 211 int max_entries = 0; 221 212 222 213 if (!cpu_list) 223 - return cpu_map__read_all_cpu_map(); 214 + return perf_cpu_map__new_online_cpus(); 224 215 225 216 /* 226 217 * must handle the case of empty cpumap to cover ··· 277 268 278 269 if (nr_cpus > 0) 279 270 cpus = cpu_map__trim_new(nr_cpus, tmp_cpus); 280 - else if (*cpu_list != '\0') 281 - cpus = cpu_map__default_new(); 282 - else 271 + else if (*cpu_list != '\0') { 272 + pr_warning("Unexpected characters at end of cpu list ('%s'), using online CPUs.", 273 + cpu_list); 274 + cpus = perf_cpu_map__new_online_cpus(); 275 + } else 283 276 cpus = perf_cpu_map__new_any_cpu(); 284 277 invalid: 285 278 free(tmp_cpus);
+14 -1
tools/lib/perf/include/perf/cpumap.h
··· 22 22 * perf_cpu_map__new_any_cpu - a map with a singular "any CPU"/dummy -1 value. 23 23 */ 24 24 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_any_cpu(void); 25 - LIBPERF_API struct perf_cpu_map *perf_cpu_map__default_new(void); 25 + /** 26 + * perf_cpu_map__new_online_cpus - a map read from 27 + * /sys/devices/system/cpu/online if 28 + * available. If reading wasn't possible a map 29 + * is created using the online processors 30 + * assuming the first 'n' processors are all 31 + * online. 32 + */ 33 + LIBPERF_API struct perf_cpu_map *perf_cpu_map__new_online_cpus(void); 34 + /** 35 + * perf_cpu_map__new - create a map from the given cpu_list such as "0-7". If no 36 + * cpu_list argument is provided then 37 + * perf_cpu_map__new_online_cpus is returned. 38 + */ 26 39 LIBPERF_API struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list); 27 40 LIBPERF_API struct perf_cpu_map *perf_cpu_map__read(FILE *file); 28 41 LIBPERF_API struct perf_cpu_map *perf_cpu_map__get(struct perf_cpu_map *map);
+1 -1
tools/lib/perf/libperf.map
··· 2 2 global: 3 3 libperf_init; 4 4 perf_cpu_map__new_any_cpu; 5 - perf_cpu_map__default_new; 5 + perf_cpu_map__new_online_cpus; 6 6 perf_cpu_map__get; 7 7 perf_cpu_map__put; 8 8 perf_cpu_map__new;
+1 -1
tools/lib/perf/tests/test-cpumap.c
··· 29 29 perf_cpu_map__put(cpus); 30 30 perf_cpu_map__put(cpus); 31 31 32 - cpus = perf_cpu_map__default_new(); 32 + cpus = perf_cpu_map__new_online_cpus(); 33 33 if (!cpus) 34 34 return -1; 35 35