Skip to content

Commit

Permalink
perf pmu-events: Hide the pmu_events
Browse files Browse the repository at this point in the history
Hide that the pmu_event structs are an array with a new wrapper struct.

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]>
  • Loading branch information
captain5050 authored and acmel committed Aug 13, 2022
1 parent 660842e commit 1ba3752
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 91 deletions.
2 changes: 1 addition & 1 deletion tools/perf/arch/arm64/util/pmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "../../../util/cpumap.h"
#include "../../../util/pmu.h"

const struct pmu_event *pmu_events_table__find(void)
const struct pmu_events_table *pmu_events_table__find(void)
{
struct perf_pmu *pmu = NULL;

Expand Down
44 changes: 24 additions & 20 deletions tools/perf/pmu-events/empty-pmu-events.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ static const struct pmu_event pme_test_soc_cpu[] = {
},
};

/* Struct used to make the PMU event table implementation opaque to callers. */
struct pmu_events_table {
const struct pmu_event *entries;
};

/*
* Map a CPU to its table of PMU events. The CPU is identified by the
Expand All @@ -188,7 +192,7 @@ static const struct pmu_event pme_test_soc_cpu[] = {
struct pmu_events_map {
const char *arch;
const char *cpuid;
const struct pmu_event *table;
const struct pmu_events_table table;
};

/*
Expand All @@ -199,12 +203,12 @@ static const struct pmu_events_map pmu_events_map[] = {
{
.arch = "testarch",
.cpuid = "testcpu",
.table = pme_test_soc_cpu,
.table = { pme_test_soc_cpu },
},
{
.arch = 0,
.cpuid = 0,
.table = 0,
.table = { 0 },
},
};

Expand Down Expand Up @@ -234,23 +238,23 @@ static const struct pmu_event pme_test_soc_sys[] = {

struct pmu_sys_events {
const char *name;
const struct pmu_event *table;
const struct pmu_events_table table;
};

static const struct pmu_sys_events pmu_sys_event_tables[] = {
{
.table = pme_test_soc_sys,
.table = { pme_test_soc_sys },
.name = "pme_test_soc_sys",
},
{
.table = 0
.table = { 0 }
},
};

int pmu_events_table_for_each_event(const struct pmu_event *table, pmu_event_iter_fn fn,
int pmu_events_table_for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn,
void *data)
{
for (const struct pmu_event *pe = &table[0];
for (const struct pmu_event *pe = &table->entries[0];
pe->name || pe->metric_group || pe->metric_name;
pe++) {
int ret = fn(pe, table, data);
Expand All @@ -261,9 +265,9 @@ int pmu_events_table_for_each_event(const struct pmu_event *table, pmu_event_ite
return 0;
}

const struct pmu_event *perf_pmu__find_table(struct perf_pmu *pmu)
const struct pmu_events_table *perf_pmu__find_table(struct perf_pmu *pmu)
{
const struct pmu_event *table = NULL;
const struct pmu_events_table *table = NULL;
char *cpuid = perf_pmu__getcpuid(pmu);
int i;

Expand All @@ -277,49 +281,49 @@ const struct pmu_event *perf_pmu__find_table(struct perf_pmu *pmu)
for (;;) {
const struct pmu_events_map *map = &pmu_events_map[i++];

if (!map->table)
if (!map->cpuid)
break;

if (!strcmp_cpuid_str(map->cpuid, cpuid)) {
table = map->table;
table = &map->table;
break;
}
}
free(cpuid);
return table;
}

const struct pmu_event *find_core_events_table(const char *arch, const char *cpuid)
const struct pmu_events_table *find_core_events_table(const char *arch, const char *cpuid)
{
for (const struct pmu_events_map *tables = &pmu_events_map[0];
tables->table;
tables->arch;
tables++) {
if (!strcmp(tables->arch, arch) && !strcmp_cpuid_str(tables->cpuid, cpuid))
return tables->table;
return &tables->table;
}
return NULL;
}

int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data)
{
for (const struct pmu_events_map *tables = &pmu_events_map[0];
tables->table;
tables->arch;
tables++) {
int ret = pmu_events_table_for_each_event(tables->table, fn, data);
int ret = pmu_events_table_for_each_event(&tables->table, fn, data);

if (ret)
return ret;
}
return 0;
}

const struct pmu_event *find_sys_events_table(const char *name)
const struct pmu_events_table *find_sys_events_table(const char *name)
{
for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
tables->name;
tables++) {
if (!strcmp(tables->name, name))
return tables->table;
return &tables->table;
}
return NULL;
}
Expand All @@ -329,7 +333,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data)
for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
tables->name;
tables++) {
int ret = pmu_events_table_for_each_event(tables->table, fn, data);
int ret = pmu_events_table_for_each_event(&tables->table, fn, data);

if (ret)
return ret;
Expand Down
47 changes: 26 additions & 21 deletions tools/perf/pmu-events/jevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ def is_leaf_dir(path: str) -> bool:
def print_mapping_table(archs: Sequence[str]) -> None:
"""Read the mapfile and generate the struct from cpuid string to event table."""
_args.output_file.write("""
/* Struct used to make the PMU event table implementation opaque to callers. */
struct pmu_events_table {
const struct pmu_event *entries;
};
/*
* Map a CPU to its table of PMU events. The CPU is identified by the
* cpuid field, which is an arch-specific identifier for the CPU.
Expand All @@ -346,7 +351,7 @@ def print_mapping_table(archs: Sequence[str]) -> None:
struct pmu_events_map {
const char *arch;
const char *cpuid;
const struct pmu_event *table;
struct pmu_events_table table;
};
/*
Expand All @@ -360,7 +365,7 @@ def print_mapping_table(archs: Sequence[str]) -> None:
_args.output_file.write("""{
\t.arch = "testarch",
\t.cpuid = "testcpu",
\t.table = pme_test_soc_cpu,
\t.table = { pme_test_soc_cpu },
},
""")
else:
Expand All @@ -375,15 +380,15 @@ def print_mapping_table(archs: Sequence[str]) -> None:
_args.output_file.write(f"""{{
\t.arch = "{arch}",
\t.cpuid = "{cpuid}",
\t.table = {tblname}
\t.table = {{ {tblname} }}
}},
""")
first = False

_args.output_file.write("""{
\t.arch = 0,
\t.cpuid = 0,
\t.table = 0,
\t.table = { 0 },
}
};
""")
Expand All @@ -394,26 +399,26 @@ def print_system_mapping_table() -> None:
_args.output_file.write("""
struct pmu_sys_events {
\tconst char *name;
\tconst struct pmu_event *table;
\tstruct pmu_events_table table;
};
static const struct pmu_sys_events pmu_sys_event_tables[] = {
""")
for tblname in _sys_event_tables:
_args.output_file.write(f"""\t{{
\t\t.table = {tblname},
\t\t.table = {{ {tblname} }},
\t\t.name = \"{tblname}\",
\t}},
""")
_args.output_file.write("""\t{
\t\t.table = 0
\t\t.table = { 0 }
\t},
};
int pmu_events_table_for_each_event(const struct pmu_event *table, pmu_event_iter_fn fn,
int pmu_events_table_for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn,
void *data)
{
for (const struct pmu_event *pe = &table[0];
for (const struct pmu_event *pe = &table->entries[0];
pe->name || pe->metric_group || pe->metric_name;
pe++) {
int ret = fn(pe, table, data);
Expand All @@ -424,9 +429,9 @@ def print_system_mapping_table() -> None:
return 0;
}
const struct pmu_event *perf_pmu__find_table(struct perf_pmu *pmu)
const struct pmu_events_table *perf_pmu__find_table(struct perf_pmu *pmu)
{
const struct pmu_event *table = NULL;
const struct pmu_events_table *table = NULL;
char *cpuid = perf_pmu__getcpuid(pmu);
int i;
Expand All @@ -439,49 +444,49 @@ def print_system_mapping_table() -> None:
i = 0;
for (;;) {
const struct pmu_events_map *map = &pmu_events_map[i++];
if (!map->table)
if (!map->arch)
break;
if (!strcmp_cpuid_str(map->cpuid, cpuid)) {
table = map->table;
table = &map->table;
break;
}
}
free(cpuid);
return table;
}
const struct pmu_event *find_core_events_table(const char *arch, const char *cpuid)
const struct pmu_events_table *find_core_events_table(const char *arch, const char *cpuid)
{
for (const struct pmu_events_map *tables = &pmu_events_map[0];
tables->table;
tables->arch;
tables++) {
if (!strcmp(tables->arch, arch) && !strcmp_cpuid_str(tables->cpuid, cpuid))
return tables->table;
return &tables->table;
}
return NULL;
}
int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data)
{
for (const struct pmu_events_map *tables = &pmu_events_map[0];
tables->table;
tables->arch;
tables++) {
int ret = pmu_events_table_for_each_event(tables->table, fn, data);
int ret = pmu_events_table_for_each_event(&tables->table, fn, data);
if (ret)
return ret;
}
return 0;
}
const struct pmu_event *find_sys_events_table(const char *name)
const struct pmu_events_table *find_sys_events_table(const char *name)
{
for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
tables->name;
tables++) {
if (!strcmp(tables->name, name))
return tables->table;
return &tables->table;
}
return NULL;
}
Expand All @@ -491,7 +496,7 @@ def print_system_mapping_table() -> None:
for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
tables->name;
tables++) {
int ret = pmu_events_table_for_each_event(tables->table, fn, data);
int ret = pmu_events_table_for_each_event(&tables->table, fn, data);
if (ret)
return ret;
Expand Down
12 changes: 7 additions & 5 deletions tools/perf/pmu-events/pmu-events.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ struct pmu_event {
const char *metric_constraint;
};

struct pmu_events_table;

typedef int (*pmu_event_iter_fn)(const struct pmu_event *pe,
const struct pmu_event *table,
const struct pmu_events_table *table,
void *data);

int pmu_events_table_for_each_event(const struct pmu_event *table, pmu_event_iter_fn fn,
int pmu_events_table_for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn,
void *data);

const struct pmu_event *perf_pmu__find_table(struct perf_pmu *pmu);
const struct pmu_event *find_core_events_table(const char *arch, const char *cpuid);
const struct pmu_events_table *perf_pmu__find_table(struct perf_pmu *pmu);
const struct pmu_events_table *find_core_events_table(const char *arch, const char *cpuid);
int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data);

const struct pmu_event *find_sys_events_table(const char *name);
const struct pmu_events_table *find_sys_events_table(const char *name);
int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data);

#endif
2 changes: 1 addition & 1 deletion tools/perf/tests/expand-cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static int expand_metric_events(void)
struct evlist *evlist;
struct rblist metric_events;
const char metric_str[] = "CPI";
const struct pmu_event *pme_test;
const struct pmu_events_table *pme_test;

evlist = evlist__new();
TEST_ASSERT_VAL("failed to get evlist", evlist);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/tests/parse-metric.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static int __compute_metric(const char *name, struct value *vals,
struct rblist metric_events = {
.nr_entries = 0,
};
const struct pmu_event *pme_test;
const struct pmu_events_table *pme_test;
struct perf_cpu_map *cpus;
struct runtime_stat st;
struct evlist *evlist;
Expand Down
Loading

0 comments on commit 1ba3752

Please sign in to comment.