Debian Codegraph @

tests/fallocate_stress.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 20015 Mike Kravetz, Oracle 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 <stdio.h>
22 #include <linux/falloc.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <sys/mman.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29 #include <linux/falloc.h>
30
31 #include <hugetlbfs.h>
32
33 #include "hugetests.h"
34
35 #define P "fallocate-stress"
36 #define DESC \
37 "* Stress test fallocate. This test starts three threads. Thread *\n"\
38 "* one will continually punch/fill holes via falloc. Thread two *\n"\
39 "* will continually fault in those same pages. Thread three will *\n"\
40 "* continually mmap/munmap that page range. *"
41
42 #define min(a,b) (((a) < (b)) ? (a) : (b))
43
44 #define MAX_PAGES_TO_USE 100
45
46 static int htlb_fd;
47 static long max_hpages;
48 static long hpage_size;
49
50 #define FALLOCATE_ITERATIONS 100000
51 static void *thread_fallocate(void *arg)
52 {
53 int i, err;
54 long tpage;
55
56 for (i=0; i < FALLOCATE_ITERATIONS; i++) {
57 tpage = ((long long)random()) % (max_hpages);
58 err = fallocate(htlb_fd,
59 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
60 tpage * hpage_size, hpage_size);
61 if (err)
62 FAIL("fallocate(): %s", strerror(errno));
63 err = fallocate(htlb_fd, 0, tpage * hpage_size, hpage_size);
64 if (err)
65 FAIL("fallocate(FALLOC_FL_PUNCH_HOLE): %s",
66 strerror(errno));
67 }
68
69 return NULL;
70 }
71
72 static void *fault_mmap_addr = NULL;
73
74 static void thread_fault_cleanup(void *arg)
75 {
76 if (fault_mmap_addr)
77 munmap(fault_mmap_addr, max_hpages * hpage_size);
78 }
79
80 static void *thread_fault(void *arg)
81 {
82 long tpage;
83 char foo;
84 struct timespec ts;
85
86 fault_mmap_addr = mmap(NULL, max_hpages * hpage_size,
87 PROT_READ | PROT_WRITE, MAP_SHARED,
88 htlb_fd, 0);
89 if (fault_mmap_addr == MAP_FAILED)
90 FAIL("mmap(): %s", strerror(errno));
91
92 pthread_cleanup_push(thread_fault_cleanup, NULL);
93
94 ts.tv_sec = 0;
95 ts.tv_nsec = 0;
96
97 while (1) {
98 tpage = ((long long)random()) % (max_hpages);
99
100 foo = *((char *)(fault_mmap_addr + (tpage * hpage_size)));
101 *((char *)(fault_mmap_addr + (tpage * hpage_size))) = foo;
102
103 nanosleep(&ts, NULL); /* thread cancellation point */
104 }
105
106 pthread_cleanup_pop(1);
107
108 return NULL;
109 }
110
111 static void *mmap_munmap_addr = NULL;
112
113 static void thread_mmap_munmap_cleanup(void *arg)
114 {
115 if (mmap_munmap_addr)
116 munmap(mmap_munmap_addr, max_hpages * hpage_size);
117 }
118
119 static void *thread_mmap_munmap(void *arg)
120 {
121 int err;
122 struct timespec ts;
123
124 pthread_cleanup_push(thread_mmap_munmap_cleanup, NULL);
125
126 ts.tv_sec = 0;
127 ts.tv_nsec = 0;
128
129 while (1) {
130 mmap_munmap_addr = mmap(NULL, max_hpages * hpage_size,
131 PROT_READ | PROT_WRITE,
132 MAP_SHARED, htlb_fd, 0);
133 if (mmap_munmap_addr == MAP_FAILED)
134 FAIL("mmap(): %s", strerror(errno));
135
136 err = munmap(mmap_munmap_addr, max_hpages * hpage_size);
137 if (err)
138 FAIL("munmap(): %s", strerror(errno));
139 mmap_munmap_addr = NULL;
140
141 nanosleep(&ts, NULL); /* thread cancellation point */
142 }
143
144 pthread_cleanup_pop(1);
145
146 return NULL;
147 }
148
149 int main(int argc, char *argv[])
150 {
151 long nr_hpages_free;
152 int err;
153 unsigned long free_before, free_after;
154 unsigned long rsvd_before, rsvd_after;
155 pthread_t falloc_th, fault_th, mmap_munmap_th;
156 void *falloc_th_ret, *fault_th_ret, *mmap_munmap_th_ret;
157
158 test_init(argc, argv);
159
160 srandom((int)getpid() * time(NULL));
161 hpage_size = check_hugepagesize();
162 nr_hpages_free = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
163 max_hpages = min(nr_hpages_free, MAX_PAGES_TO_USE);
164
165 htlb_fd = hugetlbfs_unlinked_fd();
166 if (htlb_fd < 0)
167 FAIL("hugetlbfs_unlinked_fd()");
168
169 free_before = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
170 rsvd_before = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
171
172 /* First preallocate file with max_hpages pages */
173 err = fallocate(htlb_fd, 0, 0, hpage_size * max_hpages);
174 if (err) {
175 if (errno == EOPNOTSUPP)
176 IRRELEVANT();
177 if (err)
178 FAIL("fallocate(): %s", strerror(errno));
179 }
180
181 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
182 if (free_before - free_after != max_hpages)
183 FAIL("fallocate did not preallocate %ld huge pages\n",
184 max_hpages);
185
186 err = pthread_create(&falloc_th, NULL, thread_fallocate, NULL);
187 if (err != 0)
188 FAIL("pthread_create(): %s\n", strerror(errno));
189 err = pthread_create(&fault_th, NULL, thread_fault, NULL);
190 if (err != 0)
191 FAIL("pthread_create(): %s\n", strerror(errno));
192 err = pthread_create(&mmap_munmap_th, NULL, thread_mmap_munmap, NULL);
193 if (err != 0)
194 FAIL("pthread_create(): %s\n", strerror(errno));
195
196 err = pthread_join(falloc_th, &falloc_th_ret);
197 if (err != 0)
198 FAIL("pthread_join(): %s\n", strerror(errno));
199 if (falloc_th_ret)
200 FAIL("thread_fallocate unexpected exit code\n");
201
202 err = pthread_cancel(fault_th);
203 if (err != 0)
204 FAIL("pthread_cancel(): %s\n", strerror(errno));
205 err = pthread_join(fault_th, &fault_th_ret);
206 if (err != 0)
207 FAIL("pthread_join(): %s\n", strerror(errno));
208
209 err = pthread_cancel(mmap_munmap_th);
210 if (err != 0)
211 FAIL("pthread_cancel(): %s\n", strerror(errno));
212 err = pthread_join(mmap_munmap_th, &mmap_munmap_th_ret);
213 if (err != 0)
214 FAIL("pthread_join(): %s\n", strerror(errno));
215
216 if (close(htlb_fd))
217 FAIL("close(): %s", strerror(errno));
218
219 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
220 rsvd_after = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
221 if (free_after != free_before || rsvd_after != rsvd_before)
222 FAIL("free or reserve counts not correct after fallocate stress testing\n");
223
224 PASS();
225 }