Commit f8ad6018 authored by James Clark's avatar James Clark Committed by Arnaldo Carvalho de Melo

perf pmu: Remove duplication around EVENT_SOURCE_DEVICE_PATH

The pattern for accessing EVENT_SOURCE_DEVICE_PATH is duplicated in a
few places, so add two utility functions to cover it. Also just use
perf_pmu__scan_file() instead of pmu_type() which already does the same
thing.

No functional changes.
Reviewed-by: default avatarLeo Yan <leo.yan@linaro.org>
Signed-off-by: default avatarJames Clark <james.clark@arm.com>
Acked-by: default avatarSuzuki Poulouse <suzuki.poulose@arm.com>
Tested-by: default avatarTanmay Jagdale <tanmay@marvell.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Bharat Bhushan <bbhushan2@marvell.com>
Cc: George Cherian <gcherian@marvell.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Linu Cherian <lcherian@marvell.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sunil Kovvuri Goutham <sgoutham@marvell.com>
Cc: Will Deacon <will@kernel.org>
Cc: coresight@lists.linaro.org
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20230120143702.4035046-2-james.clark@arm.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 4cbd5334
...@@ -55,17 +55,16 @@ static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err) ...@@ -55,17 +55,16 @@ static struct perf_pmu **find_all_arm_spe_pmus(int *nr_spes, int *err)
static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err) static struct perf_pmu **find_all_hisi_ptt_pmus(int *nr_ptts, int *err)
{ {
const char *sysfs = sysfs__mountpoint();
struct perf_pmu **hisi_ptt_pmus = NULL; struct perf_pmu **hisi_ptt_pmus = NULL;
struct dirent *dent; struct dirent *dent;
char path[PATH_MAX]; char path[PATH_MAX];
DIR *dir = NULL; DIR *dir = NULL;
int idx = 0; int idx = 0;
snprintf(path, PATH_MAX, "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); perf_pmu__event_source_devices_scnprintf(path, sizeof(path));
dir = opendir(path); dir = opendir(path);
if (!dir) { if (!dir) {
pr_err("can't read directory '%s'\n", EVENT_SOURCE_DEVICE_PATH); pr_err("can't read directory '%s'\n", path);
*err = -EINVAL; *err = -EINVAL;
return NULL; return NULL;
} }
......
...@@ -44,8 +44,8 @@ double perf_pmu__cpu_slots_per_cycle(void) ...@@ -44,8 +44,8 @@ double perf_pmu__cpu_slots_per_cycle(void)
struct perf_pmu *pmu = pmu__find_core_pmu(); struct perf_pmu *pmu = pmu__find_core_pmu();
if (pmu) { if (pmu) {
scnprintf(path, PATH_MAX, perf_pmu__pathname_scnprintf(path, sizeof(path),
EVENT_SOURCE_DEVICE_PATH "%s/caps/slots", pmu->name); pmu->name, "caps/slots");
/* /*
* The value of slots is not greater than 32 bits, but sysfs__read_int * The value of slots is not greater than 32 bits, but sysfs__read_int
* can't read value with 0x prefix, so use sysfs__read_ull instead. * can't read value with 0x prefix, so use sysfs__read_ull instead.
......
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
#include "../../../util/pmu.h" #include "../../../util/pmu.h"
#include "../../../util/fncache.h" #include "../../../util/fncache.h"
#define TEMPLATE_ALIAS "%s/bus/event_source/devices/%s/alias"
struct pmu_alias { struct pmu_alias {
char *name; char *name;
char *alias; char *alias;
...@@ -72,18 +70,14 @@ static int setup_pmu_alias_list(void) ...@@ -72,18 +70,14 @@ static int setup_pmu_alias_list(void)
char path[PATH_MAX]; char path[PATH_MAX];
DIR *dir; DIR *dir;
struct dirent *dent; struct dirent *dent;
const char *sysfs = sysfs__mountpoint();
struct pmu_alias *pmu_alias; struct pmu_alias *pmu_alias;
char buf[MAX_PMU_NAME_LEN]; char buf[MAX_PMU_NAME_LEN];
FILE *file; FILE *file;
int ret = -ENOMEM; int ret = -ENOMEM;
if (!sysfs) if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path)))
return -1; return -1;
snprintf(path, PATH_MAX,
"%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
dir = opendir(path); dir = opendir(path);
if (!dir) if (!dir)
return -errno; return -errno;
...@@ -93,9 +87,7 @@ static int setup_pmu_alias_list(void) ...@@ -93,9 +87,7 @@ static int setup_pmu_alias_list(void)
!strcmp(dent->d_name, "..")) !strcmp(dent->d_name, ".."))
continue; continue;
snprintf(path, PATH_MAX, perf_pmu__pathname_scnprintf(path, sizeof(path), dent->d_name, "alias");
TEMPLATE_ALIAS, sysfs, dent->d_name);
if (!file_available(path)) if (!file_available(path))
continue; continue;
......
...@@ -108,14 +108,10 @@ int perf_pmu__format_parse(char *dir, struct list_head *head) ...@@ -108,14 +108,10 @@ int perf_pmu__format_parse(char *dir, struct list_head *head)
static int pmu_format(const char *name, struct list_head *format) static int pmu_format(const char *name, struct list_head *format)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
const char *sysfs = sysfs__mountpoint();
if (!sysfs) if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "format"))
return -1; return -1;
snprintf(path, PATH_MAX,
"%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
if (!file_available(path)) if (!file_available(path))
return 0; return 0;
...@@ -514,14 +510,10 @@ static int pmu_aliases_parse(char *dir, struct list_head *head) ...@@ -514,14 +510,10 @@ static int pmu_aliases_parse(char *dir, struct list_head *head)
static int pmu_aliases(const char *name, struct list_head *head) static int pmu_aliases(const char *name, struct list_head *head)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
const char *sysfs = sysfs__mountpoint();
if (!sysfs) if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "events"))
return -1; return -1;
snprintf(path, PATH_MAX,
"%s/bus/event_source/devices/%s/events", sysfs, name);
if (!file_available(path)) if (!file_available(path))
return 0; return 0;
...@@ -555,52 +547,16 @@ static int pmu_alias_terms(struct perf_pmu_alias *alias, ...@@ -555,52 +547,16 @@ static int pmu_alias_terms(struct perf_pmu_alias *alias,
return 0; return 0;
} }
/*
* Reading/parsing the default pmu type value, which should be
* located at:
* /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
*/
static int pmu_type(const char *name, __u32 *type)
{
char path[PATH_MAX];
FILE *file;
int ret = 0;
const char *sysfs = sysfs__mountpoint();
if (!sysfs)
return -1;
snprintf(path, PATH_MAX,
"%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
if (access(path, R_OK) < 0)
return -1;
file = fopen(path, "r");
if (!file)
return -EINVAL;
if (1 != fscanf(file, "%u", type))
ret = -1;
fclose(file);
return ret;
}
/* Add all pmus in sysfs to pmu list: */ /* Add all pmus in sysfs to pmu list: */
static void pmu_read_sysfs(void) static void pmu_read_sysfs(void)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
DIR *dir; DIR *dir;
struct dirent *dent; struct dirent *dent;
const char *sysfs = sysfs__mountpoint();
if (!sysfs) if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path)))
return; return;
snprintf(path, PATH_MAX,
"%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
dir = opendir(path); dir = opendir(path);
if (!dir) if (!dir)
return; return;
...@@ -697,14 +653,9 @@ static char *pmu_id(const char *name) ...@@ -697,14 +653,9 @@ static char *pmu_id(const char *name)
static int is_arm_pmu_core(const char *name) static int is_arm_pmu_core(const char *name)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
const char *sysfs = sysfs__mountpoint();
if (!sysfs) if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus"))
return 0; return 0;
/* Look for cpu sysfs (specific to arm) */
scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus",
sysfs, name);
return file_available(path); return file_available(path);
} }
...@@ -970,11 +921,8 @@ static struct perf_pmu *pmu_lookup(const char *lookup_name) ...@@ -970,11 +921,8 @@ static struct perf_pmu *pmu_lookup(const char *lookup_name)
return NULL; return NULL;
/* /*
* Check the type first to avoid unnecessary work. * Check the aliases first to avoid unnecessary work.
*/ */
if (pmu_type(name, &type))
return NULL;
if (pmu_aliases(name, &aliases)) if (pmu_aliases(name, &aliases))
return NULL; return NULL;
...@@ -984,9 +932,14 @@ static struct perf_pmu *pmu_lookup(const char *lookup_name) ...@@ -984,9 +932,14 @@ static struct perf_pmu *pmu_lookup(const char *lookup_name)
pmu->cpus = pmu_cpumask(name); pmu->cpus = pmu_cpumask(name);
pmu->name = strdup(name); pmu->name = strdup(name);
if (!pmu->name) if (!pmu->name)
goto err; goto err;
/* Read type, and ensure that type value is successfully assigned (return 1) */
if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1)
goto err;
alias_name = pmu_find_alias_name(name); alias_name = pmu_find_alias_name(name);
if (alias_name) { if (alias_name) {
pmu->alias_name = strdup(alias_name); pmu->alias_name = strdup(alias_name);
...@@ -1787,16 +1740,11 @@ bool pmu_have_event(const char *pname, const char *name) ...@@ -1787,16 +1740,11 @@ bool pmu_have_event(const char *pname, const char *name)
static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
{ {
char path[PATH_MAX]; char path[PATH_MAX];
const char *sysfs;
sysfs = sysfs__mountpoint(); if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) ||
if (!sysfs) !file_available(path))
return NULL; return NULL;
snprintf(path, PATH_MAX,
"%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
if (!file_available(path))
return NULL;
return fopen(path, "r"); return fopen(path, "r");
} }
...@@ -1850,7 +1798,6 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) ...@@ -1850,7 +1798,6 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
{ {
struct stat st; struct stat st;
char caps_path[PATH_MAX]; char caps_path[PATH_MAX];
const char *sysfs = sysfs__mountpoint();
DIR *caps_dir; DIR *caps_dir;
struct dirent *evt_ent; struct dirent *evt_ent;
...@@ -1859,12 +1806,9 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) ...@@ -1859,12 +1806,9 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
pmu->nr_caps = 0; pmu->nr_caps = 0;
if (!sysfs) if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps"))
return -1; return -1;
snprintf(caps_path, PATH_MAX,
"%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name);
if (stat(caps_path, &st) < 0) { if (stat(caps_path, &st) < 0) {
pmu->caps_initialized = true; pmu->caps_initialized = true;
return 0; /* no error if caps does not exist */ return 0; /* no error if caps does not exist */
...@@ -1999,3 +1943,31 @@ double __weak perf_pmu__cpu_slots_per_cycle(void) ...@@ -1999,3 +1943,31 @@ double __weak perf_pmu__cpu_slots_per_cycle(void)
{ {
return NAN; return NAN;
} }
int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size)
{
const char *sysfs = sysfs__mountpoint();
if (!sysfs)
return 0;
return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs);
}
/*
* Fill 'buf' with the path to a file or folder in 'pmu_name' in
* sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format"
* then pathname will be filled with
* "/sys/bus/event_source/devices/cs_etm/format"
*
* Return 0 if the sysfs mountpoint couldn't be found or if no
* characters were written.
*/
int perf_pmu__pathname_scnprintf(char *buf, size_t size,
const char *pmu_name, const char *filename)
{
char base_path[PATH_MAX];
if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
return 0;
return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);
}
...@@ -22,7 +22,6 @@ enum { ...@@ -22,7 +22,6 @@ enum {
}; };
#define PERF_PMU_FORMAT_BITS 64 #define PERF_PMU_FORMAT_BITS 64
#define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
#define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus" #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus"
#define MAX_PMU_NAME_LEN 128 #define MAX_PMU_NAME_LEN 128
...@@ -260,4 +259,8 @@ int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus, ...@@ -260,4 +259,8 @@ int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus,
char *pmu_find_real_name(const char *name); char *pmu_find_real_name(const char *name);
char *pmu_find_alias_name(const char *name); 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__pathname_scnprintf(char *buf, size_t size,
const char *pmu_name, const char *filename);
#endif /* __PMU_H */ #endif /* __PMU_H */
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