This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shm.c
325 lines (283 loc) · 7.71 KB
/
shm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* Copyright (C) 2003,2004 Andi Kleen, SuSE Labs.
Manage shared memory policy for numactl.
The actual policy is set in numactl itself, this just sets up and maps
the shared memory segments and dumps them.
numactl is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; version
2.
numactl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should find a copy of v2 of the GNU General Public License somewhere
on your Linux system; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include "numa.h"
#include "numaif.h"
#include "numaint.h"
#include "util.h"
#include "shm.h"
int shmfd = -1;
long shmid = 0;
char *shmptr;
unsigned long long shmlen;
mode_t shmmode = 0600;
unsigned long long shmoffset;
int shmflags;
static int shm_pagesize;
long huge_page_size(void)
{
size_t len = 0;
char *line = NULL;
FILE *f = fopen("/proc/meminfo", "r");
if (f != NULL) {
while (getdelim(&line, &len, '\n', f) > 0) {
int ps;
if (sscanf(line, "Hugepagesize: %d kB", &ps) == 1)
return ps * 1024;
}
free(line);
fclose(f);
}
return getpagesize();
}
static void check_region(char *opt)
{
if (((unsigned long)shmptr % shm_pagesize) || (shmlen % shm_pagesize)) {
fprintf(stderr, "numactl: policy region not page aligned\n");
exit(1);
}
if (!shmlen) {
fprintf(stderr,
"numactl: policy region length not specified before %s\n",
opt);
exit(1);
}
}
static key_t sysvkey(char *name)
{
int fd;
key_t key = ftok(name, shmid);
if (key >= 0)
return key;
fprintf(stderr, "numactl: Creating shm key file %s mode %04o\n",
name, shmmode);
fd = creat(name, shmmode);
if (fd < 0)
nerror("cannot create key for shm %s\n", name);
key = ftok(name, shmid);
if (key < 0)
nerror("cannot get key for newly created shm key file %s",
name);
return key;
}
/* Attach a sysv style shared memory segment. */
void attach_sysvshm(char *name, char *opt)
{
struct shmid_ds s;
key_t key = sysvkey(name);
shmfd = shmget(key, shmlen, shmflags);
if (shmfd < 0 && errno == ENOENT) {
if (shmlen == 0)
complain(
"need a --length to create a sysv shared memory segment");
fprintf(stderr,
"numactl: Creating shared memory segment %s id %ld mode %04o length %.fMB\n",
name, shmid, shmmode, ((double)shmlen) / (1024*1024) );
shmfd = shmget(key, shmlen, IPC_CREAT|shmmode|shmflags);
if (shmfd < 0)
nerror("cannot create shared memory segment");
}
if (shmlen == 0) {
if (shmctl(shmfd, IPC_STAT, &s) < 0)
err("shmctl IPC_STAT");
shmlen = s.shm_segsz;
}
shmptr = shmat(shmfd, NULL, SHM_RDONLY);
if (shmptr == (void*)-1)
err("shmat");
shmptr += shmoffset;
shm_pagesize = (shmflags & SHM_HUGETLB) ? huge_page_size() : getpagesize();
check_region(opt);
}
/* Attach a shared memory file. */
void attach_shared(char *name, char *opt)
{
struct stat64 st;
shmfd = open(name, O_RDONLY);
if (shmfd < 0) {
errno = 0;
if (shmlen == 0)
complain("need a --length to create a shared file");
shmfd = open(name, O_RDWR|O_CREAT, shmmode);
if (shmfd < 0)
nerror("cannot create file %s", name);
}
if (fstat64(shmfd, &st) < 0)
err("shm stat");
if (shmlen > st.st_size) {
if (ftruncate64(shmfd, shmlen) < 0) {
/* XXX: we could do it by hand, but it would it
would be impossible to apply policy then.
need to fix that in the kernel. */
perror("ftruncate");
}
}
shm_pagesize = st.st_blksize;
check_region(opt);
/* RED-PEN For shmlen > address space may need to map in pieces.
Left for some poor 32bit soul. */
shmptr = mmap64(NULL, shmlen, PROT_READ, MAP_SHARED, shmfd, shmoffset);
if (shmptr == (char*)-1)
err("shm mmap");
}
static void
dumppol(unsigned long long start, unsigned long long end, int pol, struct bitmask *mask)
{
if (pol == MPOL_DEFAULT)
return;
printf("%016llx-%016llx: %s ",
shmoffset+start,
shmoffset+end,
policy_name(pol));
printmask("", mask);
}
/* Dump policies in a shared memory segment. */
void dump_shm(void)
{
struct bitmask *nodes, *prevnodes;
int prevpol = -1, pol;
unsigned long long c, start;
start = 0;
if (shmlen == 0) {
printf("nothing to dump\n");
return;
}
nodes = numa_allocate_nodemask();
prevnodes = numa_allocate_nodemask();
for (c = 0; c < shmlen; c += shm_pagesize) {
if (get_mempolicy(&pol, nodes->maskp, nodes->size, c+shmptr,
MPOL_F_ADDR) < 0)
err("get_mempolicy on shm");
if (pol == prevpol)
continue;
if (prevpol != -1)
dumppol(start, c, prevpol, prevnodes);
prevnodes = nodes;
prevpol = pol;
start = c;
}
dumppol(start, c, prevpol, prevnodes);
}
static void dumpnode(unsigned long long start, unsigned long long end, int node)
{
printf("%016llx-%016llx: %d\n", shmoffset+start, shmoffset+end, node);
}
/* Dump nodes in a shared memory segment. */
void dump_shm_nodes(void)
{
int prevnode = -1, node;
unsigned long long c, start;
start = 0;
if (shmlen == 0) {
printf("nothing to dump\n");
return;
}
for (c = 0; c < shmlen; c += shm_pagesize) {
if (get_mempolicy(&node, NULL, 0, c+shmptr,
MPOL_F_ADDR|MPOL_F_NODE) < 0)
err("get_mempolicy on shm");
if (node == prevnode)
continue;
if (prevnode != -1)
dumpnode(start, c, prevnode);
prevnode = node;
start = c;
}
dumpnode(start, c, prevnode);
}
static void vwarn(char *ptr, char *fmt, ...)
{
va_list ap;
unsigned long off = (unsigned long)ptr - (unsigned long)shmptr;
va_start(ap,fmt);
printf("numactl verify %lx(%lx): ", (unsigned long)ptr, off);
vprintf(fmt, ap);
va_end(ap);
exitcode = 1;
}
static unsigned interleave_next(unsigned cur, struct bitmask *mask)
{
int numa_num_nodes = numa_num_possible_nodes();
++cur;
while (!numa_bitmask_isbitset(mask, cur)) {
cur = (cur+1) % numa_num_nodes;
}
return cur;
}
/* Verify policy in a shared memory segment */
void verify_shm(int policy, struct bitmask *nodes)
{
char *p;
int ilnode, node;
int pol2;
struct bitmask *nodes2;
nodes2 = numa_allocate_nodemask();
if (policy == MPOL_INTERLEAVE) {
if (get_mempolicy(&ilnode, NULL, 0, shmptr,
MPOL_F_ADDR|MPOL_F_NODE)
< 0)
err("get_mempolicy");
}
for (p = shmptr; p - (char *)shmptr < shmlen; p += shm_pagesize) {
if (get_mempolicy(&pol2, nodes2->maskp, nodes2->size, p,
MPOL_F_ADDR) < 0)
err("get_mempolicy");
if (pol2 != policy) {
vwarn(p, "wrong policy %s, expected %s\n",
policy_name(pol2), policy_name(policy));
return;
}
if (memcmp(nodes2, nodes, numa_bitmask_nbytes(nodes))) {
vwarn(p, "mismatched node mask\n");
printmask("expected", nodes);
printmask("real", nodes2);
}
if (get_mempolicy(&node, NULL, 0, p, MPOL_F_ADDR|MPOL_F_NODE) < 0)
err("get_mempolicy");
switch (policy) {
case MPOL_INTERLEAVE:
if (node < 0 || !numa_bitmask_isbitset(nodes2, node))
vwarn(p, "interleave node out of range %d\n", node);
if (node != ilnode) {
vwarn(p, "expected interleave node %d, got %d\n",
ilnode,node);
return;
}
ilnode = interleave_next(ilnode, nodes2);
break;
case MPOL_PREFERRED:
case MPOL_BIND:
if (!numa_bitmask_isbitset(nodes2, node)) {
vwarn(p, "unexpected node %d\n", node);
printmask("expected", nodes2);
}
break;
case MPOL_DEFAULT:
break;
}
}
}