178941183SFenghua Yu // SPDX-License-Identifier: GPL-2.0
278941183SFenghua Yu
378941183SFenghua Yu #include <stdint.h>
478941183SFenghua Yu #include "resctrl.h"
578941183SFenghua Yu
678941183SFenghua Yu char llc_occup_path[1024];
778941183SFenghua Yu
perf_event_attr_initialize(struct perf_event_attr * pea,__u64 config)8433f437bSIlpo Järvinen void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config)
9790bf585SFenghua Yu {
103cdad0a5SIlpo Järvinen memset(pea, 0, sizeof(*pea));
113cdad0a5SIlpo Järvinen pea->type = PERF_TYPE_HARDWARE;
123cdad0a5SIlpo Järvinen pea->size = sizeof(*pea);
133cdad0a5SIlpo Järvinen pea->read_format = PERF_FORMAT_GROUP;
143cdad0a5SIlpo Järvinen pea->exclude_kernel = 1;
153cdad0a5SIlpo Järvinen pea->exclude_hv = 1;
163cdad0a5SIlpo Järvinen pea->exclude_idle = 1;
173cdad0a5SIlpo Järvinen pea->exclude_callchain_kernel = 1;
183cdad0a5SIlpo Järvinen pea->inherit = 1;
193cdad0a5SIlpo Järvinen pea->exclude_guest = 1;
203cdad0a5SIlpo Järvinen pea->disabled = 1;
213cdad0a5SIlpo Järvinen pea->config = config;
22790bf585SFenghua Yu }
23790bf585SFenghua Yu
243c6bfc9cSIlpo Järvinen /* Start counters to log values */
perf_event_reset_enable(int pe_fd)252892731eSIlpo Järvinen int perf_event_reset_enable(int pe_fd)
26790bf585SFenghua Yu {
272892731eSIlpo Järvinen int ret;
282892731eSIlpo Järvinen
292892731eSIlpo Järvinen ret = ioctl(pe_fd, PERF_EVENT_IOC_RESET, 0);
302892731eSIlpo Järvinen if (ret < 0)
312892731eSIlpo Järvinen return ret;
322892731eSIlpo Järvinen
332892731eSIlpo Järvinen ret = ioctl(pe_fd, PERF_EVENT_IOC_ENABLE, 0);
342892731eSIlpo Järvinen if (ret < 0)
352892731eSIlpo Järvinen return ret;
362892731eSIlpo Järvinen
372892731eSIlpo Järvinen return 0;
38790bf585SFenghua Yu }
39790bf585SFenghua Yu
perf_event_initialize_read_format(struct perf_event_read * pe_read)40433f437bSIlpo Järvinen void perf_event_initialize_read_format(struct perf_event_read *pe_read)
41790bf585SFenghua Yu {
423cdad0a5SIlpo Järvinen memset(pe_read, 0, sizeof(*pe_read));
433cdad0a5SIlpo Järvinen pe_read->nr = 1;
44790bf585SFenghua Yu }
45790bf585SFenghua Yu
perf_open(struct perf_event_attr * pea,pid_t pid,int cpu_no)46433f437bSIlpo Järvinen int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no)
47790bf585SFenghua Yu {
483cdad0a5SIlpo Järvinen int pe_fd;
493cdad0a5SIlpo Järvinen
503cdad0a5SIlpo Järvinen pe_fd = perf_event_open(pea, pid, cpu_no, -1, PERF_FLAG_FD_CLOEXEC);
51b6e6a582SIlpo Järvinen if (pe_fd == -1) {
523c6bfc9cSIlpo Järvinen ksft_perror("Error opening leader");
53790bf585SFenghua Yu return -1;
54790bf585SFenghua Yu }
55790bf585SFenghua Yu
563cdad0a5SIlpo Järvinen perf_event_reset_enable(pe_fd);
57790bf585SFenghua Yu
583cdad0a5SIlpo Järvinen return pe_fd;
59790bf585SFenghua Yu }
60790bf585SFenghua Yu
6178941183SFenghua Yu /*
6278941183SFenghua Yu * Get LLC Occupancy as reported by RESCTRL FS
632f320911SFenghua Yu * For CMT,
6478941183SFenghua Yu * 1. If con_mon grp and mon grp given, then read from mon grp in
6578941183SFenghua Yu * con_mon grp
6678941183SFenghua Yu * 2. If only con_mon grp given, then read from con_mon grp
6778941183SFenghua Yu * 3. If both not given, then read from root con_mon grp
6878941183SFenghua Yu * For CAT,
6978941183SFenghua Yu * 1. If con_mon grp given, then read from it
7078941183SFenghua Yu * 2. If con_mon grp not given, then read from root con_mon grp
7178941183SFenghua Yu *
7278941183SFenghua Yu * Return: =0 on success. <0 on failure.
7378941183SFenghua Yu */
get_llc_occu_resctrl(unsigned long * llc_occupancy)7478941183SFenghua Yu static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
7578941183SFenghua Yu {
7678941183SFenghua Yu FILE *fp;
7778941183SFenghua Yu
7878941183SFenghua Yu fp = fopen(llc_occup_path, "r");
7978941183SFenghua Yu if (!fp) {
80cc8ff7f5SIlpo Järvinen ksft_perror("Failed to open results file");
8178941183SFenghua Yu
82c90fba60SIlpo Järvinen return -1;
8378941183SFenghua Yu }
8478941183SFenghua Yu if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
85cc8ff7f5SIlpo Järvinen ksft_perror("Could not get llc occupancy");
8678941183SFenghua Yu fclose(fp);
8778941183SFenghua Yu
8878941183SFenghua Yu return -1;
8978941183SFenghua Yu }
9078941183SFenghua Yu fclose(fp);
9178941183SFenghua Yu
9278941183SFenghua Yu return 0;
9378941183SFenghua Yu }
9478941183SFenghua Yu
9578941183SFenghua Yu /*
9678941183SFenghua Yu * print_results_cache: the cache results are stored in a file
9778941183SFenghua Yu * @filename: file that stores the results
9878941183SFenghua Yu * @bm_pid: child pid that runs benchmark
9978941183SFenghua Yu * @llc_value: perf miss value /
10078941183SFenghua Yu * llc occupancy value reported by resctrl FS
10178941183SFenghua Yu *
102c90fba60SIlpo Järvinen * Return: 0 on success, < 0 on error.
10378941183SFenghua Yu */
print_results_cache(const char * filename,pid_t bm_pid,__u64 llc_value)104*8245a70eSIlpo Järvinen static int print_results_cache(const char *filename, pid_t bm_pid, __u64 llc_value)
10578941183SFenghua Yu {
10678941183SFenghua Yu FILE *fp;
10778941183SFenghua Yu
10878941183SFenghua Yu if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
109*8245a70eSIlpo Järvinen printf("Pid: %d \t LLC_value: %llu\n", (int)bm_pid, llc_value);
11078941183SFenghua Yu } else {
11178941183SFenghua Yu fp = fopen(filename, "a");
11278941183SFenghua Yu if (!fp) {
113cc8ff7f5SIlpo Järvinen ksft_perror("Cannot open results file");
11478941183SFenghua Yu
115c90fba60SIlpo Järvinen return -1;
11678941183SFenghua Yu }
117*8245a70eSIlpo Järvinen fprintf(fp, "Pid: %d \t llc_value: %llu\n", (int)bm_pid, llc_value);
11878941183SFenghua Yu fclose(fp);
11978941183SFenghua Yu }
12078941183SFenghua Yu
12178941183SFenghua Yu return 0;
12278941183SFenghua Yu }
12378941183SFenghua Yu
124a575c973SIlpo Järvinen /*
125a575c973SIlpo Järvinen * perf_event_measure - Measure perf events
126a575c973SIlpo Järvinen * @filename: Filename for writing the results
127a575c973SIlpo Järvinen * @bm_pid: PID that runs the benchmark
128a575c973SIlpo Järvinen *
129a575c973SIlpo Järvinen * Measures perf events (e.g., cache misses) and writes the results into
130a575c973SIlpo Järvinen * @filename. @bm_pid is written to the results file along with the measured
131a575c973SIlpo Järvinen * value.
132a575c973SIlpo Järvinen *
133a575c973SIlpo Järvinen * Return: =0 on success. <0 on failure.
134a575c973SIlpo Järvinen */
perf_event_measure(int pe_fd,struct perf_event_read * pe_read,const char * filename,pid_t bm_pid)135433f437bSIlpo Järvinen int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
136*8245a70eSIlpo Järvinen const char *filename, pid_t bm_pid)
13778941183SFenghua Yu {
13878941183SFenghua Yu int ret;
13978941183SFenghua Yu
1403c6bfc9cSIlpo Järvinen /* Stop counters after one span to get miss rate */
1412892731eSIlpo Järvinen ret = ioctl(pe_fd, PERF_EVENT_IOC_DISABLE, 0);
1422892731eSIlpo Järvinen if (ret < 0)
1432892731eSIlpo Järvinen return ret;
144a575c973SIlpo Järvinen
1453cdad0a5SIlpo Järvinen ret = read(pe_fd, pe_read, sizeof(*pe_read));
1463c6bfc9cSIlpo Järvinen if (ret == -1) {
1473c6bfc9cSIlpo Järvinen ksft_perror("Could not get perf value");
1483c6bfc9cSIlpo Järvinen return -1;
1493c6bfc9cSIlpo Järvinen }
1503c6bfc9cSIlpo Järvinen
1513cdad0a5SIlpo Järvinen return print_results_cache(filename, bm_pid, pe_read->values[0].value);
152790bf585SFenghua Yu }
153790bf585SFenghua Yu
154790bf585SFenghua Yu /*
155a575c973SIlpo Järvinen * measure_llc_resctrl - Measure resctrl LLC value from resctrl
156a575c973SIlpo Järvinen * @filename: Filename for writing the results
157a575c973SIlpo Järvinen * @bm_pid: PID that runs the benchmark
158a575c973SIlpo Järvinen *
159a575c973SIlpo Järvinen * Measures LLC occupancy from resctrl and writes the results into @filename.
160a575c973SIlpo Järvinen * @bm_pid is written to the results file along with the measured value.
161a575c973SIlpo Järvinen *
162a575c973SIlpo Järvinen * Return: =0 on success. <0 on failure.
16378941183SFenghua Yu */
measure_llc_resctrl(const char * filename,pid_t bm_pid)164*8245a70eSIlpo Järvinen int measure_llc_resctrl(const char *filename, pid_t bm_pid)
165a575c973SIlpo Järvinen {
166a575c973SIlpo Järvinen unsigned long llc_occu_resc = 0;
167a575c973SIlpo Järvinen int ret;
168a575c973SIlpo Järvinen
16978941183SFenghua Yu ret = get_llc_occu_resctrl(&llc_occu_resc);
17078941183SFenghua Yu if (ret < 0)
17178941183SFenghua Yu return ret;
17278941183SFenghua Yu
173a575c973SIlpo Järvinen return print_results_cache(filename, bm_pid, llc_occu_resc);
17478941183SFenghua Yu }
175790bf585SFenghua Yu
176790bf585SFenghua Yu /*
1775caf1b64SIlpo Järvinen * show_cache_info - Show generic cache test information
1785caf1b64SIlpo Järvinen * @no_of_bits: Number of bits
1795caf1b64SIlpo Järvinen * @avg_llc_val: Average of LLC cache result data
1805caf1b64SIlpo Järvinen * @cache_span: Cache span
1815caf1b64SIlpo Järvinen * @lines: @cache_span in lines or bytes
18203216ed7SFenghua Yu */
show_cache_info(int no_of_bits,__u64 avg_llc_val,size_t cache_span,bool lines)18333403bc7SIlpo Järvinen void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines)
18403216ed7SFenghua Yu {
18503216ed7SFenghua Yu ksft_print_msg("Number of bits: %d\n", no_of_bits);
18633403bc7SIlpo Järvinen ksft_print_msg("Average LLC val: %llu\n", avg_llc_val);
1875caf1b64SIlpo Järvinen ksft_print_msg("Cache span (%s): %zu\n", lines ? "lines" : "bytes",
18803216ed7SFenghua Yu cache_span);
18903216ed7SFenghua Yu }
190