plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 30 | * 3. Check if the time limit was exceeded. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 55 | #include "test.h" |
plars | d31c345 | 2002-05-21 12:59:48 +0000 | [diff] [blame] | 56 | #include <time.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | #define BLOCKSIZE 8192 |
| 59 | #define MAXBLKS 262144 |
| 60 | #define TIME_LIMIT 120 |
| 61 | |
| 62 | char *TCID = "fsync02"; |
| 63 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | void setup(void); |
| 66 | void cleanup(void); |
| 67 | |
| 68 | char tempfile[40] = ""; |
| 69 | int fd, pid; |
| 70 | off_t max_blks = MAXBLKS; |
| 71 | |
| 72 | struct statvfs stat_buf; |
| 73 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 74 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 76 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 77 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
| 79 | off_t offsetret, offset; |
| 80 | char pbuf[BUFSIZ]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | int ret, max_block = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | int i; |
plars | d31c345 | 2002-05-21 12:59:48 +0000 | [diff] [blame] | 83 | time_t time_start, time_end; |
| 84 | double time_delta; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | int data_blocks = 0; |
robbiew | 7a11dc9 | 2003-12-03 15:54:23 +0000 | [diff] [blame] | 86 | long int random_number; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 87 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 88 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 89 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
| 91 | setup(); |
| 92 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 94 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 95 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
robbiew | 7a11dc9 | 2003-12-03 15:54:23 +0000 | [diff] [blame] | 97 | while (max_block <= data_blocks) { |
| 98 | random_number = random(); |
| 99 | max_block = random_number % max_blks; |
| 100 | data_blocks = random_number % 1000 + 1; |
| 101 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 103 | for (i = 1; i <= data_blocks; i++) { |
Garrett Cooper | 1a1478a | 2011-01-19 00:42:31 -0800 | [diff] [blame] | 104 | offset = i * ((BLOCKSIZE * max_block) / data_blocks); |
| 105 | offset -= BUFSIZ; |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 106 | if ((offsetret = lseek(fd, offset, SEEK_SET)) != offset) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | tst_brkm(TBROK | TERRNO, cleanup, |
| 108 | "lseek failed: %ld, %ld", offsetret, |
| 109 | offset); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 110 | if ((ret = write(fd, pbuf, BUFSIZ)) != BUFSIZ) |
| 111 | tst_brkm(TBROK, cleanup, "write failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | } |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 113 | if (time(&time_start) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | tst_brkm(TBROK | TERRNO, cleanup, |
| 115 | "getting start time failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
| 117 | TEST(fsync(fd)); |
| 118 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 119 | if (time(&time_end) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | tst_brkm(TBROK | TERRNO, cleanup, |
| 121 | "getting end time failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
| 123 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | tst_resm(TFAIL | TTERRNO, "fsync failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | continue; |
| 126 | } |
| 127 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 128 | if (time_end < time_start) |
| 129 | tst_resm(TBROK, |
| 130 | "timer broken end %ld < start %ld", |
| 131 | time_end, time_start); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 132 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 133 | 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"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 143 | if (ftruncate(fd, 0) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | tst_brkm(TBROK, cleanup, "ftruncate failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | } |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 146 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | sync(); |
| 148 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 149 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | * setup() - performs all ONE TIME setup for this test. |
| 154 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 155 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | { |
subrata_modak | c0a902d | 2009-04-25 17:47:55 +0000 | [diff] [blame] | 157 | /* free blocks avail to non-superuser */ |
| 158 | unsigned long f_bavail; |
| 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | 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 Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 170 | if ((fd = open(tempfile, O_RDWR | O_CREAT | O_TRUNC, 0777)) == -1) |
| 171 | tst_brkm(TBROK, cleanup, "open failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 173 | if (fstatvfs(fd, &stat_buf) != 0) |
| 174 | tst_brkm(TBROK, cleanup, "fstatvfs failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | |
Stanislav Kholmanskikh | e533f08 | 2013-11-06 14:32:03 +0400 | [diff] [blame] | 176 | f_bavail = (stat_buf.f_bavail * stat_buf.f_frsize) / BLOCKSIZE; |
subrata_modak | c0a902d | 2009-04-25 17:47:55 +0000 | [diff] [blame] | 177 | if (f_bavail && (f_bavail < MAXBLKS)) |
| 178 | max_blks = f_bavail; |
| 179 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | #ifdef LARGEFILE |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 181 | if ((fcntl(fd, F_SETFL, O_LARGEFILE)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | tst_brkm(TBROK | TERRNO, cleanup, |
| 183 | "fcntl(.., O_LARGEFILE) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 185 | if (write(fd, pbuf, BUFSIZ) != BUFSIZ) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 186 | tst_brkm(TBROK | TERRNO, cleanup, "write(fd, pbuf, ..) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | #endif |
| 188 | } |
| 189 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 190 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | { |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 192 | if (close(fd) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 193 | tst_resm(TWARN | TERRNO, "close failed"); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 194 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | tst_rmdir(); |
| 196 | |
Garrett Cooper | 1a1478a | 2011-01-19 00:42:31 -0800 | [diff] [blame] | 197 | } |