forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-fallocate.c
175 lines (157 loc) · 4.71 KB
/
stress-fallocate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (C) 2013-2017 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
void stress_set_fallocate_bytes(const char *opt)
{
off_t fallocate_bytes;
fallocate_bytes = (off_t)get_uint64_byte_filesystem(opt, 1);
check_range_bytes("fallocate-bytes", fallocate_bytes,
MIN_FALLOCATE_BYTES, MAX_FALLOCATE_BYTES);
set_setting("fallocate-bytes", TYPE_ID_OFF_T, &fallocate_bytes);
}
#if (_XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L) && NEED_GLIBC(2,10,0)
static const int modes[] = {
0,
#if defined(FALLOC_FL_KEEP_SIZE)
FALLOC_FL_KEEP_SIZE,
#endif
#if defined(FALLOC_FL_KEEP_SIZE) && defined(FALLOC_FL_PUNCH_HOLE)
FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE,
#endif
#if defined(FALLOC_FL_ZERO_RANGE)
FALLOC_FL_ZERO_RANGE,
#endif
#if defined(FALLOC_FL_COLLAPSE_RANGE)
FALLOC_FL_COLLAPSE_RANGE,
#endif
#if defined(FALLOC_FL_INSERT_RANGE)
FALLOC_FL_INSERT_RANGE,
#endif
};
/*
* stress_fallocate
* stress I/O via fallocate and ftruncate
*/
int stress_fallocate(const args_t *args)
{
int fd, ret;
char filename[PATH_MAX];
uint64_t ftrunc_errs = 0;
off_t fallocate_bytes = DEFAULT_FALLOCATE_BYTES;
if (!get_setting("fallocate-bytes", &fallocate_bytes)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
fallocate_bytes = MAX_FALLOCATE_BYTES;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
fallocate_bytes = MIN_FALLOCATE_BYTES;
}
fallocate_bytes /= args->num_instances;
if (fallocate_bytes < (off_t)MIN_FALLOCATE_BYTES)
fallocate_bytes = (off_t)MIN_FALLOCATE_BYTES;
ret = stress_temp_dir_mk_args(args);
if (ret < 0)
return exit_status(-ret);
(void)stress_temp_filename_args(args,
filename, sizeof(filename), mwc32());
(void)umask(0077);
if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
ret = exit_status(errno);
pr_fail_err("open");
(void)stress_temp_dir_rm_args(args);
return ret;
}
(void)unlink(filename);
do {
ret = posix_fallocate(fd, (off_t)0, fallocate_bytes);
if (!g_keep_stressing_flag)
break;
(void)fsync(fd);
if ((ret == 0) && (g_opt_flags & OPT_FLAGS_VERIFY)) {
struct stat buf;
if (fstat(fd, &buf) < 0)
pr_fail("%s: fstat on file failed", args->name);
else if (buf.st_size != fallocate_bytes)
pr_fail("%s: file size %jd does not "
"match size the expected file "
"size of %jd\n",
args->name, (intmax_t)buf.st_size,
(intmax_t)fallocate_bytes);
}
if (ftruncate(fd, 0) < 0)
ftrunc_errs++;
if (!g_keep_stressing_flag)
break;
(void)fsync(fd);
if (g_opt_flags & OPT_FLAGS_VERIFY) {
struct stat buf;
if (fstat(fd, &buf) < 0)
pr_fail("%s: fstat on file failed", args->name);
else if (buf.st_size != (off_t)0)
pr_fail("%s: file size %jd does not "
"match size the expected file size "
"of 0\n",
args->name, (intmax_t)buf.st_size);
}
if (ftruncate(fd, fallocate_bytes) < 0)
ftrunc_errs++;
(void)fsync(fd);
if (ftruncate(fd, 0) < 0)
ftrunc_errs++;
(void)fsync(fd);
if (SIZEOF_ARRAY(modes) > 1) {
/*
* non-portable Linux fallocate()
*/
int i;
(void)shim_fallocate(fd, 0, (off_t)0, fallocate_bytes);
if (!g_keep_stressing_flag)
break;
(void)fsync(fd);
for (i = 0; i < 64; i++) {
off_t offset = (mwc64() % fallocate_bytes) & ~0xfff;
int j = (mwc32() >> 8) % SIZEOF_ARRAY(modes);
(void)shim_fallocate(fd, modes[j], offset, 64 * KB);
if (!g_keep_stressing_flag)
break;
(void)fsync(fd);
}
if (ftruncate(fd, 0) < 0)
ftrunc_errs++;
(void)fsync(fd);
}
inc_counter(args);
} while (keep_stressing());
if (ftrunc_errs)
pr_dbg("%s: %" PRIu64
" ftruncate errors occurred.\n", args->name, ftrunc_errs);
(void)close(fd);
(void)stress_temp_dir_rm_args(args);
return EXIT_SUCCESS;
}
#else
int stress_fallocate(const args_t *args)
{
return stress_not_implemented(args);
}
#endif