Skip to content

Commit e2091ce

Browse files
olsajiriacmel
authored andcommitted
perf tools: Add MEM_TOPOLOGY feature to perf data file
Adding MEM_TOPOLOGY feature to perf data file, that will carry physical memory map and its node assignments. The format of data in MEM_TOPOLOGY is as follows: 0 - version | for future changes 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes 16 - count | number of nodes For each node we store map of physical indexes for each node: 32 - node id | node index 40 - size | size of bitmap 48 - bitmap | bitmap of memory indexes that belongs to node | /sys/devices/system/node/node<NODE>/memory<INDEX> The MEM_TOPOLOGY could be displayed with following report command: $ perf report --header-only -I ... # memory nodes (nr 1, block size 0x8000000): # 0 [7G]: 0-23,32-69 Signed-off-by: Jiri Olsa <jolsa@kernel.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/20180307155020.32613-8-jolsa@kernel.org [ Rename 'index' to 'idx', as this breaks the build in rhel5, 6 and other systems where this is used by glibc headers ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 5cedb41 commit e2091ce

4 files changed

Lines changed: 316 additions & 1 deletion

File tree

‎tools/include/linux/bitmap.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static inline int test_and_set_bit(int nr, unsigned long *addr)
9898

9999
/**
100100
* bitmap_alloc - Allocate bitmap
101-
* @nr: Bit to set
101+
* @nbits: Number of bits
102102
*/
103103
static inline unsigned long *bitmap_alloc(int nbits)
104104
{

‎tools/perf/util/env.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ struct numa_node {
2727
struct cpu_map *map;
2828
};
2929

30+
struct memory_node {
31+
u64 node;
32+
u64 size;
33+
unsigned long *set;
34+
};
35+
3036
struct perf_env {
3137
char *hostname;
3238
char *os_release;
@@ -43,6 +49,7 @@ struct perf_env {
4349
int nr_sibling_cores;
4450
int nr_sibling_threads;
4551
int nr_numa_nodes;
52+
int nr_memory_nodes;
4653
int nr_pmu_mappings;
4754
int nr_groups;
4855
char *cmdline;
@@ -54,6 +61,8 @@ struct perf_env {
5461
struct cpu_cache_level *caches;
5562
int caches_cnt;
5663
struct numa_node *numa_nodes;
64+
struct memory_node *memory_nodes;
65+
unsigned long long memory_bsize;
5766
};
5867

5968
extern struct perf_env perf_env;

‎tools/perf/util/header.c‎

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <sys/stat.h>
1818
#include <sys/utsname.h>
1919
#include <linux/time64.h>
20+
#include <dirent.h>
2021

2122
#include "evlist.h"
2223
#include "evsel.h"
@@ -37,6 +38,7 @@
3738
#include "asm/bug.h"
3839
#include "tool.h"
3940
#include "time-utils.h"
41+
#include "units.h"
4042

4143
#include "sane_ctype.h"
4244

@@ -131,6 +133,25 @@ int do_write(struct feat_fd *ff, const void *buf, size_t size)
131133
return __do_write_buf(ff, buf, size);
132134
}
133135

136+
/* Return: 0 if succeded, -ERR if failed. */
137+
static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
138+
{
139+
u64 *p = (u64 *) set;
140+
int i, ret;
141+
142+
ret = do_write(ff, &size, sizeof(size));
143+
if (ret < 0)
144+
return ret;
145+
146+
for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
147+
ret = do_write(ff, p + i, sizeof(*p));
148+
if (ret < 0)
149+
return ret;
150+
}
151+
152+
return 0;
153+
}
154+
134155
/* Return: 0 if succeded, -ERR if failed. */
135156
int write_padded(struct feat_fd *ff, const void *bf,
136157
size_t count, size_t count_aligned)
@@ -243,6 +264,38 @@ static char *do_read_string(struct feat_fd *ff)
243264
return NULL;
244265
}
245266

267+
/* Return: 0 if succeded, -ERR if failed. */
268+
static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
269+
{
270+
unsigned long *set;
271+
u64 size, *p;
272+
int i, ret;
273+
274+
ret = do_read_u64(ff, &size);
275+
if (ret)
276+
return ret;
277+
278+
set = bitmap_alloc(size);
279+
if (!set)
280+
return -ENOMEM;
281+
282+
bitmap_zero(set, size);
283+
284+
p = (u64 *) set;
285+
286+
for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
287+
ret = do_read_u64(ff, p + i);
288+
if (ret < 0) {
289+
free(set);
290+
return ret;
291+
}
292+
}
293+
294+
*pset = set;
295+
*psize = size;
296+
return 0;
297+
}
298+
246299
static int write_tracing_data(struct feat_fd *ff,
247300
struct perf_evlist *evlist)
248301
{
@@ -1196,6 +1249,176 @@ static int write_sample_time(struct feat_fd *ff,
11961249
sizeof(evlist->last_sample_time));
11971250
}
11981251

1252+
1253+
static int memory_node__read(struct memory_node *n, unsigned long idx)
1254+
{
1255+
unsigned int phys, size = 0;
1256+
char path[PATH_MAX];
1257+
struct dirent *ent;
1258+
DIR *dir;
1259+
1260+
#define for_each_memory(mem, dir) \
1261+
while ((ent = readdir(dir))) \
1262+
if (strcmp(ent->d_name, ".") && \
1263+
strcmp(ent->d_name, "..") && \
1264+
sscanf(ent->d_name, "memory%u", &mem) == 1)
1265+
1266+
scnprintf(path, PATH_MAX,
1267+
"%s/devices/system/node/node%lu",
1268+
sysfs__mountpoint(), idx);
1269+
1270+
dir = opendir(path);
1271+
if (!dir) {
1272+
pr_warning("failed: cant' open memory sysfs data\n");
1273+
return -1;
1274+
}
1275+
1276+
for_each_memory(phys, dir) {
1277+
size = max(phys, size);
1278+
}
1279+
1280+
size++;
1281+
1282+
n->set = bitmap_alloc(size);
1283+
if (!n->set) {
1284+
closedir(dir);
1285+
return -ENOMEM;
1286+
}
1287+
1288+
bitmap_zero(n->set, size);
1289+
n->node = idx;
1290+
n->size = size;
1291+
1292+
rewinddir(dir);
1293+
1294+
for_each_memory(phys, dir) {
1295+
set_bit(phys, n->set);
1296+
}
1297+
1298+
closedir(dir);
1299+
return 0;
1300+
}
1301+
1302+
static int memory_node__sort(const void *a, const void *b)
1303+
{
1304+
const struct memory_node *na = a;
1305+
const struct memory_node *nb = b;
1306+
1307+
return na->node - nb->node;
1308+
}
1309+
1310+
static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1311+
{
1312+
char path[PATH_MAX];
1313+
struct dirent *ent;
1314+
DIR *dir;
1315+
u64 cnt = 0;
1316+
int ret = 0;
1317+
1318+
scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1319+
sysfs__mountpoint());
1320+
1321+
dir = opendir(path);
1322+
if (!dir) {
1323+
pr_warning("failed: can't open node sysfs data\n");
1324+
return -1;
1325+
}
1326+
1327+
while (!ret && (ent = readdir(dir))) {
1328+
unsigned int idx;
1329+
int r;
1330+
1331+
if (!strcmp(ent->d_name, ".") ||
1332+
!strcmp(ent->d_name, ".."))
1333+
continue;
1334+
1335+
r = sscanf(ent->d_name, "node%u", &idx);
1336+
if (r != 1)
1337+
continue;
1338+
1339+
if (WARN_ONCE(cnt >= size,
1340+
"failed to write MEM_TOPOLOGY, way too many nodes\n"))
1341+
return -1;
1342+
1343+
ret = memory_node__read(&nodes[cnt++], idx);
1344+
}
1345+
1346+
*cntp = cnt;
1347+
closedir(dir);
1348+
1349+
if (!ret)
1350+
qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1351+
1352+
return ret;
1353+
}
1354+
1355+
#define MAX_MEMORY_NODES 2000
1356+
1357+
/*
1358+
* The MEM_TOPOLOGY holds physical memory map for every
1359+
* node in system. The format of data is as follows:
1360+
*
1361+
* 0 - version | for future changes
1362+
* 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1363+
* 16 - count | number of nodes
1364+
*
1365+
* For each node we store map of physical indexes for
1366+
* each node:
1367+
*
1368+
* 32 - node id | node index
1369+
* 40 - size | size of bitmap
1370+
* 48 - bitmap | bitmap of memory indexes that belongs to node
1371+
*/
1372+
static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1373+
struct perf_evlist *evlist __maybe_unused)
1374+
{
1375+
static struct memory_node nodes[MAX_MEMORY_NODES];
1376+
u64 bsize, version = 1, i, nr;
1377+
int ret;
1378+
1379+
ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1380+
(unsigned long long *) &bsize);
1381+
if (ret)
1382+
return ret;
1383+
1384+
ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1385+
if (ret)
1386+
return ret;
1387+
1388+
ret = do_write(ff, &version, sizeof(version));
1389+
if (ret < 0)
1390+
goto out;
1391+
1392+
ret = do_write(ff, &bsize, sizeof(bsize));
1393+
if (ret < 0)
1394+
goto out;
1395+
1396+
ret = do_write(ff, &nr, sizeof(nr));
1397+
if (ret < 0)
1398+
goto out;
1399+
1400+
for (i = 0; i < nr; i++) {
1401+
struct memory_node *n = &nodes[i];
1402+
1403+
#define _W(v) \
1404+
ret = do_write(ff, &n->v, sizeof(n->v)); \
1405+
if (ret < 0) \
1406+
goto out;
1407+
1408+
_W(node)
1409+
_W(size)
1410+
1411+
#undef _W
1412+
1413+
ret = do_write_bitmap(ff, n->set, n->size);
1414+
if (ret < 0)
1415+
goto out;
1416+
}
1417+
1418+
out:
1419+
return ret;
1420+
}
1421+
11991422
static void print_hostname(struct feat_fd *ff, FILE *fp)
12001423
{
12011424
fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
@@ -1543,6 +1766,35 @@ static void print_sample_time(struct feat_fd *ff, FILE *fp)
15431766
fprintf(fp, "# sample duration : %10.3f ms\n", d);
15441767
}
15451768

1769+
static void memory_node__fprintf(struct memory_node *n,
1770+
unsigned long long bsize, FILE *fp)
1771+
{
1772+
char buf_map[100], buf_size[50];
1773+
unsigned long long size;
1774+
1775+
size = bsize * bitmap_weight(n->set, n->size);
1776+
unit_number__scnprintf(buf_size, 50, size);
1777+
1778+
bitmap_scnprintf(n->set, n->size, buf_map, 100);
1779+
fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1780+
}
1781+
1782+
static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1783+
{
1784+
struct memory_node *nodes;
1785+
int i, nr;
1786+
1787+
nodes = ff->ph->env.memory_nodes;
1788+
nr = ff->ph->env.nr_memory_nodes;
1789+
1790+
fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1791+
nr, ff->ph->env.memory_bsize);
1792+
1793+
for (i = 0; i < nr; i++) {
1794+
memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1795+
}
1796+
}
1797+
15461798
static int __event_process_build_id(struct build_id_event *bev,
15471799
char *filename,
15481800
struct perf_session *session)
@@ -2205,6 +2457,58 @@ static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
22052457
return 0;
22062458
}
22072459

2460+
static int process_mem_topology(struct feat_fd *ff,
2461+
void *data __maybe_unused)
2462+
{
2463+
struct memory_node *nodes;
2464+
u64 version, i, nr, bsize;
2465+
int ret = -1;
2466+
2467+
if (do_read_u64(ff, &version))
2468+
return -1;
2469+
2470+
if (version != 1)
2471+
return -1;
2472+
2473+
if (do_read_u64(ff, &bsize))
2474+
return -1;
2475+
2476+
if (do_read_u64(ff, &nr))
2477+
return -1;
2478+
2479+
nodes = zalloc(sizeof(*nodes) * nr);
2480+
if (!nodes)
2481+
return -1;
2482+
2483+
for (i = 0; i < nr; i++) {
2484+
struct memory_node n;
2485+
2486+
#define _R(v) \
2487+
if (do_read_u64(ff, &n.v)) \
2488+
goto out; \
2489+
2490+
_R(node)
2491+
_R(size)
2492+
2493+
#undef _R
2494+
2495+
if (do_read_bitmap(ff, &n.set, &n.size))
2496+
goto out;
2497+
2498+
nodes[i] = n;
2499+
}
2500+
2501+
ff->ph->env.memory_bsize = bsize;
2502+
ff->ph->env.memory_nodes = nodes;
2503+
ff->ph->env.nr_memory_nodes = nr;
2504+
ret = 0;
2505+
2506+
out:
2507+
if (ret)
2508+
free(nodes);
2509+
return ret;
2510+
}
2511+
22082512
struct feature_ops {
22092513
int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
22102514
void (*print)(struct feat_fd *ff, FILE *fp);
@@ -2263,6 +2567,7 @@ static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
22632567
FEAT_OPN(STAT, stat, false),
22642568
FEAT_OPN(CACHE, cache, true),
22652569
FEAT_OPR(SAMPLE_TIME, sample_time, false),
2570+
FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
22662571
};
22672572

22682573
struct header_print_data {

0 commit comments

Comments
 (0)