Commit c6fafe36 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

perf header: Move is_cpu_online to numa bench



The helper function is only used in the NUMA benchmark as typically
online CPUs are determined through perf_cpu_map__new_online_cpus().

Reduce the scope of the function for now.

Reviewed-by: default avatarJames Clark <james.clark@linaro.org>
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Tested-by: default avatarXu Yang <xu.yang_2@nxp.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alexghiti@rivosinc.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ben Zong-You Xie <ben717@andestech.com>
Cc: Benjamin Gray <bgray@linux.ibm.com>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Clément Le Goffic <clement.legoffic@foss.st.com>
Cc: Dima Kogan <dima@secretsauce.net>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yicong Yang <yangyicong@hisilicon.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-riscv@lists.infradead.org
Link: https://lore.kernel.org/r/20241107162035.52206-3-irogers@google.com


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 4a159e60
Loading
Loading
Loading
Loading
+53 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/kernel.h>
#include <linux/time64.h>
@@ -35,6 +36,7 @@

#include "../util/header.h"
#include "../util/mutex.h"
#include <api/fs/fs.h>
#include <numa.h>
#include <numaif.h>

@@ -533,6 +535,57 @@ static int parse_cpu_list(const char *arg)
	return 0;
}

/*
 * Check whether a CPU is online
 *
 * Returns:
 *     1 -> if CPU is online
 *     0 -> if CPU is offline
 *    -1 -> error case
 */
static int is_cpu_online(unsigned int cpu)
{
	char *str;
	size_t strlen;
	char buf[256];
	int status = -1;
	struct stat statbuf;

	snprintf(buf, sizeof(buf),
		"/sys/devices/system/cpu/cpu%d", cpu);
	if (stat(buf, &statbuf) != 0)
		return 0;

	/*
	 * Check if /sys/devices/system/cpu/cpux/online file
	 * exists. Some cases cpu0 won't have online file since
	 * it is not expected to be turned off generally.
	 * In kernels without CONFIG_HOTPLUG_CPU, this
	 * file won't exist
	 */
	snprintf(buf, sizeof(buf),
		"/sys/devices/system/cpu/cpu%d/online", cpu);
	if (stat(buf, &statbuf) != 0)
		return 1;

	/*
	 * Read online file using sysfs__read_str.
	 * If read or open fails, return -1.
	 * If read succeeds, return value from file
	 * which gets stored in "str"
	 */
	snprintf(buf, sizeof(buf),
		"devices/system/cpu/cpu%d/online", cpu);

	if (sysfs__read_str(buf, &str, &strlen) < 0)
		return status;

	status = atoi(str);

	free(str);
	return status;
}

static int parse_setup_cpu_list(void)
{
	struct thread_data *td;
+0 −51
Original line number Diff line number Diff line
@@ -987,57 +987,6 @@ static int write_dir_format(struct feat_fd *ff,
	return do_write(ff, &data->dir.version, sizeof(data->dir.version));
}

/*
 * Check whether a CPU is online
 *
 * Returns:
 *     1 -> if CPU is online
 *     0 -> if CPU is offline
 *    -1 -> error case
 */
int is_cpu_online(unsigned int cpu)
{
	char *str;
	size_t strlen;
	char buf[256];
	int status = -1;
	struct stat statbuf;

	snprintf(buf, sizeof(buf),
		"/sys/devices/system/cpu/cpu%d", cpu);
	if (stat(buf, &statbuf) != 0)
		return 0;

	/*
	 * Check if /sys/devices/system/cpu/cpux/online file
	 * exists. Some cases cpu0 won't have online file since
	 * it is not expected to be turned off generally.
	 * In kernels without CONFIG_HOTPLUG_CPU, this
	 * file won't exist
	 */
	snprintf(buf, sizeof(buf),
		"/sys/devices/system/cpu/cpu%d/online", cpu);
	if (stat(buf, &statbuf) != 0)
		return 1;

	/*
	 * Read online file using sysfs__read_str.
	 * If read or open fails, return -1.
	 * If read succeeds, return value from file
	 * which gets stored in "str"
	 */
	snprintf(buf, sizeof(buf),
		"devices/system/cpu/cpu%d/online", cpu);

	if (sysfs__read_str(buf, &str, &strlen) < 0)
		return status;

	status = atoi(str);

	free(str);
	return status;
}

#ifdef HAVE_LIBBPF_SUPPORT
static int write_bpf_prog_info(struct feat_fd *ff,
			       struct evlist *evlist __maybe_unused)
+0 −1
Original line number Diff line number Diff line
@@ -196,7 +196,6 @@ int write_padded(struct feat_fd *fd, const void *bf,

#define MAX_CACHE_LVL 4

int is_cpu_online(unsigned int cpu);
int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp);

/*