summaryrefslogtreecommitdiff
path: root/tools/perf/util/cpumap.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/cpumap.c')
-rw-r--r--tools/perf/util/cpumap.c97
1 files changed, 52 insertions, 45 deletions
diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c
index 3667e2123e5b..10af1e7524fb 100644
--- a/tools/perf/util/cpumap.c
+++ b/tools/perf/util/cpumap.c
@@ -203,6 +203,23 @@ struct cpu_map *cpu_map__dummy_new(void)
return cpus;
}
+struct cpu_map *cpu_map__empty_new(int nr)
+{
+ struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int) * nr);
+
+ if (cpus != NULL) {
+ int i;
+
+ cpus->nr = nr;
+ for (i = 0; i < nr; i++)
+ cpus->map[i] = -1;
+
+ atomic_set(&cpus->refcnt, 1);
+ }
+
+ return cpus;
+}
+
static void cpu_map__delete(struct cpu_map *map)
{
if (map) {
@@ -225,32 +242,32 @@ void cpu_map__put(struct cpu_map *map)
cpu_map__delete(map);
}
-int cpu_map__get_socket(struct cpu_map *map, int idx)
+static int cpu__get_topology_int(int cpu, const char *name, int *value)
{
- FILE *fp;
- const char *mnt;
char path[PATH_MAX];
- int cpu, ret;
- if (idx > map->nr)
- return -1;
+ snprintf(path, PATH_MAX,
+ "devices/system/cpu/cpu%d/topology/%s", cpu, name);
- cpu = map->map[idx];
+ return sysfs__read_int(path, value);
+}
- mnt = sysfs__mountpoint();
- if (!mnt)
- return -1;
+int cpu_map__get_socket_id(int cpu)
+{
+ int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
+ return ret ?: value;
+}
- snprintf(path, PATH_MAX,
- "%s/devices/system/cpu/cpu%d/topology/physical_package_id",
- mnt, cpu);
+int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
+{
+ int cpu;
- fp = fopen(path, "r");
- if (!fp)
+ if (idx > map->nr)
return -1;
- ret = fscanf(fp, "%d", &cpu);
- fclose(fp);
- return ret == 1 ? cpu : -1;
+
+ cpu = map->map[idx];
+
+ return cpu_map__get_socket_id(cpu);
}
static int cmp_ids(const void *a, const void *b)
@@ -258,8 +275,9 @@ static int cmp_ids(const void *a, const void *b)
return *(int *)a - *(int *)b;
}
-static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
- int (*f)(struct cpu_map *map, int cpu))
+int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
+ int (*f)(struct cpu_map *map, int cpu, void *data),
+ void *data)
{
struct cpu_map *c;
int nr = cpus->nr;
@@ -271,7 +289,7 @@ static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
return -1;
for (cpu = 0; cpu < nr; cpu++) {
- s1 = f(cpus, cpu);
+ s1 = f(cpus, cpu, data);
for (s2 = 0; s2 < c->nr; s2++) {
if (s1 == c->map[s2])
break;
@@ -284,40 +302,29 @@ static int cpu_map__build_map(struct cpu_map *cpus, struct cpu_map **res,
/* ensure we process id in increasing order */
qsort(c->map, c->nr, sizeof(int), cmp_ids);
- atomic_set(&cpus->refcnt, 1);
+ atomic_set(&c->refcnt, 1);
*res = c;
return 0;
}
-int cpu_map__get_core(struct cpu_map *map, int idx)
+int cpu_map__get_core_id(int cpu)
{
- FILE *fp;
- const char *mnt;
- char path[PATH_MAX];
- int cpu, ret, s;
+ int value, ret = cpu__get_topology_int(cpu, "core_id", &value);
+ return ret ?: value;
+}
+
+int cpu_map__get_core(struct cpu_map *map, int idx, void *data)
+{
+ int cpu, s;
if (idx > map->nr)
return -1;
cpu = map->map[idx];
- mnt = sysfs__mountpoint();
- if (!mnt)
- return -1;
-
- snprintf(path, PATH_MAX,
- "%s/devices/system/cpu/cpu%d/topology/core_id",
- mnt, cpu);
-
- fp = fopen(path, "r");
- if (!fp)
- return -1;
- ret = fscanf(fp, "%d", &cpu);
- fclose(fp);
- if (ret != 1)
- return -1;
+ cpu = cpu_map__get_core_id(cpu);
- s = cpu_map__get_socket(map, idx);
+ s = cpu_map__get_socket(map, idx, data);
if (s == -1)
return -1;
@@ -332,12 +339,12 @@ int cpu_map__get_core(struct cpu_map *map, int idx)
int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp)
{
- return cpu_map__build_map(cpus, sockp, cpu_map__get_socket);
+ return cpu_map__build_map(cpus, sockp, cpu_map__get_socket, NULL);
}
int cpu_map__build_core_map(struct cpu_map *cpus, struct cpu_map **corep)
{
- return cpu_map__build_map(cpus, corep, cpu_map__get_core);
+ return cpu_map__build_map(cpus, corep, cpu_map__get_core, NULL);
}
/* setup simple routines to easily access node numbers given a cpu number */