Home | History | Annotate | Download | only in mmapstress
      1 /*
      2  * Copyright (c) 2017 Red Hat, Inc.
      3  * 01/02/2003	Port to LTP avenkat (at) us.ibm.com
      4  * 06/30/2001	Port to Linux	nsharoff (at) us.ibm.com
      5  *
      6  * This program is free software: you can redistribute it and/or modify
      7  * it under the terms of the GNU General Public License as published by
      8  * the Free Software Foundation, either version 3 of the License, or
      9  * (at your option) any later version.
     10  *
     11  * This program is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  * GNU General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU General Public License
     17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     18  */
     19 /*
     20  * Mmap parts of a file, and then write to the file causing single
     21  * write requests to jump back and forth between mmaped io and regular io.
     22  */
     23 
     24 #define _GNU_SOURCE
     25 #include <stdlib.h>
     26 #include "tst_test.h"
     27 #include "tst_safe_macros.h"
     28 
     29 #define NUM_PAGES (192)
     30 #define TEST_FILE "mmapstress04-testfile"
     31 
     32 static int page_size;
     33 static unsigned char *mmap_area;
     34 
     35 static void setup(void)
     36 {
     37 	page_size = getpagesize();
     38 
     39 	/*
     40 	 * Pick large enough area, PROT_NONE doesn't matter,
     41 	 * because we remap it later.
     42 	 */
     43 	mmap_area = SAFE_MMAP(NULL, page_size * NUM_PAGES, PROT_NONE,
     44 		MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
     45 }
     46 
     47 static void write_fully(int fd, void *buf, int len)
     48 {
     49 	int ret;
     50 
     51 	do {
     52 		ret = SAFE_WRITE(0, fd, buf, len);
     53 		buf += ret;
     54 		len -= ret;
     55 	} while (len > 0);
     56 }
     57 
     58 static void mmapstress04(void)
     59 {
     60 	int i, j, rofd, rwfd;
     61 	char *buf;
     62 	int mapped_pages = 0;
     63 
     64 	if (tst_fill_file(TEST_FILE, 'b', page_size, 1))
     65 		tst_brk(TBROK | TERRNO, "fill_file");
     66 
     67 	rofd = SAFE_OPEN(TEST_FILE, O_RDONLY | O_CREAT, 0777);
     68 	/*
     69 	 * Assuming disk blocks are 8k, and logical pages are 4k, there are
     70 	 * two maps per block. In order to test mapping at the beginning and
     71 	 * ends of the block, mapping the whole block, or none of the block
     72 	 * with different mappings on preceding and following blocks, each
     73 	 * 3 blocks with 6 pages can be thought of as a binary number from 0 to
     74 	 * 64 with a bit set for mapped or cleared for unmapped. This number
     75 	 * is represented by i. The value j is used to look at the bits of i
     76 	 * and decided to map the page or not.
     77 	 * NOTE: None of the above assumptions are critical.
     78 	 */
     79 	for (i = 0; i < 64; i++) {
     80 		for (j = 0; j < 6; j++) {
     81 			off_t mapoff;
     82 
     83 			if (!(i & (1 << j)))
     84 				continue;
     85 
     86 			mapoff = page_size * (off_t)(6 * i + j);
     87 			SAFE_MMAP(mmap_area + page_size * mapped_pages++,
     88 				 page_size, PROT_READ,
     89 				 MAP_FILE | MAP_PRIVATE | MAP_FIXED,
     90 				 rofd, mapoff);
     91 		}
     92 	}
     93 	SAFE_CLOSE(rofd);
     94 
     95 	/* write out 6 pages of stuff into each of the 64 six page sections */
     96 	rwfd = SAFE_OPEN(TEST_FILE, O_RDWR);
     97 	buf = SAFE_MALLOC(page_size);
     98 	memset(buf, 'a', page_size);
     99 	for (i = 0; i < 6 * 64; i++)
    100 		write_fully(rwfd, buf, page_size);
    101 	free(buf);
    102 	SAFE_CLOSE(rwfd);
    103 
    104 	/*
    105 	 * Just finished scribbling all over interwoven mmapped and unmapped
    106 	 * regions. Check the data.
    107 	 */
    108 	for (i = 0; i < mapped_pages * page_size; i++) {
    109 		unsigned char val = *(mmap_area + i);
    110 
    111 		if (val != 'a') {
    112 			tst_res(TFAIL, "unexpected value in map, "
    113 				"i=%d,val=0x%x", i, val);
    114 			goto done;
    115 		}
    116 	}
    117 	tst_res(TPASS, "blocks have expected data");
    118 done:
    119 	SAFE_UNLINK(TEST_FILE);
    120 }
    121 
    122 static struct tst_test test = {
    123 	.tid = "mmapstress04",
    124 	.needs_tmpdir = 1,
    125 	.test_all = mmapstress04,
    126 	.setup = setup,
    127 };
    128