1591a6e85SSai Praneeth Prakhya // SPDX-License-Identifier: GPL-2.0 2591a6e85SSai Praneeth Prakhya /* 3591a6e85SSai Praneeth Prakhya * Basic resctrl file system operations 4591a6e85SSai Praneeth Prakhya * 5591a6e85SSai Praneeth Prakhya * Copyright (C) 2018 Intel Corporation 6591a6e85SSai Praneeth Prakhya * 7591a6e85SSai Praneeth Prakhya * Authors: 8591a6e85SSai Praneeth Prakhya * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>, 9591a6e85SSai Praneeth Prakhya * Fenghua Yu <fenghua.yu@intel.com> 10591a6e85SSai Praneeth Prakhya */ 11591a6e85SSai Praneeth Prakhya #include "resctrl.h" 12591a6e85SSai Praneeth Prakhya 13591a6e85SSai Praneeth Prakhya static int find_resctrl_mount(char *buffer) 14591a6e85SSai Praneeth Prakhya { 15591a6e85SSai Praneeth Prakhya FILE *mounts; 16591a6e85SSai Praneeth Prakhya char line[256], *fs, *mntpoint; 17591a6e85SSai Praneeth Prakhya 18591a6e85SSai Praneeth Prakhya mounts = fopen("/proc/mounts", "r"); 19591a6e85SSai Praneeth Prakhya if (!mounts) { 20591a6e85SSai Praneeth Prakhya perror("/proc/mounts"); 21591a6e85SSai Praneeth Prakhya return -ENXIO; 22591a6e85SSai Praneeth Prakhya } 23591a6e85SSai Praneeth Prakhya while (!feof(mounts)) { 24591a6e85SSai Praneeth Prakhya if (!fgets(line, 256, mounts)) 25591a6e85SSai Praneeth Prakhya break; 26591a6e85SSai Praneeth Prakhya fs = strtok(line, " \t"); 27591a6e85SSai Praneeth Prakhya if (!fs) 28591a6e85SSai Praneeth Prakhya continue; 29591a6e85SSai Praneeth Prakhya mntpoint = strtok(NULL, " \t"); 30591a6e85SSai Praneeth Prakhya if (!mntpoint) 31591a6e85SSai Praneeth Prakhya continue; 32591a6e85SSai Praneeth Prakhya fs = strtok(NULL, " \t"); 33591a6e85SSai Praneeth Prakhya if (!fs) 34591a6e85SSai Praneeth Prakhya continue; 35591a6e85SSai Praneeth Prakhya if (strcmp(fs, "resctrl")) 36591a6e85SSai Praneeth Prakhya continue; 37591a6e85SSai Praneeth Prakhya 38591a6e85SSai Praneeth Prakhya fclose(mounts); 39591a6e85SSai Praneeth Prakhya if (buffer) 40591a6e85SSai Praneeth Prakhya strncpy(buffer, mntpoint, 256); 41591a6e85SSai Praneeth Prakhya 42591a6e85SSai Praneeth Prakhya return 0; 43591a6e85SSai Praneeth Prakhya } 44591a6e85SSai Praneeth Prakhya 45591a6e85SSai Praneeth Prakhya fclose(mounts); 46591a6e85SSai Praneeth Prakhya 47591a6e85SSai Praneeth Prakhya return -ENOENT; 48591a6e85SSai Praneeth Prakhya } 49591a6e85SSai Praneeth Prakhya 50591a6e85SSai Praneeth Prakhya /* 51591a6e85SSai Praneeth Prakhya * remount_resctrlfs - Remount resctrl FS at /sys/fs/resctrl 52591a6e85SSai Praneeth Prakhya * @mum_resctrlfs: Should the resctrl FS be remounted? 53591a6e85SSai Praneeth Prakhya * 54591a6e85SSai Praneeth Prakhya * If not mounted, mount it. 55591a6e85SSai Praneeth Prakhya * If mounted and mum_resctrlfs then remount resctrl FS. 56591a6e85SSai Praneeth Prakhya * If mounted and !mum_resctrlfs then noop 57591a6e85SSai Praneeth Prakhya * 58591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 59591a6e85SSai Praneeth Prakhya */ 60591a6e85SSai Praneeth Prakhya int remount_resctrlfs(bool mum_resctrlfs) 61591a6e85SSai Praneeth Prakhya { 62591a6e85SSai Praneeth Prakhya char mountpoint[256]; 63591a6e85SSai Praneeth Prakhya int ret; 64591a6e85SSai Praneeth Prakhya 65591a6e85SSai Praneeth Prakhya ret = find_resctrl_mount(mountpoint); 66591a6e85SSai Praneeth Prakhya if (ret) 67591a6e85SSai Praneeth Prakhya strcpy(mountpoint, RESCTRL_PATH); 68591a6e85SSai Praneeth Prakhya 69ca2f4214SFenghua Yu if (!ret && mum_resctrlfs && umount(mountpoint)) 70ca2f4214SFenghua Yu ksft_print_msg("Fail: unmounting \"%s\"\n", mountpoint); 71591a6e85SSai Praneeth Prakhya 72591a6e85SSai Praneeth Prakhya if (!ret && !mum_resctrlfs) 73591a6e85SSai Praneeth Prakhya return 0; 74591a6e85SSai Praneeth Prakhya 75ca2f4214SFenghua Yu ksft_print_msg("Mounting resctrl to \"%s\"\n", RESCTRL_PATH); 76591a6e85SSai Praneeth Prakhya ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL); 77591a6e85SSai Praneeth Prakhya if (ret) 78591a6e85SSai Praneeth Prakhya perror("# mount"); 79591a6e85SSai Praneeth Prakhya 80591a6e85SSai Praneeth Prakhya return ret; 81591a6e85SSai Praneeth Prakhya } 82591a6e85SSai Praneeth Prakhya 83591a6e85SSai Praneeth Prakhya int umount_resctrlfs(void) 84591a6e85SSai Praneeth Prakhya { 85591a6e85SSai Praneeth Prakhya if (umount(RESCTRL_PATH)) { 86591a6e85SSai Praneeth Prakhya perror("# Unable to umount resctrl"); 87591a6e85SSai Praneeth Prakhya 88591a6e85SSai Praneeth Prakhya return errno; 89591a6e85SSai Praneeth Prakhya } 90591a6e85SSai Praneeth Prakhya 91591a6e85SSai Praneeth Prakhya return 0; 92591a6e85SSai Praneeth Prakhya } 93591a6e85SSai Praneeth Prakhya 94591a6e85SSai Praneeth Prakhya /* 95591a6e85SSai Praneeth Prakhya * get_resource_id - Get socket number/l3 id for a specified CPU 96591a6e85SSai Praneeth Prakhya * @cpu_no: CPU number 97591a6e85SSai Praneeth Prakhya * @resource_id: Socket number or l3_id 98591a6e85SSai Praneeth Prakhya * 99591a6e85SSai Praneeth Prakhya * Return: >= 0 on success, < 0 on failure. 100591a6e85SSai Praneeth Prakhya */ 101591a6e85SSai Praneeth Prakhya int get_resource_id(int cpu_no, int *resource_id) 102591a6e85SSai Praneeth Prakhya { 103591a6e85SSai Praneeth Prakhya char phys_pkg_path[1024]; 104591a6e85SSai Praneeth Prakhya FILE *fp; 105591a6e85SSai Praneeth Prakhya 106c0327e1dSBabu Moger if (is_amd) 107c0327e1dSBabu Moger sprintf(phys_pkg_path, "%s%d/cache/index3/id", 108c0327e1dSBabu Moger PHYS_ID_PATH, cpu_no); 109c0327e1dSBabu Moger else 110591a6e85SSai Praneeth Prakhya sprintf(phys_pkg_path, "%s%d/topology/physical_package_id", 111591a6e85SSai Praneeth Prakhya PHYS_ID_PATH, cpu_no); 112c0327e1dSBabu Moger 113591a6e85SSai Praneeth Prakhya fp = fopen(phys_pkg_path, "r"); 114591a6e85SSai Praneeth Prakhya if (!fp) { 115591a6e85SSai Praneeth Prakhya perror("Failed to open physical_package_id"); 116591a6e85SSai Praneeth Prakhya 117591a6e85SSai Praneeth Prakhya return -1; 118591a6e85SSai Praneeth Prakhya } 119591a6e85SSai Praneeth Prakhya if (fscanf(fp, "%d", resource_id) <= 0) { 120591a6e85SSai Praneeth Prakhya perror("Could not get socket number or l3 id"); 121591a6e85SSai Praneeth Prakhya fclose(fp); 122591a6e85SSai Praneeth Prakhya 123591a6e85SSai Praneeth Prakhya return -1; 124591a6e85SSai Praneeth Prakhya } 125591a6e85SSai Praneeth Prakhya fclose(fp); 126591a6e85SSai Praneeth Prakhya 127591a6e85SSai Praneeth Prakhya return 0; 128591a6e85SSai Praneeth Prakhya } 129591a6e85SSai Praneeth Prakhya 130591a6e85SSai Praneeth Prakhya /* 13178941183SFenghua Yu * get_cache_size - Get cache size for a specified CPU 13278941183SFenghua Yu * @cpu_no: CPU number 13378941183SFenghua Yu * @cache_type: Cache level L2/L3 13478941183SFenghua Yu * @cache_size: pointer to cache_size 13578941183SFenghua Yu * 13678941183SFenghua Yu * Return: = 0 on success, < 0 on failure. 13778941183SFenghua Yu */ 13878941183SFenghua Yu int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size) 13978941183SFenghua Yu { 14078941183SFenghua Yu char cache_path[1024], cache_str[64]; 14178941183SFenghua Yu int length, i, cache_num; 14278941183SFenghua Yu FILE *fp; 14378941183SFenghua Yu 14478941183SFenghua Yu if (!strcmp(cache_type, "L3")) { 14578941183SFenghua Yu cache_num = 3; 14678941183SFenghua Yu } else if (!strcmp(cache_type, "L2")) { 14778941183SFenghua Yu cache_num = 2; 14878941183SFenghua Yu } else { 14978941183SFenghua Yu perror("Invalid cache level"); 15078941183SFenghua Yu return -1; 15178941183SFenghua Yu } 15278941183SFenghua Yu 15378941183SFenghua Yu sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size", 15478941183SFenghua Yu cpu_no, cache_num); 15578941183SFenghua Yu fp = fopen(cache_path, "r"); 15678941183SFenghua Yu if (!fp) { 15778941183SFenghua Yu perror("Failed to open cache size"); 15878941183SFenghua Yu 15978941183SFenghua Yu return -1; 16078941183SFenghua Yu } 16178941183SFenghua Yu if (fscanf(fp, "%s", cache_str) <= 0) { 16278941183SFenghua Yu perror("Could not get cache_size"); 16378941183SFenghua Yu fclose(fp); 16478941183SFenghua Yu 16578941183SFenghua Yu return -1; 16678941183SFenghua Yu } 16778941183SFenghua Yu fclose(fp); 16878941183SFenghua Yu 16978941183SFenghua Yu length = (int)strlen(cache_str); 17078941183SFenghua Yu 17178941183SFenghua Yu *cache_size = 0; 17278941183SFenghua Yu 17378941183SFenghua Yu for (i = 0; i < length; i++) { 17478941183SFenghua Yu if ((cache_str[i] >= '0') && (cache_str[i] <= '9')) 17578941183SFenghua Yu 17678941183SFenghua Yu *cache_size = *cache_size * 10 + (cache_str[i] - '0'); 17778941183SFenghua Yu 17878941183SFenghua Yu else if (cache_str[i] == 'K') 17978941183SFenghua Yu 18078941183SFenghua Yu *cache_size = *cache_size * 1024; 18178941183SFenghua Yu 18278941183SFenghua Yu else if (cache_str[i] == 'M') 18378941183SFenghua Yu 18478941183SFenghua Yu *cache_size = *cache_size * 1024 * 1024; 18578941183SFenghua Yu 18678941183SFenghua Yu else 18778941183SFenghua Yu break; 18878941183SFenghua Yu } 18978941183SFenghua Yu 19078941183SFenghua Yu return 0; 19178941183SFenghua Yu } 19278941183SFenghua Yu 19378941183SFenghua Yu #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu" 19478941183SFenghua Yu 19578941183SFenghua Yu /* 19678941183SFenghua Yu * get_cbm_mask - Get cbm mask for given cache 19778941183SFenghua Yu * @cache_type: Cache level L2/L3 1988236c51dSFenghua Yu * @cbm_mask: cbm_mask returned as a string 19978941183SFenghua Yu * 20078941183SFenghua Yu * Return: = 0 on success, < 0 on failure. 20178941183SFenghua Yu */ 2028236c51dSFenghua Yu int get_cbm_mask(char *cache_type, char *cbm_mask) 20378941183SFenghua Yu { 20478941183SFenghua Yu char cbm_mask_path[1024]; 20578941183SFenghua Yu FILE *fp; 20678941183SFenghua Yu 2078236c51dSFenghua Yu if (!cbm_mask) 2088236c51dSFenghua Yu return -1; 2098236c51dSFenghua Yu 21078941183SFenghua Yu sprintf(cbm_mask_path, "%s/%s/cbm_mask", CBM_MASK_PATH, cache_type); 21178941183SFenghua Yu 21278941183SFenghua Yu fp = fopen(cbm_mask_path, "r"); 21378941183SFenghua Yu if (!fp) { 21478941183SFenghua Yu perror("Failed to open cache level"); 21578941183SFenghua Yu 21678941183SFenghua Yu return -1; 21778941183SFenghua Yu } 21878941183SFenghua Yu if (fscanf(fp, "%s", cbm_mask) <= 0) { 21978941183SFenghua Yu perror("Could not get max cbm_mask"); 22078941183SFenghua Yu fclose(fp); 22178941183SFenghua Yu 22278941183SFenghua Yu return -1; 22378941183SFenghua Yu } 22478941183SFenghua Yu fclose(fp); 22578941183SFenghua Yu 22678941183SFenghua Yu return 0; 22778941183SFenghua Yu } 22878941183SFenghua Yu 22978941183SFenghua Yu /* 23078941183SFenghua Yu * get_core_sibling - Get sibling core id from the same socket for given CPU 23178941183SFenghua Yu * @cpu_no: CPU number 23278941183SFenghua Yu * 23378941183SFenghua Yu * Return: > 0 on success, < 0 on failure. 23478941183SFenghua Yu */ 23578941183SFenghua Yu int get_core_sibling(int cpu_no) 23678941183SFenghua Yu { 23778941183SFenghua Yu char core_siblings_path[1024], cpu_list_str[64]; 23878941183SFenghua Yu int sibling_cpu_no = -1; 23978941183SFenghua Yu FILE *fp; 24078941183SFenghua Yu 24178941183SFenghua Yu sprintf(core_siblings_path, "%s%d/topology/core_siblings_list", 24278941183SFenghua Yu CORE_SIBLINGS_PATH, cpu_no); 24378941183SFenghua Yu 24478941183SFenghua Yu fp = fopen(core_siblings_path, "r"); 24578941183SFenghua Yu if (!fp) { 24678941183SFenghua Yu perror("Failed to open core siblings path"); 24778941183SFenghua Yu 24878941183SFenghua Yu return -1; 24978941183SFenghua Yu } 25078941183SFenghua Yu if (fscanf(fp, "%s", cpu_list_str) <= 0) { 25178941183SFenghua Yu perror("Could not get core_siblings list"); 25278941183SFenghua Yu fclose(fp); 25378941183SFenghua Yu 25478941183SFenghua Yu return -1; 25578941183SFenghua Yu } 25678941183SFenghua Yu fclose(fp); 25778941183SFenghua Yu 25878941183SFenghua Yu char *token = strtok(cpu_list_str, "-,"); 25978941183SFenghua Yu 26078941183SFenghua Yu while (token) { 26178941183SFenghua Yu sibling_cpu_no = atoi(token); 26278941183SFenghua Yu /* Skipping core 0 as we don't want to run test on core 0 */ 263f5f16ae4SReinette Chatre if (sibling_cpu_no != 0 && sibling_cpu_no != cpu_no) 26478941183SFenghua Yu break; 26578941183SFenghua Yu token = strtok(NULL, "-,"); 26678941183SFenghua Yu } 26778941183SFenghua Yu 26878941183SFenghua Yu return sibling_cpu_no; 26978941183SFenghua Yu } 27078941183SFenghua Yu 27178941183SFenghua Yu /* 272591a6e85SSai Praneeth Prakhya * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu 273591a6e85SSai Praneeth Prakhya * @bm_pid: PID that should be binded 274591a6e85SSai Praneeth Prakhya * @cpu_no: CPU number at which the PID would be binded 275591a6e85SSai Praneeth Prakhya * 276591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 277591a6e85SSai Praneeth Prakhya */ 278591a6e85SSai Praneeth Prakhya int taskset_benchmark(pid_t bm_pid, int cpu_no) 279591a6e85SSai Praneeth Prakhya { 280591a6e85SSai Praneeth Prakhya cpu_set_t my_set; 281591a6e85SSai Praneeth Prakhya 282591a6e85SSai Praneeth Prakhya CPU_ZERO(&my_set); 283591a6e85SSai Praneeth Prakhya CPU_SET(cpu_no, &my_set); 284591a6e85SSai Praneeth Prakhya 285591a6e85SSai Praneeth Prakhya if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) { 286591a6e85SSai Praneeth Prakhya perror("Unable to taskset benchmark"); 287591a6e85SSai Praneeth Prakhya 288591a6e85SSai Praneeth Prakhya return -1; 289591a6e85SSai Praneeth Prakhya } 290591a6e85SSai Praneeth Prakhya 291591a6e85SSai Praneeth Prakhya return 0; 292591a6e85SSai Praneeth Prakhya } 293591a6e85SSai Praneeth Prakhya 294591a6e85SSai Praneeth Prakhya /* 295591a6e85SSai Praneeth Prakhya * run_benchmark - Run a specified benchmark or fill_buf (default benchmark) 296591a6e85SSai Praneeth Prakhya * in specified signal. Direct benchmark stdio to /dev/null. 297591a6e85SSai Praneeth Prakhya * @signum: signal number 298591a6e85SSai Praneeth Prakhya * @info: signal info 299591a6e85SSai Praneeth Prakhya * @ucontext: user context in signal handling 300591a6e85SSai Praneeth Prakhya * 301591a6e85SSai Praneeth Prakhya * Return: void 302591a6e85SSai Praneeth Prakhya */ 303591a6e85SSai Praneeth Prakhya void run_benchmark(int signum, siginfo_t *info, void *ucontext) 304591a6e85SSai Praneeth Prakhya { 30578941183SFenghua Yu int operation, ret, malloc_and_init_memory, memflush; 30678941183SFenghua Yu unsigned long span, buffer_span; 307591a6e85SSai Praneeth Prakhya char **benchmark_cmd; 30878941183SFenghua Yu char resctrl_val[64]; 309591a6e85SSai Praneeth Prakhya FILE *fp; 310591a6e85SSai Praneeth Prakhya 311591a6e85SSai Praneeth Prakhya benchmark_cmd = info->si_ptr; 312591a6e85SSai Praneeth Prakhya 313591a6e85SSai Praneeth Prakhya /* 314591a6e85SSai Praneeth Prakhya * Direct stdio of child to /dev/null, so that only parent writes to 315591a6e85SSai Praneeth Prakhya * stdio (console) 316591a6e85SSai Praneeth Prakhya */ 317591a6e85SSai Praneeth Prakhya fp = freopen("/dev/null", "w", stdout); 318591a6e85SSai Praneeth Prakhya if (!fp) 319591a6e85SSai Praneeth Prakhya PARENT_EXIT("Unable to direct benchmark status to /dev/null"); 320591a6e85SSai Praneeth Prakhya 321591a6e85SSai Praneeth Prakhya if (strcmp(benchmark_cmd[0], "fill_buf") == 0) { 322591a6e85SSai Praneeth Prakhya /* Execute default fill_buf benchmark */ 323591a6e85SSai Praneeth Prakhya span = strtoul(benchmark_cmd[1], NULL, 10); 32478941183SFenghua Yu malloc_and_init_memory = atoi(benchmark_cmd[2]); 32578941183SFenghua Yu memflush = atoi(benchmark_cmd[3]); 326591a6e85SSai Praneeth Prakhya operation = atoi(benchmark_cmd[4]); 32778941183SFenghua Yu sprintf(resctrl_val, "%s", benchmark_cmd[5]); 32878941183SFenghua Yu 3292f320911SFenghua Yu if (strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) 33078941183SFenghua Yu buffer_span = span * MB; 33178941183SFenghua Yu else 33278941183SFenghua Yu buffer_span = span; 33378941183SFenghua Yu 33478941183SFenghua Yu if (run_fill_buf(buffer_span, malloc_and_init_memory, memflush, 33578941183SFenghua Yu operation, resctrl_val)) 336591a6e85SSai Praneeth Prakhya fprintf(stderr, "Error in running fill buffer\n"); 337591a6e85SSai Praneeth Prakhya } else { 338591a6e85SSai Praneeth Prakhya /* Execute specified benchmark */ 339591a6e85SSai Praneeth Prakhya ret = execvp(benchmark_cmd[0], benchmark_cmd); 340591a6e85SSai Praneeth Prakhya if (ret) 341591a6e85SSai Praneeth Prakhya perror("wrong\n"); 342591a6e85SSai Praneeth Prakhya } 343591a6e85SSai Praneeth Prakhya 344591a6e85SSai Praneeth Prakhya fclose(stdout); 345591a6e85SSai Praneeth Prakhya PARENT_EXIT("Unable to run specified benchmark"); 346591a6e85SSai Praneeth Prakhya } 347591a6e85SSai Praneeth Prakhya 348591a6e85SSai Praneeth Prakhya /* 349591a6e85SSai Praneeth Prakhya * create_grp - Create a group only if one doesn't exist 350591a6e85SSai Praneeth Prakhya * @grp_name: Name of the group 351591a6e85SSai Praneeth Prakhya * @grp: Full path and name of the group 352591a6e85SSai Praneeth Prakhya * @parent_grp: Full path and name of the parent group 353591a6e85SSai Praneeth Prakhya * 354591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 355591a6e85SSai Praneeth Prakhya */ 356591a6e85SSai Praneeth Prakhya static int create_grp(const char *grp_name, char *grp, const char *parent_grp) 357591a6e85SSai Praneeth Prakhya { 358591a6e85SSai Praneeth Prakhya int found_grp = 0; 359591a6e85SSai Praneeth Prakhya struct dirent *ep; 360591a6e85SSai Praneeth Prakhya DIR *dp; 361591a6e85SSai Praneeth Prakhya 36278941183SFenghua Yu /* 36378941183SFenghua Yu * At this point, we are guaranteed to have resctrl FS mounted and if 36478941183SFenghua Yu * length of grp_name == 0, it means, user wants to use root con_mon 36578941183SFenghua Yu * grp, so do nothing 36678941183SFenghua Yu */ 36778941183SFenghua Yu if (strlen(grp_name) == 0) 36878941183SFenghua Yu return 0; 36978941183SFenghua Yu 370591a6e85SSai Praneeth Prakhya /* Check if requested grp exists or not */ 371591a6e85SSai Praneeth Prakhya dp = opendir(parent_grp); 372591a6e85SSai Praneeth Prakhya if (dp) { 373591a6e85SSai Praneeth Prakhya while ((ep = readdir(dp)) != NULL) { 374591a6e85SSai Praneeth Prakhya if (strcmp(ep->d_name, grp_name) == 0) 375591a6e85SSai Praneeth Prakhya found_grp = 1; 376591a6e85SSai Praneeth Prakhya } 377591a6e85SSai Praneeth Prakhya closedir(dp); 378591a6e85SSai Praneeth Prakhya } else { 379591a6e85SSai Praneeth Prakhya perror("Unable to open resctrl for group"); 380591a6e85SSai Praneeth Prakhya 381591a6e85SSai Praneeth Prakhya return -1; 382591a6e85SSai Praneeth Prakhya } 383591a6e85SSai Praneeth Prakhya 384591a6e85SSai Praneeth Prakhya /* Requested grp doesn't exist, hence create it */ 385591a6e85SSai Praneeth Prakhya if (found_grp == 0) { 386591a6e85SSai Praneeth Prakhya if (mkdir(grp, 0) == -1) { 387591a6e85SSai Praneeth Prakhya perror("Unable to create group"); 388591a6e85SSai Praneeth Prakhya 389591a6e85SSai Praneeth Prakhya return -1; 390591a6e85SSai Praneeth Prakhya } 391591a6e85SSai Praneeth Prakhya } 392591a6e85SSai Praneeth Prakhya 393591a6e85SSai Praneeth Prakhya return 0; 394591a6e85SSai Praneeth Prakhya } 395591a6e85SSai Praneeth Prakhya 396591a6e85SSai Praneeth Prakhya static int write_pid_to_tasks(char *tasks, pid_t pid) 397591a6e85SSai Praneeth Prakhya { 398591a6e85SSai Praneeth Prakhya FILE *fp; 399591a6e85SSai Praneeth Prakhya 400591a6e85SSai Praneeth Prakhya fp = fopen(tasks, "w"); 401591a6e85SSai Praneeth Prakhya if (!fp) { 402591a6e85SSai Praneeth Prakhya perror("Failed to open tasks file"); 403591a6e85SSai Praneeth Prakhya 404591a6e85SSai Praneeth Prakhya return -1; 405591a6e85SSai Praneeth Prakhya } 406591a6e85SSai Praneeth Prakhya if (fprintf(fp, "%d\n", pid) < 0) { 407591a6e85SSai Praneeth Prakhya perror("Failed to wr pid to tasks file"); 408591a6e85SSai Praneeth Prakhya fclose(fp); 409591a6e85SSai Praneeth Prakhya 410591a6e85SSai Praneeth Prakhya return -1; 411591a6e85SSai Praneeth Prakhya } 412591a6e85SSai Praneeth Prakhya fclose(fp); 413591a6e85SSai Praneeth Prakhya 414591a6e85SSai Praneeth Prakhya return 0; 415591a6e85SSai Praneeth Prakhya } 416591a6e85SSai Praneeth Prakhya 417591a6e85SSai Praneeth Prakhya /* 418591a6e85SSai Praneeth Prakhya * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS 419591a6e85SSai Praneeth Prakhya * @bm_pid: PID that should be written 420591a6e85SSai Praneeth Prakhya * @ctrlgrp: Name of the control monitor group (con_mon grp) 421591a6e85SSai Praneeth Prakhya * @mongrp: Name of the monitor group (mon grp) 422591a6e85SSai Praneeth Prakhya * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc) 423591a6e85SSai Praneeth Prakhya * 424591a6e85SSai Praneeth Prakhya * If a con_mon grp is requested, create it and write pid to it, otherwise 425591a6e85SSai Praneeth Prakhya * write pid to root con_mon grp. 426591a6e85SSai Praneeth Prakhya * If a mon grp is requested, create it and write pid to it, otherwise 427591a6e85SSai Praneeth Prakhya * pid is not written, this means that pid is in con_mon grp and hence 428591a6e85SSai Praneeth Prakhya * should consult con_mon grp's mon_data directory for results. 429591a6e85SSai Praneeth Prakhya * 430591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 431591a6e85SSai Praneeth Prakhya */ 432591a6e85SSai Praneeth Prakhya int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp, 433591a6e85SSai Praneeth Prakhya char *resctrl_val) 434591a6e85SSai Praneeth Prakhya { 435591a6e85SSai Praneeth Prakhya char controlgroup[128], monitorgroup[512], monitorgroup_p[256]; 436591a6e85SSai Praneeth Prakhya char tasks[1024]; 437591a6e85SSai Praneeth Prakhya int ret = 0; 438591a6e85SSai Praneeth Prakhya 439591a6e85SSai Praneeth Prakhya if (strlen(ctrlgrp)) 440591a6e85SSai Praneeth Prakhya sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp); 441591a6e85SSai Praneeth Prakhya else 442591a6e85SSai Praneeth Prakhya sprintf(controlgroup, "%s", RESCTRL_PATH); 443591a6e85SSai Praneeth Prakhya 444591a6e85SSai Praneeth Prakhya /* Create control and monitoring group and write pid into it */ 445591a6e85SSai Praneeth Prakhya ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH); 446591a6e85SSai Praneeth Prakhya if (ret) 447591a6e85SSai Praneeth Prakhya goto out; 448591a6e85SSai Praneeth Prakhya sprintf(tasks, "%s/tasks", controlgroup); 449591a6e85SSai Praneeth Prakhya ret = write_pid_to_tasks(tasks, bm_pid); 450591a6e85SSai Praneeth Prakhya if (ret) 451591a6e85SSai Praneeth Prakhya goto out; 452591a6e85SSai Praneeth Prakhya 4532f320911SFenghua Yu /* Create mon grp and write pid into it for "mbm" and "cmt" test */ 4542f320911SFenghua Yu if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)) || 45524286736SFenghua Yu !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) { 45678941183SFenghua Yu if (strlen(mongrp)) { 457591a6e85SSai Praneeth Prakhya sprintf(monitorgroup_p, "%s/mon_groups", controlgroup); 458591a6e85SSai Praneeth Prakhya sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp); 459591a6e85SSai Praneeth Prakhya ret = create_grp(mongrp, monitorgroup, monitorgroup_p); 460591a6e85SSai Praneeth Prakhya if (ret) 461591a6e85SSai Praneeth Prakhya goto out; 462591a6e85SSai Praneeth Prakhya 463591a6e85SSai Praneeth Prakhya sprintf(tasks, "%s/mon_groups/%s/tasks", 464591a6e85SSai Praneeth Prakhya controlgroup, mongrp); 465591a6e85SSai Praneeth Prakhya ret = write_pid_to_tasks(tasks, bm_pid); 466591a6e85SSai Praneeth Prakhya if (ret) 467591a6e85SSai Praneeth Prakhya goto out; 468591a6e85SSai Praneeth Prakhya } 469591a6e85SSai Praneeth Prakhya } 470591a6e85SSai Praneeth Prakhya 471591a6e85SSai Praneeth Prakhya out: 472ca2f4214SFenghua Yu ksft_print_msg("Writing benchmark parameters to resctrl FS\n"); 473591a6e85SSai Praneeth Prakhya if (ret) 474591a6e85SSai Praneeth Prakhya perror("# writing to resctrlfs"); 475591a6e85SSai Praneeth Prakhya 476591a6e85SSai Praneeth Prakhya return ret; 477591a6e85SSai Praneeth Prakhya } 478591a6e85SSai Praneeth Prakhya 479591a6e85SSai Praneeth Prakhya /* 480591a6e85SSai Praneeth Prakhya * write_schemata - Update schemata of a con_mon grp 481591a6e85SSai Praneeth Prakhya * @ctrlgrp: Name of the con_mon grp 482591a6e85SSai Praneeth Prakhya * @schemata: Schemata that should be updated to 483591a6e85SSai Praneeth Prakhya * @cpu_no: CPU number that the benchmark PID is binded to 484591a6e85SSai Praneeth Prakhya * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc) 485591a6e85SSai Praneeth Prakhya * 486591a6e85SSai Praneeth Prakhya * Update schemata of a con_mon grp *only* if requested resctrl feature is 487591a6e85SSai Praneeth Prakhya * allocation type 488591a6e85SSai Praneeth Prakhya * 489591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 490591a6e85SSai Praneeth Prakhya */ 491591a6e85SSai Praneeth Prakhya int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val) 492591a6e85SSai Praneeth Prakhya { 493591a6e85SSai Praneeth Prakhya char controlgroup[1024], schema[1024], reason[64]; 494591a6e85SSai Praneeth Prakhya int resource_id, ret = 0; 495591a6e85SSai Praneeth Prakhya FILE *fp; 496591a6e85SSai Praneeth Prakhya 49724286736SFenghua Yu if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) && 49824286736SFenghua Yu strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) && 4992f320911SFenghua Yu strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) 500591a6e85SSai Praneeth Prakhya return -ENOENT; 501591a6e85SSai Praneeth Prakhya 502591a6e85SSai Praneeth Prakhya if (!schemata) { 503ca2f4214SFenghua Yu ksft_print_msg("Skipping empty schemata update\n"); 504591a6e85SSai Praneeth Prakhya 505591a6e85SSai Praneeth Prakhya return -1; 506591a6e85SSai Praneeth Prakhya } 507591a6e85SSai Praneeth Prakhya 508591a6e85SSai Praneeth Prakhya if (get_resource_id(cpu_no, &resource_id) < 0) { 509591a6e85SSai Praneeth Prakhya sprintf(reason, "Failed to get resource id"); 510591a6e85SSai Praneeth Prakhya ret = -1; 511591a6e85SSai Praneeth Prakhya 512591a6e85SSai Praneeth Prakhya goto out; 513591a6e85SSai Praneeth Prakhya } 514591a6e85SSai Praneeth Prakhya 515591a6e85SSai Praneeth Prakhya if (strlen(ctrlgrp) != 0) 516591a6e85SSai Praneeth Prakhya sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp); 517591a6e85SSai Praneeth Prakhya else 518591a6e85SSai Praneeth Prakhya sprintf(controlgroup, "%s/schemata", RESCTRL_PATH); 519591a6e85SSai Praneeth Prakhya 52024286736SFenghua Yu if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) || 5212f320911SFenghua Yu !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) 52278941183SFenghua Yu sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata); 52324286736SFenghua Yu if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) 524591a6e85SSai Praneeth Prakhya sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata); 525591a6e85SSai Praneeth Prakhya 526591a6e85SSai Praneeth Prakhya fp = fopen(controlgroup, "w"); 527591a6e85SSai Praneeth Prakhya if (!fp) { 528591a6e85SSai Praneeth Prakhya sprintf(reason, "Failed to open control group"); 529591a6e85SSai Praneeth Prakhya ret = -1; 530591a6e85SSai Praneeth Prakhya 531591a6e85SSai Praneeth Prakhya goto out; 532591a6e85SSai Praneeth Prakhya } 533591a6e85SSai Praneeth Prakhya 534591a6e85SSai Praneeth Prakhya if (fprintf(fp, "%s\n", schema) < 0) { 535591a6e85SSai Praneeth Prakhya sprintf(reason, "Failed to write schemata in control group"); 536591a6e85SSai Praneeth Prakhya fclose(fp); 537591a6e85SSai Praneeth Prakhya ret = -1; 538591a6e85SSai Praneeth Prakhya 539591a6e85SSai Praneeth Prakhya goto out; 540591a6e85SSai Praneeth Prakhya } 541591a6e85SSai Praneeth Prakhya fclose(fp); 542591a6e85SSai Praneeth Prakhya 543591a6e85SSai Praneeth Prakhya out: 544ca2f4214SFenghua Yu ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n", 545ca2f4214SFenghua Yu schema, ret ? " # " : "", 546591a6e85SSai Praneeth Prakhya ret ? reason : ""); 547591a6e85SSai Praneeth Prakhya 548591a6e85SSai Praneeth Prakhya return ret; 549591a6e85SSai Praneeth Prakhya } 550591a6e85SSai Praneeth Prakhya 551ecdbb911SFenghua Yu bool check_resctrlfs_support(void) 552ecdbb911SFenghua Yu { 553ecdbb911SFenghua Yu FILE *inf = fopen("/proc/filesystems", "r"); 554ecdbb911SFenghua Yu DIR *dp; 555ecdbb911SFenghua Yu char *res; 556ecdbb911SFenghua Yu bool ret = false; 557ecdbb911SFenghua Yu 558ecdbb911SFenghua Yu if (!inf) 559ecdbb911SFenghua Yu return false; 560ecdbb911SFenghua Yu 561ecdbb911SFenghua Yu res = fgrep(inf, "nodev\tresctrl\n"); 562ecdbb911SFenghua Yu 563ecdbb911SFenghua Yu if (res) { 564ecdbb911SFenghua Yu ret = true; 565ecdbb911SFenghua Yu free(res); 566ecdbb911SFenghua Yu } 567ecdbb911SFenghua Yu 568ecdbb911SFenghua Yu fclose(inf); 569ecdbb911SFenghua Yu 570ca2f4214SFenghua Yu ksft_print_msg("%s kernel supports resctrl filesystem\n", 571ca2f4214SFenghua Yu ret ? "Pass:" : "Fail:"); 572ecdbb911SFenghua Yu 573*a3611fbcSFenghua Yu if (!ret) 574*a3611fbcSFenghua Yu return ret; 575*a3611fbcSFenghua Yu 576ecdbb911SFenghua Yu dp = opendir(RESCTRL_PATH); 577ca2f4214SFenghua Yu ksft_print_msg("%s resctrl mountpoint \"%s\" exists\n", 578ca2f4214SFenghua Yu dp ? "Pass:" : "Fail:", RESCTRL_PATH); 579ecdbb911SFenghua Yu if (dp) 580ecdbb911SFenghua Yu closedir(dp); 581ecdbb911SFenghua Yu 582ca2f4214SFenghua Yu ksft_print_msg("resctrl filesystem %s mounted\n", 583ecdbb911SFenghua Yu find_resctrl_mount(NULL) ? "not" : "is"); 584ecdbb911SFenghua Yu 585ecdbb911SFenghua Yu return ret; 586ecdbb911SFenghua Yu } 587ecdbb911SFenghua Yu 588591a6e85SSai Praneeth Prakhya char *fgrep(FILE *inf, const char *str) 589591a6e85SSai Praneeth Prakhya { 590591a6e85SSai Praneeth Prakhya char line[256]; 591591a6e85SSai Praneeth Prakhya int slen = strlen(str); 592591a6e85SSai Praneeth Prakhya 593591a6e85SSai Praneeth Prakhya while (!feof(inf)) { 594591a6e85SSai Praneeth Prakhya if (!fgets(line, 256, inf)) 595591a6e85SSai Praneeth Prakhya break; 596591a6e85SSai Praneeth Prakhya if (strncmp(line, str, slen)) 597591a6e85SSai Praneeth Prakhya continue; 598591a6e85SSai Praneeth Prakhya 599591a6e85SSai Praneeth Prakhya return strdup(line); 600591a6e85SSai Praneeth Prakhya } 601591a6e85SSai Praneeth Prakhya 602591a6e85SSai Praneeth Prakhya return NULL; 603591a6e85SSai Praneeth Prakhya } 604591a6e85SSai Praneeth Prakhya 605591a6e85SSai Praneeth Prakhya /* 606591a6e85SSai Praneeth Prakhya * validate_resctrl_feature_request - Check if requested feature is valid. 607591a6e85SSai Praneeth Prakhya * @resctrl_val: Requested feature 608591a6e85SSai Praneeth Prakhya * 609591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 610591a6e85SSai Praneeth Prakhya */ 611591a6e85SSai Praneeth Prakhya bool validate_resctrl_feature_request(char *resctrl_val) 612591a6e85SSai Praneeth Prakhya { 613591a6e85SSai Praneeth Prakhya FILE *inf = fopen("/proc/cpuinfo", "r"); 614591a6e85SSai Praneeth Prakhya bool found = false; 615591a6e85SSai Praneeth Prakhya char *res; 616591a6e85SSai Praneeth Prakhya 617591a6e85SSai Praneeth Prakhya if (!inf) 618591a6e85SSai Praneeth Prakhya return false; 619591a6e85SSai Praneeth Prakhya 620591a6e85SSai Praneeth Prakhya res = fgrep(inf, "flags"); 621591a6e85SSai Praneeth Prakhya 622591a6e85SSai Praneeth Prakhya if (res) { 623591a6e85SSai Praneeth Prakhya char *s = strchr(res, ':'); 624591a6e85SSai Praneeth Prakhya 625591a6e85SSai Praneeth Prakhya found = s && !strstr(s, resctrl_val); 626591a6e85SSai Praneeth Prakhya free(res); 627591a6e85SSai Praneeth Prakhya } 628591a6e85SSai Praneeth Prakhya fclose(inf); 629591a6e85SSai Praneeth Prakhya 630591a6e85SSai Praneeth Prakhya return found; 631591a6e85SSai Praneeth Prakhya } 632591a6e85SSai Praneeth Prakhya 633ecdbb911SFenghua Yu int filter_dmesg(void) 634ecdbb911SFenghua Yu { 635ecdbb911SFenghua Yu char line[1024]; 636ecdbb911SFenghua Yu FILE *fp; 637ecdbb911SFenghua Yu int pipefds[2]; 638ecdbb911SFenghua Yu pid_t pid; 639ecdbb911SFenghua Yu int ret; 640ecdbb911SFenghua Yu 641ecdbb911SFenghua Yu ret = pipe(pipefds); 642ecdbb911SFenghua Yu if (ret) { 643ecdbb911SFenghua Yu perror("pipe"); 644ecdbb911SFenghua Yu return ret; 645ecdbb911SFenghua Yu } 646ecdbb911SFenghua Yu pid = fork(); 647ecdbb911SFenghua Yu if (pid == 0) { 648ecdbb911SFenghua Yu close(pipefds[0]); 649ecdbb911SFenghua Yu dup2(pipefds[1], STDOUT_FILENO); 650ecdbb911SFenghua Yu execlp("dmesg", "dmesg", NULL); 651ecdbb911SFenghua Yu perror("executing dmesg"); 652ecdbb911SFenghua Yu exit(1); 653ecdbb911SFenghua Yu } 654ecdbb911SFenghua Yu close(pipefds[1]); 655ecdbb911SFenghua Yu fp = fdopen(pipefds[0], "r"); 656ecdbb911SFenghua Yu if (!fp) { 657ecdbb911SFenghua Yu perror("fdopen(pipe)"); 658ecdbb911SFenghua Yu kill(pid, SIGTERM); 659ecdbb911SFenghua Yu 660ecdbb911SFenghua Yu return -1; 661ecdbb911SFenghua Yu } 662ecdbb911SFenghua Yu 663ecdbb911SFenghua Yu while (fgets(line, 1024, fp)) { 664ecdbb911SFenghua Yu if (strstr(line, "intel_rdt:")) 665ca2f4214SFenghua Yu ksft_print_msg("dmesg: %s", line); 666ecdbb911SFenghua Yu if (strstr(line, "resctrl:")) 667ca2f4214SFenghua Yu ksft_print_msg("dmesg: %s", line); 668ecdbb911SFenghua Yu } 669ecdbb911SFenghua Yu fclose(fp); 670ecdbb911SFenghua Yu waitpid(pid, NULL, 0); 671ecdbb911SFenghua Yu 672ecdbb911SFenghua Yu return 0; 673ecdbb911SFenghua Yu } 674ecdbb911SFenghua Yu 675591a6e85SSai Praneeth Prakhya int validate_bw_report_request(char *bw_report) 676591a6e85SSai Praneeth Prakhya { 677591a6e85SSai Praneeth Prakhya if (strcmp(bw_report, "reads") == 0) 678591a6e85SSai Praneeth Prakhya return 0; 679591a6e85SSai Praneeth Prakhya if (strcmp(bw_report, "writes") == 0) 680591a6e85SSai Praneeth Prakhya return 0; 681591a6e85SSai Praneeth Prakhya if (strcmp(bw_report, "nt-writes") == 0) { 682591a6e85SSai Praneeth Prakhya strcpy(bw_report, "writes"); 683591a6e85SSai Praneeth Prakhya return 0; 684591a6e85SSai Praneeth Prakhya } 685591a6e85SSai Praneeth Prakhya if (strcmp(bw_report, "total") == 0) 686591a6e85SSai Praneeth Prakhya return 0; 687591a6e85SSai Praneeth Prakhya 688591a6e85SSai Praneeth Prakhya fprintf(stderr, "Requested iMC B/W report type unavailable\n"); 689591a6e85SSai Praneeth Prakhya 690591a6e85SSai Praneeth Prakhya return -1; 691591a6e85SSai Praneeth Prakhya } 692591a6e85SSai Praneeth Prakhya 693591a6e85SSai Praneeth Prakhya int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, 694591a6e85SSai Praneeth Prakhya int group_fd, unsigned long flags) 695591a6e85SSai Praneeth Prakhya { 696591a6e85SSai Praneeth Prakhya int ret; 697591a6e85SSai Praneeth Prakhya 698591a6e85SSai Praneeth Prakhya ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, 699591a6e85SSai Praneeth Prakhya group_fd, flags); 700591a6e85SSai Praneeth Prakhya return ret; 701591a6e85SSai Praneeth Prakhya } 70278941183SFenghua Yu 70378941183SFenghua Yu unsigned int count_bits(unsigned long n) 70478941183SFenghua Yu { 70578941183SFenghua Yu unsigned int count = 0; 70678941183SFenghua Yu 70778941183SFenghua Yu while (n) { 70878941183SFenghua Yu count += n & 1; 70978941183SFenghua Yu n >>= 1; 71078941183SFenghua Yu } 71178941183SFenghua Yu 71278941183SFenghua Yu return count; 71378941183SFenghua Yu } 714