1 Star 0 Fork 2

gaoxuelong / stress-ng

forked from HoperunHarmony / stress-ng 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stress-procfs.c 15.83 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
/*
* Copyright (C) 2014-2021 Canonical, Ltd.
* Copyright (C) 2021-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"
#include "core-put.h"
#if defined(HAVE_ASM_MTRR_H)
#include <asm/mtrr.h>
#endif
#if defined(HAVE_LINUX_PCI_H)
#include <linux/pci.h>
#endif
#if defined(HAVE_POLL_H)
#include <poll.h>
#endif
static const stress_help_t help[] = {
{ NULL, "procfs N", "start N workers reading portions of /proc" },
{ NULL, "procfs-ops N", "stop procfs workers after N bogo read operations" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_LIB_PTHREAD) && \
defined(__linux__)
#define PROC_BUF_SZ (4096)
#define MAX_PROCFS_THREADS (4)
typedef struct stress_ctxt {
const stress_args_t *args;
const char *path;
bool writeable;
} stress_ctxt_t;
typedef struct {
const char *filename;
void (*stress_func)(const int fd);
} stress_proc_info_t;
#if !defined(NSIO)
#define NSIO 0xb7
#endif
#if !defined(NS_GET_USERNS)
#define NS_GET_USERNS _IO(NSIO, 0x1)
#endif
#if !defined(NS_GET_PARENT)
#define NS_GET_PARENT _IO(NSIO, 0x2)
#endif
#if !defined(NS_GET_NSTYPE)
#define NS_GET_NSTYPE _IO(NSIO, 0x3)
#endif
#if !defined(NS_GET_OWNER_UID)
#define NS_GET_OWNER_UID _IO(NSIO, 0x4)
#endif
static sigset_t set;
static shim_pthread_spinlock_t lock;
static char proc_path[PATH_MAX];
static uint32_t mixup;
static uint32_t path_sum(const char *path)
{
const char *ptr = path;
register uint32_t sum = mixup;
while (*ptr) {
sum <<= 1;
sum += (uint32_t)*(ptr++);
}
return sum;
}
static int mixup_sort(const struct dirent **d1, const struct dirent **d2)
{
uint32_t s1, s2;
s1 = path_sum((*d1)->d_name);
s2 = path_sum((*d2)->d_name);
if (s1 == s2)
return 0;
return (s1 < s2) ? -1 : 1;
}
#if defined(HAVE_ASM_MTRR_H) && \
defined(HAVE_MTRR_GENTRY) && \
defined(MTRRIOC_GET_ENTRY)
/*
* stress_proc_mtrr()
* exercise /proc/mtrr ioctl MTRRIOC_GET_ENTRY
*/
static void stress_proc_mtrr(const int fd)
{
struct mtrr_gentry gentry;
(void)memset(&gentry, 0, sizeof(gentry));
while (ioctl(fd, MTRRIOC_GET_ENTRY, &gentry) == 0) {
gentry.regnum++;
}
}
#endif
/*
* stress_proc_pci()
* exercise PCI PCIIOC_CONTROLLER
*/
#if defined(HAVE_LINUX_PCI_H) && \
defined(PCIIOC_CONTROLLER)
static void stress_proc_pci(const int fd)
{
int ret;
ret = ioctl(fd, PCIIOC_CONTROLLER);
(void)ret;
#if defined(PCIIOC_BASE)
/* EINVAL ioctl */
ret = ioctl(fd, PCIIOC_BASE | 0xff);
(void)ret;
#endif
}
#endif
static stress_proc_info_t stress_proc_info[] = {
#if defined(HAVE_ASM_MTRR_H) && \
defined(HAVE_MTRR_GENTRY) && \
defined(MTRRIOC_GET_ENTRY)
{ "/proc/mtrr", stress_proc_mtrr },
#endif
#if defined(HAVE_LINUX_PCI_H) && \
defined(PCIIOC_CONTROLLER)
{ "/proc/bus/pci/00/00.0", stress_proc_pci }, /* x86 */
{ "/proc/bus/pci/0000:00/00.0", stress_proc_pci }, /* RISC-V */
#endif
};
/*
* stress_proc_rw()
* read a proc file
*/
static inline void stress_proc_rw(
const stress_ctxt_t *ctxt,
int32_t loops)
{
int fd;
ssize_t ret;
char buffer[PROC_BUF_SZ];
char path[PATH_MAX];
const double threshold = 0.2;
const size_t page_size = ctxt->args->page_size;
off_t pos;
while ((loops == -1) || (loops > 0)) {
double t_start;
bool timeout = false;
uint8_t *ptr;
struct stat statbuf;
bool writeable = true;
size_t len;
ssize_t i;
bool skip_fifo = true;
ret = shim_pthread_spin_lock(&lock);
if (ret)
return;
(void)shim_strlcpy(path, proc_path, sizeof(path));
(void)shim_pthread_spin_unlock(&lock);
if (!*path || !keep_stressing_flag())
break;
if (!strncmp(path, "/proc/self", 10))
writeable = false;
if (!strncmp(path, "/proc", 5) && isdigit(path[5]))
writeable = false;
t_start = stress_time_now();
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
return;
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
/*
* Check if there any special features to exercise
*/
for (i = 0; i < (ssize_t)SIZEOF_ARRAY(stress_proc_info); i++) {
if (!strcmp(path, stress_proc_info[i].filename)) {
stress_proc_info[i].stress_func(fd);
break;
}
}
/*
* fstat the file
*/
ret = fstat(fd, &statbuf);
if (ret == 0) {
#if defined(S_IFMT) && \
defined(S_IFIFO)
if ((statbuf.st_mode & S_IFMT) != S_IFIFO)
skip_fifo = false;
#endif
}
#if defined(__linux__)
/*
* Linux name space symlinks can be exercised
* with some special name space ioctls:
*/
if ((ret == 0) && (statbuf.st_mode & S_IFLNK)) {
if (!strncmp(path, "/proc/self", 10) && (strstr(path, "/ns/"))) {
int ns_fd;
uid_t uid;
ns_fd = ioctl(fd, NS_GET_USERNS);
if (ns_fd >= 0)
(void)close(ns_fd);
ns_fd = ioctl(fd, NS_GET_PARENT);
if (ns_fd >= 0)
(void)close(ns_fd);
ret = ioctl(fd, NS_GET_NSTYPE);
(void)ret;
/* The following returns -EINVAL */
ret = ioctl(fd, NS_GET_OWNER_UID, &uid);
(void)ret;
}
}
#endif
/*
* Multiple randomly sized reads
*/
for (i = 0; i < 4096 * PROC_BUF_SZ; i++) {
ssize_t sz = 1 + (stress_mwc32() % sizeof(buffer));
if (!keep_stressing_flag())
break;
ret = read(fd, buffer, (size_t)sz);
if (ret < 0)
break;
if (ret < sz)
break;
i += sz;
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
}
(void)close(fd);
/* Multiple 1 char sized reads */
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
return;
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
for (i = 0; ; i++) {
if (!keep_stressing_flag())
break;
ret = read(fd, buffer, 1);
if (ret < 1)
break;
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
}
(void)close(fd);
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
return;
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
/*
* Zero sized reads
*/
ret = read(fd, buffer, 0);
if (ret < 0)
goto err;
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
/*
* Broken offset reads, see Linux commit
* 3bfa7e141b0bbb818b25e0daafb65aee92e49ac4
* "fs/seq_file.c: seq_read(): add info message
* about buggy .next functions"
*/
pos = lseek(fd, 0, SEEK_SET);
if (pos < 0)
goto mmap_test;
(void)memset(buffer, 0, sizeof(buffer));
ret = read(fd, buffer, sizeof(buffer));
if (ret < 0)
goto mmap_test;
if (ret < (ssize_t)(sizeof(buffer) >> 1)) {
char *bptr;
for (bptr = buffer; *bptr && *bptr != '\n'; bptr++)
;
if (*bptr == '\n') {
const off_t offset = 2 + (bptr - buffer);
pos = lseek(fd, offset, SEEK_SET);
if (pos == offset) {
/* Causes incorrect 2nd read */
ret = read(fd, buffer, sizeof(buffer));
(void)ret;
}
}
}
/*
* exercise 13 x 5 byte reads backwards through procfs file to
* ensure we perform some weird misaligned non-word sized reads
*/
if (!skip_fifo) {
off_t dec;
pos = lseek(fd, 0, SEEK_END);
if (pos < 0)
goto mmap_test;
dec = pos / 13;
if (dec < 1)
dec = 1;
while (pos > 0) {
off_t seek_pos;
seek_pos = lseek(fd, pos, SEEK_SET);
if (seek_pos < 0)
break;
ret = read(fd, buffer, 5);
(void)ret;
if (dec > pos)
dec = pos;
pos -= dec;
}
}
mmap_test:
/*
* mmap it
*/
ptr = mmap(NULL, page_size, PROT_READ,
MAP_SHARED | MAP_ANONYMOUS, fd, 0);
if (ptr != MAP_FAILED) {
stress_uint8_put(*ptr);
(void)munmap(ptr, page_size);
}
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
#if defined(FIONREAD)
{
int nbytes;
/*
* ioctl(), bytes ready to read
*/
ret = ioctl(fd, FIONREAD, &nbytes);
(void)ret;
}
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
#endif
#if defined(HAVE_POLL_H)
{
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN;
fds[0].revents = 0;
ret = poll(fds, 1, 0);
(void)ret;
}
#endif
/*
* Seek and read
*/
pos = lseek(fd, 0, SEEK_SET);
if (pos == (off_t)-1)
goto err;
pos = lseek(fd, 1, SEEK_CUR);
if (pos == (off_t)-1)
goto err;
pos = lseek(fd, 0, SEEK_END);
if (pos == (off_t)-1)
goto err;
pos = lseek(fd, 1, SEEK_SET);
if (pos == (off_t)-1)
goto err;
if (stress_time_now() - t_start > threshold) {
timeout = true;
(void)close(fd);
goto next;
}
ret = read(fd, buffer, 1);
(void)ret;
err:
(void)close(fd);
if (stress_time_now() - t_start > threshold) {
timeout = true;
goto next;
}
if (writeable && ctxt->writeable) {
/*
* Zero sized writes
*/
if ((fd = open(path, O_WRONLY | O_NONBLOCK)) < 0)
return;
ret = write(fd, buffer, 0);
(void)ret;
(void)close(fd);
}
/*
* Create /proc/ filename with - corruption to force
* ENOENT procfs open failures
*/
len = strlen(path);
/* /proc + ... */
if (len > 5) {
char *pptr = path + 5 + (stress_mwc16() % (len - 5));
/* Skip over / */
while (*pptr == '/')
pptr++;
if (*pptr) {
*pptr = '-';
/*
* Expect ENOENT, but if it does open then
* close it immediately
*/
fd = open(path, O_WRONLY | O_NONBLOCK);
if (fd >= 0)
(void)close(fd);
}
}
next:
if (loops > 0) {
if (timeout)
break;
loops--;
}
}
}
/*
* stress_proc_rw_thread
* keep exercising a procfs entry until
* controlling thread triggers an exit
*/
static void *stress_proc_rw_thread(void *ctxt_ptr)
{
static void *nowt = NULL;
stress_ctxt_t *ctxt = (stress_ctxt_t *)ctxt_ptr;
/*
* Block all signals, let controlling thread
* handle these
*/
(void)sigprocmask(SIG_BLOCK, &set, NULL);
while (keep_stressing_flag()) {
stress_proc_rw(ctxt, -1);
if (!*proc_path)
break;
}
return &nowt;
}
/*
* stress_proc_dir()
* read directory
*/
static void stress_proc_dir(
const stress_ctxt_t *ctxt,
const char *path,
const bool recurse,
const int depth)
{
struct dirent **dlist;
const stress_args_t *args = ctxt->args;
int32_t loops = args->instance < 8 ?
(int32_t)(args->instance + 1) : 8;
int i, n, ret;
char tmp[PATH_MAX];
if (!keep_stressing_flag())
return;
/* Don't want to go too deep */
if (depth > 20)
return;
mixup = stress_mwc32();
dlist = NULL;
n = scandir(path, &dlist, NULL, mixup_sort);
if (n <= 0) {
stress_dirent_list_free(dlist, n);
return;
}
/* Non-directories files first */
for (i = 0; (i < n) && keep_stressing_flag(); i++) {
struct dirent *d = dlist[i];
if (stress_is_dot_filename(d->d_name)) {
free(d);
dlist[i] = NULL;
continue;
}
if ((d->d_type == DT_REG) || (d->d_type == DT_LNK)) {
ret = shim_pthread_spin_lock(&lock);
if (!ret) {
(void)stress_mk_filename(tmp, sizeof(tmp), path, d->d_name);
(void)shim_strlcpy(proc_path, tmp, sizeof(proc_path));
(void)shim_pthread_spin_unlock(&lock);
stress_proc_rw(ctxt, loops);
inc_counter(args);
}
free(d);
dlist[i] = NULL;
}
}
if (!recurse) {
stress_dirent_list_free(dlist, n);
return;
}
/* Now recurse on directories */
for (i = 0; i < n && keep_stressing_flag(); i++) {
struct dirent *d = dlist[i];
if (d && d->d_type == DT_DIR) {
(void)stress_mk_filename(tmp, sizeof(tmp), path, d->d_name);
free(d);
dlist[i] = NULL;
stress_proc_dir(ctxt, tmp, recurse, depth + 1);
inc_counter(args);
}
}
stress_dirent_list_free(dlist, n);
}
/*
* stress_random_pid()
* return /proc/$pid where pid is a random existing process ID
*/
static char *stress_random_pid(void)
{
struct dirent **dlist = NULL;
static char path[PATH_MAX];
int i, n;
size_t j;
(void)shim_strlcpy(path, "/proc/self", sizeof(path));
n = scandir("/proc", &dlist, NULL, mixup_sort);
if (!n) {
stress_dirent_list_free(dlist, n);
return path;
}
/*
* try 32 random probes before giving up
*/
for (i = 0, j = 0; i < 32; i++) {
char *name;
j += (size_t)stress_mwc32();
j %= (size_t)n;
name = dlist[j]->d_name;
if (isdigit(name[0])) {
(void)stress_mk_filename(path, sizeof(path), "/proc", name);
break;
}
}
stress_dirent_list_free(dlist, n);
return path;
}
/*
* stress_dirent_proc_prune()
* remove . and .. and pid files from directory list
*/
static int stress_dirent_proc_prune(struct dirent **dlist, const int n)
{
int i, j;
for (i = 0, j = 0; i < n; i++) {
if (dlist[i]) {
if (stress_is_dot_filename(dlist[i]->d_name) ||
isdigit((int)dlist[i]->d_name[0])) {
free(dlist[i]);
dlist[i] = NULL;
} else {
dlist[j] = dlist[i];
j++;
}
}
}
return j;
}
/*
* stress_procfs
* stress reading all of /proc
*/
static int stress_procfs(const stress_args_t *args)
{
int i, n;
pthread_t pthreads[MAX_PROCFS_THREADS];
int rc, ret[MAX_PROCFS_THREADS];
stress_ctxt_t ctxt;
struct dirent **dlist = NULL;
n = scandir("/proc", &dlist, NULL, alphasort);
if (n <= 0) {
if (args->instance == 0)
pr_inf_skip("%s: no /proc entries found, skipping stressor\n", args->name);
return EXIT_NO_RESOURCE;
}
n = stress_dirent_proc_prune(dlist, n);
(void)sigfillset(&set);
shim_strlcpy(proc_path, "/proc/self", sizeof(proc_path));
ctxt.args = args;
ctxt.writeable = (geteuid() != 0);
rc = shim_pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
if (rc) {
pr_inf("%s: pthread_spin_init failed, errno=%d (%s)\n",
args->name, rc, strerror(rc));
return EXIT_NO_RESOURCE;
}
(void)memset(ret, 0, sizeof(ret));
for (i = 0; i < MAX_PROCFS_THREADS; i++) {
ret[i] = pthread_create(&pthreads[i], NULL,
stress_proc_rw_thread, &ctxt);
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
size_t j = args->instance % (size_t)n;
for (i = 0; i < n; i++) {
char procfspath[PATH_MAX];
struct dirent *d = dlist[i];
if (!keep_stressing(args))
break;
stress_mk_filename(procfspath, sizeof(procfspath), "/proc", d->d_name);
if ((d->d_type == DT_REG) || (d->d_type == DT_LNK)) {
if (!shim_pthread_spin_lock(&lock)) {
(void)shim_strlcpy(proc_path, procfspath, sizeof(proc_path));
(void)shim_pthread_spin_unlock(&lock);
stress_proc_rw(&ctxt, 8);
inc_counter(args);
}
} else if (d->d_type == DT_DIR) {
stress_proc_dir(&ctxt, procfspath, true, 0);
}
j = (j + args->num_instances) % (size_t)n;
inc_counter(args);
}
if (!keep_stressing(args))
break;
stress_proc_dir(&ctxt, stress_random_pid(), true, 0);
inc_counter(args);
} while (keep_stressing(args));
rc = shim_pthread_spin_lock(&lock);
if (rc) {
pr_dbg("%s: spin lock failed for %s\n", args->name, proc_path);
} else {
shim_strlcpy(proc_path, "", sizeof(proc_path));
rc = shim_pthread_spin_unlock(&lock);
(void)rc;
}
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
for (i = 0; i < MAX_PROCFS_THREADS; i++) {
if (ret[i] == 0)
(void)pthread_join(pthreads[i], NULL);
}
(void)shim_pthread_spin_destroy(&lock);
stress_dirent_list_free(dlist, n);
return EXIT_SUCCESS;
}
stressor_info_t stress_procfs_info = {
.stressor = stress_procfs,
.class = CLASS_FILESYSTEM | CLASS_OS,
.help = help
};
#else
stressor_info_t stress_procfs_info = {
.stressor = stress_not_implemented,
.class = CLASS_FILESYSTEM | CLASS_OS,
.help = help
};
#endif
1
https://gitee.com/gaoxuelong/stress-ng.git
git@gitee.com:gaoxuelong/stress-ng.git
gaoxuelong
stress-ng
stress-ng
master

搜索帮助