| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <sys/types.h> |
| 20 |
#include <sys/shm.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/mman.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <hugetlbfs.h> |
| 31 |
#include "hugetests.h" |
| 32 |
|
| 33 |
#define P "mmap-gettest" |
| 34 |
#define DESC \ |
| 35 |
"* This baseline test validates that a mapping of a certain size *\n"\ |
| 36 |
"* can be created, correctly. Once created, all the pages are *\n"\ |
| 37 |
"* filled with a pattern and rechecked to test for corruption. *\n"\ |
| 38 |
"* The mapping is then released. This process is repeated for a *\n"\ |
| 39 |
"* specified number of iterations. *" |
| 40 |
|
| 41 |
extern int errno; |
| 42 |
|
| 43 |
#define BUF_SZ 256 |
| 44 |
|
| 45 |
|
| 46 |
#define HTLB_FILE "mmap-gettest" |
| 47 |
static char hugetlb_mount[BUF_SZ]; |
| 48 |
static unsigned int iter; |
| 49 |
static int nr_hugepages; |
| 50 |
static long hpage_size; |
| 51 |
|
| 52 |
static int do_one(char *mountpoint, size_t size) { |
| 53 |
char *ma; |
| 54 |
int fha; |
| 55 |
size_t i,j; |
| 56 |
char pattern = 'A'; |
| 57 |
|
| 58 |
fha = hugetlbfs_unlinked_fd(); |
| 59 |
if (fha < 0) |
| 60 |
CONFIG("Unable to open temp file in hugetlbfs (%s)", |
| 61 |
strerror(errno)); |
| 62 |
|
| 63 |
|
| 64 |
ma = mmap(NULL, size, (PROT_READ|PROT_WRITE), MAP_SHARED, fha, 0); |
| 65 |
if (ma == MAP_FAILED) |
| 66 |
FAIL("Failed to mmap the hugetlb file: %s", strerror(errno)); |
| 67 |
|
| 68 |
|
| 69 |
for (i = 0; i < nr_hugepages; i++) { |
| 70 |
verbose_printf("Verifying %p\n", (ma+(i*hpage_size))); |
| 71 |
for (j = 0; j < hpage_size; j++) { |
| 72 |
if (*(ma+(i*hpage_size)+j) != 0) |
| 73 |
FAIL("Verifying the mmap area failed. " |
| 74 |
"Got %c, expected 0", |
| 75 |
*(ma+(i*hpage_size)+j)); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
for (i = 0; i < nr_hugepages; i++) { |
| 80 |
pattern = 65+(i%26); |
| 81 |
verbose_printf("Touching %p with %c\n", ma+(i*hpage_size),pattern); |
| 82 |
memset(ma+(i*hpage_size), pattern, hpage_size); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
for (i = 0; i < nr_hugepages; i++) { |
| 87 |
pattern = 65+(i%26); |
| 88 |
verbose_printf("Verifying %p\n", (ma+(i*hpage_size))); |
| 89 |
for (j = 0; j < hpage_size; j++) { |
| 90 |
if (*(ma+(i*hpage_size)+j) != pattern) |
| 91 |
FAIL("Verifying the mmap area failed. " |
| 92 |
"Got %c, expected %c", |
| 93 |
*(ma+(i*hpage_size)+j),pattern); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
munmap(ma, size); |
| 99 |
|
| 100 |
|
| 101 |
close(fha); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
int main(int argc, char ** argv) |
| 106 |
{ |
| 107 |
size_t size; |
| 108 |
int i; |
| 109 |
|
| 110 |
test_init(argc, argv); |
| 111 |
|
| 112 |
if (argc < 3) |
| 113 |
CONFIG("Usage: %s <# iterations> <# pages>\n", argv[0]); |
| 114 |
|
| 115 |
iter = atoi(argv[1]); |
| 116 |
nr_hugepages = atoi(argv[2]); |
| 117 |
|
| 118 |
hpage_size = check_hugepagesize(); |
| 119 |
size = nr_hugepages * hpage_size; |
| 120 |
|
| 121 |
for (i=0; i < iter; i++) { |
| 122 |
verbose_printf("Iteration %d\n", i); |
| 123 |
do_one(hugetlb_mount, size); |
| 124 |
} |
| 125 |
|
| 126 |
PASS(); |
| 127 |
} |