summaryrefslogtreecommitdiffstats
path: root/kernel/tools/perf/util/cpumap.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/tools/perf/util/cpumap.c')
-rw-r--r--kernel/tools/perf/util/cpumap.c121
1 files changed, 75 insertions, 46 deletions
diff --git a/kernel/tools/perf/util/cpumap.c b/kernel/tools/perf/util/cpumap.c
index c4e55b710..10af1e752 100644
--- a/kernel/tools/perf/util/cpumap.c
+++ b/kernel/tools/perf/util/cpumap.c
@@ -5,6 +5,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
+#include "asm/bug.h"
static struct cpu_map *cpu_map__default_new(void)
{
@@ -22,6 +23,7 @@ static struct cpu_map *cpu_map__default_new(void)
cpus->map[i] = i;
cpus->nr = nr_cpus;
+ atomic_set(&cpus->refcnt, 1);
}
return cpus;
@@ -35,6 +37,7 @@ static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
if (cpus != NULL) {
cpus->nr = nr_cpus;
memcpy(cpus->map, tmp_cpus, payload_size);
+ atomic_set(&cpus->refcnt, 1);
}
return cpus;
@@ -194,42 +197,77 @@ struct cpu_map *cpu_map__dummy_new(void)
if (cpus != NULL) {
cpus->nr = 1;
cpus->map[0] = -1;
+ atomic_set(&cpus->refcnt, 1);
}
return cpus;
}
-void cpu_map__delete(struct cpu_map *map)
+struct cpu_map *cpu_map__empty_new(int nr)
{
- free(map);
+ 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;
}
-int cpu_map__get_socket(struct cpu_map *map, int idx)
+static void cpu_map__delete(struct cpu_map *map)
{
- FILE *fp;
- const char *mnt;
- char path[PATH_MAX];
- int cpu, ret;
+ if (map) {
+ WARN_ONCE(atomic_read(&map->refcnt) != 0,
+ "cpu_map refcnt unbalanced\n");
+ free(map);
+ }
+}
- if (idx > map->nr)
- return -1;
+struct cpu_map *cpu_map__get(struct cpu_map *map)
+{
+ if (map)
+ atomic_inc(&map->refcnt);
+ return map;
+}
- cpu = map->map[idx];
+void cpu_map__put(struct cpu_map *map)
+{
+ if (map && atomic_dec_and_test(&map->refcnt))
+ cpu_map__delete(map);
+}
- mnt = sysfs__mountpoint();
- if (!mnt)
- return -1;
+static int cpu__get_topology_int(int cpu, const char *name, int *value)
+{
+ char path[PATH_MAX];
snprintf(path, PATH_MAX,
- "%s/devices/system/cpu/cpu%d/topology/physical_package_id",
- mnt, cpu);
+ "devices/system/cpu/cpu%d/topology/%s", cpu, name);
- fp = fopen(path, "r");
- if (!fp)
+ return sysfs__read_int(path, value);
+}
+
+int cpu_map__get_socket_id(int cpu)
+{
+ int value, ret = cpu__get_topology_int(cpu, "physical_package_id", &value);
+ return ret ?: value;
+}
+
+int cpu_map__get_socket(struct cpu_map *map, int idx, void *data __maybe_unused)
+{
+ int cpu;
+
+ 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)
@@ -237,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;
@@ -250,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;
@@ -263,39 +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(&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;
@@ -310,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 */