Skip to content

Commit e7a233f

Browse files
committed
Fetch per-numa (effectively per-kind) memory usage from the OS.
A simplified version, without assigning kinds, etc.
1 parent 17a6cfa commit e7a233f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ libmemkind_la_SOURCES = src/hbwmalloc.c \
4040
src/ranking_queue.c \
4141
src/slab_allocator.c \
4242
src/ranking_controller.c \
43+
src/numus.c \
4344
# end
4445

4546

src/numus.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#include <string.h>
4+
#include <stdint.h>
5+
#include <stdlib.h>
6+
7+
#define MAX_NODES 1024 /* kernel max */
8+
static struct {
9+
size_t used;
10+
} numamem[MAX_NODES];
11+
12+
static bool gather_numamem()
13+
{
14+
FILE *f = fopen("/proc/self/numa_maps", "r");
15+
if (!f)
16+
return false;
17+
18+
char line[1024];
19+
while (fgets(line, sizeof(line), f)) {
20+
char *saveptr = 0;
21+
char *err;
22+
// we get page size of a mapping only at the end
23+
int maxnode = 0;
24+
short num[MAX_NODES];
25+
size_t count[MAX_NODES];
26+
uint64_t pagesize = 0;
27+
28+
char *tok = strtok_r(line, " ", &saveptr);
29+
if (!tok)
30+
return fprintf(stderr, "Empty line in proc/numa_maps\n"), false;
31+
strtoul(tok, &err, 16);
32+
if (*err)
33+
return fprintf(stderr, "Invalid addr: %s\n", tok), false;
34+
//printf("Addr: %lx\n", val);
35+
36+
while ((tok = strtok_r(0, " ", &saveptr))) {
37+
char *valp = strchr(tok, '=');
38+
if (!valp) {
39+
/* flags */
40+
if (!strcmp(tok, "stack"))
41+
goto skip_mapping;
42+
continue;
43+
}
44+
*valp++ = 0;
45+
46+
if (!strcmp(tok, "kernelpagesize_kB"))
47+
pagesize = atoi(valp) * 1024ULL;
48+
else if (tok[0] == 'N' && tok[1] >= '0' && tok[1] <= '9') {
49+
int node = atoi(tok + 1);
50+
if (node >= MAX_NODES)
51+
continue;
52+
if (maxnode + 1 >= MAX_NODES)
53+
continue;
54+
num[maxnode] = node;
55+
count[maxnode] = atol(valp);
56+
maxnode++;
57+
}
58+
}
59+
60+
if (maxnode && !pagesize)
61+
return fprintf(stderr, "Pages used but no page size\n"), false;
62+
for (int i = 0; i < maxnode; i++)
63+
numamem[num[i]].used += count[i] * pagesize;
64+
65+
skip_mapping:
66+
}
67+
fclose(f);
68+
return true;
69+
}

0 commit comments

Comments
 (0)