Home | History | Annotate | Download | only in fsync
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * NAME
     22  *	fsync02.c
     23  *
     24  * DESCRIPTION
     25  *	Create a sparse file, fsync it, and time the fsync
     26  *
     27  * ALGORITHM
     28  *	1. Create a file.
     29  *	2. Write to the file at equally spaced intervals up to a max block
     30  *	3. Check if the time limit was exceeded.
     31  *
     32  * USAGE:  <for command-line>
     33  *  fsync02 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
     34  *     where,  -c n : Run n copies concurrently.
     35  *             -f   : Turn off functionality Testing.
     36  *             -i n : Execute test n times.
     37  *             -I x : Execute test for x seconds.
     38  *             -P x : Pause for x seconds between iterations.
     39  *             -t   : Turn on syscall timing.
     40  *
     41  * HISTORY
     42  *	07/2001 Ported by Wayne Boyer
     43  *
     44  * RESTRICTIONS
     45  *	None
     46  */
     47 
     48 #include <stdio.h>
     49 #include <unistd.h>
     50 #include <sys/types.h>
     51 #include <sys/statvfs.h>
     52 #include <fcntl.h>
     53 #include <errno.h>
     54 #include <sys/resource.h>
     55 #include "test.h"
     56 #include "safe_macros.h"
     57 #include <time.h>
     58 
     59 #define BLOCKSIZE 8192
     60 #define MAXBLKS 262144
     61 #define TIME_LIMIT 120
     62 
     63 char *TCID = "fsync02";
     64 int TST_TOTAL = 1;
     65 
     66 void setup(void);
     67 void cleanup(void);
     68 
     69 char tempfile[40] = "";
     70 int fd, pid;
     71 off_t max_blks = MAXBLKS;
     72 
     73 struct statvfs stat_buf;
     74 
     75 int main(int ac, char **av)
     76 {
     77 	int lc;
     78 
     79 	off_t offsetret, offset;
     80 	char pbuf[BUFSIZ];
     81 	int ret, max_block = 0;
     82 	int i;
     83 	time_t time_start, time_end;
     84 	double time_delta;
     85 	int data_blocks = 0;
     86 	long int random_number;
     87 
     88 	tst_parse_opts(ac, av, NULL, NULL);
     89 
     90 	setup();
     91 
     92 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     93 
     94 		tst_count = 0;
     95 
     96 		while (max_block <= data_blocks) {
     97 			random_number = random();
     98 			max_block = random_number % max_blks;
     99 			data_blocks = random_number % 1000 + 1;
    100 		}
    101 
    102 		for (i = 1; i <= data_blocks; i++) {
    103 			offset = i * ((BLOCKSIZE * max_block) / data_blocks);
    104 			offset -= BUFSIZ;
    105 			if ((offsetret = lseek(fd, offset, SEEK_SET)) != offset)
    106 				tst_brkm(TBROK | TERRNO, cleanup,
    107 					 "lseek failed: %ld, %ld", offsetret,
    108 					 offset);
    109 			if ((ret = write(fd, pbuf, BUFSIZ)) != BUFSIZ)
    110 				tst_brkm(TBROK, cleanup, "write failed");
    111 		}
    112 		if (time(&time_start) == -1)
    113 			tst_brkm(TBROK | TERRNO, cleanup,
    114 				 "getting start time failed");
    115 
    116 		TEST(fsync(fd));
    117 
    118 		if (time(&time_end) == -1)
    119 			tst_brkm(TBROK | TERRNO, cleanup,
    120 				 "getting end time failed");
    121 
    122 		if (TEST_RETURN == -1) {
    123 			tst_resm(TFAIL | TTERRNO, "fsync failed");
    124 			continue;
    125 		}
    126 
    127 		if (time_end < time_start)
    128 			tst_resm(TBROK,
    129 				 "timer broken end %ld < start %ld",
    130 				 time_end, time_start);
    131 
    132 		if ((time_delta =
    133 		     difftime(time_end, time_start)) > TIME_LIMIT)
    134 			tst_resm(TFAIL,
    135 				 "fsync took too long: %lf seconds; "
    136 				 "max_block: %d; data_blocks: %d",
    137 				 time_delta, max_block, data_blocks);
    138 		else
    139 			tst_resm(TPASS, "fsync succeeded in an "
    140 				 "acceptable amount of time");
    141 
    142 		SAFE_FTRUNCATE(cleanup, fd, 0);
    143 	}
    144 
    145 	sync();
    146 	cleanup();
    147 	tst_exit();
    148 }
    149 
    150 /*
    151  * setup() - performs all ONE TIME setup for this test.
    152  */
    153 void setup(void)
    154 {
    155 	/* free blocks avail to non-superuser */
    156 	unsigned long f_bavail;
    157 
    158 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    159 
    160 	TEST_PAUSE;
    161 
    162 	/* make a temporary directory and cd to it */
    163 	tst_tmpdir();
    164 
    165 	sprintf(tempfile, "%s.%d", TCID, pid = getpid());
    166 	srand48(pid);
    167 
    168 	if ((fd = open(tempfile, O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1)
    169 		tst_brkm(TBROK, cleanup, "open failed");
    170 
    171 	if (fstatvfs(fd, &stat_buf) != 0)
    172 		tst_brkm(TBROK, cleanup, "fstatvfs failed");
    173 
    174 	f_bavail = (stat_buf.f_bavail * stat_buf.f_frsize) / BLOCKSIZE;
    175 	if (f_bavail && (f_bavail < MAXBLKS))
    176 		max_blks = f_bavail;
    177 
    178 #ifdef LARGEFILE
    179 	if ((fcntl(fd, F_SETFL, O_LARGEFILE)) == -1)
    180 		tst_brkm(TBROK | TERRNO, cleanup,
    181 			 "fcntl(.., O_LARGEFILE) failed");
    182 
    183 	if (write(fd, pbuf, BUFSIZ) != BUFSIZ)
    184 		tst_brkm(TBROK | TERRNO, cleanup, "write(fd, pbuf, ..) failed");
    185 #endif
    186 }
    187 
    188 void cleanup(void)
    189 {
    190 	if (close(fd) == -1)
    191 		tst_resm(TWARN | TERRNO, "close failed");
    192 
    193 	tst_rmdir();
    194 
    195 }
    196