1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef RESCTRL_H 3 #define RESCTRL_H 4 #include <stdio.h> 5 #include <math.h> 6 #include <errno.h> 7 #include <sched.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 #include <string.h> 11 #include <signal.h> 12 #include <dirent.h> 13 #include <stdbool.h> 14 #include <ctype.h> 15 #include <sys/stat.h> 16 #include <sys/ioctl.h> 17 #include <sys/mount.h> 18 #include <sys/types.h> 19 #include <sys/wait.h> 20 #include <sys/select.h> 21 #include <sys/time.h> 22 #include <sys/eventfd.h> 23 #include <asm/unistd.h> 24 #include <linux/perf_event.h> 25 #include <linux/compiler.h> 26 #include <linux/bits.h> 27 #include "kselftest.h" 28 29 #define MB (1024 * 1024) 30 #define RESCTRL_PATH "/sys/fs/resctrl" 31 #define PHYS_ID_PATH "/sys/devices/system/cpu/cpu" 32 #define INFO_PATH "/sys/fs/resctrl/info" 33 34 /* 35 * CPU vendor IDs 36 * 37 * Define as bits because they're used for vendor_specific bitmask in 38 * the struct resctrl_test. 39 */ 40 #define ARCH_INTEL BIT(0) 41 #define ARCH_AMD BIT(1) 42 #define ARCH_HYGON BIT(2) 43 44 #define END_OF_TESTS 1 45 46 #define BENCHMARK_ARGS 64 47 48 #define MINIMUM_SPAN (250 * MB) 49 50 /* 51 * Memory bandwidth (in MiB) below which the bandwidth comparisons 52 * between iMC and resctrl are considered unreliable. For example RAS 53 * features or memory performance features that generate memory traffic 54 * may drive accesses that are counted differently by performance counters 55 * and MBM respectively, for instance generating "overhead" traffic which 56 * is not counted against any specific RMID. 57 */ 58 #define THROTTLE_THRESHOLD 2500 59 60 /* 61 * fill_buf_param: "fill_buf" benchmark parameters 62 * @buf_size: Size (in bytes) of buffer used in benchmark. 63 * "fill_buf" allocates and initializes buffer of 64 * @buf_size. User can change value via command line. 65 * @memflush: If false the buffer will not be flushed after 66 * allocation and initialization, otherwise the 67 * buffer will be flushed. User can change value via 68 * command line (via integers with 0 interpreted as 69 * false and anything else as true). 70 */ 71 struct fill_buf_param { 72 size_t buf_size; 73 bool memflush; 74 }; 75 76 /* 77 * user_params: User supplied parameters 78 * @cpu: CPU number to which the benchmark will be bound to 79 * @bits: Number of bits used for cache allocation size 80 * @benchmark_cmd: Benchmark command to run during (some of the) tests 81 * @fill_buf: Pointer to user provided parameters for "fill_buf", 82 * NULL if user did not provide parameters and test 83 * specific defaults should be used. 84 */ 85 struct user_params { 86 int cpu; 87 int bits; 88 const char *benchmark_cmd[BENCHMARK_ARGS]; 89 const struct fill_buf_param *fill_buf; 90 }; 91 92 /* 93 * resctrl_test: resctrl test definition 94 * @name: Test name 95 * @group: Test group - a common name for tests that share some characteristic 96 * (e.g., L3 CAT test belongs to the CAT group). Can be NULL 97 * @resource: Resource to test (e.g., MB, L3, L2, etc.) 98 * @vendor_specific: Bitmask for vendor-specific tests (can be 0 for universal tests) 99 * @disabled: Test is disabled 100 * @feature_check: Callback to check required resctrl features 101 * @run_test: Callback to run the test 102 * @cleanup: Callback to cleanup after the test 103 */ 104 struct resctrl_test { 105 const char *name; 106 const char *group; 107 const char *resource; 108 unsigned int vendor_specific; 109 bool disabled; 110 bool (*feature_check)(const struct resctrl_test *test); 111 int (*run_test)(const struct resctrl_test *test, 112 const struct user_params *uparams); 113 void (*cleanup)(void); 114 }; 115 116 /* 117 * resctrl_val_param: resctrl test parameters 118 * @ctrlgrp: Name of the control monitor group (con_mon grp) 119 * @mongrp: Name of the monitor group (mon grp) 120 * @filename: Name of file to which the o/p should be written 121 * @init: Callback function to initialize test environment 122 * @setup: Callback function to setup per test run environment 123 * @measure: Callback that performs the measurement (a single test) 124 * @fill_buf: Parameters for default "fill_buf" benchmark. 125 * Initialized with user provided parameters, possibly 126 * adapted to be relevant to the test. If user does 127 * not provide parameters for "fill_buf" nor a 128 * replacement benchmark then initialized with defaults 129 * appropriate for test. NULL if user provided 130 * benchmark. 131 */ 132 struct resctrl_val_param { 133 const char *ctrlgrp; 134 const char *mongrp; 135 char filename[64]; 136 unsigned long mask; 137 int num_of_runs; 138 int (*init)(const struct resctrl_test *test, 139 const struct user_params *uparams, 140 const struct resctrl_val_param *param, 141 int domain_id); 142 int (*setup)(const struct resctrl_test *test, 143 const struct user_params *uparams, 144 struct resctrl_val_param *param); 145 int (*measure)(const struct user_params *uparams, 146 struct resctrl_val_param *param, 147 pid_t bm_pid); 148 struct fill_buf_param *fill_buf; 149 }; 150 151 struct perf_event_read { 152 __u64 nr; /* The number of events */ 153 struct { 154 __u64 value; /* The value of the event */ 155 } values[2]; 156 }; 157 158 /* 159 * Memory location that consumes values compiler must not optimize away. 160 * Volatile ensures writes to this location cannot be optimized away by 161 * compiler. 162 */ 163 extern volatile int *value_sink; 164 165 extern int snc_unreliable; 166 167 extern char llc_occup_path[1024]; 168 169 int snc_nodes_per_l3_cache(void); 170 unsigned int get_vendor(void); 171 bool check_resctrlfs_support(void); 172 int filter_dmesg(void); 173 int get_domain_id(const char *resource, int cpu_no, int *domain_id); 174 int mount_resctrlfs(void); 175 int umount_resctrlfs(void); 176 bool resctrl_resource_exists(const char *resource); 177 bool resctrl_mon_feature_exists(const char *resource, const char *feature); 178 bool resource_info_file_exists(const char *resource, const char *file); 179 bool test_resource_feature_check(const struct resctrl_test *test); 180 char *fgrep(FILE *inf, const char *str); 181 int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity); 182 int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity); 183 int write_schemata(const char *ctrlgrp, char *schemata, int cpu_no, 184 const char *resource); 185 int write_bm_pid_to_resctrl(pid_t bm_pid, const char *ctrlgrp, const char *mongrp); 186 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, 187 int group_fd, unsigned long flags); 188 unsigned char *alloc_buffer(size_t buf_size, bool memflush); 189 void mem_flush(unsigned char *buf, size_t buf_size); 190 void fill_cache_read(unsigned char *buf, size_t buf_size, bool once); 191 ssize_t get_fill_buf_size(int cpu_no, const char *cache_type); 192 int initialize_read_mem_bw_imc(void); 193 int measure_read_mem_bw(const struct user_params *uparams, 194 struct resctrl_val_param *param, pid_t bm_pid); 195 void initialize_mem_bw_resctrl(const struct resctrl_val_param *param, 196 int domain_id); 197 int resctrl_val(const struct resctrl_test *test, 198 const struct user_params *uparams, 199 struct resctrl_val_param *param); 200 unsigned long create_bit_mask(unsigned int start, unsigned int len); 201 unsigned int count_contiguous_bits(unsigned long val, unsigned int *start); 202 int get_full_cbm(const char *cache_type, unsigned long *mask); 203 int get_mask_no_shareable(const char *cache_type, unsigned long *mask); 204 int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size); 205 int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val); 206 void ctrlc_handler(int signum, siginfo_t *info, void *ptr); 207 int signal_handler_register(const struct resctrl_test *test); 208 void signal_handler_unregister(void); 209 unsigned int count_bits(unsigned long n); 210 int snc_kernel_support(void); 211 212 void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config); 213 void perf_event_initialize_read_format(struct perf_event_read *pe_read); 214 int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no); 215 int perf_event_reset_enable(int pe_fd); 216 int perf_event_measure(int pe_fd, struct perf_event_read *pe_read, 217 const char *filename, pid_t bm_pid); 218 int measure_llc_resctrl(const char *filename, pid_t bm_pid); 219 int minimize_l2_occupancy(const struct resctrl_test *test, 220 const struct user_params *uparams, 221 const struct resctrl_val_param *param); 222 void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines); 223 224 /* 225 * cache_portion_size - Calculate the size of a cache portion 226 * @cache_size: Total cache size in bytes 227 * @portion_mask: Cache portion mask 228 * @full_cache_mask: Full Cache Bit Mask (CBM) for the cache 229 * 230 * Return: The size of the cache portion in bytes. 231 */ 232 static inline unsigned long cache_portion_size(unsigned long cache_size, 233 unsigned long portion_mask, 234 unsigned long full_cache_mask) 235 { 236 unsigned int bits = count_bits(full_cache_mask); 237 238 /* 239 * With no bits the full CBM, assume cache cannot be split into 240 * smaller portions. To avoid divide by zero, return cache_size. 241 */ 242 if (!bits) 243 return cache_size; 244 245 return cache_size * count_bits(portion_mask) / bits; 246 } 247 248 extern struct resctrl_test mbm_test; 249 extern struct resctrl_test mba_test; 250 extern struct resctrl_test cmt_test; 251 extern struct resctrl_test l3_cat_test; 252 extern struct resctrl_test l3_noncont_cat_test; 253 extern struct resctrl_test l2_noncont_cat_test; 254 255 #endif /* RESCTRL_H */ 256