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 { 854e5cb354SFenghua Yu if (find_resctrl_mount(NULL)) 864e5cb354SFenghua Yu return 0; 874e5cb354SFenghua Yu 88591a6e85SSai Praneeth Prakhya if (umount(RESCTRL_PATH)) { 89591a6e85SSai Praneeth Prakhya perror("# Unable to umount resctrl"); 90591a6e85SSai Praneeth Prakhya 91591a6e85SSai Praneeth Prakhya return errno; 92591a6e85SSai Praneeth Prakhya } 93591a6e85SSai Praneeth Prakhya 94591a6e85SSai Praneeth Prakhya return 0; 95591a6e85SSai Praneeth Prakhya } 96591a6e85SSai Praneeth Prakhya 97591a6e85SSai Praneeth Prakhya /* 98591a6e85SSai Praneeth Prakhya * get_resource_id - Get socket number/l3 id for a specified CPU 99591a6e85SSai Praneeth Prakhya * @cpu_no: CPU number 100591a6e85SSai Praneeth Prakhya * @resource_id: Socket number or l3_id 101591a6e85SSai Praneeth Prakhya * 102591a6e85SSai Praneeth Prakhya * Return: >= 0 on success, < 0 on failure. 103591a6e85SSai Praneeth Prakhya */ 104591a6e85SSai Praneeth Prakhya int get_resource_id(int cpu_no, int *resource_id) 105591a6e85SSai Praneeth Prakhya { 106591a6e85SSai Praneeth Prakhya char phys_pkg_path[1024]; 107591a6e85SSai Praneeth Prakhya FILE *fp; 108591a6e85SSai Praneeth Prakhya 1096220f69eSShaopeng Tan if (get_vendor() == ARCH_AMD) 110c0327e1dSBabu Moger sprintf(phys_pkg_path, "%s%d/cache/index3/id", 111c0327e1dSBabu Moger PHYS_ID_PATH, cpu_no); 112c0327e1dSBabu Moger else 113591a6e85SSai Praneeth Prakhya sprintf(phys_pkg_path, "%s%d/topology/physical_package_id", 114591a6e85SSai Praneeth Prakhya PHYS_ID_PATH, cpu_no); 115c0327e1dSBabu Moger 116591a6e85SSai Praneeth Prakhya fp = fopen(phys_pkg_path, "r"); 117591a6e85SSai Praneeth Prakhya if (!fp) { 118591a6e85SSai Praneeth Prakhya perror("Failed to open physical_package_id"); 119591a6e85SSai Praneeth Prakhya 120591a6e85SSai Praneeth Prakhya return -1; 121591a6e85SSai Praneeth Prakhya } 122591a6e85SSai Praneeth Prakhya if (fscanf(fp, "%d", resource_id) <= 0) { 123591a6e85SSai Praneeth Prakhya perror("Could not get socket number or l3 id"); 124591a6e85SSai Praneeth Prakhya fclose(fp); 125591a6e85SSai Praneeth Prakhya 126591a6e85SSai Praneeth Prakhya return -1; 127591a6e85SSai Praneeth Prakhya } 128591a6e85SSai Praneeth Prakhya fclose(fp); 129591a6e85SSai Praneeth Prakhya 130591a6e85SSai Praneeth Prakhya return 0; 131591a6e85SSai Praneeth Prakhya } 132591a6e85SSai Praneeth Prakhya 133591a6e85SSai Praneeth Prakhya /* 13478941183SFenghua Yu * get_cache_size - Get cache size for a specified CPU 13578941183SFenghua Yu * @cpu_no: CPU number 13678941183SFenghua Yu * @cache_type: Cache level L2/L3 13778941183SFenghua Yu * @cache_size: pointer to cache_size 13878941183SFenghua Yu * 13978941183SFenghua Yu * Return: = 0 on success, < 0 on failure. 14078941183SFenghua Yu */ 14178941183SFenghua Yu int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size) 14278941183SFenghua Yu { 14378941183SFenghua Yu char cache_path[1024], cache_str[64]; 14478941183SFenghua Yu int length, i, cache_num; 14578941183SFenghua Yu FILE *fp; 14678941183SFenghua Yu 14778941183SFenghua Yu if (!strcmp(cache_type, "L3")) { 14878941183SFenghua Yu cache_num = 3; 14978941183SFenghua Yu } else if (!strcmp(cache_type, "L2")) { 15078941183SFenghua Yu cache_num = 2; 15178941183SFenghua Yu } else { 15278941183SFenghua Yu perror("Invalid cache level"); 15378941183SFenghua Yu return -1; 15478941183SFenghua Yu } 15578941183SFenghua Yu 15678941183SFenghua Yu sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size", 15778941183SFenghua Yu cpu_no, cache_num); 15878941183SFenghua Yu fp = fopen(cache_path, "r"); 15978941183SFenghua Yu if (!fp) { 16078941183SFenghua Yu perror("Failed to open cache size"); 16178941183SFenghua Yu 16278941183SFenghua Yu return -1; 16378941183SFenghua Yu } 16478941183SFenghua Yu if (fscanf(fp, "%s", cache_str) <= 0) { 16578941183SFenghua Yu perror("Could not get cache_size"); 16678941183SFenghua Yu fclose(fp); 16778941183SFenghua Yu 16878941183SFenghua Yu return -1; 16978941183SFenghua Yu } 17078941183SFenghua Yu fclose(fp); 17178941183SFenghua Yu 17278941183SFenghua Yu length = (int)strlen(cache_str); 17378941183SFenghua Yu 17478941183SFenghua Yu *cache_size = 0; 17578941183SFenghua Yu 17678941183SFenghua Yu for (i = 0; i < length; i++) { 17778941183SFenghua Yu if ((cache_str[i] >= '0') && (cache_str[i] <= '9')) 17878941183SFenghua Yu 17978941183SFenghua Yu *cache_size = *cache_size * 10 + (cache_str[i] - '0'); 18078941183SFenghua Yu 18178941183SFenghua Yu else if (cache_str[i] == 'K') 18278941183SFenghua Yu 18378941183SFenghua Yu *cache_size = *cache_size * 1024; 18478941183SFenghua Yu 18578941183SFenghua Yu else if (cache_str[i] == 'M') 18678941183SFenghua Yu 18778941183SFenghua Yu *cache_size = *cache_size * 1024 * 1024; 18878941183SFenghua Yu 18978941183SFenghua Yu else 19078941183SFenghua Yu break; 19178941183SFenghua Yu } 19278941183SFenghua Yu 19378941183SFenghua Yu return 0; 19478941183SFenghua Yu } 19578941183SFenghua Yu 19678941183SFenghua Yu #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu" 19778941183SFenghua Yu 19878941183SFenghua Yu /* 19978941183SFenghua Yu * get_cbm_mask - Get cbm mask for given cache 20078941183SFenghua Yu * @cache_type: Cache level L2/L3 2018236c51dSFenghua Yu * @cbm_mask: cbm_mask returned as a string 20278941183SFenghua Yu * 20378941183SFenghua Yu * Return: = 0 on success, < 0 on failure. 20478941183SFenghua Yu */ 2058236c51dSFenghua Yu int get_cbm_mask(char *cache_type, char *cbm_mask) 20678941183SFenghua Yu { 20778941183SFenghua Yu char cbm_mask_path[1024]; 20878941183SFenghua Yu FILE *fp; 20978941183SFenghua Yu 2108236c51dSFenghua Yu if (!cbm_mask) 2118236c51dSFenghua Yu return -1; 2128236c51dSFenghua Yu 213e48c3230SFenghua Yu sprintf(cbm_mask_path, "%s/%s/cbm_mask", INFO_PATH, cache_type); 21478941183SFenghua Yu 21578941183SFenghua Yu fp = fopen(cbm_mask_path, "r"); 21678941183SFenghua Yu if (!fp) { 21778941183SFenghua Yu perror("Failed to open cache level"); 21878941183SFenghua Yu 21978941183SFenghua Yu return -1; 22078941183SFenghua Yu } 22178941183SFenghua Yu if (fscanf(fp, "%s", cbm_mask) <= 0) { 22278941183SFenghua Yu perror("Could not get max cbm_mask"); 22378941183SFenghua Yu fclose(fp); 22478941183SFenghua Yu 22578941183SFenghua Yu return -1; 22678941183SFenghua Yu } 22778941183SFenghua Yu fclose(fp); 22878941183SFenghua Yu 22978941183SFenghua Yu return 0; 23078941183SFenghua Yu } 23178941183SFenghua Yu 23278941183SFenghua Yu /* 23378941183SFenghua Yu * get_core_sibling - Get sibling core id from the same socket for given CPU 23478941183SFenghua Yu * @cpu_no: CPU number 23578941183SFenghua Yu * 23678941183SFenghua Yu * Return: > 0 on success, < 0 on failure. 23778941183SFenghua Yu */ 23878941183SFenghua Yu int get_core_sibling(int cpu_no) 23978941183SFenghua Yu { 24078941183SFenghua Yu char core_siblings_path[1024], cpu_list_str[64]; 24178941183SFenghua Yu int sibling_cpu_no = -1; 24278941183SFenghua Yu FILE *fp; 24378941183SFenghua Yu 24478941183SFenghua Yu sprintf(core_siblings_path, "%s%d/topology/core_siblings_list", 24578941183SFenghua Yu CORE_SIBLINGS_PATH, cpu_no); 24678941183SFenghua Yu 24778941183SFenghua Yu fp = fopen(core_siblings_path, "r"); 24878941183SFenghua Yu if (!fp) { 24978941183SFenghua Yu perror("Failed to open core siblings path"); 25078941183SFenghua Yu 25178941183SFenghua Yu return -1; 25278941183SFenghua Yu } 25378941183SFenghua Yu if (fscanf(fp, "%s", cpu_list_str) <= 0) { 25478941183SFenghua Yu perror("Could not get core_siblings list"); 25578941183SFenghua Yu fclose(fp); 25678941183SFenghua Yu 25778941183SFenghua Yu return -1; 25878941183SFenghua Yu } 25978941183SFenghua Yu fclose(fp); 26078941183SFenghua Yu 26178941183SFenghua Yu char *token = strtok(cpu_list_str, "-,"); 26278941183SFenghua Yu 26378941183SFenghua Yu while (token) { 26478941183SFenghua Yu sibling_cpu_no = atoi(token); 26578941183SFenghua Yu /* Skipping core 0 as we don't want to run test on core 0 */ 266f5f16ae4SReinette Chatre if (sibling_cpu_no != 0 && sibling_cpu_no != cpu_no) 26778941183SFenghua Yu break; 26878941183SFenghua Yu token = strtok(NULL, "-,"); 26978941183SFenghua Yu } 27078941183SFenghua Yu 27178941183SFenghua Yu return sibling_cpu_no; 27278941183SFenghua Yu } 27378941183SFenghua Yu 27478941183SFenghua Yu /* 275591a6e85SSai Praneeth Prakhya * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu 276591a6e85SSai Praneeth Prakhya * @bm_pid: PID that should be binded 277591a6e85SSai Praneeth Prakhya * @cpu_no: CPU number at which the PID would be binded 278591a6e85SSai Praneeth Prakhya * 279591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 280591a6e85SSai Praneeth Prakhya */ 281591a6e85SSai Praneeth Prakhya int taskset_benchmark(pid_t bm_pid, int cpu_no) 282591a6e85SSai Praneeth Prakhya { 283591a6e85SSai Praneeth Prakhya cpu_set_t my_set; 284591a6e85SSai Praneeth Prakhya 285591a6e85SSai Praneeth Prakhya CPU_ZERO(&my_set); 286591a6e85SSai Praneeth Prakhya CPU_SET(cpu_no, &my_set); 287591a6e85SSai Praneeth Prakhya 288591a6e85SSai Praneeth Prakhya if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) { 289591a6e85SSai Praneeth Prakhya perror("Unable to taskset benchmark"); 290591a6e85SSai Praneeth Prakhya 291591a6e85SSai Praneeth Prakhya return -1; 292591a6e85SSai Praneeth Prakhya } 293591a6e85SSai Praneeth Prakhya 294591a6e85SSai Praneeth Prakhya return 0; 295591a6e85SSai Praneeth Prakhya } 296591a6e85SSai Praneeth Prakhya 297591a6e85SSai Praneeth Prakhya /* 298591a6e85SSai Praneeth Prakhya * run_benchmark - Run a specified benchmark or fill_buf (default benchmark) 299591a6e85SSai Praneeth Prakhya * in specified signal. Direct benchmark stdio to /dev/null. 300591a6e85SSai Praneeth Prakhya * @signum: signal number 301591a6e85SSai Praneeth Prakhya * @info: signal info 302591a6e85SSai Praneeth Prakhya * @ucontext: user context in signal handling 303591a6e85SSai Praneeth Prakhya * 304591a6e85SSai Praneeth Prakhya * Return: void 305591a6e85SSai Praneeth Prakhya */ 306591a6e85SSai Praneeth Prakhya void run_benchmark(int signum, siginfo_t *info, void *ucontext) 307591a6e85SSai Praneeth Prakhya { 30878941183SFenghua Yu int operation, ret, malloc_and_init_memory, memflush; 30978941183SFenghua Yu unsigned long span, buffer_span; 310591a6e85SSai Praneeth Prakhya char **benchmark_cmd; 31178941183SFenghua Yu char resctrl_val[64]; 312591a6e85SSai Praneeth Prakhya FILE *fp; 313591a6e85SSai Praneeth Prakhya 314591a6e85SSai Praneeth Prakhya benchmark_cmd = info->si_ptr; 315591a6e85SSai Praneeth Prakhya 316591a6e85SSai Praneeth Prakhya /* 317591a6e85SSai Praneeth Prakhya * Direct stdio of child to /dev/null, so that only parent writes to 318591a6e85SSai Praneeth Prakhya * stdio (console) 319591a6e85SSai Praneeth Prakhya */ 320591a6e85SSai Praneeth Prakhya fp = freopen("/dev/null", "w", stdout); 321591a6e85SSai Praneeth Prakhya if (!fp) 322591a6e85SSai Praneeth Prakhya PARENT_EXIT("Unable to direct benchmark status to /dev/null"); 323591a6e85SSai Praneeth Prakhya 324591a6e85SSai Praneeth Prakhya if (strcmp(benchmark_cmd[0], "fill_buf") == 0) { 325591a6e85SSai Praneeth Prakhya /* Execute default fill_buf benchmark */ 326591a6e85SSai Praneeth Prakhya span = strtoul(benchmark_cmd[1], NULL, 10); 32778941183SFenghua Yu malloc_and_init_memory = atoi(benchmark_cmd[2]); 32878941183SFenghua Yu memflush = atoi(benchmark_cmd[3]); 329591a6e85SSai Praneeth Prakhya operation = atoi(benchmark_cmd[4]); 33078941183SFenghua Yu sprintf(resctrl_val, "%s", benchmark_cmd[5]); 33178941183SFenghua Yu 3322f320911SFenghua Yu if (strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) 33378941183SFenghua Yu buffer_span = span * MB; 33478941183SFenghua Yu else 33578941183SFenghua Yu buffer_span = span; 33678941183SFenghua Yu 33778941183SFenghua Yu if (run_fill_buf(buffer_span, malloc_and_init_memory, memflush, 33878941183SFenghua Yu operation, resctrl_val)) 339591a6e85SSai Praneeth Prakhya fprintf(stderr, "Error in running fill buffer\n"); 340591a6e85SSai Praneeth Prakhya } else { 341591a6e85SSai Praneeth Prakhya /* Execute specified benchmark */ 342591a6e85SSai Praneeth Prakhya ret = execvp(benchmark_cmd[0], benchmark_cmd); 343591a6e85SSai Praneeth Prakhya if (ret) 344591a6e85SSai Praneeth Prakhya perror("wrong\n"); 345591a6e85SSai Praneeth Prakhya } 346591a6e85SSai Praneeth Prakhya 347591a6e85SSai Praneeth Prakhya fclose(stdout); 348591a6e85SSai Praneeth Prakhya PARENT_EXIT("Unable to run specified benchmark"); 349591a6e85SSai Praneeth Prakhya } 350591a6e85SSai Praneeth Prakhya 351591a6e85SSai Praneeth Prakhya /* 352591a6e85SSai Praneeth Prakhya * create_grp - Create a group only if one doesn't exist 353591a6e85SSai Praneeth Prakhya * @grp_name: Name of the group 354591a6e85SSai Praneeth Prakhya * @grp: Full path and name of the group 355591a6e85SSai Praneeth Prakhya * @parent_grp: Full path and name of the parent group 356591a6e85SSai Praneeth Prakhya * 357591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 358591a6e85SSai Praneeth Prakhya */ 359591a6e85SSai Praneeth Prakhya static int create_grp(const char *grp_name, char *grp, const char *parent_grp) 360591a6e85SSai Praneeth Prakhya { 361591a6e85SSai Praneeth Prakhya int found_grp = 0; 362591a6e85SSai Praneeth Prakhya struct dirent *ep; 363591a6e85SSai Praneeth Prakhya DIR *dp; 364591a6e85SSai Praneeth Prakhya 36578941183SFenghua Yu /* 36678941183SFenghua Yu * At this point, we are guaranteed to have resctrl FS mounted and if 36778941183SFenghua Yu * length of grp_name == 0, it means, user wants to use root con_mon 36878941183SFenghua Yu * grp, so do nothing 36978941183SFenghua Yu */ 37078941183SFenghua Yu if (strlen(grp_name) == 0) 37178941183SFenghua Yu return 0; 37278941183SFenghua Yu 373591a6e85SSai Praneeth Prakhya /* Check if requested grp exists or not */ 374591a6e85SSai Praneeth Prakhya dp = opendir(parent_grp); 375591a6e85SSai Praneeth Prakhya if (dp) { 376591a6e85SSai Praneeth Prakhya while ((ep = readdir(dp)) != NULL) { 377591a6e85SSai Praneeth Prakhya if (strcmp(ep->d_name, grp_name) == 0) 378591a6e85SSai Praneeth Prakhya found_grp = 1; 379591a6e85SSai Praneeth Prakhya } 380591a6e85SSai Praneeth Prakhya closedir(dp); 381591a6e85SSai Praneeth Prakhya } else { 382591a6e85SSai Praneeth Prakhya perror("Unable to open resctrl for group"); 383591a6e85SSai Praneeth Prakhya 384591a6e85SSai Praneeth Prakhya return -1; 385591a6e85SSai Praneeth Prakhya } 386591a6e85SSai Praneeth Prakhya 387591a6e85SSai Praneeth Prakhya /* Requested grp doesn't exist, hence create it */ 388591a6e85SSai Praneeth Prakhya if (found_grp == 0) { 389591a6e85SSai Praneeth Prakhya if (mkdir(grp, 0) == -1) { 390591a6e85SSai Praneeth Prakhya perror("Unable to create group"); 391591a6e85SSai Praneeth Prakhya 392591a6e85SSai Praneeth Prakhya return -1; 393591a6e85SSai Praneeth Prakhya } 394591a6e85SSai Praneeth Prakhya } 395591a6e85SSai Praneeth Prakhya 396591a6e85SSai Praneeth Prakhya return 0; 397591a6e85SSai Praneeth Prakhya } 398591a6e85SSai Praneeth Prakhya 399591a6e85SSai Praneeth Prakhya static int write_pid_to_tasks(char *tasks, pid_t pid) 400591a6e85SSai Praneeth Prakhya { 401591a6e85SSai Praneeth Prakhya FILE *fp; 402591a6e85SSai Praneeth Prakhya 403591a6e85SSai Praneeth Prakhya fp = fopen(tasks, "w"); 404591a6e85SSai Praneeth Prakhya if (!fp) { 405591a6e85SSai Praneeth Prakhya perror("Failed to open tasks file"); 406591a6e85SSai Praneeth Prakhya 407591a6e85SSai Praneeth Prakhya return -1; 408591a6e85SSai Praneeth Prakhya } 409591a6e85SSai Praneeth Prakhya if (fprintf(fp, "%d\n", pid) < 0) { 410591a6e85SSai Praneeth Prakhya perror("Failed to wr pid to tasks file"); 411591a6e85SSai Praneeth Prakhya fclose(fp); 412591a6e85SSai Praneeth Prakhya 413591a6e85SSai Praneeth Prakhya return -1; 414591a6e85SSai Praneeth Prakhya } 415591a6e85SSai Praneeth Prakhya fclose(fp); 416591a6e85SSai Praneeth Prakhya 417591a6e85SSai Praneeth Prakhya return 0; 418591a6e85SSai Praneeth Prakhya } 419591a6e85SSai Praneeth Prakhya 420591a6e85SSai Praneeth Prakhya /* 421591a6e85SSai Praneeth Prakhya * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS 422591a6e85SSai Praneeth Prakhya * @bm_pid: PID that should be written 423591a6e85SSai Praneeth Prakhya * @ctrlgrp: Name of the control monitor group (con_mon grp) 424591a6e85SSai Praneeth Prakhya * @mongrp: Name of the monitor group (mon grp) 425591a6e85SSai Praneeth Prakhya * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc) 426591a6e85SSai Praneeth Prakhya * 427591a6e85SSai Praneeth Prakhya * If a con_mon grp is requested, create it and write pid to it, otherwise 428591a6e85SSai Praneeth Prakhya * write pid to root con_mon grp. 429591a6e85SSai Praneeth Prakhya * If a mon grp is requested, create it and write pid to it, otherwise 430591a6e85SSai Praneeth Prakhya * pid is not written, this means that pid is in con_mon grp and hence 431591a6e85SSai Praneeth Prakhya * should consult con_mon grp's mon_data directory for results. 432591a6e85SSai Praneeth Prakhya * 433591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 434591a6e85SSai Praneeth Prakhya */ 435591a6e85SSai Praneeth Prakhya int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp, 436591a6e85SSai Praneeth Prakhya char *resctrl_val) 437591a6e85SSai Praneeth Prakhya { 438591a6e85SSai Praneeth Prakhya char controlgroup[128], monitorgroup[512], monitorgroup_p[256]; 439591a6e85SSai Praneeth Prakhya char tasks[1024]; 440591a6e85SSai Praneeth Prakhya int ret = 0; 441591a6e85SSai Praneeth Prakhya 442591a6e85SSai Praneeth Prakhya if (strlen(ctrlgrp)) 443591a6e85SSai Praneeth Prakhya sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp); 444591a6e85SSai Praneeth Prakhya else 445591a6e85SSai Praneeth Prakhya sprintf(controlgroup, "%s", RESCTRL_PATH); 446591a6e85SSai Praneeth Prakhya 447591a6e85SSai Praneeth Prakhya /* Create control and monitoring group and write pid into it */ 448591a6e85SSai Praneeth Prakhya ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH); 449591a6e85SSai Praneeth Prakhya if (ret) 450591a6e85SSai Praneeth Prakhya goto out; 451591a6e85SSai Praneeth Prakhya sprintf(tasks, "%s/tasks", controlgroup); 452591a6e85SSai Praneeth Prakhya ret = write_pid_to_tasks(tasks, bm_pid); 453591a6e85SSai Praneeth Prakhya if (ret) 454591a6e85SSai Praneeth Prakhya goto out; 455591a6e85SSai Praneeth Prakhya 4562f320911SFenghua Yu /* Create mon grp and write pid into it for "mbm" and "cmt" test */ 4572f320911SFenghua Yu if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)) || 45824286736SFenghua Yu !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) { 45978941183SFenghua Yu if (strlen(mongrp)) { 460591a6e85SSai Praneeth Prakhya sprintf(monitorgroup_p, "%s/mon_groups", controlgroup); 461591a6e85SSai Praneeth Prakhya sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp); 462591a6e85SSai Praneeth Prakhya ret = create_grp(mongrp, monitorgroup, monitorgroup_p); 463591a6e85SSai Praneeth Prakhya if (ret) 464591a6e85SSai Praneeth Prakhya goto out; 465591a6e85SSai Praneeth Prakhya 466591a6e85SSai Praneeth Prakhya sprintf(tasks, "%s/mon_groups/%s/tasks", 467591a6e85SSai Praneeth Prakhya controlgroup, mongrp); 468591a6e85SSai Praneeth Prakhya ret = write_pid_to_tasks(tasks, bm_pid); 469591a6e85SSai Praneeth Prakhya if (ret) 470591a6e85SSai Praneeth Prakhya goto out; 471591a6e85SSai Praneeth Prakhya } 472591a6e85SSai Praneeth Prakhya } 473591a6e85SSai Praneeth Prakhya 474591a6e85SSai Praneeth Prakhya out: 475ca2f4214SFenghua Yu ksft_print_msg("Writing benchmark parameters to resctrl FS\n"); 476591a6e85SSai Praneeth Prakhya if (ret) 477591a6e85SSai Praneeth Prakhya perror("# writing to resctrlfs"); 478591a6e85SSai Praneeth Prakhya 479591a6e85SSai Praneeth Prakhya return ret; 480591a6e85SSai Praneeth Prakhya } 481591a6e85SSai Praneeth Prakhya 482591a6e85SSai Praneeth Prakhya /* 483591a6e85SSai Praneeth Prakhya * write_schemata - Update schemata of a con_mon grp 484591a6e85SSai Praneeth Prakhya * @ctrlgrp: Name of the con_mon grp 485591a6e85SSai Praneeth Prakhya * @schemata: Schemata that should be updated to 486591a6e85SSai Praneeth Prakhya * @cpu_no: CPU number that the benchmark PID is binded to 487591a6e85SSai Praneeth Prakhya * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc) 488591a6e85SSai Praneeth Prakhya * 489591a6e85SSai Praneeth Prakhya * Update schemata of a con_mon grp *only* if requested resctrl feature is 490591a6e85SSai Praneeth Prakhya * allocation type 491591a6e85SSai Praneeth Prakhya * 492591a6e85SSai Praneeth Prakhya * Return: 0 on success, non-zero on failure 493591a6e85SSai Praneeth Prakhya */ 494591a6e85SSai Praneeth Prakhya int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val) 495591a6e85SSai Praneeth Prakhya { 496591a6e85SSai Praneeth Prakhya char controlgroup[1024], schema[1024], reason[64]; 497591a6e85SSai Praneeth Prakhya int resource_id, ret = 0; 498591a6e85SSai Praneeth Prakhya FILE *fp; 499591a6e85SSai Praneeth Prakhya 50024286736SFenghua Yu if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) && 50142e3b093SShaopeng Tan strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) && 50224286736SFenghua Yu strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) && 5032f320911SFenghua Yu strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) 504591a6e85SSai Praneeth Prakhya return -ENOENT; 505591a6e85SSai Praneeth Prakhya 506591a6e85SSai Praneeth Prakhya if (!schemata) { 507ca2f4214SFenghua Yu ksft_print_msg("Skipping empty schemata update\n"); 508591a6e85SSai Praneeth Prakhya 509591a6e85SSai Praneeth Prakhya return -1; 510591a6e85SSai Praneeth Prakhya } 511591a6e85SSai Praneeth Prakhya 512591a6e85SSai Praneeth Prakhya if (get_resource_id(cpu_no, &resource_id) < 0) { 513591a6e85SSai Praneeth Prakhya sprintf(reason, "Failed to get resource id"); 514591a6e85SSai Praneeth Prakhya ret = -1; 515591a6e85SSai Praneeth Prakhya 516591a6e85SSai Praneeth Prakhya goto out; 517591a6e85SSai Praneeth Prakhya } 518591a6e85SSai Praneeth Prakhya 519591a6e85SSai Praneeth Prakhya if (strlen(ctrlgrp) != 0) 520591a6e85SSai Praneeth Prakhya sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp); 521591a6e85SSai Praneeth Prakhya else 522591a6e85SSai Praneeth Prakhya sprintf(controlgroup, "%s/schemata", RESCTRL_PATH); 523591a6e85SSai Praneeth Prakhya 52424286736SFenghua Yu if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) || 5252f320911SFenghua Yu !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) 52678941183SFenghua Yu sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata); 52742e3b093SShaopeng Tan if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) || 52842e3b093SShaopeng Tan !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) 529591a6e85SSai Praneeth Prakhya sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata); 530591a6e85SSai Praneeth Prakhya 531591a6e85SSai Praneeth Prakhya fp = fopen(controlgroup, "w"); 532591a6e85SSai Praneeth Prakhya if (!fp) { 533591a6e85SSai Praneeth Prakhya sprintf(reason, "Failed to open control group"); 534591a6e85SSai Praneeth Prakhya ret = -1; 535591a6e85SSai Praneeth Prakhya 536591a6e85SSai Praneeth Prakhya goto out; 537591a6e85SSai Praneeth Prakhya } 538591a6e85SSai Praneeth Prakhya 539591a6e85SSai Praneeth Prakhya if (fprintf(fp, "%s\n", schema) < 0) { 540591a6e85SSai Praneeth Prakhya sprintf(reason, "Failed to write schemata in control group"); 541591a6e85SSai Praneeth Prakhya fclose(fp); 542591a6e85SSai Praneeth Prakhya ret = -1; 543591a6e85SSai Praneeth Prakhya 544591a6e85SSai Praneeth Prakhya goto out; 545591a6e85SSai Praneeth Prakhya } 546591a6e85SSai Praneeth Prakhya fclose(fp); 547591a6e85SSai Praneeth Prakhya 548591a6e85SSai Praneeth Prakhya out: 549ca2f4214SFenghua Yu ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n", 550ca2f4214SFenghua Yu schema, ret ? " # " : "", 551591a6e85SSai Praneeth Prakhya ret ? reason : ""); 552591a6e85SSai Praneeth Prakhya 553591a6e85SSai Praneeth Prakhya return ret; 554591a6e85SSai Praneeth Prakhya } 555591a6e85SSai Praneeth Prakhya 556ecdbb911SFenghua Yu bool check_resctrlfs_support(void) 557ecdbb911SFenghua Yu { 558ecdbb911SFenghua Yu FILE *inf = fopen("/proc/filesystems", "r"); 559ecdbb911SFenghua Yu DIR *dp; 560ecdbb911SFenghua Yu char *res; 561ecdbb911SFenghua Yu bool ret = false; 562ecdbb911SFenghua Yu 563ecdbb911SFenghua Yu if (!inf) 564ecdbb911SFenghua Yu return false; 565ecdbb911SFenghua Yu 566ecdbb911SFenghua Yu res = fgrep(inf, "nodev\tresctrl\n"); 567ecdbb911SFenghua Yu 568ecdbb911SFenghua Yu if (res) { 569ecdbb911SFenghua Yu ret = true; 570ecdbb911SFenghua Yu free(res); 571ecdbb911SFenghua Yu } 572ecdbb911SFenghua Yu 573ecdbb911SFenghua Yu fclose(inf); 574ecdbb911SFenghua Yu 575e7507478SFenghua Yu ksft_print_msg("%s Check kernel supports resctrl filesystem\n", 576ca2f4214SFenghua Yu ret ? "Pass:" : "Fail:"); 577ecdbb911SFenghua Yu 578a3611fbcSFenghua Yu if (!ret) 579a3611fbcSFenghua Yu return ret; 580a3611fbcSFenghua Yu 581ecdbb911SFenghua Yu dp = opendir(RESCTRL_PATH); 582e7507478SFenghua Yu ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n", 583ca2f4214SFenghua Yu dp ? "Pass:" : "Fail:", RESCTRL_PATH); 584ecdbb911SFenghua Yu if (dp) 585ecdbb911SFenghua Yu closedir(dp); 586ecdbb911SFenghua Yu 587ca2f4214SFenghua Yu ksft_print_msg("resctrl filesystem %s mounted\n", 588ecdbb911SFenghua Yu find_resctrl_mount(NULL) ? "not" : "is"); 589ecdbb911SFenghua Yu 590ecdbb911SFenghua Yu return ret; 591ecdbb911SFenghua Yu } 592ecdbb911SFenghua Yu 593591a6e85SSai Praneeth Prakhya char *fgrep(FILE *inf, const char *str) 594591a6e85SSai Praneeth Prakhya { 595591a6e85SSai Praneeth Prakhya char line[256]; 596591a6e85SSai Praneeth Prakhya int slen = strlen(str); 597591a6e85SSai Praneeth Prakhya 598591a6e85SSai Praneeth Prakhya while (!feof(inf)) { 599591a6e85SSai Praneeth Prakhya if (!fgets(line, 256, inf)) 600591a6e85SSai Praneeth Prakhya break; 601591a6e85SSai Praneeth Prakhya if (strncmp(line, str, slen)) 602591a6e85SSai Praneeth Prakhya continue; 603591a6e85SSai Praneeth Prakhya 604591a6e85SSai Praneeth Prakhya return strdup(line); 605591a6e85SSai Praneeth Prakhya } 606591a6e85SSai Praneeth Prakhya 607591a6e85SSai Praneeth Prakhya return NULL; 608591a6e85SSai Praneeth Prakhya } 609591a6e85SSai Praneeth Prakhya 610591a6e85SSai Praneeth Prakhya /* 611591a6e85SSai Praneeth Prakhya * validate_resctrl_feature_request - Check if requested feature is valid. 612591a6e85SSai Praneeth Prakhya * @resctrl_val: Requested feature 613591a6e85SSai Praneeth Prakhya * 614ee041568SFenghua Yu * Return: True if the feature is supported, else false 615591a6e85SSai Praneeth Prakhya */ 616ee041568SFenghua Yu bool validate_resctrl_feature_request(const char *resctrl_val) 617591a6e85SSai Praneeth Prakhya { 618ee041568SFenghua Yu struct stat statbuf; 619591a6e85SSai Praneeth Prakhya bool found = false; 620591a6e85SSai Praneeth Prakhya char *res; 621ee041568SFenghua Yu FILE *inf; 622591a6e85SSai Praneeth Prakhya 623ee041568SFenghua Yu if (!resctrl_val) 624ee041568SFenghua Yu return false; 625ee041568SFenghua Yu 626ee041568SFenghua Yu if (remount_resctrlfs(false)) 627ee041568SFenghua Yu return false; 628ee041568SFenghua Yu 629ee041568SFenghua Yu if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) { 630ee041568SFenghua Yu if (!stat(L3_PATH, &statbuf)) 631ee041568SFenghua Yu return true; 632ee041568SFenghua Yu } else if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) { 633ee041568SFenghua Yu if (!stat(MB_PATH, &statbuf)) 634ee041568SFenghua Yu return true; 635ee041568SFenghua Yu } else if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) || 636ee041568SFenghua Yu !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) { 637ee041568SFenghua Yu if (!stat(L3_MON_PATH, &statbuf)) { 638ee041568SFenghua Yu inf = fopen(L3_MON_FEATURES_PATH, "r"); 639591a6e85SSai Praneeth Prakhya if (!inf) 640591a6e85SSai Praneeth Prakhya return false; 641591a6e85SSai Praneeth Prakhya 642ee041568SFenghua Yu if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) { 643ee041568SFenghua Yu res = fgrep(inf, "llc_occupancy"); 644591a6e85SSai Praneeth Prakhya if (res) { 645ee041568SFenghua Yu found = true; 646591a6e85SSai Praneeth Prakhya free(res); 647591a6e85SSai Praneeth Prakhya } 648ee041568SFenghua Yu } 649ee041568SFenghua Yu 650ee041568SFenghua Yu if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) { 651ee041568SFenghua Yu res = fgrep(inf, "mbm_total_bytes"); 652ee041568SFenghua Yu if (res) { 653ee041568SFenghua Yu free(res); 654ee041568SFenghua Yu res = fgrep(inf, "mbm_local_bytes"); 655ee041568SFenghua Yu if (res) { 656ee041568SFenghua Yu found = true; 657ee041568SFenghua Yu free(res); 658ee041568SFenghua Yu } 659ee041568SFenghua Yu } 660ee041568SFenghua Yu } 661591a6e85SSai Praneeth Prakhya fclose(inf); 662ee041568SFenghua Yu } 663ee041568SFenghua Yu } 664591a6e85SSai Praneeth Prakhya 665591a6e85SSai Praneeth Prakhya return found; 666591a6e85SSai Praneeth Prakhya } 667591a6e85SSai Praneeth Prakhya 668ecdbb911SFenghua Yu int filter_dmesg(void) 669ecdbb911SFenghua Yu { 670ecdbb911SFenghua Yu char line[1024]; 671ecdbb911SFenghua Yu FILE *fp; 672ecdbb911SFenghua Yu int pipefds[2]; 673ecdbb911SFenghua Yu pid_t pid; 674ecdbb911SFenghua Yu int ret; 675ecdbb911SFenghua Yu 676ecdbb911SFenghua Yu ret = pipe(pipefds); 677ecdbb911SFenghua Yu if (ret) { 678ecdbb911SFenghua Yu perror("pipe"); 679ecdbb911SFenghua Yu return ret; 680ecdbb911SFenghua Yu } 681*a080b6e7SShaopeng Tan fflush(stdout); 682ecdbb911SFenghua Yu pid = fork(); 683ecdbb911SFenghua Yu if (pid == 0) { 684ecdbb911SFenghua Yu close(pipefds[0]); 685ecdbb911SFenghua Yu dup2(pipefds[1], STDOUT_FILENO); 686ecdbb911SFenghua Yu execlp("dmesg", "dmesg", NULL); 687ecdbb911SFenghua Yu perror("executing dmesg"); 688ecdbb911SFenghua Yu exit(1); 689ecdbb911SFenghua Yu } 690ecdbb911SFenghua Yu close(pipefds[1]); 691ecdbb911SFenghua Yu fp = fdopen(pipefds[0], "r"); 692ecdbb911SFenghua Yu if (!fp) { 693ecdbb911SFenghua Yu perror("fdopen(pipe)"); 694ecdbb911SFenghua Yu kill(pid, SIGTERM); 695ecdbb911SFenghua Yu 696ecdbb911SFenghua Yu return -1; 697ecdbb911SFenghua Yu } 698ecdbb911SFenghua Yu 699ecdbb911SFenghua Yu while (fgets(line, 1024, fp)) { 700ecdbb911SFenghua Yu if (strstr(line, "intel_rdt:")) 701ca2f4214SFenghua Yu ksft_print_msg("dmesg: %s", line); 702ecdbb911SFenghua Yu if (strstr(line, "resctrl:")) 703ca2f4214SFenghua Yu ksft_print_msg("dmesg: %s", line); 704ecdbb911SFenghua Yu } 705ecdbb911SFenghua Yu fclose(fp); 706ecdbb911SFenghua Yu waitpid(pid, NULL, 0); 707ecdbb911SFenghua Yu 708ecdbb911SFenghua Yu return 0; 709ecdbb911SFenghua Yu } 710ecdbb911SFenghua Yu 711591a6e85SSai Praneeth Prakhya int validate_bw_report_request(char *bw_report) 712591a6e85SSai Praneeth Prakhya { 713591a6e85SSai Praneeth Prakhya if (strcmp(bw_report, "reads") == 0) 714591a6e85SSai Praneeth Prakhya return 0; 715591a6e85SSai Praneeth Prakhya if (strcmp(bw_report, "writes") == 0) 716591a6e85SSai Praneeth Prakhya return 0; 717591a6e85SSai Praneeth Prakhya if (strcmp(bw_report, "nt-writes") == 0) { 718591a6e85SSai Praneeth Prakhya strcpy(bw_report, "writes"); 719591a6e85SSai Praneeth Prakhya return 0; 720591a6e85SSai Praneeth Prakhya } 721591a6e85SSai Praneeth Prakhya if (strcmp(bw_report, "total") == 0) 722591a6e85SSai Praneeth Prakhya return 0; 723591a6e85SSai Praneeth Prakhya 724591a6e85SSai Praneeth Prakhya fprintf(stderr, "Requested iMC B/W report type unavailable\n"); 725591a6e85SSai Praneeth Prakhya 726591a6e85SSai Praneeth Prakhya return -1; 727591a6e85SSai Praneeth Prakhya } 728591a6e85SSai Praneeth Prakhya 729591a6e85SSai Praneeth Prakhya int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, 730591a6e85SSai Praneeth Prakhya int group_fd, unsigned long flags) 731591a6e85SSai Praneeth Prakhya { 732591a6e85SSai Praneeth Prakhya int ret; 733591a6e85SSai Praneeth Prakhya 734591a6e85SSai Praneeth Prakhya ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, 735591a6e85SSai Praneeth Prakhya group_fd, flags); 736591a6e85SSai Praneeth Prakhya return ret; 737591a6e85SSai Praneeth Prakhya } 73878941183SFenghua Yu 73978941183SFenghua Yu unsigned int count_bits(unsigned long n) 74078941183SFenghua Yu { 74178941183SFenghua Yu unsigned int count = 0; 74278941183SFenghua Yu 74378941183SFenghua Yu while (n) { 74478941183SFenghua Yu count += n & 1; 74578941183SFenghua Yu n >>= 1; 74678941183SFenghua Yu } 74778941183SFenghua Yu 74878941183SFenghua Yu return count; 74978941183SFenghua Yu } 750