1 /* SPDX-License-Identifier: GPL-2.0 */
15 #include <sys/ioctl.h>
16 #include <sys/mount.h>
17 #include <sys/types.h>
19 #include <sys/select.h>
21 #include <sys/eventfd.h>
22 #include <asm/unistd.h>
23 #include <linux/perf_event.h>
24 #include "../kselftest.h"
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"
34 #define END_OF_TESTS 1
36 #define BENCHMARK_ARGS 64
38 #define DEFAULT_SPAN (250 * MB)
40 #define PARENT_EXIT() \
42 kill(ppid, SIGKILL); \
48 * resctrl_val_param: resctrl test parameters
49 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
50 * @ctrlgrp: Name of the control monitor group (con_mon grp)
51 * @mongrp: Name of the monitor group (mon grp)
52 * @cpu_no: CPU number to which the benchmark would be binded
53 * @filename: Name of file to which the o/p should be written
54 * @bw_report: Bandwidth report type (reads vs writes)
55 * @setup: Call back function to setup test environment
57 struct resctrl_val_param {
66 int (*setup)(struct resctrl_val_param *param);
69 struct perf_event_read {
70 __u64 nr; /* The number of events */
72 __u64 value; /* The value of the event */
82 * Memory location that consumes values compiler must not optimize away.
83 * Volatile ensures writes to this location cannot be optimized away by
86 extern volatile int *value_sink;
88 extern pid_t bm_pid, ppid;
90 extern char llc_occup_path[1024];
93 bool check_resctrlfs_support(void);
94 int filter_dmesg(void);
95 int get_resource_id(int cpu_no, int *resource_id);
96 int mount_resctrlfs(void);
97 int umount_resctrlfs(void);
98 int validate_bw_report_request(char *bw_report);
99 bool validate_resctrl_feature_request(const char *resource, const char *feature);
100 char *fgrep(FILE *inf, const char *str);
101 int taskset_benchmark(pid_t bm_pid, int cpu_no);
102 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no,
104 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
106 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
107 int group_fd, unsigned long flags);
108 unsigned char *alloc_buffer(size_t buf_size, int memflush);
109 void mem_flush(unsigned char *buf, size_t buf_size);
110 void fill_cache_read(unsigned char *buf, size_t buf_size, bool once);
111 int run_fill_buf(size_t buf_size, int memflush, int op, bool once);
112 int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *param);
113 int mbm_bw_change(int cpu_no, const char * const *benchmark_cmd);
114 void tests_cleanup(void);
115 void mbm_test_cleanup(void);
116 int mba_schemata_change(int cpu_no, const char * const *benchmark_cmd);
117 void mba_test_cleanup(void);
118 unsigned long create_bit_mask(unsigned int start, unsigned int len);
119 unsigned int count_contiguous_bits(unsigned long val, unsigned int *start);
120 int get_full_cbm(const char *cache_type, unsigned long *mask);
121 int get_mask_no_shareable(const char *cache_type, unsigned long *mask);
122 int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size);
123 void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
124 int signal_handler_register(void);
125 void signal_handler_unregister(void);
126 void cat_test_cleanup(void);
127 int cat_perf_miss_val(int cpu_no, int no_of_bits, char *cache_type);
128 int cmt_resctrl_val(int cpu_no, int n, const char * const *benchmark_cmd);
129 unsigned int count_bits(unsigned long n);
130 void cmt_test_cleanup(void);
132 void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config);
133 void perf_event_initialize_read_format(struct perf_event_read *pe_read);
134 int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no);
135 int perf_event_reset_enable(int pe_fd);
136 int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
137 const char *filename, int bm_pid);
138 int measure_llc_resctrl(const char *filename, int bm_pid);
139 void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines);
142 * cache_portion_size - Calculate the size of a cache portion
143 * @cache_size: Total cache size in bytes
144 * @portion_mask: Cache portion mask
145 * @full_cache_mask: Full Cache Bit Mask (CBM) for the cache
147 * Return: The size of the cache portion in bytes.
149 static inline unsigned long cache_portion_size(unsigned long cache_size,
150 unsigned long portion_mask,
151 unsigned long full_cache_mask)
153 unsigned int bits = count_bits(full_cache_mask);
156 * With no bits the full CBM, assume cache cannot be split into
157 * smaller portions. To avoid divide by zero, return cache_size.
162 return cache_size * count_bits(portion_mask) / bits;
165 #endif /* RESCTRL_H */