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