| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _LARGEFILE64_SOURCE |
| 21 |
#define _GNU_SOURCE |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <limits.h> |
| 26 |
#include <string.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <ctype.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <signal.h> |
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/vfs.h> |
| 33 |
#include <sys/ipc.h> |
| 34 |
#include <sys/shm.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <sys/mman.h> |
| 37 |
#include <fcntl.h> |
| 38 |
|
| 39 |
#include "hugetlbfs.h" |
| 40 |
#include "libhugetlbfs_privutils.h" |
| 41 |
#include "hugetests.h" |
| 42 |
|
| 43 |
void check_free_huge_pages(int nr_pages_needed) |
| 44 |
{ |
| 45 |
long hpage_size = gethugepagesize(); |
| 46 |
int freepages = get_huge_page_counter(hpage_size, HUGEPAGES_FREE); |
| 47 |
if (freepages < nr_pages_needed) |
| 48 |
CONFIG("Must have at least %i free hugepages", nr_pages_needed); |
| 49 |
} |
| 50 |
|
| 51 |
int using_system_hpage_size(const char *mount) |
| 52 |
{ |
| 53 |
struct statfs64 sb; |
| 54 |
int err; |
| 55 |
long meminfo_size, mount_size; |
| 56 |
|
| 57 |
if (!mount) |
| 58 |
FAIL("using_system_hpage_size: hugetlbfs is not mounted\n"); |
| 59 |
|
| 60 |
err = statfs64(mount, &sb); |
| 61 |
if (err) |
| 62 |
FAIL("statfs64: %s\n", strerror(errno)); |
| 63 |
|
| 64 |
meminfo_size = read_meminfo("Hugepagesize:"); |
| 65 |
if (meminfo_size < 0) |
| 66 |
FAIL("using_system_hpage_size: Failed to read /proc/meminfo\n"); |
| 67 |
|
| 68 |
mount_size = sb.f_bsize / 1024; |
| 69 |
if (mount_size == meminfo_size) |
| 70 |
return 1; |
| 71 |
else |
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
int kernel_has_private_reservations(void) |
| 81 |
{ |
| 82 |
int fd; |
| 83 |
long t, f, r, s; |
| 84 |
long nt, nf, nr, ns; |
| 85 |
long hpage_size = gethugepagesize(); |
| 86 |
void *map; |
| 87 |
|
| 88 |
|
| 89 |
t = get_huge_page_counter(hpage_size, HUGEPAGES_TOTAL); |
| 90 |
f = get_huge_page_counter(hpage_size, HUGEPAGES_FREE); |
| 91 |
r = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD); |
| 92 |
s = get_huge_page_counter(hpage_size, HUGEPAGES_SURP); |
| 93 |
|
| 94 |
fd = hugetlbfs_unlinked_fd(); |
| 95 |
if (fd < 0) { |
| 96 |
ERROR("kernel_has_private_reservations: hugetlbfs_unlinked_fd: " |
| 97 |
"%s\n", strerror(errno)); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
map = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 101 |
if (map == MAP_FAILED) { |
| 102 |
ERROR("kernel_has_private_reservations: mmap: %s\n", |
| 103 |
strerror(errno)); |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
nt = get_huge_page_counter(hpage_size, HUGEPAGES_TOTAL); |
| 109 |
nf = get_huge_page_counter(hpage_size, HUGEPAGES_FREE); |
| 110 |
nr = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD); |
| 111 |
ns = get_huge_page_counter(hpage_size, HUGEPAGES_SURP); |
| 112 |
|
| 113 |
munmap(map, hpage_size); |
| 114 |
close(fd); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if ((nt == t + 1) && (nf == f + 1) && (ns == s + 1) && (nr == r + 1)) { |
| 126 |
return 1; |
| 127 |
} else if ((nt == t) && (nf == f) && (ns == s)) { |
| 128 |
if (nr == r + 1) |
| 129 |
return 1; |
| 130 |
else if (nr == r) |
| 131 |
return 0; |
| 132 |
} else { |
| 133 |
ERROR("kernel_has_private_reservations: bad counter state - " |
| 134 |
"T:%li F:%li R:%li S:%li -> T:%li F:%li R:%li S:%li\n", |
| 135 |
t, f, r, s, nt, nf, nr, ns); |
| 136 |
} |
| 137 |
return -1; |
| 138 |
} |