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