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 <sys/stat.h>
15 #include <sys/ioctl.h>
16 #include <sys/mount.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <sys/select.h>
20 #include <sys/time.h>
21 #include <sys/eventfd.h>
22 #include <asm/unistd.h>
23 #include <linux/perf_event.h>
24 #include "../kselftest.h"
25
26 #define MB (1024 * 1024)
27 #define RESCTRL_PATH "/sys/fs/resctrl"
28 #define PHYS_ID_PATH "/sys/devices/system/cpu/cpu"
29 #define INFO_PATH "/sys/fs/resctrl/info"
30
31 /*
32 * CPU vendor IDs
33 *
34 * Define as bits because they're used for vendor_specific bitmask in
35 * the struct resctrl_test.
36 */
37 #define ARCH_INTEL 1
38 #define ARCH_AMD 2
39
40 #define END_OF_TESTS 1
41
42 #define BENCHMARK_ARGS 64
43
44 #define DEFAULT_SPAN (250 * MB)
45
46 /*
47 * user_params: User supplied parameters
48 * @cpu: CPU number to which the benchmark will be bound to
49 * @bits: Number of bits used for cache allocation size
50 * @benchmark_cmd: Benchmark command to run during (some of the) tests
51 */
52 struct user_params {
53 int cpu;
54 int bits;
55 const char *benchmark_cmd[BENCHMARK_ARGS];
56 };
57
58 /*
59 * resctrl_test: resctrl test definition
60 * @name: Test name
61 * @group: Test group - a common name for tests that share some characteristic
62 * (e.g., L3 CAT test belongs to the CAT group). Can be NULL
63 * @resource: Resource to test (e.g., MB, L3, L2, etc.)
64 * @vendor_specific: Bitmask for vendor-specific tests (can be 0 for universal tests)
65 * @disabled: Test is disabled
66 * @feature_check: Callback to check required resctrl features
67 * @run_test: Callback to run the test
68 * @cleanup: Callback to cleanup after the test
69 */
70 struct resctrl_test {
71 const char *name;
72 const char *group;
73 const char *resource;
74 unsigned int vendor_specific;
75 bool disabled;
76 bool (*feature_check)(const struct resctrl_test *test);
77 int (*run_test)(const struct resctrl_test *test,
78 const struct user_params *uparams);
79 void (*cleanup)(void);
80 };
81
82 /*
83 * resctrl_val_param: resctrl test parameters
84 * @ctrlgrp: Name of the control monitor group (con_mon grp)
85 * @mongrp: Name of the monitor group (mon grp)
86 * @filename: Name of file to which the o/p should be written
87 * @init: Callback function to initialize test environment
88 * @setup: Callback function to setup per test run environment
89 * @measure: Callback that performs the measurement (a single test)
90 */
91 struct resctrl_val_param {
92 const char *ctrlgrp;
93 const char *mongrp;
94 char filename[64];
95 unsigned long mask;
96 int num_of_runs;
97 int (*init)(const struct resctrl_val_param *param,
98 int domain_id);
99 int (*setup)(const struct resctrl_test *test,
100 const struct user_params *uparams,
101 struct resctrl_val_param *param);
102 int (*measure)(const struct user_params *uparams,
103 struct resctrl_val_param *param,
104 pid_t bm_pid);
105 };
106
107 struct perf_event_read {
108 __u64 nr; /* The number of events */
109 struct {
110 __u64 value; /* The value of the event */
111 } values[2];
112 };
113
114 /*
115 * Memory location that consumes values compiler must not optimize away.
116 * Volatile ensures writes to this location cannot be optimized away by
117 * compiler.
118 */
119 extern volatile int *value_sink;
120
121 extern char llc_occup_path[1024];
122
123 int get_vendor(void);
124 bool check_resctrlfs_support(void);
125 int filter_dmesg(void);
126 int get_domain_id(const char *resource, int cpu_no, int *domain_id);
127 int mount_resctrlfs(void);
128 int umount_resctrlfs(void);
129 const char *get_bw_report_type(const char *bw_report);
130 bool resctrl_resource_exists(const char *resource);
131 bool resctrl_mon_feature_exists(const char *resource, const char *feature);
132 bool resource_info_file_exists(const char *resource, const char *file);
133 bool test_resource_feature_check(const struct resctrl_test *test);
134 char *fgrep(FILE *inf, const char *str);
135 int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity);
136 int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity);
137 int write_schemata(const char *ctrlgrp, char *schemata, int cpu_no,
138 const char *resource);
139 int write_bm_pid_to_resctrl(pid_t bm_pid, const char *ctrlgrp, const char *mongrp);
140 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
141 int group_fd, unsigned long flags);
142 unsigned char *alloc_buffer(size_t buf_size, int memflush);
143 void mem_flush(unsigned char *buf, size_t buf_size);
144 void fill_cache_read(unsigned char *buf, size_t buf_size, bool once);
145 int run_fill_buf(size_t buf_size, int memflush, int op, bool once);
146 int initialize_mem_bw_imc(void);
147 int measure_mem_bw(const struct user_params *uparams,
148 struct resctrl_val_param *param, pid_t bm_pid,
149 const char *bw_report);
150 void initialize_mem_bw_resctrl(const struct resctrl_val_param *param,
151 int domain_id);
152 int resctrl_val(const struct resctrl_test *test,
153 const struct user_params *uparams,
154 const char * const *benchmark_cmd,
155 struct resctrl_val_param *param);
156 unsigned long create_bit_mask(unsigned int start, unsigned int len);
157 unsigned int count_contiguous_bits(unsigned long val, unsigned int *start);
158 int get_full_cbm(const char *cache_type, unsigned long *mask);
159 int get_mask_no_shareable(const char *cache_type, unsigned long *mask);
160 int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size);
161 int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val);
162 void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
163 int signal_handler_register(const struct resctrl_test *test);
164 void signal_handler_unregister(void);
165 unsigned int count_bits(unsigned long n);
166
167 void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config);
168 void perf_event_initialize_read_format(struct perf_event_read *pe_read);
169 int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no);
170 int perf_event_reset_enable(int pe_fd);
171 int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
172 const char *filename, pid_t bm_pid);
173 int measure_llc_resctrl(const char *filename, pid_t bm_pid);
174 void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines);
175
176 /*
177 * cache_portion_size - Calculate the size of a cache portion
178 * @cache_size: Total cache size in bytes
179 * @portion_mask: Cache portion mask
180 * @full_cache_mask: Full Cache Bit Mask (CBM) for the cache
181 *
182 * Return: The size of the cache portion in bytes.
183 */
cache_portion_size(unsigned long cache_size,unsigned long portion_mask,unsigned long full_cache_mask)184 static inline unsigned long cache_portion_size(unsigned long cache_size,
185 unsigned long portion_mask,
186 unsigned long full_cache_mask)
187 {
188 unsigned int bits = count_bits(full_cache_mask);
189
190 /*
191 * With no bits the full CBM, assume cache cannot be split into
192 * smaller portions. To avoid divide by zero, return cache_size.
193 */
194 if (!bits)
195 return cache_size;
196
197 return cache_size * count_bits(portion_mask) / bits;
198 }
199
200 extern struct resctrl_test mbm_test;
201 extern struct resctrl_test mba_test;
202 extern struct resctrl_test cmt_test;
203 extern struct resctrl_test l3_cat_test;
204 extern struct resctrl_test l3_noncont_cat_test;
205 extern struct resctrl_test l2_noncont_cat_test;
206
207 #endif /* RESCTRL_H */
208