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