forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-lease.c
278 lines (242 loc) · 6.38 KB
/
stress-lease.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#define MIN_LEASE_BREAKERS (1)
#define MAX_LEASE_BREAKERS (64)
#define DEFAULT_LEASE_BREAKERS (1)
#if defined(F_SETLEASE) && \
defined(F_WRLCK) && \
defined(F_UNLCK)
static uint64_t lease_sigio;
#endif
static const stress_help_t help[] = {
{ NULL, "lease N", "start N workers holding and breaking a lease" },
{ NULL, "lease-ops N", "stop after N lease bogo operations" },
{ NULL, "lease-breakers N", "number of lease breaking workers to start" },
{ NULL, NULL, NULL }
};
static int stress_set_lease_breakers(const char *opt)
{
uint64_t lease_breakers;
lease_breakers = stress_get_uint64(opt);
stress_check_range("lease-breakers", lease_breakers,
MIN_LEASE_BREAKERS, MAX_LEASE_BREAKERS);
return stress_set_setting("lease-breakers", TYPE_ID_UINT64, &lease_breakers);
}
static const stress_opt_set_func_t opt_set_funcs[] = {
{ OPT_lease_breakers, stress_set_lease_breakers },
{ 0, NULL }
};
#if defined(F_SETLEASE) && \
defined(F_WRLCK) && \
defined(F_UNLCK)
/*
* stress_lease_handler()
* lease signal handler
*/
static void MLOCKED_TEXT stress_lease_handler(int signum)
{
(void)signum;
lease_sigio++;
}
/*
* stress_get_lease()
* exercise getting the lease on fd
*/
static int stress_get_lease(const int fd)
{
return fcntl(fd, F_GETLEASE);
}
/*
* stress_lease_spawn()
* spawn a process
*/
static pid_t stress_lease_spawn(
const stress_args_t *args,
const char *filename)
{
pid_t pid;
int count = 0;
again:
pid = fork();
if (pid < 0) {
if (stress_redo_fork(errno))
goto again;
return -1;
}
if (pid == 0) {
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
(void)sched_settings_apply(true);
do {
int fd;
errno = 0;
fd = open(filename, O_NONBLOCK | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd < 0) {
if ((errno != EWOULDBLOCK) &&
(errno != EACCES)) {
pr_dbg("%s: open failed (child): errno=%d: (%s)\n",
args->name, errno, strerror(errno));
if (count++ > 3)
break;
}
continue;
}
(void)stress_get_lease(fd);
(void)close(fd);
} while (keep_stressing(args));
_exit(EXIT_SUCCESS);
}
(void)setpgid(pid, g_pgrp);
return pid;
}
/*
* stress_try_lease()
* try and get a lease with lock type 'lock'
*/
static int stress_try_lease(
const stress_args_t *args,
const char *filename,
const int flags,
const int lock)
{
int fd;
fd = open(filename, flags, S_IRUSR | S_IWUSR);
if (fd < 0) {
int ret;
ret = exit_status(errno);
pr_err("%s: open failed (parent): errno=%d: (%s)\n",
args->name, errno, strerror(errno));
return ret;
}
/*
* attempt a lease lock
*/
while (fcntl(fd, F_SETLEASE, lock) < 0) {
if (!keep_stressing_flag())
goto tidy;
}
(void)stress_get_lease(fd);
inc_counter(args);
(void)shim_sched_yield();
/*
* attempt a lease unlock
*/
while (fcntl(fd, F_SETLEASE, F_UNLCK) < 0) {
if (!keep_stressing_flag())
break;
if (errno != EAGAIN) {
pr_fail("%s: fcntl failed: errno=%d: (%s)\n",
args->name, errno, strerror(errno));
break;
}
}
tidy:
(void)close(fd);
return EXIT_SUCCESS;
}
/*
* stress_lease
* stress fcntl lease activity
*/
static int stress_lease(const stress_args_t *args)
{
char filename[PATH_MAX];
int ret, fd, status;
pid_t l_pids[MAX_LEASE_BREAKERS];
uint64_t i, lease_breakers = DEFAULT_LEASE_BREAKERS;
double t1 = 0.0, t2 = 0.0, dt;
if (!stress_get_setting("lease-breakers", &lease_breakers)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
lease_breakers = MAX_LEASE_BREAKERS;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
lease_breakers = MIN_LEASE_BREAKERS;
}
(void)memset(l_pids, 0, sizeof(l_pids));
if (stress_sighandler(args->name, SIGIO, stress_lease_handler, NULL) < 0)
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), stress_mwc32());
fd = creat(filename, S_IRUSR | S_IWUSR);
if (fd < 0) {
ret = exit_status(errno);
pr_err("%s: creat failed: errno=%d: (%s)\n",
args->name, errno, strerror(errno));
(void)stress_temp_dir_rm_args(args);
return ret;
}
(void)close(fd);
/*
* start lease breaker child processes
*/
for (i = 0; i < lease_breakers; i++) {
l_pids[i] = stress_lease_spawn(args, filename);
if (l_pids[i] < 0) {
pr_err("%s: failed to start all the lease breaker processes\n", args->name);
goto reap;
}
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
t1 = stress_time_now();
do {
ret = stress_try_lease(args, filename, O_WRONLY | O_APPEND, F_WRLCK);
if (ret != EXIT_SUCCESS)
break;
ret = stress_try_lease(args, filename, O_RDONLY, F_RDLCK);
if (ret != EXIT_SUCCESS)
break;
} while (keep_stressing(args));
t2 = stress_time_now();
reap:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
for (i = 0; i < lease_breakers; i++) {
if (l_pids[i]) {
(void)kill(l_pids[i], SIGKILL);
(void)shim_waitpid(l_pids[i], &status, 0);
}
}
(void)shim_unlink(filename);
(void)stress_temp_dir_rm_args(args);
pr_dbg("%s: %" PRIu64 " lease sigio interrupts caught\n", args->name, lease_sigio);
dt = t2 - t1;
if (dt > 0.0) {
stress_misc_stats_set(args->misc_stats, 0, "lease sigio interrupts per sec",
(double)lease_sigio / dt);
}
return ret;
}
stressor_info_t stress_lease_info = {
.stressor = stress_lease,
.class = CLASS_FILESYSTEM | CLASS_OS,
.opt_set_funcs = opt_set_funcs,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
stressor_info_t stress_lease_info = {
.stressor = stress_not_implemented,
.class = CLASS_FILESYSTEM | CLASS_OS,
.opt_set_funcs = opt_set_funcs,
.help = help
};
#endif