selftests/resctrl: Rewrite Cache Allocation Technology (CAT) test
[sfrench/cifs-2.6.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 BENCHMARK_ARGS          64
37
38 #define DEFAULT_SPAN            (250 * MB)
39
40 #define PARENT_EXIT()                           \
41         do {                                    \
42                 kill(ppid, SIGKILL);            \
43                 umount_resctrlfs();             \
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  * @filename:           Name of file to which the o/p should be written
54  * @bw_report:          Bandwidth report type (reads vs writes)
55  * @setup:              Call back function to setup test environment
56  */
57 struct resctrl_val_param {
58         char            *resctrl_val;
59         char            ctrlgrp[64];
60         char            mongrp[64];
61         int             cpu_no;
62         char            filename[64];
63         char            *bw_report;
64         unsigned long   mask;
65         int             num_of_runs;
66         int             (*setup)(struct resctrl_val_param *param);
67 };
68
69 struct perf_event_read {
70         __u64 nr;                       /* The number of events */
71         struct {
72                 __u64 value;            /* The value of the event */
73         } values[2];
74 };
75
76 #define MBM_STR                 "mbm"
77 #define MBA_STR                 "mba"
78 #define CMT_STR                 "cmt"
79 #define CAT_STR                 "cat"
80
81 /*
82  * Memory location that consumes values compiler must not optimize away.
83  * Volatile ensures writes to this location cannot be optimized away by
84  * compiler.
85  */
86 extern volatile int *value_sink;
87
88 extern pid_t bm_pid, ppid;
89
90 extern char llc_occup_path[1024];
91
92 int get_vendor(void);
93 bool check_resctrlfs_support(void);
94 int filter_dmesg(void);
95 int get_resource_id(int cpu_no, int *resource_id);
96 int mount_resctrlfs(void);
97 int umount_resctrlfs(void);
98 int validate_bw_report_request(char *bw_report);
99 bool validate_resctrl_feature_request(const char *resource, const char *feature);
100 char *fgrep(FILE *inf, const char *str);
101 int taskset_benchmark(pid_t bm_pid, int cpu_no);
102 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no,
103                    char *resctrl_val);
104 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
105                             char *resctrl_val);
106 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
107                     int group_fd, unsigned long flags);
108 unsigned char *alloc_buffer(size_t buf_size, int memflush);
109 void mem_flush(unsigned char *buf, size_t buf_size);
110 void fill_cache_read(unsigned char *buf, size_t buf_size, bool once);
111 int run_fill_buf(size_t buf_size, int memflush, int op, bool once);
112 int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *param);
113 int mbm_bw_change(int cpu_no, const char * const *benchmark_cmd);
114 void tests_cleanup(void);
115 void mbm_test_cleanup(void);
116 int mba_schemata_change(int cpu_no, const char * const *benchmark_cmd);
117 void mba_test_cleanup(void);
118 unsigned long create_bit_mask(unsigned int start, unsigned int len);
119 unsigned int count_contiguous_bits(unsigned long val, unsigned int *start);
120 int get_full_cbm(const char *cache_type, unsigned long *mask);
121 int get_mask_no_shareable(const char *cache_type, unsigned long *mask);
122 int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size);
123 void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
124 int signal_handler_register(void);
125 void signal_handler_unregister(void);
126 void cat_test_cleanup(void);
127 int cat_perf_miss_val(int cpu_no, int no_of_bits, char *cache_type);
128 int cmt_resctrl_val(int cpu_no, int n, const char * const *benchmark_cmd);
129 unsigned int count_bits(unsigned long n);
130 void cmt_test_cleanup(void);
131
132 void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config);
133 void perf_event_initialize_read_format(struct perf_event_read *pe_read);
134 int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no);
135 int perf_event_reset_enable(int pe_fd);
136 int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
137                        const char *filename, int bm_pid);
138 int measure_llc_resctrl(const char *filename, int bm_pid);
139 void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines);
140
141 /*
142  * cache_portion_size - Calculate the size of a cache portion
143  * @cache_size:         Total cache size in bytes
144  * @portion_mask:       Cache portion mask
145  * @full_cache_mask:    Full Cache Bit Mask (CBM) for the cache
146  *
147  * Return: The size of the cache portion in bytes.
148  */
149 static inline unsigned long cache_portion_size(unsigned long cache_size,
150                                                unsigned long portion_mask,
151                                                unsigned long full_cache_mask)
152 {
153         unsigned int bits = count_bits(full_cache_mask);
154
155         /*
156          * With no bits the full CBM, assume cache cannot be split into
157          * smaller portions. To avoid divide by zero, return cache_size.
158          */
159         if (!bits)
160                 return cache_size;
161
162         return cache_size * count_bits(portion_mask) / bits;
163 }
164
165 #endif /* RESCTRL_H */