Debian Codegraph @

tests/mmap-cow.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 #define _GNU_SOURCE
20
21 #include <sys/types.h>
22 #include <sys/shm.h>
23 #include <sys/wait.h>
24 #include <sys/mman.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <hugetlbfs.h>
34 #include "hugetests.h"
35
36 extern int errno;
37
38 #define P "mmap-cow"
39 #define DESC \
40 "* Tests copy-on-write semantics of large pages where a number *\n"\
41 "* of threads map the same file with the MAP_PRIVATE flag. The *\n"\
42 "* threads then write into their copy of the mapping and recheck *\n"\
43 "* the contents to ensure they were not corrupted by the other *\n"\
44 "* threads. *"\
45
46 #define HTLB_FILE "mmap-cow"
47 #define BUF_SZ 256
48
49 #define CHILD_FAIL(thread, fmt, ...) \
50 do { \
51 verbose_printf("Thread %d (pid=%d) FAIL: " fmt, \
52 thread, getpid(), __VA_ARGS__); \
53 exit(1); \
54 } while (0)
55
56 /* Setup Configuration */
57 static int nr_hugepages; /* Number of huge pages to allocate */
58 static unsigned int threads; /* Number of threads to run */
59
60 static int mmap_file(int fd, char **addr, size_t size, int type)
61 {
62 int flags = 0;
63
64 *addr = mmap(NULL, size, PROT_READ|PROT_WRITE, flags | type, fd, 0);
65 if (*addr == MAP_FAILED)
66 return -1;
67
68 return 0;
69 }
70
71 static void do_work(int thread, size_t size, int fd)
72 {
73 char *addr;
74 size_t i;
75 char pattern = thread+65;
76
77 if (mmap_file(fd, &addr, size, MAP_PRIVATE))
78 CHILD_FAIL(thread, "mmap() failed: %s", strerror(errno));
79
80 verbose_printf("Thread %d (pid=%d): Mapped at address %p\n",
81 thread, getpid(), addr);
82
83 /* Write to the mapping with a distinct pattern */
84 verbose_printf("Thread %d (pid=%d): Writing %c to the mapping\n",
85 thread, getpid(), pattern);
86 for (i = 0; i < size; i++)
87 memcpy((char *)addr+i, &pattern, 1);
88
89 if (msync(addr, size, MS_SYNC))
90 CHILD_FAIL(thread, "msync() failed: %s", strerror(errno));
91
92 /* Verify the pattern */
93 for (i = 0; i < size; i++)
94 if (addr[i] != pattern)
95 CHILD_FAIL(thread, "Corruption at %p; "
96 "Got %c, Expected %c",
97 &addr[i], addr[i], pattern);
98
99 verbose_printf("Thread %d (pid=%d): Pattern verified\n",
100 thread, getpid());
101
102 /* Munmap the area */
103 munmap(addr, size);
104 close(fd);
105 exit(0);
106 }
107
108 int main(int argc, char ** argv)
109 {
110 char *addr;
111 long hpage_size;
112 size_t size;
113 int i, pid, status, fd, ret;
114 pid_t *wait_list;
115
116 test_init(argc, argv);
117
118 if (argc < 3)
119 CONFIG("Usage: mmap-cow <# threads> <# pages>\n");
120
121 nr_hugepages = atoi(argv[2]);
122 threads = atoi(argv[1]);
123
124 if ((threads+1) > nr_hugepages)
125 CONFIG("Need more hugepages than threads\n");
126
127 wait_list = malloc(threads * sizeof(pid_t));
128 if (wait_list == NULL)
129 CONFIG("Couldn't allocate memory for wait_list\n");
130
131 hpage_size = check_hugepagesize();
132 /* Have to have enough available hugepages for each thread to
133 * get its own copy, plus one for the parent/page-cache */
134 size = (nr_hugepages / (threads+1)) * hpage_size;
135 verbose_printf("hpage_size is %lx, Size is %zu, threads: %u\n",
136 hpage_size, size, threads);
137
138 /* First, open the file */
139 fd = hugetlbfs_unlinked_fd();
140 if (fd < 0)
141 CONFIG("hugetlbfs_unlinked_fd() failed: %s\n",
142 strerror(errno));
143
144 /* First, mmap the file with MAP_SHARED and fill with data
145 * If this is not done, then the fault handler will not be
146 * called in the kernel since private mappings will be
147 * created for the children at prefault time.
148 */
149 if (mmap_file(fd, &addr, size, MAP_SHARED))
150 FAIL("Failed to create shared mapping: %s", strerror(errno));
151
152 for (i = 0; i < size; i += 8) {
153 memcpy(addr+i, "deadbeef", 8);
154 }
155
156 for (i=0; i<threads; i++) {
157 if ((pid = fork()) < 0)
158 FAIL("fork: %s", strerror(errno));
159
160 if (pid == 0)
161 do_work(i, size, fd);
162
163 wait_list[i] = pid;
164 }
165 for (i=0; i<threads; i++) {
166 ret = waitpid(wait_list[i], &status, 0);
167 if (ret < 0)
168 FAIL("waitpid(): %s", strerror(errno));
169 if (WEXITSTATUS(status) != 0)
170 FAIL("Thread %d (pid=%d) failed", i, wait_list[i]);
171
172 if (WIFSIGNALED(status))
173 FAIL("Thread %d (pid=%d) received unhandled signal", i,
174 wait_list[i]);
175 }
176
177 munmap(addr, size);
178 close(fd);
179 free(wait_list);
180
181 PASS();
182 }