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 <math.h>
7 #include <errno.h>
8 #include <sched.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <dirent.h>
14 #include <stdbool.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 "../kselftest.h"
26 
27 #define MB			(1024 * 1024)
28 #define RESCTRL_PATH		"/sys/fs/resctrl"
29 #define PHYS_ID_PATH		"/sys/devices/system/cpu/cpu"
30 #define INFO_PATH		"/sys/fs/resctrl/info"
31 #define L3_PATH			"/sys/fs/resctrl/info/L3"
32 #define MB_PATH			"/sys/fs/resctrl/info/MB"
33 #define L3_MON_PATH		"/sys/fs/resctrl/info/L3_MON"
34 #define L3_MON_FEATURES_PATH	"/sys/fs/resctrl/info/L3_MON/mon_features"
35 
36 #define ARCH_INTEL     1
37 #define ARCH_AMD       2
38 
39 #define END_OF_TESTS	1
40 
41 #define PARENT_EXIT(err_msg)			\
42 	do {					\
43 		perror(err_msg);		\
44 		kill(ppid, SIGKILL);		\
45 		umount_resctrlfs();		\
46 		exit(EXIT_FAILURE);		\
47 	} while (0)
48 
49 /*
50  * resctrl_val_param:	resctrl test parameters
51  * @resctrl_val:	Resctrl feature (Eg: mbm, mba.. etc)
52  * @ctrlgrp:		Name of the control monitor group (con_mon grp)
53  * @mongrp:		Name of the monitor group (mon grp)
54  * @cpu_no:		CPU number to which the benchmark would be binded
55  * @span:		Memory bytes accessed in each benchmark iteration
56  * @filename:		Name of file to which the o/p should be written
57  * @bw_report:		Bandwidth report type (reads vs writes)
58  * @setup:		Call back function to setup test environment
59  */
60 struct resctrl_val_param {
61 	char		*resctrl_val;
62 	char		ctrlgrp[64];
63 	char		mongrp[64];
64 	int		cpu_no;
65 	size_t		span;
66 	char		filename[64];
67 	char		*bw_report;
68 	unsigned long	mask;
69 	int		num_of_runs;
70 	int		(*setup)(struct resctrl_val_param *param);
71 };
72 
73 #define MBM_STR			"mbm"
74 #define MBA_STR			"mba"
75 #define CMT_STR			"cmt"
76 #define CAT_STR			"cat"
77 
78 extern pid_t bm_pid, ppid;
79 
80 extern char llc_occup_path[1024];
81 
82 int get_vendor(void);
83 bool check_resctrlfs_support(void);
84 int filter_dmesg(void);
85 int get_resource_id(int cpu_no, int *resource_id);
86 int mount_resctrlfs(void);
87 int umount_resctrlfs(void);
88 int validate_bw_report_request(char *bw_report);
89 bool validate_resctrl_feature_request(const char *resctrl_val);
90 char *fgrep(FILE *inf, const char *str);
91 int taskset_benchmark(pid_t bm_pid, int cpu_no);
92 void run_benchmark(int signum, siginfo_t *info, void *ucontext);
93 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no,
94 		   char *resctrl_val);
95 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
96 			    char *resctrl_val);
97 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
98 		    int group_fd, unsigned long flags);
99 int run_fill_buf(size_t span, int memflush, int op, bool once);
100 int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param);
101 int mbm_bw_change(size_t span, int cpu_no, char *bw_report, char **benchmark_cmd);
102 void tests_cleanup(void);
103 void mbm_test_cleanup(void);
104 int mba_schemata_change(int cpu_no, char *bw_report, char **benchmark_cmd);
105 void mba_test_cleanup(void);
106 int get_cbm_mask(char *cache_type, char *cbm_mask);
107 int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size);
108 void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
109 int signal_handler_register(void);
110 void signal_handler_unregister(void);
111 int cat_val(struct resctrl_val_param *param);
112 void cat_test_cleanup(void);
113 int cat_perf_miss_val(int cpu_no, int no_of_bits, char *cache_type);
114 int cmt_resctrl_val(int cpu_no, int n, char **benchmark_cmd);
115 unsigned int count_bits(unsigned long n);
116 void cmt_test_cleanup(void);
117 int get_core_sibling(int cpu_no);
118 int measure_cache_vals(struct resctrl_val_param *param, int bm_pid);
119 int show_cache_info(unsigned long sum_llc_val, int no_of_bits,
120 		    size_t cache_span, unsigned long max_diff,
121 		    unsigned long max_diff_percent, unsigned long num_of_runs,
122 		    bool platform, bool cmt);
123 
124 #endif /* RESCTRL_H */
125