#include <fcntl.h>
#include <limits.h>
#include "resctrl.h"
int snc_unreliable;
static int find_resctrl_mount(char *buffer)
{
FILE *mounts;
char line[256], *fs, *mntpoint;
mounts = fopen("/proc/mounts", "r");
if (!mounts) {
ksft_perror("/proc/mounts");
return -ENXIO;
}
while (!feof(mounts)) {
if (!fgets(line, 256, mounts))
break;
fs = strtok(line, " \t");
if (!fs)
continue;
mntpoint = strtok(NULL, " \t");
if (!mntpoint)
continue;
fs = strtok(NULL, " \t");
if (!fs)
continue;
if (strcmp(fs, "resctrl"))
continue;
fclose(mounts);
if (buffer)
strncpy(buffer, mntpoint, 256);
return 0;
}
fclose(mounts);
return -ENOENT;
}
int mount_resctrlfs(void)
{
int ret;
ret = find_resctrl_mount(NULL);
if (ret != -ENOENT)
return -1;
ksft_print_msg("Mounting resctrl to \"%s\"\n", RESCTRL_PATH);
ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
if (ret)
ksft_perror("mount");
return ret;
}
int umount_resctrlfs(void)
{
char mountpoint[256];
int ret;
ret = find_resctrl_mount(mountpoint);
if (ret == -ENOENT)
return 0;
if (ret)
return ret;
if (umount(mountpoint)) {
ksft_perror("Unable to umount resctrl");
return -1;
}
return 0;
}
static int get_cache_level(const char *cache_type)
{
if (!strcmp(cache_type, "L3"))
return 3;
if (!strcmp(cache_type, "L2"))
return 2;
ksft_print_msg("Invalid cache level\n");
return -1;
}
static int get_resource_cache_level(const char *resource)
{
if (!strcmp(resource, "MB"))
return 3;
return get_cache_level(resource);
}
int get_domain_id(const char *resource, int cpu_no, int *domain_id)
{
char phys_pkg_path[1024];
int cache_num;
FILE *fp;
cache_num = get_resource_cache_level(resource);
if (cache_num < 0)
return cache_num;
sprintf(phys_pkg_path, "%s%d/cache/index%d/id", PHYS_ID_PATH, cpu_no, cache_num);
fp = fopen(phys_pkg_path, "r");
if (!fp) {
ksft_perror("Failed to open cache id file");
return -1;
}
if (fscanf(fp, "%d", domain_id) <= 0) {
ksft_perror("Could not get domain ID");
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
static unsigned int count_sys_bitmap_bits(char *name)
{
FILE *fp = fopen(name, "r");
int count = 0, c;
if (!fp)
return 0;
while ((c = fgetc(fp)) != EOF) {
if (!isxdigit(c))
continue;
switch (c) {
case 'f':
count++;
fallthrough;
case '7': case 'b': case 'd': case 'e':
count++;
fallthrough;
case '3': case '5': case '6': case '9': case 'a': case 'c':
count++;
fallthrough;
case '1': case '2': case '4': case '8':
count++;
break;
}
}
fclose(fp);
return count;
}
static bool cpus_offline_empty(void)
{
char offline_cpus_str[64];
FILE *fp;
fp = fopen("/sys/devices/system/cpu/offline", "r");
if (!fp) {
ksft_perror("Could not open /sys/devices/system/cpu/offline");
return 0;
}
if (fscanf(fp, "%63s", offline_cpus_str) < 0) {
if (!errno) {
fclose(fp);
return 1;
}
ksft_perror("Could not read /sys/devices/system/cpu/offline");
}
fclose(fp);
return 0;
}
int snc_nodes_per_l3_cache(void)
{
int node_cpus, cache_cpus;
static int snc_mode;
if (!snc_mode) {
snc_mode = 1;
if (!cpus_offline_empty()) {
ksft_print_msg("Runtime SNC detection unreliable due to offline CPUs.\n");
ksft_print_msg("Setting SNC mode to disabled.\n");
snc_unreliable = 1;
return snc_mode;
}
node_cpus = count_sys_bitmap_bits("/sys/devices/system/node/node0/cpumap");
cache_cpus = count_sys_bitmap_bits("/sys/devices/system/cpu/cpu0/cache/index3/shared_cpu_map");
if (!node_cpus || !cache_cpus) {
ksft_print_msg("Could not determine Sub-NUMA Cluster mode.\n");
snc_unreliable = 1;
return snc_mode;
}
snc_mode = cache_cpus / node_cpus;
if (!snc_mode)
snc_mode = 1;
if (snc_mode > 1)
ksft_print_msg("SNC-%d mode discovered.\n", snc_mode);
}
return snc_mode;
}
int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size)
{
char cache_path[1024], cache_str[64];
int length, i, cache_num;
FILE *fp;
cache_num = get_cache_level(cache_type);
if (cache_num < 0)
return cache_num;
sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
cpu_no, cache_num);
fp = fopen(cache_path, "r");
if (!fp) {
ksft_perror("Failed to open cache size");
return -1;
}
if (fscanf(fp, "%63s", cache_str) <= 0) {
ksft_perror("Could not get cache_size");
fclose(fp);
return -1;
}
fclose(fp);
length = (int)strlen(cache_str);
*cache_size = 0;
for (i = 0; i < length; i++) {
if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
*cache_size = *cache_size * 10 + (cache_str[i] - '0');
else if (cache_str[i] == 'K')
*cache_size = *cache_size * 1024;
else if (cache_str[i] == 'M')
*cache_size = *cache_size * 1024 * 1024;
else
break;
}
if (cache_num == 3)
*cache_size /= snc_nodes_per_l3_cache();
return 0;
}
#define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu"
static int get_bit_mask(const char *filename, unsigned long *mask)
{
FILE *fp;
if (!filename || !mask)
return -1;
fp = fopen(filename, "r");
if (!fp) {
ksft_print_msg("Failed to open bit mask file '%s': %s\n",
filename, strerror(errno));
return -1;
}
if (fscanf(fp, "%lx", mask) <= 0) {
ksft_print_msg("Could not read bit mask file '%s': %s\n",
filename, strerror(errno));
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
int resource_info_unsigned_get(const char *resource, const char *filename,
unsigned int *val)
{
char file_path[PATH_MAX];
FILE *fp;
snprintf(file_path, sizeof(file_path), "%s/%s/%s", INFO_PATH, resource,
filename);
fp = fopen(file_path, "r");
if (!fp) {
ksft_print_msg("Error opening %s: %m\n", file_path);
return -1;
}
if (fscanf(fp, "%u", val) <= 0) {
ksft_print_msg("Could not get contents of %s: %m\n", file_path);
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
unsigned long create_bit_mask(unsigned int start, unsigned int len)
{
return ((1UL << len) - 1UL) << start;
}
unsigned int count_contiguous_bits(unsigned long val, unsigned int *start)
{
unsigned long last_val;
unsigned int count = 0;
while (val) {
last_val = val;
val &= (val >> 1);
count++;
}
if (start) {
if (count)
*start = ffsl(last_val) - 1;
else
*start = 0;
}
return count;
}
int get_full_cbm(const char *cache_type, unsigned long *mask)
{
char cbm_path[PATH_MAX];
int ret;
if (!cache_type)
return -1;
snprintf(cbm_path, sizeof(cbm_path), "%s/%s/cbm_mask",
INFO_PATH, cache_type);
ret = get_bit_mask(cbm_path, mask);
if (ret || !*mask)
return -1;
return 0;
}
static int get_shareable_mask(const char *cache_type, unsigned long *shareable_mask)
{
char mask_path[PATH_MAX];
if (!cache_type)
return -1;
snprintf(mask_path, sizeof(mask_path), "%s/%s/shareable_bits",
INFO_PATH, cache_type);
return get_bit_mask(mask_path, shareable_mask);
}
int get_mask_no_shareable(const char *cache_type, unsigned long *mask)
{
unsigned long full_mask, shareable_mask;
unsigned int start, len;
if (get_full_cbm(cache_type, &full_mask) < 0)
return -1;
if (get_shareable_mask(cache_type, &shareable_mask) < 0)
return -1;
len = count_contiguous_bits(full_mask & ~shareable_mask, &start);
if (!len)
return -1;
*mask = create_bit_mask(start, len);
return 0;
}
int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity)
{
cpu_set_t my_set;
if (old_affinity) {
CPU_ZERO(old_affinity);
if (sched_getaffinity(bm_pid, sizeof(*old_affinity),
old_affinity)) {
ksft_perror("Unable to read CPU affinity");
return -1;
}
}
CPU_ZERO(&my_set);
CPU_SET(cpu_no, &my_set);
if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
ksft_perror("Unable to taskset benchmark");
return -1;
}
return 0;
}
int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity)
{
if (sched_setaffinity(bm_pid, sizeof(*old_affinity), old_affinity)) {
ksft_perror("Unable to restore CPU affinity");
return -1;
}
return 0;
}
static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
{
int found_grp = 0;
struct dirent *ep;
DIR *dp;
if (!grp_name)
return 0;
dp = opendir(parent_grp);
if (dp) {
while ((ep = readdir(dp)) != NULL) {
if (strcmp(ep->d_name, grp_name) == 0)
found_grp = 1;
}
closedir(dp);
} else {
ksft_perror("Unable to open resctrl for group");
return -1;
}
if (found_grp == 0) {
if (mkdir(grp, 0) == -1) {
ksft_perror("Unable to create group");
return -1;
}
}
return 0;
}
static int write_pid_to_tasks(char *tasks, pid_t pid)
{
FILE *fp;
fp = fopen(tasks, "w");
if (!fp) {
ksft_perror("Failed to open tasks file");
return -1;
}
if (fprintf(fp, "%d\n", (int)pid) < 0) {
ksft_print_msg("Failed to write pid to tasks file\n");
fclose(fp);
return -1;
}
fclose(fp);
return 0;
}
int write_bm_pid_to_resctrl(pid_t bm_pid, const char *ctrlgrp, const char *mongrp)
{
char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
char tasks[1024];
int ret = 0;
if (ctrlgrp)
sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
else
sprintf(controlgroup, "%s", RESCTRL_PATH);
ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
if (ret)
goto out;
sprintf(tasks, "%s/tasks", controlgroup);
ret = write_pid_to_tasks(tasks, bm_pid);
if (ret)
goto out;
if (mongrp) {
sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
if (ret)
goto out;
sprintf(tasks, "%s/mon_groups/%s/tasks",
controlgroup, mongrp);
ret = write_pid_to_tasks(tasks, bm_pid);
if (ret)
goto out;
}
out:
ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
if (ret)
ksft_print_msg("Failed writing to resctrlfs\n");
return ret;
}
int write_schemata(const char *ctrlgrp, char *schemata, int cpu_no,
const char *resource)
{
char controlgroup[1024], reason[128], schema[1024] = {};
int domain_id, fd, schema_len, ret = 0;
if (!schemata) {
ksft_print_msg("Skipping empty schemata update\n");
return -1;
}
if (get_domain_id(resource, cpu_no, &domain_id) < 0) {
sprintf(reason, "Failed to get domain ID");
ret = -1;
goto out;
}
if (ctrlgrp)
sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
else
sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
schema_len = snprintf(schema, sizeof(schema), "%s:%d=%s\n",
resource, domain_id, schemata);
if (schema_len < 0 || schema_len >= sizeof(schema)) {
snprintf(reason, sizeof(reason),
"snprintf() failed with return value : %d", schema_len);
ret = -1;
goto out;
}
fd = open(controlgroup, O_WRONLY);
if (fd < 0) {
snprintf(reason, sizeof(reason),
"open() failed : %s", strerror(errno));
ret = -1;
goto err_schema_not_empty;
}
if (write(fd, schema, schema_len) < 0) {
snprintf(reason, sizeof(reason),
"write() failed : %s", strerror(errno));
close(fd);
ret = -1;
goto err_schema_not_empty;
}
close(fd);
err_schema_not_empty:
schema[schema_len - 1] = 0;
out:
ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
schema, ret ? " # " : "",
ret ? reason : "");
return ret;
}
bool check_resctrlfs_support(void)
{
FILE *inf = fopen("/proc/filesystems", "r");
DIR *dp;
char *res;
bool ret = false;
if (!inf)
return false;
res = fgrep(inf, "nodev\tresctrl\n");
if (res) {
ret = true;
free(res);
}
fclose(inf);
ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
ret ? "Pass:" : "Fail:");
if (!ret)
return ret;
dp = opendir(RESCTRL_PATH);
ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
dp ? "Pass:" : "Fail:", RESCTRL_PATH);
if (dp)
closedir(dp);
ksft_print_msg("resctrl filesystem %s mounted\n",
find_resctrl_mount(NULL) ? "not" : "is");
return ret;
}
char *fgrep(FILE *inf, const char *str)
{
char line[256];
int slen = strlen(str);
while (!feof(inf)) {
if (!fgets(line, 256, inf))
break;
if (strncmp(line, str, slen))
continue;
return strdup(line);
}
return NULL;
}
bool resctrl_resource_exists(const char *resource)
{
char res_path[PATH_MAX];
struct stat statbuf;
int ret;
if (!resource)
return false;
ret = find_resctrl_mount(NULL);
if (ret)
return false;
snprintf(res_path, sizeof(res_path), "%s/%s", INFO_PATH, resource);
if (stat(res_path, &statbuf))
return false;
return true;
}
bool resctrl_mon_feature_exists(const char *resource, const char *feature)
{
char res_path[PATH_MAX];
char *res;
FILE *inf;
if (!feature || !resource)
return false;
snprintf(res_path, sizeof(res_path), "%s/%s/mon_features", INFO_PATH, resource);
inf = fopen(res_path, "r");
if (!inf)
return false;
res = fgrep(inf, feature);
free(res);
fclose(inf);
return !!res;
}
bool resource_info_file_exists(const char *resource, const char *file)
{
char res_path[PATH_MAX];
struct stat statbuf;
if (!file || !resource)
return false;
snprintf(res_path, sizeof(res_path), "%s/%s/%s", INFO_PATH, resource,
file);
if (stat(res_path, &statbuf))
return false;
return true;
}
bool test_resource_feature_check(const struct resctrl_test *test)
{
return resctrl_resource_exists(test->resource);
}
int filter_dmesg(void)
{
char line[1024];
FILE *fp;
int pipefds[2];
pid_t pid;
int ret;
ret = pipe(pipefds);
if (ret) {
ksft_perror("pipe");
return ret;
}
fflush(stdout);
pid = fork();
if (pid == 0) {
close(pipefds[0]);
dup2(pipefds[1], STDOUT_FILENO);
execlp("dmesg", "dmesg", NULL);
ksft_perror("Executing dmesg");
exit(1);
}
close(pipefds[1]);
fp = fdopen(pipefds[0], "r");
if (!fp) {
ksft_perror("fdopen(pipe)");
kill(pid, SIGTERM);
return -1;
}
while (fgets(line, 1024, fp)) {
if (strstr(line, "intel_rdt:"))
ksft_print_msg("dmesg: %s", line);
if (strstr(line, "resctrl:"))
ksft_print_msg("dmesg: %s", line);
}
fclose(fp);
waitpid(pid, NULL, 0);
return 0;
}
int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
int group_fd, unsigned long flags)
{
int ret;
ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags);
return ret;
}
unsigned int count_bits(unsigned long n)
{
unsigned int count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
int snc_kernel_support(void)
{
char node_path[PATH_MAX];
struct stat statbuf;
int ret;
ret = snc_nodes_per_l3_cache();
if (ret == 1)
return ret;
snprintf(node_path, sizeof(node_path), "%s/%s", RESCTRL_PATH,
"mon_data/mon_L3_00/mon_sub_L3_00");
if (!stat(node_path, &statbuf))
return 1;
return 0;
}