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
|
/*
* 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 <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"
typedef int (*open_func_t)(void);
static int open_dev_zero_rd(void)
{
int flags = 0;
#if defined(O_ASYNC)
flags |= (mwc32() & O_ASYNC);
#endif
#if defined(O_CLOEXEC)
flags |= (mwc32() & O_CLOEXEC);
#endif
#if defined(O_LARGEFILE)
flags |= (mwc32() & O_LARGEFILE);
#endif
#if defined(O_NOFOLLOW)
flags |= (mwc32() & O_NOFOLLOW);
#endif
#if defined(O_NONBLOCK)
flags |= (mwc32() & O_NONBLOCK);
#endif
return open("/dev/zero", O_RDONLY | flags);
}
static int open_dev_null_wr(void)
{
int flags = 0;
#if defined(O_ASYNC)
flags |= (mwc32() & O_ASYNC);
#endif
#if defined(O_CLOEXEC)
flags |= (mwc32() & O_CLOEXEC);
#endif
#if defined(O_LARGEFILE)
flags |= (mwc32() & O_LARGEFILE);
#endif
#if defined(O_NOFOLLOW)
flags |= (mwc32() & O_NOFOLLOW);
#endif
#if defined(O_NONBLOCK)
flags |= (mwc32() & O_NONBLOCK);
#endif
#if defined(O_DSYNC)
flags |= (mwc32() & O_DSYNC);
#endif
#if defined(O_SYNC)
flags |= (mwc32() & O_SYNC);
#endif
return open("/dev/null", O_WRONLY | flags);
}
#if defined(O_TMPFILE)
static int open_tmp_rdwr(void)
{
int flags = 0;
#if defined(O_TRUNC)
flags |= (mwc32() & O_TRUNC);
#endif
#if defined(O_APPEND)
flags |= (mwc32() & O_APPEND);
#endif
#if defined(O_NOATIME)
flags |= (mwc32() & O_NOATIME);
#endif
#if defined(O_DIRECT)
flags |= (mwc32() & O_DIRECT);
#endif
return open("/tmp", O_TMPFILE | flags | O_RDWR, S_IRUSR | S_IWUSR);
}
#endif
#if defined(O_TMPFILE) && defined(O_EXCL)
static int open_tmp_rdwr_excl(void)
{
return open("/tmp", O_TMPFILE | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
}
#endif
#if defined(O_DIRECTORY)
static int open_dir(void)
{
return open(".", O_DIRECTORY | O_RDONLY);
}
#endif
#if defined(O_PATH)
static int open_path(void)
{
return open(".", O_DIRECTORY | O_PATH);
}
#endif
static open_func_t open_funcs[] = {
open_dev_zero_rd,
open_dev_null_wr,
#if defined(O_TMPFILE)
open_tmp_rdwr,
#endif
#if defined(O_TMPFILE) && defined(O_EXCL)
open_tmp_rdwr_excl,
#endif
#if defined(O_DIRECTORY)
open_dir,
#endif
#if defined(O_PATH)
open_path
#endif
};
/*
* stress_open()
* stress system by rapid open/close calls
*/
int stress_open(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
int fds[STRESS_FD_MAX];
size_t max_fd = stress_get_file_limit();
size_t i;
(void)instance;
(void)name;
if (max_fd > SIZEOF_ARRAY(fds))
max_fd = SIZEOF_ARRAY(fds);
do {
for (i = 0; i < max_fd; i++) {
int idx = mwc32() % SIZEOF_ARRAY(open_funcs);
fds[i] = open_funcs[idx]();
if (fds[i] < 0)
break;
if (!opt_do_run)
break;
(*counter)++;
}
for (i = 0; i < max_fd; i++) {
if (fds[i] < 0)
break;
if (!opt_do_run)
break;
(void)close(fds[i]);
}
} while (opt_do_run && (!max_ops || *counter < max_ops));
return EXIT_SUCCESS;
}
|