Debian Codegraph @

tests/mmap-gettest.c

raw symbols SCIP
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 #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 /* Global test configuration */
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 /* Map the files with MAP_PRIVATE */
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 /* Make sure the page is zeroed */
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 /* Fill each file with a pattern */
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 /* Verify the pattern */
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 /* Munmap the area */
98 munmap(ma, size);
99
100 /* Close and delete the file */
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 }