Skip to content

Commit c091ee9

Browse files
captain5050acmel
authored andcommitted
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: Ian 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: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 9897009 commit c091ee9

15 files changed

Lines changed: 51 additions & 62 deletions

File tree

tools/perf/arch/arm64/util/mem-events.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct perf_mem_event *perf_mem_events__ptr(int i)
2020
return &perf_mem_events[i];
2121
}
2222

23-
char *perf_mem_events__name(int i, char *pmu_name __maybe_unused)
23+
const char *perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
2424
{
2525
struct perf_mem_event *e = perf_mem_events__ptr(i);
2626

tools/perf/arch/powerpc/util/mem-events.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include "mem-events.h"
44

55
/* PowerPC does not support 'ldlat' parameter. */
6-
char *perf_mem_events__name(int i, char *pmu_name __maybe_unused)
6+
const char *perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
77
{
88
if (i == PERF_MEM_EVENTS__LOAD)
9-
return (char *) "cpu/mem-loads/";
9+
return "cpu/mem-loads/";
1010

11-
return (char *) "cpu/mem-stores/";
11+
return "cpu/mem-stores/";
1212
}

tools/perf/arch/x86/util/mem-events.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool is_mem_loads_aux_event(struct evsel *leader)
5252
return leader->core.attr.config == MEM_LOADS_AUX;
5353
}
5454

55-
char *perf_mem_events__name(int i, char *pmu_name)
55+
const char *perf_mem_events__name(int i, const char *pmu_name)
5656
{
5757
struct perf_mem_event *e = perf_mem_events__ptr(i);
5858

@@ -65,7 +65,7 @@ char *perf_mem_events__name(int i, char *pmu_name)
6565

6666
if (!pmu_name) {
6767
mem_loads_name__init = true;
68-
pmu_name = (char *)"cpu";
68+
pmu_name = "cpu";
6969
}
7070

7171
if (perf_pmus__have_event(pmu_name, "mem-loads-aux")) {
@@ -82,12 +82,12 @@ char *perf_mem_events__name(int i, char *pmu_name)
8282

8383
if (i == PERF_MEM_EVENTS__STORE) {
8484
if (!pmu_name)
85-
pmu_name = (char *)"cpu";
85+
pmu_name = "cpu";
8686

8787
scnprintf(mem_stores_name, sizeof(mem_stores_name),
8888
e->name, pmu_name);
8989
return mem_stores_name;
9090
}
9191

92-
return (char *)e->name;
92+
return e->name;
9393
}

tools/perf/arch/x86/util/pmu.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static int setup_pmu_alias_list(void)
126126
return ret;
127127
}
128128

129-
static char *__pmu_find_real_name(const char *name)
129+
static const char *__pmu_find_real_name(const char *name)
130130
{
131131
struct pmu_alias *pmu_alias;
132132

@@ -135,10 +135,10 @@ static char *__pmu_find_real_name(const char *name)
135135
return pmu_alias->name;
136136
}
137137

138-
return (char *)name;
138+
return name;
139139
}
140140

141-
char *pmu_find_real_name(const char *name)
141+
const char *pmu_find_real_name(const char *name)
142142
{
143143
if (cached_list)
144144
return __pmu_find_real_name(name);
@@ -149,7 +149,7 @@ char *pmu_find_real_name(const char *name)
149149
return __pmu_find_real_name(name);
150150
}
151151

152-
static char *__pmu_find_alias_name(const char *name)
152+
static const char *__pmu_find_alias_name(const char *name)
153153
{
154154
struct pmu_alias *pmu_alias;
155155

@@ -160,7 +160,7 @@ static char *__pmu_find_alias_name(const char *name)
160160
return NULL;
161161
}
162162

163-
char *pmu_find_alias_name(const char *name)
163+
const char *pmu_find_alias_name(const char *name)
164164
{
165165
if (cached_list)
166166
return __pmu_find_alias_name(name);

tools/perf/builtin-list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ int cmd_list(int argc, const char **argv)
513513
ret = -1;
514514
goto out;
515515
}
516-
default_ps.pmu_glob = pmu->name;
516+
default_ps.pmu_glob = strdup(pmu->name);
517517
}
518518
}
519519
print_cb.print_start(ps);

tools/perf/tests/pmu-events.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ static int test_core_pmu_event_aliases_cb(void *state, struct pmu_event_info *al
522522
}
523523

524524
/* Verify aliases are as expected */
525-
static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
525+
static int __test_core_pmu_event_aliases(const char *pmu_name, int *count)
526526
{
527527
struct perf_pmu_test_event const **test_event_table;
528528
struct perf_pmu *pmu;
@@ -635,7 +635,7 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
635635
static struct perf_pmu_test_pmu test_pmus[] = {
636636
{
637637
.pmu = {
638-
.name = (char *)"hisi_sccl1_ddrc2",
638+
.name = "hisi_sccl1_ddrc2",
639639
.is_uncore = 1,
640640
},
641641
.aliases = {
@@ -644,7 +644,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
644644
},
645645
{
646646
.pmu = {
647-
.name = (char *)"uncore_cbox_0",
647+
.name = "uncore_cbox_0",
648648
.is_uncore = 1,
649649
},
650650
.aliases = {
@@ -655,7 +655,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
655655
},
656656
{
657657
.pmu = {
658-
.name = (char *)"hisi_sccl3_l3c7",
658+
.name = "hisi_sccl3_l3c7",
659659
.is_uncore = 1,
660660
},
661661
.aliases = {
@@ -664,7 +664,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
664664
},
665665
{
666666
.pmu = {
667-
.name = (char *)"uncore_imc_free_running_0",
667+
.name = "uncore_imc_free_running_0",
668668
.is_uncore = 1,
669669
},
670670
.aliases = {
@@ -673,7 +673,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
673673
},
674674
{
675675
.pmu = {
676-
.name = (char *)"uncore_imc_0",
676+
.name = "uncore_imc_0",
677677
.is_uncore = 1,
678678
},
679679
.aliases = {
@@ -682,7 +682,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
682682
},
683683
{
684684
.pmu = {
685-
.name = (char *)"uncore_sys_ddr_pmu0",
685+
.name = "uncore_sys_ddr_pmu0",
686686
.is_uncore = 1,
687687
.id = (char *)"v8",
688688
},
@@ -692,7 +692,7 @@ static struct perf_pmu_test_pmu test_pmus[] = {
692692
},
693693
{
694694
.pmu = {
695-
.name = (char *)"uncore_sys_ccn_pmu4",
695+
.name = "uncore_sys_ccn_pmu4",
696696
.is_uncore = 1,
697697
.id = (char *)"0x01",
698698
},

tools/perf/util/env.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,9 @@ int perf_env__read_pmu_mappings(struct perf_env *env)
324324
u32 pmu_num = 0;
325325
struct strbuf sb;
326326

327-
while ((pmu = perf_pmus__scan(pmu))) {
328-
if (!pmu->name)
329-
continue;
327+
while ((pmu = perf_pmus__scan(pmu)))
330328
pmu_num++;
331-
}
329+
332330
if (!pmu_num) {
333331
pr_debug("pmu mappings not available\n");
334332
return -ENOENT;
@@ -339,8 +337,6 @@ int perf_env__read_pmu_mappings(struct perf_env *env)
339337
return -ENOMEM;
340338

341339
while ((pmu = perf_pmus__scan(pmu))) {
342-
if (!pmu->name)
343-
continue;
344340
if (strbuf_addf(&sb, "%u:%s", pmu->type, pmu->name) < 0)
345341
goto error;
346342
/* include a NULL character at the end */

tools/perf/util/header.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -746,20 +746,14 @@ static int write_pmu_mappings(struct feat_fd *ff,
746746
* Do a first pass to count number of pmu to avoid lseek so this
747747
* works in pipe mode as well.
748748
*/
749-
while ((pmu = perf_pmus__scan(pmu))) {
750-
if (!pmu->name)
751-
continue;
749+
while ((pmu = perf_pmus__scan(pmu)))
752750
pmu_num++;
753-
}
754751

755752
ret = do_write(ff, &pmu_num, sizeof(pmu_num));
756753
if (ret < 0)
757754
return ret;
758755

759756
while ((pmu = perf_pmus__scan(pmu))) {
760-
if (!pmu->name)
761-
continue;
762-
763757
ret = do_write(ff, &pmu->type, sizeof(pmu->type));
764758
if (ret < 0)
765759
return ret;

tools/perf/util/mem-events.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct perf_mem_event * __weak perf_mem_events__ptr(int i)
3737
return &perf_mem_events[i];
3838
}
3939

40-
char * __weak perf_mem_events__name(int i, char *pmu_name __maybe_unused)
40+
const char * __weak perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
4141
{
4242
struct perf_mem_event *e = perf_mem_events__ptr(i);
4343

@@ -53,7 +53,7 @@ char * __weak perf_mem_events__name(int i, char *pmu_name __maybe_unused)
5353
return mem_loads_name;
5454
}
5555

56-
return (char *)e->name;
56+
return e->name;
5757
}
5858

5959
__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,
186186
int i = *argv_nr, k = 0;
187187
struct perf_mem_event *e;
188188
struct perf_pmu *pmu;
189-
char *s;
190189

191190
for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
192191
e = perf_mem_events__ptr(j);
@@ -209,15 +208,16 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr,
209208
}
210209

211210
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
211+
const char *s = perf_mem_events__name(j, pmu->name);
212+
212213
rec_argv[i++] = "-e";
213-
s = perf_mem_events__name(j, pmu->name);
214214
if (s) {
215-
s = strdup(s);
216-
if (!s)
215+
char *copy = strdup(s);
216+
if (!copy)
217217
return -1;
218218

219-
rec_argv[i++] = s;
220-
rec_tmp[k++] = s;
219+
rec_argv[i++] = copy;
220+
rec_tmp[k++] = copy;
221221
}
222222
}
223223
}

tools/perf/util/mem-events.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern unsigned int perf_mem_events__loads_ldlat;
3838
int perf_mem_events__parse(const char *str);
3939
int perf_mem_events__init(void);
4040

41-
char *perf_mem_events__name(int i, char *pmu_name);
41+
const char *perf_mem_events__name(int i, const char *pmu_name);
4242
struct perf_mem_event *perf_mem_events__ptr(int i);
4343
bool is_mem_loads_aux_event(struct evsel *leader);
4444

0 commit comments

Comments
 (0)