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