Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Basic resctrl file system operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2018 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *    Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *    Fenghua Yu <fenghua.yu@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "resctrl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) int tests_run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static int find_resctrl_mount(char *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	FILE *mounts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	char line[256], *fs, *mntpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	mounts = fopen("/proc/mounts", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	if (!mounts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		perror("/proc/mounts");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	while (!feof(mounts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		if (!fgets(line, 256, mounts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		fs = strtok(line, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		if (!fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		mntpoint = strtok(NULL, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		if (!mntpoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		fs = strtok(NULL, " \t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		if (!fs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		if (strcmp(fs, "resctrl"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		fclose(mounts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		if (buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			strncpy(buffer, mntpoint, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	fclose(mounts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * remount_resctrlfs - Remount resctrl FS at /sys/fs/resctrl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * @mum_resctrlfs:	Should the resctrl FS be remounted?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * If not mounted, mount it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * If mounted and mum_resctrlfs then remount resctrl FS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * If mounted and !mum_resctrlfs then noop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * Return: 0 on success, non-zero on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) int remount_resctrlfs(bool mum_resctrlfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	char mountpoint[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	ret = find_resctrl_mount(mountpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		strcpy(mountpoint, RESCTRL_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (!ret && mum_resctrlfs && umount(mountpoint)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		printf("not ok unmounting \"%s\"\n", mountpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		perror("# umount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		tests_run++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (!ret && !mum_resctrlfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	printf("%sok mounting resctrl to \"%s\"\n", ret ? "not " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	       RESCTRL_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		perror("# mount");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	tests_run++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) int umount_resctrlfs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (umount(RESCTRL_PATH)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		perror("# Unable to umount resctrl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * get_resource_id - Get socket number/l3 id for a specified CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * @cpu_no:	CPU number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * @resource_id: Socket number or l3_id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * Return: >= 0 on success, < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int get_resource_id(int cpu_no, int *resource_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	char phys_pkg_path[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (is_amd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		sprintf(phys_pkg_path, "%s%d/cache/index3/id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			PHYS_ID_PATH, cpu_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			PHYS_ID_PATH, cpu_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	fp = fopen(phys_pkg_path, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		perror("Failed to open physical_package_id");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (fscanf(fp, "%d", resource_id) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		perror("Could not get socket number or l3 id");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * get_cache_size - Get cache size for a specified CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * @cpu_no:	CPU number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  * @cache_type:	Cache level L2/L3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)  * @cache_size:	pointer to cache_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  * Return: = 0 on success, < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	char cache_path[1024], cache_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	int length, i, cache_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (!strcmp(cache_type, "L3")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		cache_num = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	} else if (!strcmp(cache_type, "L2")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		cache_num = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		perror("Invalid cache level");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		cpu_no, cache_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	fp = fopen(cache_path, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		perror("Failed to open cache size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (fscanf(fp, "%s", cache_str) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		perror("Could not get cache_size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	length = (int)strlen(cache_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	*cache_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	for (i = 0; i < length; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			*cache_size = *cache_size * 10 + (cache_str[i] - '0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		else if (cache_str[i] == 'K')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			*cache_size = *cache_size * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		else if (cache_str[i] == 'M')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			*cache_size = *cache_size * 1024 * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) #define CORE_SIBLINGS_PATH	"/sys/bus/cpu/devices/cpu"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * get_cbm_mask - Get cbm mask for given cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * @cache_type:	Cache level L2/L3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * @cbm_mask:	cbm_mask returned as a string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * Return: = 0 on success, < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int get_cbm_mask(char *cache_type, char *cbm_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	char cbm_mask_path[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (!cbm_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	sprintf(cbm_mask_path, "%s/%s/cbm_mask", CBM_MASK_PATH, cache_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	fp = fopen(cbm_mask_path, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		perror("Failed to open cache level");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	if (fscanf(fp, "%s", cbm_mask) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		perror("Could not get max cbm_mask");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * get_core_sibling - Get sibling core id from the same socket for given CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * @cpu_no:	CPU number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * Return:	> 0 on success, < 0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int get_core_sibling(int cpu_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	char core_siblings_path[1024], cpu_list_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	int sibling_cpu_no = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		CORE_SIBLINGS_PATH, cpu_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	fp = fopen(core_siblings_path, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		perror("Failed to open core siblings path");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (fscanf(fp, "%s", cpu_list_str) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		perror("Could not get core_siblings list");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	char *token = strtok(cpu_list_str, "-,");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	while (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		sibling_cpu_no = atoi(token);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		/* Skipping core 0 as we don't want to run test on core 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if (sibling_cpu_no != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		token = strtok(NULL, "-,");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return sibling_cpu_no;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * @bm_pid:	PID that should be binded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  * @cpu_no:	CPU number at which the PID would be binded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  * Return: 0 on success, non-zero on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) int taskset_benchmark(pid_t bm_pid, int cpu_no)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	cpu_set_t my_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	CPU_ZERO(&my_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	CPU_SET(cpu_no, &my_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		perror("Unable to taskset benchmark");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * run_benchmark - Run a specified benchmark or fill_buf (default benchmark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  *		   in specified signal. Direct benchmark stdio to /dev/null.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  * @signum:	signal number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * @info:	signal info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * @ucontext:	user context in signal handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * Return: void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) void run_benchmark(int signum, siginfo_t *info, void *ucontext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	int operation, ret, malloc_and_init_memory, memflush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	unsigned long span, buffer_span;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	char **benchmark_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	char resctrl_val[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	benchmark_cmd = info->si_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	 * Direct stdio of child to /dev/null, so that only parent writes to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	 * stdio (console)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	fp = freopen("/dev/null", "w", stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		PARENT_EXIT("Unable to direct benchmark status to /dev/null");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		/* Execute default fill_buf benchmark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		span = strtoul(benchmark_cmd[1], NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		malloc_and_init_memory = atoi(benchmark_cmd[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		memflush =  atoi(benchmark_cmd[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		operation = atoi(benchmark_cmd[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		sprintf(resctrl_val, "%s", benchmark_cmd[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		if (strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 			buffer_span = span * MB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			buffer_span = span;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		if (run_fill_buf(buffer_span, malloc_and_init_memory, memflush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				 operation, resctrl_val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			fprintf(stderr, "Error in running fill buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		/* Execute specified benchmark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		ret = execvp(benchmark_cmd[0], benchmark_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			perror("wrong\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	fclose(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	PARENT_EXIT("Unable to run specified benchmark");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * create_grp - Create a group only if one doesn't exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  * @grp_name:	Name of the group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  * @grp:	Full path and name of the group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  * @parent_grp:	Full path and name of the parent group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  * Return: 0 on success, non-zero on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int found_grp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	struct dirent *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	DIR *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	 * At this point, we are guaranteed to have resctrl FS mounted and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	 * length of grp_name == 0, it means, user wants to use root con_mon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	 * grp, so do nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (strlen(grp_name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	/* Check if requested grp exists or not */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	dp = opendir(parent_grp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (dp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		while ((ep = readdir(dp)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			if (strcmp(ep->d_name, grp_name) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				found_grp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		closedir(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		perror("Unable to open resctrl for group");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	/* Requested grp doesn't exist, hence create it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (found_grp == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		if (mkdir(grp, 0) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			perror("Unable to create group");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static int write_pid_to_tasks(char *tasks, pid_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	fp = fopen(tasks, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		perror("Failed to open tasks file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (fprintf(fp, "%d\n", pid) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		perror("Failed to wr pid to tasks file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)  * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)  * @bm_pid:		PID that should be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)  * @ctrlgrp:		Name of the control monitor group (con_mon grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)  * @mongrp:		Name of the monitor group (mon grp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)  * @resctrl_val:	Resctrl feature (Eg: mbm, mba.. etc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)  * If a con_mon grp is requested, create it and write pid to it, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  * write pid to root con_mon grp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)  * If a mon grp is requested, create it and write pid to it, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)  * pid is not written, this means that pid is in con_mon grp and hence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)  * should consult con_mon grp's mon_data directory for results.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)  * Return: 0 on success, non-zero on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			    char *resctrl_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	char tasks[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (strlen(ctrlgrp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		sprintf(controlgroup, "%s", RESCTRL_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	/* Create control and monitoring group and write pid into it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	sprintf(tasks, "%s/tasks", controlgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	ret = write_pid_to_tasks(tasks, bm_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	/* Create mon grp and write pid into it for "mbm" and "cqm" test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	    !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		if (strlen(mongrp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 			sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			sprintf(tasks, "%s/mon_groups/%s/tasks",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 				controlgroup, mongrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			ret = write_pid_to_tasks(tasks, bm_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	printf("%sok writing benchmark parameters to resctrl FS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	       ret ? "not " : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		perror("# writing to resctrlfs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	tests_run++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * write_schemata - Update schemata of a con_mon grp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * @ctrlgrp:		Name of the con_mon grp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  * @schemata:		Schemata that should be updated to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * @cpu_no:		CPU number that the benchmark PID is binded to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  * @resctrl_val:	Resctrl feature (Eg: mbm, mba.. etc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)  * Update schemata of a con_mon grp *only* if requested resctrl feature is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)  * allocation type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)  * Return: 0 on success, non-zero on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	char controlgroup[1024], schema[1024], reason[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	int resource_id, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	    strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	    strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	if (!schemata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		printf("# Skipping empty schemata update\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	if (get_resource_id(cpu_no, &resource_id) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		sprintf(reason, "Failed to get resource id");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	if (strlen(ctrlgrp) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	    !strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	fp = fopen(controlgroup, "w");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		sprintf(reason, "Failed to open control group");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (fprintf(fp, "%s\n", schema) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		sprintf(reason, "Failed to write schemata in control group");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	printf("%sok Write schema \"%s\" to resctrl FS%s%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	       ret ? "not " : "", schema, ret ? " # " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	       ret ? reason : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	tests_run++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) bool check_resctrlfs_support(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	FILE *inf = fopen("/proc/filesystems", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	DIR *dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	char *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	if (!inf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	res = fgrep(inf, "nodev\tresctrl\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		free(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	fclose(inf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	printf("%sok kernel supports resctrl filesystem\n", ret ? "" : "not ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	tests_run++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	dp = opendir(RESCTRL_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	printf("%sok resctrl mountpoint \"%s\" exists\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	       dp ? "" : "not ", RESCTRL_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	if (dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 		closedir(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	tests_run++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	printf("# resctrl filesystem %s mounted\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	       find_resctrl_mount(NULL) ? "not" : "is");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) char *fgrep(FILE *inf, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	char line[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	int slen = strlen(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	while (!feof(inf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		if (!fgets(line, 256, inf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		if (strncmp(line, str, slen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		return strdup(line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)  * validate_resctrl_feature_request - Check if requested feature is valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)  * @resctrl_val:	Requested feature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)  * Return: 0 on success, non-zero on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) bool validate_resctrl_feature_request(char *resctrl_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	FILE *inf = fopen("/proc/cpuinfo", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	bool found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	char *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	if (!inf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	res = fgrep(inf, "flags");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		char *s = strchr(res, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		found = s && !strstr(s, resctrl_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		free(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	fclose(inf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) int filter_dmesg(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	char line[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	FILE *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	int pipefds[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	ret = pipe(pipefds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		perror("pipe");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	pid = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	if (pid == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		close(pipefds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		dup2(pipefds[1], STDOUT_FILENO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		execlp("dmesg", "dmesg", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		perror("executing dmesg");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	close(pipefds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	fp = fdopen(pipefds[0], "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	if (!fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		perror("fdopen(pipe)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		kill(pid, SIGTERM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	while (fgets(line, 1024, fp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		if (strstr(line, "intel_rdt:"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 			printf("# dmesg: %s", line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		if (strstr(line, "resctrl:"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 			printf("# dmesg: %s", line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	fclose(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	waitpid(pid, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) int validate_bw_report_request(char *bw_report)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	if (strcmp(bw_report, "reads") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	if (strcmp(bw_report, "writes") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	if (strcmp(bw_report, "nt-writes") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		strcpy(bw_report, "writes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	if (strcmp(bw_report, "total") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	fprintf(stderr, "Requested iMC B/W report type unavailable\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		    int group_fd, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 		      group_fd, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) unsigned int count_bits(unsigned long n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	unsigned int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 		count += n & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		n >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }