| 1 |
|
| 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 | |
| 152 | * Memory location that consumes values compiler must not optimize away.
|
| 153 | * Volatile ensures writes to this location cannot be optimized away by
|
| 154 | * compiler.
|
| 155 |
|
| 156 | extern volatile int *value_sink;
|
| 157 |
|
| 158 | extern int snc_unreliable;
|
| 159 |
|
| 160 | extern char llc_occup_path[1024];
|
| 161 |
|
| 162 | int snc_nodes_per_l3_cache(void);
|
| 163 | unsigned int get_vendor(void);
|
| 164 | bool check_resctrlfs_support(void);
|
| 165 | int filter_dmesg(void);
|
| 166 | int get_domain_id(const char *resource, int cpu_no, int *domain_id);
|
| 167 | int mount_resctrlfs(void);
|
| 168 | int umount_resctrlfs(void);
|
| 169 | bool resctrl_resource_exists(const char *resource);
|
| 170 | bool resctrl_mon_feature_exists(const char *resource, const char *feature);
|
| 171 | bool resource_info_file_exists(const char *resource, const char *file);
|
| 172 | bool test_resource_feature_check(const struct resctrl_test *test);
|
| 173 | char *fgrep(FILE *inf, const char *str);
|
| 174 | int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity);
|
| 175 | int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity);
|
| 176 | int write_schemata(const char *ctrlgrp, char *schemata, int cpu_no,
|
| 177 | const char *resource);
|
| 178 | int write_bm_pid_to_resctrl(pid_t bm_pid, const char *ctrlgrp, const char *mongrp);
|
| 179 | int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
|
| 180 | int group_fd, unsigned long flags);
|
| 181 | unsigned char *alloc_buffer(size_t buf_size, bool memflush);
|
| 182 | void mem_flush(unsigned char *buf, size_t buf_size);
|
| 183 | void fill_cache_read(unsigned char *buf, size_t buf_size, bool once);
|
| 184 | ssize_t get_fill_buf_size(int cpu_no, const char *cache_type);
|
| 185 | int initialize_read_mem_bw_imc(void);
|
| 186 | int measure_read_mem_bw(const struct user_params *uparams,
|
| 187 | struct resctrl_val_param *param, pid_t bm_pid);
|
| 188 | void initialize_mem_bw_resctrl(const struct resctrl_val_param *param,
|
| 189 | int domain_id);
|
| 190 | int resctrl_val(const struct resctrl_test *test,
|
| 191 | const struct user_params *uparams,
|
| 192 | struct resctrl_val_param *param);
|
| 193 | unsigned long create_bit_mask(unsigned int start, unsigned int len);
|
| 194 | unsigned int count_contiguous_bits(unsigned long val, unsigned int *start);
|
| 195 | int get_full_cbm(const char *cache_type, unsigned long *mask);
|
| 196 | int get_mask_no_shareable(const char *cache_type, unsigned long *mask);
|
| 197 | int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size);
|
| 198 | int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val);
|
| 199 | void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
|
| 200 | int signal_handler_register(const struct resctrl_test *test);
|
| 201 | void signal_handler_unregister(void);
|
| 202 | unsigned int count_bits(unsigned long n);
|
| 203 | int snc_kernel_support(void);
|
| 204 |
|
| 205 | void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config);
|
| 206 | int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no);
|
| 207 | int perf_event_reset_enable(int pe_fd);
|
| 208 | int perf_event_measure(int pe_fd, const char *filename, pid_t bm_pid);
|
| 209 | int measure_llc_resctrl(const char *filename, pid_t bm_pid);
|
| 210 | int minimize_l2_occupancy(const struct resctrl_test *test,
|
| 211 | const struct user_params *uparams,
|
| 212 | const struct resctrl_val_param *param);
|
| 213 | void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines);
|
| 214 |
|
| 215 | |
| 216 | * cache_portion_size - Calculate the size of a cache portion
|
| 217 | * @cache_size: Total cache size in bytes
|
| 218 | * @portion_mask: Cache portion mask
|
| 219 | * @full_cache_mask: Full Cache Bit Mask (CBM) for the cache
|
| 220 | *
|
| 221 | * Return: The size of the cache portion in bytes.
|
| 222 |
|
| 223 | static inline unsigned long cache_portion_size(unsigned long cache_size,
|
| 224 | unsigned long portion_mask,
|
| 225 | unsigned long full_cache_mask)
|
| 226 | {
|
| 227 | unsigned int bits = count_bits(full_cache_mask);
|
| 228 |
|
| 229 | |
| 230 | * With no bits the full CBM, assume cache cannot be split into
|
| 231 | * smaller portions. To avoid divide by zero, return cache_size.
|
| 232 |
|
| 233 | if (!bits)
|
| 234 | return cache_size;
|
| 235 |
|
| 236 | return cache_size * count_bits(portion_mask) / bits;
|
| 237 | }
|
| 238 |
|
| 239 | extern struct resctrl_test mbm_test;
|
| 240 | extern struct resctrl_test mba_test;
|
| 241 | extern struct resctrl_test cmt_test;
|
| 242 | extern struct resctrl_test l3_cat_test;
|
| 243 | extern struct resctrl_test l3_noncont_cat_test;
|
| 244 | extern struct resctrl_test l2_noncont_cat_test;
|
| 245 |
|
| 246 | #endif
|