forked from libhugetlbfs/libhugetlbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hugeutils.c
1190 lines (1017 loc) · 27.5 KB
/
hugeutils.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
/*
* libhugetlbfs - Easy use of Linux hugepages
* Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
*
* This library 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; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library 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 have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _LARGEFILE64_SOURCE /* Need this for statfs64 */
#define _GNU_SOURCE
#include <dlfcn.h>
#include <features.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/vfs.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <sys/syscall.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include <dirent.h>
#include "libhugetlbfs_internal.h"
#include "hugetlbfs.h"
struct libhugeopts_t __hugetlb_opts;
static int hugepagesize_errno; /* = 0 */
#define MAX_HPAGE_SIZES 10
static struct hpage_size hpage_sizes[MAX_HPAGE_SIZES];
static int nr_hpage_sizes;
static int hpage_sizes_default_idx = -1;
static long default_size;
/********************************************************************/
/* Internal functions */
/********************************************************************/
/*
* Lookup the kernel default page size.
*/
long kernel_default_hugepage_size()
{
if (default_size == 0) {
default_size = file_read_ulong(MEMINFO, "Hugepagesize:");
default_size = size_to_smaller_unit(default_size); /* kB to B */ }
return default_size;
}
void kernel_default_hugepage_size_reset(void)
{
default_size = 0;
}
#define BUF_SZ 256
#define MEMINFO_SIZE 2048
/*
* Convert a quantity in a given unit to the next smallest unit by
* multiplying the quantity by 1024 (eg. convert 1MB to 1024kB).
* If the conversion would overflow the variable, return ULONGLONG_MAX to
* signify the error.
*/
unsigned long long size_to_smaller_unit(unsigned long long size)
{
if (size * 1024 < size)
return -1;
else
return size * 1024;
}
/*
* Convert a page size string with an optional unit suffix into a page size
* in bytes.
*
* On error, -1 is returned and errno is set appropriately:
* EINVAL - str could not be parsed or was not greater than zero
* EOVERFLOW - Overflow when converting from the specified units
*/
long parse_page_size(const char *str)
{
char *pos;
long size;
errno = 0;
size = strtol(str, &pos, 0);
/* Catch strtoul errors and sizes that overflow the native word size */
if (errno || str == pos || size <= 0) {
if (errno == ERANGE)
errno = EOVERFLOW;
else
errno = EINVAL;
return -1;
}
switch (*pos) {
case 'G':
case 'g':
size = size_to_smaller_unit(size);
case 'M':
case 'm':
size = size_to_smaller_unit(size);
case 'K':
case 'k':
size = size_to_smaller_unit(size);
}
if (size < 0)
errno = EOVERFLOW;
return size;
}
struct hugetlb_pool_counter_info_t {
char *meminfo_key;
char *sysfs_file;
};
static struct hugetlb_pool_counter_info_t hugetlb_counter_info[] = {
[HUGEPAGES_TOTAL] = {
.meminfo_key = "HugePages_Total:",
.sysfs_file = "nr_hugepages",
},
[HUGEPAGES_TOTAL_MEMPOL] = {
.meminfo_key = "HugePages_Total:",
.sysfs_file = "nr_hugepages_mempolicy",
},
[HUGEPAGES_FREE] = {
.meminfo_key = "HugePages_Free:",
.sysfs_file = "free_hugepages",
},
[HUGEPAGES_RSVD] = {
.meminfo_key = "HugePages_Rsvd:",
.sysfs_file = "resv_hugepages",
},
[HUGEPAGES_SURP] = {
.meminfo_key = "HugePages_Surp:",
.sysfs_file = "surplus_hugepages",
},
[HUGEPAGES_OC] = {
.meminfo_key = NULL,
.sysfs_file = "nr_overcommit_hugepages"
},
};
/*
* Read numeric data from raw and tagged kernel status files. Used to read
* /proc and /sys data (without a tag) and from /proc/meminfo (with a tag).
*/
long file_read_ulong(char *file, const char *tag)
{
int fd;
char buf[MEMINFO_SIZE];
int len, readerr;
char *p, *q;
long val;
fd = open(file, O_RDONLY);
if (fd < 0) {
ERROR("Couldn't open %s: %s\n", file, strerror(errno));
return -1;
}
len = read(fd, buf, sizeof(buf));
readerr = errno;
close(fd);
if (len < 0) {
ERROR("Error reading %s: %s\n", file, strerror(readerr));
return -1;
}
if (len == sizeof(buf)) {
ERROR("%s is too large\n", file);
return -1;
}
buf[len] = '\0';
/* Search for a tag if provided */
if (tag) {
p = strstr(buf, tag);
if (!p)
return -1; /* looks like the line we want isn't there */
p += strlen(tag);
} else
p = buf;
val = strtol(p, &q, 0);
if (! isspace(*q)) {
ERROR("Couldn't parse %s value\n", file);
return -1;
}
return val;
}
int file_write_ulong(char *file, unsigned long val)
{
FILE *f;
int ret;
f = fopen(file, "w");
if (!f) {
ERROR("Couldn't open %s: %s\n", file, strerror(errno));
return -1;
}
ret = fprintf(f, "%lu", val);
fclose(f);
return ret > 0 ? 0 : -1;
}
/*
* Return the name of this executable, using buf as temporary space.
*/
#define MAX_EXE 4096
static char *get_exe_name(char *buf, int size)
{
char *p;
int fd;
ssize_t nread;
buf[0] = 0;
fd = open("/proc/self/cmdline", O_RDONLY);
if (fd < 0) {
WARNING("Unable to open cmdline, no exe name\n");
return buf;
}
nread = read(fd, buf, size-1);
close(fd);
if (nread < 0) {
WARNING("Error %d reading cmdline, no exe name\n", errno);
return buf;
}
if (nread == 0) {
WARNING("Read zero bytes from cmdline, no exe name\n");
return buf;
}
buf[nread] = 0; /* make sure we're null terminated */
/*
* Take advantage of cmdline being a series of null-terminated
* strings. The first string is the path to the executable in
* the form:
*
* /path/to/exe
*
* The exe name starts one character after the last '/'.
*/
p = strrchr(buf, '/');
if (!p)
return buf;
return p + 1; /* skip over "/" */
}
/*
* Reads the contents of hugetlb environment variables and save their
* values for later use.
*/
void hugetlbfs_setup_env()
{
char *env;
__hugetlb_opts.min_copy = true;
env = getenv("HUGETLB_VERBOSE");
if (env)
__hugetlbfs_verbose = atoi(env);
env = getenv("HUGETLB_DEBUG");
if (env) {
__hugetlbfs_debug = true;
__hugetlbfs_verbose = VERBOSE_DEBUG;
}
env = getenv("HUGETLB_RESTRICT_EXE");
if (env) {
char *p, *tok, *exe, buf[MAX_EXE+1], restriction[MAX_EXE];
int found = 0;
exe = get_exe_name(buf, sizeof buf);
DEBUG("Found HUGETLB_RESTRICT_EXE, this exe is \"%s\"\n", exe);
strncpy(restriction, env, sizeof restriction);
restriction[sizeof(restriction)-1] = 0;
for (p = restriction; (tok = strtok(p, ":")) != NULL; p = NULL) {
DEBUG(" ...check exe match for \"%s\"\n", tok);
if (strcmp(tok, exe) == 0) {
found = 1;
DEBUG("exe match - libhugetlbfs is active for this exe\n");
break;
}
}
if (!found) {
DEBUG("No exe match - libhugetlbfs is inactive for this exe\n");
return;
}
}
env = getenv("HUGETLB_NO_PREFAULT");
if (env)
__hugetlbfs_prefault = false;
__hugetlb_opts.share_path = getenv("HUGETLB_SHARE_PATH");
__hugetlb_opts.elfmap = getenv("HUGETLB_ELFMAP");
__hugetlb_opts.ld_preload = getenv("LD_PRELOAD");
__hugetlb_opts.def_page_size = getenv("HUGETLB_DEFAULT_PAGE_SIZE");
__hugetlb_opts.path = getenv("HUGETLB_PATH");
__hugetlb_opts.features = getenv("HUGETLB_FEATURES");
__hugetlb_opts.morecore = getenv("HUGETLB_MORECORE");
__hugetlb_opts.heapbase = getenv("HUGETLB_MORECORE_HEAPBASE");
if (__hugetlb_opts.morecore)
__hugetlb_opts.thp_morecore =
(strcasecmp(__hugetlb_opts.morecore, "thp") == 0);
if (__hugetlb_opts.thp_morecore && __hugetlb_opts.heapbase) {
DEBUG("Heapbase specified with THP for morecore, ignoring heapbase\n");
__hugetlb_opts.heapbase = NULL;
}
env = getenv("HUGETLB_FORCE_ELFMAP");
if (env && (strcasecmp(env, "yes") == 0))
__hugetlb_opts.force_elfmap = 1;
env = getenv("HUGETLB_MINIMAL_COPY");
if (__hugetlb_opts.min_copy && env && (strcasecmp(env, "no") == 0)) {
INFO("HUGETLB_MINIMAL_COPY=%s, disabling filesz copy "
"optimization\n", env);
__hugetlb_opts.min_copy = false;
}
env = getenv("HUGETLB_SHARE");
if (env)
__hugetlb_opts.sharing = atoi(env);
/*
* We have been seeing some unexpected behavior from malloc when
* heap shrinking is enabled, so heap shrinking is disabled by
* default.
*
* If malloc has been called successfully before setup_morecore,
* glibc will notice a gap between the previous top-of-heap and
* the new top-of-heap when it calls hugetlbfs_morecore. It treats
* this as a "foreign sbrk." Unfortunately, the "foreign sbrk"
* handling code will then immediately try to free the memory
* allocated by hugetlbfs_morecore!
*
* This behavior has been reported to the ptmalloc2 maintainer,
* along with a patch to correct the behavior.
*/
env = getenv("HUGETLB_MORECORE_SHRINK");
if (env && strcasecmp(env, "yes") == 0)
__hugetlb_opts.shrink_ok = true;
/* Determine if shmget() calls should be overridden */
env = getenv("HUGETLB_SHM");
if (env && !strcasecmp(env, "yes"))
__hugetlb_opts.shm_enabled = true;
/* Determine if all reservations should be avoided */
env = getenv("HUGETLB_NO_RESERVE");
if (env && !strcasecmp(env, "yes"))
__hugetlb_opts.no_reserve = true;
}
void hugetlbfs_setup_kernel_page_size()
{
long page_size = kernel_default_hugepage_size();
if (page_size <= 0) {
WARNING("Unable to find default kernel huge page size\n");
return;
}
INFO("Found pagesize %ld kB\n", page_size / 1024);
hpage_sizes[0].pagesize = page_size;
nr_hpage_sizes = 1;
}
void hugetlbfs_check_priv_resv()
{
/*
* If the kernel supports MAP_PRIVATE reservations, we can skip
* prefaulting the huge pages we allocate since the kernel
* guarantees them. This can help NUMA performance quite a bit.
*/
if (hugetlbfs_test_feature(HUGETLB_FEATURE_PRIVATE_RESV) > 0) {
INFO("Kernel has MAP_PRIVATE reservations. Disabling "
"heap prefaulting.\n");
__hugetlbfs_prefault = false;
}
}
void hugetlbfs_check_safe_noreserve()
{
/*
* Some kernels will trigger an OOM if MAP_NORESERVE is used and
* a huge page allocation fails. This is unfortunate so limit
* the user of NORESERVE where necessary
*/
if (__hugetlb_opts.no_reserve &&
hugetlbfs_test_feature(HUGETLB_FEATURE_SAFE_NORESERVE) <= 0) {
INFO("Kernel is not safe for MAP_NORESERVE. Forcing "
"use of reservations.\n");
__hugetlb_opts.no_reserve = false;
}
}
void hugetlbfs_check_map_hugetlb()
{
/*
* FIXME: MAP_HUGETLB has not been picked up by glibc so even though the
* kernel may support it, without the userspace mmap flag it cannot be
* used. This ifdef should be removed when the MAP_HUGETLB flag makes it
* into glibc.
*/
#ifdef MAP_HUGETLB
/*
* Kernels after 2.6.32 support mmaping pseudo-anonymous regions
* backed by huge pages, use this feature for huge pages we
* don't intend to share.
*/
if (hugetlbfs_test_feature(HUGETLB_FEATURE_MAP_HUGETLB) > 0) {
INFO("Kernel supports MAP_HUGETLB\n");
__hugetlb_opts.map_hugetlb = true;
}
#endif
}
/*
* Pool counters are typically exposed in sysfs in modern kernels, the
* counters for the default page size are exposed in procfs in all kernels
* supporting hugepages. Given a specific counter (e.g. HUGEPAGES_RSVD)
* and a page size return both a filename and an optional tag to locate
* and extract this counter.
*/
static int select_pool_counter(unsigned int counter, unsigned long pagesize,
char *filename, char **key)
{
long default_size;
char *meminfo_key;
char *sysfs_file;
if (counter >= HUGEPAGES_MAX_COUNTERS) {
ERROR("Invalid counter specified\n");
return -1;
}
meminfo_key = hugetlb_counter_info[counter].meminfo_key;
sysfs_file = hugetlb_counter_info[counter].sysfs_file;
if (key)
*key = NULL;
/*
* Get the meminfo page size.
* This could be made more efficient if utility functions were shared
* between libhugetlbfs and the test suite. For now we will just
* read /proc/meminfo.
*/
default_size = kernel_default_hugepage_size();
if (default_size < 0) {
ERROR("Cannot determine the default page size\n");
return -1;
}
/* If the user is dealing in the default page size, we can use /proc */
if (pagesize == default_size) {
if (meminfo_key && key) {
strcpy(filename, MEMINFO);
*key = meminfo_key;
} else
sprintf(filename, PROC_HUGEPAGES_DIR "%s", sysfs_file);
} else /* Use the sysfs interface */
sprintf(filename, SYSFS_HUGEPAGES_DIR "hugepages-%lukB/%s",
pagesize / 1024, sysfs_file);
return 0;
}
static int hpage_size_to_index(unsigned long size)
{
int i;
for (i = 0; i < nr_hpage_sizes; i++)
if (hpage_sizes[i].pagesize == size)
return i;
return -1;
}
void probe_default_hpage_size(void)
{
long size;
int index;
int default_overrided;
if (nr_hpage_sizes == 0) {
INFO("No configured huge page sizes\n");
hpage_sizes_default_idx = -1;
return;
}
/*
* Check if the user specified a default size, otherwise use the
* system default size as reported by /proc/meminfo.
*/
default_overrided = (__hugetlb_opts.def_page_size &&
strlen(__hugetlb_opts.def_page_size) > 0);
if (default_overrided)
size = parse_page_size(__hugetlb_opts.def_page_size);
else {
size = kernel_default_hugepage_size();
}
if (size >= 0) {
index = hpage_size_to_index(size);
if (index >= 0)
hpage_sizes_default_idx = index;
else {
/*
* If the user specified HUGETLB_DEFAULT_PAGE_SIZE,
* then this situation will alter semantics and they
* should receive a WARNING. Otherwise, this detail
* is purely informational in nature.
*/
char msg[] = "No mount point found for default huge " \
"page size. Using first available mount "
"point.\n";
if (default_overrided)
WARNING("%s", msg);
else
INFO("%s", msg);
hpage_sizes_default_idx = 0;
}
} else {
ERROR("Unable to determine default huge page size\n");
hpage_sizes_default_idx = -1;
}
}
static void add_hugetlbfs_mount(char *path, int user_mount)
{
int idx;
long size;
if (strlen(path) > PATH_MAX)
return;
if (!hugetlbfs_test_path(path)) {
WARNING("%s is not a hugetlbfs mount point, ignoring\n", path);
return;
}
size = hugetlbfs_test_pagesize(path);
if (size < 0) {
WARNING("Unable to detect page size for path %s\n", path);
return;
}
idx = hpage_size_to_index(size);
if (idx < 0) {
if (nr_hpage_sizes >= MAX_HPAGE_SIZES) {
WARNING("Maximum number of huge page sizes exceeded, "
"ignoring %lukB page size\n", size);
return;
}
idx = nr_hpage_sizes;
hpage_sizes[nr_hpage_sizes++].pagesize = size;
}
if (strlen(hpage_sizes[idx].mount)) {
if (user_mount)
WARNING("Mount point already defined for size %li, "
"ignoring %s\n", size, path);
return;
}
strcpy(hpage_sizes[idx].mount, path);
}
void debug_show_page_sizes(void)
{
int i;
INFO("Detected page sizes:\n");
for (i = 0; i < nr_hpage_sizes; i++)
INFO(" Size: %li kB %s Mount: %s\n",
hpage_sizes[i].pagesize / 1024,
i == hpage_sizes_default_idx ? "(default)" : "",
hpage_sizes[i].mount);
}
#define LINE_MAXLEN 2048
static void find_mounts(void)
{
int fd;
char path[PATH_MAX+1];
char line[LINE_MAXLEN + 1];
char *eol;
char *match;
char *end;
int bytes;
off_t offset;
fd = open("/proc/mounts", O_RDONLY);
if (fd < 0) {
fd = open("/etc/mtab", O_RDONLY);
if (fd < 0) {
ERROR("Couldn't open /proc/mounts or /etc/mtab (%s)\n",
strerror(errno));
return;
}
}
while ((bytes = read(fd, line, LINE_MAXLEN)) > 0) {
line[LINE_MAXLEN] = '\0';
eol = strchr(line, '\n');
if (!eol) {
ERROR("Line too long when parsing mounts\n");
break;
}
/*
* Truncate the string to just one line and reset the file
* to begin reading at the start of the next line.
*/
*eol = '\0';
offset = bytes - (eol + 1 - line);
lseek(fd, -offset, SEEK_CUR);
/* Match only hugetlbfs filesystems. */
match = strstr(line, " hugetlbfs ");
if (match) {
match = strchr(line, '/');
if (!match)
continue;
end = strchr(match, ' ');
if (!end)
continue;
strncpy(path, match, end - match);
path[end - match] = '\0';
if ((hugetlbfs_test_path(path) == 1) &&
!(access(path, R_OK | W_OK | X_OK)))
add_hugetlbfs_mount(path, 0);
}
}
close(fd);
}
void setup_mounts(void)
{
int do_scan = 1;
/* If HUGETLB_PATH is set, only add mounts specified there */
while (__hugetlb_opts.path) {
char path[PATH_MAX + 1];
char *next = strchrnul(__hugetlb_opts.path, ':');
do_scan = 0;
if (next - __hugetlb_opts.path > PATH_MAX) {
ERROR("Path too long in HUGETLB_PATH -- "
"ignoring environment\n");
break;
}
strncpy(path, __hugetlb_opts.path, next - __hugetlb_opts.path);
path[next - __hugetlb_opts.path] = '\0';
add_hugetlbfs_mount(path, 1);
/* skip the ':' token */
__hugetlb_opts.path = *next == '\0' ? NULL : next + 1;
}
/* Then probe all mounted filesystems */
if (do_scan)
find_mounts();
}
int get_pool_size(long size, struct hpage_pool *pool)
{
long nr_over = 0;
long nr_used = 0;
long nr_surp = 0;
long nr_resv = 0;
long nr_static = 0;
long it_used = -1;
long it_surp = -1;
long it_resv = -1;
/*
* Pick up those values which are basically stable with respect to
* the admin; ie. only changed by them.
*
* nr_over may be negative if this kernel does not support overcommit
* in that case we will consider it always 0 and max will track min
* always.
*/
nr_over = get_huge_page_counter(size, HUGEPAGES_OC);
if (nr_over < 0)
nr_over = 0;
/* Sample the volatile values until they are stable. */
while (nr_used != it_used || nr_surp != it_surp || nr_resv != it_resv) {
nr_used = it_used;
nr_surp = it_surp;
nr_resv = it_resv;
it_used = get_huge_page_counter(size, HUGEPAGES_TOTAL);
it_surp = get_huge_page_counter(size, HUGEPAGES_SURP);
it_resv = get_huge_page_counter(size, HUGEPAGES_RSVD);
}
if (nr_surp < 0)
nr_surp = 0;
if (nr_resv < 0)
nr_resv = 0;
nr_static = nr_used - nr_surp;
if (nr_static >= 0) {
DEBUG("pagesize<%ld> min<%ld> max<%ld> "
"in-use<%ld>\n",
size, nr_static, nr_static + nr_over,
nr_used);
pool->pagesize = size;
pool->minimum = nr_static;
pool->maximum = nr_static + nr_over;
pool->size = nr_used;
pool->is_default = 0;
return 1;
}
return 0;
}
int hpool_sizes(struct hpage_pool *pools, int pcnt)
{
long default_size;
int which = 0;
DIR *dir;
struct dirent *entry;
default_size = kernel_default_hugepage_size();
if (default_size >= 0 && which < pcnt)
if (get_pool_size(default_size, &pools[which])) {
pools[which].is_default = 1;
which++;
}
dir = opendir(SYSFS_HUGEPAGES_DIR);
if (dir) {
while ((entry = readdir(dir))) {
char *name = entry->d_name;
long size;
DEBUG("parsing<%s>\n", name);
if (strncmp(name, "hugepages-", 10) != 0)
continue;
name += 10;
size = size_to_smaller_unit(atol(name));
if (size < 0 || size == default_size)
continue;
if (get_pool_size(size, &pools[which]))
which++;
}
closedir(dir);
}
return (which < pcnt) ? which : -1;
}
/*
* If we have a default page size then we support hugepages.
*/
int kernel_has_hugepages(void)
{
long default_size = kernel_default_hugepage_size();
if (default_size < 0)
return 0;
return 1;
}
/*
* If we can find the default page size, and if we can find an overcommit
* control for it then the kernel must support overcommit.
*/
int kernel_has_overcommit(void)
{
long default_size = kernel_default_hugepage_size();
if (default_size < 0)
return 0;
if (get_huge_page_counter(default_size, HUGEPAGES_OC) < 0)
return 0;
return 1;
}
/********************************************************************/
/* Library user visible functions */
/********************************************************************/
/*
* NOTE: This function uses data that is initialized by
* setup_mounts() which is called during libhugetlbfs initialization.
*
* returns:
* on success, size of a huge page in number of bytes
* on failure, -1
* errno set to ENOSYS if huge pages are not supported
* errno set to EOVERFLOW if huge page size would overflow return type
*/
long gethugepagesize(void)
{
long hpage_size;
/* Are huge pages available and have they been initialized? */
if (hpage_sizes_default_idx == -1) {
errno = hugepagesize_errno = ENOSYS;
return -1;
}
errno = 0;
hpage_size = hpage_sizes[hpage_sizes_default_idx].pagesize;
return hpage_size;
}
int gethugepagesizes(long pagesizes[], int n_elem)
{
long default_size;
DIR *sysfs;
struct dirent *ent;
int nr_sizes = 0;
if (n_elem < 0) {
errno = EINVAL;
return -1;
}
if (n_elem > 0 && pagesizes == NULL) {
errno = EINVAL;
return -1;
}
errno = 0;
/* Get the system default size. */
default_size = kernel_default_hugepage_size();
if (default_size < 0)
return 0;
if (pagesizes && (nr_sizes == n_elem))
return nr_sizes;
if (pagesizes)
pagesizes[nr_sizes] = default_size;
nr_sizes++;
/*
* Scan sysfs to look for other sizes.
* Non-existing dir is not an error, we got one size from /proc/meminfo.
*/
sysfs = opendir(SYSFS_HUGEPAGES_DIR);
if (!sysfs) {
if (errno == ENOENT) {
errno = 0;
return nr_sizes;
} else
return -1;
}
while ((ent = readdir(sysfs))) {
long size;
if (strncmp(ent->d_name, "hugepages-", 10))
continue;
size = strtol(ent->d_name + 10, NULL, 10);
if (size == LONG_MIN || size == LONG_MAX)
continue;
size = size_to_smaller_unit(size);
if (size < 0 || size == default_size)
continue;
if (pagesizes && (nr_sizes == n_elem))
return nr_sizes;
if (pagesizes)
pagesizes[nr_sizes] = size;
nr_sizes++;
}
closedir(sysfs);
return nr_sizes;
}
int getpagesizes(long pagesizes[], int n_elem)
{
int ret;
if (n_elem < 0 || (n_elem > 0 && pagesizes == NULL)) {
errno = EINVAL;
return -1;
}
/* Requests for sizing, we need one more slot than gethugepagesizes. */
if (pagesizes == NULL && n_elem == 0) {
ret = gethugepagesizes(pagesizes, n_elem);
} else {
/* Install the base page size. */
if (pagesizes && n_elem == 0)
return 0;
if (pagesizes)
pagesizes[0] = sysconf(_SC_PAGESIZE);
ret = gethugepagesizes(pagesizes + 1, n_elem - 1);
}
if (ret < 0)
return ret;
return ret + 1;
}
int hugetlbfs_test_path(const char *mount)
{
struct statfs64 sb;
int err;
/* Bugs in the 32<->64 translation code in pre-2.6.15 kernels
* mean that plain statfs() returns bogus errors on hugetlbfs
* filesystems. Use statfs64() to work around. */
err = statfs64(mount, &sb);
if (err)
return -1;
return (sb.f_type == HUGETLBFS_MAGIC);
}
/* Return the page size for the given mount point in bytes */
long hugetlbfs_test_pagesize(const char *mount)
{
struct statfs64 sb;
int err;
err = statfs64(mount, &sb);
if (err)
return -1;
if ((sb.f_bsize <= 0) || (sb.f_bsize > LONG_MAX))
return -1;
return sb.f_bsize;
}
const char *hugetlbfs_find_path_for_size(long page_size)
{
char *path;
int idx;
idx = hpage_size_to_index(page_size);
if (idx >= 0) {
path = hpage_sizes[idx].mount;
if (strlen(path))
return path;
}
return NULL;
}
const char *hugetlbfs_find_path(void)
{
long hpage_size = gethugepagesize();
if (hpage_size > 0)
return hugetlbfs_find_path_for_size(hpage_size);