aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hugetlbfs.h4
-rw-r--r--hugeutils.c94
-rw-r--r--morecore.c5
-rwxr-xr-xrun_tests.sh2
-rw-r--r--tests/Makefile2
-rw-r--r--tests/hugetests.h4
-rw-r--r--tests/tempfile.c36
-rw-r--r--tests/unlinked_fd.c45
8 files changed, 134 insertions, 58 deletions
diff --git a/hugetlbfs.h b/hugetlbfs.h
index 9c7997d..55fcc2a 100644
--- a/hugetlbfs.h
+++ b/hugetlbfs.h
@@ -6,5 +6,9 @@
int gethugepagesize(void);
int hugetlbfs_test_path(const char *mount);
const char *hugetlbfs_find_path(void);
+int hugetlbfs_unlinked_fd(void);
+
+/* Debugging functions */
+int hugetlbfs_test_addr(void *p);
#endif /* _HUGETLBFS_H */
diff --git a/hugeutils.c b/hugeutils.c
index 408ca13..aeafa8a 100644
--- a/hugeutils.c
+++ b/hugeutils.c
@@ -29,17 +29,17 @@ static int read_meminfo(const char *format)
int val;
FILE *f;
int retcode;
- char buff[BUF_SZ];
+ char buf[BUF_SZ];
f = fopen("/proc/meminfo", "r");
if (!f) {
- ERROR("Failed to open /proc/meminfo for reading\n");
+ ERROR("Failed to open /proc/meminfo\n");
return -1;
}
while (!feof(f)) {
- fgets(buff, BUF_SZ, f);
- retcode = sscanf(buff, format, &val);
+ fgets(buf, BUF_SZ, f);
+ retcode = sscanf(buf, format, &val);
if (retcode == 1) {
fclose(f);
return val;
@@ -50,6 +50,44 @@ static int read_meminfo(const char *format)
return -1;
}
+#define MAPS_BUF_SZ 4096
+
+static int read_maps(unsigned long addr, char *buf)
+{
+ FILE *f;
+ char line[MAPS_BUF_SZ];
+ char *tmp;
+
+ f = fopen("/proc/self/maps", "r");
+ if (!f) {
+ ERROR("Failed to open /proc/self/maps\n");
+ return -1;
+ }
+
+ while (1) {
+ unsigned long start, end, off, ino;
+ int ret;
+
+ tmp = fgets(line, MAPS_BUF_SZ, f);
+ if (!tmp)
+ break;
+
+ ret = sscanf(line, "%lx-%lx %*s %lx %*s %ld %"
+ stringify(PATH_MAX) "s", &start, &end, &off, &ino,
+ buf);
+ if ((ret < 4) || (ret > 5)) {
+ ERROR("Couldn't parse /proc/self/maps line: %s\n",
+ line);
+ return -1;
+ }
+
+ if ((start <= addr) && (addr < end))
+ return 1;
+ }
+
+ return 0;
+}
+
/********************************************************************/
/* Library user visible functions */
/********************************************************************/
@@ -147,39 +185,61 @@ const char *hugetlbfs_find_path(void)
return NULL;
}
-int hugetlbfs_tempfile(int unlink_after_open)
+int hugetlbfs_unlinked_fd(void)
{
char name[PATH_MAX+1];
int fd;
name[sizeof(name)-1] = '\0';
strcpy(name, hugetlbfs_find_path());
- strncat(name, "/XXXXXX", sizeof(name)-1);
+ strncat(name, "/libhugetlbfs.tmp.XXXXXX", sizeof(name)-1);
/* FIXME: deal with overflows */
fd = mkstemp(name);
- if (unlink_after_open)
+ if (fd >= 0)
unlink(name);
return fd;
}
+int hugetlbfs_test_addr(void *p)
+{
+ char name[PATH_MAX+1];
+ char *dirend;
+ int ret;
+
+ ret = read_maps((unsigned long)p, name);
+ if (ret < 0)
+ return ret;
+ if (ret == 0) {
+ ERROR("Couldn't find addres %p in /proc/self/maps\n", p);
+ return -1;
+ }
+
+ /* Truncate the filename portion */
+ dirend = strrchr(name, '/');
+ if (dirend && dirend > name) {
+ *dirend = '\0';
+ }
+ return hugetlbfs_test_path(name);
+}
+
#if 0
int get_sysctl(char *file)
{
FILE* f;
- char buff[BUF_SZ];
+ char buf[BUF_SZ];
f = fopen(file, "r");
if (!f) {
printf("Failed to open %s for reading\n", file);
return(-1);
}
- fgets(buff, BUF_SZ, f);
+ fgets(buf, BUF_SZ, f);
fclose(f);
- return atoi(buff);
+ return atoi(buf);
}
int set_sysctl_str(char *file, char *str)
@@ -198,10 +258,10 @@ int set_sysctl_str(char *file, char *str)
int set_sysctl(char *file, int val)
{
- char buff[BUF_SZ];
+ char buf[BUF_SZ];
- snprintf(buff, BUF_SZ*sizeof(char), "%i", val);
- return set_sysctl_str(file, buff);
+ snprintf(buf, BUF_SZ*sizeof(char), "%i", val);
+ return set_sysctl_str(file, buf);
}
int mount_hugetlbfs(char *mountpoint, size_t strsz)
@@ -277,16 +337,16 @@ error:
int shmem_setup(int nr_hugepages)
{
int ret = 0;
- char buff[BUF_SZ];
+ char buf[BUF_SZ];
int hpage_size = get_hugepage_size();
unsigned long bytes = nr_hugepages * hpage_size * 1024;
- snprintf(buff, sizeof(char)*BUF_SZ, "%lu", bytes);
+ snprintf(buf, sizeof(char)*BUF_SZ, "%lu", bytes);
if (get_sysctl("/proc/sys/kernel/shmall") < bytes)
- ret = set_sysctl_str("/proc/sys/kernel/shmall", buff);
+ ret = set_sysctl_str("/proc/sys/kernel/shmall", buf);
if (get_sysctl("/proc/sys/kernel/shmmax") < bytes)
- ret += set_sysctl_str("/proc/sys/kernel/shmmax", buff);
+ ret += set_sysctl_str("/proc/sys/kernel/shmmax", buf);
if (ret)
return -1;
else
diff --git a/morecore.c b/morecore.c
index b1f9868..740df91 100644
--- a/morecore.c
+++ b/morecore.c
@@ -65,6 +65,9 @@ void __attribute__ ((constructor)) setup_morecore(void)
orig_morecore = __morecore;
env = getenv("HUGETLB_MORECORE");
- if (env)
+ if (env) {
__morecore = &hugetlbfs_morecore;
+ /* we always want to use our morecore, not mmap() */
+ mallopt(M_MMAP_MAX, 0);
+ }
}
diff --git a/run_tests.sh b/run_tests.sh
index 24d7d24..887bab9 100755
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -16,4 +16,4 @@ run_test () {
run_test gethugepagesize
run_test test_root_hugetlbfs
run_test find_path
-run_test tempfile
+run_test unlinked_fd
diff --git a/tests/Makefile b/tests/Makefile
index 6195279..171542d 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,4 +1,4 @@
-TESTS = gethugepagesize test_root_hugetlbfs find_path tempfile
+TESTS = gethugepagesize test_root_hugetlbfs find_path unlinked_fd
CPPFLAGS = -I..
LDFLAGS = -L..
diff --git a/tests/hugetests.h b/tests/hugetests.h
index 9107e6a..319d8d7 100644
--- a/tests/hugetests.h
+++ b/tests/hugetests.h
@@ -26,14 +26,14 @@ void cleanup();
#define PASS() \
do { \
cleanup(); \
- printf("PASS\n", test_name); \
+ printf("PASS\n"); \
exit(RC_PASS); \
} while (0)
#define FAIL(s) \
do { \
cleanup(); \
- printf("FAIL\t%s\n", test_name, s); \
+ printf("FAIL\t%s\n", s); \
exit(RC_FAIL); \
} while (0)
diff --git a/tests/tempfile.c b/tests/tempfile.c
deleted file mode 100644
index 235add0..0000000
--- a/tests/tempfile.c
+++ /dev/null
@@ -1,36 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/mman.h>
-
-#include <hugetlbfs.h>
-
-#include "hugetests.h"
-
-int main(int argc, char *argv[])
-{
- int fd;
- void *p;
- int err;
-
- test_init(argc, argv);
-
- fd = hugetlbfs_tempfile(1);
- if (fd < 0)
- CONFIG();
-
- p = mmap(NULL, gethugepagesize(), PROT_READ|PROT_WRITE, MAP_SHARED,
- fd, 0);
- if (p == MAP_FAILED)
- FAIL("mmap");
-
- err = munmap(p, gethugepagesize());
- if (err != 0)
- FAIL("munmap");
-
- if (close(fd))
- FAIL("close");
-
- PASS();
-}
-
diff --git a/tests/unlinked_fd.c b/tests/unlinked_fd.c
new file mode 100644
index 0000000..26cbca3
--- /dev/null
+++ b/tests/unlinked_fd.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+#include <hugetlbfs.h>
+
+#include "hugetests.h"
+
+int main(int argc, char *argv[])
+{
+ int hpage_size;
+ int fd;
+ void *p;
+ int err;
+
+ test_init(argc, argv);
+
+ hpage_size = gethugepagesize();
+ if (hpage_size < 0)
+ CONFIG();
+
+ fd = hugetlbfs_unlinked_fd();
+ if (fd < 0)
+ FAIL("hugetlbfs_tempfile()");
+
+ p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+ fd, 0);
+ if (p == MAP_FAILED)
+ FAIL("mmap()");
+
+ err = hugetlbfs_test_addr(p);
+ if (err != 1)
+ FAIL("Mapped address is not hugepage");
+
+ err = munmap(p, hpage_size);
+ if (err != 0)
+ FAIL("munmap()");
+
+ if (close(fd))
+ FAIL("close()");
+
+ PASS();
+}
+