| 1 | /* |
| 2 | * libhugetlbfs - Easy use of Linux hugepages |
| 3 | * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public License |
| 7 | * as published by the Free Software Foundation; either version 2.1 of |
| 8 | * the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <sys/mman.h> |
| 25 | |
| 26 | #include "hugetests.h" |
| 27 | |
| 28 | /* |
| 29 | * We cannot test mapping size against huge page size because we are not linked |
| 30 | * against libhugetlbfs so gethugepagesize() won't work. So instead we define |
| 31 | * our MIN_PAGE_SIZE as 64 kB (the largest base page available) and make sure |
| 32 | * the mapping page size is larger than this. |
| 33 | */ |
| 34 | #define MIN_PAGE_SIZE 65536 |
| 35 | |
| 36 | #define ALLOC_SIZE (128) |
| 37 | #define NUM_ALLOCS (262144) |
| 38 | |
| 39 | int main(int argc, char *argv[]) |
| 40 | { |
| 41 | int i; |
| 42 | char *env; |
| 43 | char *p; |
| 44 | int expect_hugepage = 0; |
| 45 | |
| 46 | test_init(argc, argv); |
| 47 | |
| 48 | env = getenv("HUGETLB_MORECORE"); |
| 49 | verbose_printf("HUGETLB_MORECORE=%s\n", env); |
| 50 | if (env) |
| 51 | expect_hugepage = 1; |
| 52 | |
| 53 | for (i = 0; i < NUM_ALLOCS; i++) { |
| 54 | p = malloc(ALLOC_SIZE); |
| 55 | if (! p) |
| 56 | FAIL("malloc()"); |
| 57 | |
| 58 | if (i < 16) |
| 59 | verbose_printf("p = %p\n", p); |
| 60 | |
| 61 | memset(p, 0, ALLOC_SIZE); |
| 62 | |
| 63 | if ((i % 157) == 0) { |
| 64 | /* With this many allocs, testing every one |
| 65 | * takes forever */ |
| 66 | unsigned long long mapping_size = |
| 67 | get_mapping_page_size(p); |
| 68 | if (expect_hugepage && (mapping_size <= MIN_PAGE_SIZE)) |
| 69 | FAIL("Address is not hugepage"); |
| 70 | if (!expect_hugepage && (mapping_size > MIN_PAGE_SIZE)) |
| 71 | FAIL("Address is unexpectedly huge"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | PASS(); |
| 76 | } |