// SPDX-License-Identifier: GPL-2.0
/*
* Basic resctrl file system operations
*
* Copyright (C) 2018 Intel Corporation
*
* Authors:
* Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
* Fenghua Yu <fenghua.yu@intel.com>
*/
#include <fcntl.h>
#include <limits.h>
#include "resctrl.h"
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;
}
/*
* mount_resctrlfs - Mount resctrl FS at /sys/fs/resctrl
*
* Mounts resctrl FS. Fails if resctrl FS is already mounted to avoid
* pre-existing settings interfering with the test results.
*
* Return: 0 on success, < 0 on error.
*/
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;
}
/*
* get_cache_level - Convert cache level from string to integer
* @cache_type: Cache level as string
*
* Return: cache level as integer or -1 if @cache_type is invalid.
*/
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)
{
/* "MB" use L3 (LLC) as resource */
if (!strcmp(resource, "MB"))
return 3;
return get_cache_level(resource);
}
/*
* get_domain_id - Get resctrl domain ID for a specified CPU
* @resource: resource name
* @cpu_no: CPU number
* @domain_id: domain ID (cache ID; for MB, L3 cache ID)
*
* Return: >= 0 on success, < 0 on failure.
*/
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;
}
/*
* get_cache_size - Get cache size for a specified CPU
* @cpu_no: CPU number
* @cache_type: Cache level L2/L3
* @cache_size: pointer to cache_size
*
* Return: = 0 on success, < 0 on failure.
*/
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, "%s", 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') &&