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

perf: Extract building cache level for a CPU into separate function

build_caches() builds the complete cache topology of the system by
iterating over all CPU, building and comparing cache levels of each CPU,
keeping only the unique ones at the end.

Extract the unit that build the cache levels for a single CPU into a
separate function. Expose this function, and the MAX_CACHE_LVL value to
be used elsewhere in perf too.

Signed-off-by: K Prateek Nayak <kprateek.nayak@amd.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ananth Narayan <ananth.narayan@amd.com>
Cc: Gautham Shenoy <gautham.shenoy@amd.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Wen Pu <puwen@hygon.cn>
Link: https://lore.kernel.org/r/20230517172745.5833-2-kprateek.nayak@amd.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

K Prateek Nayak and committed by
Arnaldo Carvalho de Melo
2b72cec9 bc4e4121

+44 -24
+40 -24
tools/perf/util/header.c
··· 1213 1213 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map); 1214 1214 } 1215 1215 1216 - #define MAX_CACHE_LVL 4 1216 + /* 1217 + * Build caches levels for a particular CPU from the data in 1218 + * /sys/devices/system/cpu/cpu<cpu>/cache/ 1219 + * The cache level data is stored in caches[] from index at 1220 + * *cntp. 1221 + */ 1222 + int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp) 1223 + { 1224 + u16 level; 1225 + 1226 + for (level = 0; level < MAX_CACHE_LVL; level++) { 1227 + struct cpu_cache_level c; 1228 + int err; 1229 + u32 i; 1230 + 1231 + err = cpu_cache_level__read(&c, cpu, level); 1232 + if (err < 0) 1233 + return err; 1234 + 1235 + if (err == 1) 1236 + break; 1237 + 1238 + for (i = 0; i < *cntp; i++) { 1239 + if (cpu_cache_level__cmp(&c, &caches[i])) 1240 + break; 1241 + } 1242 + 1243 + if (i == *cntp) { 1244 + caches[*cntp] = c; 1245 + *cntp = *cntp + 1; 1246 + } else 1247 + cpu_cache_level__free(&c); 1248 + } 1249 + 1250 + return 0; 1251 + } 1217 1252 1218 1253 static int build_caches(struct cpu_cache_level caches[], u32 *cntp) 1219 1254 { 1220 - u32 i, cnt = 0; 1221 - u32 nr, cpu; 1222 - u16 level; 1255 + u32 nr, cpu, cnt = 0; 1223 1256 1224 1257 nr = cpu__max_cpu().cpu; 1225 1258 1226 1259 for (cpu = 0; cpu < nr; cpu++) { 1227 - for (level = 0; level < MAX_CACHE_LVL; level++) { 1228 - struct cpu_cache_level c; 1229 - int err; 1260 + int ret = build_caches_for_cpu(cpu, caches, &cnt); 1230 1261 1231 - err = cpu_cache_level__read(&c, cpu, level); 1232 - if (err < 0) 1233 - return err; 1234 - 1235 - if (err == 1) 1236 - break; 1237 - 1238 - for (i = 0; i < cnt; i++) { 1239 - if (cpu_cache_level__cmp(&c, &caches[i])) 1240 - break; 1241 - } 1242 - 1243 - if (i == cnt) 1244 - caches[cnt++] = c; 1245 - else 1246 - cpu_cache_level__free(&c); 1247 - } 1262 + if (ret) 1263 + return ret; 1248 1264 } 1249 1265 *cntp = cnt; 1250 1266 return 0;
+4
tools/perf/util/header.h
··· 179 179 int write_padded(struct feat_fd *fd, const void *bf, 180 180 size_t count, size_t count_aligned); 181 181 182 + #define MAX_CACHE_LVL 4 183 + 182 184 int is_cpu_online(unsigned int cpu); 185 + int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp); 186 + 183 187 /* 184 188 * arch specific callback 185 189 */