2020-01-16 13:32:41 -08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "resctrl.h"
|
|
|
|
|
|
|
|
struct read_format {
|
|
|
|
__u64 nr; /* The number of events */
|
|
|
|
struct {
|
|
|
|
__u64 value; /* The value of the event */
|
|
|
|
} values[2];
|
|
|
|
};
|
|
|
|
|
2020-01-16 13:32:42 -08:00
|
|
|
static struct perf_event_attr pea_llc_miss;
|
|
|
|
static struct read_format rf_cqm;
|
|
|
|
static int fd_lm;
|
2020-01-16 13:32:41 -08:00
|
|
|
char llc_occup_path[1024];
|
|
|
|
|
2020-01-16 13:32:42 -08:00
|
|
|
static void initialize_perf_event_attr(void)
|
|
|
|
{
|
|
|
|
pea_llc_miss.type = PERF_TYPE_HARDWARE;
|
|
|
|
pea_llc_miss.size = sizeof(struct perf_event_attr);
|
|
|
|
pea_llc_miss.read_format = PERF_FORMAT_GROUP;
|
|
|
|
pea_llc_miss.exclude_kernel = 1;
|
|
|
|
pea_llc_miss.exclude_hv = 1;
|
|
|
|
pea_llc_miss.exclude_idle = 1;
|
|
|
|
pea_llc_miss.exclude_callchain_kernel = 1;
|
|
|
|
pea_llc_miss.inherit = 1;
|
|
|
|
pea_llc_miss.exclude_guest = 1;
|
|
|
|
pea_llc_miss.disabled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ioctl_perf_event_ioc_reset_enable(void)
|
|
|
|
{
|
|
|
|
ioctl(fd_lm, PERF_EVENT_IOC_RESET, 0);
|
|
|
|
ioctl(fd_lm, PERF_EVENT_IOC_ENABLE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int perf_event_open_llc_miss(pid_t pid, int cpu_no)
|
|
|
|
{
|
|
|
|
fd_lm = perf_event_open(&pea_llc_miss, pid, cpu_no, -1,
|
|
|
|
PERF_FLAG_FD_CLOEXEC);
|
|
|
|
if (fd_lm == -1) {
|
2023-12-15 17:04:47 +02:00
|
|
|
ksft_perror("Error opening leader");
|
2020-01-16 13:32:42 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-15 15:06:02 +02:00
|
|
|
static void initialize_llc_perf(void)
|
2020-01-16 13:32:42 -08:00
|
|
|
{
|
|
|
|
memset(&pea_llc_miss, 0, sizeof(struct perf_event_attr));
|
|
|
|
memset(&rf_cqm, 0, sizeof(struct read_format));
|
|
|
|
|
|
|
|
/* Initialize perf_event_attr structures for HW_CACHE_MISSES */
|
|
|
|
initialize_perf_event_attr();
|
|
|
|
|
|
|
|
pea_llc_miss.config = PERF_COUNT_HW_CACHE_MISSES;
|
|
|
|
|
|
|
|
rf_cqm.nr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reset_enable_llc_perf(pid_t pid, int cpu_no)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = perf_event_open_llc_miss(pid, cpu_no);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Start counters to log values */
|
|
|
|
ioctl_perf_event_ioc_reset_enable();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_llc_perf: llc cache miss through perf events
|
2023-02-15 15:06:05 +02:00
|
|
|
* @llc_perf_miss: LLC miss counter that is filled on success
|
2020-01-16 13:32:42 -08:00
|
|
|
*
|
|
|
|
* Perf events like HW_CACHE_MISSES could be used to validate number of
|
|
|
|
* cache lines allocated.
|
|
|
|
*
|
|
|
|
* Return: =0 on success. <0 on failure.
|
|
|
|
*/
|
2023-12-15 17:04:59 +02:00
|
|
|
static int get_llc_perf(__u64 *llc_perf_miss)
|
2020-01-16 13:32:42 -08:00
|
|
|
{
|
2023-07-17 16:14:52 +03:00
|
|
|
int ret;
|
2020-01-16 13:32:42 -08:00
|
|
|
|
|
|
|
/* Stop counters after one span to get miss rate */
|
|
|
|
|
|
|
|
ioctl(fd_lm, PERF_EVENT_IOC_DISABLE, 0);
|
|
|
|
|
2023-07-17 16:14:52 +03:00
|
|
|
ret = read(fd_lm, &rf_cqm, sizeof(struct read_format));
|
|
|
|
if (ret == -1) {
|
2023-12-15 17:04:47 +02:00
|
|
|
ksft_perror("Could not get llc misses through perf");
|
2020-01-16 13:32:42 -08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-15 17:04:59 +02:00
|
|
|
*llc_perf_miss = rf_cqm.values[0].value;
|
2020-01-16 13:32:42 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-16 13:32:41 -08:00
|
|
|
/*
|
|
|
|
* Get LLC Occupancy as reported by RESCTRL FS
|
selftests/resctrl: Rename CQM test as CMT test
CMT (Cache Monitoring Technology) [1] is a H/W feature that reports cache
occupancy of a process. resctrl selftest suite has a unit test to test CMT
for LLC but the test is named as CQM (Cache Quality Monitoring).
Furthermore, the unit test source file is named as cqm_test.c and several
functions, variables, comments, preprocessors and statements widely use
"cqm" as either suffix or prefix. This rampant misusage of CQM for CMT
might confuse someone who is newly looking at resctrl selftests because
this feature is named CMT in the Intel Software Developer's Manual.
Hence, rename all the occurrences (unit test source file name, functions,
variables, comments and preprocessors) of cqm with cmt.
[1] Please see Intel SDM, Volume 3, chapter 17 and section 18 for more
information on CMT: https://software.intel.com/content/www/us/en/develop/articles/intel-sdm.html
Suggested-by: Reinette Chatre <reinette.chatre@intel.com>
Tested-by: Babu Moger <babu.moger@amd.com>
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2021-03-17 02:22:41 +00:00
|
|
|
* For CMT,
|
2020-01-16 13:32:41 -08:00
|
|
|
* 1. If con_mon grp and mon grp given, then read from mon grp in
|
|
|
|
* con_mon grp
|
|
|
|
* 2. If only con_mon grp given, then read from con_mon grp
|
|
|
|
* 3. If both not given, then read from root con_mon grp
|
|
|
|
* For CAT,
|
|
|
|
* 1. If con_mon grp given, then read from it
|
|
|
|
* 2. If con_mon grp not given, then read from root con_mon grp
|
|
|
|
*
|
|
|
|
* Return: =0 on success. <0 on failure.
|
|
|
|
*/
|
|
|
|
static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
fp = fopen(llc_occup_path, "r");
|
|
|
|
if (!fp) {
|
2023-12-15 17:04:47 +02:00
|
|
|
ksft_perror("Failed to open results file");
|
2020-01-16 13:32:41 -08:00
|
|
|
|
2023-12-15 17:04:48 +02:00
|
|
|
return -1;
|
2020-01-16 13:32:41 -08:00
|
|
|
}
|
|
|
|
if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
|
2023-12-15 17:04:47 +02:00
|
|
|
ksft_perror("Could not get llc occupancy");
|
2020-01-16 13:32:41 -08:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* print_results_cache: the cache results are stored in a file
|
|
|
|
* @filename: file that stores the results
|
|
|
|
* @bm_pid: child pid that runs benchmark
|
|
|
|
* @llc_value: perf miss value /
|
|
|
|
* llc occupancy value reported by resctrl FS
|
|
|
|
*
|
2023-12-15 17:04:48 +02:00
|
|
|
* Return: 0 on success, < 0 on error.
|
2020-01-16 13:32:41 -08:00
|
|
|
*/
|
2023-12-15 17:04:59 +02:00
|
|
|
static int print_results_cache(const char *filename, int bm_pid, __u64 llc_value)
|
2020-01-16 13:32:41 -08:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
|
2023-12-15 17:04:59 +02:00
|
|
|
printf("Pid: %d \t LLC_value: %llu\n", bm_pid, llc_value);
|
2020-01-16 13:32:41 -08:00
|
|
|
} else {
|
|
|
|
fp = fopen(filename, "a");
|
|
|
|
if (!fp) {
|
2023-12-15 17:04:47 +02:00
|
|
|
ksft_perror("Cannot open results file");
|
2020-01-16 13:32:41 -08:00
|
|
|
|
2023-12-15 17:04:48 +02:00
|
|
|
return -1;
|
2020-01-16 13:32:41 -08:00
|
|
|
}
|
2023-12-15 17:04:59 +02:00
|
|
|
fprintf(fp, "Pid: %d \t llc_value: %llu\n", bm_pid, llc_value);
|
2020-01-16 13:32:41 -08:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-15 17:04:57 +02:00
|
|
|
/*
|
|
|
|
* perf_event_measure - Measure perf events
|
|
|
|
* @filename: Filename for writing the results
|
|
|
|
* @bm_pid: PID that runs the benchmark
|
|
|
|
*
|
|
|
|
* Measures perf events (e.g., cache misses) and writes the results into
|
|
|
|
* @filename. @bm_pid is written to the results file along with the measured
|
|
|
|
* value.
|
|
|
|
*
|
|
|
|
* Return: =0 on success. <0 on failure.
|
|
|
|
*/
|
|
|
|
static int perf_event_measure(const char *filename, int bm_pid)
|
2020-01-16 13:32:41 -08:00
|
|
|
{
|
2023-12-15 17:04:59 +02:00
|
|
|
__u64 llc_perf_miss = 0;
|
2020-01-16 13:32:41 -08:00
|
|
|
int ret;
|
|
|
|
|
2023-12-15 17:04:57 +02:00
|
|
|
ret = get_llc_perf(&llc_perf_miss);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-01-16 13:32:42 -08:00
|
|
|
|
2023-12-15 17:04:57 +02:00
|
|
|
return print_results_cache(filename, bm_pid, llc_perf_miss);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* measure_llc_resctrl - Measure resctrl LLC value from resctrl
|
|
|
|
* @filename: Filename for writing the results
|
|
|
|
* @bm_pid: PID that runs the benchmark
|
|
|
|
*
|
|
|
|
* Measures LLC occupancy from resctrl and writes the results into @filename.
|
|
|
|
* @bm_pid is written to the results file along with the measured value.
|
|
|
|
*
|
|
|
|
* Return: =0 on success. <0 on failure.
|
|
|
|
*/
|
|
|
|
int measure_llc_resctrl(const char *filename, int bm_pid)
|
|
|
|
{
|
|
|
|
unsigned long llc_occu_resc = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = get_llc_occu_resctrl(&llc_occu_resc);
|
|
|
|
if (ret < 0)
|
2020-01-16 13:32:41 -08:00
|
|
|
return ret;
|
|
|
|
|
2023-12-15 17:04:57 +02:00
|
|
|
return print_results_cache(filename, bm_pid, llc_occu_resc);
|
2020-01-16 13:32:41 -08:00
|
|
|
}
|
2020-01-16 13:32:42 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* cache_val: execute benchmark and measure LLC occupancy resctrl
|
|
|
|
* and perf cache miss for the benchmark
|
|
|
|
* @param: parameters passed to cache_val()
|
2023-09-04 12:53:35 +03:00
|
|
|
* @span: buffer size for the benchmark
|
2020-01-16 13:32:42 -08:00
|
|
|
*
|
2023-12-15 17:04:50 +02:00
|
|
|
* Return: 0 when the test was run, < 0 on error.
|
2020-01-16 13:32:42 -08:00
|
|
|
*/
|
2023-09-04 12:53:35 +03:00
|
|
|
int cat_val(struct resctrl_val_param *param, size_t span)
|
2020-01-16 13:32:42 -08:00
|
|
|
{
|
2023-07-17 16:15:00 +03:00
|
|
|
int memflush = 1, operation = 0, ret = 0;
|
2020-01-16 13:32:42 -08:00
|
|
|
char *resctrl_val = param->resctrl_val;
|
|
|
|
pid_t bm_pid;
|
|
|
|
|
|
|
|
if (strcmp(param->filename, "") == 0)
|
|
|
|
sprintf(param->filename, "stdio");
|
|
|
|
|
|
|
|
bm_pid = getpid();
|
|
|
|
|
|
|
|
/* Taskset benchmark to specified cpu */
|
|
|
|
ret = taskset_benchmark(bm_pid, param->cpu_no);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/
|
|
|
|
ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
|
|
|
|
resctrl_val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2023-07-17 16:15:07 +03:00
|
|
|
initialize_llc_perf();
|
2020-01-16 13:32:42 -08:00
|
|
|
|
|
|
|
/* Test runs until the callback setup() tells the test to stop. */
|
|
|
|
while (1) {
|
2023-07-17 16:15:07 +03:00
|
|
|
ret = param->setup(param);
|
|
|
|
if (ret == END_OF_TESTS) {
|
|
|
|
ret = 0;
|
2020-01-16 13:32:42 -08:00
|
|
|
break;
|
|
|
|
}
|
2023-07-17 16:15:07 +03:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
ret = reset_enable_llc_perf(bm_pid, param->cpu_no);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
2023-09-04 12:53:35 +03:00
|
|
|
if (run_fill_buf(span, memflush, operation, true)) {
|
2023-07-17 16:15:07 +03:00
|
|
|
fprintf(stderr, "Error-running fill buffer\n");
|
|
|
|
ret = -1;
|
|
|
|
goto pe_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
2023-12-15 17:04:57 +02:00
|
|
|
ret = perf_event_measure(param->filename, bm_pid);
|
2023-07-17 16:15:07 +03:00
|
|
|
if (ret)
|
|
|
|
goto pe_close;
|
2020-01-16 13:32:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2023-07-17 16:14:52 +03:00
|
|
|
|
|
|
|
pe_close:
|
|
|
|
close(fd_lm);
|
|
|
|
return ret;
|
2020-01-16 13:32:42 -08:00
|
|
|
}
|
2021-03-17 02:22:43 +00:00
|
|
|
|
|
|
|
/*
|
2023-12-15 17:04:58 +02:00
|
|
|
* show_cache_info - Show generic cache test information
|
|
|
|
* @no_of_bits: Number of bits
|
|
|
|
* @avg_llc_val: Average of LLC cache result data
|
|
|
|
* @cache_span: Cache span
|
|
|
|
* @lines: @cache_span in lines or bytes
|
2021-03-17 02:22:43 +00:00
|
|
|
*/
|
2023-12-15 17:04:59 +02:00
|
|
|
void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines)
|
2021-03-17 02:22:43 +00:00
|
|
|
{
|
|
|
|
ksft_print_msg("Number of bits: %d\n", no_of_bits);
|
2023-12-15 17:04:59 +02:00
|
|
|
ksft_print_msg("Average LLC val: %llu\n", avg_llc_val);
|
2023-12-15 17:04:58 +02:00
|
|
|
ksft_print_msg("Cache span (%s): %zu\n", lines ? "lines" : "bytes",
|
2021-03-17 02:22:43 +00:00
|
|
|
cache_span);
|
|
|
|
}
|