1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <stdint.h>
4 #include "resctrl.h"
5 
6 char llc_occup_path[1024];
7 
8 void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config)
9 {
10 	memset(pea, 0, sizeof(*pea));
11 	pea->type = PERF_TYPE_HARDWARE;
12 	pea->size = sizeof(*pea);
13 	pea->read_format = PERF_FORMAT_GROUP;
14 	pea->exclude_kernel = 1;
15 	pea->exclude_hv = 1;
16 	pea->exclude_idle = 1;
17 	pea->exclude_callchain_kernel = 1;
18 	pea->inherit = 1;
19 	pea->exclude_guest = 1;
20 	pea->disabled = 1;
21 	pea->config = config;
22 }
23 
24 /* Start counters to log values */
25 static void perf_event_reset_enable(int pe_fd)
26 {
27 	ioctl(pe_fd, PERF_EVENT_IOC_RESET, 0);
28 	ioctl(pe_fd, PERF_EVENT_IOC_ENABLE, 0);
29 }
30 
31 void perf_event_initialize_read_format(struct perf_event_read *pe_read)
32 {
33 	memset(pe_read, 0, sizeof(*pe_read));
34 	pe_read->nr = 1;
35 }
36 
37 int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no)
38 {
39 	int pe_fd;
40 
41 	pe_fd = perf_event_open(pea, pid, cpu_no, -1, PERF_FLAG_FD_CLOEXEC);
42 	if (pe_fd == -1) {
43 		ksft_perror("Error opening leader");
44 		return -1;
45 	}
46 
47 	perf_event_reset_enable(pe_fd);
48 
49 	return pe_fd;
50 }
51 
52 /*
53  * Get LLC Occupancy as reported by RESCTRL FS
54  * For CMT,
55  * 1. If con_mon grp and mon grp given, then read from mon grp in
56  * con_mon grp
57  * 2. If only con_mon grp given, then read from con_mon grp
58  * 3. If both not given, then read from root con_mon grp
59  * For CAT,
60  * 1. If con_mon grp given, then read from it
61  * 2. If con_mon grp not given, then read from root con_mon grp
62  *
63  * Return: =0 on success.  <0 on failure.
64  */
65 static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
66 {
67 	FILE *fp;
68 
69 	fp = fopen(llc_occup_path, "r");
70 	if (!fp) {
71 		ksft_perror("Failed to open results file");
72 
73 		return -1;
74 	}
75 	if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
76 		ksft_perror("Could not get llc occupancy");
77 		fclose(fp);
78 
79 		return -1;
80 	}
81 	fclose(fp);
82 
83 	return 0;
84 }
85 
86 /*
87  * print_results_cache:	the cache results are stored in a file
88  * @filename:		file that stores the results
89  * @bm_pid:		child pid that runs benchmark
90  * @llc_value:		perf miss value /
91  *			llc occupancy value reported by resctrl FS
92  *
93  * Return:		0 on success, < 0 on error.
94  */
95 static int print_results_cache(const char *filename, int bm_pid, __u64 llc_value)
96 {
97 	FILE *fp;
98 
99 	if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
100 		printf("Pid: %d \t LLC_value: %llu\n", bm_pid, llc_value);
101 	} else {
102 		fp = fopen(filename, "a");
103 		if (!fp) {
104 			ksft_perror("Cannot open results file");
105 
106 			return -1;
107 		}
108 		fprintf(fp, "Pid: %d \t llc_value: %llu\n", bm_pid, llc_value);
109 		fclose(fp);
110 	}
111 
112 	return 0;
113 }
114 
115 /*
116  * perf_event_measure - Measure perf events
117  * @filename:	Filename for writing the results
118  * @bm_pid:	PID that runs the benchmark
119  *
120  * Measures perf events (e.g., cache misses) and writes the results into
121  * @filename. @bm_pid is written to the results file along with the measured
122  * value.
123  *
124  * Return: =0 on success. <0 on failure.
125  */
126 int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
127 		       const char *filename, int bm_pid)
128 {
129 	int ret;
130 
131 	/* Stop counters after one span to get miss rate */
132 	ioctl(pe_fd, PERF_EVENT_IOC_DISABLE, 0);
133 
134 	ret = read(pe_fd, pe_read, sizeof(*pe_read));
135 	if (ret == -1) {
136 		ksft_perror("Could not get perf value");
137 		return -1;
138 	}
139 
140 	return print_results_cache(filename, bm_pid, pe_read->values[0].value);
141 }
142 
143 /*
144  * measure_llc_resctrl - Measure resctrl LLC value from resctrl
145  * @filename:	Filename for writing the results
146  * @bm_pid:	PID that runs the benchmark
147  *
148  * Measures LLC occupancy from resctrl and writes the results into @filename.
149  * @bm_pid is written to the results file along with the measured value.
150  *
151  * Return: =0 on success. <0 on failure.
152  */
153 int measure_llc_resctrl(const char *filename, int bm_pid)
154 {
155 	unsigned long llc_occu_resc = 0;
156 	int ret;
157 
158 	ret = get_llc_occu_resctrl(&llc_occu_resc);
159 	if (ret < 0)
160 		return ret;
161 
162 	return print_results_cache(filename, bm_pid, llc_occu_resc);
163 }
164 
165 /*
166  * show_cache_info - Show generic cache test information
167  * @no_of_bits:		Number of bits
168  * @avg_llc_val:	Average of LLC cache result data
169  * @cache_span:		Cache span
170  * @lines:		@cache_span in lines or bytes
171  */
172 void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines)
173 {
174 	ksft_print_msg("Number of bits: %d\n", no_of_bits);
175 	ksft_print_msg("Average LLC val: %llu\n", avg_llc_val);
176 	ksft_print_msg("Cache span (%s): %zu\n", lines ? "lines" : "bytes",
177 		       cache_span);
178 }
179