forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-aio-linux.c
208 lines (188 loc) · 5.47 KB
/
stress-aio-linux.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* 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"
#if defined(__linux__) && \
defined(HAVE_LIB_AIO) && \
defined(__NR_io_setup) && \
defined(__NR_io_destroy) && \
defined(__NR_io_submit) && \
defined(__NR_io_getevents)
#include <libaio.h>
#endif
#define BUFFER_SZ (4096)
void stress_set_aio_linux_requests(const char *opt)
{
uint64_t aio_linux_requests;
aio_linux_requests = get_uint32(opt);
check_range("aiol-requests", aio_linux_requests,
MIN_AIO_LINUX_REQUESTS, MAX_AIO_LINUX_REQUESTS);
set_setting("aiol-requests", TYPE_ID_UINT64, &aio_linux_requests);
}
#if defined(__linux__) && \
defined(HAVE_LIB_AIO) && \
defined(__NR_io_setup) && \
defined(__NR_io_destroy) && \
defined(__NR_io_submit) && \
defined(__NR_io_getevents)
/*
* aio_linux_fill_buffer()
* fill buffer with some known pattern
*/
static inline void aio_linux_fill_buffer(
const int request,
uint8_t *const buffer,
const size_t size)
{
register size_t i;
for (i = 0; i < size; i++)
buffer[i] = (uint8_t)(request + i);
}
/*
* stress_aiol
* stress asynchronous I/O using the linux specific aio ABI
*/
int stress_aiol(const args_t *args)
{
int fd, ret, rc = EXIT_FAILURE;
char filename[PATH_MAX];
io_context_t ctx = 0;
uint64_t aio_linux_requests = DEFAULT_AIO_LINUX_REQUESTS;
if (!get_setting("aiol-requests", &aio_linux_requests)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
aio_linux_requests = MAX_AIO_REQUESTS;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
aio_linux_requests = MIN_AIO_REQUESTS;
}
if ((aio_linux_requests < MIN_AIO_REQUESTS) ||
(aio_linux_requests > MAX_AIO_REQUESTS)) {
pr_err("%s: iol_requests out of range", args->name);
return EXIT_FAILURE;
}
ret = io_setup(aio_linux_requests, &ctx);
if (ret < 0) {
/*
* The libaio interface returns -errno in the
* return value, so set errno accordingly
*/
errno = -ret;
if ((errno == EAGAIN) || (errno == EACCES)) {
pr_err("%s: io_setup failed, ran out of "
"available events, consider increasing "
"/proc/sys/fs/aio-max-nr, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_NO_RESOURCE;
} else if (errno == ENOMEM) {
pr_err("%s: io_setup failed, ran out of "
"memory, errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_NO_RESOURCE;
} else if (errno == ENOSYS) {
pr_err("%s: io_setup failed, no io_setup "
"system call with this kernel, "
"errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_NO_RESOURCE;
} else {
pr_fail_err("io_setup");
return EXIT_FAILURE;
}
}
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) {
rc = exit_status(errno);
pr_fail_err("open");
goto finish;
}
(void)unlink(filename);
do {
struct iocb cb[aio_linux_requests];
struct iocb *cbs[aio_linux_requests];
struct io_event events[aio_linux_requests];
uint8_t buffers[aio_linux_requests][BUFFER_SZ];
uint64_t i;
long n;
for (i = 0; i < aio_linux_requests; i++)
aio_linux_fill_buffer(i, buffers[i], BUFFER_SZ);
memset(cb, 0, sizeof(cb));
for (i = 0; i < aio_linux_requests; i++) {
cb[i].aio_fildes = fd;
cb[i].aio_lio_opcode = IO_CMD_PWRITE;
cb[i].u.c.buf = buffers[i];
cb[i].u.c.offset = mwc16() * BUFFER_SZ;
cb[i].u.c.nbytes = BUFFER_SZ;
cbs[i] = &cb[i];
}
ret = io_submit(ctx, (long)aio_linux_requests, cbs);
if (ret < 0) {
errno = -ret;
if (errno == EAGAIN)
continue;
pr_fail_err("io_submit");
break;
}
n = aio_linux_requests;
do {
struct timespec timeout, *timeout_ptr;
if (clock_gettime(CLOCK_REALTIME, &timeout) < 0) {
timeout_ptr = NULL;
} else {
timeout.tv_nsec += 1000000;
if (timeout.tv_nsec > 1000000000) {
timeout.tv_nsec -= 1000000000;
timeout.tv_sec++;
}
timeout_ptr = &timeout;
}
ret = io_getevents(ctx, 1, n, events, timeout_ptr);
if (ret < 0) {
errno = -ret;
if ((errno == EINTR) && (g_keep_stressing_flag))
continue;
pr_fail_err("io_getevents");
break;
} else {
n -= ret;
}
} while ((n > 0) && g_keep_stressing_flag);
inc_counter(args);
} while (keep_stressing());
rc = EXIT_SUCCESS;
(void)close(fd);
finish:
(void)io_destroy(ctx);
(void)stress_temp_dir_rm_args(args);
return rc;
}
#else
int stress_aiol(const args_t *args)
{
return stress_not_implemented(args);
}
#endif