Files
linux/tools/perf/arch/riscv/util/header.c
T
Ian Rogers 494c403ff1 perf header: Pass a perf_cpu rather than a PMU to get_cpuid_str
On ARM the cpuid is dependent on the core type of the CPU in
question. The PMU was passed for the sake of the CPU map but this
means in places a temporary PMU is created just to pass a CPU
value. Just pass the CPU and fix up the callers.

As there are no longer PMU users in header.h, shuffle forward
declarations earlier to work around build failures.

Reviewed-by: James Clark <[email protected]>
Signed-off-by: Ian Rogers <[email protected]>
Tested-by: Xu Yang <[email protected]>
Cc: Adrian Hunter <[email protected]>
Cc: Albert Ou <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: Athira Rajeev <[email protected]>
Cc: Ben Zong-You Xie <[email protected]>
Cc: Benjamin Gray <[email protected]>
Cc: Bibo Mao <[email protected]>
Cc: Clément Le Goffic <[email protected]>
Cc: Dima Kogan <[email protected]>
Cc: Dr. David Alan Gilbert <[email protected]>
Cc: Huacai Chen <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: John Garry <[email protected]>
Cc: Kan Liang <[email protected]>
Cc: Leo Yan <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Masami Hiramatsu <[email protected]>
Cc: Mike Leach <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Paul Walmsley <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Ravi Bangoria <[email protected]>
Cc: Sandipan Das <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Yicong Yang <[email protected]>
Cc: [email protected]
Cc: [email protected]
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
2024-11-16 16:40:30 -03:00

105 lines
1.9 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* Implementation of get_cpuid().
*
* Author: Nikita Shubin <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <api/fs/fs.h>
#include <errno.h>
#include "../../util/debug.h"
#include "../../util/header.h"
#define CPUINFO_MVEN "mvendorid"
#define CPUINFO_MARCH "marchid"
#define CPUINFO_MIMP "mimpid"
#define CPUINFO "/proc/cpuinfo"
static char *_get_field(const char *line)
{
char *line2, *nl;
line2 = strrchr(line, ' ');
if (!line2)
return NULL;
line2++;
nl = strrchr(line, '\n');
if (!nl)
return NULL;
return strndup(line2, nl - line2);
}
static char *_get_cpuid(void)
{
char *line = NULL;
char *mvendorid = NULL;
char *marchid = NULL;
char *mimpid = NULL;
char *cpuid = NULL;
int read;
size_t line_sz;
FILE *cpuinfo;
cpuinfo = fopen(CPUINFO, "r");
if (cpuinfo == NULL)
return cpuid;
while ((read = getline(&line, &line_sz, cpuinfo)) != -1) {
if (!strncmp(line, CPUINFO_MVEN, strlen(CPUINFO_MVEN))) {
mvendorid = _get_field(line);
if (!mvendorid)
goto free;
} else if (!strncmp(line, CPUINFO_MARCH, strlen(CPUINFO_MARCH))) {
marchid = _get_field(line);
if (!marchid)
goto free;
} else if (!strncmp(line, CPUINFO_MIMP, strlen(CPUINFO_MIMP))) {
mimpid = _get_field(line);
if (!mimpid)
goto free;
break;
}
}
if (!mvendorid || !marchid || !mimpid)
goto free;
if (asprintf(&cpuid, "%s-%s-%s", mvendorid, marchid, mimpid) < 0)
cpuid = NULL;
free:
fclose(cpuinfo);
free(mvendorid);
free(marchid);
free(mimpid);
return cpuid;
}
int get_cpuid(char *buffer, size_t sz, struct perf_cpu cpu __maybe_unused)
{
char *cpuid = _get_cpuid();
int ret = 0;
if (sz < strlen(cpuid)) {
ret = -EINVAL;
goto free;
}
scnprintf(buffer, sz, "%s", cpuid);
free:
free(cpuid);
return ret;
}
char *
get_cpuid_str(struct perf_cpu cpu __maybe_unused)
{
return _get_cpuid();
}