Commit c091ee90 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf pmu: Remove logic for PMU name being NULL

The PMU name could be NULL in the case of the fake_pmu. Initialize the
name for the fake_pmu to "fake" so that all other logic can assume it
is initialized. Add a const to the type of name so that a literal can
be used to avoid additional initialization code. Propagate the cost
through related routines and remove now unnecessary "(char *)"
casts. Doing this located a bug in builtin-list for the pmu_glob that
was missing a strdup.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20230825024002.801955-3-irogers@google.com
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: James Clark <james.clark@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Wei Li <liwei391@huawei.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Will Deacon <will@kernel.org>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: Ming Wang <wangming01@loongson.cn>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: linux-kernel@vger.kernel.org
Cc: linux-perf-users@vger.kernel.org
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 9897009e
...@@ -20,7 +20,7 @@ struct perf_mem_event *perf_mem_events__ptr(int i) ...@@ -20,7 +20,7 @@ struct perf_mem_event *perf_mem_events__ptr(int i)
return &perf_mem_events[i]; return &perf_mem_events[i];
} }
char *perf_mem_events__name(int i, char *pmu_name __maybe_unused) const char *perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
{ {
struct perf_mem_event *e = perf_mem_events__ptr(i); struct perf_mem_event *e = perf_mem_events__ptr(i);
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
#include "mem-events.h" #include "mem-events.h"
/* PowerPC does not support 'ldlat' parameter. */ /* PowerPC does not support 'ldlat' parameter. */
char *perf_mem_events__name(int i, char *pmu_name __maybe_unused) const char *perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
{ {
if (i == PERF_MEM_EVENTS__LOAD) if (i == PERF_MEM_EVENTS__LOAD)
return (char *) "cpu/mem-loads/"; return "cpu/mem-loads/";
return (char *) "cpu/mem-stores/"; return "cpu/mem-stores/";
} }
...@@ -52,7 +52,7 @@ bool is_mem_loads_aux_event(struct evsel *leader) ...@@ -52,7 +52,7 @@ bool is_mem_loads_aux_event(struct evsel *leader)
return leader->core.attr.config == MEM_LOADS_AUX; return leader->core.attr.config == MEM_LOADS_AUX;
} }
char *perf_mem_events__name(int i, char *pmu_name) const char *perf_mem_events__name(int i, const char *pmu_name)
{ {
struct perf_mem_event *e = perf_mem_events__ptr(i); struct perf_mem_event *e = perf_mem_events__ptr(i);
...@@ -65,7 +65,7 @@ char *perf_mem_events__name(int i, char *pmu_name) ...@@ -65,7 +65,7 @@ char *perf_mem_events__name(int i, char *pmu_name)
if (!pmu_name) { if (!pmu_name) {
mem_loads_name__init = true; mem_loads_name__init = true;
pmu_name = (char *)"cpu"; pmu_name = "cpu";
} }
if (perf_pmus__have_event(pmu_name, "mem-loads-aux")) { if (perf_pmus__have_event(pmu_name, "mem-loads-aux")) {
...@@ -82,12 +82,12 @@ char *perf_mem_events__name(int i, char *pmu_name) ...@@ -82,12 +82,12 @@ char *perf_mem_events__name(int i, char *pmu_name)
if (i == PERF_MEM_EVENTS__STORE) { if (i == PERF_MEM_EVENTS__STORE) {
if (!pmu_name) if (!pmu_name)
pmu_name = (char *)"cpu"; pmu_name = "cpu";
scnprintf(mem_stores_name, sizeof(mem_stores_name), scnprintf(mem_stores_name, sizeof(mem_stores_name),
e->name, pmu_name); e->name, pmu_name);
return mem_stores_name; return mem_stores_name;
} }
return (char *)e->name; return e->name;
} }
...@@ -126,7 +126,7 @@ static int setup_pmu_alias_list(void) ...@@ -126,7 +126,7 @@ static int setup_pmu_alias_list(void)
return ret; return ret;
} }
static char *__pmu_find_real_name(const char *name) static const char *__pmu_find_real_name(const char *name)
{ {
struct pmu_alias *pmu_alias; struct pmu_alias *pmu_alias;
...@@ -135,10 +135,10 @@ static char *__pmu_find_real_name(const char *name) ...@@ -135,10 +135,10 @@ static char *__pmu_find_real_name(const char *name)
return pmu_alias->name; return pmu_alias->name;
} }
return (char *)name; return name;
} }
char *pmu_find_real_name(const char *name) const char *pmu_find_real_name(const char *name)
{ {
if (cached_list) if (cached_list)
return __pmu_find_real_name(name); return __pmu_find_real_name(name);
...@@ -149,7 +149,7 @@ char *pmu_find_real_name(const char *name) ...@@ -149,7 +149,7 @@ char *pmu_find_real_name(const char *name)
return __pmu_find_real_name(name); return __pmu_find_real_name(name);
} }
static char *__pmu_find_alias_name(const char *name) static const char *__pmu_find_alias_name(const char *name)
{ {
struct pmu_alias *pmu_alias; struct pmu_alias *pmu_alias;
...@@ -160,7 +160,7 @@ static char *__pmu_find_alias_name(const char *name) ...@@ -160,7 +160,7 @@ static char *__pmu_find_alias_name(const char *name)
return NULL; return NULL;
} }
char *pmu_find_alias_name(const char *name) const char *pmu_find_alias_name(const char *name)
{ {
if (cached_list) if (cached_list)
return __pmu_find_alias_name(name); return __pmu_find_alias_name(name);
......
...@@ -513,7 +513,7 @@ int cmd_list(int argc, const char **argv) ...@@ -513,7 +513,7 @@ int cmd_list(int argc, const char **argv)
ret = -1; ret = -1;
goto out; goto out;
} }
default_ps.pmu_glob = pmu->name; default_ps.pmu_glob = strdup(pmu->name);
} }
} }
print_cb.print_start(ps); print_cb.print_start(ps);
......
...@@ -522,7 +522,7 @@ static int test_core_pmu_event_aliases_cb(void *state, struct pmu_event_info *al ...@@ -522,7 +522,7 @@ static int test_core_pmu_event_aliases_cb(void *state, struct pmu_event_info *al
} }
/* Verify aliases are as expected */ /* Verify aliases are as expected */
static int __test_core_pmu_event_aliases(char *pmu_name, int *count) static int __test_core_pmu_event_aliases(const char *pmu_name, int *count)
{ {
struct perf_pmu_test_event const **test_event_table; struct perf_pmu_test_event const **test_event_table;
struct perf_pmu *pmu; struct perf_pmu *pmu;
...@@ -635,7 +635,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu) ...@@ -635,7 +635,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
static struct perf_pmu_test_pmu test_pmus[] = { static struct perf_pmu_test_pmu test_pmus[] = {
{ {
.pmu = { .pmu = {
.name = (char *)"hisi_sccl1_ddrc2", .name = "hisi_sccl1_ddrc2",
.is_uncore = 1, .is_uncore = 1,
}, },
.aliases = { .aliases = {
...@@ -644,7 +644,7 @@ static struct perf_pmu_test_pmu test_pmus[] = { ...@@ -644,7 +644,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
}, },
{ {
.pmu = { .pmu = {
.name = (char *)"uncore_cbox_0", .name = "uncore_cbox_0",
.is_uncore = 1, .is_uncore = 1,
}, },
.aliases = { .aliases = {
...@@ -655,7 +655,7 @@ static struct perf_pmu_test_pmu test_pmus[] = { ...@@ -655,7 +655,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
}, },
{ {
.pmu = { .pmu = {
.name = (char *)"hisi_sccl3_l3c7", .name = "hisi_sccl3_l3c7",
.is_uncore = 1, .is_uncore = 1,
}, },
.aliases = { .aliases = {
...@@ -664,7 +664,7 @@ static struct perf_pmu_test_pmu test_pmus[] = { ...@@ -664,7 +664,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
}, },
{ {
.pmu = { .pmu = {
.name = (char *)"uncore_imc_free_running_0", .name = "uncore_imc_free_running_0",
.is_uncore = 1, .is_uncore = 1,
}, },
.aliases = { .aliases = {
...@@ -673,7 +673,7 @@ static struct perf_pmu_test_pmu test_pmus[] = { ...@@ -673,7 +673,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
}, },
{ {
.pmu = { .pmu = {
.name = (char *)"uncore_imc_0", .name = "uncore_imc_0",
.is_uncore = 1, .is_uncore = 1,
}, },
.aliases = { .aliases = {
...@@ -682,7 +682,7 @@ static struct perf_pmu_test_pmu test_pmus[] = { ...@@ -682,7 +682,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
}, },
{ {
.pmu = { .pmu = {
.name = (char *)"uncore_sys_ddr_pmu0", .name = "uncore_sys_ddr_pmu0",
.is_uncore = 1, .is_uncore = 1,
.id = (char *)"v8", .id = (char *)"v8",
}, },
...@@ -692,7 +692,7 @@ static struct perf_pmu_test_pmu test_pmus[] = { ...@@ -692,7 +692,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
}, },
{ {
.pmu = { .pmu = {
.name = (char *)"uncore_sys_ccn_pmu4", .name = "uncore_sys_ccn_pmu4",
.is_uncore = 1, .is_uncore = 1,
.id = (char *)"0x01", .id = (char *)"0x01",
}, },
......
...@@ -324,11 +324,9 @@ int perf_env__read_pmu_mappings(struct perf_env *env) ...@@ -324,11 +324,9 @@ int perf_env__read_pmu_mappings(struct perf_env *env)
u32 pmu_num = 0; u32 pmu_num = 0;
struct strbuf sb; struct strbuf sb;
while ((pmu = perf_pmus__scan(pmu))) { while ((pmu = perf_pmus__scan(pmu)))
if (!pmu->name)
continue;
pmu_num++; pmu_num++;
}
if (!pmu_num) { if (!pmu_num) {
pr_debug("pmu mappings not available\n"); pr_debug("pmu mappings not available\n");
return -ENOENT; return -ENOENT;
...@@ -339,8 +337,6 @@ int perf_env__read_pmu_mappings(struct perf_env *env) ...@@ -339,8 +337,6 @@ int perf_env__read_pmu_mappings(struct perf_env *env)
return -ENOMEM; return -ENOMEM;
while ((pmu = perf_pmus__scan(pmu))) { while ((pmu = perf_pmus__scan(pmu))) {
if (!pmu->name)
continue;
if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0) if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0)
goto error; goto error;
/* include a NULL character at the end */ /* include a NULL character at the end */
......
...@@ -746,20 +746,14 @@ static int write_pmu_mappings(struct feat_fd *ff, ...@@ -746,20 +746,14 @@ static int write_pmu_mappings(struct feat_fd *ff,
* Do a first pass to count number of pmu to avoid lseek so this * Do a first pass to count number of pmu to avoid lseek so this
* works in pipe mode as well. * works in pipe mode as well.
*/ */
while ((pmu = perf_pmus__scan(pmu))) { while ((pmu = perf_pmus__scan(pmu)))
if (!pmu->name)
continue;
pmu_num++; pmu_num++;
}
ret = do_write(ff, &pmu_num, sizeof(pmu_num)); ret = do_write(ff, &pmu_num, sizeof(pmu_num));
if (ret < 0) if (ret < 0)
return ret; return ret;
while ((pmu = perf_pmus__scan(pmu))) { while ((pmu = perf_pmus__scan(pmu))) {
if (!pmu->name)
continue;
ret = do_write(ff, &pmu->type, sizeof(pmu->type)); ret = do_write(ff, &pmu->type, sizeof(pmu->type));
if (ret < 0) if (ret < 0)
return ret; return ret;
......
...@@ -37,7 +37,7 @@ struct perf_mem_event * __weak perf_mem_events__ptr(int i) ...@@ -37,7 +37,7 @@ struct perf_mem_event * __weak perf_mem_events__ptr(int i)
return &perf_mem_events[i]; return &perf_mem_events[i];
} }
char * __weak perf_mem_events__name(int i, char *pmu_name __maybe_unused) const char * __weak perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
{ {
struct perf_mem_event *e = perf_mem_events__ptr(i); struct perf_mem_event *e = perf_mem_events__ptr(i);
...@@ -53,7 +53,7 @@ char * __weak perf_mem_events__name(int i, char *pmu_name __maybe_unused) ...@@ -53,7 +53,7 @@ char * __weak perf_mem_events__name(int i, char *pmu_name __maybe_unused)
return mem_loads_name; return mem_loads_name;
} }
return (char *)e->name; return e->name;
} }
__weak bool is_mem_loads_aux_event(struct evsel *leader __maybe_unused) __weak bool is_mem_loads_aux_event(struct evsel *leader __maybe_unused)
...@@ -186,7 +186,6 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr, ...@@ -186,7 +186,6 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr,
int i = *argv_nr, k = 0; int i = *argv_nr, k = 0;
struct perf_mem_event *e; struct perf_mem_event *e;
struct perf_pmu *pmu; struct perf_pmu *pmu;
char *s;
for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) { for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
e = perf_mem_events__ptr(j); e = perf_mem_events__ptr(j);
...@@ -209,15 +208,16 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr, ...@@ -209,15 +208,16 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr,
} }
while ((pmu = perf_pmus__scan(pmu)) != NULL) { while ((pmu = perf_pmus__scan(pmu)) != NULL) {
const char *s = perf_mem_events__name(j, pmu->name);
rec_argv[i++] = "-e"; rec_argv[i++] = "-e";
s = perf_mem_events__name(j, pmu->name);
if (s) { if (s) {
s = strdup(s); char *copy = strdup(s);
if (!s) if (!copy)
return -1; return -1;
rec_argv[i++] = s; rec_argv[i++] = copy;
rec_tmp[k++] = s; rec_tmp[k++] = copy;
} }
} }
} }
......
...@@ -38,7 +38,7 @@ extern unsigned int perf_mem_events__loads_ldlat; ...@@ -38,7 +38,7 @@ extern unsigned int perf_mem_events__loads_ldlat;
int perf_mem_events__parse(const char *str); int perf_mem_events__parse(const char *str);
int perf_mem_events__init(void); int perf_mem_events__init(void);
char *perf_mem_events__name(int i, char *pmu_name); const char *perf_mem_events__name(int i, const char *pmu_name);
struct perf_mem_event *perf_mem_events__ptr(int i); struct perf_mem_event *perf_mem_events__ptr(int i);
bool is_mem_loads_aux_event(struct evsel *leader); bool is_mem_loads_aux_event(struct evsel *leader);
......
...@@ -262,7 +262,7 @@ __add_event(struct list_head *list, int *idx, ...@@ -262,7 +262,7 @@ __add_event(struct list_head *list, int *idx,
evsel->core.is_pmu_core = pmu ? pmu->is_core : false; evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
evsel->auto_merge_stats = auto_merge_stats; evsel->auto_merge_stats = auto_merge_stats;
evsel->pmu = pmu; evsel->pmu = pmu;
evsel->pmu_name = pmu && pmu->name ? strdup(pmu->name) : NULL; evsel->pmu_name = pmu ? strdup(pmu->name) : NULL;
if (name) if (name)
evsel->name = strdup(name); evsel->name = strdup(name);
...@@ -437,9 +437,6 @@ bool parse_events__filter_pmu(const struct parse_events_state *parse_state, ...@@ -437,9 +437,6 @@ bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
if (parse_state->pmu_filter == NULL) if (parse_state->pmu_filter == NULL)
return false; return false;
if (pmu->name == NULL)
return true;
return strcmp(parse_state->pmu_filter, pmu->name) != 0; return strcmp(parse_state->pmu_filter, pmu->name) != 0;
} }
...@@ -1292,7 +1289,7 @@ static bool config_term_percore(struct list_head *config_terms) ...@@ -1292,7 +1289,7 @@ static bool config_term_percore(struct list_head *config_terms)
} }
int parse_events_add_pmu(struct parse_events_state *parse_state, int parse_events_add_pmu(struct parse_events_state *parse_state,
struct list_head *list, char *name, struct list_head *list, const char *name,
struct list_head *head_config, struct list_head *head_config,
bool auto_merge_stats, void *loc_) bool auto_merge_stats, void *loc_)
{ {
......
...@@ -176,7 +176,7 @@ int parse_events_add_breakpoint(struct parse_events_state *parse_state, ...@@ -176,7 +176,7 @@ int parse_events_add_breakpoint(struct parse_events_state *parse_state,
u64 addr, char *type, u64 len, u64 addr, char *type, u64 len,
struct list_head *head_config); struct list_head *head_config);
int parse_events_add_pmu(struct parse_events_state *parse_state, int parse_events_add_pmu(struct parse_events_state *parse_state,
struct list_head *list, char *name, struct list_head *list, const char *name,
struct list_head *head_config, struct list_head *head_config,
bool auto_merge_stats, void *loc); bool auto_merge_stats, void *loc);
......
...@@ -307,7 +307,7 @@ PE_NAME opt_pmu_config ...@@ -307,7 +307,7 @@ PE_NAME opt_pmu_config
} }
while ((pmu = perf_pmus__scan(pmu)) != NULL) { while ((pmu = perf_pmus__scan(pmu)) != NULL) {
char *name = pmu->name; const char *name = pmu->name;
if (parse_events__filter_pmu(parse_state, pmu)) if (parse_events__filter_pmu(parse_state, pmu))
continue; continue;
......
...@@ -29,7 +29,9 @@ ...@@ -29,7 +29,9 @@
#include "fncache.h" #include "fncache.h"
#include "util/evsel_config.h" #include "util/evsel_config.h"
struct perf_pmu perf_pmu__fake; struct perf_pmu perf_pmu__fake = {
.name = "fake",
};
#define UNIT_MAX_LEN 31 /* max length for event unit name */ #define UNIT_MAX_LEN 31 /* max length for event unit name */
...@@ -967,13 +969,13 @@ perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) ...@@ -967,13 +969,13 @@ perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
return NULL; return NULL;
} }
char * __weak const char * __weak
pmu_find_real_name(const char *name) pmu_find_real_name(const char *name)
{ {
return (char *)name; return name;
} }
char * __weak const char * __weak
pmu_find_alias_name(const char *name __maybe_unused) pmu_find_alias_name(const char *name __maybe_unused)
{ {
return NULL; return NULL;
...@@ -991,8 +993,8 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char ...@@ -991,8 +993,8 @@ struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char
{ {
struct perf_pmu *pmu; struct perf_pmu *pmu;
__u32 type; __u32 type;
char *name = pmu_find_real_name(lookup_name); const char *name = pmu_find_real_name(lookup_name);
char *alias_name; const char *alias_name;
pmu = zalloc(sizeof(*pmu)); pmu = zalloc(sizeof(*pmu));
if (!pmu) if (!pmu)
...@@ -1974,7 +1976,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, ...@@ -1974,7 +1976,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
name ?: "N/A", buf, config_name, config); name ?: "N/A", buf, config_name, config);
} }
int perf_pmu__match(char *pattern, char *name, char *tok) int perf_pmu__match(const char *pattern, const char *name, const char *tok)
{ {
if (!name) if (!name)
return -1; return -1;
......
...@@ -39,7 +39,7 @@ struct perf_pmu_caps { ...@@ -39,7 +39,7 @@ struct perf_pmu_caps {
*/ */
struct perf_pmu { struct perf_pmu {
/** @name: The name of the PMU such as "cpu". */ /** @name: The name of the PMU such as "cpu". */
char *name; const char *name;
/** /**
* @alias_name: Optional alternate name for the PMU determined in * @alias_name: Optional alternate name for the PMU determined in
* architecture specific code. * architecture specific code.
...@@ -249,10 +249,10 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, ...@@ -249,10 +249,10 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
const char *config_name); const char *config_name);
void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu); void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu);
int perf_pmu__match(char *pattern, char *name, char *tok); int perf_pmu__match(const char *pattern, const char *name, const char *tok);
char *pmu_find_real_name(const char *name); const char *pmu_find_real_name(const char *name);
char *pmu_find_alias_name(const char *name); const char *pmu_find_alias_name(const char *name);
double perf_pmu__cpu_slots_per_cycle(void); double perf_pmu__cpu_slots_per_cycle(void);
int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size); int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size);
int perf_pmu__pathname_scnprintf(char *buf, size_t size, int perf_pmu__pathname_scnprintf(char *buf, size_t size,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment