2020-01-16 13:32:42 -08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Cache Allocation Technology (CAT) test
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
|
|
|
|
* Fenghua Yu <fenghua.yu@intel.com>
|
|
|
|
*/
|
|
|
|
#include "resctrl.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
#define RESULT_FILE_NAME "result_cat"
|
2020-01-16 13:32:42 -08:00
|
|
|
#define NUM_OF_RUNS 5
|
|
|
|
|
|
|
|
/*
|
2023-12-15 17:05:08 +02:00
|
|
|
* Minimum difference in LLC misses between a test with n+1 bits CBM to the
|
|
|
|
* test with n bits is MIN_DIFF_PERCENT_PER_BIT * (n - 1). With e.g. 5 vs 4
|
|
|
|
* bits in the CBM mask, the minimum difference must be at least
|
|
|
|
* MIN_DIFF_PERCENT_PER_BIT * (4 - 1) = 3 percent.
|
|
|
|
*
|
|
|
|
* The relationship between number of used CBM bits and difference in LLC
|
|
|
|
* misses is not expected to be linear. With a small number of bits, the
|
|
|
|
* margin is smaller than with larger number of bits. For selftest purposes,
|
|
|
|
* however, linear approach is enough because ultimately only pass/fail
|
|
|
|
* decision has to be made and distinction between strong and stronger
|
|
|
|
* signal is irrelevant.
|
2020-01-16 13:32:42 -08:00
|
|
|
*/
|
2023-12-15 17:05:08 +02:00
|
|
|
#define MIN_DIFF_PERCENT_PER_BIT 1UL
|
2020-01-16 13:32:42 -08:00
|
|
|
|
2023-12-15 17:04:59 +02:00
|
|
|
static int show_results_info(__u64 sum_llc_val, int no_of_bits,
|
2023-12-15 17:05:08 +02:00
|
|
|
unsigned long cache_span,
|
|
|
|
unsigned long min_diff_percent,
|
|
|
|
unsigned long num_of_runs, bool platform,
|
|
|
|
__s64 *prev_avg_llc_val)
|
2023-12-15 17:04:58 +02:00
|
|
|
{
|
2023-12-15 17:04:59 +02:00
|
|
|
__u64 avg_llc_val = 0;
|
2023-12-15 17:05:08 +02:00
|
|
|
float avg_diff;
|
|
|
|
int ret = 0;
|
2023-12-15 17:04:58 +02:00
|
|
|
|
|
|
|
avg_llc_val = sum_llc_val / num_of_runs;
|
2023-12-15 17:05:08 +02:00
|
|
|
if (*prev_avg_llc_val) {
|
|
|
|
float delta = (__s64)(avg_llc_val - *prev_avg_llc_val);
|
2023-12-15 17:04:58 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
avg_diff = delta / *prev_avg_llc_val;
|
|
|
|
ret = platform && (avg_diff * 100) < (float)min_diff_percent;
|
2023-12-15 17:04:58 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
ksft_print_msg("%s Check cache miss rate changed more than %.1f%%\n",
|
|
|
|
ret ? "Fail:" : "Pass:", (float)min_diff_percent);
|
2023-12-15 17:04:58 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
ksft_print_msg("Percent diff=%.1f\n", avg_diff * 100);
|
|
|
|
}
|
|
|
|
*prev_avg_llc_val = avg_llc_val;
|
2023-12-15 17:04:58 +02:00
|
|
|
|
|
|
|
show_cache_info(no_of_bits, avg_llc_val, cache_span, true);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
/* Remove the highest bit from CBM */
|
|
|
|
static unsigned long next_mask(unsigned long current_mask)
|
|
|
|
{
|
|
|
|
return current_mask & (current_mask >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_results(struct resctrl_val_param *param, const char *cache_type,
|
|
|
|
unsigned long cache_total_size, unsigned long full_cache_mask,
|
|
|
|
unsigned long current_mask)
|
2020-01-16 13:32:42 -08:00
|
|
|
{
|
|
|
|
char *token_array[8], temp[512];
|
2023-12-15 17:04:59 +02:00
|
|
|
__u64 sum_llc_perf_miss = 0;
|
2023-12-15 17:05:08 +02:00
|
|
|
__s64 prev_avg_llc_val = 0;
|
|
|
|
unsigned long alloc_size;
|
|
|
|
int runs = 0;
|
|
|
|
int fail = 0;
|
|
|
|
int ret;
|
2020-01-16 13:32:42 -08:00
|
|
|
FILE *fp;
|
|
|
|
|
2021-03-17 02:22:42 +00:00
|
|
|
ksft_print_msg("Checking for pass/fail\n");
|
2020-01-16 13:32:42 -08:00
|
|
|
fp = fopen(param->filename, "r");
|
|
|
|
if (!fp) {
|
2023-12-15 17:04:47 +02:00
|
|
|
ksft_perror("Cannot open file");
|
2020-01-16 13:32:42 -08:00
|
|
|
|
2023-12-15 17:04:48 +02:00
|
|
|
return -1;
|
2020-01-16 13:32:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(temp, sizeof(temp), fp)) {
|
|
|
|
char *token = strtok(temp, ":\t");
|
|
|
|
int fields = 0;
|
2023-12-15 17:05:08 +02:00
|
|
|
int bits;
|
2020-01-16 13:32:42 -08:00
|
|
|
|
|
|
|
while (token) {
|
|
|
|
token_array[fields++] = token;
|
|
|
|
token = strtok(NULL, ":\t");
|
|
|
|
}
|
2023-12-15 17:05:08 +02:00
|
|
|
|
|
|
|
sum_llc_perf_miss += strtoull(token_array[3], NULL, 0);
|
2020-01-16 13:32:42 -08:00
|
|
|
runs++;
|
2023-12-15 17:05:08 +02:00
|
|
|
|
|
|
|
if (runs < NUM_OF_RUNS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!current_mask) {
|
|
|
|
ksft_print_msg("Unexpected empty cache mask\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc_size = cache_portion_size(cache_total_size, current_mask, full_cache_mask);
|
|
|
|
|
|
|
|
bits = count_bits(current_mask);
|
|
|
|
|
|
|
|
ret = show_results_info(sum_llc_perf_miss, bits,
|
|
|
|
alloc_size / 64,
|
|
|
|
MIN_DIFF_PERCENT_PER_BIT * (bits - 1),
|
|
|
|
runs, get_vendor() == ARCH_INTEL,
|
|
|
|
&prev_avg_llc_val);
|
|
|
|
if (ret)
|
|
|
|
fail = 1;
|
|
|
|
|
|
|
|
runs = 0;
|
|
|
|
sum_llc_perf_miss = 0;
|
|
|
|
current_mask = next_mask(current_mask);
|
2020-01-16 13:32:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
return fail;
|
2020-01-16 13:32:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cat_test_cleanup(void)
|
|
|
|
{
|
2023-12-15 17:05:08 +02:00
|
|
|
remove(RESULT_FILE_NAME);
|
2020-01-16 13:32:42 -08:00
|
|
|
}
|
|
|
|
|
2023-12-15 17:05:04 +02:00
|
|
|
/*
|
|
|
|
* cat_test - Execute CAT benchmark and measure cache misses
|
2023-12-15 17:05:12 +02:00
|
|
|
* @test: Test information structure
|
2023-12-15 17:05:10 +02:00
|
|
|
* @uparams: User supplied parameters
|
2023-12-15 17:05:04 +02:00
|
|
|
* @param: Parameters passed to cat_test()
|
|
|
|
* @span: Buffer size for the benchmark
|
2023-12-15 17:05:08 +02:00
|
|
|
* @current_mask Start mask for the first iteration
|
|
|
|
*
|
|
|
|
* Run CAT selftest by varying the allocated cache portion and comparing the
|
|
|
|
* impact on cache misses (the result analysis is done in check_results()
|
|
|
|
* and show_results_info(), not in this function).
|
|
|
|
*
|
|
|
|
* One bit is removed from the CAT allocation bit mask (in current_mask) for
|
|
|
|
* each subsequent test which keeps reducing the size of the allocated cache
|
|
|
|
* portion. A single test flushes the buffer, reads it to warm up the cache,
|
|
|
|
* and reads the buffer again. The cache misses are measured during the last
|
|
|
|
* read pass.
|
2023-12-15 17:05:04 +02:00
|
|
|
*
|
|
|
|
* Return: 0 when the test was run, < 0 on error.
|
|
|
|
*/
|
2023-12-15 17:05:12 +02:00
|
|
|
static int cat_test(const struct resctrl_test *test,
|
|
|
|
const struct user_params *uparams,
|
|
|
|
struct resctrl_val_param *param,
|
2023-12-15 17:05:10 +02:00
|
|
|
size_t span, unsigned long current_mask)
|
2023-12-15 17:05:04 +02:00
|
|
|
{
|
|
|
|
char *resctrl_val = param->resctrl_val;
|
|
|
|
struct perf_event_read pe_read;
|
|
|
|
struct perf_event_attr pea;
|
2023-12-15 17:05:09 +02:00
|
|
|
cpu_set_t old_affinity;
|
2023-12-15 17:05:08 +02:00
|
|
|
unsigned char *buf;
|
|
|
|
char schemata[64];
|
|
|
|
int ret, i, pe_fd;
|
2023-12-15 17:05:04 +02:00
|
|
|
pid_t bm_pid;
|
|
|
|
|
|
|
|
if (strcmp(param->filename, "") == 0)
|
|
|
|
sprintf(param->filename, "stdio");
|
|
|
|
|
|
|
|
bm_pid = getpid();
|
|
|
|
|
|
|
|
/* Taskset benchmark to specified cpu */
|
2023-12-15 17:05:10 +02:00
|
|
|
ret = taskset_benchmark(bm_pid, uparams->cpu, &old_affinity);
|
2023-12-15 17:05:04 +02:00
|
|
|
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)
|
2023-12-15 17:05:09 +02:00
|
|
|
goto reset_affinity;
|
2023-12-15 17:05:04 +02:00
|
|
|
|
|
|
|
perf_event_attr_initialize(&pea, PERF_COUNT_HW_CACHE_MISSES);
|
|
|
|
perf_event_initialize_read_format(&pe_read);
|
2023-12-15 17:05:10 +02:00
|
|
|
pe_fd = perf_open(&pea, bm_pid, uparams->cpu);
|
2023-12-15 17:05:09 +02:00
|
|
|
if (pe_fd < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto reset_affinity;
|
|
|
|
}
|
2023-12-15 17:05:04 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
buf = alloc_buffer(span, 1);
|
|
|
|
if (!buf) {
|
|
|
|
ret = -1;
|
|
|
|
goto pe_close;
|
|
|
|
}
|
2023-12-15 17:05:05 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
while (current_mask) {
|
|
|
|
snprintf(schemata, sizeof(schemata), "%lx", param->mask & ~current_mask);
|
2023-12-15 17:05:12 +02:00
|
|
|
ret = write_schemata("", schemata, uparams->cpu, test->resource);
|
2023-12-15 17:05:05 +02:00
|
|
|
if (ret)
|
2023-12-15 17:05:08 +02:00
|
|
|
goto free_buf;
|
|
|
|
snprintf(schemata, sizeof(schemata), "%lx", current_mask);
|
2023-12-15 17:05:12 +02:00
|
|
|
ret = write_schemata(param->ctrlgrp, schemata, uparams->cpu, test->resource);
|
2023-12-15 17:05:08 +02:00
|
|
|
if (ret)
|
|
|
|
goto free_buf;
|
2023-12-15 17:05:04 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
for (i = 0; i < NUM_OF_RUNS; i++) {
|
|
|
|
mem_flush(buf, span);
|
|
|
|
fill_cache_read(buf, span, true);
|
2023-12-15 17:05:04 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
ret = perf_event_reset_enable(pe_fd);
|
|
|
|
if (ret)
|
|
|
|
goto free_buf;
|
2023-12-15 17:05:04 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
fill_cache_read(buf, span, true);
|
2023-12-15 17:05:04 +02:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
ret = perf_event_measure(pe_fd, &pe_read, param->filename, bm_pid);
|
|
|
|
if (ret)
|
|
|
|
goto free_buf;
|
|
|
|
}
|
|
|
|
current_mask = next_mask(current_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_buf:
|
|
|
|
free(buf);
|
2023-12-15 17:05:04 +02:00
|
|
|
pe_close:
|
|
|
|
close(pe_fd);
|
2023-12-15 17:05:09 +02:00
|
|
|
reset_affinity:
|
|
|
|
taskset_restore(bm_pid, &old_affinity);
|
2023-12-15 17:05:08 +02:00
|
|
|
|
2023-12-15 17:05:04 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-12-15 17:05:11 +02:00
|
|
|
static int cat_run_test(const struct resctrl_test *test, const struct user_params *uparams)
|
2020-01-16 13:32:42 -08:00
|
|
|
{
|
2023-12-15 17:05:08 +02:00
|
|
|
unsigned long long_mask, start_mask, full_cache_mask;
|
2023-12-15 17:04:55 +02:00
|
|
|
unsigned long cache_total_size = 0;
|
2023-12-15 17:05:10 +02:00
|
|
|
int n = uparams->bits;
|
2023-12-15 17:05:08 +02:00
|
|
|
unsigned int start;
|
2023-07-17 16:15:05 +03:00
|
|
|
int count_of_bits;
|
2023-09-04 12:53:35 +03:00
|
|
|
size_t span;
|
2023-12-15 17:05:08 +02:00
|
|
|
int ret;
|
2020-01-16 13:32:42 -08:00
|
|
|
|
2023-12-15 17:05:11 +02:00
|
|
|
ret = get_full_cbm(test->resource, &full_cache_mask);
|
2023-12-15 17:04:56 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
/* Get the largest contiguous exclusive portion of the cache */
|
2023-12-15 17:05:11 +02:00
|
|
|
ret = get_mask_no_shareable(test->resource, &long_mask);
|
2020-01-16 13:32:42 -08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Get L3/L2 cache size */
|
2023-12-15 17:05:11 +02:00
|
|
|
ret = get_cache_size(uparams->cpu, test->resource, &cache_total_size);
|
2020-01-16 13:32:42 -08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2023-12-15 17:04:55 +02:00
|
|
|
ksft_print_msg("Cache size :%lu\n", cache_total_size);
|
2020-01-16 13:32:42 -08:00
|
|
|
|
2023-12-15 17:05:08 +02:00
|
|
|
count_of_bits = count_contiguous_bits(long_mask, &start);
|
2020-01-16 13:32:42 -08:00
|
|
|
|
2021-03-17 02:22:49 +00:00
|
|
|
if (!n)
|
|
|
|
n = count_of_bits / 2;
|
|
|
|
|
|
|
|
if (n > count_of_bits - 1) {
|
2021-03-17 02:22:42 +00:00
|
|
|
ksft_print_msg("Invalid input value for no_of_bits n!\n");
|
|
|
|
ksft_print_msg("Please enter value in range 1 to %d\n",
|
|
|
|
count_of_bits - 1);
|
2020-01-16 13:32:42 -08:00
|
|
|
return -1;
|
|
|
|
}
|
2023-12-15 17:05:08 +02:00
|
|
|
start_mask = create_bit_mask(start, n);
|
2020-01-16 13:32:42 -08:00
|
|
|
|
|
|
|
struct resctrl_val_param param = {
|
2021-03-17 02:22:38 +00:00
|
|
|
.resctrl_val = CAT_STR,
|
2023-12-15 17:05:08 +02:00
|
|
|
.ctrlgrp = "c1",
|
|
|
|
.filename = RESULT_FILE_NAME,
|
|
|
|
.num_of_runs = 0,
|
2020-01-16 13:32:42 -08:00
|
|
|
};
|
2023-12-15 17:05:08 +02:00
|
|
|
param.mask = long_mask;
|
|
|
|
span = cache_portion_size(cache_total_size, start_mask, full_cache_mask);
|
2020-01-16 13:32:42 -08:00
|
|
|
|
|
|
|
remove(param.filename);
|
|
|
|
|
2023-12-15 17:05:12 +02:00
|
|
|
ret = cat_test(test, uparams, ¶m, span, start_mask);
|
2023-12-15 17:05:08 +02:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
2020-01-16 13:32:42 -08:00
|
|
|
|
2023-12-15 17:05:11 +02:00
|
|
|
ret = check_results(¶m, test->resource,
|
|
|
|
cache_total_size, full_cache_mask, start_mask);
|
2023-12-15 17:05:08 +02:00
|
|
|
out:
|
2020-01-16 13:32:42 -08:00
|
|
|
cat_test_cleanup();
|
|
|
|
|
2023-04-13 16:22:57 +09:00
|
|
|
return ret;
|
2020-01-16 13:32:42 -08:00
|
|
|
}
|
2023-12-15 17:05:11 +02:00
|
|
|
|
|
|
|
struct resctrl_test l3_cat_test = {
|
|
|
|
.name = "CAT",
|
|
|
|
.resource = "L3",
|
|
|
|
.feature_check = test_resource_feature_check,
|
|
|
|
.run_test = cat_run_test,
|
|
|
|
};
|