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