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
/
libnuma.c
2030 lines (1794 loc) · 46.1 KB
/
libnuma.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Simple NUMA library.
Copyright (C) 2003,2004,2005,2008 Andi Kleen,SuSE Labs and
Cliff Wickman,SGI.
libnuma is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; version
2.1.
libnuma 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
Lesser General Public License for more details.
You should find a copy of v2.1 of the GNU Lesser 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
All calls are undefined when numa_available returns an error. */
#define _GNU_SOURCE 1
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sched.h>
#include <dirent.h>
#include <errno.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/mman.h>
#include <limits.h>
#include "config.h"
#include "numa.h"
#include "numaif.h"
#include "numaint.h"
#include "util.h"
#include "affinity.h"
#define WEAK __attribute__((weak))
#define CPU_BUFFER_SIZE 4096 /* This limits you to 32768 CPUs */
/* these are the old (version 1) masks */
nodemask_t numa_no_nodes;
nodemask_t numa_all_nodes;
/* these are now the default bitmask (pointers to) (version 2) */
struct bitmask *numa_no_nodes_ptr = NULL;
struct bitmask *numa_all_nodes_ptr = NULL;
struct bitmask *numa_possible_nodes_ptr = NULL;
struct bitmask *numa_all_cpus_ptr = NULL;
struct bitmask *numa_possible_cpus_ptr = NULL;
/* I would prefer to use symbol versioning to create v1 and v2 versions
of numa_no_nodes and numa_all_nodes, but the loader does not correctly
handle versioning of BSS versus small data items */
struct bitmask *numa_nodes_ptr = NULL;
static struct bitmask *numa_memnode_ptr = NULL;
static unsigned long *node_cpu_mask_v1[NUMA_NUM_NODES];
static struct bitmask **node_cpu_mask_v2;
WEAK void numa_error(char *where);
#ifndef TLS
#warning "not threadsafe"
#define __thread
#endif
static __thread int bind_policy = MPOL_BIND;
static __thread unsigned int mbind_flags = 0;
static int sizes_set=0;
static int maxconfigurednode = -1;
static int maxconfiguredcpu = -1;
static int numprocnode = -1;
static int numproccpu = -1;
static int nodemask_sz = 0;
static int cpumask_sz = 0;
int numa_exit_on_error = 0;
int numa_exit_on_warn = 0;
static void set_sizes(void);
/*
* There are two special functions, _init(void) and _fini(void), which
* are called automatically by the dynamic loader whenever a library is loaded.
*
* The v1 library depends upon nodemask_t's of all nodes and no nodes.
*/
void __attribute__((constructor))
numa_init(void)
{
int max,i;
if (sizes_set)
return;
set_sizes();
/* numa_all_nodes should represent existing nodes on this system */
max = numa_num_configured_nodes();
for (i = 0; i < max; i++)
nodemask_set_compat((nodemask_t *)&numa_all_nodes, i);
memset(&numa_no_nodes, 0, sizeof(numa_no_nodes));
}
#define FREE_AND_ZERO(x) if (x) { \
numa_bitmask_free(x); \
x = NULL; \
}
void __attribute__((destructor))
numa_fini(void)
{
FREE_AND_ZERO(numa_all_cpus_ptr);
FREE_AND_ZERO(numa_possible_cpus_ptr);
FREE_AND_ZERO(numa_all_nodes_ptr);
FREE_AND_ZERO(numa_possible_nodes_ptr);
FREE_AND_ZERO(numa_no_nodes_ptr);
FREE_AND_ZERO(numa_memnode_ptr);
FREE_AND_ZERO(numa_nodes_ptr);
}
/*
* The following bitmask declarations, bitmask_*() routines, and associated
* _setbit() and _getbit() routines are:
* Copyright (c) 2004_2007 Silicon Graphics, Inc. (SGI) All rights reserved.
* SGI publishes it under the terms of the GNU General Public License, v2,
* as published by the Free Software Foundation.
*/
static unsigned int
_getbit(const struct bitmask *bmp, unsigned int n)
{
if (n < bmp->size)
return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1;
else
return 0;
}
static void
_setbit(struct bitmask *bmp, unsigned int n, unsigned int v)
{
if (n < bmp->size) {
if (v)
bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong);
else
bmp->maskp[n/bitsperlong] &= ~(1UL << (n % bitsperlong));
}
}
int
numa_bitmask_isbitset(const struct bitmask *bmp, unsigned int i)
{
return _getbit(bmp, i);
}
struct bitmask *
numa_bitmask_setall(struct bitmask *bmp)
{
unsigned int i;
for (i = 0; i < bmp->size; i++)
_setbit(bmp, i, 1);
return bmp;
}
struct bitmask *
numa_bitmask_clearall(struct bitmask *bmp)
{
unsigned int i;
for (i = 0; i < bmp->size; i++)
_setbit(bmp, i, 0);
return bmp;
}
struct bitmask *
numa_bitmask_setbit(struct bitmask *bmp, unsigned int i)
{
_setbit(bmp, i, 1);
return bmp;
}
struct bitmask *
numa_bitmask_clearbit(struct bitmask *bmp, unsigned int i)
{
_setbit(bmp, i, 0);
return bmp;
}
unsigned int
numa_bitmask_nbytes(struct bitmask *bmp)
{
return longsperbits(bmp->size) * sizeof(unsigned long);
}
/* where n is the number of bits in the map */
/* This function should not exit on failure, but right now we cannot really
recover from this. */
struct bitmask *
numa_bitmask_alloc(unsigned int n)
{
struct bitmask *bmp;
if (n < 1) {
numa_error("request to allocate mask for invalid number; abort\n");
exit(1);
}
bmp = malloc(sizeof(*bmp));
if (bmp == 0)
goto oom;
bmp->size = n;
bmp->maskp = calloc(longsperbits(n), sizeof(unsigned long));
if (bmp->maskp == 0) {
free(bmp);
goto oom;
}
return bmp;
oom:
numa_error("Out of memory allocating bitmask");
exit(1);
}
void
numa_bitmask_free(struct bitmask *bmp)
{
if (bmp == 0)
return;
free(bmp->maskp);
bmp->maskp = (unsigned long *)0xdeadcdef; /* double free tripwire */
free(bmp);
return;
}
/* True if two bitmasks are equal */
int
numa_bitmask_equal(const struct bitmask *bmp1, const struct bitmask *bmp2)
{
unsigned int i;
for (i = 0; i < bmp1->size || i < bmp2->size; i++)
if (_getbit(bmp1, i) != _getbit(bmp2, i))
return 0;
return 1;
}
/* Hamming Weight: number of set bits */
unsigned int numa_bitmask_weight(const struct bitmask *bmp)
{
unsigned int i;
unsigned int w = 0;
for (i = 0; i < bmp->size; i++)
if (_getbit(bmp, i))
w++;
return w;
}
/* *****end of bitmask_ routines ************ */
/* Next two can be overwritten by the application for different error handling */
WEAK void numa_error(char *where)
{
int olde = errno;
perror(where);
if (numa_exit_on_error)
exit(1);
errno = olde;
}
WEAK void numa_warn(int num, char *fmt, ...)
{
static unsigned warned;
va_list ap;
int olde = errno;
/* Give each warning only once */
if ((1<<num) & warned)
return;
warned |= (1<<num);
va_start(ap,fmt);
fprintf(stderr, "libnuma: Warning: ");
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
va_end(ap);
errno = olde;
}
static void setpol(int policy, struct bitmask *bmp)
{
if (set_mempolicy(policy, bmp->maskp, bmp->size + 1) < 0)
numa_error("set_mempolicy");
}
static void getpol(int *oldpolicy, struct bitmask *bmp)
{
if (get_mempolicy(oldpolicy, bmp->maskp, bmp->size + 1, 0, 0) < 0)
numa_error("get_mempolicy");
}
static void dombind(void *mem, size_t size, int pol, struct bitmask *bmp)
{
if (mbind(mem, size, pol, bmp ? bmp->maskp : NULL, bmp ? bmp->size + 1 : 0,
mbind_flags) < 0)
numa_error("mbind");
}
/* (undocumented) */
/* gives the wrong answer for hugetlbfs mappings. */
int numa_pagesize(void)
{
static int pagesize;
if (pagesize > 0)
return pagesize;
pagesize = getpagesize();
return pagesize;
}
make_internal_alias(numa_pagesize);
/*
* Find nodes (numa_nodes_ptr), nodes with memory (numa_memnode_ptr)
* and the highest numbered existing node (maxconfigurednode).
*/
static void
set_configured_nodes(void)
{
DIR *d;
struct dirent *de;
long long freep;
numa_memnode_ptr = numa_allocate_nodemask();
numa_nodes_ptr = numa_allocate_nodemask();
d = opendir("/sys/devices/system/node");
if (!d) {
maxconfigurednode = 0;
} else {
while ((de = readdir(d)) != NULL) {
int nd;
if (strncmp(de->d_name, "node", 4))
continue;
nd = strtoul(de->d_name+4, NULL, 0);
numa_bitmask_setbit(numa_nodes_ptr, nd);
if (numa_node_size64(nd, &freep) > 0)
numa_bitmask_setbit(numa_memnode_ptr, nd);
if (maxconfigurednode < nd)
maxconfigurednode = nd;
}
closedir(d);
}
}
/*
* Convert the string length of an ascii hex mask to the number
* of bits represented by that mask.
*/
static int s2nbits(const char *s)
{
return strlen(s) * 32 / 9;
}
/* Is string 'pre' a prefix of string 's'? */
static int strprefix(const char *s, const char *pre)
{
return strncmp(s, pre, strlen(pre)) == 0;
}
static const char *mask_size_file = "/proc/self/status";
static const char *nodemask_prefix = "Mems_allowed:\t";
/*
* (do this the way Paul Jackson's libcpuset does it)
* The nodemask values in /proc/self/status are in an
* ascii format that uses 9 characters for each 32 bits of mask.
* (this could also be used to find the cpumask size)
*/
static void
set_nodemask_size(void)
{
FILE *fp;
char *buf = NULL;
size_t bufsize = 0;
if ((fp = fopen(mask_size_file, "r")) == NULL)
goto done;
while (getline(&buf, &bufsize, fp) > 0) {
if (strprefix(buf, nodemask_prefix)) {
nodemask_sz = s2nbits(buf + strlen(nodemask_prefix));
break;
}
}
free(buf);
fclose(fp);
done:
if (nodemask_sz == 0) {/* fall back on error */
int pol;
unsigned long *mask = NULL;
nodemask_sz = 16;
do {
nodemask_sz <<= 1;
mask = realloc(mask, nodemask_sz / 8);
if (!mask)
return;
} while (get_mempolicy(&pol, mask, nodemask_sz + 1, 0, 0) < 0 && errno == EINVAL &&
nodemask_sz < 4096*8);
free(mask);
}
}
/*
* Read a mask consisting of a sequence of hexadecimal longs separated by
* commas. Order them correctly and return the number of bits set.
*/
static int
read_mask(char *s, struct bitmask *bmp)
{
char *end = s;
int tmplen = (bmp->size + bitsperint - 1) / bitsperint;
unsigned int tmp[tmplen];
unsigned int *start = tmp;
unsigned int i, n = 0, m = 0;
if (!s)
return 0; /* shouldn't happen */
i = strtoul(s, &end, 16);
/* Skip leading zeros */
while (!i && *end++ == ',') {
i = strtoul(end, &end, 16);
}
if (!i)
/* End of string. No mask */
return -1;
start[n++] = i;
/* Read sequence of ints */
while (*end++ == ',') {
i = strtoul(end, &end, 16);
start[n++] = i;
/* buffer overflow */
if (n > tmplen)
return -1;
}
/*
* Invert sequence of ints if necessary since the first int
* is the highest and we put it first because we read it first.
*/
while (n) {
int w;
unsigned long x = 0;
/* read into long values in an endian-safe way */
for (w = 0; n && w < bitsperlong; w += bitsperint)
x |= ((unsigned long)start[n-- - 1] << w);
bmp->maskp[m++] = x;
}
/*
* Return the number of bits set
*/
return numa_bitmask_weight(bmp);
}
/*
* Read a processes constraints in terms of nodes and cpus from
* /proc/self/status.
*/
static void
set_task_constraints(void)
{
int hicpu = maxconfiguredcpu;
int i;
char *buffer = NULL;
size_t buflen = 0;
FILE *f;
numa_all_cpus_ptr = numa_allocate_cpumask();
numa_possible_cpus_ptr = numa_allocate_cpumask();
numa_all_nodes_ptr = numa_allocate_nodemask();
numa_possible_nodes_ptr = numa_allocate_cpumask();
numa_no_nodes_ptr = numa_allocate_nodemask();
f = fopen(mask_size_file, "r");
if (!f) {
//numa_warn(W_cpumap, "Cannot parse %s", mask_size_file);
return;
}
while (getline(&buffer, &buflen, f) > 0) {
/* mask starts after [last] tab */
char *mask = strrchr(buffer,'\t') + 1;
if (strncmp(buffer,"Cpus_allowed:",13) == 0)
numproccpu = read_mask(mask, numa_all_cpus_ptr);
if (strncmp(buffer,"Mems_allowed:",13) == 0) {
numprocnode = read_mask(mask, numa_all_nodes_ptr);
}
}
fclose(f);
free(buffer);
for (i = 0; i <= hicpu; i++)
numa_bitmask_setbit(numa_possible_cpus_ptr, i);
for (i = 0; i <= maxconfigurednode; i++)
numa_bitmask_setbit(numa_possible_nodes_ptr, i);
/*
* Cpus_allowed in the kernel can be defined to all f's
* i.e. it may be a superset of the actual available processors.
* As such let's reduce numproccpu to the number of actual
* available cpus.
*/
if (numproccpu <= 0) {
for (i = 0; i <= hicpu; i++)
numa_bitmask_setbit(numa_all_cpus_ptr, i);
numproccpu = hicpu+1;
}
if (numproccpu > hicpu+1) {
numproccpu = hicpu+1;
for (i=hicpu+1; i<numa_all_cpus_ptr->size; i++) {
numa_bitmask_clearbit(numa_all_cpus_ptr, i);
}
}
if (numprocnode <= 0) {
for (i = 0; i <= maxconfigurednode; i++)
numa_bitmask_setbit(numa_all_nodes_ptr, i);
numprocnode = maxconfigurednode + 1;
}
return;
}
/*
* Find the highest cpu number possible (in other words the size
* of a kernel cpumask_t (in bits) - 1)
*/
static void
set_numa_max_cpu(void)
{
int len = 4096;
int n;
int olde = errno;
struct bitmask *buffer;
do {
buffer = numa_bitmask_alloc(len);
n = numa_sched_getaffinity_v2_int(0, buffer);
/* on success, returns size of kernel cpumask_t, in bytes */
if (n < 0 && errno == EINVAL) {
if (len >= 1024*1024)
break;
len *= 2;
numa_bitmask_free(buffer);
continue;
}
} while (n < 0);
numa_bitmask_free(buffer);
errno = olde;
cpumask_sz = n*8;
}
/*
* get the total (configured) number of cpus - both online and offline
*/
static void
set_configured_cpus(void)
{
maxconfiguredcpu = sysconf(_SC_NPROCESSORS_CONF) - 1;
if (maxconfiguredcpu == -1)
numa_error("sysconf(NPROCESSORS_CONF) failed.\n");
}
/*
* Initialize all the sizes.
*/
static void
set_sizes(void)
{
sizes_set++;
set_nodemask_size(); /* size of kernel nodemask_t */
set_configured_nodes(); /* configured nodes listed in /sys */
set_numa_max_cpu(); /* size of kernel cpumask_t */
set_configured_cpus(); /* cpus listed in /sys/devices/system/cpu */
set_task_constraints(); /* cpus and nodes for current task */
}
int
numa_num_configured_nodes(void)
{
/*
* NOTE: this function's behavior matches the documentation (ie: it
* returns a count of nodes with memory) despite the poor function
* naming. We also cannot use the similarly poorly named
* numa_all_nodes_ptr as it only tracks nodes with memory from which
* the calling process can allocate. Think sparse nodes, memory-less
* nodes, cpusets...
*/
int memnodecount=0, i;
for (i=0; i <= maxconfigurednode; i++) {
if (numa_bitmask_isbitset(numa_memnode_ptr, i))
memnodecount++;
}
return memnodecount;
}
int
numa_num_configured_cpus(void)
{
return maxconfiguredcpu+1;
}
int
numa_num_possible_nodes(void)
{
return nodemask_sz;
}
int
numa_num_possible_cpus(void)
{
return cpumask_sz;
}
int
numa_num_task_nodes(void)
{
return numprocnode;
}
/*
* for backward compatibility
*/
int
numa_num_thread_nodes(void)
{
return numa_num_task_nodes();
}
int
numa_num_task_cpus(void)
{
return numproccpu;
}
/*
* for backward compatibility
*/
int
numa_num_thread_cpus(void)
{
return numa_num_task_cpus();
}
/*
* Return the number of the highest node in this running system,
*/
int
numa_max_node(void)
{
return maxconfigurednode;
}
make_internal_alias(numa_max_node);
/*
* Return the number of the highest possible node in a system,
* which for v1 is the size of a numa.h nodemask_t(in bits)-1.
* but for v2 is the size of a kernel nodemask_t(in bits)-1.
*/
int
numa_max_possible_node_v1(void)
{
return ((sizeof(nodemask_t)*8)-1);
}
__asm__(".symver numa_max_possible_node_v1,numa_max_possible_node@libnuma_1.1");
int
numa_max_possible_node_v2(void)
{
return numa_num_possible_nodes()-1;
}
__asm__(".symver numa_max_possible_node_v2,numa_max_possible_node@@libnuma_1.2");
make_internal_alias(numa_max_possible_node_v1);
make_internal_alias(numa_max_possible_node_v2);
/*
* Allocate a bitmask for cpus, of a size large enough to
* match the kernel's cpumask_t.
*/
struct bitmask *
numa_allocate_cpumask()
{
int ncpus = numa_num_possible_cpus();
return numa_bitmask_alloc(ncpus);
}
/*
* Allocate a bitmask the size of a libnuma nodemask_t
*/
static struct bitmask *
allocate_nodemask_v1(void)
{
int nnodes = numa_max_possible_node_v1_int()+1;
return numa_bitmask_alloc(nnodes);
}
/*
* Allocate a bitmask for nodes, of a size large enough to
* match the kernel's nodemask_t.
*/
struct bitmask *
numa_allocate_nodemask(void)
{
struct bitmask *bmp;
int nnodes = numa_max_possible_node_v2_int() + 1;
bmp = numa_bitmask_alloc(nnodes);
return bmp;
}
/* (cache the result?) */
long long numa_node_size64(int node, long long *freep)
{
size_t len = 0;
char *line = NULL;
long long size = -1;
FILE *f;
char fn[64];
int ok = 0;
int required = freep ? 2 : 1;
if (freep)
*freep = -1;
sprintf(fn,"/sys/devices/system/node/node%d/meminfo", node);
f = fopen(fn, "r");
if (!f)
return -1;
while (getdelim(&line, &len, '\n', f) > 0) {
char *end;
char *s = strcasestr(line, "kB");
if (!s)
continue;
--s;
while (s > line && isspace(*s))
--s;
while (s > line && isdigit(*s))
--s;
if (strstr(line, "MemTotal")) {
size = strtoull(s,&end,0) << 10;
if (end == s)
size = -1;
else
ok++;
}
if (freep && strstr(line, "MemFree")) {
*freep = strtoull(s,&end,0) << 10;
if (end == s)
*freep = -1;
else
ok++;
}
}
fclose(f);
free(line);
if (ok != required)
numa_warn(W_badmeminfo, "Cannot parse sysfs meminfo (%d)", ok);
return size;
}
make_internal_alias(numa_node_size64);
long numa_node_size(int node, long *freep)
{
long long f2;
long sz = numa_node_size64_int(node, &f2);
if (freep)
*freep = f2;
return sz;
}
int numa_available(void)
{
if (get_mempolicy(NULL, NULL, 0, 0, 0) < 0 && errno == ENOSYS)
return -1;
return 0;
}
void
numa_interleave_memory_v1(void *mem, size_t size, const nodemask_t *mask)
{
struct bitmask bitmask;
bitmask.size = sizeof(nodemask_t) * 8;
bitmask.maskp = (unsigned long *)mask;
dombind(mem, size, MPOL_INTERLEAVE, &bitmask);
}
__asm__(".symver numa_interleave_memory_v1,numa_interleave_memory@libnuma_1.1");
void
numa_interleave_memory_v2(void *mem, size_t size, struct bitmask *bmp)
{
dombind(mem, size, MPOL_INTERLEAVE, bmp);
}
__asm__(".symver numa_interleave_memory_v2,numa_interleave_memory@@libnuma_1.2");
void numa_tonode_memory(void *mem, size_t size, int node)
{
struct bitmask *nodes;
nodes = numa_allocate_nodemask();
numa_bitmask_setbit(nodes, node);
dombind(mem, size, bind_policy, nodes);
numa_bitmask_free(nodes);
}
void
numa_tonodemask_memory_v1(void *mem, size_t size, const nodemask_t *mask)
{
struct bitmask bitmask;
bitmask.maskp = (unsigned long *)mask;
bitmask.size = sizeof(nodemask_t);
dombind(mem, size, bind_policy, &bitmask);
}
__asm__(".symver numa_tonodemask_memory_v1,numa_tonodemask_memory@libnuma_1.1");
void
numa_tonodemask_memory_v2(void *mem, size_t size, struct bitmask *bmp)
{
dombind(mem, size, bind_policy, bmp);
}
__asm__(".symver numa_tonodemask_memory_v2,numa_tonodemask_memory@@libnuma_1.2");
void numa_setlocal_memory(void *mem, size_t size)
{
dombind(mem, size, MPOL_PREFERRED, NULL);
}
void numa_police_memory(void *mem, size_t size)
{
int pagesize = numa_pagesize_int();
unsigned long i;
for (i = 0; i < size; i += pagesize)
asm volatile("" :: "r" (((volatile unsigned char *)mem)[i]));
}
make_internal_alias(numa_police_memory);
void *numa_alloc(size_t size)
{
char *mem;
mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);
if (mem == (char *)-1)
return NULL;
numa_police_memory_int(mem, size);
return mem;
}
void *numa_realloc(void *old_addr, size_t old_size, size_t new_size)
{
char *mem;
mem = mremap(old_addr, old_size, new_size, MREMAP_MAYMOVE);
if (mem == (char *)-1)
return NULL;
/*
* The memory policy of the allocated pages is preserved by mremap(), so
* there is no need to (re)set it here. If the policy of the original
* allocation is not set, the new pages will be allocated according to the
* process' mempolicy. Trying to allocate explicitly the new pages on the
* same node as the original ones would require changing the policy of the
* newly allocated pages, which violates the numa_realloc() semantics.
*/
return mem;
}
void *numa_alloc_interleaved_subset_v1(size_t size, const nodemask_t *mask)
{
char *mem;
struct bitmask bitmask;
mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);
if (mem == (char *)-1)
return NULL;
bitmask.maskp = (unsigned long *)mask;
bitmask.size = sizeof(nodemask_t);
dombind(mem, size, MPOL_INTERLEAVE, &bitmask);
return mem;
}
__asm__(".symver numa_alloc_interleaved_subset_v1,numa_alloc_interleaved_subset@libnuma_1.1");
void *numa_alloc_interleaved_subset_v2(size_t size, struct bitmask *bmp)
{
char *mem;
mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);
if (mem == (char *)-1)
return NULL;
dombind(mem, size, MPOL_INTERLEAVE, bmp);
return mem;
}
__asm__(".symver numa_alloc_interleaved_subset_v2,numa_alloc_interleaved_subset@@libnuma_1.2");
make_internal_alias(numa_alloc_interleaved_subset_v1);
make_internal_alias(numa_alloc_interleaved_subset_v2);
void *
numa_alloc_interleaved(size_t size)
{
return numa_alloc_interleaved_subset_v2_int(size, numa_all_nodes_ptr);
}
/*
* given a user node mask, set memory policy to use those nodes
*/
void
numa_set_interleave_mask_v1(nodemask_t *mask)
{
struct bitmask *bmp;
int nnodes = numa_max_possible_node_v1_int()+1;
bmp = numa_bitmask_alloc(nnodes);
copy_nodemask_to_bitmask(mask, bmp);
if (numa_bitmask_equal(bmp, numa_no_nodes_ptr))
setpol(MPOL_DEFAULT, bmp);
else
setpol(MPOL_INTERLEAVE, bmp);
numa_bitmask_free(bmp);
}
__asm__(".symver numa_set_interleave_mask_v1,numa_set_interleave_mask@libnuma_1.1");
void
numa_set_interleave_mask_v2(struct bitmask *bmp)
{
if (numa_bitmask_equal(bmp, numa_no_nodes_ptr))
setpol(MPOL_DEFAULT, bmp);
else
setpol(MPOL_INTERLEAVE, bmp);
}
__asm__(".symver numa_set_interleave_mask_v2,numa_set_interleave_mask@@libnuma_1.2");
nodemask_t
numa_get_interleave_mask_v1(void)
{
int oldpolicy;
struct bitmask *bmp;
nodemask_t mask;
bmp = allocate_nodemask_v1();
getpol(&oldpolicy, bmp);
if (oldpolicy == MPOL_INTERLEAVE)
copy_bitmask_to_nodemask(bmp, &mask);
else
copy_bitmask_to_nodemask(numa_no_nodes_ptr, &mask);
numa_bitmask_free(bmp);
return mask;
}
__asm__(".symver numa_get_interleave_mask_v1,numa_get_interleave_mask@libnuma_1.1");
struct bitmask *
numa_get_interleave_mask_v2(void)
{
int oldpolicy;
struct bitmask *bmp;
bmp = numa_allocate_nodemask();
getpol(&oldpolicy, bmp);
if (oldpolicy != MPOL_INTERLEAVE)
copy_bitmask_to_bitmask(numa_no_nodes_ptr, bmp);
return bmp;
}
__asm__(".symver numa_get_interleave_mask_v2,numa_get_interleave_mask@@libnuma_1.2");
/* (undocumented) */
int numa_get_interleave_node(void)
{
int nd;
if (get_mempolicy(&nd, NULL, 0, 0, MPOL_F_NODE) == 0)
return nd;
return 0;
}
void *numa_alloc_onnode(size_t size, int node)
{
char *mem;
struct bitmask *bmp;