Skip to content

Commit 0dafc84

Browse files
Ravi Bangoriagregkh
authored andcommitted
perf tool x86: Consolidate is_amd check into single function
[ Upstream commit 0cd1ca4 ] There are multiple places where x86 specific code determines AMD vs Intel arch and acts based on that. Consolidate those checks into a single function. Signed-off-by: Ravi Bangoria <ravi.bangoria@amd.com> Acked-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Ali Saidi <alisaidi@amazon.com> Cc: Ananth Narayan <ananth.narayan@amd.com> Cc: James Clark <james.clark@arm.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Leo Yan <leo.yan@linaro.org> Cc: Madhavan Srinivasan <maddy@linux.ibm.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Sandipan Das <sandipan.das@amd.com> Cc: Santosh Shukla <santosh.shukla@amd.com> Link: https://lore.kernel.org/r/20230613095506.547-3-ravi.bangoria@amd.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Stable-dep-of: 99d4850 ("perf tool x86: Fix perf_env memory leak") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c94376d commit 0dafc84

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

tools/perf/arch/x86/util/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ perf-y += evlist.o
1010
perf-y += mem-events.o
1111
perf-y += evsel.o
1212
perf-y += iostat.o
13+
perf-y += env.o
1314

1415
perf-$(CONFIG_DWARF) += dwarf-regs.o
1516
perf-$(CONFIG_BPF_PROLOGUE) += dwarf-regs.o

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "linux/string.h"
3+
#include "util/env.h"
4+
#include "env.h"
5+
6+
bool x86__is_amd_cpu(void)
7+
{
8+
struct perf_env env = { .total_mem = 0, };
9+
static int is_amd; /* 0: Uninitialized, 1: Yes, -1: No */
10+
11+
if (is_amd)
12+
goto ret;
13+
14+
perf_env__cpuid(&env);
15+
is_amd = env.cpuid && strstarts(env.cpuid, "AuthenticAMD") ? 1 : -1;
16+
17+
ret:
18+
return is_amd >= 1 ? true : false;
19+
}

tools/perf/arch/x86/util/env.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _X86_ENV_H
3+
#define _X86_ENV_H
4+
5+
bool x86__is_amd_cpu(void);
6+
7+
#endif /* _X86_ENV_H */

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "linux/string.h"
88
#include "evsel.h"
99
#include "util/debug.h"
10+
#include "env.h"
1011

1112
#define IBS_FETCH_L3MISSONLY (1ULL << 59)
1213
#define IBS_OP_L3MISSONLY (1ULL << 16)
@@ -97,23 +98,10 @@ void arch__post_evsel_config(struct evsel *evsel, struct perf_event_attr *attr)
9798
{
9899
struct perf_pmu *evsel_pmu, *ibs_fetch_pmu, *ibs_op_pmu;
99100
static int warned_once;
100-
/* 0: Uninitialized, 1: Yes, -1: No */
101-
static int is_amd;
102101

103-
if (warned_once || is_amd == -1)
102+
if (warned_once || !x86__is_amd_cpu())
104103
return;
105104

106-
if (!is_amd) {
107-
struct perf_env *env = evsel__env(evsel);
108-
109-
if (!perf_env__cpuid(env) || !env->cpuid ||
110-
!strstarts(env->cpuid, "AuthenticAMD")) {
111-
is_amd = -1;
112-
return;
113-
}
114-
is_amd = 1;
115-
}
116-
117105
evsel_pmu = evsel__find_pmu(evsel);
118106
if (!evsel_pmu)
119107
return;

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "map_symbol.h"
55
#include "mem-events.h"
66
#include "linux/string.h"
7+
#include "env.h"
78

89
static char mem_loads_name[100];
910
static bool mem_loads_name__init;
@@ -26,28 +27,12 @@ static struct perf_mem_event perf_mem_events_amd[PERF_MEM_EVENTS__MAX] = {
2627
E("mem-ldst", "ibs_op//", "ibs_op"),
2728
};
2829

29-
static int perf_mem_is_amd_cpu(void)
30-
{
31-
struct perf_env env = { .total_mem = 0, };
32-
33-
perf_env__cpuid(&env);
34-
if (env.cpuid && strstarts(env.cpuid, "AuthenticAMD"))
35-
return 1;
36-
return -1;
37-
}
38-
3930
struct perf_mem_event *perf_mem_events__ptr(int i)
4031
{
41-
/* 0: Uninitialized, 1: Yes, -1: No */
42-
static int is_amd;
43-
4432
if (i >= PERF_MEM_EVENTS__MAX)
4533
return NULL;
4634

47-
if (!is_amd)
48-
is_amd = perf_mem_is_amd_cpu();
49-
50-
if (is_amd == 1)
35+
if (x86__is_amd_cpu())
5136
return &perf_mem_events_amd[i];
5237

5338
return &perf_mem_events_intel[i];

0 commit comments

Comments
 (0)