First allocation in malloc tests is just 1024 bytes.
If there is enough free mem in malloc arenas, this first
allocation can be satisfied without call to MORECORE()
and testcase will fail.

LD_PRELOAD=libhugetlbfs.so HUGETLB_MORECORE=yes malloc (2M: 64):        FAIL    
Address is not hugepage
When looking at output from HUGETLB_DEBUG=1 it appears that
hook to __morecore doesn't work as there is no trace
from hugetlbfs_morecore.

This patch will keep malloc-ing 1-byte chunks of memory
as long as it comes from '[heap]' or maximum is reached.
This will force glibc to call MORECORE() before actual
malloc/malloc_manysmall test begins.

Signed-off-by: Jan Stancek <jstan...@redhat.com>
---
 tests/hugetests.h        |    1 +
 tests/malloc.c           |    3 +++
 tests/malloc_manysmall.c |    3 +++
 tests/testutils.c        |   36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/tests/hugetests.h b/tests/hugetests.h
index a5a54d6..cdcb41b 100644
--- a/tests/hugetests.h
+++ b/tests/hugetests.h
@@ -48,6 +48,7 @@ int test_addr_huge(void *p);
 unsigned long long get_mapping_page_size(void *p);
 long read_meminfo(const char *tag);
 ino_t get_addr_inode(void *p);
+int consume_heap(long max);
 
 #define ALIGN(x, a)    (((x) + (a) - 1) & ~((a) - 1))
 #define PALIGN(p, a)   ((void *)ALIGN((unsigned long)(p), (a)))
diff --git a/tests/malloc.c b/tests/malloc.c
index a1af5e1..dbdd617 100644
--- a/tests/malloc.c
+++ b/tests/malloc.c
@@ -61,6 +61,9 @@ int main(int argc, char *argv[])
                expect_hugepage = 1;
        verbose_printf("expect_hugepage=%d\n", expect_hugepage);
 
+       if (expect_hugepage)
+               consume_heap(1024*1024);
+
        for (i = 0; i < NUM_SIZES; i++) {
                int size = block_sizes[i];
                unsigned long long mapping_size;
diff --git a/tests/malloc_manysmall.c b/tests/malloc_manysmall.c
index 25086a8..15cf4f0 100644
--- a/tests/malloc_manysmall.c
+++ b/tests/malloc_manysmall.c
@@ -50,6 +50,9 @@ int main(int argc, char *argv[])
        if (env)
                expect_hugepage = 1;
 
+       if (expect_hugepage)
+               consume_heap(1024*1024);
+
        for (i = 0; i < NUM_ALLOCS; i++) {
                p = malloc(ALLOC_SIZE);
                if (! p)
diff --git a/tests/testutils.c b/tests/testutils.c
index 68d8e62..2dc5d5d 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -162,6 +162,42 @@ static int read_maps(unsigned long addr, char *buf)
        return 0;
 }
 
+/* malloc memory one byte at a time until:
+ * - max limit is reached
+ * - malloc-ed memory is no longer on heap
+ */
+int consume_heap(long max)
+{
+       char aname[256];
+       int ret;
+       long consumed = 0;
+       void *p;
+
+       while (1) {
+               p = malloc(1);
+               if (p == NULL)
+                       FAIL("Could not malloc memory");
+
+               ret = read_maps((unsigned long)p, aname);
+               if (ret <= 0)
+                       FAIL("read_maps ret <= 0");
+
+               if (strstr(aname, "heap") == NULL) {
+                       ret = 0;
+                       break;
+               }
+
+               consumed++;
+               if (consumed > max) {
+                       verbose_printf("Warning: heap unexpectedly large\n");
+                       ret = 1;
+                       break;
+               }
+       }
+       verbose_printf("malloc-ed %ld bytes from heap\n", consumed);
+       return ret;
+}
+
 /*
  * With the inclusion of MAP_HUGETLB it is now possible to have huge pages
  * without using hugetlbfs, so not all huge page regions will show with the
-- 
1.7.1


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Libhugetlbfs-devel mailing list
Libhugetlbfs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to