| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | #ifndef __PI_STRESS_H |
| 3 | #define __PI_STRESS_H |
| 4 | #include <stdio.h> |
| 5 | #include <errno.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdarg.h> |
| 8 | #include <pthread.h> |
| 9 | #include <sched.h> |
| 10 | #include <unistd.h> |
| 11 | #include <string.h> |
| 12 | #include <signal.h> |
| 13 | #include <getopt.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/mman.h> |
| 16 | #include <sys/wait.h> |
| 17 | #include <sys/syscall.h> |
| 18 | #include <termios.h> |
| 19 | #include <stdint.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <limits.h> |
| 22 | |
| 23 | #include "rt-sched.h" |
| 24 | #include "rt-utils.h" |
| 25 | #include "rt-error.h" |
| 26 | |
| 27 | /* test timeout */ |
| 28 | #define TIMEOUT 2 |
| 29 | |
| 30 | /* determine if the C library supports Priority Inheritance mutexes */ |
| 31 | #if defined(_POSIX_THREAD_PRIO_INHERIT) && _POSIX_THREAD_PRIO_INHERIT != -1 |
| 32 | #define HAVE_PI_MUTEX 1 |
| 33 | #else |
| 34 | #define HAVE_PI_MUTEX 0 |
| 35 | #endif |
| 36 | |
| 37 | #if HAVE_PI_MUTEX == 0 |
| 38 | #error "Can't run this test without PI Mutex support" |
| 39 | #endif |
| 40 | |
| 41 | #define SUCCESS 0 |
| 42 | #define FAILURE 1 |
| 43 | |
| 44 | /* cursor control */ |
| 45 | #define UP_ONE "\033[1A" |
| 46 | #define DOWN_ONE "\033[1B" |
| 47 | |
| 48 | #define pi_info(fmt, arg...) \ |
| 49 | do { info(verbose, fmt, ## arg); } while (0) |
| 50 | #define pi_debug(fmt, arg...) \ |
| 51 | do { debug(debugging, fmt, ## arg); } while (0) |
| 52 | #define pi_error(fmt, arg...) \ |
| 53 | do { err_msg(fmt, ## arg); have_errors = 1; } while (0) |
| 54 | |
| 55 | #define NUM_TEST_THREADS 3 |
| 56 | #define NUM_ADMIN_THREADS 1 |
| 57 | |
| 58 | #define SA_INIT_LOW (1 << 0) |
| 59 | #define SA_INIT_MED (1 << 1) |
| 60 | #define SA_INIT_HIGH (1 << 2) |
| 61 | #define SA_INIT_ADMIN (1 << 3) |
| 62 | |
| 63 | /* number of consecutive watchdog hits before quitting */ |
| 64 | #define WATCHDOG_LIMIT 5 |
| 65 | |
| 66 | struct group_parameters { |
| 67 | |
| 68 | /* group id (index) */ |
| 69 | int id; |
| 70 | |
| 71 | /* cpu this group is bound to */ |
| 72 | long cpu; |
| 73 | |
| 74 | /* threads in the group */ |
| 75 | pthread_t low_tid; |
| 76 | pthread_t med_tid; |
| 77 | pthread_t high_tid; |
| 78 | |
| 79 | /* number of machine iterations to perform */ |
| 80 | int inversions; |
| 81 | |
| 82 | /* group mutex */ |
| 83 | pthread_mutex_t mutex; |
| 84 | |
| 85 | /* state barriers */ |
| 86 | pthread_barrier_t start_barrier; |
| 87 | pthread_barrier_t locked_barrier; |
| 88 | pthread_barrier_t elevate_barrier; |
| 89 | pthread_barrier_t finish_barrier; |
| 90 | |
| 91 | /* Either everyone goes through the loop, or else no-ones does */ |
| 92 | pthread_barrier_t loop_barr; |
| 93 | pthread_mutex_t loop_mtx; /* Protect access to int loop */ |
| 94 | int loop; /* boolean, loop or not, connected to shutdown */ |
| 95 | |
| 96 | /* state variables */ |
| 97 | volatile int watchdog; |
| 98 | |
| 99 | /* total number of inversions performed */ |
| 100 | unsigned long total; |
| 101 | |
| 102 | /* total watchdog hits */ |
| 103 | int watchdog_hits; |
| 104 | |
| 105 | }; |
| 106 | |
| 107 | /* forward prototypes */ |
| 108 | void *low_priority(void *arg); |
| 109 | void *med_priority(void *arg); |
| 110 | void *high_priority(void *arg); |
| 111 | void *reporter(void *arg); |
| 112 | void *watchdog(void *arg); |
| 113 | int setup_thread_attr(pthread_attr_t *attr, struct sched_attr *sa, |
| 114 | cpu_set_t *mask); |
| 115 | int set_cpu_affinity(cpu_set_t *test_mask, cpu_set_t *admin_mask); |
| 116 | void process_command_line(int argc, char **argv); |
| 117 | void usage(int error); |
| 118 | int block_signals(void); |
| 119 | int allow_sigterm(void); |
| 120 | void set_shutdown_flag(void); |
| 121 | int initialize_group(struct group_parameters *group); |
| 122 | int create_group(struct group_parameters *group); |
| 123 | unsigned long total_inversions(void); |
| 124 | void banner(void); |
| 125 | void summary(void); |
| 126 | void write_stats(FILE *f, void *data); |
| 127 | void wait_for_termination(void); |
| 128 | int barrier_init(pthread_barrier_t *b, const pthread_barrierattr_t *attr, |
| 129 | unsigned int count, const char *name); |
| 130 | void setup_sched_attr(struct sched_attr *attr, int policy, int prio); |
| 131 | void setup_sched_config(int policy); |
| 132 | |
| 133 | #endif /* __PI_STRESS_H */ |