[LTP] [PATCH] msync01: Rewrite using new LTP API
Cyril Hrubis
chrubis@suse.cz
Wed Feb 21 15:04:59 CET 2024
Hi!
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - *
> - * Get system page size,
> - * Creat a temporary directory and a file under it used for mapping.
> - * Write 1 page size char data into file.
> - * Initialize paged region (256 bytes) from the specified offset pos.
> - */
> -void setup(void)
> +static void setup(void)
> {
> - size_t c_total = 0, nwrite = 0; /* no. of bytes to be written */
> -
> - tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> - TEST_PAUSE;
> + page_sz = SAFE_SYSCONF(_SC_PAGESIZE);
>
> - page_sz = (size_t)getpagesize();
> + fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
>
> - tst_tmpdir();
> -
> - if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
> - tst_brkm(TBROK | TERRNO, cleanup, "open failed");
> -
> - while (c_total < page_sz) {
> - nwrite = write(fildes, write_buf, sizeof(write_buf));
> - if (nwrite <= 0)
> - tst_brkm(TBROK | TERRNO, cleanup, "write failed");
> - else
> - c_total += nwrite;
> - }
> -
> - /*
> - * Call mmap to map virtual memory (mul. of page size bytes) from the
> - * beginning of temporary file (offset is 0) into memory.
> - */
> - addr = mmap(0, page_sz, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
> - fildes, 0);
> -
> - /* Check for the return value of mmap() */
> - if (addr == MAP_FAILED)
> - tst_brkm(TBROK | TERRNO, cleanup, "mmap failed");
> -
> - /* Set 256 bytes, at 100 byte offset in the mapped region */
> - memset(addr + 100, 1, 256);
> + SAFE_LSEEK(fd, page_sz, SEEK_SET);
Why the LSEEK here? If I'm reading the original code correctly it wrote
page_size worth of bytes not a single byte at page size offset.
Also I guess that we have to rewrite the file on each iteration, i.e.
- write page_size bytes 'a'
- mmap() that region and memset() to 'b'
- msync() that region
- read that back by read() and expect it to be changed to 'b'
Also given the speed of current hardware I would make the size of the
msynced() region to be configurable and set the default to a few
megabytes (8 or 16) at least.
> + SAFE_WRITE(SAFE_WRITE_ALL, fd, "a", 1);
> }
>
> -void cleanup(void)
> +static void cleanup(void)
> {
> - if (munmap(addr, page_sz) == -1)
> - tst_resm(TBROK | TERRNO, "munmap failed");
> -
> - if (close(fildes) == -1)
> - tst_resm(TWARN | TERRNO, "close failed");
> -
> - tst_rmdir();
> + if (fd > 0)
> + SAFE_CLOSE(fd);
> }
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .cleanup = cleanup,
> + .test_all = run,
> + .needs_tmpdir = 1
> +};
> --
> 2.43.0
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list