[PATCH 1/3 v2] perf: add cpu__max_present_cpu()

From: Jan Stancek
Date: Tue Jan 31 2017 - 08:41:46 EST


Similar to cpu__max_cpu() (which returns max possible CPU),
returns max present CPU.

Signed-off-by: Jan Stancek <jstancek@xxxxxxxxxx>
---
tools/perf/util/cpumap.c | 22 ++++++++++++++++++++++
tools/perf/util/cpumap.h | 1 +
2 files changed, 23 insertions(+)

diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
index 2c0b52264a46..8c7504939113 100644
--- a/tools/perf/util/cpumap.c
+++ b/tools/perf/util/cpumap.c
@@ -9,6 +9,7 @@
#include "asm/bug.h"

static int max_cpu_num;
+static int max_present_cpu_num;
static int max_node_num;
static int *cpunode_map;

@@ -442,6 +443,7 @@ static void set_max_cpu_num(void)

/* set up default */
max_cpu_num = 4096;
+ max_present_cpu_num = 4096;

mnt = sysfs__mountpoint();
if (!mnt)
@@ -455,6 +457,17 @@ static void set_max_cpu_num(void)
}

ret = get_max_num(path, &max_cpu_num);
+ if (ret)
+ goto out;
+
+ /* get the highest present cpu number for a sparse allocation */
+ ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
+ if (ret == PATH_MAX) {
+ pr_err("sysfs path crossed PATH_MAX(%d) size\n", PATH_MAX);
+ goto out;
+ }
+
+ ret = get_max_num(path, &max_present_cpu_num);

out:
if (ret)
@@ -505,6 +518,15 @@ int cpu__max_cpu(void)
return max_cpu_num;
}

+int cpu__max_present_cpu(void)
+{
+ if (unlikely(!max_present_cpu_num))
+ set_max_cpu_num();
+
+ return max_present_cpu_num;
+}
+
+
int cpu__get_node(int cpu)
{
if (unlikely(cpunode_map == NULL)) {
diff --git a/tools/perf/util/cpumap.h b/tools/perf/util/cpumap.h
index 06bd689f5989..1a0549af8f5c 100644
--- a/tools/perf/util/cpumap.h
+++ b/tools/perf/util/cpumap.h
@@ -62,6 +62,7 @@ static inline bool cpu_map__empty(const struct cpu_map *map)

int cpu__max_node(void);
int cpu__max_cpu(void);
+int cpu__max_present_cpu(void);
int cpu__get_node(int cpu);

int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
--
1.8.3.1


--------------E9B8192AB6D6402A1340922B
Content-Type: text/x-patch;
name="0002-perf-make-build_cpu_topology-skip-offline-absent-CPU.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename*0="0002-perf-make-build_cpu_topology-skip-offline-absent-CPU.pa";
filename*1="tch"