-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathstress-procfs.c
494 lines (433 loc) · 9.96 KB
/
stress-procfs.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* 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 <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
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;
#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 += *(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;
}
/*
* 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, i = 0;
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;
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;
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;
}
/*
* fstat the file
*/
ret = fstat(fd, &statbuf);
#if defined(__linux__)
/*
* Linux name space symlinks can be exercised
* with some special name space ioctls:
*/
if (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
*/
while (i < (4096 * PROC_BUF_SZ)) {
ssize_t sz = 1 + (stress_mwc32() % sizeof(buffer));
if (!keep_stressing_flag())
break;
ret = read(fd, buffer, 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);
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;
/*
* 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 (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);
}
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);
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 ? args->instance + 1 : 8;
int i, n;
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)
goto done;
for (i = 0; i < n; i++) {
int ret;
char tmp[PATH_MAX];
struct dirent *d = dlist[i];
if (!keep_stressing_flag())
break;
if (stress_is_dot_filename(d->d_name))
continue;
(void)snprintf(tmp, sizeof(tmp), "%s/%s", path, d->d_name);
switch (d->d_type) {
case DT_DIR:
if (!recurse)
continue;
inc_counter(args);
stress_proc_dir(ctxt, tmp, recurse, depth + 1);
break;
case DT_REG:
case DT_LNK:
ret = shim_pthread_spin_lock(&lock);
if (!ret) {
(void)shim_strlcpy(proc_path, tmp, sizeof(proc_path));
(void)shim_pthread_spin_unlock(&lock);
stress_proc_rw(ctxt, loops);
inc_counter(args);
}
break;
default:
break;
}
}
done:
if (dlist) {
for (i = 0; i < n; i++)
free(dlist[i]);
free(dlist);
}
}
/*
* stress_procfs
* stress reading all of /proc
*/
static int stress_procfs(const stress_args_t *args)
{
size_t i;
pthread_t pthreads[MAX_PROCFS_THREADS];
int rc, ret[MAX_PROCFS_THREADS];
stress_ctxt_t ctxt;
(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);
}
i = args->instance;
do {
i %= 12;
switch (i) {
case 0:
stress_proc_dir(&ctxt, "/proc", false, 0);
break;
case 1:
stress_proc_dir(&ctxt, "/proc/self", true, 0);
break;
case 2:
stress_proc_dir(&ctxt, "/proc/thread_self", true, 0);
break;
case 3:
stress_proc_dir(&ctxt, "/proc/sys", true, 0);
break;
case 4:
stress_proc_dir(&ctxt, "/proc/sysvipc", true, 0);
break;
case 5:
stress_proc_dir(&ctxt, "/proc/fs", true, 0);
break;
case 6:
stress_proc_dir(&ctxt, "/proc/bus", true, 0);
break;
case 7:
stress_proc_dir(&ctxt, "/proc/irq", true, 0);
break;
case 8:
stress_proc_dir(&ctxt, "/proc/scsi", true, 0);
break;
case 9:
stress_proc_dir(&ctxt, "/proc/tty", true, 0);
break;
case 10:
stress_proc_dir(&ctxt, "/proc/driver", true, 0);
break;
case 11:
stress_proc_dir(&ctxt, "/proc/tty", true, 0);
break;
default:
break;
}
i++;
inc_counter(args);
if (!keep_stressing())
break;
} while (keep_stressing());
rc = shim_pthread_spin_lock(&lock);
if (rc) {
pr_dbg("%s: failed to lock spin lock for sysfs_path\n", args->name);
} else {
shim_strlcpy(proc_path, "", sizeof(proc_path));
rc = shim_pthread_spin_unlock(&lock);
(void)rc;
}
for (i = 0; i < MAX_PROCFS_THREADS; i++) {
if (ret[i] == 0)
(void)pthread_join(pthreads[i], NULL);
}
(void)shim_pthread_spin_destroy(&lock);
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