perf pmu: Remove use of perf_cpu_map__read()
authorIan Rogers <irogers@google.com>
Fri, 6 Dec 2024 04:40:32 +0000 (20:40 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 9 Dec 2024 20:52:41 +0000 (17:52 -0300)
Remove use of a FILE and switch to reading a string that is then
passed to perf_cpu_map__new().

Being able to remove perf_cpu_map__read() avoids duplicated parsing logic.

Reviewed-by: Leo Yan <leo.yan@arm.com>
Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ben Gainey <ben.gainey@arm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Kyle Meyer <kyle.meyer@hpe.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20241206044035.1062032-6-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/pmu.c

index 08a9d0bd93017dcc0b1970aea18e7e3525209383..891c905d08a1998425a7366d39e116842b4ad290 100644 (file)
@@ -12,6 +12,7 @@
 #include <stdbool.h>
 #include <dirent.h>
 #include <api/fs/fs.h>
+#include <api/io.h>
 #include <locale.h>
 #include <fnmatch.h>
 #include <math.h>
@@ -748,26 +749,35 @@ static int pmu_alias_terms(struct perf_pmu_alias *alias, int err_loc, struct lis
  * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64)
  * may have a "cpus" file.
  */
-static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core)
+static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *pmu_name, bool is_core)
 {
-       struct perf_cpu_map *cpus;
        const char *templates[] = {
                "cpumask",
                "cpus",
                NULL
        };
        const char **template;
-       char pmu_name[PATH_MAX];
-       struct perf_pmu pmu = {.name = pmu_name};
-       FILE *file;
 
-       strlcpy(pmu_name, name, sizeof(pmu_name));
        for (template = templates; *template; template++) {
-               file = perf_pmu__open_file_at(&pmu, dirfd, *template);
-               if (!file)
+               struct io io;
+               char buf[128];
+               char *cpumask = NULL;
+               size_t cpumask_len;
+               ssize_t ret;
+               struct perf_cpu_map *cpus;
+
+               io.fd = perf_pmu__pathname_fd(dirfd, pmu_name, *template, O_RDONLY);
+               if (io.fd < 0)
                        continue;
-               cpus = perf_cpu_map__read(file);
-               fclose(file);
+
+               io__init(&io, io.fd, buf, sizeof(buf));
+               ret = io__getline(&io, &cpumask, &cpumask_len);
+               close(io.fd);
+               if (ret < 0)
+                       continue;
+
+               cpus = perf_cpu_map__new(cpumask);
+               free(cpumask);
                if (cpus)
                        return cpus;
        }