diff options
Diffstat (limited to 'tools/perf/util/cpumap.c')
-rw-r--r-- | tools/perf/util/cpumap.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tools/perf/util/cpumap.c b/tools/perf/util/cpumap.c index 2b32ffa9ebdb..f817046e22b1 100644 --- a/tools/perf/util/cpumap.c +++ b/tools/perf/util/cpumap.c | |||
@@ -1,4 +1,5 @@ | |||
1 | #include "util.h" | 1 | #include "util.h" |
2 | #include "sysfs.h" | ||
2 | #include "../perf.h" | 3 | #include "../perf.h" |
3 | #include "cpumap.h" | 4 | #include "cpumap.h" |
4 | #include <assert.h> | 5 | #include <assert.h> |
@@ -201,3 +202,56 @@ void cpu_map__delete(struct cpu_map *map) | |||
201 | { | 202 | { |
202 | free(map); | 203 | free(map); |
203 | } | 204 | } |
205 | |||
206 | int cpu_map__get_socket(struct cpu_map *map, int idx) | ||
207 | { | ||
208 | FILE *fp; | ||
209 | const char *mnt; | ||
210 | char path[PATH_MAX]; | ||
211 | int cpu, ret; | ||
212 | |||
213 | if (idx > map->nr) | ||
214 | return -1; | ||
215 | |||
216 | cpu = map->map[idx]; | ||
217 | |||
218 | mnt = sysfs_find_mountpoint(); | ||
219 | if (!mnt) | ||
220 | return -1; | ||
221 | |||
222 | sprintf(path, | ||
223 | "%s/devices/system/cpu/cpu%d/topology/physical_package_id", | ||
224 | mnt, cpu); | ||
225 | |||
226 | fp = fopen(path, "r"); | ||
227 | if (!fp) | ||
228 | return -1; | ||
229 | ret = fscanf(fp, "%d", &cpu); | ||
230 | fclose(fp); | ||
231 | return ret == 1 ? cpu : -1; | ||
232 | } | ||
233 | |||
234 | int cpu_map__build_socket_map(struct cpu_map *cpus, struct cpu_map **sockp) | ||
235 | { | ||
236 | struct cpu_map *sock; | ||
237 | int nr = cpus->nr; | ||
238 | int cpu, s1, s2; | ||
239 | |||
240 | sock = calloc(1, sizeof(*sock) + nr * sizeof(int)); | ||
241 | if (!sock) | ||
242 | return -1; | ||
243 | |||
244 | for (cpu = 0; cpu < nr; cpu++) { | ||
245 | s1 = cpu_map__get_socket(cpus, cpu); | ||
246 | for (s2 = 0; s2 < sock->nr; s2++) { | ||
247 | if (s1 == sock->map[s2]) | ||
248 | break; | ||
249 | } | ||
250 | if (s2 == sock->nr) { | ||
251 | sock->map[sock->nr] = s1; | ||
252 | sock->nr++; | ||
253 | } | ||
254 | } | ||
255 | *sockp = sock; | ||
256 | return 0; | ||
257 | } | ||