Skip to content

Commit 29be2fe

Browse files
captain5050acmel
authored andcommitted
perf pmu-events: Hide pmu_events_map
Move usage of the table to pmu-events.c so it may be hidden. By abstracting the table the implementation can later be changed. Signed-off-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Garry <[email protected]> Cc: Kan Liang <[email protected]> Cc: Leo Yan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mike Leach <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ravi Bangoria <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Will Deacon <[email protected]> Cc: Xing Zhengjun <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent eeac773 commit 29be2fe

File tree

7 files changed

+280
-180
lines changed

7 files changed

+280
-180
lines changed

tools/perf/pmu-events/empty-pmu-events.c

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* The test cpu/soc is provided for testing.
77
*/
88
#include "pmu-events/pmu-events.h"
9+
#include "util/header.h"
10+
#include "util/pmu.h"
911
#include <string.h>
1012
#include <stddef.h>
1113

@@ -110,7 +112,26 @@ static const struct pmu_event pme_test_soc_cpu[] = {
110112
},
111113
};
112114

113-
const struct pmu_events_map pmu_events_map[] = {
115+
116+
/*
117+
* Map a CPU to its table of PMU events. The CPU is identified by the
118+
* cpuid field, which is an arch-specific identifier for the CPU.
119+
* The identifier specified in tools/perf/pmu-events/arch/xxx/mapfile
120+
* must match the get_cpuid_str() in tools/perf/arch/xxx/util/header.c)
121+
*
122+
* The cpuid can contain any character other than the comma.
123+
*/
124+
struct pmu_events_map {
125+
const char *arch;
126+
const char *cpuid;
127+
const struct pmu_event *table;
128+
};
129+
130+
/*
131+
* Global table mapping each known CPU for the architecture to its
132+
* table of PMU events.
133+
*/
134+
static const struct pmu_events_map pmu_events_map[] = {
114135
{
115136
.arch = "testarch",
116137
.cpuid = "testcpu",
@@ -162,6 +183,62 @@ static const struct pmu_sys_events pmu_sys_event_tables[] = {
162183
},
163184
};
164185

186+
const struct pmu_event *perf_pmu__find_table(struct perf_pmu *pmu)
187+
{
188+
const struct pmu_event *table = NULL;
189+
char *cpuid = perf_pmu__getcpuid(pmu);
190+
int i;
191+
192+
/* on some platforms which uses cpus map, cpuid can be NULL for
193+
* PMUs other than CORE PMUs.
194+
*/
195+
if (!cpuid)
196+
return NULL;
197+
198+
i = 0;
199+
for (;;) {
200+
const struct pmu_events_map *map = &pmu_events_map[i++];
201+
202+
if (!map->table)
203+
break;
204+
205+
if (!strcmp_cpuid_str(map->cpuid, cpuid)) {
206+
table = map->table;
207+
break;
208+
}
209+
}
210+
free(cpuid);
211+
return table;
212+
}
213+
214+
const struct pmu_event *find_core_events_table(const char *arch, const char *cpuid)
215+
{
216+
for (const struct pmu_events_map *tables = &pmu_events_map[0];
217+
tables->table;
218+
tables++) {
219+
if (!strcmp(tables->arch, arch) && !strcmp_cpuid_str(tables->cpuid, cpuid))
220+
return tables->table;
221+
}
222+
return NULL;
223+
}
224+
225+
int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data)
226+
{
227+
for (const struct pmu_events_map *tables = &pmu_events_map[0];
228+
tables->table;
229+
tables++) {
230+
for (const struct pmu_event *pe = &tables->table[0];
231+
pe->name || pe->metric_group || pe->metric_name;
232+
pe++) {
233+
int ret = fn(pe, &tables->table[0], data);
234+
235+
if (ret)
236+
return ret;
237+
}
238+
}
239+
return 0;
240+
}
241+
165242
const struct pmu_event *find_sys_events_table(const char *name)
166243
{
167244
for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
@@ -181,7 +258,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data)
181258
for (const struct pmu_event *pe = &tables->table[0];
182259
pe->name || pe->metric_group || pe->metric_name;
183260
pe++) {
184-
int ret = fn(pe, data);
261+
int ret = fn(pe, &tables->table[0], data);
185262

186263
if (ret)
187264
return ret;

tools/perf/pmu-events/jevents.py

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,27 @@ def is_leaf_dir(path: str) -> bool:
334334

335335
def print_mapping_table(archs: Sequence[str]) -> None:
336336
"""Read the mapfile and generate the struct from cpuid string to event table."""
337-
_args.output_file.write('const struct pmu_events_map pmu_events_map[] = {\n')
337+
_args.output_file.write("""
338+
/*
339+
* Map a CPU to its table of PMU events. The CPU is identified by the
340+
* cpuid field, which is an arch-specific identifier for the CPU.
341+
* The identifier specified in tools/perf/pmu-events/arch/xxx/mapfile
342+
* must match the get_cpuid_str() in tools/perf/arch/xxx/util/header.c)
343+
*
344+
* The cpuid can contain any character other than the comma.
345+
*/
346+
struct pmu_events_map {
347+
const char *arch;
348+
const char *cpuid;
349+
const struct pmu_event *table;
350+
};
351+
352+
/*
353+
* Global table mapping each known CPU for the architecture to its
354+
* table of PMU events.
355+
*/
356+
const struct pmu_events_map pmu_events_map[] = {
357+
""")
338358
for arch in archs:
339359
if arch == 'test':
340360
_args.output_file.write("""{
@@ -390,6 +410,61 @@ def print_system_mapping_table() -> None:
390410
\t},
391411
};
392412
413+
const struct pmu_event *perf_pmu__find_table(struct perf_pmu *pmu)
414+
{
415+
const struct pmu_event *table = NULL;
416+
char *cpuid = perf_pmu__getcpuid(pmu);
417+
int i;
418+
419+
/* on some platforms which uses cpus map, cpuid can be NULL for
420+
* PMUs other than CORE PMUs.
421+
*/
422+
if (!cpuid)
423+
return NULL;
424+
425+
i = 0;
426+
for (;;) {
427+
const struct pmu_events_map *map = &pmu_events_map[i++];
428+
if (!map->table)
429+
break;
430+
431+
if (!strcmp_cpuid_str(map->cpuid, cpuid)) {
432+
table = map->table;
433+
break;
434+
}
435+
}
436+
free(cpuid);
437+
return table;
438+
}
439+
440+
const struct pmu_event *find_core_events_table(const char *arch, const char *cpuid)
441+
{
442+
for (const struct pmu_events_map *tables = &pmu_events_map[0];
443+
tables->table;
444+
tables++) {
445+
if (!strcmp(tables->arch, arch) && !strcmp_cpuid_str(tables->cpuid, cpuid))
446+
return tables->table;
447+
}
448+
return NULL;
449+
}
450+
451+
int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data)
452+
{
453+
for (const struct pmu_events_map *tables = &pmu_events_map[0];
454+
tables->table;
455+
tables++) {
456+
for (const struct pmu_event *pe = &tables->table[0];
457+
pe->name || pe->metric_group || pe->metric_name;
458+
pe++) {
459+
int ret = fn(pe, &tables->table[0], data);
460+
461+
if (ret)
462+
return ret;
463+
}
464+
}
465+
return 0;
466+
}
467+
393468
const struct pmu_event *find_sys_events_table(const char *name)
394469
{
395470
for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
@@ -409,7 +484,7 @@ def print_system_mapping_table() -> None:
409484
for (const struct pmu_event *pe = &tables->table[0];
410485
pe->name || pe->metric_group || pe->metric_name;
411486
pe++) {
412-
int ret = fn(pe, data);
487+
int ret = fn(pe, &tables->table[0], data);
413488
414489
if (ret)
415490
return ret;
@@ -450,6 +525,8 @@ def ftw(path: str, parents: Sequence[str],
450525

451526
_args.output_file.write("""
452527
#include "pmu-events/pmu-events.h"
528+
#include "util/header.h"
529+
#include "util/pmu.h"
453530
#include <string.h>
454531
#include <stddef.h>
455532

tools/perf/pmu-events/pmu-events.h

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef PMU_EVENTS_H
33
#define PMU_EVENTS_H
44

5+
struct perf_pmu;
6+
57
enum aggr_mode_class {
68
PerChip = 1,
79
PerCore
@@ -28,30 +30,15 @@ struct pmu_event {
2830
const char *metric_constraint;
2931
};
3032

31-
/*
32-
*
33-
* Map a CPU to its table of PMU events. The CPU is identified by the
34-
* cpuid field, which is an arch-specific identifier for the CPU.
35-
* The identifier specified in tools/perf/pmu-events/arch/xxx/mapfile
36-
* must match the get_cpuid_str() in tools/perf/arch/xxx/util/header.c)
37-
*
38-
* The cpuid can contain any character other than the comma.
39-
*/
40-
struct pmu_events_map {
41-
const char *arch;
42-
const char *cpuid;
43-
const struct pmu_event *table;
44-
};
33+
typedef int (*pmu_event_iter_fn)(const struct pmu_event *pe,
34+
const struct pmu_event *table,
35+
void *data);
4536

46-
/*
47-
* Global table mapping each known CPU for the architecture to its
48-
* table of PMU events.
49-
*/
50-
extern const struct pmu_events_map pmu_events_map[];
37+
const struct pmu_event *perf_pmu__find_table(struct perf_pmu *pmu);
38+
const struct pmu_event *find_core_events_table(const char *arch, const char *cpuid);
39+
int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data);
5140

5241
const struct pmu_event *find_sys_events_table(const char *name);
53-
54-
typedef int (*pmu_event_iter_fn)(const struct pmu_event *pe, void *data);
5542
int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data);
5643

5744
#endif

0 commit comments

Comments
 (0)