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
|
/*
* 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"
#if defined(HAVE_LIB_PTHREAD) && defined(__linux__)
#include <semaphore.h>
#endif
static uint64_t opt_semaphore_posix_procs = DEFAULT_SEMAPHORE_PROCS;
static bool set_semaphore_posix_procs = false;
void stress_set_semaphore_posix_procs(const char *optarg)
{
set_semaphore_posix_procs = true;
opt_semaphore_posix_procs = get_uint64_byte(optarg);
check_range("sem-procs", opt_semaphore_posix_procs,
MIN_SEMAPHORE_PROCS, MAX_SEMAPHORE_PROCS);
}
#if defined(HAVE_LIB_PTHREAD) && defined(__linux__)
/*
* stress_semaphore_posix_init()
* initialize a POSIX semaphore
*/
void stress_semaphore_posix_init(void)
{
/* create a mutex */
if (sem_init(&shared->sem_posix.sem, 1, 1) >= 0) {
shared->sem_posix.init = true;
return;
}
if (opt_sequential) {
pr_inf(stdout, "semaphore init (POSIX) failed: errno=%d: "
"(%s), skipping semaphore stressor\n",
errno, strerror(errno));
} else {
pr_err(stderr, "semaphore init (POSIX) failed: errno=%d: "
"(%s)\n", errno, strerror(errno));
}
}
/*
* stress_semaphore_posix_destroy()
* destroy a POSIX semaphore
*/
void stress_semaphore_posix_destroy(void)
{
if (shared->sem_posix.init) {
if (sem_destroy(&shared->sem_posix.sem) < 0) {
pr_err(stderr, "semaphore destroy failed: errno=%d (%s)\n",
errno, strerror(errno));
}
}
}
/*
* semaphore_posix_thrash()
* exercise the semaphore
*/
static void semaphore_posix_thrash(
const char *name,
const uint64_t max_ops,
uint64_t *counter)
{
do {
int i;
struct timespec timeout;
if (clock_gettime(CLOCK_REALTIME, &timeout) < 0) {
pr_fail_dbg(name, "clock_gettime");
return;
}
timeout.tv_sec++;
for (i = 0; i < 1000; i++) {
if (sem_timedwait(&shared->sem_posix.sem, &timeout) < 0) {
if (errno == ETIMEDOUT)
goto timed_out;
if (errno != EINTR)
pr_fail_dbg(name, "sem_wait");
break;
}
(*counter)++;
if (sem_post(&shared->sem_posix.sem) < 0) {
pr_fail_dbg(name, "sem_post");
break;
}
timed_out:
if (!opt_do_run)
break;
}
} while (opt_do_run && (!max_ops || *counter < max_ops));
}
/*
* semaphore_posix_spawn()
* spawn a process
*/
static pid_t semaphore_posix_spawn(
const char *name,
const uint64_t max_ops,
uint64_t *counter)
{
pid_t pid;
again:
pid = fork();
if (pid < 0) {
if (opt_do_run && (errno == EAGAIN))
goto again;
return -1;
}
if (pid == 0) {
(void)setpgid(0, pgrp);
stress_parent_died_alarm();
semaphore_posix_thrash(name, max_ops, counter);
exit(EXIT_SUCCESS);
}
(void)setpgid(pid, pgrp);
return pid;
}
/*
* stress_sem()
* stress system by POSIX sem ops
*/
int stress_sem(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
pid_t pids[MAX_SEMAPHORE_PROCS];
uint64_t i;
(void)instance;
if (!set_semaphore_posix_procs) {
if (opt_flags & OPT_FLAGS_MAXIMIZE)
opt_semaphore_posix_procs = MAX_SEMAPHORE_PROCS;
if (opt_flags & OPT_FLAGS_MINIMIZE)
opt_semaphore_posix_procs = MIN_SEMAPHORE_PROCS;
}
if (!shared->sem_posix.init) {
pr_err(stderr, "%s: aborting, semaphore not initialised\n", name);
return EXIT_FAILURE;
}
memset(pids, 0, sizeof(pids));
for (i = 0; i < opt_semaphore_posix_procs; i++) {
pids[i] = semaphore_posix_spawn(name, max_ops, counter);
if (!opt_do_run || pids[i] < 0)
goto reap;
}
/* Wait for termination */
while (opt_do_run && (!max_ops || *counter < max_ops))
(void)shim_usleep(100000);
reap:
for (i = 0; i < opt_semaphore_posix_procs; i++) {
if (pids[i] > 0)
(void)kill(pids[i], SIGKILL);
}
for (i = 0; i < opt_semaphore_posix_procs; i++) {
if (pids[i] > 0) {
int status;
(void)waitpid(pids[i], &status, 0);
}
}
return EXIT_SUCCESS;
}
#else
void stress_semaphore_posix_init(void)
{
}
void stress_semaphore_posix_destroy(void)
{
}
int stress_sem(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
return stress_not_implemented(counter, instance, max_ops, name);
}
#endif
|