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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/*
* Copyright (C) 2013-2019 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 <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if defined(F_GETLK) && defined(F_SETLK) && defined(F_SETLKW) && \
defined(F_WRLCK) && defined(F_UNLCK)
#define LOCK_FILE_SIZE (1024 * 1024)
#define LOCK_SIZE (8)
#define LOCK_MAX (1024)
typedef struct locka_info {
struct locka_info *next;
off_t offset;
off_t len;
pid_t pid;
} locka_info_t;
typedef struct {
locka_info_t *head; /* Head of locka_info procs list */
locka_info_t *tail; /* Tail of locka_info procs list */
locka_info_t *free; /* List of free'd locka_infos */
uint64_t length; /* Length of list */
} locka_info_list_t;
static locka_info_list_t locka_infos;
/*
* stress_locka_info_new()
* allocate a new locka_info, add to end of list
*/
static locka_info_t *stress_locka_info_new(void)
{
locka_info_t *new;
if (locka_infos.free) {
/* Pop an old one off the free list */
new = locka_infos.free;
locka_infos.free = new->next;
new->next = NULL;
} else {
new = calloc(1, sizeof(*new));
if (!new)
return NULL;
}
if (locka_infos.head)
locka_infos.tail->next = new;
else
locka_infos.head = new;
locka_infos.tail = new;
locka_infos.length++;
return new;
}
/*
* stress_locka_info_head_remove
* reap a locka_info and remove a locka_info from head of list, put it onto
* the free locka_info list
*/
static void stress_locka_info_head_remove(void)
{
if (locka_infos.head) {
locka_info_t *head = locka_infos.head;
if (locka_infos.tail == locka_infos.head) {
locka_infos.tail = NULL;
locka_infos.head = NULL;
} else {
locka_infos.head = head->next;
}
/* Shove it on the free list */
head->next = locka_infos.free;
locka_infos.free = head;
locka_infos.length--;
}
}
/*
* stress_locka_info_free()
* free the locka_infos off the locka_info head and free lists
*/
static void stress_locka_info_free(void)
{
while (locka_infos.head) {
locka_info_t *next = locka_infos.head->next;
free(locka_infos.head);
locka_infos.head = next;
}
while (locka_infos.free) {
locka_info_t *next = locka_infos.free->next;
free(locka_infos.free);
locka_infos.free = next;
}
}
/*
* stress_locka_unlock()
* pop oldest lock record off list and unlock it
*/
static int stress_locka_unlock(const args_t *args, const int fd)
{
struct flock f;
/* Pop one off list */
if (!locka_infos.head)
return 0;
f.l_type = F_UNLCK;
f.l_whence = SEEK_SET;
f.l_start = locka_infos.head->offset;
f.l_len = locka_infos.head->len;
f.l_pid = locka_infos.head->pid;
stress_locka_info_head_remove();
if (fcntl(fd, F_SETLK, &f) < 0) {
pr_fail_err("F_SETLK");
return -1;
}
return 0;
}
/*
* stress_locka_contention()
* hammer advisory lock/unlock to create some file lock contention
*/
static int stress_locka_contention(
const args_t *args,
const int fd)
{
mwc_reseed();
do {
off_t offset;
off_t len;
int rc;
locka_info_t *locka_info;
struct flock f;
if (locka_infos.length >= LOCK_MAX)
if (stress_locka_unlock(args, fd) < 0)
return -1;
len = (mwc16() + 1) & 0xfff;
offset = mwc64() % (LOCK_FILE_SIZE - len);
f.l_type = F_WRLCK;
f.l_whence = SEEK_SET;
f.l_start = offset;
f.l_len = len;
f.l_pid = args->pid;
rc = fcntl(fd, F_GETLK, &f);
if (rc < 0)
continue;
/* Locked OK, add to lock list */
locka_info = stress_locka_info_new();
if (!locka_info) {
pr_fail_err("calloc");
return -1;
}
locka_info->offset = offset;
locka_info->len = len;
locka_info->pid = args->pid;
inc_counter(args);
} while (keep_stressing());
return 0;
}
/*
* stress_locka
* stress file locking via advisory locking
*/
static int stress_locka(const args_t *args)
{
int fd, ret = EXIT_FAILURE;
pid_t cpid = -1;
char filename[PATH_MAX];
char dirname[PATH_MAX];
char buffer[4096];
off_t offset;
ssize_t rc;
(void)memset(buffer, 0, sizeof(buffer));
/*
* There will be a race to create the directory
* so EEXIST is expected on all but one instance
*/
(void)stress_temp_dir_args(args, dirname, sizeof(dirname));
if (mkdir(dirname, S_IRWXU) < 0) {
if (errno != EEXIST) {
ret = exit_status(errno);
pr_fail_err("mkdir");
return ret;
}
}
/*
* Lock file is based on parent pid and instance 0
* as we need to share this among all the other
* stress flock processes
*/
(void)stress_temp_filename_args(args,
filename, sizeof(filename), mwc32());
if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
ret = exit_status(errno);
pr_fail_err("open");
(void)rmdir(dirname);
return ret;
}
if (lseek(fd, 0, SEEK_SET) < 0) {
pr_fail_err("lseek");
goto tidy;
}
for (offset = 0; offset < LOCK_FILE_SIZE; offset += sizeof(buffer)) {
redo:
if (!g_keep_stressing_flag) {
ret = EXIT_SUCCESS;
goto tidy;
}
rc = write(fd, buffer, sizeof(buffer));
if ((rc < 0) || (rc != sizeof(buffer))) {
if ((errno == EAGAIN) || (errno == EINTR))
goto redo;
ret = exit_status(errno);
pr_fail_err("write");
goto tidy;
}
}
again:
cpid = fork();
if (cpid < 0) {
if (!g_keep_stressing_flag) {
ret = EXIT_SUCCESS;
goto tidy;
}
if (errno == EAGAIN)
goto again;
pr_fail_err("fork");
goto tidy;
}
if (cpid == 0) {
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
if (stress_locka_contention(args, fd) < 0)
_exit(EXIT_FAILURE);
stress_locka_info_free();
_exit(EXIT_SUCCESS);
}
(void)setpgid(cpid, g_pgrp);
if (stress_locka_contention(args, fd) == 0)
ret = EXIT_SUCCESS;
tidy:
if (cpid > 0) {
int status;
(void)kill(cpid, SIGKILL);
(void)waitpid(cpid, &status, 0);
}
stress_locka_info_free();
(void)close(fd);
(void)unlink(filename);
(void)rmdir(dirname);
return ret;
}
stressor_info_t stress_locka_info = {
.stressor = stress_locka,
.class = CLASS_FILESYSTEM | CLASS_OS
};
#else
stressor_info_t stress_locka_info = {
.stressor = stress_not_implemented,
.class = CLASS_FILESYSTEM | CLASS_OS
};
#endif
|