robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 1 | /* IBM Corporation */ |
| 2 | /* 01/02/2003 Port to LTP avenkat@us.ibm.com */ |
| 3 | /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ |
| 4 | |
| 5 | /* |
| 6 | * Copyright (c) International Business Machines Corp., 2003 |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 16 | * the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 23 | /* |
| 24 | * weave: |
| 25 | * Mmap parts of a file, and then write to the file causing single |
| 26 | * write requests to jump back and forth between mmaped io and regular io. |
| 27 | * |
| 28 | * Usage: weave filename startoffset. pageoffset = 4096. |
| 29 | * |
| 30 | * startoffset specifies a byte count in the file at which to begin |
| 31 | * the test. When this value is non-zero, a sparse file is created. |
| 32 | * This is useful for testing with large files. |
| 33 | * |
| 34 | * Compile with -DLARGE_FILE to enable file sizes > 2 GB. |
| 35 | */ |
| 36 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 37 | #include <sys/types.h> |
| 38 | #include <sys/mman.h> |
| 39 | #include <unistd.h> |
| 40 | #include <errno.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <signal.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | /***** LTP Port *****/ |
| 46 | #include "test.h" |
| 47 | #include "usctest.h" |
| 48 | #define FAILED 0 |
| 49 | #define PASSED 1 |
| 50 | /***** ** ** *****/ |
| 51 | |
| 52 | #define CLEAN (void)close(rofd); \ |
| 53 | (void)close(rwfd); \ |
| 54 | (void)unlink(filename); |
| 55 | #define ERROR(M) (void)fprintf(stderr, "%s: errno = %d; " M "\n", \ |
| 56 | argv[0], errno) |
| 57 | #define CLEANERROR(M) CLEAN; ERROR(M) |
| 58 | #define CATCH_SIG(SIG) \ |
| 59 | if (sigaction(SIG, &sa, 0) == -1) { \ |
| 60 | ERROR("couldn't catch signal " #SIG); \ |
| 61 | exit(1); \ |
| 62 | } |
| 63 | |
| 64 | static char *filename; |
| 65 | |
| 66 | /***** LTP Port *****/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 67 | char *TCID = "mmapstress04"; //weave |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 68 | int local_flag = PASSED; |
| 69 | int block_number; |
| 70 | FILE *temp; |
| 71 | int TST_TOTAL = 1; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 72 | |
| 73 | int anyfail(); |
| 74 | int blenter(); |
| 75 | int blexit(); |
| 76 | int instress(); |
| 77 | void setup(); |
| 78 | void terror(); |
| 79 | void fail_exit(); |
| 80 | void ok_exit(); |
| 81 | /***** ** ** *****/ |
| 82 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | extern time_t time(time_t *); |
| 84 | extern char *ctime(const time_t *); |
| 85 | extern void exit(int); |
| 86 | static int rofd, rwfd; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 87 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | /*ARGSUSED*/ static |
| 89 | void cleanup(int sig) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 90 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | /* |
| 92 | * Don't check error codes - we could be signaled before the file is |
| 93 | * created. |
| 94 | */ |
| 95 | (void)close(rofd); |
| 96 | (void)close(rwfd); |
| 97 | (void)unlink(filename); |
| 98 | exit(1); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 101 | int main(int argc, char *argv[]) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 102 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | char *buf; |
| 104 | size_t pagesize = (size_t) sysconf(_SC_PAGE_SIZE); |
| 105 | caddr_t mmapaddr; |
| 106 | time_t t; |
| 107 | int i, j; |
| 108 | struct sigaction sa; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 109 | #ifdef LARGE_FILE |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | off64_t startoffset; |
| 111 | off64_t seekoff; |
| 112 | off64_t mapoff; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 113 | #else /* LARGE_FILE */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | off_t startoffset; |
| 115 | off_t seekoff; |
| 116 | off_t mapoff; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 117 | #endif /* LARGE_FILE */ |
| 118 | |
subrata_modak | cd17243 | 2008-12-12 10:39:17 +0000 | [diff] [blame] | 119 | if (argc < 2 || argc > 3) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 120 | (void)fprintf(stderr, "Usage: %s filename startoffset\n", |
| 121 | argv[0]); |
| 122 | return 1; |
| 123 | } |
| 124 | filename = argv[1]; |
subrata_modak | cd17243 | 2008-12-12 10:39:17 +0000 | [diff] [blame] | 125 | |
| 126 | if (argc >= 3) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 127 | #ifdef LARGE_FILE |
subrata_modak | cd17243 | 2008-12-12 10:39:17 +0000 | [diff] [blame] | 128 | startoffset = atoll(argv[2]); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 129 | #else /* LARGE_FILE */ |
subrata_modak | cd17243 | 2008-12-12 10:39:17 +0000 | [diff] [blame] | 130 | startoffset = atoi(argv[2]); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 131 | #endif /* LARGE_FILE */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | } else |
subrata_modak | cd17243 | 2008-12-12 10:39:17 +0000 | [diff] [blame] | 133 | startoffset = pagesize; |
| 134 | |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 135 | if (startoffset % pagesize != 0) { |
robbiew | d764fa2 | 2003-04-03 21:59:03 +0000 | [diff] [blame] | 136 | fprintf(stderr, "pagesize=%ld\n", (long)pagesize); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 137 | fprintf(stderr, "startoffset must be a pagesize multiple\n"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 138 | anyfail(); //LTP Port |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 139 | } |
| 140 | (void)time(&t); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | // (void)printf("%s: Started %s", argv[0], ctime(&t)); |
| 142 | if ((buf = sbrk(6 * pagesize)) == (char *)-1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 143 | ERROR("couldn't allocate buf"); |
| 144 | anyfail(); //LTP Port |
| 145 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | if (sbrk(pagesize - ((ulong) sbrk(0) & (pagesize - 1))) == (char *)-1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 147 | ERROR("couldn't round up brk"); |
| 148 | anyfail(); //LTP Port |
| 149 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 150 | if ((mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 151 | ERROR("couldn't find top of brk"); |
| 152 | anyfail(); //LTP Port |
| 153 | } |
| 154 | sa.sa_handler = cleanup; |
| 155 | sa.sa_flags = 0; |
| 156 | if (sigemptyset(&sa.sa_mask)) { |
| 157 | ERROR("sigemptyset failed"); |
| 158 | anyfail(); //LTP Port |
| 159 | } |
| 160 | CATCH_SIG(SIGINT); |
| 161 | CATCH_SIG(SIGQUIT); |
| 162 | CATCH_SIG(SIGTERM); |
| 163 | tst_tmpdir(); |
| 164 | #ifdef LARGE_FILE |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 165 | if ((rofd = open64(filename, O_RDONLY | O_CREAT, 0777)) == -1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 166 | #else /* LARGE_FILE */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 167 | if ((rofd = open(filename, O_RDONLY | O_CREAT, 0777)) == -1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 168 | #endif /* LARGE_FILE */ |
| 169 | ERROR("read only open failed"); |
| 170 | anyfail(); //LTP Port |
| 171 | } |
| 172 | #ifdef LARGE_FILE |
| 173 | if ((rwfd = open64(filename, O_RDWR)) == -1) { |
| 174 | #else /* LARGE_FILE */ |
| 175 | if ((rwfd = open(filename, O_RDWR)) == -1) { |
| 176 | #endif /* LARGE_FILE */ |
| 177 | (void)close(rofd); |
| 178 | (void)unlink(filename); |
| 179 | ERROR("read/write open failed"); |
| 180 | anyfail(); //LTP Port |
| 181 | } |
| 182 | #ifdef LARGE_FILE |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 183 | seekoff = startoffset + (off64_t) 64 *(off64_t) 6 *(off64_t) pagesize; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 184 | if (lseek64(rwfd, seekoff, SEEK_SET) != seekoff) { |
| 185 | #else /* LARGE_FILE */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 186 | seekoff = startoffset + (off_t) 64 *(off_t) 6 *(off_t) pagesize; |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 187 | if (lseek(rwfd, seekoff, SEEK_SET) != seekoff) { |
| 188 | #endif /* LARGE_FILE */ |
| 189 | CLEANERROR("first lseek failed"); |
| 190 | anyfail(); //LTP Port |
| 191 | } |
| 192 | i = 0; |
| 193 | while (i < pagesize && write(rwfd, "b", 1) == 1) |
| 194 | i++; |
| 195 | if (i != pagesize) { |
| 196 | CLEANERROR("write to extend file failed"); |
| 197 | anyfail(); //LTP Port |
| 198 | } |
| 199 | /* The file is now really big, and empty. |
| 200 | * Assuming disk blocks are 8k, and logical pages are 4k, there are |
| 201 | * two maps per page. In order to test mapping at the beginning and |
| 202 | * ends of the block, mapping the whole block, or none of the block |
| 203 | * with different mappings on preceding and following blocks, each |
| 204 | * 3 blocks with 6 pages can be thought of as a binary number from 0 to |
| 205 | * 64 with a bit set for mapped or cleared for unmapped. This number |
| 206 | * is represented by i. The value j is used to look at the bits of i |
| 207 | * and decided to map the page or not. |
| 208 | * NOTE: None of the above assumptions are critical. |
| 209 | */ |
| 210 | for (i = 0; i < 64; i++) { |
| 211 | for (j = 0; j < 6; j++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 212 | if (i & (1 << j)) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 213 | #ifdef LARGE_FILE |
| 214 | mapoff = startoffset + |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 215 | (off64_t) pagesize *(off64_t) (6 + i + j); |
| 216 | if (mmap64(mmapaddr + pagesize * (6 * i + j), |
| 217 | pagesize, PROT_READ, |
| 218 | MAP_FILE | MAP_PRIVATE | MAP_FIXED, |
| 219 | rofd, mapoff) |
| 220 | == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 221 | #else /* LARGE_FILE */ |
| 222 | mapoff = startoffset + |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 223 | (off_t) pagesize *(off_t) (6 + i + j); |
| 224 | if (mmap(mmapaddr + pagesize * (6 * i + j), |
| 225 | pagesize, PROT_READ, |
| 226 | MAP_FILE | MAP_PRIVATE | MAP_FIXED, |
| 227 | rofd, mapoff) |
| 228 | == (caddr_t) - 1) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 229 | #endif /* LARGE_FILE */ |
| 230 | CLEANERROR("mmap failed"); |
| 231 | anyfail(); //LTP Port |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | /* done mapping */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 237 | for (i = 0; i < 6 * pagesize; i++) |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 238 | buf[i] = 'a'; |
| 239 | /* write out 6 pages of stuff into each of the 64 six page sections */ |
| 240 | #ifdef LARGE_FILE |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 241 | if (lseek64(rwfd, startoffset, SEEK_SET) != startoffset) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 242 | #else /* LARGE_FILE */ |
| 243 | if (lseek(rwfd, startoffset, SEEK_SET) != startoffset) { |
| 244 | #endif /* LARGE_FILE */ |
| 245 | CLEANERROR("second lseek failed"); |
| 246 | anyfail(); //LTP Port |
| 247 | } |
| 248 | for (i = 0; i < 64; i++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 249 | if (write(rwfd, buf, 6 * pagesize) != 6 * pagesize) { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 250 | CLEANERROR("write failed"); |
| 251 | anyfail(); //LTP Port |
| 252 | } |
| 253 | } |
| 254 | /* Just finished scribbling all over interwoven mmapped and unmapped |
| 255 | * regions. |
| 256 | */ |
| 257 | for (i = 0; i < 64; i++) { |
| 258 | for (j = 0; j < 6; j++) { |
| 259 | /* if mmaped && not updated */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 260 | if ((i & (1 << j)) |
| 261 | && *(mmapaddr + pagesize * (6 * i + j)) != 'a') { |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 262 | CLEANERROR("'a' missing from mmap"); |
| 263 | (void)fprintf(stderr, "i=%d\nj=%d\n" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 264 | "val=0x%x\n", i, j, |
| 265 | (int)(* |
| 266 | (mmapaddr + |
| 267 | pagesize * (6 * i + j)))); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 268 | anyfail(); //LTP Port |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | /* Just checked to see that each mmapped page at least had an 'a' at |
| 273 | * the beginning. |
| 274 | */ |
| 275 | CLEAN; |
| 276 | (void)time(&t); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 277 | // (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port |
| 278 | (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed\n") : tst_resm(TPASS, "Test passed\n"); //LTP Port |
| 279 | tst_rmdir(); |
| 280 | tst_exit(); //LTP Port |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 281 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 282 | tst_exit(); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /***** LTP Port *****/ |
| 286 | int anyfail() |
| 287 | { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame^] | 288 | tst_brkm(TFAIL, tst_rmdir, "Test failed\n"); |
robbiew | 8445709 | 2003-01-10 17:48:12 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 291 | /***** ** ** *****/ |