Linux Audio

Check our new training course

Loading...
v6.13.7
  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 MINIMUM_SPAN		(250 * MB)
 45
 46/*
 47 * Memory bandwidth (in MiB) below which the bandwidth comparisons
 48 * between iMC and resctrl are considered unreliable. For example RAS
 49 * features or memory performance features that generate memory traffic
 50 * may drive accesses that are counted differently by performance counters
 51 * and MBM respectively, for instance generating "overhead" traffic which
 52 * is not counted against any specific RMID.
 53 */
 54#define THROTTLE_THRESHOLD	750
 55
 56/*
 57 * fill_buf_param:	"fill_buf" benchmark parameters
 58 * @buf_size:		Size (in bytes) of buffer used in benchmark.
 59 *			"fill_buf" allocates and initializes buffer of
 60 *			@buf_size. User can change value via command line.
 61 * @memflush:		If false the buffer will not be flushed after
 62 *			allocation and initialization, otherwise the
 63 *			buffer will be flushed. User can change value via
 64 *			command line (via integers with 0 interpreted as
 65 *			false and anything else as true).
 66 */
 67struct fill_buf_param {
 68	size_t		buf_size;
 69	bool		memflush;
 70};
 71
 72/*
 73 * user_params:		User supplied parameters
 74 * @cpu:		CPU number to which the benchmark will be bound to
 75 * @bits:		Number of bits used for cache allocation size
 76 * @benchmark_cmd:	Benchmark command to run during (some of the) tests
 77 * @fill_buf:		Pointer to user provided parameters for "fill_buf",
 78 *			NULL if user did not provide parameters and test
 79 *			specific defaults should be used.
 80 */
 81struct user_params {
 82	int cpu;
 83	int bits;
 84	const char *benchmark_cmd[BENCHMARK_ARGS];
 85	const struct fill_buf_param *fill_buf;
 86};
 87
 88/*
 89 * resctrl_test:	resctrl test definition
 90 * @name:		Test name
 91 * @group:		Test group - a common name for tests that share some characteristic
 92 *			(e.g., L3 CAT test belongs to the CAT group). Can be NULL
 93 * @resource:		Resource to test (e.g., MB, L3, L2, etc.)
 94 * @vendor_specific:	Bitmask for vendor-specific tests (can be 0 for universal tests)
 95 * @disabled:		Test is disabled
 96 * @feature_check:	Callback to check required resctrl features
 97 * @run_test:		Callback to run the test
 98 * @cleanup:		Callback to cleanup after the test
 99 */
100struct resctrl_test {
101	const char	*name;
102	const char	*group;
103	const char	*resource;
104	unsigned int	vendor_specific;
105	bool		disabled;
106	bool		(*feature_check)(const struct resctrl_test *test);
107	int		(*run_test)(const struct resctrl_test *test,
108				    const struct user_params *uparams);
109	void		(*cleanup)(void);
110};
111
112/*
113 * resctrl_val_param:	resctrl test parameters
 
114 * @ctrlgrp:		Name of the control monitor group (con_mon grp)
115 * @mongrp:		Name of the monitor group (mon grp)
 
 
 
116 * @filename:		Name of file to which the o/p should be written
117 * @init:		Callback function to initialize test environment
118 * @setup:		Callback function to setup per test run environment
119 * @measure:		Callback that performs the measurement (a single test)
120 * @fill_buf:		Parameters for default "fill_buf" benchmark.
121 *			Initialized with user provided parameters, possibly
122 *			adapted to be relevant to the test. If user does
123 *			not provide parameters for "fill_buf" nor a
124 *			replacement benchmark then initialized with defaults
125 *			appropriate for test. NULL if user provided
126 *			benchmark.
127 */
128struct resctrl_val_param {
129	const char		*ctrlgrp;
130	const char		*mongrp;
131	char			filename[64];
132	unsigned long		mask;
133	int			num_of_runs;
134	int			(*init)(const struct resctrl_val_param *param,
135					int domain_id);
136	int			(*setup)(const struct resctrl_test *test,
137					 const struct user_params *uparams,
138					 struct resctrl_val_param *param);
139	int			(*measure)(const struct user_params *uparams,
140					   struct resctrl_val_param *param,
141					   pid_t bm_pid);
142	struct fill_buf_param	*fill_buf;
143};
144
145struct perf_event_read {
146	__u64 nr;			/* The number of events */
147	struct {
148		__u64 value;		/* The value of the event */
149	} values[2];
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 */
157extern volatile int *value_sink;
158
159extern char llc_occup_path[1024];
160
161int get_vendor(void);
162bool check_resctrlfs_support(void);
163int filter_dmesg(void);
164int get_domain_id(const char *resource, int cpu_no, int *domain_id);
165int mount_resctrlfs(void);
166int umount_resctrlfs(void);
167bool resctrl_resource_exists(const char *resource);
168bool resctrl_mon_feature_exists(const char *resource, const char *feature);
169bool resource_info_file_exists(const char *resource, const char *file);
170bool test_resource_feature_check(const struct resctrl_test *test);
171char *fgrep(FILE *inf, const char *str);
172int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity);
173int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity);
174int write_schemata(const char *ctrlgrp, char *schemata, int cpu_no,
175		   const char *resource);
176int write_bm_pid_to_resctrl(pid_t bm_pid, const char *ctrlgrp, const char *mongrp);
 
177int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
178		    int group_fd, unsigned long flags);
179unsigned char *alloc_buffer(size_t buf_size, bool memflush);
180void mem_flush(unsigned char *buf, size_t buf_size);
181void fill_cache_read(unsigned char *buf, size_t buf_size, bool once);
182ssize_t get_fill_buf_size(int cpu_no, const char *cache_type);
183int initialize_read_mem_bw_imc(void);
184int measure_read_mem_bw(const struct user_params *uparams,
185			struct resctrl_val_param *param, pid_t bm_pid);
186void initialize_mem_bw_resctrl(const struct resctrl_val_param *param,
187			       int domain_id);
188int resctrl_val(const struct resctrl_test *test,
189		const struct user_params *uparams,
190		struct resctrl_val_param *param);
191unsigned long create_bit_mask(unsigned int start, unsigned int len);
192unsigned int count_contiguous_bits(unsigned long val, unsigned int *start);
193int get_full_cbm(const char *cache_type, unsigned long *mask);
194int get_mask_no_shareable(const char *cache_type, unsigned long *mask);
195int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size);
196int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val);
197void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
198int signal_handler_register(const struct resctrl_test *test);
199void signal_handler_unregister(void);
 
 
200unsigned int count_bits(unsigned long n);
201
202void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config);
203void perf_event_initialize_read_format(struct perf_event_read *pe_read);
204int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no);
205int perf_event_reset_enable(int pe_fd);
206int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
207		       const char *filename, pid_t bm_pid);
208int measure_llc_resctrl(const char *filename, pid_t bm_pid);
209void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines);
210
211/*
212 * cache_portion_size - Calculate the size of a cache portion
213 * @cache_size:		Total cache size in bytes
214 * @portion_mask:	Cache portion mask
215 * @full_cache_mask:	Full Cache Bit Mask (CBM) for the cache
216 *
217 * Return: The size of the cache portion in bytes.
218 */
219static inline unsigned long cache_portion_size(unsigned long cache_size,
220					       unsigned long portion_mask,
221					       unsigned long full_cache_mask)
222{
223	unsigned int bits = count_bits(full_cache_mask);
224
225	/*
226	 * With no bits the full CBM, assume cache cannot be split into
227	 * smaller portions. To avoid divide by zero, return cache_size.
228	 */
229	if (!bits)
230		return cache_size;
231
232	return cache_size * count_bits(portion_mask) / bits;
233}
234
235extern struct resctrl_test mbm_test;
236extern struct resctrl_test mba_test;
237extern struct resctrl_test cmt_test;
238extern struct resctrl_test l3_cat_test;
239extern struct resctrl_test l3_noncont_cat_test;
240extern struct resctrl_test l2_noncont_cat_test;
241
242#endif /* RESCTRL_H */
v6.2
  1/* SPDX-License-Identifier: GPL-2.0 */
  2#define _GNU_SOURCE
  3#ifndef RESCTRL_H
  4#define RESCTRL_H
  5#include <stdio.h>
  6#include <stdarg.h>
  7#include <math.h>
  8#include <errno.h>
  9#include <sched.h>
 10#include <stdlib.h>
 11#include <unistd.h>
 12#include <string.h>
 13#include <signal.h>
 14#include <dirent.h>
 15#include <stdbool.h>
 16#include <sys/stat.h>
 17#include <sys/ioctl.h>
 18#include <sys/mount.h>
 19#include <sys/types.h>
 20#include <sys/wait.h>
 21#include <sys/select.h>
 22#include <sys/time.h>
 23#include <sys/eventfd.h>
 24#include <asm/unistd.h>
 25#include <linux/perf_event.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 CBM_MASK_PATH		"/sys/fs/resctrl/info"
 32#define L3_PATH			"/sys/fs/resctrl/info/L3"
 33#define MB_PATH			"/sys/fs/resctrl/info/MB"
 34#define L3_MON_PATH		"/sys/fs/resctrl/info/L3_MON"
 35#define L3_MON_FEATURES_PATH	"/sys/fs/resctrl/info/L3_MON/mon_features"
 36
 
 
 
 
 
 
 37#define ARCH_INTEL     1
 38#define ARCH_AMD       2
 39
 40#define PARENT_EXIT(err_msg)			\
 41	do {					\
 42		perror(err_msg);		\
 43		kill(ppid, SIGKILL);		\
 44		exit(EXIT_FAILURE);		\
 45	} while (0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 46
 47/*
 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 * @span:		Memory bytes accessed in each benchmark iteration
 54 * @mum_resctrlfs:	Should the resctrl FS be remounted?
 55 * @filename:		Name of file to which the o/p should be written
 56 * @bw_report:		Bandwidth report type (reads vs writes)
 57 * @setup:		Call back function to setup test environment
 
 
 
 
 
 
 
 
 58 */
 59struct resctrl_val_param {
 60	char		*resctrl_val;
 61	char		ctrlgrp[64];
 62	char		mongrp[64];
 63	int		cpu_no;
 64	unsigned long	span;
 65	int		mum_resctrlfs;
 66	char		filename[64];
 67	char		*bw_report;
 68	unsigned long	mask;
 69	int		num_of_runs;
 70	int		(*setup)(int num, ...);
 
 
 
 71};
 72
 73#define MBM_STR			"mbm"
 74#define MBA_STR			"mba"
 75#define CMT_STR			"cmt"
 76#define CAT_STR			"cat"
 
 
 77
 78extern pid_t bm_pid, ppid;
 
 
 
 
 
 79
 80extern char llc_occup_path[1024];
 81
 82int get_vendor(void);
 83bool check_resctrlfs_support(void);
 84int filter_dmesg(void);
 85int remount_resctrlfs(bool mum_resctrlfs);
 86int get_resource_id(int cpu_no, int *resource_id);
 87int umount_resctrlfs(void);
 88int validate_bw_report_request(char *bw_report);
 89bool validate_resctrl_feature_request(const char *resctrl_val);
 
 
 90char *fgrep(FILE *inf, const char *str);
 91int taskset_benchmark(pid_t bm_pid, int cpu_no);
 92void run_benchmark(int signum, siginfo_t *info, void *ucontext);
 93int write_schemata(char *ctrlgrp, char *schemata, int cpu_no,
 94		   char *resctrl_val);
 95int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
 96			    char *resctrl_val);
 97int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
 98		    int group_fd, unsigned long flags);
 99int run_fill_buf(unsigned long span, int malloc_and_init_memory, int memflush,
100		 int op, char *resctrl_va);
101int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param);
102int mbm_bw_change(int span, int cpu_no, char *bw_report, char **benchmark_cmd);
103void tests_cleanup(void);
104void mbm_test_cleanup(void);
105int mba_schemata_change(int cpu_no, char *bw_report, char **benchmark_cmd);
106void mba_test_cleanup(void);
107int get_cbm_mask(char *cache_type, char *cbm_mask);
108int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size);
 
 
 
 
 
 
 
 
109void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
110int cat_val(struct resctrl_val_param *param);
111void cat_test_cleanup(void);
112int cat_perf_miss_val(int cpu_no, int no_of_bits, char *cache_type);
113int cmt_resctrl_val(int cpu_no, int n, char **benchmark_cmd);
114unsigned int count_bits(unsigned long n);
115void cmt_test_cleanup(void);
116int get_core_sibling(int cpu_no);
117int measure_cache_vals(struct resctrl_val_param *param, int bm_pid);
118int show_cache_info(unsigned long sum_llc_val, int no_of_bits,
119		    unsigned long cache_span, unsigned long max_diff,
120		    unsigned long max_diff_percent, unsigned long num_of_runs,
121		    bool platform, bool cmt);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
123#endif /* RESCTRL_H */