1 // SPDX-License-Identifier: GPL-2.0
6 char llc_occup_path
[1024];
8 void perf_event_attr_initialize(struct perf_event_attr
*pea
, __u64 config
)
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;
16 pea
->exclude_idle
= 1;
17 pea
->exclude_callchain_kernel
= 1;
19 pea
->exclude_guest
= 1;
24 /* Start counters to log values */
25 int perf_event_reset_enable(int pe_fd
)
29 ret
= ioctl(pe_fd
, PERF_EVENT_IOC_RESET
, 0);
33 ret
= ioctl(pe_fd
, PERF_EVENT_IOC_ENABLE
, 0);
40 void perf_event_initialize_read_format(struct perf_event_read
*pe_read
)
42 memset(pe_read
, 0, sizeof(*pe_read
));
46 int perf_open(struct perf_event_attr
*pea
, pid_t pid
, int cpu_no
)
50 pe_fd
= perf_event_open(pea
, pid
, cpu_no
, -1, PERF_FLAG_FD_CLOEXEC
);
52 ksft_perror("Error opening leader");
56 perf_event_reset_enable(pe_fd
);
62 * Get LLC Occupancy as reported by RESCTRL FS
64 * 1. If con_mon grp and mon grp given, then read from mon grp in
66 * 2. If only con_mon grp given, then read from con_mon grp
67 * 3. If both not given, then read from root con_mon grp
69 * 1. If con_mon grp given, then read from it
70 * 2. If con_mon grp not given, then read from root con_mon grp
72 * Return: =0 on success. <0 on failure.
74 static int get_llc_occu_resctrl(unsigned long *llc_occupancy
)
78 fp
= fopen(llc_occup_path
, "r");
80 ksft_perror("Failed to open results file");
84 if (fscanf(fp
, "%lu", llc_occupancy
) <= 0) {
85 ksft_perror("Could not get llc occupancy");
96 * print_results_cache: the cache results are stored in a file
97 * @filename: file that stores the results
98 * @bm_pid: child pid that runs benchmark
99 * @llc_value: perf miss value /
100 * llc occupancy value reported by resctrl FS
102 * Return: 0 on success, < 0 on error.
104 static int print_results_cache(const char *filename
, pid_t bm_pid
, __u64 llc_value
)
108 if (strcmp(filename
, "stdio") == 0 || strcmp(filename
, "stderr") == 0) {
109 printf("Pid: %d \t LLC_value: %llu\n", (int)bm_pid
, llc_value
);
111 fp
= fopen(filename
, "a");
113 ksft_perror("Cannot open results file");
117 fprintf(fp
, "Pid: %d \t llc_value: %llu\n", (int)bm_pid
, llc_value
);
125 * perf_event_measure - Measure perf events
126 * @filename: Filename for writing the results
127 * @bm_pid: PID that runs the benchmark
129 * Measures perf events (e.g., cache misses) and writes the results into
130 * @filename. @bm_pid is written to the results file along with the measured
133 * Return: =0 on success. <0 on failure.
135 int perf_event_measure(int pe_fd
, struct perf_event_read
*pe_read
,
136 const char *filename
, pid_t bm_pid
)
140 /* Stop counters after one span to get miss rate */
141 ret
= ioctl(pe_fd
, PERF_EVENT_IOC_DISABLE
, 0);
145 ret
= read(pe_fd
, pe_read
, sizeof(*pe_read
));
147 ksft_perror("Could not get perf value");
151 return print_results_cache(filename
, bm_pid
, pe_read
->values
[0].value
);
155 * measure_llc_resctrl - Measure resctrl LLC value from resctrl
156 * @filename: Filename for writing the results
157 * @bm_pid: PID that runs the benchmark
159 * Measures LLC occupancy from resctrl and writes the results into @filename.
160 * @bm_pid is written to the results file along with the measured value.
162 * Return: =0 on success. <0 on failure.
164 int measure_llc_resctrl(const char *filename
, pid_t bm_pid
)
166 unsigned long llc_occu_resc
= 0;
169 ret
= get_llc_occu_resctrl(&llc_occu_resc
);
173 return print_results_cache(filename
, bm_pid
, llc_occu_resc
);
177 * show_cache_info - Show generic cache test information
178 * @no_of_bits: Number of bits
179 * @avg_llc_val: Average of LLC cache result data
180 * @cache_span: Cache span
181 * @lines: @cache_span in lines or bytes
183 void show_cache_info(int no_of_bits
, __u64 avg_llc_val
, size_t cache_span
, bool lines
)
185 ksft_print_msg("Number of bits: %d\n", no_of_bits
);
186 ksft_print_msg("Average LLC val: %llu\n", avg_llc_val
);
187 ksft_print_msg("Cache span (%s): %zu\n", lines
? "lines" : "bytes",