Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/mmap_sync.c
48529 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122#include <stdlib.h>23#include <stdio.h>24#include <string.h>25#include <sys/mman.h>26#include <sys/stat.h>27#include <sys/time.h>28#include <fcntl.h>29#include <unistd.h>30#include <time.h>3132static void33cleanup(char *file)34{35(void) remove(file);36}3738int39main(int argc, char *argv[])40{41char *testdir = getenv("TESTDIR");42if (!testdir) {43fprintf(stderr, "environment variable TESTDIR not set\n");44return (1);45}4647struct stat st;48umask(0);49if (stat(testdir, &st) != 0 &&50mkdir(testdir, 0777) != 0) {51perror("mkdir");52return (1);53}5455if (argc > 3) {56fprintf(stderr, "usage: %s "57"[run time in mins] "58"[max msync time in ms]\n", argv[0]);59return (1);60}6162int run_time_mins = 1;63if (argc >= 2) {64run_time_mins = atoi(argv[1]);65}6667int max_msync_time_ms = 2000;68if (argc >= 3) {69max_msync_time_ms = atoi(argv[2]);70}7172char filepath[512];73filepath[0] = '\0';74char *file = &filepath[0];7576(void) snprintf(file, 512, "%s/msync_file", testdir);7778const int LEN = 8;79cleanup(file);8081int fd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR |82S_IRGRP | S_IROTH);8384if (fd == -1) {85(void) fprintf(stderr, "%s: %s: ", argv[0], file);86perror("open");87return (1);88}8990if (ftruncate(fd, LEN) != 0) {91perror("ftruncate");92cleanup(file);93return (1);94}9596void *ptr = mmap(NULL, LEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);9798if (ptr == MAP_FAILED) {99perror("mmap");100cleanup(file);101return (1);102}103104struct timeval tstart;105gettimeofday(&tstart, NULL);106107long long x = 0LL;108109for (;;) {110*((long long *)ptr) = x;111x++;112113struct timeval t1, t2;114gettimeofday(&t1, NULL);115if (msync(ptr, LEN, MS_SYNC|MS_INVALIDATE) != 0) {116perror("msync");117cleanup(file);118return (1);119}120121gettimeofday(&t2, NULL);122123double elapsed = (t2.tv_sec - t1.tv_sec) * 1000.0;124elapsed += ((t2.tv_usec - t1.tv_usec) / 1000.0);125if (elapsed > max_msync_time_ms) {126fprintf(stderr, "slow msync: %f ms\n", elapsed);127if (munmap(ptr, LEN) != 0)128perror("munmap");129cleanup(file);130return (1);131}132133double elapsed_start = (t2.tv_sec - tstart.tv_sec) * 1000.0;134elapsed_start += ((t2.tv_usec - tstart.tv_usec) / 1000.0);135if (elapsed_start > run_time_mins * 60 * 1000) {136break;137}138}139140if (munmap(ptr, LEN) != 0) {141perror("munmap");142cleanup(file);143return (1);144}145146if (close(fd) != 0) {147perror("close");148}149150cleanup(file);151return (0);152}153154155