blob: aad5f7fa04c98ecde3f72224516251720e49e4a5 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080030 * 3. Check if the time limit was exceeded.
plars865695b2001-08-27 22:15:12 +000031 *
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>
Garrett Coopere8530df2010-12-21 11:37:57 -080055#include "test.h"
plarsd31c3452002-05-21 12:59:48 +000056#include <time.h>
plars865695b2001-08-27 22:15:12 +000057
58#define BLOCKSIZE 8192
59#define MAXBLKS 262144
60#define TIME_LIMIT 120
61
62char *TCID = "fsync02";
63int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000064
65void setup(void);
66void cleanup(void);
67
68char tempfile[40] = "";
69int fd, pid;
70off_t max_blks = MAXBLKS;
71
72struct statvfs stat_buf;
73
plars74948ad2002-11-14 16:16:14 +000074int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000075{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020076 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020077 const char *msg;
plars865695b2001-08-27 22:15:12 +000078
79 off_t offsetret, offset;
80 char pbuf[BUFSIZ];
subrata_modak56207ce2009-03-23 13:35:39 +000081 int ret, max_block = 0;
plars865695b2001-08-27 22:15:12 +000082 int i;
plarsd31c3452002-05-21 12:59:48 +000083 time_t time_start, time_end;
84 double time_delta;
subrata_modak56207ce2009-03-23 13:35:39 +000085 int data_blocks = 0;
robbiew7a11dc92003-12-03 15:54:23 +000086 long int random_number;
subrata_modakbdbaec52009-02-26 12:14:51 +000087
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080088 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080089 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000090
91 setup();
92
plars865695b2001-08-27 22:15:12 +000093 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080094
Caspar Zhangd59a6592013-03-07 14:59:12 +080095 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000096
robbiew7a11dc92003-12-03 15:54:23 +000097 while (max_block <= data_blocks) {
98 random_number = random();
99 max_block = random_number % max_blks;
100 data_blocks = random_number % 1000 + 1;
101 }
plars865695b2001-08-27 22:15:12 +0000102
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800103 for (i = 1; i <= data_blocks; i++) {
Garrett Cooper1a1478a2011-01-19 00:42:31 -0800104 offset = i * ((BLOCKSIZE * max_block) / data_blocks);
105 offset -= BUFSIZ;
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800106 if ((offsetret = lseek(fd, offset, SEEK_SET)) != offset)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 tst_brkm(TBROK | TERRNO, cleanup,
108 "lseek failed: %ld, %ld", offsetret,
109 offset);
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800110 if ((ret = write(fd, pbuf, BUFSIZ)) != BUFSIZ)
111 tst_brkm(TBROK, cleanup, "write failed");
plars865695b2001-08-27 22:15:12 +0000112 }
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800113 if (time(&time_start) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114 tst_brkm(TBROK | TERRNO, cleanup,
115 "getting start time failed");
plars865695b2001-08-27 22:15:12 +0000116
117 TEST(fsync(fd));
118
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800119 if (time(&time_end) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 tst_brkm(TBROK | TERRNO, cleanup,
121 "getting end time failed");
plars865695b2001-08-27 22:15:12 +0000122
123 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 tst_resm(TFAIL | TTERRNO, "fsync failed");
plars865695b2001-08-27 22:15:12 +0000125 continue;
126 }
127
Cyril Hrubise38b9612014-06-02 17:20:57 +0200128 if (time_end < time_start)
129 tst_resm(TBROK,
130 "timer broken end %ld < start %ld",
131 time_end, time_start);
subrata_modakbdbaec52009-02-26 12:14:51 +0000132
Cyril Hrubise38b9612014-06-02 17:20:57 +0200133 if ((time_delta =
134 difftime(time_end, time_start)) > TIME_LIMIT)
135 tst_resm(TFAIL,
136 "fsync took too long: %lf seconds; "
137 "max_block: %d; data_blocks: %d",
138 time_delta, max_block, data_blocks);
139 else
140 tst_resm(TPASS, "fsync succeeded in an "
141 "acceptable amount of time");
plars865695b2001-08-27 22:15:12 +0000142
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800143 if (ftruncate(fd, 0) == -1)
plars865695b2001-08-27 22:15:12 +0000144 tst_brkm(TBROK, cleanup, "ftruncate failed");
plars865695b2001-08-27 22:15:12 +0000145 }
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800146
plars865695b2001-08-27 22:15:12 +0000147 sync();
148 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800149 tst_exit();
plars865695b2001-08-27 22:15:12 +0000150}
151
152/*
153 * setup() - performs all ONE TIME setup for this test.
154 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400155void setup(void)
plars865695b2001-08-27 22:15:12 +0000156{
subrata_modakc0a902d2009-04-25 17:47:55 +0000157 /* free blocks avail to non-superuser */
158 unsigned long f_bavail;
159
plars865695b2001-08-27 22:15:12 +0000160 tst_sig(NOFORK, DEF_HANDLER, cleanup);
161
plars865695b2001-08-27 22:15:12 +0000162 TEST_PAUSE;
163
164 /* make a temporary directory and cd to it */
165 tst_tmpdir();
166
167 sprintf(tempfile, "%s.%d", TCID, pid = getpid());
168 srand48(pid);
169
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800170 if ((fd = open(tempfile, O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1)
171 tst_brkm(TBROK, cleanup, "open failed");
plars865695b2001-08-27 22:15:12 +0000172
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800173 if (fstatvfs(fd, &stat_buf) != 0)
174 tst_brkm(TBROK, cleanup, "fstatvfs failed");
plars865695b2001-08-27 22:15:12 +0000175
Stanislav Kholmanskikhe533f082013-11-06 14:32:03 +0400176 f_bavail = (stat_buf.f_bavail * stat_buf.f_frsize) / BLOCKSIZE;
subrata_modakc0a902d2009-04-25 17:47:55 +0000177 if (f_bavail && (f_bavail < MAXBLKS))
178 max_blks = f_bavail;
179
plars865695b2001-08-27 22:15:12 +0000180#ifdef LARGEFILE
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800181 if ((fcntl(fd, F_SETFL, O_LARGEFILE)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 tst_brkm(TBROK | TERRNO, cleanup,
183 "fcntl(.., O_LARGEFILE) failed");
plars865695b2001-08-27 22:15:12 +0000184
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800185 if (write(fd, pbuf, BUFSIZ) != BUFSIZ)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800186 tst_brkm(TBROK | TERRNO, cleanup, "write(fd, pbuf, ..) failed");
plars865695b2001-08-27 22:15:12 +0000187#endif
188}
189
Mike Frysingerc57fba52014-04-09 18:56:30 -0400190void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000191{
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800192 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800193 tst_resm(TWARN | TERRNO, "close failed");
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800194
plars865695b2001-08-27 22:15:12 +0000195 tst_rmdir();
196
Garrett Cooper1a1478a2011-01-19 00:42:31 -0800197}