1 /*
2 * Copyright (C) 2013-2021 Canonical, Ltd.
3 * Copyright (C) 2021 Colin Ian King
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20 #ifndef STRESS_NG_H
21 #define STRESS_NG_H
22
23 #define _GNU_SOURCE
24 #define _ATFILE_SOURCE
25 #ifndef _LARGEFILE_SOURCE
26 #define _LARGEFILE_SOURCE
27 #endif
28 #ifndef _LARGEFILE64_SOURCE
29 #define _LARGEFILE64_SOURCE
30 #endif
31
32 #if !defined(_FILE_OFFSET_BITS)
33 #define _FILE_OFFSET_BITS (64)
34 #endif
35
36 /* Some Solaris tool chains only define __sun */
37 #if defined(__sun) && \
38 !defined(__sun__)
39 #define __sun__
40 #endif
41
42 /*
43 * Standard includes
44 */
45 #include <ctype.h>
46 #include <dirent.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <getopt.h>
50 #include <inttypes.h>
51 #include <limits.h>
52 #include <math.h>
53 #include <pwd.h>
54 #include <sched.h>
55 #include <search.h>
56 #include <setjmp.h>
57 #include <signal.h>
58 #include <stdarg.h>
59 #include <stdbool.h>
60 #include <stddef.h>
61 #include <stdint.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <time.h>
66 #include <unistd.h>
67
68 #include "config.h"
69
70 #ifndef SCHED_FLAG_RESET_ON_FORK
71 #define SCHED_FLAG_RESET_ON_FORK (0x01)
72 #endif
73
74 /*
75 * SCHED_DEADLINE
76 */
77 #ifndef SCHED_FLAG_RECLAIM
78 #define SCHED_FLAG_RECLAIM (0x02)
79 #endif
80
81 #ifndef SCHED_FLAG_DL_OVERRUN
82 #define SCHED_FLAG_DL_OVERRUN (0x04)
83 #endif
84
85 /*
86 * Networking includes that are part of
87 * Single UNIX Specification V2
88 */
89 #include <arpa/inet.h>
90 #include <netinet/in.h>
91 #if defined(HAVE_NET_IF_H)
92 #include <net/if.h>
93 #endif
94 #if defined(HAVE_NETINET_IP_H)
95 #include <netinet/ip.h>
96 #endif
97 #if defined(HAVE_NETINET_IP_ICMP_H)
98 #include <netinet/ip_icmp.h>
99 #endif
100 #if defined(HAVE_NETINET_TCP_H)
101 #include <netinet/tcp.h>
102 #endif
103 #if defined(HAVE_NETINET_SCTP_H)
104 #include <netinet/sctp.h>
105 #endif
106
107 #if defined(HAVE_AIO_H)
108 #include <aio.h>
109 #endif
110
111 #if defined(HAVE_COMPLEX_H)
112 #include <complex.h>
113 #endif
114
115 #if defined(HAVE_CRYPT_H)
116 #include <crypt.h>
117 #endif
118
119 #if defined(HAVE_FEATURES_H)
120 #include <features.h>
121 #endif
122
123 #if defined(HAVE_FENV_H)
124 #include <fenv.h>
125 #endif
126
127 #if defined(HAVE_FLOAT_H)
128 #include <float.h>
129 #endif
130
131 #if defined(HAVE_GRP_H)
132 #include <grp.h>
133 #endif
134
135 #if defined(HAVE_JUDY_H)
136 #include <Judy.h>
137 #endif
138
139 #if defined(HAVE_INTEL_IPSEC_MB_H)
140 #include <intel-ipsec-mb.h>
141 #endif
142
143 #if defined(HAVE_KEYUTILS_H)
144 #include <keyutils.h>
145 #endif
146
147 #if defined(HAVE_LIBAIO_H)
148 #include <libaio.h>
149 #endif
150
151 #if defined(HAVE_LIBGEN_H)
152 #include <libgen.h>
153 #endif
154
155 #if defined(HAVE_LIBKMOD_H)
156 #include <libkmod.h>
157 #endif
158
159 #if defined(HAVE_LINK_H)
160 #include <link.h>
161 #endif
162
163 #if defined(HAVE_LOCALE_H)
164 #include <locale.h>
165 #endif
166
167 #if defined(HAVE_MALLOC_H)
168 #include <malloc.h>
169 #endif
170
171 #if defined(HAVE_MNTENT_H)
172 #include <mntent.h>
173 #endif
174
175 #if defined(HAVE_MQUEUE_H)
176 #include <mqueue.h>
177 #endif
178
179 #if defined(HAVE_POLL_H)
180 #include <poll.h>
181 #endif
182
183 #if defined(HAVE_LIB_PTHREAD)
184 #include <pthread.h>
185 #endif
186
187 #if defined(HAVE_SEMAPHORE_H)
188 #include <semaphore.h>
189 #endif
190
191 #if defined(HAVE_SOUND_ASOUND_H)
192 #include <sound/asound.h>
193 #endif
194
195 #if defined(HAVE_SPAWN_H)
196 #include <spawn.h>
197 #endif
198
199 #if defined(HAVE_STRINGS_H)
200 #include <strings.h>
201 #endif
202
203 #if defined(HAVE_SYSLOG_H)
204 #include <syslog.h>
205 #endif
206
207 #if defined(HAVE_TERMIO_H)
208 #include <termio.h>
209 #endif
210
211 #if defined(HAVE_TERMIOS_H)
212 #include <termios.h>
213 #endif
214
215 #if defined(HAVE_UCONTEXT_H)
216 #include <ucontext.h>
217 #endif
218
219 #if defined(HAVE_USTAT_H)
220 #if defined(__sun__)
221 /* ustat and long file support on sun does not build */
222 #undef HAVE_USTAT_H
223 #else
224 #include <ustat.h>
225 #endif
226 #endif
227
228 #if defined(HAVE_UTIME_H)
229 #include <utime.h>
230 #endif
231
232 #if defined(HAVE_WCHAR)
233 #include <wchar.h>
234 #endif
235
236 #if defined(HAVE_XMMINTRIN_H)
237 #include <xmmintrin.h>
238 #endif
239
240 #if defined(HAVE_SYS_TREE_H)
241 #include <sys/tree.h>
242 #endif
243
244 #if defined(HAVE_BSD_STDLIB_H)
245 #include <bsd/stdlib.h>
246 #endif
247
248 #if defined(HAVE_BSD_STRING_H)
249 #include <bsd/string.h>
250 #endif
251
252 #if defined(HAVE_BSD_SYS_TREE_H)
253 #include <bsd/sys/tree.h>
254 #endif
255
256 #if defined(HAVE_BSD_UNISTD_H)
257 #include <bsd/unistd.h>
258 #endif
259
260 #if defined(HAVE_BSD_WCHAR)
261 #include <bsd/wchar.h>
262 #endif
263
264 #if defined(HAVE_ASM_PRCTL_H)
265 #include <asm/prctl.h>
266 #endif
267
268 #if defined(HAVE_ASM_MTRR_H)
269 #include <asm/mtrr.h>
270 #endif
271
272 #if defined(HAVE_MODIFY_LDT)
273 #include <asm/ldt.h>
274 #endif
275
276 /*
277 * Various sys include files
278 */
279 #include <sys/ioctl.h>
280 #include <sys/file.h>
281 #include <sys/mman.h>
282 #include <sys/resource.h>
283 #include <sys/socket.h>
284 #include <sys/stat.h>
285 #include <sys/time.h>
286 #include <sys/times.h>
287 #include <sys/types.h>
288 #include <sys/wait.h>
289
290 #if defined(HAVE_SYS_APPARMOR_H)
291 #include <sys/apparmor.h>
292 #endif
293
294 #if defined(HAVE_SYS_AUXV_H)
295 #include <sys/auxv.h>
296 #endif
297
298 #if defined(HAVE_SYS_CAPABILITY_H)
299 #include <sys/capability.h>
300 #endif
301
302 #if defined(HAVE_SYS_EPOLL_H)
303 #include <sys/epoll.h>
304 #endif
305
306 #if defined(HAVE_SYS_EVENTFD_H)
307 #include <sys/eventfd.h>
308 #endif
309
310 #if defined(HAVE_SYS_FANOTIFY_H)
311 #include <sys/fanotify.h>
312 #endif
313
314 #if defined(HAVE_SYS_FSUID_H)
315 #include <sys/fsuid.h>
316 #endif
317
318 #if defined(HAVE_SYS_IO_H)
319 #include <sys/io.h>
320 #endif
321
322 #if defined(HAVE_SYS_IPC_H)
323 #include <sys/ipc.h>
324 #endif
325
326 #if defined(HAVE_SYS_INOTIFY_H)
327 #include <sys/inotify.h>
328 #endif
329
330 #if defined(HAVE_SYS_LOADAVG_H)
331 #include <sys/loadavg.h>
332 #endif
333
334 #if defined(HAVE_SYS_MOUNT_H)
335 #include <sys/mount.h>
336 #endif
337
338 #if defined(HAVE_SYS_MSG_H)
339 #include <sys/msg.h>
340 #endif
341
342 #if defined(HAVE_SYS_PARAM_H)
343 #include <sys/param.h>
344 #endif
345
346 #if defined(HAVE_SYS_PERSONALITY_H)
347 #include <sys/personality.h>
348 #endif
349
350 #if defined(HAVE_SYS_PRCTL_H)
351 #include <sys/prctl.h>
352 #endif
353
354 #if defined(HAVE_SYS_PLATFORM_PPC_H)
355 #include <sys/platform/ppc.h>
356 #endif
357
358 #if defined(HAVE_PTRACE)
359 #include <sys/ptrace.h>
360 #endif
361
362 #if defined(HAVE_SYS_QUEUE_H)
363 #include <sys/queue.h>
364 #endif
365
366 #if defined(HAVE_SYS_QUOTA_H)
367 #include <sys/quota.h>
368 #endif
369
370 #if defined(__APPLE__)
371 #include <sys/random.h>
372 #endif
373
374 #if defined(HAVE_SYS_SELECT_H)
375 #include <sys/select.h>
376 #endif
377
378 #if defined(HAVE_SYS_SYSCTL_H) && \
379 !defined(__linux__)
380 #include <sys/sysctl.h>
381 #endif
382
383 #if defined(HAVE_SEM_SYSV)
384 #include <sys/sem.h>
385 #endif
386
387 #if defined(HAVE_SYS_SENDFILE_H)
388 #include <sys/sendfile.h>
389 #endif
390
391 #if defined(HAVE_SYS_SHM_H)
392 #include <sys/shm.h>
393 #endif
394
395 #if defined(HAVE_SYS_SIGNALFD_H)
396 #include <sys/signalfd.h>
397 #endif
398
399 #if defined(HAVE_SYS_STATFS_H)
400 #include <sys/statfs.h>
401 #endif
402
403 #if defined(HAVE_SYS_STATVFS_H)
404 #include <sys/statvfs.h>
405 #endif
406
407 #if defined(__sun__)
408 /* Disable for SunOs/Solaris because */
409 #undef HAVE_SYS_SWAP_H
410 #endif
411 #if defined(HAVE_SYS_SWAP_H)
412 #include <sys/swap.h>
413 #endif
414
415 #if defined(HAVE_SYSCALL_H)
416 #include <sys/syscall.h>
417 #endif
418
419 #if defined(HAVE_SYS_SYSINFO_H)
420 #include <sys/sysinfo.h>
421 #if defined(__GNUC__) && \
422 !defined(__GLIBC__)
423 /* Suppress kernel sysinfo to avoid collision with musl */
424 #define _LINUX_SYSINFO_H
425 #endif
426 #endif
427
428 #if defined(HAVE_SYS_SYSMACROS_H)
429 #include <sys/sysmacros.h>
430 #endif
431
432 #if defined(HAVE_SYS_TIMEX_H)
433 #include <sys/timex.h>
434 #endif
435
436 #if defined(HAVE_SYS_TIMERFD_H)
437 #include <sys/timerfd.h>
438 #endif
439
440 #if defined(HAVE_SYS_UCRED_H)
441 #include <sys/ucred.h>
442 #endif
443
444 #if defined(HAVE_SYS_UIO_H)
445 #include <sys/uio.h>
446 #endif
447
448 #if defined(HAVE_SYS_UN_H)
449 #include <sys/un.h>
450 #endif
451
452 #if defined(HAVE_SYS_UTSNAME_H)
453 #include <sys/utsname.h>
454 #endif
455
456 #if defined(HAVE_SYS_VFS_H)
457 #include <sys/vfs.h>
458 #endif
459
460 #if defined(HAVE_SYS_VMMETER_H)
461 #include <sys/vmmeter.h>
462 #endif
463
464 /*
465 * SCSI related headers
466 */
467 #if defined(HAVE_SCSI_SCSI_H)
468 #include <scsi/scsi.h>
469 #endif
470
471 #if defined(HAVE_SCSI_SCSI_IOCTL_H)
472 #include <scsi/scsi_ioctl.h>
473 #endif
474
475 #if defined(HAVE_SCSI_SG_H)
476 #include <scsi/sg.h>
477 #endif
478
479 /*
480 * Linux specific includes
481 */
482 #if defined(HAVE_LINUX_ANDROID_BINDER_H)
483 #include <linux/android/binder.h>
484 #endif
485
486 #if defined(HAVE_LINUX_ANDROID_BINDERFS_H)
487 #include <linux/android/binderfs.h>
488 #endif
489
490 #if defined(HAVE_LINUX_AUDIT_H)
491 #include <linux/audit.h>
492 #endif
493
494 #if defined(HAVE_LINUX_CDROM_H)
495 #include <linux/cdrom.h>
496 #endif
497
498 #if defined(HAVE_LINUX_CN_PROC_H)
499 #include <linux/cn_proc.h>
500 #endif
501
502 #if defined(HAVE_LINUX_CONNECTOR_H)
503 #include <linux/connector.h>
504 #endif
505
506 #if defined(HAVE_LINUX_DM_IOCTL_H)
507 #include <linux/dm-ioctl.h>
508 #endif
509
510 #if defined(HAVE_LINUX_FD_H)
511 #include <linux/fd.h>
512 #endif
513
514 #if defined(HAVE_LINUX_GENETLINK_H)
515 #include <linux/genetlink.h>
516 #endif
517
518 #if defined(HAVE_LINUX_HDREG_H)
519 #include <linux/hdreg.h>
520 #endif
521
522 #if defined(HAVE_LINUX_IF_ALG_H)
523 #include <linux/if_alg.h>
524 #endif
525
526 #if defined(HAVE_LINUX_IF_PACKET_H)
527 #include <linux/if_packet.h>
528 #endif
529
530 #if defined(HAVE_LINUX_IF_TUN_H)
531 #include <linux/if_tun.h>
532 #endif
533
534 #if defined(HAVE_LINUX_IO_URING_H)
535 #include <linux/io_uring.h>
536 #endif
537
538 #if defined(HAVE_LINUX_FIEMAP_H)
539 #include <linux/fiemap.h>
540 #endif
541
542 #if defined(HAVE_LINUX_FILTER_H)
543 #include <linux/filter.h>
544 #endif
545
546 #if defined(HAVE_LINUX_FS_H)
547 #include <linux/fs.h>
548 #endif
549
550 #if defined(HAVE_LINUX_FSVERITY_H)
551 #include <linux/fsverity.h>
552 #endif
553
554 #if defined(HAVE_LINUX_FUTEX_H)
555 #include <linux/futex.h>
556 #endif
557
558 #if defined(HAVE_LINUX_HPET_H)
559 #include <linux/hpet.h>
560 #endif
561
562 #if defined(HAVE_LINUX_KD_H)
563 #include <linux/kd.h>
564 #endif
565
566 #if defined(HAVE_LINUX_LANDLOCK_H)
567 #include <linux/landlock.h>
568 #endif
569
570 #if defined(HAVE_LINUX_LOOP_H)
571 #include <linux/loop.h>
572 #endif
573
574 #if defined(HAVE_LINUX_MEDIA_H)
575 #include <linux/media.h>
576 #endif
577
578 #if defined(HAVE_LINUX_MEMBARRIER_H)
579 #include <linux/membarrier.h>
580 #endif
581
582 #if defined(HAVE_LINUX_MEMPOLICY_H)
583 #include <linux/mempolicy.h>
584 #endif
585
586 #if defined(HAVE_LINUX_NETLINK_H)
587 #include <linux/netlink.h>
588 #endif
589
590 #if defined(HAVE_LINUX_OPENAT2_H)
591 #include <linux/openat2.h>
592 #endif
593
594 #if defined(HAVE_LINUX_PCI_H)
595 #include <linux/pci.h>
596 #endif
597
598 #if defined(HAVE_LINUX_PERF_EVENT_H)
599 #include <linux/perf_event.h>
600 #endif
601
602 #if defined(HAVE_LINUX_PTP_CLOCK_H)
603 #include <linux/ptp_clock.h>
604 #endif
605
606 #if defined(HAVE_LINUX_POSIX_TYPES_H)
607 #include <linux/posix_types.h>
608 #endif
609
610 #if defined(HAVE_LINUX_PPDEV_H)
611 #include <linux/ppdev.h>
612 #endif
613
614 #if defined(HAVE_LINUX_RANDOM_H)
615 #include <linux/random.h>
616 #endif
617
618 #if defined(HAVE_LINUX_RSEQ_H)
619 #include <linux/rseq.h>
620 #endif
621
622 #if defined(HAVE_LINUX_RTC_H)
623 #include <linux/rtc.h>
624 #endif
625
626 #if defined(HAVE_LINUX_RTNETLINK_H)
627 #include <linux/rtnetlink.h>
628 #endif
629
630 #if defined(HAVE_LINUX_SECCOMP_H)
631 #include <linux/seccomp.h>
632 #endif
633
634 #if defined(HAVE_LINUX_SERIAL_H)
635 #include <linux/serial.h>
636 #endif
637
638 #if defined(HAVE_LINUX_SOCK_DIAG_H)
639 #include <linux/sock_diag.h>
640 #endif
641
642 #if defined(HAVE_LINUX_SOCKET_H)
643 #include <linux/socket.h>
644 #endif
645
646 #if defined(HAVE_LINUX_SOCKIOS_H)
647 #include <linux/sockios.h>
648 #endif
649
650 #if defined(HAVE_LINUX_SYSCTL_H)
651 #include <linux/sysctl.h>
652 #endif
653
654 #if defined(HAVE_LINUX_TASKSTATS_H)
655 #include <linux/taskstats.h>
656 #endif
657
658 #if defined(HAVE_LINUX_UDP_H)
659 #include <linux/udp.h>
660 #endif
661
662 #if defined(HAVE_LINUX_UNIX_DIAG_H)
663 #include <linux/unix_diag.h>
664 #endif
665
666 #if defined(HAVE_LINUX_USERFAULTFD_H)
667 #include <linux/userfaultfd.h>
668 #endif
669
670 #if defined(HAVE_LINUX_VERSION_H)
671 #include <linux/version.h>
672 #endif
673
674 #if defined(HAVE_LINUX_VIDEODEV2_H)
675 #include <linux/videodev2.h>
676 #endif
677
678 #if defined(HAVE_LINUX_VT_H)
679 #include <linux/vt.h>
680 #endif
681
682 #if defined(HAVE_LINUX_WATCHDOG_H)
683 #include <linux/watchdog.h>
684 #endif
685
686 /*
687 * We want sys/xattr.h in preference
688 * to the older attr/xattr.h if both
689 * are available
690 */
691 #if defined(HAVE_SYS_XATTR_H)
692 #include <sys/xattr.h>
693 #undef HAVE_ATTR_XATTR_H
694 #elif defined(HAVE_ATTR_XATTR_H)
695 #include <attr/xattr.h>
696 #endif
697 /* Sanity check */
698 #if defined(HAVE_SYS_XATTR_H) && \
699 defined(HAVE_ATTR_XATTR_H)
700 #error cannot have both HAVE_SYS_XATTR_H and HAVE_ATTR_XATTR_H
701 #endif
702
703 #if defined(HAVE_LIB_DL)
704 #include <dlfcn.h>
705 #include <gnu/lib-names.h>
706 #endif
707
708 /*
709 * Various system calls not included in libc (yet)
710 */
711 #if defined(__linux__)
712
713 #if defined(__NR_add_key)
714 #define HAVE_ADD_KEY
715 #endif
716
717 #if defined(__NR_getcpu)
718 #define HAVE_GETCPU
719 #endif
720
721 #if defined(__NR_getdents)
722 #define HAVE_GETDENTS
723 #endif
724
725 #if defined(__NR_getdents64)
726 #define HAVE_GETDENTS64
727 #endif
728
729 #if defined(__NR_get_thread_area)
730 #define HAVE_GET_THREAD_AREA
731 #endif
732
733 #if defined(__NR_gettid)
734 #define HAVE_GETTID
735 #endif
736
737 #if defined(__NR_get_robust_list)
738 #define HAVE_GET_ROBUST_LIST
739 #endif
740
741 #if defined(__NR_ioprio_get)
742 #define HAVE_IOPRIO_GET
743 #endif
744
745 #if defined(__NR_ioprio_set)
746 #define HAVE_IOPRIO_SET
747 #endif
748
749 #if defined(__NR_kcmp)
750 #define HAVE_KCMP
751 #endif
752
753 #if defined(__NR_keyctl)
754 #define HAVE_KEYCTL
755 #endif
756
757 #if defined(__NR_membarrier)
758 #define HAVE_MEMBARRIER
759 #endif
760
761 #if defined(__NR_mmap2)
762 #define HAVE_MMAP2
763 #endif
764
765 #if defined(__NR_pkey_get)
766 #define HAVE_PKEY_GET
767 #endif
768
769 #if defined(__NR_pkey_set)
770 #define HAVE_PKEY_SET
771 #endif
772
773 #if defined(__NR_request_key)
774 #define HAVE_REQUEST_KEY
775 #endif
776
777 #if defined(__NR_sched_getattr)
778 #define HAVE_SCHED_GETATTR
779 #endif
780
781 #if defined(__NR_sched_setattr)
782 #define HAVE_SCHED_SETATTR
783 #endif
784
785 #if defined(__NR_set_robust_list)
786 #define HAVE_SET_ROBUST_LIST
787 #endif
788
789 #if defined(__NR_set_thread_area)
790 #define HAVE_SET_THREAD_AREA
791 #endif
792
793 #if defined(__NR_syslog)
794 #define HAVE_SYSLOG
795 #endif
796
797 #if defined(__NR_userfaultfd)
798 #define HAVE_USERFAULTFD
799 #endif
800
801 #endif
802
803 #include "stress-version.h"
804
805 /*
806 * BeagleBoneBlack with 4.1.15 kernel does not
807 * define the following, these should be defined
808 * in linux/posix_types.h - define them if they
809 * don't exist.
810 */
811 #if !defined(HAVE_KERNEL_LONG_T)
812 typedef long int __kernel_long_t;
813 #endif
814 #if !defined(HAVE_KERNEL_ULONG_T)
815 typedef unsigned long int __kernel_ulong_t;
816 #endif
817
818 #define STRESS_BIT_U(shift) (1U << shift)
819 #define STRESS_BIT_UL(shift) (1UL << shift)
820 #define STRESS_BIT_ULL(shift) (1ULL << shift)
821
822 /*
823 * cacheflush(2) cache options
824 */
825 #ifdef ICACHE
826 #define SHIM_ICACHE (ICACHE)
827 #else
828 #define SHIM_ICACHE (1 << 0)
829 #endif
830
831 #ifdef DCACHE
832 #define SHIM_DCACHE (DCACHE)
833 #else
834 #define SHIM_DCACHE (1 << 1)
835 #endif
836
837 #if !defined(EXIT_SUCCESS)
838 #define EXIT_SUCESSS (0)
839 #endif
840 #if !defined(EXIT_FAILURE)
841 #define EXIT_FAILURE (1)
842 #endif
843 #define EXIT_NOT_SUCCESS (2)
844 #define EXIT_NO_RESOURCE (3)
845 #define EXIT_NOT_IMPLEMENTED (4)
846 #define EXIT_SIGNALED (5)
847 #define EXIT_BY_SYS_EXIT (6)
848 #define EXIT_METRICS_UNTRUSTWORTHY (7)
849
850 /*
851 * Stressor run states
852 */
853 #define STRESS_STATE_START (0)
854 #define STRESS_STATE_INIT (1)
855 #define STRESS_STATE_RUN (2)
856 #define STRESS_STATE_DEINIT (3)
857 #define STRESS_STATE_STOP (4)
858 #define STRESS_STATE_EXIT (5)
859 #define STRESS_STATE_WAIT (6)
860
861 /*
862 * Timing units
863 */
864 #define STRESS_NANOSECOND (1000000000L)
865 #define STRESS_MICROSECOND (1000000L)
866 #define STRESS_MILLISECOND (1000L)
867
868 /*
869 * STRESS_ASSERT(test)
870 * throw compile time error if test not true
871 */
872 #define STRESS_CONCAT(a, b) a ## b
873 #define STRESS_CONCAT_EXPAND(a, b) STRESS_CONCAT(a, b)
874 #define STRESS_ASSERT(expr) \
875 enum { STRESS_CONCAT_EXPAND(STRESS_ASSERT_AT_LINE_, __LINE__) = \
876 1 / !!(expr) };
877
878 #define STRESS_MINIMUM(a,b) (((a) < (b)) ? (a) : (b))
879 #define STRESS_MAXIMUM(a,b) (((a) > (b)) ? (a) : (b))
880
881 /* NetBSD does not define MAP_ANONYMOUS */
882 #if defined(MAP_ANON) && \
883 !defined(MAP_ANONYMOUS)
884 #define MAP_ANONYMOUS MAP_ANON
885 #endif
886
887 /* GNU HURD and other systems that don't define PATH_MAX */
888 #ifndef PATH_MAX
889 #define PATH_MAX (4096)
890 #endif
891
892 /*
893 * making local static fixes globbering warnings
894 */
895 #define NOCLOBBER static
896
897 #if (_BSD_SOURCE || _SVID_SOURCE || !defined(__gnu_hurd__))
898 #define STRESS_PAGE_IN
899 #endif
900
901 #define STRESS_TRY_OPEN_OK (0) /* File can be opened */
902 #define STRESS_TRY_OPEN_FORK_FAIL (1) /* Try failed, e.g. can't fork */
903 #define STRESS_TRY_OPEN_WAIT_FAIL (2) /* Wait on child open failed */
904 #define STRESS_TRY_OPEN_EXIT_FAIL (3) /* Can't get _exit() status */
905 #define STRESS_TRY_OPEN_FAIL (4) /* Can't open file */
906 #define STRESS_TRY_AGAIN (5) /* Device busy, try again */
907
908 #define STRESS_FD_MAX (65536) /* Max fds if we can't figure it out */
909 #define STRESS_PROCS_MAX (8192) /* Max number of processes per stressor */
910
911 #define DCCP_BUF (1024) /* DCCP I/O buffer size */
912 #define SOCKET_BUF (8192) /* Socket I/O buffer size */
913 #define UDP_BUF (1024) /* UDP I/O buffer size */
914 #define SOCKET_PAIR_BUF (64) /* Socket pair I/O buffer size */
915
916 #define ABORT_FAILURES (5) /* Number of failures before we abort */
917
918 /* debug output bitmasks */
919 #define PR_ERROR STRESS_BIT_ULL(0) /* Print errors */
920 #define PR_INFO STRESS_BIT_ULL(1) /* Print info */
921 #define PR_DEBUG STRESS_BIT_ULL(2) /* Print debug */
922 #define PR_FAIL STRESS_BIT_ULL(3) /* Print test failure message */
923 #define PR_ALL (PR_ERROR | PR_INFO | PR_DEBUG | PR_FAIL)
924
925 /* Option bit masks */
926 #define OPT_FLAGS_DRY_RUN STRESS_BIT_ULL(4) /* Don't actually run */
927 #define OPT_FLAGS_METRICS STRESS_BIT_ULL(5) /* Dump metrics at end */
928 #define OPT_FLAGS_RANDOM STRESS_BIT_ULL(6) /* Randomize */
929 #define OPT_FLAGS_SET STRESS_BIT_ULL(7) /* Set if user specifies stress procs */
930 #define OPT_FLAGS_KEEP_NAME STRESS_BIT_ULL(8) /* Keep stress names to stress-ng */
931 #define OPT_FLAGS_METRICS_BRIEF STRESS_BIT_ULL(9) /* dump brief metrics */
932 #define OPT_FLAGS_VERIFY STRESS_BIT_ULL(10) /* verify mode */
933 #define OPT_FLAGS_MMAP_MADVISE STRESS_BIT_ULL(11) /* enable random madvise settings */
934 #define OPT_FLAGS_MMAP_MINCORE STRESS_BIT_ULL(12) /* mincore force pages into mem */
935 #define OPT_FLAGS_TIMES STRESS_BIT_ULL(13) /* user/system time summary */
936 #define OPT_FLAGS_HDD_SYNC STRESS_BIT_ULL(14) /* HDD O_SYNC */
937 #define OPT_FLAGS_HDD_DSYNC STRESS_BIT_ULL(15) /* HDD O_DYNC */
938 #define OPT_FLAGS_HDD_DIRECT STRESS_BIT_ULL(16) /* HDD O_DIRECT */
939 #define OPT_FLAGS_HDD_NOATIME STRESS_BIT_ULL(17) /* HDD O_NOATIME */
940 #define OPT_FLAGS_MINIMIZE STRESS_BIT_ULL(18) /* Minimize */
941 #define OPT_FLAGS_MAXIMIZE STRESS_BIT_ULL(19) /* Maximize */
942 #define OPT_FLAGS_SYSLOG STRESS_BIT_ULL(20) /* log test progress to syslog */
943 #define OPT_FLAGS_AGGRESSIVE STRESS_BIT_ULL(21) /* aggressive mode enabled */
944 #define OPT_FLAGS_ALL STRESS_BIT_ULL(22) /* --all mode */
945 #define OPT_FLAGS_SEQUENTIAL STRESS_BIT_ULL(23) /* --sequential mode */
946 #define OPT_FLAGS_PERF_STATS STRESS_BIT_ULL(24) /* --perf stats mode */
947 #define OPT_FLAGS_LOG_BRIEF STRESS_BIT_ULL(25) /* --log-brief */
948 #define OPT_FLAGS_THERMAL_ZONES STRESS_BIT_ULL(26) /* --tz thermal zones */
949 #define OPT_FLAGS_SOCKET_NODELAY STRESS_BIT_ULL(27) /* --sock-nodelay */
950 #define OPT_FLAGS_IGNITE_CPU STRESS_BIT_ULL(28) /* --cpu-ignite */
951 #define OPT_FLAGS_PATHOLOGICAL STRESS_BIT_ULL(29) /* --pathological */
952 #define OPT_FLAGS_NO_RAND_SEED STRESS_BIT_ULL(30) /* --no-rand-seed */
953 #define OPT_FLAGS_THRASH STRESS_BIT_ULL(31) /* --thrash */
954 #define OPT_FLAGS_OOMABLE STRESS_BIT_ULL(32) /* --oomable */
955 #define OPT_FLAGS_ABORT STRESS_BIT_ULL(33) /* --abort */
956 #define OPT_FLAGS_CPU_ONLINE_ALL STRESS_BIT_ULL(34) /* --cpu-online-all */
957 #define OPT_FLAGS_TIMESTAMP STRESS_BIT_ULL(35) /* --timestamp */
958 #define OPT_FLAGS_DEADLINE_GRUB STRESS_BIT_ULL(36) /* --sched-reclaim */
959 #define OPT_FLAGS_FTRACE STRESS_BIT_ULL(37) /* --ftrace */
960 #define OPT_FLAGS_SEED STRESS_BIT_ULL(38) /* --seed */
961 #define OPT_FLAGS_SKIP_SILENT STRESS_BIT_ULL(39) /* --skip-silent */
962 #define OPT_FLAGS_SMART STRESS_BIT_ULL(40) /* --smart */
963 #define OPT_FLAGS_NO_OOM_ADJUST STRESS_BIT_ULL(41) /* --no-oom-adjust */
964
965 #define OPT_FLAGS_MINMAX_MASK \
966 (OPT_FLAGS_MINIMIZE | OPT_FLAGS_MAXIMIZE)
967
968 /* Aggressive mode flags */
969 #define OPT_FLAGS_AGGRESSIVE_MASK \
970 (OPT_FLAGS_MMAP_MADVISE | \
971 OPT_FLAGS_MMAP_MINCORE | \
972 OPT_FLAGS_HDD_SYNC | \
973 OPT_FLAGS_HDD_DSYNC | \
974 OPT_FLAGS_HDD_DIRECT | \
975 OPT_FLAGS_AGGRESSIVE | \
976 OPT_FLAGS_IGNITE_CPU)
977
978 /* Stressor classes */
979 #define CLASS_CPU STRESS_BIT_UL(0) /* CPU only */
980 #define CLASS_MEMORY STRESS_BIT_UL(1) /* Memory thrashers */
981 #define CLASS_CPU_CACHE STRESS_BIT_UL(2) /* CPU cache */
982 #define CLASS_IO STRESS_BIT_UL(3) /* I/O read/writes etc */
983 #define CLASS_NETWORK STRESS_BIT_UL(4) /* Network, sockets, etc */
984 #define CLASS_SCHEDULER STRESS_BIT_UL(5) /* Scheduling */
985 #define CLASS_VM STRESS_BIT_UL(6) /* VM stress, big memory, swapping */
986 #define CLASS_INTERRUPT STRESS_BIT_UL(7) /* interrupt floods */
987 #define CLASS_OS STRESS_BIT_UL(8) /* generic OS tests */
988 #define CLASS_PIPE_IO STRESS_BIT_UL(9) /* pipe I/O */
989 #define CLASS_FILESYSTEM STRESS_BIT_UL(10) /* file system */
990 #define CLASS_DEV STRESS_BIT_UL(11) /* device (null, zero, etc) */
991 #define CLASS_SECURITY STRESS_BIT_UL(12) /* security APIs */
992 #define CLASS_PATHOLOGICAL STRESS_BIT_UL(13) /* can hang a machine */
993
994
995 /* Help information for options */
996 typedef struct {
997 const char *opt_s; /* short option */
998 const char *opt_l; /* long option */
999 const char *description; /* description */
1000 } stress_help_t;
1001
1002 /* native setting types */
1003 typedef enum {
1004 TYPE_ID_UNDEFINED,
1005 TYPE_ID_UINT8,
1006 TYPE_ID_INT8,
1007 TYPE_ID_UINT16,
1008 TYPE_ID_INT16,
1009 TYPE_ID_UINT32,
1010 TYPE_ID_INT32,
1011 TYPE_ID_UINT64,
1012 TYPE_ID_INT64,
1013 TYPE_ID_SIZE_T,
1014 TYPE_ID_SSIZE_T,
1015 TYPE_ID_UINT,
1016 TYPE_ID_INT,
1017 TYPE_ID_ULONG,
1018 TYPE_ID_LONG,
1019 TYPE_ID_OFF_T,
1020 TYPE_ID_STR,
1021 TYPE_ID_BOOL,
1022 TYPE_ID_UINTPTR_T
1023 } stress_type_id_t;
1024
1025 typedef struct stress_stressor_info *stress_pstressor_info_t;
1026
1027 /*
1028 * Per ELISA request, we have a duplicated counter
1029 * and run_ok flag in a different shared memory region
1030 * so we can sanity check these just in case the stats
1031 * have got corrupted.
1032 */
1033 typedef struct {
1034 struct {
1035 uint64_t counter; /* Copy of stats counter */
1036 bool run_ok; /* Copy of run_ok */
1037 } data;
1038 uint32_t hash; /* Hash of data */
1039 } stress_checksum_t;
1040
1041 /* settings for storing opt arg parsed data */
1042 typedef struct stress_setting {
1043 struct stress_setting *next; /* next setting in list */
1044 stress_pstressor_info_t proc;
1045 char *name; /* name of setting */
1046 stress_type_id_t type_id; /* setting type */
1047 bool global; /* true if global */
1048 union { /* setting value */
1049 uint8_t uint8;
1050 int8_t int8;
1051 uint16_t uint16;
1052 int16_t int16;
1053 uint32_t uint32;
1054 int32_t int32;
1055 uint64_t uint64;
1056 int64_t int64;
1057 size_t size;
1058 ssize_t ssize;
1059 unsigned int uint;
1060 signed int sint;
1061 unsigned long ulong;
1062 signed long slong;
1063 off_t off;
1064 char *str;
1065 bool boolean;
1066 uintptr_t uintptr;/* for func pointers */
1067 } u;
1068 } stress_setting_t;
1069
1070 typedef union {
1071 volatile uint8_t uint8_val;
1072 volatile uint16_t uint16_val;
1073 volatile uint32_t uint32_val;
1074 volatile uint64_t uint64_val;
1075 #if defined(HAVE_INT128_T)
1076 volatile __uint128_t uint128_val;
1077 #endif
1078 volatile float float_val;
1079 volatile double double_val;
1080 volatile long double long_double_val;
1081 volatile void *void_ptr_val;
1082 } stress_put_val_t;
1083
1084 /* Network domains flags */
1085 #define DOMAIN_INET (0x00000001) /* AF_INET */
1086 #define DOMAIN_INET6 (0x00000002) /* AF_INET6 */
1087 #define DOMAIN_UNIX (0x00000004) /* AF_UNIX */
1088
1089 #define DOMAIN_INET_ALL (DOMAIN_INET | DOMAIN_INET6)
1090 #define DOMAIN_ALL (DOMAIN_INET | DOMAIN_INET6 | DOMAIN_UNIX)
1091
1092 /* Large prime to stride around large VM regions */
1093 #define PRIME_64 (0x8f0000000017116dULL)
1094
1095 typedef uint32_t stress_class_t;
1096
1097 typedef struct {
1098 void *page_none; /* mmap'd PROT_NONE page */
1099 void *page_ro; /* mmap'd PROT_RO page */
1100 void *page_wo; /* mmap'd PROT_WO page */
1101 } stress_mapped_t;
1102
1103 #define STRESS_MISC_STATS_MAX (10)
1104
1105 typedef struct {
1106 char description[32];
1107 double value;
1108 } stress_misc_stats_t;
1109
1110 /* stressor args */
1111 typedef struct {
1112 uint64_t *counter; /* stressor counter */
1113 bool *counter_ready; /* counter can be read */
1114 const char *name; /* stressor name */
1115 uint64_t max_ops; /* max number of bogo ops */
1116 const uint32_t instance; /* stressor instance # */
1117 const uint32_t num_instances; /* number of instances */
1118 pid_t pid; /* stressor pid */
1119 pid_t ppid; /* stressor ppid */
1120 size_t page_size; /* page size */
1121 stress_mapped_t *mapped; /* mmap'd pages, addr of g_shared mapped */
1122 stress_misc_stats_t *misc_stats;/* misc per stressor stats */
1123 } stress_args_t;
1124
1125 typedef struct {
1126 const int opt; /* optarg option*/
1127 int (*opt_set_func)(const char *opt); /* function to set it */
1128 } stress_opt_set_func_t;
1129
1130 /* stressor information */
1131 typedef struct {
1132 int (*stressor)(const stress_args_t *args); /* stressor function */
1133 int (*supported)(const char *name); /* return 0 = supported, -1, not */
1134 void (*init)(void); /* stressor init, NULL = ignore */
1135 void (*deinit)(void); /* stressor de-init, NULL = ignore */
1136 void (*set_default)(void); /* default set-up */
1137 void (*set_limit)(uint64_t max);/* set limits */
1138 const stress_class_t class; /* stressor class */
1139 const stress_opt_set_func_t *opt_set_funcs; /* option functions */
1140 const stress_help_t *help; /* stressor help options */
1141 } stressor_info_t;
1142
1143 /* pthread wrapped stress_args_t */
1144 typedef struct {
1145 const stress_args_t *args; /* Stress test args */
1146 void *data; /* Per thread private data */
1147 int pthread_ret; /* Per thread return value */
1148 } stress_pthread_args_t;
1149
1150 /* string hash linked list type */
1151 typedef struct stress_hash {
1152 struct stress_hash *next; /* next hash item */
1153 } stress_hash_t;
1154
1155 /* string hash table */
1156 typedef struct {
1157 stress_hash_t **table; /* hash table */
1158 size_t n; /* number of hash items in table */
1159 } stress_hash_table_t;
1160
1161 /* vmstat information */
1162 typedef struct { /* vmstat column */
1163 uint64_t procs_running; /* r */
1164 uint64_t procs_blocked; /* b */
1165 uint64_t swap_total; /* swpd info, total */
1166 uint64_t swap_free; /* swpd info, free */
1167 uint64_t swap_used; /* swpd used = total - free */
1168 uint64_t memory_free; /* free */
1169 uint64_t memory_buff; /* buff */
1170 uint64_t memory_cache; /* cache */
1171 uint64_t swap_in; /* si */
1172 uint64_t swap_out; /* so */
1173 uint64_t block_in; /* bi */
1174 uint64_t block_out; /* bo */
1175 uint64_t interrupt; /* in */
1176 uint64_t context_switch; /* cs */
1177 uint64_t user_time; /* us */
1178 uint64_t system_time; /* sy */
1179 uint64_t idle_time; /* id */
1180 uint64_t wait_time; /* wa */
1181 uint64_t stolen_time; /* st */
1182 } stress_vmstat_t;
1183
1184 /* iostat information, from /sys/block/$dev/stat */
1185 typedef struct {
1186 uint64_t read_io; /* number of read I/Os processed */
1187 uint64_t read_merges; /* number of read I/Os merged with in-queue I/O */
1188 uint64_t read_sectors; /* number of sectors read */
1189 uint64_t read_ticks; /* total wait time for read requests */
1190 uint64_t write_io; /* number of write I/Os processed */
1191 uint64_t write_merges; /* number of write I/Os merged with in-queue I/O */
1192 uint64_t write_sectors; /* number of sectors written */
1193 uint64_t write_ticks; /* total wait time for write requests */
1194 uint64_t in_flight; /* number of I/Os currently in flight */
1195 uint64_t io_ticks; /* total time this block device has been active */
1196 uint64_t time_in_queue; /* total wait time for all requests */
1197 uint64_t discard_io; /* number of discard I/Os processed */
1198 uint64_t discard_merges; /* number of discard I/Os merged with in-queue I/O */
1199 uint64_t discard_sectors;/* number of sectors discarded */
1200 uint64_t discard_ticks; /* total wait time for discard requests */
1201 } stress_iostat_t;
1202
1203 /* gcc 4.7 and later support vector ops */
1204 #if defined(__GNUC__) && \
1205 NEED_GNUC(4, 7, 0)
1206 #define STRESS_VECTOR 1
1207 #endif
1208
1209 /* gcc 7.0 and later support __attribute__((fallthrough)); */
1210 #if defined(HAVE_ATTRIBUTE_FALLTHROUGH)
1211 #define CASE_FALLTHROUGH __attribute__((fallthrough)) /* Fallthrough */
1212 #else
1213 #define CASE_FALLTHROUGH /* Fallthrough */
1214 #endif
1215
1216 /* no return hint */
1217 #if (defined(__GNUC__) && NEED_GNUC(2, 5, 0)) || \
1218 (defined(__clang__) && NEED_CLANG(3, 0, 0))
1219 #define NORETURN __attribute__ ((noreturn))
1220 #else
1221 #define NORETURN
1222 #endif
1223
1224 /* weak attribute */
1225 #if (defined(__GNUC__) && NEED_GNUC(4, 0, 0)) || \
1226 (defined(__clang__) && NEED_CLANG(3, 4, 0))
1227 #define WEAK __attribute__ ((weak))
1228 #define HAVE_WEAK_ATTRIBUTE
1229 #else
1230 #define WEAK
1231 #endif
1232
1233 /* force inlining hint */
1234 #if (defined(__GNUC__) && NEED_GNUC(3, 4, 0) \
1235 && ((!defined(__s390__) && !defined(__s390x__)) || NEED_GNUC(6, 0, 1))) || \
1236 (defined(__clang__) && NEED_CLANG(3, 0, 0))
1237 #define ALWAYS_INLINE __attribute__ ((always_inline))
1238 #else
1239 #define ALWAYS_INLINE
1240 #endif
1241
1242 /* force no inlining hint */
1243 #if (defined(__GNUC__) && NEED_GNUC(3, 4, 0)) || \
1244 (defined(__clang__) && NEED_CLANG(3, 0, 0))
1245 #define NOINLINE __attribute__ ((noinline))
1246 #else
1247 #define NOINLINE
1248 #endif
1249
1250 /* -O3 attribute support */
1251 #if defined(__GNUC__) && \
1252 !defined(__clang__) && \
1253 NEED_GNUC(4, 6, 0)
1254 #define OPTIMIZE3 __attribute__((optimize("-O3")))
1255 #else
1256 #define OPTIMIZE3
1257 #endif
1258
1259 /* -O1 attribute support */
1260 #if defined(__GNUC__) && \
1261 !defined(__clang__) && \
1262 NEED_GNUC(4, 6, 0)
1263 #define OPTIMIZE1 __attribute__((optimize("-O1")))
1264 #else
1265 #define OPTIMIZE1
1266 #endif
1267
1268 /* -O0 attribute support */
1269 #if defined(__GNUC__) && \
1270 !defined(__clang__) && \
1271 NEED_GNUC(4, 6, 0)
1272 #define OPTIMIZE0 __attribute__((optimize("-O0")))
1273 #else
1274 #define OPTIMIZE0
1275 #endif
1276
1277 /* warn unused attribute */
1278 #if (defined(__GNUC__) && NEED_GNUC(4, 2, 0)) || \
1279 (defined(__clang__) && NEED_CLANG(3, 0, 0))
1280 #define WARN_UNUSED __attribute__((warn_unused_result))
1281 #else
1282 #define WARN_UNUSED
1283 #endif
1284
1285 #if ((defined(__GNUC__) && NEED_GNUC(3, 3, 0)) || \
1286 (defined(__clang__) && NEED_CLANG(3, 0, 0))) && \
1287 !defined(__PCC__)
1288 #define ALIGNED(a) __attribute__((aligned(a)))
1289 #else
1290 #define ALIGNED(a)
1291 #endif
1292
1293 /* Force alignment to nearest 128 bytes */
1294 #if ((defined(__GNUC__) && NEED_GNUC(3, 3, 0)) || \
1295 (defined(__clang__) && NEED_CLANG(3, 0, 0))) && \
1296 defined(HAVE_ALIGNED_128)
1297 #define ALIGN128 ALIGNED(128)
1298 #else
1299 #define ALIGN128
1300 #endif
1301
1302 /* Force alignment to nearest 64 bytes */
1303 #if ((defined(__GNUC__) && NEED_GNUC(3, 3, 0)) || \
1304 (defined(__clang__) && NEED_CLANG(3, 0, 0))) && \
1305 defined(HAVE_ALIGNED_64)
1306 #define ALIGN64 ALIGNED(64)
1307 #else
1308 #define ALIGN64
1309 #endif
1310
1311 #if (defined(__GNUC__) && NEED_GNUC(4, 6, 0)) || \
1312 (defined(__clang__) && NEED_CLANG(3, 0, 0))
1313 #define SECTION(s) __attribute__((__section__(# s)))
1314 #else
1315 #define SECTION(s)
1316 #endif
1317
1318 /* Choose cacheline alignment */
1319 #if defined(ALIGN128)
1320 #define ALIGN_CACHELINE ALIGN128
1321 #else
1322 #define ALIGN_CACHELINE ALIGN64
1323 #endif
1324
1325 /* GCC hot attribute */
1326 #if (defined(__GNUC__) && NEED_GNUC(4, 6, 0)) || \
1327 (defined(__clang__) && NEED_CLANG(3, 3, 0))
1328 #define HOT __attribute__ ((hot))
1329 #else
1330 #define HOT
1331 #endif
1332
1333 /* GCC mlocked data and data section attribute */
1334 #if ((defined(__GNUC__) && NEED_GNUC(4, 6, 0) || \
1335 (defined(__clang__) && NEED_CLANG(3, 0, 0)))) && \
1336 !defined(__sun__) && \
1337 !defined(__APPLE__) && \
1338 !defined(BUILD_STATIC)
1339 #define MLOCKED_TEXT __attribute__((__section__("mlocked_text")))
1340 #define MLOCKED_SECTION 1
1341 #else
1342 #define MLOCKED_TEXT
1343 #endif
1344
1345 /* print format attribute */
1346 #if ((defined(__GNUC__) && NEED_GNUC(3, 2, 0)) || \
1347 (defined(__clang__) && NEED_CLANG(3, 0, 0)))
1348 #define FORMAT(func, a, b) __attribute__((format(func, a, b)))
1349 #else
1350 #define FORMAT(func, a, b)
1351 #endif
1352
1353 /* restrict keyword */
1354 #if defined(HAVE___RESTRICT)
1355 #define RESTRICT __restrict
1356 #else
1357 #define RESTRICT
1358 #endif
1359
1360 /* optimisation on branching */
1361 #if defined(HAVE_BUILTIN_EXPECT)
1362 #define LIKELY(x) __builtin_expect((x),1)
1363 #define UNLIKELY(x) __builtin_expect((x),0)
1364 #else
1365 #define LIKELY(x) (x)
1366 #define UNLIKELY(x) (x)
1367 #endif
1368
1369 #if !defined(HAVE_BUILTIN_PREFETCH) || defined(__PCC__)
1370 /* a fake prefetch var-args no-op */
shim_builtin_prefetch(const void * addr,...)1371 static inline void shim_builtin_prefetch(const void *addr, ...)
1372 {
1373 va_list ap;
1374
1375 va_start(ap, addr);
1376 va_end(ap);
1377 }
1378 #else
1379 #define shim_builtin_prefetch __builtin_prefetch
1380 #endif
1381
1382 #if defined(HAVE_BUILTIN_MEMMOVE)
1383 #define shim_builtin_memmove __builtin_memmove
1384 #else
1385 #define shim_builtin_memmove memmove
1386 #endif
1387
1388 /* use syscall if we can, fallback to vfork otherwise */
1389 #define shim_vfork() g_shared->vfork()
1390
1391 /* do nothing */
1392 #if defined(HAVE_ASM_NOP)
1393 #define FORCE_DO_NOTHING() __asm__ __volatile__("nop;")
1394 #elif defined(HAVE_ASM_NOTHING)
1395 #define FORCE_DO_NOTHING() __asm__ __volatile__("")
1396 #else
1397 #define FORCE_DO_NOTHING() while (0)
1398 #endif
1399
1400 #if defined(__clang__) && \
1401 NEED_CLANG(4, 0, 0) && \
1402 defined(HAVE_PRAGMA)
1403 #define STRESS_PRAGMA_PUSH _Pragma("GCC diagnostic push")
1404 #define STRESS_PRAGMA_POP _Pragma("GCC diagnostic pop")
1405 #define STRESS_PRAGMA_WARN_OFF _Pragma("GCC diagnostic ignored \"-Weverything\"")
1406 #elif defined(__GNUC__) && \
1407 defined(HAVE_PRAGMA) && \
1408 NEED_GNUC(4, 4, 0)
1409 #define STRESS_PRAGMA_PUSH _Pragma("GCC diagnostic push")
1410 #define STRESS_PRAGMA_POP _Pragma("GCC diagnostic pop")
1411 #define STRESS_PRAGMA_WARN_OFF _Pragma("GCC diagnostic ignored \"-Wall\"") \
1412 _Pragma("GCC diagnostic ignored \"-Wextra\"") \
1413 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") \
1414 _Pragma("GCC diagnostic ignored \"-Wcast-qual\"") \
1415 _Pragma("GCC diagnostic ignored \"-Wnonnull\"")
1416 #else
1417 #define STRESS_PRAGMA_PUSH
1418 #define STRESS_PRAGMA_POP
1419 #define STRESS_PRAGMA_WARN_OFF
1420 #endif
1421
1422 /* Logging helpers */
1423 extern int pr_msg(FILE *fp, const uint64_t flag,
1424 const char *const fmt, va_list va) FORMAT(printf, 3, 0);
1425 extern int pr_yaml(FILE *fp, const char *const fmt, ...) FORMAT(printf, 2, 3);
1426 extern void pr_yaml_runinfo(FILE *fp);
1427 extern void pr_runinfo(void);
1428 extern void pr_openlog(const char *filename);
1429 extern void pr_closelog(void);
1430 extern void pr_fail_check(int *rc);
1431
1432 extern void pr_dbg(const char *fmt, ...) FORMAT(printf, 1, 2);
1433 extern void pr_dbg_skip(const char *fmt, ...) FORMAT(printf, 1, 2);
1434 extern void pr_inf(const char *fmt, ...) FORMAT(printf, 1, 2);
1435 extern void pr_inf_skip(const char *fmt, ...) FORMAT(printf, 1, 2);
1436 extern void pr_err(const char *fmt, ...) FORMAT(printf, 1, 2);
1437 extern void pr_err_skip(const char *fmt, ...) FORMAT(printf, 1, 2);
1438 extern void pr_fail(const char *fmt, ...) FORMAT(printf, 1, 2);
1439 extern void pr_tidy(const char *fmt, ...) FORMAT(printf, 1, 2);
1440
1441 extern void pr_lock(bool *locked);
1442 extern void pr_unlock(bool *locked);
1443 extern void pr_inf_lock(bool *locked, const char *fmt, ...) FORMAT(printf, 2, 3);
1444 extern void pr_dbg_lock(bool *locked, const char *fmt, ...) FORMAT(printf, 2, 3);
1445
1446 /* volatile debug print macro */
1447 #ifdef DEBUG
1448 #define pr_dbg_v(fmt, ...) \
1449 do { if (DEBUG) pr_dbg(fmt, __VA_ARGS__); } while (0)
1450 #else
1451 #define pr_dbg_v(fmt, ...)
1452 #endif
1453
1454 #if defined(HAVE_SYSLOG_H)
1455 #define shim_syslog(priority, format, ...) \
1456 syslog(priority, format, __VA_ARGS__)
1457 #define shim_openlog(ident, option, facility) \
1458 openlog(ident, option, facility)
1459 #define shim_closelog() closelog()
1460 #else
1461 #define shim_syslog(priority, format, ...)
1462 #define shim_openlog(ident, option, facility)
1463 #define shim_closelog()
1464 #endif
1465
1466 /* Memory size constants */
1467 #define KB (1ULL << 10)
1468 #define MB (1ULL << 20)
1469 #define GB (1ULL << 30)
1470 #define TB (1ULL << 40)
1471 #define PB (1ULL << 50)
1472 #define EB (1ULL << 60)
1473
1474 #define ONE_BILLIONTH (1.0E-9)
1475 #define ONE_MILLIONTH (1.0E-6)
1476 #define ONE_THOUSANDTH (1.0E-3)
1477
1478 #define PAGE_4K_SHIFT (12)
1479 #define PAGE_4K (1 << PAGE_4K_SHIFT)
1480
1481 #define STACK_ALIGNMENT (64) /* thread stacks align to 64 bytes */
1482
1483 #define MIN_OPS (1ULL)
1484 #define MAX_OPS (100000000ULL)
1485 #define MAX_32 (0xffffffffUL)
1486 #define MAX_48 (0xffffffffffffULL)
1487 #define MAX_64 (0xffffffffffffffffULL)
1488
1489 /* Maximum memory limits, 256TB for 64 bit is good enough for 2017 */
1490 #if UINTPTR_MAX == MAX_32
1491 #define MAX_MEM_LIMIT (MAX_32)
1492 #else
1493 #define MAX_MEM_LIMIT (MAX_48)
1494 #endif
1495
1496 #define MAX_FILE_LIMIT ((1ULL << ((sizeof(off_t) * 8) - 1)) - 1)
1497 /*
1498 * --maximize files must not be so big that we fill up
1499 * a disk, so make them either the MAX_FILE_FILE_LIMIT for
1500 * systems with small off_t or 4GB for large off_t systems
1501 */
1502 #define MAXIMIZED_FILE_SIZE ((sizeof(off_t) < 8) ? MAX_FILE_LIMIT : MAX_32)
1503
1504 /* Stressor defaults */
1505 #define MIN_AIO_REQUESTS (1)
1506 #define MAX_AIO_REQUESTS (4096)
1507 #define DEFAULT_AIO_REQUESTS (16)
1508
1509 #define MIN_AIO_LINUX_REQUESTS (1)
1510 #define MAX_AIO_LINUX_REQUESTS (4096)
1511 #define DEFAULT_AIO_LINUX_REQUESTS (64)
1512
1513 #define MIN_BIGHEAP_GROWTH (4 * KB)
1514 #define MAX_BIGHEAP_GROWTH (64 * MB)
1515 #define DEFAULT_BIGHEAP_GROWTH (64 * KB)
1516
1517 #define MIN_BSEARCH_SIZE (1 * KB)
1518 #define MAX_BSEARCH_SIZE (4 * MB)
1519 #define DEFAULT_BSEARCH_SIZE (64 * KB)
1520
1521 #define MIN_CHDIR_DIRS (64)
1522 #define MAX_CHDIR_DIRS (65536)
1523 #define DEFAULT_CHDIR_DIRS (8192)
1524
1525 #define MIN_CLONES (1)
1526 #define MAX_CLONES (1000000)
1527 #define DEFAULT_CLONES (8192)
1528
1529 #define MIN_COPY_FILE_BYTES (128 * MB)
1530 #define MAX_COPY_FILE_BYTES (MAX_FILE_LIMIT)
1531 #define DEFAULT_COPY_FILE_BYTES (256 * MB)
1532 #define DEFAULT_COPY_FILE_SIZE (2 * MB)
1533
1534 #define MIN_DCCP_PORT (1024)
1535 #define MAX_DCCP_PORT (65535)
1536 #define DEFAULT_DCCP_PORT (10000)
1537
1538 #define MIN_DENTRIES (1)
1539 #define MAX_DENTRIES (1000000)
1540 #define DEFAULT_DENTRIES (2048)
1541
1542 #define MIN_DIR_DIRS (64)
1543 #define MAX_DIR_DIRS (65536)
1544 #define DEFAULT_DIR_DIRS (8192)
1545
1546 #define MIN_EPOLL_PORT (1024)
1547 #define MAX_EPOLL_PORT (65535)
1548 #define DEFAULT_EPOLL_PORT (6000)
1549
1550 #define MIN_HDD_BYTES (1 * MB)
1551 #define MAX_HDD_BYTES (MAX_FILE_LIMIT)
1552 #define DEFAULT_HDD_BYTES (1 * GB)
1553
1554 #define MIN_HDD_WRITE_SIZE (1)
1555 #define MAX_HDD_WRITE_SIZE (4 * MB)
1556 #define DEFAULT_HDD_WRITE_SIZE (64 * 1024)
1557
1558 #define MIN_FALLOCATE_BYTES (1 * MB)
1559 #define MAX_FALLOCATE_BYTES (MAX_FILE_LIMIT)
1560 #define DEFAULT_FALLOCATE_BYTES (1 * GB)
1561
1562 #define MIN_FIEMAP_SIZE (1 * MB)
1563 #define MAX_FIEMAP_SIZE (MAX_FILE_LIMIT)
1564 #define DEFAULT_FIEMAP_SIZE (16 * MB)
1565
1566 #define MIN_FIFO_READERS (1)
1567 #define MAX_FIFO_READERS (64)
1568 #define DEFAULT_FIFO_READERS (4)
1569
1570 #define MIN_ITIMER_FREQ (1)
1571 #define MAX_ITIMER_FREQ (100000000)
1572 #define DEFAULT_ITIMER_FREQ (1000000)
1573
1574 #define MIN_MQ_SIZE (1)
1575 #define MAX_MQ_SIZE (32)
1576 #define DEFAULT_MQ_SIZE (10)
1577
1578 #define MIN_SEMAPHORE_PROCS (2)
1579 #define MAX_SEMAPHORE_PROCS (64)
1580 #define DEFAULT_SEMAPHORE_PROCS (2)
1581
1582 #define MIN_EXECS (1)
1583 #define MAX_EXECS (16000)
1584 #define DEFAULT_EXECS (1)
1585
1586 #define MIN_FORKS (1)
1587 #define MAX_FORKS (16000)
1588 #define DEFAULT_FORKS (1)
1589
1590 #define MIN_HEAPSORT_SIZE (1 * KB)
1591 #define MAX_HEAPSORT_SIZE (4 * MB)
1592 #define DEFAULT_HEAPSORT_SIZE (256 * KB)
1593
1594 #define MIN_IOMIX_BYTES (1 * MB)
1595 #define MAX_IOMIX_BYTES (MAX_FILE_LIMIT)
1596 #define DEFAULT_IOMIX_BYTES (1 * GB)
1597
1598 #define MIN_JUDY_SIZE (1 * KB)
1599 #define MAX_JUDY_SIZE (4 * MB)
1600 #define DEFAULT_JUDY_SIZE (256 * KB)
1601
1602 #define MIN_VFORKS (1)
1603 #define MAX_VFORKS (16000)
1604 #define DEFAULT_VFORKS (1)
1605
1606 #define MIN_HSEARCH_SIZE (1 * KB)
1607 #define MAX_HSEARCH_SIZE (4 * MB)
1608 #define DEFAULT_HSEARCH_SIZE (8 * KB)
1609
1610 #define MIN_LEASE_BREAKERS (1)
1611 #define MAX_LEASE_BREAKERS (64)
1612 #define DEFAULT_LEASE_BREAKERS (1)
1613
1614 #define MIN_LIST_SIZE (64)
1615 #define MAX_LIST_SIZE (1000000)
1616 #define DEFAULT_LIST_SIZE (5000)
1617
1618 #define MIN_LSEARCH_SIZE (1 * KB)
1619 #define MAX_LSEARCH_SIZE (1 * MB)
1620 #define DEFAULT_LSEARCH_SIZE (8 * KB)
1621
1622 #define MIN_MALLOC_BYTES (1 * KB)
1623 #define MAX_MALLOC_BYTES (MAX_MEM_LIMIT)
1624 #define DEFAULT_MALLOC_BYTES (64 * KB)
1625
1626 #define MIN_MALLOC_MAX (32)
1627 #define MAX_MALLOC_MAX (256 * 1024)
1628 #define DEFAULT_MALLOC_MAX (64 * KB)
1629
1630 #define MIN_MALLOC_THRESHOLD (1)
1631 #define MAX_MALLOC_THRESHOLD (256 * MB)
1632 #define DEFAULT_MALLOC_THRESHOLD (128 * KB)
1633
1634 #define MIN_MATRIX_SIZE (16)
1635 #define MAX_MATRIX_SIZE (8192)
1636 #define DEFAULT_MATRIX_SIZE (256)
1637
1638 #define MIN_MATRIX3D_SIZE (16)
1639 #define MAX_MATRIX3D_SIZE (1024)
1640 #define DEFAULT_MATRIX3D_SIZE (64)
1641
1642 #define MIN_MEMFD_BYTES (2 * MB)
1643 #define MAX_MEMFD_BYTES (MAX_MEM_LIMIT)
1644 #define DEFAULT_MEMFD_BYTES (256 * MB)
1645
1646 #define MIN_MEMFD_FDS (8)
1647 #define MAX_MEMFD_FDS (4096)
1648 #define DEFAULT_MEMFD_FDS (256)
1649
1650 #define MIN_MERGESORT_SIZE (1 * KB)
1651 #define MAX_MERGESORT_SIZE (4 * MB)
1652 #define DEFAULT_MERGESORT_SIZE (256 * KB)
1653
1654 #define MIN_MMAP_BYTES (4 * KB)
1655 #define MAX_MMAP_BYTES (MAX_MEM_LIMIT)
1656 #define DEFAULT_MMAP_BYTES (256 * MB)
1657
1658 #define MIN_MEMRATE_BYTES (4 * KB)
1659 #define MAX_MEMRATE_BYTES (MAX_MEM_LIMIT)
1660 #define DEFAULT_MEMRATE_BYTES (256 * MB)
1661
1662 #define DEFAULT_MREMAP_BYTES (256 * MB)
1663 #define MIN_MREMAP_BYTES (4 * KB)
1664 #define MAX_MREMAP_BYTES (MAX_MEM_LIMIT)
1665 #define DEFAULT_MREMAP_BYTES (256 * MB)
1666
1667 #define MIN_MSYNC_BYTES (1 * MB) /* MUST NOT BE page size or less! */
1668 #define MAX_MSYNC_BYTES (MAX_FILE_LIMIT)
1669 #define DEFAULT_MSYNC_BYTES (256 * MB)
1670
1671 #define MIN_PTHREAD (1)
1672 #define MAX_PTHREAD (30000)
1673 #define DEFAULT_PTHREAD (1024)
1674
1675 #define MIN_PTYS (8)
1676 #define MAX_PTYS (65536)
1677 #define DEFAULT_PTYS (65536)
1678
1679 #define MIN_QSORT_SIZE (1 * KB)
1680 #define MAX_QSORT_SIZE (4 * MB)
1681 #define DEFAULT_QSORT_SIZE (256 * KB)
1682
1683 #define MIN_RADIXSORT_SIZE (1 * KB)
1684 #define MAX_RADIXSORT_SIZE (4 * MB)
1685 #define DEFAULT_RADIXSORT_SIZE (256 * KB)
1686
1687 #define MIN_READAHEAD_BYTES (1 * MB)
1688 #define MAX_READAHEAD_BYTES (MAX_FILE_LIMIT)
1689 #define DEFAULT_READAHEAD_BYTES (64 * MB)
1690
1691 #define MIN_REVIO_BYTES (1 * MB)
1692 #define MAX_REVIO_BYTES (MAX_FILE_LIMIT)
1693 #define DEFAULT_REVIO_BYTES (1 * GB)
1694
1695 #define MIN_SCTP_PORT (1024)
1696 #define MAX_SCTP_PORT (65535)
1697 #define DEFAULT_SCTP_PORT (9000)
1698
1699 #define MIN_SENDFILE_SIZE (1 * KB)
1700 #define MAX_SENDFILE_SIZE (1 * GB)
1701 #define DEFAULT_SENDFILE_SIZE (4 * MB)
1702
1703 #define MIN_SEEK_SIZE (1 * MB)
1704 #define MAX_SEEK_SIZE (MAX_FILE_LIMIT)
1705 #define DEFAULT_SEEK_SIZE (16 * MB)
1706
1707 #define MIN_SEQUENTIAL (0)
1708 #define MAX_SEQUENTIAL (1000000)
1709 #define DEFAULT_SEQUENTIAL (0) /* Disabled */
1710 #define DEFAULT_PARALLEL (0) /* Disabled */
1711
1712 #define MIN_SHELLSORT_SIZE (1 * KB)
1713 #define MAX_SHELLSORT_SIZE (4 * MB)
1714 #define DEFAULT_SHELLSORT_SIZE (256 * KB)
1715
1716 #define MIN_SHM_SYSV_BYTES (1 * MB)
1717 #define MAX_SHM_SYSV_BYTES (256 * MB)
1718 #define DEFAULT_SHM_SYSV_BYTES (8 * MB)
1719
1720 #define MIN_SHM_SYSV_SEGMENTS (1)
1721 #define MAX_SHM_SYSV_SEGMENTS (128)
1722 #define DEFAULT_SHM_SYSV_SEGMENTS (8)
1723
1724 #define MIN_SHM_POSIX_BYTES (1 * MB)
1725 #define MAX_SHM_POSIX_BYTES (1 * GB)
1726 #define DEFAULT_SHM_POSIX_BYTES (8 * MB)
1727
1728 #define MIN_SHM_POSIX_OBJECTS (1)
1729 #define MAX_SHM_POSIX_OBJECTS (128)
1730 #define DEFAULT_SHM_POSIX_OBJECTS (32)
1731
1732 #define MAX_SIGSUSPEND_PIDS (4)
1733
1734 #define MIN_SKIPLIST_SIZE (1 * KB)
1735 #define MAX_SKIPLIST_SIZE (4 * MB)
1736 #define DEFAULT_SKIPLIST_SIZE (64 * KB)
1737
1738 #define MIN_SLEEP (1)
1739 #define MAX_SLEEP (30000)
1740 #define DEFAULT_SLEEP (1024)
1741
1742 #define MIN_SOCKET_PORT (1024)
1743 #define MAX_SOCKET_PORT (65535)
1744 #define DEFAULT_SOCKET_PORT (5000)
1745
1746 #define DEFAULT_SOCKET_MANY_PORT (11000)
1747 #define DEFAULT_SOCKABUSE_PORT (12000)
1748
1749 #define MIN_RAWUDP_PORT (1024)
1750 #define MAX_RAWUDP_PORT (65535)
1751 #define DEFAULT_RAWUDP_PORT (13000)
1752
1753 #define MIN_RAWPKT_PORT (1024)
1754 #define MAX_RAWPKT_PORT (65535)
1755 #define DEFAULT_RAWPKT_PORT (14000)
1756
1757 #define MIN_SOCKET_FD_PORT (1024)
1758 #define MAX_SOCKET_FD_PORT (65535)
1759 #define DEFAULT_SOCKET_FD_PORT (8000)
1760
1761 #define MIN_SPLICE_BYTES (1 * KB)
1762 #define MAX_SPLICE_BYTES (64 * MB)
1763 #define DEFAULT_SPLICE_BYTES (64 * KB)
1764
1765 #define MIN_STREAM_L3_SIZE (4 * KB)
1766 #define MAX_STREAM_L3_SIZE (MAX_MEM_LIMIT)
1767 #define DEFAULT_STREAM_L3_SIZE (4 * MB)
1768
1769 #define MIN_SYNC_FILE_BYTES (1 * MB)
1770 #define MAX_SYNC_FILE_BYTES (MAX_FILE_LIMIT)
1771 #define DEFAULT_SYNC_FILE_BYTES (1 * GB)
1772
1773 #define MIN_TIMER_FREQ (1)
1774 #define MAX_TIMER_FREQ (100000000)
1775 #define DEFAULT_TIMER_FREQ (1000000)
1776
1777 #define MIN_TIMERFD_FREQ (1)
1778 #define MAX_TIMERFD_FREQ (100000000)
1779 #define DEFAULT_TIMERFD_FREQ (1000000)
1780
1781 #define MIN_TREE_SIZE (1000)
1782 #define MAX_TREE_SIZE (25000000)
1783 #define DEFAULT_TREE_SIZE (250000)
1784
1785 #define MIN_TSEARCH_SIZE (1 * KB)
1786 #define MAX_TSEARCH_SIZE (4 * MB)
1787 #define DEFAULT_TSEARCH_SIZE (64 * KB)
1788
1789 #define MIN_UDP_PORT (1024)
1790 #define MAX_UDP_PORT (65535)
1791 #define DEFAULT_UDP_PORT (7000)
1792
1793 #define MIN_USERFAULTFD_BYTES (4 * KB)
1794 #define MAX_USERFAULTFD_BYTES (MAX_MEM_LIMIT)
1795 #define DEFAULT_USERFAULTFD_BYTES (16 * MB)
1796
1797 #define MIN_VM_BYTES (4 * KB)
1798 #define MAX_VM_BYTES (MAX_MEM_LIMIT)
1799 #define DEFAULT_VM_BYTES (256 * MB)
1800
1801 #define MIN_VM_HANG (0)
1802 #define MAX_VM_HANG (3600)
1803 #define DEFAULT_VM_HANG (~0ULL)
1804
1805 #define MIN_VM_RW_BYTES (4 * KB)
1806 #define MAX_VM_RW_BYTES (MAX_MEM_LIMIT)
1807 #define DEFAULT_VM_RW_BYTES (16 * MB)
1808
1809 #define MIN_VM_SPLICE_BYTES (4 * KB)
1810 #define MAX_VM_SPLICE_BYTES (64 * MB)
1811 #define DEFAULT_VM_SPLICE_BYTES (64 * KB)
1812
1813 #define MIN_ZOMBIES (1)
1814 #define MAX_ZOMBIES (1000000)
1815 #define DEFAULT_ZOMBIES (8192)
1816
1817 #define TIMEOUT_NOT_SET (~0ULL)
1818 #define DEFAULT_TIMEOUT (60 * 60 * 24)
1819 #define DEFAULT_BACKOFF (0)
1820 #define DEFAULT_LINKS (8192)
1821 #define DEFAULT_DIRS (8192)
1822
1823 #define STR_SHARED_SIZE (65536 * 32)
1824 #define MEM_CACHE_SIZE (65536 * 32)
1825 #define DEFAULT_CACHE_LEVEL 3
1826 #define UNDEFINED (-1)
1827
1828 #define PAGE_MAPPED (0x01)
1829 #define PAGE_MAPPED_FAIL (0x02)
1830
1831 #if defined(__GNUC__) || defined(__clang__)
1832 #define TYPEOF_CAST(a) (typeof(a))
1833 #else
1834 #define TYPEOF_CAST(a)
1835 #endif
1836
1837 /* Generic bit setting on an array macros */
1838 #define STRESS_NBITS(a) (sizeof(a[0]) * 8)
1839 #define STRESS_GETBIT(a, i) (a[i / STRESS_NBITS(a)] & \
1840 (TYPEOF_CAST(a[0])1 << (i & (STRESS_NBITS(a)-1))))
1841 #define STRESS_CLRBIT(a, i) (a[i / STRESS_NBITS(a)] &= \
1842 ~(TYPEOF_CAST(a[0])1 << (i & (STRESS_NBITS(a)-1))))
1843 #define STRESS_SETBIT(a, i) (a[i / STRESS_NBITS(a)] |= \
1844 (TYPEOF_CAST(a[0])1 << (i & (STRESS_NBITS(a)-1))))
1845
1846 /* MWC random number initial seed */
1847 #define STRESS_MWC_SEED_Z (362436069UL)
1848 #define STRESS_MWC_SEED_W (521288629UL)
1849 #define STRESS_MWC_SEED() stress_mwc_seed(STRESS_MWC_SEED_W, STRESS_MWC_SEED_Z)
1850
1851 #define SIZEOF_ARRAY(a) (sizeof(a) / sizeof(a[0]))
1852
1853 /* Arch specific, x86 */
1854 #if defined(__x86_64__) || defined(__x86_64) || \
1855 defined(__i386__) || defined(__i386)
1856 #define STRESS_ARCH_X86 (1)
1857 #endif
1858
1859 /* Arch specific, ARM */
1860 #if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || \
1861 defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || \
1862 defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) || \
1863 defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7__) || \
1864 defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || \
1865 defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) || \
1866 defined(__ARM_ARCH_8A__) || defined(__aarch64__)
1867 #define STRESS_ARCH_ARM (1)
1868 #endif
1869
1870 /* Arch specific RISC-V */
1871 #if defined(__riscv) || \
1872 defined(__riscv__)
1873 #define STRESS_ARCH_RISCV (1)
1874 #endif
1875
1876 /* Arch specific, IBM S390 */
1877 #if defined(__s390__)
1878 #define STRESS_ARCH_S390 (1)
1879 #endif
1880
1881 /* Arch specific PPC64 */
1882 #if defined(__PPC64__)
1883 #define STRESS_ARCH_PPC64 (1)
1884 #endif
1885
1886 /* Arch specific M68K */
1887 #if defined(__m68k__) || \
1888 defined(__mc68000__) || \
1889 defined(__mc68010__) || \
1890 defined(__mc68020__)
1891 #define STRESS_ARCH_M68K (1)
1892 #endif
1893
1894 /* Arch specific SPARC */
1895 #if defined(__sparc) || \
1896 defined(__sparc__) || \
1897 defined(__sparc_v9__)
1898 #define STRESS_ARCH_SPARC (1)
1899 #endif
1900
1901 /* Arch specific SH4 */
1902 #if defined(__SH4__)
1903 #define STRESS_ARCH_SH4 (1)
1904 #endif
1905
1906 /* Arch specific ALPHA */
1907 #if defined(__alpha) || \
1908 defined(__alpha__)
1909 #define STRESS_ARCH_ALPHA (1)
1910 #endif
1911
1912 /* Arch specific HPPA */
1913 #if defined(__hppa) || \
1914 defined(__hppa__)
1915 #define STRESS_ARCH_HPPA (1)
1916 #undef HAVE_SIGALTSTACK
1917 #endif
1918
1919 /* Arch specific Kalray VLIW core */
1920 #if defined(__KVX__) || \
1921 defined(__kvx__)
1922 #define STRESS_ARCH_KVX (1)
1923 #endif
1924
1925 /* GCC5.0+ target_clones attribute, x86 */
1926 #if defined(STRESS_ARCH_X86) && \
1927 defined(HAVE_TARGET_CLONES)
1928
1929 #if defined(HAVE_TARGET_CLONES_MMX)
1930 #define TARGET_CLONE_MMX "mmx",
1931 #define TARGET_CLONE_USE
1932 #else
1933 #define TARGET_CLONE_MMX
1934 #endif
1935
1936 #if defined(HAVE_TARGET_CLONES_AVX)
1937 #define TARGET_CLONE_AVX "avx",
1938 #define TARGET_CLONE_USE
1939 #else
1940 #define TARGET_CLONE_AVX
1941 #endif
1942
1943 #if defined(HAVE_TARGET_CLONES_AVX2)
1944 #define TARGET_CLONE_AVX2 "avx2",
1945 #define TARGET_CLONE_USE
1946 #else
1947 #define TARGET_CLONE_AVX2
1948 #endif
1949
1950 #if defined(HAVE_TARGET_CLONES_SSE)
1951 #define TARGET_CLONE_SSE "sse",
1952 #define TARGET_CLONE_USE
1953 #else
1954 #define TARGET_CLONE_SSE
1955 #endif
1956
1957 #if defined(HAVE_TARGET_CLONES_SSE2)
1958 #define TARGET_CLONE_SSE2 "sse2",
1959 #define TARGET_CLONE_USE
1960 #else
1961 #define TARGET_CLONE_SSE2
1962 #endif
1963
1964 #if defined(HAVE_TARGET_CLONES_SSE3)
1965 #define TARGET_CLONE_SSE3 "sse3",
1966 #define TARGET_CLONE_USE
1967 #else
1968 #define TARGET_CLONE_SSE3
1969 #endif
1970
1971 #if defined(HAVE_TARGET_CLONES_SSSE3)
1972 #define TARGET_CLONE_SSSE3 "ssse3",
1973 #define TARGET_CLONE_USE
1974 #else
1975 #define TARGET_CLONE_SSSE3
1976 #endif
1977
1978 #if defined(HAVE_TARGET_CLONES_SSE4_1)
1979 #define TARGET_CLONE_SSE4_1 "sse4.1",
1980 #define TARGET_CLONE_USE
1981 #else
1982 #define TARGET_CLONE_SSE4_1
1983 #endif
1984
1985 #if defined(HAVE_TARGET_CLONES_SSE4_2)
1986 #define TARGET_CLONE_SSE4_2 "sse4.2",
1987 #define TARGET_CLONE_USE
1988 #else
1989 #define TARGET_CLONE_SSE4_2
1990 #endif
1991
1992 #if defined(HAVE_TARGET_CLONES_SKYLAKE_AVX512)
1993 #define TARGET_CLONE_SKYLAKE_AVX512 "arch=skylake-avx512",
1994 #define TARGET_CLONE_USE
1995 #else
1996 #define TARGET_CLONE_SKYLAKE_AVX512
1997 #endif
1998
1999 #if defined(HAVE_TARGET_CLONES_ALDERLAKE)
2000 #define TARGET_CLONE_ALDERLAKE "arch=alderlake",
2001 #define TARGET_CLONE_USE
2002 #else
2003 #define TARGET_CLONE_ALDERLAKE
2004 #endif
2005
2006 #define TARGET_CLONES_ALL \
2007 TARGET_CLONE_AVX TARGET_CLONE_AVX2 \
2008 TARGET_CLONE_MMX TARGET_CLONE_SSE \
2009 TARGET_CLONE_SSE2 TARGET_CLONE_SSE3 \
2010 TARGET_CLONE_SSSE3 TARGET_CLONE_SSE4_1 \
2011 TARGET_CLONE_SSE4_2 \
2012 TARGET_CLONE_SKYLAKE_AVX512 \
2013 TARGET_CLONE_ALDERLAKE \
2014 "default"
2015
2016 #if defined(TARGET_CLONE_USE)
2017 #define TARGET_CLONES __attribute__((target_clones(TARGET_CLONES_ALL)))
2018 #endif
2019 #endif
2020
2021 /* GCC5.0+ target_clones attribute, ppc64 */
2022
2023 #if defined(STRESS_ARCH_PPC64) && \
2024 defined(HAVE_TARGET_CLONES) && \
2025 defined(HAVE_TARGET_CLONES_POWER9)
2026 #define TARGET_CLONES __attribute__((target_clones("cpu=power9,default")))
2027 #endif
2028
2029 #if !defined(TARGET_CLONES)
2030 #define TARGET_CLONES
2031 #endif
2032
2033 /*
2034 * See ioprio_set(2) and linux/ioprio.h, glibc has no definitions
2035 * for these at present. Also refer to Documentation/block/ioprio.txt
2036 * in the Linux kernel source.
2037 */
2038 #if !defined(IOPRIO_CLASS_RT)
2039 #define IOPRIO_CLASS_RT (1)
2040 #endif
2041 #if !defined(IOPRIO_CLASS_BE)
2042 #define IOPRIO_CLASS_BE (2)
2043 #endif
2044 #if !defined(IOPRIO_CLASS_IDLE)
2045 #define IOPRIO_CLASS_IDLE (3)
2046 #endif
2047
2048 #if !defined(IOPRIO_WHO_PROCESS)
2049 #define IOPRIO_WHO_PROCESS (1)
2050 #endif
2051 #if !defined(IOPRIO_WHO_PGRP)
2052 #define IOPRIO_WHO_PGRP (2)
2053 #endif
2054 #if !defined(IOPRIO_WHO_USER)
2055 #define IOPRIO_WHO_USER (3)
2056 #endif
2057
2058 #if !defined(IOPRIO_PRIO_VALUE)
2059 #define IOPRIO_PRIO_VALUE(class, data) (((class) << 13) | data)
2060 #endif
2061
2062 /* prctl(2) timer slack support */
2063 #if defined(HAVE_SYS_PRCTL_H) && \
2064 defined(HAVE_PRCTL) && \
2065 defined(PR_SET_TIMERSLACK) && \
2066 defined(PR_GET_TIMERSLACK)
2067 #define HAVE_PRCTL_TIMER_SLACK
2068 #endif
2069
shim_mb(void)2070 static inline void ALWAYS_INLINE shim_mb(void)
2071 {
2072 asm volatile ("" ::: "memory");
2073 }
2074
2075 /* increment the stessor bogo ops counter */
inc_counter(const stress_args_t * args)2076 static inline void ALWAYS_INLINE inc_counter(const stress_args_t *args)
2077 {
2078 *args->counter_ready = false;
2079 shim_mb();
2080 (*(args->counter))++;
2081 shim_mb();
2082 *args->counter_ready = true;
2083 shim_mb();
2084 }
2085
get_counter(const stress_args_t * args)2086 static inline uint64_t ALWAYS_INLINE get_counter(const stress_args_t *args)
2087 {
2088 return *args->counter;
2089 }
2090
set_counter(const stress_args_t * args,const uint64_t val)2091 static inline void ALWAYS_INLINE set_counter(const stress_args_t *args, const uint64_t val)
2092 {
2093 *args->counter_ready = false;
2094 shim_mb();
2095 *args->counter = val;
2096 shim_mb();
2097 *args->counter_ready = true;
2098 shim_mb();
2099 }
2100
add_counter(const stress_args_t * args,const uint64_t inc)2101 static inline void ALWAYS_INLINE add_counter(const stress_args_t *args, const uint64_t inc)
2102 {
2103 *args->counter_ready = false;
2104 shim_mb();
2105 *args->counter += inc;
2106 shim_mb();
2107 *args->counter_ready = true;
2108 shim_mb();
2109 }
2110
2111 /* pthread porting shims, spinlock or fallback to mutex */
2112 #if defined(HAVE_LIB_PTHREAD)
2113 #if defined(HAVE_LIB_PTHREAD_SPINLOCK) && \
2114 !defined(__DragonFly__) && \
2115 !defined(__OpenBSD__)
2116 typedef pthread_spinlock_t shim_pthread_spinlock_t;
2117
2118 #define SHIM_PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
2119 #define SHIM_PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
2120
2121 #define shim_pthread_spin_lock(lock) pthread_spin_lock(lock)
2122 #define shim_pthread_spin_unlock(lock) pthread_spin_unlock(lock)
2123 #define shim_pthread_spin_init(lock, shared) pthread_spin_init(lock, shared)
2124 #define shim_pthread_spin_destroy(lock) pthread_spin_destroy(lock)
2125 #else
2126 typedef pthread_mutex_t shim_pthread_spinlock_t;
2127
2128 #define SHIM_PTHREAD_PROCESS_SHARED NULL
2129 #define SHIM_PTHREAD_PROCESS_PRIVATE NULL
2130
2131 #define shim_pthread_spin_lock(lock) pthread_mutex_lock(lock)
2132 #define shim_pthread_spin_unlock(lock) pthread_mutex_unlock(lock)
2133 #define shim_pthread_spin_init(lock, shared) pthread_mutex_init(lock, shared)
2134 #define shim_pthread_spin_destroy(lock) pthread_mutex_destroy(lock)
2135 #endif
2136 #endif
2137
2138 /* Capabilities shim */
2139
2140 #define SHIM_CAP_IS_ROOT (-1)
2141
2142 /* POSIX-draft defined capabilities */
2143 #if defined(CAP_CHOWN)
2144 #define SHIM_CAP_CHOWN CAP_CHOWN
2145 #else
2146 #define SHIM_CAP_CHOWN SHIM_CAP_IS_ROOT
2147 #endif
2148
2149 #if defined(CAP_DAC_OVERRIDE)
2150 #define SHIM_CAP_DAC_OVERRIDE CAP_DAC_OVERRIDE
2151 #else
2152 #define SHIM_CAP_DAC_OVERRIDE SHIM_CAP_IS_ROOT
2153 #endif
2154
2155 #if defined(CAP_DAC_READ_SEARCH)
2156 #define SHIM_CAP_DAC_READ_SEARCH CAP_DAC_READ_SEARCH
2157 #else
2158 #define SHIM_CAP_DAC_READ_SEARCH SHIM_CAP_IS_ROOT
2159 #endif
2160
2161 #if defined(CAP_FOWNER)
2162 #define SHIM_CAP_FOWNER CAP_FOWNER
2163 #else
2164 #define SHIM_CAP_FOWNER SHIM_CAP_IS_ROOT
2165 #endif
2166
2167 #if defined(CAP_FSETID)
2168 #define SHIM_CAP_FSETID CAP_FSETID
2169 #else
2170 #define SHIM_CAP_FSETID SHIM_CAP_IS_ROOT
2171 #endif
2172
2173 #if defined(CAP_KILL)
2174 #define SHIM_CAP_KILL CAP_KILL
2175 #else
2176 #define SHIM_CAP_KILL SHIM_CAP_IS_ROOT
2177 #endif
2178
2179 #if defined(CAP_SETGID)
2180 #define SHIM_CAP_SETGID CAP_SETGID
2181 #else
2182 #define SHIM_CAP_SETGID SHIM_CAP_IS_ROOT
2183 #endif
2184
2185 #if defined(CAP_SETUID)
2186 #define SHIM_CAP_SETUID CAP_SETUID
2187 #else
2188 #define SHIM_CAP_SETUID SHIM_CAP_IS_ROOT
2189 #endif
2190
2191 /* Linux specific capabilities */
2192 #if defined(CAP_SETPCAP)
2193 #define SHIM_CAP_SETPCAP CAP_SETPCAP
2194 #else
2195 #define SHIM_CAP_SETPCAP SHIM_CAP_IS_ROOT
2196 #endif
2197
2198 #if defined(CAP_LINUX_IMMUTABLE)
2199 #define SHIM_CAP_LINUX_IMMUTABLE CAP_LINUX_IMMUTABLE
2200 #else
2201 #define SHIM_CAP_LINUX_IMMUTABLE SHIM_CAP_IS_ROOT
2202 #endif
2203
2204 #if defined(CAP_NET_BIND_SERVICE)
2205 #define SHIM_CAP_NET_BIND_SERVICE CAP_NET_BIND_SERVICE
2206 #else
2207 #define SHIM_CAP_NET_BIND_SERVICE SHIM_CAP_IS_ROOT
2208 #endif
2209
2210 #if defined(CAP_NET_BROADCAST)
2211 #define SHIM_CAP_NET_BROADCAST CAP_NET_BROADCAST
2212 #else
2213 #define SHIM_CAP_NET_BROADCAST SHIM_CAP_IS_ROOT
2214 #endif
2215
2216 #if defined(CAP_NET_ADMIN)
2217 #define SHIM_CAP_NET_ADMIN CAP_NET_ADMIN
2218 #else
2219 #define SHIM_CAP_NET_ADMIN SHIM_CAP_IS_ROOT
2220 #endif
2221
2222 #if defined(CAP_NET_RAW)
2223 #define SHIM_CAP_NET_RAW CAP_NET_RAW
2224 #else
2225 #define SHIM_CAP_NET_RAW SHIM_CAP_IS_ROOT
2226 #endif
2227
2228 #if defined(CAP_IPC_LOCK)
2229 #define SHIM_CAP_IPC_LOCK CAP_IPC_LOCK
2230 #else
2231 #define SHIM_CAP_IPC_LOCK SHIM_CAP_IS_ROOT
2232 #endif
2233
2234 #if defined(CAP_IPC_OWNER)
2235 #define SHIM_CAP_IPC_OWNER CAP_IPC_OWNER
2236 #else
2237 #define SHIM_CAP_IPC_OWNER SHIM_CAP_IS_ROOT
2238 #endif
2239
2240 #if defined(CAP_SYS_MODULE)
2241 #define SHIM_CAP_SYS_MODULE CAP_SYS_MODULE
2242 #else
2243 #define SHIM_CAP_SYS_MODULE SHIM_CAP_IS_ROOT
2244 #endif
2245
2246 #if defined(CAP_SYS_RAWIO)
2247 #define SHIM_CAP_SYS_RAWIO CAP_SYS_RAWIO
2248 #else
2249 #define SHIM_CAP_SYS_RAWIO SHIM_CAP_IS_ROOT
2250 #endif
2251
2252 #if defined(CAP_SYS_CHROOT)
2253 #define SHIM_CAP_SYS_CHROOT CAP_SYS_CHROOT
2254 #else
2255 #define SHIM_CAP_SYS_CHROOT SHIM_CAP_IS_ROOT
2256 #endif
2257
2258 #if defined(CAP_SYS_PTRACE)
2259 #define SHIM_CAP_SYS_PTRACE CAP_SYS_PTRACE
2260 #else
2261 #define SHIM_CAP_SYS_PTRACE SHIM_CAP_IS_ROOT
2262 #endif
2263
2264 #if defined(CAP_SYS_PACCT)
2265 #define SHIM_CAP_SYS_PACCT CAP_SYS_PACCT
2266 #else
2267 #define SHIM_CAP_SYS_PACCT SHIM_CAP_IS_ROOT
2268 #endif
2269
2270 #if defined(CAP_SYS_ADMIN)
2271 #define SHIM_CAP_SYS_ADMIN CAP_SYS_ADMIN
2272 #else
2273 #define SHIM_CAP_SYS_ADMIN SHIM_CAP_IS_ROOT
2274 #endif
2275
2276 #if defined(CAP_SYS_BOOT)
2277 #define SHIM_CAP_SYS_BOOT CAP_SYS_BOOT
2278 #else
2279 #define SHIM_CAP_SYS_BOOT SHIM_CAP_IS_ROOT
2280 #endif
2281
2282 #if defined(CAP_SYS_NICE)
2283 #define SHIM_CAP_SYS_NICE CAP_SYS_NICE
2284 #else
2285 #define SHIM_CAP_SYS_NICE SHIM_CAP_IS_ROOT
2286 #endif
2287
2288 #if defined(CAP_SYS_RESOURCE)
2289 #define SHIM_CAP_SYS_RESOURCE CAP_SYS_RESOURCE
2290 #else
2291 #define SHIM_CAP_SYS_RESOURCE SHIM_CAP_IS_ROOT
2292 #endif
2293
2294 #if defined(CAP_SYS_TIME)
2295 #define SHIM_CAP_SYS_TIME CAP_SYS_TIME
2296 #else
2297 #define SHIM_CAP_SYS_TIME SHIM_CAP_IS_ROOT
2298 #endif
2299
2300 #if defined(CAP_SYS_TTY_CONFIG)
2301 #define SHIM_CAP_SYS_TTY_CONFIG CAP_SYS_TTY_CONFIG
2302 #else
2303 #define SHIM_CAP_SYS_TTY_CONFIG SHIM_CAP_IS_ROOT
2304 #endif
2305
2306 #if defined(CAP_MKNOD)
2307 #define SHIM_CAP_MKNOD CAP_MKNOD
2308 #else
2309 #define SHIM_CAP_MKNOD SHIM_CAP_IS_ROOT
2310 #endif
2311
2312 #if defined(CAP_LEASE)
2313 #define SHIM_CAP_LEASE CAP_LEASE
2314 #else
2315 #define SHIM_CAP_LEASE SHIM_CAP_IS_ROOT
2316 #endif
2317
2318 #if defined(CAP_AUDIT_WRITE)
2319 #define SHIM_CAP_AUDIT_WRITE CAP_AUDIT_WRITE
2320 #else
2321 #define SHIM_CAP_AUDIT_WRITE SHIM_CAP_IS_ROOT
2322 #endif
2323
2324 #if defined(CAP_AUDIT_CONTROL)
2325 #define SHIM_CAP_AUDIT_CONTROL CAP_AUDIT_CONTROL
2326 #else
2327 #define SHIM_CAP_AUDIT_CONTROL SHIM_CAP_IS_ROOT
2328 #endif
2329
2330 #if defined(CAP_SETFCAP)
2331 #define SHIM_CAP_SETFCAP CAP_SETFCAP
2332 #else
2333 #define SHIM_CAP_SETFCAP SHIM_CAP_IS_ROOT
2334 #endif
2335
2336 #if defined(CAP_MAC_OVERRIDE)
2337 #define SHIM_CAP_MAC_OVERRIDE CAP_MAC_OVERRIDE
2338 #else
2339 #define SHIM_CAP_MAC_OVERRIDE SHIM_CAP_IS_ROOT
2340 #endif
2341
2342 #if defined(CAP_MAC_ADMIN)
2343 #define SHIM_CAP_MAC_ADMIN CAP_MAC_ADMIN
2344 #else
2345 #define SHIM_CAP_MAC_ADMIN SHIM_CAP_IS_ROOT
2346 #endif
2347
2348 #if defined(CAP_SYSLOG)
2349 #define SHIM_CAP_SYSLOG CAP_SYSLOG
2350 #else
2351 #define SHIM_CAP_SYSLOG SHIM_CAP_IS_ROOT
2352 #endif
2353
2354 #if defined(CAP_WAKE_ALARM)
2355 #define SHIM_CAP_WAKE_ALARM CAP_WAKE_ALARM
2356 #else
2357 #define SHIM_CAP_WAKE_ALARM SHIM_CAP_IS_ROOT
2358 #endif
2359
2360 #if defined(CAP_BLOCK_SUSPEND)
2361 #define SHIM_CAP_BLOCK_SUSPEND CAP_BLOCK_SUSPEND
2362 #else
2363 #define SHIM_CAP_BLOCK_SUSPEND SHIM_CAP_IS_ROOT
2364 #endif
2365
2366 #if defined(CAP_AUDIT_READ)
2367 #define SHIM_CAP_AUDIT_READ CAP_AUDIT_READ
2368 #else
2369 #define SHIM_CAP_AUDIT_READ SHIM_CAP_IS_ROOT
2370 #endif
2371
2372 #if defined(CAP_PERFMON)
2373 #define SHIM_CAP_PERFMON CAP_PERFMON
2374 #else
2375 #define SHIM_CAP_PERFMON SHIM_CAP_IS_ROOT
2376 #endif
2377
2378 #if defined(CAP_BPF)
2379 #define SHIM_CAP_BPF CAP_BPF
2380 #else
2381 #define SHIM_CAP_BPF SHIM_CAP_IS_ROOT
2382 #endif
2383
2384
2385 /* stress process prototype */
2386 typedef int (*stress_func_t)(const stress_args_t *args);
2387
2388 /* Fast random number generator state */
2389 typedef struct {
2390 uint32_t w;
2391 uint32_t z;
2392 } stress_mwc_t;
2393
2394 /* perf related constants */
2395 #if defined(HAVE_LIB_PTHREAD) && \
2396 defined(HAVE_LINUX_PERF_EVENT_H) && \
2397 defined(__NR_perf_event_open)
2398 #define STRESS_PERF_STATS (1)
2399 #define STRESS_PERF_INVALID (~0ULL)
2400 #define STRESS_PERF_MAX (128)
2401
2402 /* per perf counter info */
2403 typedef struct {
2404 uint64_t counter; /* perf counter */
2405 int fd; /* perf per counter fd */
2406 } stress_perf_stat_t;
2407
2408 /* per stressor perf info */
2409 typedef struct {
2410 stress_perf_stat_t perf_stat[STRESS_PERF_MAX]; /* perf counters */
2411 int perf_opened; /* count of opened counters */
2412 } stress_perf_t;
2413 #endif
2414
2415 /* linux thermal zones */
2416 #define STRESS_THERMAL_ZONES (1)
2417 #define STRESS_THERMAL_ZONES_MAX (31) /* best if prime */
2418
2419 #if defined(STRESS_THERMAL_ZONES)
2420 /* per stressor thermal zone info */
2421 typedef struct stress_tz_info {
2422 char *path; /* thermal zone path */
2423 char *type; /* thermal zone type */
2424 uint32_t type_instance; /* thermal zone instance # */
2425 size_t index; /* thermal zone # index */
2426 struct stress_tz_info *next; /* next thermal zone in list */
2427 } stress_tz_info_t;
2428
2429 typedef struct {
2430 uint64_t temperature; /* temperature in Celsius * 1000 */
2431 } stress_tz_stat_t;
2432
2433 typedef struct {
2434 stress_tz_stat_t tz_stat[STRESS_THERMAL_ZONES_MAX];
2435 } stress_tz_t;
2436 #endif
2437
2438 /* Per stressor statistics and accounting info */
2439 typedef struct {
2440 uint64_t counter; /* number of bogo ops */
2441 bool counter_ready; /* counter can be read */
2442 struct tms tms; /* run time stats of process */
2443 double start; /* wall clock start time */
2444 double finish; /* wall clock stop time */
2445 #if defined(STRESS_PERF_STATS)
2446 stress_perf_t sp; /* perf counters */
2447 #endif
2448 #if defined(STRESS_THERMAL_ZONES)
2449 stress_tz_t tz; /* thermal zones */
2450 #endif
2451 bool run_ok; /* true if stressor exited OK */
2452 stress_checksum_t *checksum; /* pointer to checksum data */
2453 stress_misc_stats_t misc_stats[STRESS_MISC_STATS_MAX];
2454 } stress_stats_t;
2455
2456 #define STRESS_WARN_HASH_MAX (128)
2457
2458 /* The stress-ng global shared memory segment */
2459 typedef struct {
2460 size_t length; /* Size of segment */
2461 uint8_t *mem_cache; /* Shared memory cache */
2462 uint64_t mem_cache_size; /* Bytes */
2463 uint16_t mem_cache_level; /* 1=L1, 2=L2, 3=L3 */
2464 uint16_t padding1; /* alignment padding */
2465 uint32_t mem_cache_ways; /* cache ways size */
2466 uint64_t zero; /* zero'd data */
2467 void *nullptr; /* Null pointer */
2468 pid_t (*vfork)(void); /* vfork syscall */
2469 stress_mapped_t mapped; /* mmap'd pages to help testing */
2470 struct {
2471 uint32_t hash[STRESS_WARN_HASH_MAX]; /* hash patterns */
2472 #if defined(HAVE_LIB_PTHREAD)
2473 shim_pthread_spinlock_t lock; /* protection lock */
2474 #endif
2475 } warn_once;
2476 uint32_t warn_once_flags; /* Warn once flags */
2477 struct {
2478 uint64_t val64;
2479 uint32_t val32;
2480 uint16_t val16;
2481 uint8_t val8;
2482 uint8_t padding2; /* more padding */
2483 } atomic; /* Shared atomic temp vars */
2484 struct {
2485 uint32_t futex[STRESS_PROCS_MAX]; /* Shared futexes */
2486 uint64_t timeout[STRESS_PROCS_MAX]; /* Shared futex timeouts */
2487 } futex;
2488 struct {
2489 key_t key_id; /* System V semaphore key id */
2490 int sem_id; /* System V semaphore id */
2491 bool init; /* System V semaphore initialized */
2492 } sem_sysv;
2493 #if defined(STRESS_PERF_STATS)
2494 struct {
2495 bool no_perf; /* true = Perf not available */
2496 shim_pthread_spinlock_t lock; /* spinlock on no_perf updates */
2497 } perf;
2498 #endif
2499 bool *af_alg_hash_skip; /* Shared array of hash skip flags */
2500 bool *af_alg_cipher_skip; /* Shared array of cipher skip flags */
2501 #if defined(STRESS_THERMAL_ZONES)
2502 stress_tz_info_t *tz_info; /* List of valid thermal zones */
2503 #endif
2504 #if defined(HAVE_ATOMIC)
2505 uint32_t softlockup_count; /* Atomic counter of softlock children */
2506 #endif
2507 struct {
2508 #if defined(HAVE_LIB_PTHREAD)
2509 shim_pthread_spinlock_t lock; /* protection lock */
2510 #endif
2511 double start_time ALIGNED(8); /* Time to complete operation */
2512 } syncload;
2513 uint8_t str_shared[STR_SHARED_SIZE]; /* str copying buffer */
2514 stress_checksum_t *checksums; /* per stressor counter checksum */
2515 size_t checksums_length; /* size of checksums mapping */
2516 stress_stats_t stats[0]; /* Shared statistics */
2517 } stress_shared_t;
2518
2519 /* Stress test classes */
2520 typedef struct {
2521 stress_class_t class; /* Class type bit mask */
2522 const char *name; /* Name of class */
2523 } stress_class_info_t;
2524
2525 #define STRESSORS(MACRO) \
2526 MACRO(access) \
2527 MACRO(af_alg) \
2528 MACRO(affinity) \
2529 MACRO(aio) \
2530 MACRO(aiol) \
2531 MACRO(apparmor) \
2532 MACRO(alarm) \
2533 MACRO(atomic) \
2534 MACRO(bad_altstack) \
2535 MACRO(bad_ioctl) \
2536 MACRO(bigheap) \
2537 MACRO(bind_mount) \
2538 MACRO(binderfs) \
2539 MACRO(branch) \
2540 MACRO(brk) \
2541 MACRO(bsearch) \
2542 MACRO(cache) \
2543 MACRO(cap) \
2544 MACRO(chattr) \
2545 MACRO(chdir) \
2546 MACRO(chmod) \
2547 MACRO(chown) \
2548 MACRO(chroot) \
2549 MACRO(clock) \
2550 MACRO(clone) \
2551 MACRO(close) \
2552 MACRO(context) \
2553 MACRO(copy_file) \
2554 MACRO(cpu) \
2555 MACRO(cpu_online) \
2556 MACRO(crypt) \
2557 MACRO(cyclic) \
2558 MACRO(daemon) \
2559 MACRO(dccp) \
2560 MACRO(dentry) \
2561 MACRO(dev) \
2562 MACRO(dev_shm) \
2563 MACRO(dir) \
2564 MACRO(dirdeep) \
2565 MACRO(dirmany) \
2566 MACRO(dnotify) \
2567 MACRO(dup) \
2568 MACRO(dynlib) \
2569 MACRO(efivar) \
2570 MACRO(enosys) \
2571 MACRO(env) \
2572 MACRO(epoll) \
2573 MACRO(eventfd) \
2574 MACRO(exec) \
2575 MACRO(exit_group) \
2576 MACRO(fallocate) \
2577 MACRO(fanotify) \
2578 MACRO(fault) \
2579 MACRO(fcntl) \
2580 MACRO(fiemap) \
2581 MACRO(fifo) \
2582 MACRO(file_ioctl) \
2583 MACRO(filename) \
2584 MACRO(flock) \
2585 MACRO(fork) \
2586 MACRO(fp_error) \
2587 MACRO(fpunch) \
2588 MACRO(fstat) \
2589 MACRO(full) \
2590 MACRO(funccall) \
2591 MACRO(funcret) \
2592 MACRO(futex) \
2593 MACRO(get) \
2594 MACRO(getdent) \
2595 MACRO(getrandom) \
2596 MACRO(goto) \
2597 MACRO(handle) \
2598 MACRO(hdd) \
2599 MACRO(heapsort) \
2600 MACRO(hrtimers) \
2601 MACRO(hsearch) \
2602 MACRO(icache) \
2603 MACRO(icmp_flood) \
2604 MACRO(idle_page) \
2605 MACRO(inode_flags) \
2606 MACRO(inotify) \
2607 MACRO(io) \
2608 MACRO(iomix) \
2609 MACRO(ioport) \
2610 MACRO(ioprio) \
2611 MACRO(io_uring) \
2612 MACRO(ipsec_mb) \
2613 MACRO(itimer) \
2614 MACRO(judy) \
2615 MACRO(kcmp) \
2616 MACRO(key) \
2617 MACRO(kill) \
2618 MACRO(klog) \
2619 MACRO(l1cache) \
2620 MACRO(landlock) \
2621 MACRO(lease) \
2622 MACRO(link) \
2623 MACRO(list) \
2624 MACRO(loadavg) \
2625 MACRO(locka) \
2626 MACRO(lockbus) \
2627 MACRO(lockf) \
2628 MACRO(lockofd) \
2629 MACRO(longjmp) \
2630 MACRO(loop) \
2631 MACRO(lsearch) \
2632 MACRO(madvise) \
2633 MACRO(malloc) \
2634 MACRO(matrix) \
2635 MACRO(matrix_3d) \
2636 MACRO(mcontend) \
2637 MACRO(membarrier) \
2638 MACRO(memcpy) \
2639 MACRO(memfd) \
2640 MACRO(memhotplug) \
2641 MACRO(memrate) \
2642 MACRO(memthrash) \
2643 MACRO(mergesort) \
2644 MACRO(mincore) \
2645 MACRO(misaligned) \
2646 MACRO(mknod) \
2647 MACRO(mlock) \
2648 MACRO(mlockmany) \
2649 MACRO(mmap) \
2650 MACRO(mmapaddr) \
2651 MACRO(mmapfixed) \
2652 MACRO(mmapfork) \
2653 MACRO(mmaphuge) \
2654 MACRO(mmapmany) \
2655 MACRO(mq) \
2656 MACRO(mremap) \
2657 MACRO(msg) \
2658 MACRO(msync) \
2659 MACRO(munmap) \
2660 MACRO(nanosleep) \
2661 MACRO(netdev) \
2662 MACRO(netlink_proc) \
2663 MACRO(netlink_task) \
2664 MACRO(nice) \
2665 MACRO(nop) \
2666 MACRO(null) \
2667 MACRO(numa) \
2668 MACRO(oom_pipe) \
2669 MACRO(opcode) \
2670 MACRO(open) \
2671 MACRO(pci) \
2672 MACRO(personality) \
2673 MACRO(physpage) \
2674 MACRO(pidfd) \
2675 MACRO(ping_sock) \
2676 MACRO(pipe) \
2677 MACRO(pipeherd) \
2678 MACRO(pkey) \
2679 MACRO(poll) \
2680 MACRO(prctl) \
2681 MACRO(prefetch) \
2682 MACRO(procfs) \
2683 MACRO(pthread) \
2684 MACRO(ptrace) \
2685 MACRO(pty) \
2686 MACRO(qsort) \
2687 MACRO(quota) \
2688 MACRO(radixsort) \
2689 MACRO(randlist) \
2690 MACRO(ramfs) \
2691 MACRO(rawdev) \
2692 MACRO(rawpkt) \
2693 MACRO(rawsock) \
2694 MACRO(rawudp) \
2695 MACRO(rdrand) \
2696 MACRO(readahead) \
2697 MACRO(reboot) \
2698 MACRO(remap) \
2699 MACRO(rename) \
2700 MACRO(resched) \
2701 MACRO(resources) \
2702 MACRO(revio) \
2703 MACRO(rlimit) \
2704 MACRO(rmap) \
2705 MACRO(rseq) \
2706 MACRO(rtc) \
2707 MACRO(schedpolicy) \
2708 MACRO(sctp) \
2709 MACRO(seal) \
2710 MACRO(seccomp) \
2711 MACRO(secretmem) \
2712 MACRO(seek) \
2713 MACRO(sem) \
2714 MACRO(sem_sysv) \
2715 MACRO(sendfile) \
2716 MACRO(session) \
2717 MACRO(set) \
2718 MACRO(shellsort) \
2719 MACRO(shm) \
2720 MACRO(shm_sysv) \
2721 MACRO(sigabrt) \
2722 MACRO(sigchld) \
2723 MACRO(sigfd) \
2724 MACRO(sigfpe) \
2725 MACRO(sigio) \
2726 MACRO(signal) \
2727 MACRO(signest) \
2728 MACRO(sigpending) \
2729 MACRO(sigpipe) \
2730 MACRO(sigq) \
2731 MACRO(sigrt) \
2732 MACRO(sigsegv) \
2733 MACRO(sigsuspend) \
2734 MACRO(sigtrap) \
2735 MACRO(skiplist) \
2736 MACRO(sleep) \
2737 MACRO(smi) \
2738 MACRO(sock) \
2739 MACRO(sockabuse) \
2740 MACRO(sockdiag) \
2741 MACRO(sockfd) \
2742 MACRO(sockpair) \
2743 MACRO(sockmany) \
2744 MACRO(softlockup) \
2745 MACRO(spawn) \
2746 MACRO(splice) \
2747 MACRO(stack) \
2748 MACRO(stackmmap) \
2749 MACRO(str) \
2750 MACRO(stream) \
2751 MACRO(swap) \
2752 MACRO(switch) \
2753 MACRO(symlink) \
2754 MACRO(sync_file) \
2755 MACRO(syncload) \
2756 MACRO(sysbadaddr) \
2757 MACRO(sysinfo) \
2758 MACRO(sysinval) \
2759 MACRO(sysfs) \
2760 MACRO(tee) \
2761 MACRO(timer) \
2762 MACRO(timerfd) \
2763 MACRO(tlb_shootdown) \
2764 MACRO(tmpfs) \
2765 MACRO(tree) \
2766 MACRO(tsc) \
2767 MACRO(tsearch) \
2768 MACRO(tun) \
2769 MACRO(udp) \
2770 MACRO(udp_flood) \
2771 MACRO(unshare) \
2772 MACRO(uprobe) \
2773 MACRO(urandom) \
2774 MACRO(userfaultfd) \
2775 MACRO(utime) \
2776 MACRO(vdso) \
2777 MACRO(vecmath) \
2778 MACRO(vecwide) \
2779 MACRO(verity) \
2780 MACRO(vfork) \
2781 MACRO(vforkmany) \
2782 MACRO(vm) \
2783 MACRO(vm_addr) \
2784 MACRO(vm_rw) \
2785 MACRO(vm_segv) \
2786 MACRO(vm_splice) \
2787 MACRO(wait) \
2788 MACRO(watchdog) \
2789 MACRO(wcs) \
2790 MACRO(x86syscall) \
2791 MACRO(xattr) \
2792 MACRO(yield) \
2793 MACRO(zero) \
2794 MACRO(zlib) \
2795 MACRO(zombie)
2796
2797 /*
2798 * Declaration of stress_*_info object
2799 */
2800 #define STRESSOR_ENUM(name) \
2801 STRESS_ ## name,
2802
2803 /*
2804 * Declaration of stress_*_info object
2805 */
2806 #define STRESSOR_DECL(name) \
2807 extern stressor_info_t stress_ ## name ## _info;
2808
2809 STRESSORS(STRESSOR_DECL)
2810
2811 /* Stress tests */
2812 typedef enum {
2813 STRESS_START = -1,
2814 STRESSORS(STRESSOR_ENUM)
2815 /* STRESS_MAX must be last one */
2816 STRESS_MAX
2817 } stress_id_t;
2818
2819 /* Command line long options */
2820 typedef enum {
2821 OPT_undefined = 0,
2822 /* Short options */
2823 OPT_query = '?',
2824 OPT_all = 'a',
2825 OPT_backoff = 'b',
2826 OPT_bigheap = 'B',
2827 OPT_cpu = 'c',
2828 OPT_cache = 'C',
2829 OPT_hdd = 'd',
2830 OPT_dentry = 'D',
2831 OPT_fork = 'f',
2832 OPT_fallocate = 'F',
2833 OPT_io = 'i',
2834 OPT_job = 'j',
2835 OPT_help = 'h',
2836 OPT_keep_name = 'k',
2837 OPT_cpu_load = 'l',
2838 OPT_vm = 'm',
2839 OPT_metrics = 'M',
2840 OPT_dry_run = 'n',
2841 OPT_rename = 'R',
2842 OPT_open = 'o',
2843 OPT_pipe = 'p',
2844 OPT_poll = 'P',
2845 OPT_quiet = 'q',
2846 OPT_random = 'r',
2847 OPT_switch = 's',
2848 OPT_sock = 'S',
2849 OPT_timeout = 't',
2850 OPT_timer = 'T',
2851 OPT_urandom = 'u',
2852 OPT_verbose = 'v',
2853 OPT_version = 'V',
2854 OPT_yield = 'y',
2855 OPT_yaml = 'Y',
2856 OPT_exclude = 'x',
2857
2858 /* Long options only */
2859
2860 OPT_long_ops_start = 0x7f,
2861
2862 OPT_abort,
2863
2864 OPT_access,
2865 OPT_access_ops,
2866
2867 OPT_affinity,
2868 OPT_affinity_delay,
2869 OPT_affinity_ops,
2870 OPT_affinity_pin,
2871 OPT_affinity_rand,
2872 OPT_affinity_sleep,
2873
2874 OPT_af_alg,
2875 OPT_af_alg_ops,
2876 OPT_af_alg_dump,
2877
2878 OPT_aggressive,
2879
2880 OPT_aio,
2881 OPT_aio_ops,
2882 OPT_aio_requests,
2883
2884 OPT_aiol,
2885 OPT_aiol_ops,
2886 OPT_aiol_requests,
2887
2888 OPT_alarm,
2889 OPT_alarm_ops,
2890
2891 OPT_apparmor,
2892 OPT_apparmor_ops,
2893
2894 OPT_atomic,
2895 OPT_atomic_ops,
2896
2897 OPT_bad_altstack,
2898 OPT_bad_altstack_ops,
2899
2900 OPT_bad_ioctl,
2901 OPT_bad_ioctl_ops,
2902
2903 OPT_branch,
2904 OPT_branch_ops,
2905
2906 OPT_brk,
2907 OPT_brk_ops,
2908 OPT_brk_mlock,
2909 OPT_brk_notouch,
2910
2911 OPT_bsearch,
2912 OPT_bsearch_ops,
2913 OPT_bsearch_size,
2914
2915 OPT_bigheap_ops,
2916 OPT_bigheap_growth,
2917
2918 OPT_bind_mount,
2919 OPT_bind_mount_ops,
2920
2921 OPT_binderfs,
2922 OPT_binderfs_ops,
2923
2924 OPT_class,
2925 OPT_cache_ops,
2926 OPT_cache_clflushopt,
2927 OPT_cache_cldemote,
2928 OPT_cache_prefetch,
2929 OPT_cache_flush,
2930 OPT_cache_fence,
2931 OPT_cache_sfence,
2932 OPT_cache_level,
2933 OPT_cache_ways,
2934 OPT_cache_no_affinity,
2935
2936 OPT_cap,
2937 OPT_cap_ops,
2938
2939 OPT_chattr,
2940 OPT_chattr_ops,
2941
2942 OPT_chdir,
2943 OPT_chdir_dirs,
2944 OPT_chdir_ops,
2945
2946 OPT_chmod,
2947 OPT_chmod_ops,
2948
2949 OPT_chown,
2950 OPT_chown_ops,
2951
2952 OPT_chroot,
2953 OPT_chroot_ops,
2954
2955 OPT_clock,
2956 OPT_clock_ops,
2957
2958 OPT_clone,
2959 OPT_clone_ops,
2960 OPT_clone_max,
2961
2962 OPT_close,
2963 OPT_close_ops,
2964
2965 OPT_context,
2966 OPT_context_ops,
2967
2968 OPT_copy_file,
2969 OPT_copy_file_ops,
2970 OPT_copy_file_bytes,
2971
2972 OPT_cpu_ops,
2973 OPT_cpu_method,
2974 OPT_cpu_load_slice,
2975
2976 OPT_cpu_online,
2977 OPT_cpu_online_ops,
2978 OPT_cpu_online_all,
2979
2980 OPT_crypt,
2981 OPT_crypt_ops,
2982
2983 OPT_cyclic,
2984 OPT_cyclic_ops,
2985 OPT_cyclic_method,
2986 OPT_cyclic_policy,
2987 OPT_cyclic_prio,
2988 OPT_cyclic_sleep,
2989 OPT_cyclic_dist,
2990
2991 OPT_daemon,
2992 OPT_daemon_ops,
2993
2994 OPT_dccp,
2995 OPT_dccp_domain,
2996 OPT_dccp_ops,
2997 OPT_dccp_opts,
2998 OPT_dccp_port,
2999
3000 OPT_dentry_ops,
3001 OPT_dentries,
3002 OPT_dentry_order,
3003
3004 OPT_dev,
3005 OPT_dev_ops,
3006 OPT_dev_file,
3007
3008 OPT_dev_shm,
3009 OPT_dev_shm_ops,
3010
3011 OPT_dir,
3012 OPT_dir_ops,
3013 OPT_dir_dirs,
3014
3015 OPT_dirdeep,
3016 OPT_dirdeep_ops,
3017 OPT_dirdeep_dirs,
3018 OPT_dirdeep_inodes,
3019
3020 OPT_dirmany,
3021 OPT_dirmany_ops,
3022
3023 OPT_dnotify,
3024 OPT_dnotify_ops,
3025
3026 OPT_dup,
3027 OPT_dup_ops,
3028
3029 OPT_dynlib,
3030 OPT_dynlib_ops,
3031
3032 OPT_efivar,
3033 OPT_efivar_ops,
3034
3035 OPT_enosys,
3036 OPT_enosys_ops,
3037
3038 OPT_env,
3039 OPT_env_ops,
3040
3041 OPT_epoll,
3042 OPT_epoll_ops,
3043 OPT_epoll_port,
3044 OPT_epoll_domain,
3045
3046 OPT_eventfd,
3047 OPT_eventfd_ops,
3048 OPT_eventfd_nonblock,
3049
3050 OPT_exec,
3051 OPT_exec_ops,
3052 OPT_exec_max,
3053
3054 OPT_exit_group,
3055 OPT_exit_group_ops,
3056
3057 OPT_fallocate_ops,
3058 OPT_fallocate_bytes,
3059
3060 OPT_fanotify,
3061 OPT_fanotify_ops,
3062
3063 OPT_fault,
3064 OPT_fault_ops,
3065
3066 OPT_fcntl,
3067 OPT_fcntl_ops,
3068
3069 OPT_fiemap,
3070 OPT_fiemap_ops,
3071 OPT_fiemap_bytes,
3072
3073 OPT_fifo,
3074 OPT_fifo_ops,
3075 OPT_fifo_readers,
3076
3077 OPT_file_ioctl,
3078 OPT_file_ioctl_ops,
3079
3080 OPT_filename,
3081 OPT_filename_ops,
3082 OPT_filename_opts,
3083
3084 OPT_flock,
3085 OPT_flock_ops,
3086
3087 OPT_fork_ops,
3088 OPT_fork_max,
3089 OPT_fork_vm,
3090
3091 OPT_fp_error,
3092 OPT_fp_error_ops,
3093
3094 OPT_fpunch,
3095 OPT_fpunch_ops,
3096
3097 OPT_fstat,
3098 OPT_fstat_ops,
3099 OPT_fstat_dir,
3100
3101 OPT_ftrace,
3102
3103 OPT_full,
3104 OPT_full_ops,
3105
3106 OPT_funccall,
3107 OPT_funccall_ops,
3108 OPT_funccall_method,
3109
3110 OPT_funcret,
3111 OPT_funcret_ops,
3112 OPT_funcret_method,
3113
3114 OPT_futex,
3115 OPT_futex_ops,
3116
3117 OPT_get,
3118 OPT_get_ops,
3119
3120 OPT_getrandom,
3121 OPT_getrandom_ops,
3122
3123 OPT_getdent,
3124 OPT_getdent_ops,
3125
3126 OPT_goto,
3127 OPT_goto_ops,
3128 OPT_goto_direction,
3129
3130 OPT_handle,
3131 OPT_handle_ops,
3132
3133 OPT_hdd_bytes,
3134 OPT_hdd_write_size,
3135 OPT_hdd_ops,
3136 OPT_hdd_opts,
3137
3138 OPT_heapsort,
3139 OPT_heapsort_ops,
3140 OPT_heapsort_integers,
3141
3142 OPT_hrtimers,
3143 OPT_hrtimers_ops,
3144 OPT_hrtimers_adjust,
3145
3146 OPT_hsearch,
3147 OPT_hsearch_ops,
3148 OPT_hsearch_size,
3149
3150 OPT_icache,
3151 OPT_icache_ops,
3152
3153 OPT_icmp_flood,
3154 OPT_icmp_flood_ops,
3155
3156 OPT_idle_page,
3157 OPT_idle_page_ops,
3158
3159 OPT_ignite_cpu,
3160
3161 OPT_inode_flags,
3162 OPT_inode_flags_ops,
3163
3164 OPT_inotify,
3165 OPT_inotify_ops,
3166
3167 OPT_iomix,
3168 OPT_iomix_bytes,
3169 OPT_iomix_ops,
3170
3171 OPT_ioport,
3172 OPT_ioport_ops,
3173 OPT_ioport_opts,
3174
3175 OPT_ionice_class,
3176 OPT_ionice_level,
3177
3178 OPT_ioprio,
3179 OPT_ioprio_ops,
3180
3181 OPT_iostat,
3182
3183 OPT_io_ops,
3184
3185 OPT_io_uring,
3186 OPT_io_uring_ops,
3187
3188 OPT_ipsec_mb,
3189 OPT_ipsec_mb_ops,
3190 OPT_ipsec_mb_feature,
3191
3192 OPT_itimer,
3193 OPT_itimer_ops,
3194 OPT_itimer_freq,
3195 OPT_itimer_rand,
3196
3197 OPT_judy,
3198 OPT_judy_ops,
3199 OPT_judy_size,
3200
3201 OPT_kcmp,
3202 OPT_kcmp_ops,
3203
3204 OPT_key,
3205 OPT_key_ops,
3206
3207 OPT_kill,
3208 OPT_kill_ops,
3209
3210 OPT_klog,
3211 OPT_klog_ops,
3212
3213 OPT_l1cache,
3214 OPT_l1cache_ops,
3215 OPT_l1cache_line_size,
3216 OPT_l1cache_size,
3217 OPT_l1cache_sets,
3218 OPT_l1cache_ways,
3219
3220 OPT_landlock,
3221 OPT_landlock_ops,
3222
3223 OPT_lease,
3224 OPT_lease_ops,
3225 OPT_lease_breakers,
3226
3227 OPT_link,
3228 OPT_link_ops,
3229
3230 OPT_list,
3231 OPT_list_ops,
3232 OPT_list_method,
3233 OPT_list_size,
3234
3235 OPT_loadavg,
3236 OPT_loadavg_ops,
3237
3238 OPT_lockbus,
3239 OPT_lockbus_ops,
3240
3241 OPT_locka,
3242 OPT_locka_ops,
3243
3244 OPT_lockf,
3245 OPT_lockf_ops,
3246 OPT_lockf_nonblock,
3247
3248 OPT_lockofd,
3249 OPT_lockofd_ops,
3250
3251 OPT_log_brief,
3252 OPT_log_file,
3253
3254 OPT_longjmp,
3255 OPT_longjmp_ops,
3256
3257 OPT_loop,
3258 OPT_loop_ops,
3259
3260 OPT_lsearch,
3261 OPT_lsearch_ops,
3262 OPT_lsearch_size,
3263
3264 OPT_madvise,
3265 OPT_madvise_ops,
3266
3267 OPT_malloc,
3268 OPT_malloc_ops,
3269 OPT_malloc_bytes,
3270 OPT_malloc_max,
3271 OPT_malloc_pthreads,
3272 OPT_malloc_threshold,
3273 OPT_malloc_touch,
3274
3275 OPT_matrix,
3276 OPT_matrix_ops,
3277 OPT_matrix_size,
3278 OPT_matrix_method,
3279 OPT_matrix_yx,
3280
3281 OPT_matrix_3d,
3282 OPT_matrix_3d_ops,
3283 OPT_matrix_3d_size,
3284 OPT_matrix_3d_method,
3285 OPT_matrix_3d_zyx,
3286
3287 OPT_maximize,
3288 OPT_max_fd,
3289
3290 OPT_mcontend,
3291 OPT_mcontend_ops,
3292
3293 OPT_membarrier,
3294 OPT_membarrier_ops,
3295
3296 OPT_memcpy,
3297 OPT_memcpy_ops,
3298 OPT_memcpy_method,
3299
3300 OPT_memfd,
3301 OPT_memfd_ops,
3302 OPT_memfd_bytes,
3303 OPT_memfd_fds,
3304
3305 OPT_memhotplug,
3306 OPT_memhotplug_ops,
3307
3308 OPT_memrate,
3309 OPT_memrate_ops,
3310 OPT_memrate_rd_mbs,
3311 OPT_memrate_wr_mbs,
3312 OPT_memrate_bytes,
3313
3314 OPT_memthrash,
3315 OPT_memthrash_ops,
3316 OPT_memthrash_method,
3317
3318 OPT_mergesort,
3319 OPT_mergesort_ops,
3320 OPT_mergesort_integers,
3321
3322 OPT_metrics_brief,
3323
3324 OPT_mincore,
3325 OPT_mincore_ops,
3326 OPT_mincore_rand,
3327
3328 OPT_misaligned,
3329 OPT_misaligned_ops,
3330 OPT_misaligned_method,
3331
3332 OPT_mknod,
3333 OPT_mknod_ops,
3334
3335 OPT_minimize,
3336
3337 OPT_mlock,
3338 OPT_mlock_ops,
3339
3340 OPT_mlockmany,
3341 OPT_mlockmany_ops,
3342 OPT_mlockmany_procs,
3343
3344 OPT_mmap,
3345 OPT_mmap_ops,
3346 OPT_mmap_bytes,
3347 OPT_mmap_file,
3348 OPT_mmap_async,
3349 OPT_mmap_mprotect,
3350 OPT_mmap_osync,
3351 OPT_mmap_odirect,
3352 OPT_mmap_mmap2,
3353
3354 OPT_mmapaddr,
3355 OPT_mmapaddr_ops,
3356
3357 OPT_mmapfixed,
3358 OPT_mmapfixed_ops,
3359
3360 OPT_mmapfork,
3361 OPT_mmapfork_ops,
3362
3363 OPT_mmaphuge,
3364 OPT_mmaphuge_ops,
3365 OPT_mmaphuge_mmaps,
3366
3367 OPT_mmapmany,
3368 OPT_mmapmany_ops,
3369
3370 OPT_mq,
3371 OPT_mq_ops,
3372 OPT_mq_size,
3373
3374 OPT_mremap,
3375 OPT_mremap_ops,
3376 OPT_mremap_bytes,
3377 OPT_mremap_mlock,
3378
3379 OPT_msg,
3380 OPT_msg_ops,
3381 OPT_msg_types,
3382
3383 OPT_msync,
3384 OPT_msync_bytes,
3385 OPT_msync_ops,
3386
3387 OPT_munmap,
3388 OPT_munmap_ops,
3389
3390 OPT_nanosleep,
3391 OPT_nanosleep_ops,
3392
3393 OPT_netdev,
3394 OPT_netdev_ops,
3395
3396 OPT_netlink_proc,
3397 OPT_netlink_proc_ops,
3398
3399 OPT_netlink_task,
3400 OPT_netlink_task_ops,
3401
3402 OPT_nice,
3403 OPT_nice_ops,
3404
3405 OPT_no_madvise,
3406 OPT_no_oom_adjust,
3407 OPT_no_rand_seed,
3408
3409 OPT_nop,
3410 OPT_nop_ops,
3411 OPT_nop_instr,
3412
3413 OPT_null,
3414 OPT_null_ops,
3415
3416 OPT_numa,
3417 OPT_numa_ops,
3418
3419 OPT_oomable,
3420
3421 OPT_oom_pipe,
3422 OPT_oom_pipe_ops,
3423
3424 OPT_opcode,
3425 OPT_opcode_ops,
3426 OPT_opcode_method,
3427
3428 OPT_open_ops,
3429 OPT_open_fd,
3430
3431 OPT_page_in,
3432 OPT_pathological,
3433
3434 OPT_pci,
3435 OPT_pci_ops,
3436
3437 OPT_perf_stats,
3438
3439 OPT_personality,
3440 OPT_personality_ops,
3441
3442 OPT_physpage,
3443 OPT_physpage_ops,
3444
3445 OPT_pidfd,
3446 OPT_pidfd_ops,
3447
3448 OPT_ping_sock,
3449 OPT_ping_sock_ops,
3450
3451 OPT_pipe_ops,
3452 OPT_pipe_size,
3453 OPT_pipe_data_size,
3454
3455 OPT_pipeherd,
3456 OPT_pipeherd_ops,
3457 OPT_pipeherd_yield,
3458
3459 OPT_pkey,
3460 OPT_pkey_ops,
3461
3462 OPT_poll_ops,
3463 OPT_poll_fds,
3464
3465 OPT_prefetch,
3466 OPT_prefetch_ops,
3467 OPT_prefetch_l3_size,
3468
3469 OPT_prctl,
3470 OPT_prctl_ops,
3471
3472 OPT_procfs,
3473 OPT_procfs_ops,
3474
3475 OPT_pthread,
3476 OPT_pthread_ops,
3477 OPT_pthread_max,
3478
3479 OPT_ptrace,
3480 OPT_ptrace_ops,
3481
3482 OPT_pty,
3483 OPT_pty_ops,
3484 OPT_pty_max,
3485
3486 OPT_qsort,
3487 OPT_qsort_ops,
3488 OPT_qsort_integers,
3489
3490 OPT_quota,
3491 OPT_quota_ops,
3492
3493 OPT_radixsort,
3494 OPT_radixsort_ops,
3495 OPT_radixsort_size,
3496
3497 OPT_randlist,
3498 OPT_randlist_ops,
3499 OPT_randlist_compact,
3500 OPT_randlist_items,
3501 OPT_randlist_size,
3502
3503 OPT_ramfs,
3504 OPT_ramfs_ops,
3505 OPT_ramfs_size,
3506
3507 OPT_rawdev,
3508 OPT_rawdev_method,
3509 OPT_rawdev_ops,
3510
3511 OPT_rawpkt,
3512 OPT_rawpkt_ops,
3513 OPT_rawpkt_port,
3514
3515 OPT_rawsock,
3516 OPT_rawsock_ops,
3517
3518 OPT_rawudp,
3519 OPT_rawudp_ops,
3520 OPT_rawudp_port,
3521
3522 OPT_rdrand,
3523 OPT_rdrand_ops,
3524 OPT_rdrand_seed,
3525
3526 OPT_readahead,
3527 OPT_readahead_ops,
3528 OPT_readahead_bytes,
3529
3530 OPT_reboot,
3531 OPT_reboot_ops,
3532
3533 OPT_remap,
3534 OPT_remap_ops,
3535
3536 OPT_rename_ops,
3537
3538 OPT_resched,
3539 OPT_resched_ops,
3540
3541 OPT_resources,
3542 OPT_resources_ops,
3543
3544 OPT_revio,
3545 OPT_revio_ops,
3546 OPT_revio_opts,
3547 OPT_revio_bytes,
3548
3549 OPT_rlimit,
3550 OPT_rlimit_ops,
3551
3552 OPT_rmap,
3553 OPT_rmap_ops,
3554
3555 OPT_rseq,
3556 OPT_rseq_ops,
3557
3558 OPT_rtc,
3559 OPT_rtc_ops,
3560
3561 OPT_sched,
3562 OPT_sched_prio,
3563
3564 OPT_schedpolicy,
3565 OPT_schedpolicy_ops,
3566
3567 OPT_sched_period,
3568 OPT_sched_runtime,
3569 OPT_sched_deadline,
3570 OPT_sched_reclaim,
3571
3572 OPT_sctp,
3573 OPT_sctp_ops,
3574 OPT_sctp_domain,
3575 OPT_sctp_port,
3576
3577 OPT_seal,
3578 OPT_seal_ops,
3579
3580 OPT_seccomp,
3581 OPT_seccomp_ops,
3582
3583 OPT_secretmem,
3584 OPT_secretmem_ops,
3585
3586 OPT_seed,
3587
3588 OPT_seek,
3589 OPT_seek_ops,
3590 OPT_seek_punch,
3591 OPT_seek_size,
3592
3593 OPT_sendfile,
3594 OPT_sendfile_ops,
3595 OPT_sendfile_size,
3596
3597 OPT_sem,
3598 OPT_sem_ops,
3599 OPT_sem_procs,
3600
3601 OPT_sem_sysv,
3602 OPT_sem_sysv_ops,
3603 OPT_sem_sysv_procs,
3604
3605 OPT_session,
3606 OPT_session_ops,
3607
3608 OPT_set,
3609 OPT_set_ops,
3610
3611 OPT_shellsort,
3612 OPT_shellsort_ops,
3613 OPT_shellsort_integers,
3614
3615 OPT_shm,
3616 OPT_shm_ops,
3617 OPT_shm_bytes,
3618 OPT_shm_objects,
3619
3620 OPT_shm_sysv,
3621 OPT_shm_sysv_ops,
3622 OPT_shm_sysv_bytes,
3623 OPT_shm_sysv_segments,
3624
3625 OPT_sequential,
3626
3627 OPT_sigabrt,
3628 OPT_sigabrt_ops,
3629
3630 OPT_sigchld,
3631 OPT_sigchld_ops,
3632
3633 OPT_sigfd,
3634 OPT_sigfd_ops,
3635
3636 OPT_sigfpe,
3637 OPT_sigfpe_ops,
3638
3639 OPT_sigio,
3640 OPT_sigio_ops,
3641
3642 OPT_signal,
3643 OPT_signal_ops,
3644
3645 OPT_signest,
3646 OPT_signest_ops,
3647
3648 OPT_sigpending,
3649 OPT_sigpending_ops,
3650
3651 OPT_sigpipe,
3652 OPT_sigpipe_ops,
3653
3654 OPT_sigq,
3655 OPT_sigq_ops,
3656
3657 OPT_sigrt,
3658 OPT_sigrt_ops,
3659
3660 OPT_sigsegv,
3661 OPT_sigsegv_ops,
3662
3663 OPT_sigsuspend,
3664 OPT_sigsuspend_ops,
3665
3666 OPT_sigtrap,
3667 OPT_sigtrap_ops,
3668
3669 OPT_skiplist,
3670 OPT_skiplist_ops,
3671 OPT_skiplist_size,
3672
3673 OPT_skip_silent,
3674
3675 OPT_sleep,
3676 OPT_sleep_ops,
3677 OPT_sleep_max,
3678
3679 OPT_smart,
3680
3681 OPT_smi,
3682 OPT_smi_ops,
3683
3684 OPT_sock_ops,
3685 OPT_sock_domain,
3686 OPT_sock_nodelay,
3687 OPT_sock_opts,
3688 OPT_sock_port,
3689 OPT_sock_protocol,
3690 OPT_sock_type,
3691 OPT_sock_zerocopy,
3692
3693 OPT_sockabuse,
3694 OPT_sockabuse_ops,
3695
3696 OPT_sockdiag,
3697 OPT_sockdiag_ops,
3698
3699 OPT_sockfd,
3700 OPT_sockfd_ops,
3701 OPT_sockfd_port,
3702
3703 OPT_sockmany,
3704 OPT_sockmany_ops,
3705
3706 OPT_sockpair,
3707 OPT_sockpair_ops,
3708
3709 OPT_softlockup,
3710 OPT_softlockup_ops,
3711
3712 OPT_swap,
3713 OPT_swap_ops,
3714
3715 OPT_switch_ops,
3716 OPT_switch_freq,
3717
3718 OPT_spawn,
3719 OPT_spawn_ops,
3720
3721 OPT_splice,
3722 OPT_splice_ops,
3723 OPT_splice_bytes,
3724
3725 OPT_stack,
3726 OPT_stack_ops,
3727 OPT_stack_fill,
3728 OPT_stack_mlock,
3729
3730 OPT_stackmmap,
3731 OPT_stackmmap_ops,
3732
3733 OPT_str,
3734 OPT_str_ops,
3735 OPT_str_method,
3736
3737 OPT_stream,
3738 OPT_stream_ops,
3739 OPT_stream_index,
3740 OPT_stream_l3_size,
3741 OPT_stream_madvise,
3742
3743 OPT_stressors,
3744
3745 OPT_symlink,
3746 OPT_symlink_ops,
3747
3748 OPT_sync_file,
3749 OPT_sync_file_ops,
3750 OPT_sync_file_bytes,
3751
3752 OPT_syncload,
3753 OPT_syncload_ops,
3754 OPT_syncload_msbusy,
3755 OPT_syncload_mssleep,
3756
3757 OPT_sysbadaddr,
3758 OPT_sysbadaddr_ops,
3759
3760 OPT_sysinfo,
3761 OPT_sysinfo_ops,
3762
3763 OPT_sysinval,
3764 OPT_sysinval_ops,
3765
3766 OPT_sysfs,
3767 OPT_sysfs_ops,
3768
3769 OPT_syslog,
3770
3771 OPT_tee,
3772 OPT_tee_ops,
3773
3774 OPT_taskset,
3775
3776 OPT_temp_path,
3777
3778 OPT_thermalstat,
3779 OPT_thermal_zones,
3780
3781 OPT_thrash,
3782
3783 OPT_timer_slack,
3784
3785 OPT_timer_ops,
3786 OPT_timer_freq,
3787 OPT_timer_rand,
3788
3789 OPT_timerfd,
3790 OPT_timerfd_ops,
3791 OPT_timerfd_fds,
3792 OPT_timerfd_freq,
3793 OPT_timerfd_rand,
3794
3795 OPT_times,
3796
3797 OPT_timestamp,
3798
3799 OPT_tlb_shootdown,
3800 OPT_tlb_shootdown_ops,
3801
3802 OPT_tmpfs,
3803 OPT_tmpfs_ops,
3804 OPT_tmpfs_mmap_async,
3805 OPT_tmpfs_mmap_file,
3806
3807 OPT_tree,
3808 OPT_tree_ops,
3809 OPT_tree_method,
3810 OPT_tree_size,
3811
3812 OPT_tsc,
3813 OPT_tsc_ops,
3814
3815 OPT_tsearch,
3816 OPT_tsearch_ops,
3817 OPT_tsearch_size,
3818
3819 OPT_tun,
3820 OPT_tun_ops,
3821 OPT_tun_tap,
3822
3823 OPT_udp,
3824 OPT_udp_ops,
3825 OPT_udp_port,
3826 OPT_udp_domain,
3827 OPT_udp_lite,
3828
3829 OPT_udp_flood,
3830 OPT_udp_flood_ops,
3831 OPT_udp_flood_domain,
3832
3833 OPT_unshare,
3834 OPT_unshare_ops,
3835
3836 OPT_uprobe,
3837 OPT_uprobe_ops,
3838
3839 OPT_urandom_ops,
3840
3841 OPT_userfaultfd,
3842 OPT_userfaultfd_ops,
3843 OPT_userfaultfd_bytes,
3844
3845 OPT_utime,
3846 OPT_utime_ops,
3847 OPT_utime_fsync,
3848
3849 OPT_vdso,
3850 OPT_vdso_ops,
3851 OPT_vdso_func,
3852
3853 OPT_vecmath,
3854 OPT_vecmath_ops,
3855
3856 OPT_vecwide,
3857 OPT_vecwide_ops,
3858
3859 OPT_verify,
3860
3861 OPT_verity,
3862 OPT_verity_ops,
3863
3864 OPT_vfork,
3865 OPT_vfork_ops,
3866 OPT_vfork_max,
3867 OPT_vfork_vm,
3868
3869 OPT_vforkmany,
3870 OPT_vforkmany_ops,
3871 OPT_vforkmany_vm,
3872
3873 OPT_vm_bytes,
3874 OPT_vm_hang,
3875 OPT_vm_keep,
3876 OPT_vm_mmap_populate,
3877 OPT_vm_mmap_locked,
3878 OPT_vm_ops,
3879 OPT_vm_madvise,
3880 OPT_vm_method,
3881
3882 OPT_vm_addr,
3883 OPT_vm_addr_method,
3884 OPT_vm_addr_ops,
3885
3886 OPT_vm_rw,
3887 OPT_vm_rw_ops,
3888 OPT_vm_rw_bytes,
3889
3890 OPT_vm_segv,
3891 OPT_vm_segv_ops,
3892
3893 OPT_vm_splice,
3894 OPT_vm_splice_ops,
3895 OPT_vm_splice_bytes,
3896
3897 OPT_vmstat,
3898
3899 OPT_wait,
3900 OPT_wait_ops,
3901
3902 OPT_watchdog,
3903 OPT_watchdog_ops,
3904
3905 OPT_wcs,
3906 OPT_wcs_ops,
3907 OPT_wcs_method,
3908
3909 OPT_x86syscall,
3910 OPT_x86syscall_ops,
3911 OPT_x86syscall_func,
3912
3913 OPT_xattr,
3914 OPT_xattr_ops,
3915
3916 OPT_yield_ops,
3917
3918 OPT_zero,
3919 OPT_zero_ops,
3920
3921 OPT_zlib,
3922 OPT_zlib_ops,
3923 OPT_zlib_level,
3924 OPT_zlib_mem_level,
3925 OPT_zlib_method,
3926 OPT_zlib_window_bits,
3927 OPT_zlib_stream_bytes,
3928 OPT_zlib_strategy,
3929
3930 OPT_zombie,
3931 OPT_zombie_ops,
3932 OPT_zombie_max,
3933 } stress_op_t;
3934
3935 /* stress test metadata */
3936 typedef struct {
3937 const stressor_info_t *info; /* stress test info */
3938 const stress_id_t id; /* stress test ID */
3939 const short int short_getopt; /* getopt short option */
3940 const stress_op_t op; /* ops option */
3941 const char *name; /* name of stress test */
3942 } stress_t;
3943
3944 /* Per stressor information */
3945 typedef struct stress_stressor_info {
3946 struct stress_stressor_info *next; /* next proc info struct in list */
3947 struct stress_stressor_info *prev; /* prev proc info struct in list */
3948 const stress_t *stressor; /* stressor */
3949 pid_t *pids; /* stressor process id */
3950 stress_stats_t **stats; /* stressor stats info */
3951 int32_t started_instances; /* count of started instances */
3952 int32_t num_instances; /* number of instances per stressor */
3953 uint64_t bogo_ops; /* number of bogo ops */
3954 } stress_stressor_t;
3955
3956 /* Pointer to current running stressor proc info */
3957 extern stress_stressor_t *g_stressor_current;
3958
3959 /* Scale lookup mapping, suffix -> scale by */
3960 typedef struct {
3961 const char ch; /* Scaling suffix */
3962 const uint64_t scale; /* Amount to scale by */
3963 } stress_scale_t;
3964
3965 /* Cache types */
3966 typedef enum stress_cache_type {
3967 CACHE_TYPE_UNKNOWN = 0, /* Unknown type */
3968 CACHE_TYPE_DATA, /* D$ */
3969 CACHE_TYPE_INSTRUCTION, /* I$ */
3970 CACHE_TYPE_UNIFIED, /* D$ + I$ */
3971 } stress_cache_type_t;
3972
3973 /* CPU cache information */
3974 typedef struct stress_cpu_cache {
3975 uint64_t size; /* cache size in bytes */
3976 uint32_t line_size; /* cache line size in bytes */
3977 uint32_t ways; /* cache ways */
3978 stress_cache_type_t type; /* cache type */
3979 uint16_t level; /* cache level, L1, L2 etc */
3980 } stress_cpu_cache_t;
3981
3982 typedef struct stress_cpu {
3983 stress_cpu_cache_t *caches; /* CPU cache data */
3984 uint32_t num; /* CPU # number */
3985 uint32_t cache_count; /* CPU cache # */
3986 bool online; /* CPU online when true */
3987 } stress_cpu_t;
3988
3989 typedef struct stress_cpus {
3990 stress_cpu_t *cpus; /* CPU data */
3991 uint32_t count; /* CPU count */
3992 } stress_cpus_t;
3993
3994 /* Various global option settings and flags */
3995 extern const char g_app_name[]; /* Name of application */
3996 extern stress_shared_t *g_shared; /* shared memory */
3997 extern uint64_t g_opt_timeout; /* timeout in seconds */
3998 extern uint64_t g_opt_flags; /* option flags */
3999 extern int32_t g_opt_sequential; /* Number of sequential stressors */
4000 extern int32_t g_opt_parallel; /* Number of parallel stressors */
4001 extern volatile bool g_keep_stressing_flag; /* false to exit stressor */
4002 extern volatile bool g_caught_sigint; /* true if stopped by SIGINT */
4003 extern pid_t g_pgrp; /* proceess group leader */
4004 extern jmp_buf g_error_env; /* parsing error env */
4005 extern stress_put_val_t g_put_val; /* sync data to somewhere */
4006
keep_stressing_flag(void)4007 static inline bool ALWAYS_INLINE OPTIMIZE3 keep_stressing_flag(void)
4008 {
4009 return g_keep_stressing_flag;
4010 }
4011
keep_stressing_set_flag(const bool setting)4012 static inline void ALWAYS_INLINE OPTIMIZE3 keep_stressing_set_flag(const bool setting)
4013 {
4014 g_keep_stressing_flag = setting;
4015 }
4016
4017 /*
4018 * keep_stressing()
4019 * returns true if we can keep on running a stressor
4020 */
keep_stressing(const stress_args_t * args)4021 static inline bool ALWAYS_INLINE OPTIMIZE3 keep_stressing(const stress_args_t *args)
4022 {
4023 return (LIKELY(g_keep_stressing_flag) &&
4024 LIKELY(!args->max_ops || (get_counter(args) < args->max_ops)));
4025 }
4026
4027 /*
4028 * stressor option value handling
4029 */
4030 extern int stress_set_setting(const char *name, const stress_type_id_t type_id,
4031 const void *value);
4032 extern int stress_set_setting_global(const char *name, const stress_type_id_t type_id,
4033 const void *value);
4034 extern bool stress_get_setting(const char *name, void *value);
4035 extern void stress_settings_free(void);
4036
4037 /*
4038 * externs to force gcc to stash computed values and hence
4039 * to stop the optimiser optimising code away to zero. The
4040 * *_put funcs are essentially no-op functions.
4041 */
4042 extern uint64_t stress_uint64_zero(void);
4043
4044 /*
4045 * uint8_put()
4046 * stash a uint8_t value
4047 */
stress_uint8_put(const uint8_t a)4048 static inline void ALWAYS_INLINE stress_uint8_put(const uint8_t a)
4049 {
4050 g_put_val.uint8_val = a;
4051 }
4052
4053 /*
4054 * uint16_put()
4055 * stash a uint16_t value
4056 */
stress_uint16_put(const uint16_t a)4057 static inline void ALWAYS_INLINE stress_uint16_put(const uint16_t a)
4058 {
4059 g_put_val.uint16_val = a;
4060 }
4061
4062 /*
4063 * stress_uint32_put()
4064 * stash a uint32_t value
4065 */
stress_uint32_put(const uint32_t a)4066 static inline void ALWAYS_INLINE stress_uint32_put(const uint32_t a)
4067 {
4068 g_put_val.uint32_val = a;
4069 }
4070
4071 /*
4072 * stress_uint64_put()
4073 * stash a uint64_t value
4074 */
stress_uint64_put(const uint64_t a)4075 static inline void ALWAYS_INLINE stress_uint64_put(const uint64_t a)
4076 {
4077 g_put_val.uint64_val = a;
4078 }
4079
4080 #if defined(HAVE_INT128_T)
4081 /*
4082 * stress_uint128_put()
4083 * stash a uint128_t value
4084 */
stress_uint128_put(const __uint128_t a)4085 static inline void ALWAYS_INLINE stress_uint128_put(const __uint128_t a)
4086 {
4087 g_put_val.uint128_val = a;
4088 }
4089 #endif
4090
4091 /*
4092 * stress_float_put()
4093 * stash a float value
4094 */
stress_float_put(const float a)4095 static inline void ALWAYS_INLINE stress_float_put(const float a)
4096 {
4097 g_put_val.float_val = a;
4098 }
4099
4100 /*
4101 * stress_double_put()
4102 * stash a double value
4103 */
stress_double_put(const double a)4104 static inline void ALWAYS_INLINE stress_double_put(const double a)
4105 {
4106 g_put_val.double_val = a;
4107 }
4108
4109 /*
4110 * stress_long_double_put()
4111 * stash a double value
4112 */
stress_long_double_put(const long double a)4113 static inline void ALWAYS_INLINE stress_long_double_put(const long double a)
4114 {
4115 g_put_val.long_double_val = a;
4116 }
4117
4118 /*
4119 * stress_void_ptr_put()
4120 * stash a void * pointer value
4121 */
stress_void_ptr_put(volatile void * const a)4122 static inline void ALWAYS_INLINE stress_void_ptr_put(volatile void * const a)
4123 {
4124 g_put_val.void_ptr_val = a;
4125 }
4126
4127 /* Filenames and directories */
4128 extern int stress_temp_filename(char *path, const size_t len,
4129 const char *name, const pid_t pid, const uint32_t instance,
4130 const uint64_t magic);
4131 extern int stress_temp_filename_args(const stress_args_t *args, char *path,
4132 const size_t len, const uint64_t magic);
4133 extern int stress_temp_dir(char *path, const size_t len,
4134 const char *name, const pid_t pid, const uint32_t instance);
4135 extern int stress_temp_dir_args(const stress_args_t *args, char *path,
4136 const size_t len);
4137 extern WARN_UNUSED int stress_temp_dir_mk(const char *name, const pid_t pid,
4138 const uint32_t instance);
4139 extern WARN_UNUSED int stress_temp_dir_mk_args(const stress_args_t *args);
4140 extern int stress_temp_dir_rm(const char *name, const pid_t pid,
4141 const uint32_t instance);
4142 extern int stress_temp_dir_rm_args(const stress_args_t *args);
4143 extern void stress_cwd_readwriteable(void);
4144
4145 extern const char *stress_signal_name(const int signum);
4146 extern const char *stress_strsignal(const int signum);
4147
4148 /* CPU helpers */
4149 extern void stress_x86_cpuid(uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
4150 extern WARN_UNUSED bool stress_cpu_is_x86(void);
4151 extern WARN_UNUSED bool stress_cpu_x86_has_cldemote(void);
4152 extern WARN_UNUSED bool stress_cpu_x86_has_clfsh(void);
4153 extern WARN_UNUSED bool stress_cpu_x86_has_clflushopt(void);
4154 extern WARN_UNUSED bool stress_cpu_x86_has_clwb(void);
4155 extern WARN_UNUSED bool stress_cpu_x86_has_msr(void);
4156 extern WARN_UNUSED bool stress_cpu_x86_has_rdrand(void);
4157 extern WARN_UNUSED bool stress_cpu_x86_has_rdseed(void);
4158 extern WARN_UNUSED bool stress_cpu_x86_has_syscall(void);
4159 extern WARN_UNUSED bool stress_cpu_x86_has_tsc(void);
4160
4161 #if defined(STRESS_ARCH_X86) && \
4162 defined(HAVE_ASM_X86_CLFLUSH)
4163
4164 typedef void (*shim_clflush_func_t)(volatile void *ptr);
4165
4166 static inline void ALWAYS_INLINE shim_clflush_select(volatile void *ptr);
4167 static shim_clflush_func_t shim_clflush_func = shim_clflush_select;
4168
shim_clflush_op(volatile void * ptr)4169 static inline void ALWAYS_INLINE shim_clflush_op(volatile void *ptr)
4170 {
4171 asm volatile("clflush (%0)\n" : : "r"(ptr) : "memory");
4172 }
4173
shim_clflush_nop(volatile void * ptr)4174 static inline void ALWAYS_INLINE shim_clflush_nop(volatile void *ptr)
4175 {
4176 (void)ptr;
4177 }
4178
shim_clflush_select(volatile void * ptr)4179 static inline void ALWAYS_INLINE shim_clflush_select(volatile void *ptr)
4180 {
4181 shim_clflush_func = stress_cpu_x86_has_clfsh() ? shim_clflush_op : shim_clflush_nop;
4182
4183 shim_clflush_func(ptr);
4184 }
4185
4186 /*
4187 * shim_clflush()
4188 * flush a cache line
4189 */
shim_clflush(volatile void * ptr)4190 static inline void ALWAYS_INLINE shim_clflush(volatile void *ptr)
4191 {
4192 shim_clflush_func(ptr);
4193 }
4194 #elif defined(DCACHE)
4195 #define shim_clflush(ptr) shim_cacheflush((char *)ptr, 64, DCACHE)
4196 #else
4197 #define shim_clflush(ptr) do { } while (0) /* No-op */
4198 #endif
4199
4200 /*
4201 * shim_mfence()
4202 * serializing memory fence
4203 */
shim_mfence(void)4204 static inline void ALWAYS_INLINE shim_mfence(void)
4205 {
4206 #if defined(STRESS_ARCH_RISCV) && \
4207 defined(HAVE_ASM_RISCV_FENCE)
4208 asm volatile ("fence" ::: "memory");
4209 #else
4210 #if NEED_GNUC(4, 2, 0) && \
4211 !defined(__PCC__)
4212 __sync_synchronize();
4213 #else
4214 #if defined(STRESS_ARCH_X86)
4215 asm volatile("mfence" : : : "memory");
4216 #else
4217 /* Other arches not yet implemented for older GCC flavours */
4218 #endif
4219 #endif
4220 #endif
4221 }
4222
4223 /* Fast random numbers */
4224 extern uint32_t stress_mwc32(void);
4225 extern uint64_t stress_mwc64(void);
4226 extern uint16_t stress_mwc16(void);
4227 extern uint8_t stress_mwc8(void);
4228 extern uint8_t stress_mwc1(void);
4229 extern void stress_mwc_seed(const uint32_t w, const uint32_t z);
4230 extern void stress_mwc_reseed(void);
4231
4232 /* Time handling */
4233 extern WARN_UNUSED double stress_timeval_to_double(const struct timeval *tv);
4234 extern WARN_UNUSED double stress_time_now(void);
4235 extern const char *stress_duration_to_str(const double duration);
4236
4237 /* Perf statistics */
4238 #if defined(STRESS_PERF_STATS)
4239 extern int stress_perf_open(stress_perf_t *sp);
4240 extern int stress_perf_enable(stress_perf_t *sp);
4241 extern int stress_perf_disable(stress_perf_t *sp);
4242 extern int stress_perf_close(stress_perf_t *sp);
4243 extern bool stress_perf_stat_succeeded(const stress_perf_t *sp);
4244 extern void stress_perf_stat_dump(FILE *yaml, stress_stressor_t *procs_head,
4245 const double duration);
4246 extern void stress_perf_init(void);
4247 #endif
4248
4249 typedef int stress_oomable_child_func_t(const stress_args_t *args, void *context);
4250
4251 #define STRESS_OOMABLE_NORMAL (0x00000000) /* Normal oomability */
4252 #define STRESS_OOMABLE_DROP_CAP (0x00000001) /* Drop capabilities */
4253 #define STRESS_OOMABLE_QUIET (0x00000002) /* Don't report activity */
4254
4255 /* Misc helpers */
4256 extern size_t stress_mk_filename(char *fullname, const size_t fullname_len,
4257 const char *pathname, const char *filename);
4258 extern void stress_set_oom_adjustment(const char *name, const bool killable);
4259 extern WARN_UNUSED bool stress_process_oomed(const pid_t pid);
4260 extern WARN_UNUSED int stress_oomable_child(const stress_args_t *args,
4261 void *context, stress_oomable_child_func_t func, const int flag);
4262 extern WARN_UNUSED int stress_set_sched(const pid_t pid, const int sched,
4263 const int sched_priority, const bool quiet);
4264 extern WARN_UNUSED int stress_set_deadline_sched(const pid_t, const uint64_t period,
4265 const uint64_t runtime, const uint64_t deadline, const bool quiet);
4266 extern int sched_settings_apply(const bool quiet);
4267 extern const char *stress_get_sched_name(const int sched);
4268 extern void stress_set_iopriority(const int32_t class, const int32_t level);
4269 extern void stress_set_proc_name_init(int argc, char *argv[], char *envp[]);
4270 extern void stress_set_proc_name(const char *name);
4271 extern void stress_set_proc_state(const char *name, const int state);
4272 extern WARN_UNUSED int stress_get_unused_uid(uid_t *uid);
4273 extern void NORETURN MLOCKED_TEXT stress_sig_handler_exit(int signum);
4274
4275 /* Memory locking */
4276 extern int stress_mlock_region(const void *addr_start, const void *addr_end);
4277
4278 /* Argument parsing and range checking */
4279 extern WARN_UNUSED uint64_t stress_get_uint64(const char *const str);
4280 extern WARN_UNUSED uint64_t stress_get_uint64_scale(const char *const str,
4281 const stress_scale_t scales[], const char *const msg);
4282 extern WARN_UNUSED uint64_t stress_get_uint64_percent(const char *const str,
4283 const uint32_t instances, const uint64_t max, const char *const errmsg);
4284 extern WARN_UNUSED uint64_t stress_get_uint64_byte(const char *const str);
4285 extern WARN_UNUSED uint64_t stress_get_uint64_byte_memory(
4286 const char *const str, const uint32_t instances);
4287 extern WARN_UNUSED uint64_t stress_get_uint64_byte_filesystem(
4288 const char *const str, const uint32_t instances);
4289 extern WARN_UNUSED uint64_t stress_get_uint64_time(const char *const str);
4290 extern void stress_check_max_stressors(const char *const msg, const int val);
4291 extern void stress_check_range(const char *const opt, const uint64_t val,
4292 const uint64_t lo, const uint64_t hi);
4293 extern void stress_check_range_bytes(const char *const opt,
4294 const uint64_t val, const uint64_t lo, const uint64_t hi);
4295 extern WARN_UNUSED int stress_set_cpu_affinity(const char *arg);
4296 extern WARN_UNUSED uint32_t stress_get_uint32(const char *const str);
4297 extern WARN_UNUSED int32_t stress_get_int32(const char *const str);
4298 extern WARN_UNUSED int32_t stress_get_opt_sched(const char *const str);
4299 extern WARN_UNUSED int32_t stress_get_opt_ionice_class(const char *const str);
4300
4301 /* Misc helper funcs */
4302 extern WARN_UNUSED size_t stress_sig_stack_size(void);
4303 extern WARN_UNUSED size_t stress_min_sig_stack_size(void);
4304 extern WARN_UNUSED size_t stress_min_pthread_stack_size(void);
4305
4306 #define STRESS_SIGSTKSZ (stress_sig_stack_size())
4307 #define STRESS_MINSIGSTKSZ (stress_min_sig_stack_size())
4308
4309 extern void stress_shared_unmap(void);
4310 extern void stress_log_system_mem_info(void);
4311 extern WARN_UNUSED char *stress_munge_underscore(const char *str);
4312 extern size_t stress_get_pagesize(void);
4313 extern WARN_UNUSED int32_t stress_get_processors_online(void);
4314 extern WARN_UNUSED int32_t stress_get_processors_configured(void);
4315 extern WARN_UNUSED int32_t stress_get_ticks_per_second(void);
4316 extern WARN_UNUSED ssize_t stress_get_stack_direction(void);
4317 extern WARN_UNUSED void *stress_get_stack_top(void *start, size_t size);
4318 extern void stress_get_memlimits(size_t *shmall, size_t *freemem,
4319 size_t *totalmem, size_t *freeswap);
4320 extern WARN_UNUSED int stress_get_load_avg(double *min1, double *min5,
4321 double *min15);
4322 extern void stress_set_max_limits(void);
4323 extern void stress_parent_died_alarm(void);
4324 extern int stress_process_dumpable(const bool dumpable);
4325 extern int stress_set_timer_slack_ns(const char *opt);
4326 extern void stress_set_timer_slack(void);
4327 extern WARN_UNUSED int stress_set_temp_path(const char *path);
4328 extern WARN_UNUSED const char *stress_get_temp_path(void);
4329 extern WARN_UNUSED int stress_check_temp_path(void);
4330 extern void stress_temp_path_free(void);
4331 extern void stress_strnrnd(char *str, const size_t len);
4332 extern void stress_get_cache_size(uint64_t *l2, uint64_t *l3);
4333 extern WARN_UNUSED unsigned int stress_get_cpu(void);
4334 extern WARN_UNUSED const char *stress_get_compiler(void);
4335 extern WARN_UNUSED const char *stress_get_uname_info(void);
4336 extern WARN_UNUSED int stress_cache_alloc(const char *name);
4337 extern void stress_cache_free(void);
4338 extern void stress_ignite_cpu_start(void);
4339 extern void stress_ignite_cpu_stop(void);
4340 extern ssize_t system_write(const char *path, const char *buf, const size_t buf_len);
4341 extern WARN_UNUSED int stress_set_nonblock(const int fd);
4342 extern WARN_UNUSED ssize_t system_read(const char *path, char *buf,
4343 const size_t buf_len);
4344 extern WARN_UNUSED bool stress_is_prime64(const uint64_t n);
4345 extern WARN_UNUSED uint64_t stress_get_prime64(const uint64_t n);
4346 extern WARN_UNUSED size_t stress_get_file_limit(void);
4347 extern WARN_UNUSED size_t stress_get_max_file_limit(void);
4348 extern WARN_UNUSED int stress_get_bad_fd(void);
4349 extern void stress_vmstat_start(void);
4350 extern void stress_vmstat_stop(void);
4351 extern WARN_UNUSED int stress_sigaltstack_no_check(void *stack, const size_t size);
4352 extern WARN_UNUSED int stress_sigaltstack(void *stack, const size_t size);
4353 extern WARN_UNUSED int stress_sighandler(const char *name, const int signum,
4354 void (*handler)(int), struct sigaction *orig_action);
4355 extern int stress_sighandler_default(const int signum);
4356 extern void stress_handle_stop_stressing(int dummy);
4357 extern WARN_UNUSED int stress_sig_stop_stressing(const char *name,
4358 const int sig);
4359 extern int stress_sigrestore(const char *name, const int signum,
4360 struct sigaction *orig_action);
4361 extern WARN_UNUSED int stress_not_implemented(const stress_args_t *args);
4362 extern WARN_UNUSED size_t stress_probe_max_pipe_size(void);
4363 extern WARN_UNUSED void *stress_align_address(const void *addr,
4364 const size_t alignment);
4365 extern void stress_mmap_set(uint8_t *buf, const size_t sz,
4366 const size_t page_size);
4367 extern WARN_UNUSED int stress_mmap_check(uint8_t *buf, const size_t sz,
4368 const size_t page_size);
4369 extern WARN_UNUSED uint64_t stress_get_phys_mem_size(void);
4370 extern WARN_UNUSED uint64_t stress_get_filesystem_size(void);
4371 extern WARN_UNUSED ssize_t stress_read_buffer(int, void*, ssize_t, bool);
4372 extern WARN_UNUSED ssize_t stress_write_buffer(int, void*, ssize_t, bool);
4373 extern WARN_UNUSED uint64_t stress_get_filesystem_available_inodes(void);
4374 extern WARN_UNUSED int stress_kernel_release(const int major,
4375 const int minor, const int patchlevel);
4376 extern WARN_UNUSED int stress_get_kernel_release(void);
4377 extern char *stress_uint64_to_str(char *str, size_t len, const uint64_t val);
4378 extern WARN_UNUSED int stress_drop_capabilities(const char *name);
4379 extern WARN_UNUSED bool stress_is_dot_filename(const char *name);
4380 extern WARN_UNUSED char *stress_const_optdup(const char *opt);
4381 extern size_t stress_text_addr(char **start, char **end);
4382 extern WARN_UNUSED bool stress_check_capability(const int capability);
4383 extern WARN_UNUSED bool stress_sigalrm_pending(void);
4384 extern WARN_UNUSED bool stress_is_dev_tty(const int fd);
4385 extern WARN_UNUSED stress_hash_table_t *stress_hash_create(const size_t n);
4386 extern stress_hash_t *stress_hash_add(stress_hash_table_t *hash_table,
4387 const char *str);
4388 extern WARN_UNUSED stress_hash_t *stress_hash_get(
4389 stress_hash_table_t *hash_table, const char *str);
4390 extern void stress_hash_delete(stress_hash_table_t *hash_table);
4391 extern WARN_UNUSED int stress_try_open(const stress_args_t *args,
4392 const char *path, const int flags, const unsigned long timeout_ns);
4393 extern WARN_UNUSED int stress_open_timeout(const char *name,
4394 const char *path, const int flags, const unsigned long timeout_ns);
4395 extern WARN_UNUSED uint32_t stress_hash_jenkin(const uint8_t *data,
4396 const size_t len);
4397 extern WARN_UNUSED uint32_t stress_hash_pjw(const char *str);
4398 extern WARN_UNUSED uint32_t stress_hash_djb2a(const char *str);
4399 extern WARN_UNUSED uint32_t stress_hash_fnv1a(const char *str);
4400 extern WARN_UNUSED uint32_t stress_hash_sdbm(const char *str);
4401 extern WARN_UNUSED uint32_t stress_hash_nhash(const char *str);
4402 extern WARN_UNUSED uint32_t stress_hash_murmur3_32(const uint8_t* key, size_t len, uint32_t seed);
4403 extern void stress_dirent_list_free(struct dirent **dlist, const int n);
4404 extern WARN_UNUSED int stress_dirent_list_prune(struct dirent **dlist, const int n);
4405 extern WARN_UNUSED uint16_t stress_ipv4_checksum(uint16_t *ptr, const size_t n);
4406 extern int stress_read_fdinfo(const pid_t pid, const int fd);
4407 extern WARN_UNUSED pid_t stress_get_unused_pid_racy(const bool fork_test);
4408 extern WARN_UNUSED size_t stress_hostname_length(void);
4409 extern WARN_UNUSED int32_t stress_set_vmstat(const char *const str);
4410 extern WARN_UNUSED int32_t stress_set_thermalstat(const char *const str);
4411 extern WARN_UNUSED int32_t stress_set_iostat(const char *const str);
4412 extern void stress_misc_stats_set(stress_misc_stats_t *misc_stats,
4413 const int idx, const char *description, const double value);
4414 extern WARN_UNUSED int stress_tty_width(void);
4415 extern WARN_UNUSED size_t stress_get_extents(const int fd);
4416 extern WARN_UNUSED bool stress_redo_fork(const int err);
4417 extern int stress_killpid(const pid_t pid);
4418
4419 extern void stress_smart_start(void);
4420 extern void stress_smart_stop(void);
4421
4422 extern int stress_ftrace_start(void);
4423 extern void stress_ftrace_stop(void);
4424 extern void stress_ftrace_free(void);
4425 extern void stress_ftrace_add_pid(const pid_t pid);
4426
4427 extern int stress_module_load(const char *name, const char *alias,
4428 const char *options, bool *already_loaded);
4429 extern int stress_module_unload(const char *name, const char *alias,
4430 const bool already_loaded);
4431
4432 /*
4433 * Indicate a stress test failed because of limited resources
4434 * rather than a failure of the tests during execution.
4435 * err is the errno of the failure.
4436 */
exit_status(const int err)4437 static inline WARN_UNUSED ALWAYS_INLINE int exit_status(const int err)
4438 {
4439 switch (err) {
4440 case ENOMEM:
4441 case ENOSPC:
4442 return EXIT_NO_RESOURCE;
4443 case ENOSYS:
4444 return EXIT_NOT_IMPLEMENTED;
4445 }
4446 return EXIT_FAILURE; /* cppcheck-suppress ConfigurationNotChecked */
4447 }
4448
4449 /*
4450 * Stack aligning for clone() system calls
4451 * align to nearest 16 bytes for aarch64 et al,
4452 * assumes we have enough slop to do this
4453 */
stress_align_stack(void * stack_top)4454 static inline WARN_UNUSED ALWAYS_INLINE void *stress_align_stack(void *stack_top)
4455 {
4456 return (void *)((uintptr_t)stack_top & ~(uintptr_t)0xf);
4457 }
4458
4459 /*
4460 * stress_warn_once hashes the current filename and line where
4461 * the macro is used and returns true if it's never been called
4462 * there before across all threads and child processes
4463 */
4464 extern WARN_UNUSED bool stress_warn_once_hash(const char *filename, const int line);
4465 #define stress_warn_once() stress_warn_once_hash(__FILE__, __LINE__)
4466
4467 /* Jobfile parsing */
4468 extern WARN_UNUSED int stress_parse_jobfile(int argc, char **argv,
4469 const char *jobfile);
4470 extern WARN_UNUSED int stress_parse_opts(int argc, char **argv,
4471 const bool jobmode);
4472
4473 /* Memory tweaking */
4474 extern int stress_madvise_random(void *addr, const size_t length);
4475 extern void stress_madvise_pid_all_pages(const pid_t pid, const int advise);
4476 extern int stress_mincore_touch_pages(void *buf, const size_t buf_len);
4477 extern int stress_mincore_touch_pages_interruptible(void *buf,
4478 const size_t buf_len);
4479
4480 /* Mounts */
4481 extern void stress_mount_free(char *mnts[], const int n);
4482 extern WARN_UNUSED int stress_mount_get(char *mnts[], const int max);
4483
4484 /* Thermal Zones */
4485 #if defined(STRESS_THERMAL_ZONES)
4486 extern int stress_tz_init(stress_tz_info_t **tz_info_list);
4487 extern void stress_tz_free(stress_tz_info_t **tz_info_list);
4488 extern int stress_tz_get_temperatures(stress_tz_info_t **tz_info_list,
4489 stress_tz_t *tz);
4490 extern void stress_tz_dump(FILE *yaml, stress_stressor_t *procs_head);
4491 #endif
4492
4493 /* Network helpers */
4494
4495 #define NET_ADDR_ANY (0)
4496 #define NET_ADDR_LOOPBACK (1)
4497
4498 extern void stress_set_net_port(const char *optname, const char *opt,
4499 const int min_port, const int max_port, int *port);
4500 extern WARN_UNUSED int stress_set_net_domain(const int domain_mask,
4501 const char *name, const char *domain_name, int *domain);
4502 extern void stress_set_sockaddr(const char *name, const uint32_t instance,
4503 const pid_t ppid, const int domain, const int port,
4504 struct sockaddr **sockaddr, socklen_t *len, const int net_addr);
4505 extern void stress_set_sockaddr_port(const int domain, const int port,
4506 struct sockaddr *sockaddr);
4507
4508 /* CPU caches */
4509 extern stress_cpus_t *stress_get_all_cpu_cache_details(void);
4510 extern uint16_t stress_get_max_cache_level(const stress_cpus_t *cpus);
4511 extern stress_cpu_cache_t *stress_get_cpu_cache(const stress_cpus_t *cpus,
4512 const uint16_t cache_level);
4513 extern void stress_free_cpu_caches(stress_cpus_t *cpus);
4514
4515 /* CPU thrashing start/stop helpers */
4516 extern int stress_thrash_start(void);
4517 extern void stress_thrash_stop(void);
4518
4519 /* Used to set options for specific stressors */
4520 extern void stress_adjust_pthread_max(const uint64_t max);
4521 extern void stress_adjust_sleep_max(const uint64_t max);
4522
4523 /* Enable/disable stack smashing error message */
4524 extern void stress_set_stack_smash_check_flag(const bool flag);
4525
4526 /* loff_t and off64_t porting shims */
4527 #if defined(HAVE_LOFF_T)
4528 typedef loff_t shim_loff_t;
4529 #elif defined(HAVE_OFF_T)
4530 typedef off_t shim_loff_t;
4531 #else
4532 typedef long shim_loff_t;
4533 #endif
4534
4535 #if defined(HAVE_OFF64_T)
4536 typedef off64_t shim_off64_t;
4537 #else
4538 typedef uint64_t shim_off64_t;
4539 #endif
4540
4541 /* clone3 clone args */
4542 struct shim_clone_args {
4543 uint64_t flags; /* Flags bit mask */
4544 uint64_t pidfd; /* (pid_t *) PID fd */
4545 uint64_t child_tid; /* (pid_t *) child TID */
4546 uint64_t parent_tid; /* (pid_t *) parent TID */
4547 uint64_t exit_signal; /* exit signal */
4548 uint64_t stack; /* lowest address of stack */
4549 uint64_t stack_size; /* size of stack */
4550 uint64_t tls; /* tls address */
4551 };
4552
4553 struct shim_getcpu_cache {
4554 unsigned long blob[128 / sizeof(long)];
4555 };
4556
4557 /* futex2 waitv shim */
4558 struct shim_futex_waitv {
4559 uint64_t val;
4560 uint64_t uaddr;
4561 uint32_t flags;
4562 uint32_t __reserved;
4563 };
4564
4565 /*
4566 * shim'd abstracted system or library calls
4567 * that have a layer of OS abstraction
4568 */
4569 struct shim_linux_dirent {
4570 unsigned long d_ino; /* Inode number */
4571 unsigned long d_off; /* Offset to next linux_dirent */
4572 unsigned short d_reclen; /* Length of this linux_dirent */
4573 char d_name[]; /* Filename (null-terminated) */
4574 };
4575
4576 /* dirent64 porting shim */
4577 struct shim_linux_dirent64 {
4578 #if defined(HAVE_INO64_T)
4579 ino64_t d_ino; /* 64-bit inode number */
4580 #else
4581 int64_t d_ino; /* 64-bit inode number */
4582 #endif
4583 shim_off64_t d_off; /* 64-bit offset to next structure */
4584 unsigned short d_reclen; /* Size of this dirent */
4585 unsigned char d_type; /* File type */
4586 char d_name[]; /* Filename (null-terminated) */
4587 };
4588
4589 /* sched_getattr attributes */
4590 struct shim_sched_attr {
4591 uint32_t size; /* size of struct */
4592 uint32_t sched_policy; /* policy, SCHED_* */
4593 uint64_t sched_flags; /* scheduling flags */
4594 int32_t sched_nice; /* nice value SCHED_OTHER, SCHED_BATCH */
4595 uint32_t sched_priority; /* priority SCHED_FIFO, SCHED_RR */
4596 uint64_t sched_runtime; /* runtime SCHED_DEADLINE, ns */
4597 uint64_t sched_deadline; /* deadline time, ns */
4598 uint64_t sched_period; /* period, ns */
4599 uint32_t sched_util_min; /* utilization hint, min */
4600 uint32_t sched_util_max; /* utilization hint, max */
4601 };
4602
4603 #if defined(HAVE_TERMIOS_H)
4604
4605 #define HAVE_SHIM_TERMIOS2
4606 /* shim_speed_t */
4607 typedef unsigned int shim_speed_t;
4608
4609 /* shim termios2 */
4610 struct shim_termios2 {
4611 tcflag_t c_iflag; /* input mode flags */
4612 tcflag_t c_oflag; /* output mode flags */
4613 tcflag_t c_cflag; /* control mode flags */
4614 tcflag_t c_lflag; /* local mode flags */
4615 cc_t c_line; /* line discipline */
4616 cc_t c_cc[NCCS]; /* control characters */
4617 shim_speed_t c_ispeed; /* input speed */
4618 shim_speed_t c_ospeed; /* output speed */
4619 };
4620 #endif
4621
4622 /* shim'd STATX flags */
4623 #define SHIM_STATX_TYPE (0x00000001U)
4624 #define SHIM_STATX_MODE (0x00000002U)
4625 #define SHIM_STATX_NLINK (0x00000004U)
4626 #define SHIM_STATX_UID (0x00000008U)
4627 #define SHIM_STATX_GID (0x00000010U)
4628 #define SHIM_STATX_ATIME (0x00000020U)
4629 #define SHIM_STATX_MTIME (0x00000040U)
4630 #define SHIM_STATX_CTIME (0x00000080U)
4631 #define SHIM_STATX_INO (0x00000100U)
4632 #define SHIM_STATX_SIZE (0x00000200U)
4633 #define SHIM_STATX_BLOCKS (0x00000400U)
4634 #define SHIM_STATX_BASIC_STATS (0x000007ffU)
4635 #define SHIM_STATX_BTIME (0x00000800U)
4636 #define SHIM_STATX_ALL (0x00000fffU)
4637
4638 struct shim_statx_timestamp {
4639 int64_t tv_sec;
4640 int32_t tv_nsec;
4641 int32_t __reserved;
4642 };
4643
4644 /* shim'd statx */
4645 struct shim_statx {
4646 uint32_t stx_mask;
4647 uint32_t stx_blksize;
4648 uint64_t stx_attributes;
4649 uint32_t stx_nlink;
4650 uint32_t stx_uid;
4651 uint32_t stx_gid;
4652 uint16_t stx_mode;
4653 uint16_t __spare0[1];
4654 uint64_t stx_ino;
4655 uint64_t stx_size;
4656 uint64_t stx_blocks;
4657 uint64_t __spare1[1];
4658 struct shim_statx_timestamp stx_atime;
4659 struct shim_statx_timestamp stx_btime;
4660 struct shim_statx_timestamp stx_ctime;
4661 struct shim_statx_timestamp stx_mtime;
4662 uint32_t stx_rdev_major;
4663 uint32_t stx_rdev_minor;
4664 uint32_t stx_dev_major;
4665 uint32_t stx_dev_minor;
4666 uint64_t __spare2[14];
4667 };
4668
4669 /* old ustat struct */
4670 struct shim_ustat {
4671 #if defined(HAVE_DADDR_T)
4672 daddr_t f_tfree;
4673 #else
4674 long f_tfree;
4675 #endif
4676 ino_t f_tinode;
4677 char f_fname[6];
4678 char f_fpack[6];
4679 };
4680
4681 /* waitid/pidfd shims */
4682 #if !defined(P_PIDFD)
4683 #define P_PIDFD (3)
4684 #endif
4685
4686 #if defined(HAVE_SYS_TIMEX_H)
4687 #define shim_timex timex
4688 #else
4689 struct shim_timex {
4690 char data[128];
4691 };
4692 #endif
4693
4694 /* linux/pid.h */
4695 enum shim_pid_type {
4696 SHIM_PIDTYPE_PID,
4697 SHIM_PIDTYPE_TGID,
4698 SHIM_PIDTYPE_PGID,
4699 SHIM_PIDTYPE_SID,
4700 SHIM_PIDTYPE_MAX,
4701 };
4702
4703 /*
4704 * shim_unconstify_ptr()
4705 * some older system calls require non-const void *
4706 * or caddr_t args, so we need to unconstify them
4707 */
4708 #if defined(__sun__)
shim_unconstify_ptr(const void * ptr)4709 static inline void *shim_unconstify_ptr(const void *ptr)
4710 {
4711 void *unconst_ptr = (void *)ptr;
4712
4713 return unconst_ptr;
4714 }
4715 #else
4716 #define shim_unconstify_ptr(ptr) (ptr)
4717 #endif
4718
4719 extern int shim_arch_prctl(int code, unsigned long addr);
4720 extern int shim_brk(void *addr);
4721 extern int shim_cacheflush(char *addr, int nbytes, int cache);
4722 extern void shim_flush_icache(void *begin, void *end);
4723 extern int shim_clock_adjtime(clockid_t clk_id, struct shim_timex *tx);
4724 extern int shim_clock_getres(clockid_t clk_id, struct timespec *res);
4725 extern int shim_clock_gettime(clockid_t clk_id, struct timespec *tp);
4726 extern int shim_clock_settime(clockid_t clk_id, struct timespec *tp);
4727 extern int sys_clone3(struct shim_clone_args *cl_args, size_t size);
4728 extern int shim_close_range(unsigned int fd, unsigned int max_fd, unsigned int flags);
4729 extern ssize_t shim_copy_file_range(int fd_in, shim_loff_t *off_in,
4730 int fd_out, shim_loff_t *off_out, size_t len, unsigned int flags);
4731 extern int shim_dup3(int oldfd, int newfd, int flags);
4732 extern int shim_execveat(int dir_fd, const char *pathname, char *const argv[],
4733 char *const envp[], int flags);
4734 extern void shim_exit_group(int status);
4735 extern int shim_fallocate(int fd, int mode, off_t offset, off_t len);
4736 extern int shim_fdatasync(int fd);
4737 extern ssize_t shim_fgetxattr(int fd, const char *name, void *value, size_t size);
4738 extern ssize_t shim_flistxattr(int fd, char *list, size_t size);
4739 extern int shim_fsconfig(int fd, unsigned int cmd, const char *key,
4740 const void *value, int aux);
4741 extern int shim_fsetxattr(int fd, const char *name, const void *value,
4742 size_t size, int flags);
4743 extern int shim_fsmount(int fd, unsigned int flags, unsigned int ms_flags);
4744 extern int shim_fsopen(const char *fsname, unsigned int flags);
4745 extern int shim_fsync(int fd);
4746 extern int shim_futex_wait(const void *futex, const int val,
4747 const struct timespec *timeout);
4748 extern int shim_futex_wake(const void *futex, const int n);
4749 extern long shim_getcpu(unsigned *cpu, unsigned *node, void *tcache);
4750 extern int shim_getdents(unsigned int fd, struct shim_linux_dirent *dirp,
4751 unsigned int count);
4752 extern int shim_getdents64(unsigned int fd, struct shim_linux_dirent64 *dirp,
4753 unsigned int count);
4754 extern char *shim_getlogin(void);
4755 extern int shim_get_mempolicy(int *mode, unsigned long *nodemask,
4756 unsigned long maxnode, void *addr, unsigned long flags);
4757 extern int shim_getrandom(void *buff, size_t buflen, unsigned int flags);
4758 extern int shim_getrusage(int who, struct rusage *usage);
4759 extern int shim_gettid(void);
4760 extern ssize_t shim_getxattr(const char *path, const char *name,
4761 void *value, size_t size);
4762 extern int shim_ioprio_set(int which, int who, int ioprio);
4763 extern int shim_ioprio_get(int which, int who);
4764 extern long shim_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1,
4765 unsigned long idx2);
4766 extern int shim_klogctl(int type, char *bufp, int len);
4767 extern ssize_t shim_lgetxattr(const char *path, const char *name, void *value,
4768 size_t size);
4769 extern ssize_t shim_llistxattr(const char *path, char *list, size_t size);
4770 extern int shim_lsetxattr(const char *path, const char *name,
4771 const void *value, size_t size, int flags);
4772 extern ssize_t shim_listxattr(const char *path, char *list, size_t size);
4773 extern int shim_lookup_dcookie(uint64_t cookie, char *buffer, size_t len);
4774 extern int shim_lremovexattr(const char *path, const char *name);
4775 extern int shim_madvise(void *addr, size_t length, int advice);
4776 extern long shim_mbind(void *addr, unsigned long len,
4777 int mode, const unsigned long *nodemask,
4778 unsigned long maxnode, unsigned flags);
4779 extern int shim_membarrier(int cmd, int flags, int cpu_id);
4780 extern int shim_memfd_create(const char *name, unsigned int flags);
4781 extern int shim_memfd_secret(unsigned long flags);
4782 extern long shim_migrate_pages(int pid, unsigned long maxnode,
4783 const unsigned long *old_nodes, const unsigned long *new_nodes);
4784 extern int shim_mincore(void *addr, size_t length, unsigned char *vec);
4785 extern int shim_mlock(const void *addr, size_t len);
4786 extern int shim_mlock2(const void *addr, size_t len, int flags);
4787 extern int shim_mlockall(int flags);
4788 extern int shim_move_mount(int from_dfd, const char *from_pathname,
4789 int to_dfd, const char *to_pathname, unsigned int flags);
4790 extern long shim_move_pages(int pid, unsigned long count,
4791 void **pages, const int *nodes, int *status, int flags);
4792 extern int shim_msync(void *addr, size_t length, int flags);
4793 extern int shim_munlock(const void *addr, size_t len);
4794 extern int shim_munlockall(void);
4795 extern int shim_modify_ldt(int func, void *ptr, unsigned long bytecount);
4796 extern int shim_nanosleep_uint64(uint64_t usec);
4797 extern int shim_nice(int inc);
4798 extern time_t shim_time(time_t *tloc);
4799 extern int shim_gettimeofday(struct timeval *tv, struct timezone *tz);
4800 extern int shim_pidfd_getfd(int pidfd, int targetfd, unsigned int flags);
4801 extern int shim_pidfd_open(pid_t pid, unsigned int flags);
4802 extern int shim_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
4803 unsigned int flags);
4804 extern int shim_pkey_alloc(unsigned long flags, unsigned long access_rights);
4805 extern int shim_pkey_free(int pkey);
4806 extern int shim_pkey_mprotect(void *addr, size_t len, int prot, int pkey);
4807 extern int shim_pkey_get(int pkey);
4808 extern int shim_pkey_set(int pkey, unsigned int rights);
4809 extern ssize_t shim_process_madvise(int pidfd, const struct iovec *iovec,
4810 unsigned long vlen, int advice, unsigned int flags);
4811 extern int shim_process_mrelease(int pidfd, unsigned int flags);
4812 extern int shim_quotactl_fd(unsigned int fd, unsigned int cmd, int id, void *addr);
4813 extern ssize_t shim_readlink(const char *pathname, char *buf, size_t bufsiz);
4814 extern int shim_reboot(int magic, int magic2, int cmd, void *arg);
4815 extern int shim_removexattr(const char *path, const char *name);
4816 extern void *shim_sbrk(intptr_t increment);
4817 extern int shim_sched_getattr(pid_t pid, struct shim_sched_attr *attr,
4818 unsigned int size, unsigned int flags);
4819 extern int shim_sched_setattr(pid_t pid, struct shim_sched_attr *attr,
4820 unsigned int flags);
4821 extern long shim_sgetmask(void);
4822 extern long shim_ssetmask(long newmask);
4823 extern int shim_stime(const time_t *t);
4824 extern int shim_sched_yield(void);
4825 extern int shim_set_mempolicy(int mode, unsigned long *nodemask,
4826 unsigned long maxnode);
4827 extern int shim_seccomp(unsigned int operation, unsigned int flags, void *args);
4828 extern ssize_t shim_statx(int dfd, const char *filename, unsigned int flags,
4829 unsigned int mask, struct shim_statx *buffer);
4830 extern int shim_setxattr(const char *path, const char *name, const void *value,
4831 size_t size, int flags);
4832 extern size_t shim_strlcat(char *dst, const char *src, size_t len);
4833 extern size_t shim_strlcpy(char *dst, const char *src, size_t len);
4834 extern int shim_sync_file_range(int fd, shim_off64_t offset,
4835 shim_off64_t nbytes, unsigned int flags);
4836 extern int shim_sysfs(int option, ...);
4837 extern int shim_tgkill(int tgid, int tid, int sig);
4838 extern int shim_tkill(int tid, int sig);
4839 extern int shim_fremovexattr(int fd, const char *name);
4840 extern int shim_unshare(int flags);
4841 extern int shim_userfaultfd(int flags);
4842 extern int shim_usleep(uint64_t usec);
4843 extern int shim_usleep_interruptible(uint64_t usec);
4844 extern int shim_ustat(dev_t dev, struct shim_ustat *ubuf);
4845 extern int shim_vhangup(void);
4846 extern pid_t shim_waitpid(pid_t pid, int *wstatus, int options);
4847 extern pid_t shim_wait(int *wstatus);
4848 extern pid_t shim_wait3(int *wstatus, int options, struct rusage *rusage);
4849 extern pid_t shim_wait4(pid_t pid, int *wstatus, int options, struct rusage *rusage);
4850 extern int shim_futex_waitv(struct shim_futex_waitv *waiters, unsigned int nr_futexes,
4851 unsigned int flags, struct timespec *timeout, clockid_t clockid);
4852 #endif
4853