xref: /linux/tools/testing/selftests/resctrl/resctrl_val.c (revision 2f320911d9fab38597d2a32d91b4f31165e0c9b4)
11d3f0868SSai Praneeth Prakhya // SPDX-License-Identifier: GPL-2.0
21d3f0868SSai Praneeth Prakhya /*
31d3f0868SSai Praneeth Prakhya  * Memory bandwidth monitoring and allocation library
41d3f0868SSai Praneeth Prakhya  *
51d3f0868SSai Praneeth Prakhya  * Copyright (C) 2018 Intel Corporation
61d3f0868SSai Praneeth Prakhya  *
71d3f0868SSai Praneeth Prakhya  * Authors:
81d3f0868SSai Praneeth Prakhya  *    Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
91d3f0868SSai Praneeth Prakhya  *    Fenghua Yu <fenghua.yu@intel.com>
101d3f0868SSai Praneeth Prakhya  */
111d3f0868SSai Praneeth Prakhya #include "resctrl.h"
121d3f0868SSai Praneeth Prakhya 
131d3f0868SSai Praneeth Prakhya #define UNCORE_IMC		"uncore_imc"
141d3f0868SSai Praneeth Prakhya #define READ_FILE_NAME		"events/cas_count_read"
151d3f0868SSai Praneeth Prakhya #define WRITE_FILE_NAME		"events/cas_count_write"
161d3f0868SSai Praneeth Prakhya #define DYN_PMU_PATH		"/sys/bus/event_source/devices"
171d3f0868SSai Praneeth Prakhya #define SCALE			0.00006103515625
181d3f0868SSai Praneeth Prakhya #define MAX_IMCS		20
191d3f0868SSai Praneeth Prakhya #define MAX_TOKENS		5
201d3f0868SSai Praneeth Prakhya #define READ			0
211d3f0868SSai Praneeth Prakhya #define WRITE			1
221d3f0868SSai Praneeth Prakhya #define CON_MON_MBM_LOCAL_BYTES_PATH				\
231d3f0868SSai Praneeth Prakhya 	"%s/%s/mon_groups/%s/mon_data/mon_L3_%02d/mbm_local_bytes"
241d3f0868SSai Praneeth Prakhya 
251d3f0868SSai Praneeth Prakhya #define CON_MBM_LOCAL_BYTES_PATH		\
261d3f0868SSai Praneeth Prakhya 	"%s/%s/mon_data/mon_L3_%02d/mbm_local_bytes"
271d3f0868SSai Praneeth Prakhya 
281d3f0868SSai Praneeth Prakhya #define MON_MBM_LOCAL_BYTES_PATH		\
291d3f0868SSai Praneeth Prakhya 	"%s/mon_groups/%s/mon_data/mon_L3_%02d/mbm_local_bytes"
301d3f0868SSai Praneeth Prakhya 
311d3f0868SSai Praneeth Prakhya #define MBM_LOCAL_BYTES_PATH			\
321d3f0868SSai Praneeth Prakhya 	"%s/mon_data/mon_L3_%02d/mbm_local_bytes"
331d3f0868SSai Praneeth Prakhya 
3478941183SFenghua Yu #define CON_MON_LCC_OCCUP_PATH		\
3578941183SFenghua Yu 	"%s/%s/mon_groups/%s/mon_data/mon_L3_%02d/llc_occupancy"
3678941183SFenghua Yu 
3778941183SFenghua Yu #define CON_LCC_OCCUP_PATH		\
3878941183SFenghua Yu 	"%s/%s/mon_data/mon_L3_%02d/llc_occupancy"
3978941183SFenghua Yu 
4078941183SFenghua Yu #define MON_LCC_OCCUP_PATH		\
4178941183SFenghua Yu 	"%s/mon_groups/%s/mon_data/mon_L3_%02d/llc_occupancy"
4278941183SFenghua Yu 
4378941183SFenghua Yu #define LCC_OCCUP_PATH			\
4478941183SFenghua Yu 	"%s/mon_data/mon_L3_%02d/llc_occupancy"
4578941183SFenghua Yu 
461d3f0868SSai Praneeth Prakhya struct membw_read_format {
471d3f0868SSai Praneeth Prakhya 	__u64 value;         /* The value of the event */
481d3f0868SSai Praneeth Prakhya 	__u64 time_enabled;  /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
491d3f0868SSai Praneeth Prakhya 	__u64 time_running;  /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
501d3f0868SSai Praneeth Prakhya 	__u64 id;            /* if PERF_FORMAT_ID */
511d3f0868SSai Praneeth Prakhya };
521d3f0868SSai Praneeth Prakhya 
531d3f0868SSai Praneeth Prakhya struct imc_counter_config {
541d3f0868SSai Praneeth Prakhya 	__u32 type;
551d3f0868SSai Praneeth Prakhya 	__u64 event;
561d3f0868SSai Praneeth Prakhya 	__u64 umask;
571d3f0868SSai Praneeth Prakhya 	struct perf_event_attr pe;
581d3f0868SSai Praneeth Prakhya 	struct membw_read_format return_value;
591d3f0868SSai Praneeth Prakhya 	int fd;
601d3f0868SSai Praneeth Prakhya };
611d3f0868SSai Praneeth Prakhya 
627f4d257eSSai Praneeth Prakhya static char mbm_total_path[1024];
637f4d257eSSai Praneeth Prakhya static int imcs;
641d3f0868SSai Praneeth Prakhya static struct imc_counter_config imc_counters_config[MAX_IMCS][2];
651d3f0868SSai Praneeth Prakhya 
661d3f0868SSai Praneeth Prakhya void membw_initialize_perf_event_attr(int i, int j)
671d3f0868SSai Praneeth Prakhya {
681d3f0868SSai Praneeth Prakhya 	memset(&imc_counters_config[i][j].pe, 0,
691d3f0868SSai Praneeth Prakhya 	       sizeof(struct perf_event_attr));
701d3f0868SSai Praneeth Prakhya 	imc_counters_config[i][j].pe.type = imc_counters_config[i][j].type;
711d3f0868SSai Praneeth Prakhya 	imc_counters_config[i][j].pe.size = sizeof(struct perf_event_attr);
721d3f0868SSai Praneeth Prakhya 	imc_counters_config[i][j].pe.disabled = 1;
731d3f0868SSai Praneeth Prakhya 	imc_counters_config[i][j].pe.inherit = 1;
741d3f0868SSai Praneeth Prakhya 	imc_counters_config[i][j].pe.exclude_guest = 0;
751d3f0868SSai Praneeth Prakhya 	imc_counters_config[i][j].pe.config =
761d3f0868SSai Praneeth Prakhya 		imc_counters_config[i][j].umask << 8 |
771d3f0868SSai Praneeth Prakhya 		imc_counters_config[i][j].event;
781d3f0868SSai Praneeth Prakhya 	imc_counters_config[i][j].pe.sample_type = PERF_SAMPLE_IDENTIFIER;
791d3f0868SSai Praneeth Prakhya 	imc_counters_config[i][j].pe.read_format =
801d3f0868SSai Praneeth Prakhya 		PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
811d3f0868SSai Praneeth Prakhya }
821d3f0868SSai Praneeth Prakhya 
831d3f0868SSai Praneeth Prakhya void membw_ioctl_perf_event_ioc_reset_enable(int i, int j)
841d3f0868SSai Praneeth Prakhya {
851d3f0868SSai Praneeth Prakhya 	ioctl(imc_counters_config[i][j].fd, PERF_EVENT_IOC_RESET, 0);
861d3f0868SSai Praneeth Prakhya 	ioctl(imc_counters_config[i][j].fd, PERF_EVENT_IOC_ENABLE, 0);
871d3f0868SSai Praneeth Prakhya }
881d3f0868SSai Praneeth Prakhya 
891d3f0868SSai Praneeth Prakhya void membw_ioctl_perf_event_ioc_disable(int i, int j)
901d3f0868SSai Praneeth Prakhya {
911d3f0868SSai Praneeth Prakhya 	ioctl(imc_counters_config[i][j].fd, PERF_EVENT_IOC_DISABLE, 0);
921d3f0868SSai Praneeth Prakhya }
931d3f0868SSai Praneeth Prakhya 
941d3f0868SSai Praneeth Prakhya /*
951d3f0868SSai Praneeth Prakhya  * get_event_and_umask:	Parse config into event and umask
961d3f0868SSai Praneeth Prakhya  * @cas_count_cfg:	Config
971d3f0868SSai Praneeth Prakhya  * @count:		iMC number
981d3f0868SSai Praneeth Prakhya  * @op:			Operation (read/write)
991d3f0868SSai Praneeth Prakhya  */
1001d3f0868SSai Praneeth Prakhya void get_event_and_umask(char *cas_count_cfg, int count, bool op)
1011d3f0868SSai Praneeth Prakhya {
1021d3f0868SSai Praneeth Prakhya 	char *token[MAX_TOKENS];
1031d3f0868SSai Praneeth Prakhya 	int i = 0;
1041d3f0868SSai Praneeth Prakhya 
1051d3f0868SSai Praneeth Prakhya 	strcat(cas_count_cfg, ",");
1061d3f0868SSai Praneeth Prakhya 	token[0] = strtok(cas_count_cfg, "=,");
1071d3f0868SSai Praneeth Prakhya 
1081d3f0868SSai Praneeth Prakhya 	for (i = 1; i < MAX_TOKENS; i++)
1091d3f0868SSai Praneeth Prakhya 		token[i] = strtok(NULL, "=,");
1101d3f0868SSai Praneeth Prakhya 
1111d3f0868SSai Praneeth Prakhya 	for (i = 0; i < MAX_TOKENS; i++) {
1121d3f0868SSai Praneeth Prakhya 		if (!token[i])
1131d3f0868SSai Praneeth Prakhya 			break;
1141d3f0868SSai Praneeth Prakhya 		if (strcmp(token[i], "event") == 0) {
1151d3f0868SSai Praneeth Prakhya 			if (op == READ)
1161d3f0868SSai Praneeth Prakhya 				imc_counters_config[count][READ].event =
1171d3f0868SSai Praneeth Prakhya 				strtol(token[i + 1], NULL, 16);
1181d3f0868SSai Praneeth Prakhya 			else
1191d3f0868SSai Praneeth Prakhya 				imc_counters_config[count][WRITE].event =
1201d3f0868SSai Praneeth Prakhya 				strtol(token[i + 1], NULL, 16);
1211d3f0868SSai Praneeth Prakhya 		}
1221d3f0868SSai Praneeth Prakhya 		if (strcmp(token[i], "umask") == 0) {
1231d3f0868SSai Praneeth Prakhya 			if (op == READ)
1241d3f0868SSai Praneeth Prakhya 				imc_counters_config[count][READ].umask =
1251d3f0868SSai Praneeth Prakhya 				strtol(token[i + 1], NULL, 16);
1261d3f0868SSai Praneeth Prakhya 			else
1271d3f0868SSai Praneeth Prakhya 				imc_counters_config[count][WRITE].umask =
1281d3f0868SSai Praneeth Prakhya 				strtol(token[i + 1], NULL, 16);
1291d3f0868SSai Praneeth Prakhya 		}
1301d3f0868SSai Praneeth Prakhya 	}
1311d3f0868SSai Praneeth Prakhya }
1327f4d257eSSai Praneeth Prakhya 
1337f4d257eSSai Praneeth Prakhya static int open_perf_event(int i, int cpu_no, int j)
1347f4d257eSSai Praneeth Prakhya {
1357f4d257eSSai Praneeth Prakhya 	imc_counters_config[i][j].fd =
1367f4d257eSSai Praneeth Prakhya 		perf_event_open(&imc_counters_config[i][j].pe, -1, cpu_no, -1,
1377f4d257eSSai Praneeth Prakhya 				PERF_FLAG_FD_CLOEXEC);
1387f4d257eSSai Praneeth Prakhya 
1397f4d257eSSai Praneeth Prakhya 	if (imc_counters_config[i][j].fd == -1) {
1407f4d257eSSai Praneeth Prakhya 		fprintf(stderr, "Error opening leader %llx\n",
1417f4d257eSSai Praneeth Prakhya 			imc_counters_config[i][j].pe.config);
1427f4d257eSSai Praneeth Prakhya 
1437f4d257eSSai Praneeth Prakhya 		return -1;
1447f4d257eSSai Praneeth Prakhya 	}
1457f4d257eSSai Praneeth Prakhya 
1467f4d257eSSai Praneeth Prakhya 	return 0;
1477f4d257eSSai Praneeth Prakhya }
1487f4d257eSSai Praneeth Prakhya 
1497f4d257eSSai Praneeth Prakhya /* Get type and config (read and write) of an iMC counter */
1507f4d257eSSai Praneeth Prakhya static int read_from_imc_dir(char *imc_dir, int count)
1517f4d257eSSai Praneeth Prakhya {
1527f4d257eSSai Praneeth Prakhya 	char cas_count_cfg[1024], imc_counter_cfg[1024], imc_counter_type[1024];
1537f4d257eSSai Praneeth Prakhya 	FILE *fp;
1547f4d257eSSai Praneeth Prakhya 
1557f4d257eSSai Praneeth Prakhya 	/* Get type of iMC counter */
1567f4d257eSSai Praneeth Prakhya 	sprintf(imc_counter_type, "%s%s", imc_dir, "type");
1577f4d257eSSai Praneeth Prakhya 	fp = fopen(imc_counter_type, "r");
1587f4d257eSSai Praneeth Prakhya 	if (!fp) {
1597f4d257eSSai Praneeth Prakhya 		perror("Failed to open imc counter type file");
1607f4d257eSSai Praneeth Prakhya 
1617f4d257eSSai Praneeth Prakhya 		return -1;
1627f4d257eSSai Praneeth Prakhya 	}
1637f4d257eSSai Praneeth Prakhya 	if (fscanf(fp, "%u", &imc_counters_config[count][READ].type) <= 0) {
1647f4d257eSSai Praneeth Prakhya 		perror("Could not get imc type");
1657f4d257eSSai Praneeth Prakhya 		fclose(fp);
1667f4d257eSSai Praneeth Prakhya 
1677f4d257eSSai Praneeth Prakhya 		return -1;
1687f4d257eSSai Praneeth Prakhya 	}
1697f4d257eSSai Praneeth Prakhya 	fclose(fp);
1707f4d257eSSai Praneeth Prakhya 
1717f4d257eSSai Praneeth Prakhya 	imc_counters_config[count][WRITE].type =
1727f4d257eSSai Praneeth Prakhya 				imc_counters_config[count][READ].type;
1737f4d257eSSai Praneeth Prakhya 
1747f4d257eSSai Praneeth Prakhya 	/* Get read config */
1757f4d257eSSai Praneeth Prakhya 	sprintf(imc_counter_cfg, "%s%s", imc_dir, READ_FILE_NAME);
1767f4d257eSSai Praneeth Prakhya 	fp = fopen(imc_counter_cfg, "r");
1777f4d257eSSai Praneeth Prakhya 	if (!fp) {
1787f4d257eSSai Praneeth Prakhya 		perror("Failed to open imc config file");
1797f4d257eSSai Praneeth Prakhya 
1807f4d257eSSai Praneeth Prakhya 		return -1;
1817f4d257eSSai Praneeth Prakhya 	}
1827f4d257eSSai Praneeth Prakhya 	if (fscanf(fp, "%s", cas_count_cfg) <= 0) {
1837f4d257eSSai Praneeth Prakhya 		perror("Could not get imc cas count read");
1847f4d257eSSai Praneeth Prakhya 		fclose(fp);
1857f4d257eSSai Praneeth Prakhya 
1867f4d257eSSai Praneeth Prakhya 		return -1;
1877f4d257eSSai Praneeth Prakhya 	}
1887f4d257eSSai Praneeth Prakhya 	fclose(fp);
1897f4d257eSSai Praneeth Prakhya 
1907f4d257eSSai Praneeth Prakhya 	get_event_and_umask(cas_count_cfg, count, READ);
1917f4d257eSSai Praneeth Prakhya 
1927f4d257eSSai Praneeth Prakhya 	/* Get write config */
1937f4d257eSSai Praneeth Prakhya 	sprintf(imc_counter_cfg, "%s%s", imc_dir, WRITE_FILE_NAME);
1947f4d257eSSai Praneeth Prakhya 	fp = fopen(imc_counter_cfg, "r");
1957f4d257eSSai Praneeth Prakhya 	if (!fp) {
1967f4d257eSSai Praneeth Prakhya 		perror("Failed to open imc config file");
1977f4d257eSSai Praneeth Prakhya 
1987f4d257eSSai Praneeth Prakhya 		return -1;
1997f4d257eSSai Praneeth Prakhya 	}
2007f4d257eSSai Praneeth Prakhya 	if  (fscanf(fp, "%s", cas_count_cfg) <= 0) {
2017f4d257eSSai Praneeth Prakhya 		perror("Could not get imc cas count write");
2027f4d257eSSai Praneeth Prakhya 		fclose(fp);
2037f4d257eSSai Praneeth Prakhya 
2047f4d257eSSai Praneeth Prakhya 		return -1;
2057f4d257eSSai Praneeth Prakhya 	}
2067f4d257eSSai Praneeth Prakhya 	fclose(fp);
2077f4d257eSSai Praneeth Prakhya 
2087f4d257eSSai Praneeth Prakhya 	get_event_and_umask(cas_count_cfg, count, WRITE);
2097f4d257eSSai Praneeth Prakhya 
2107f4d257eSSai Praneeth Prakhya 	return 0;
2117f4d257eSSai Praneeth Prakhya }
2127f4d257eSSai Praneeth Prakhya 
2137f4d257eSSai Praneeth Prakhya /*
2147f4d257eSSai Praneeth Prakhya  * A system can have 'n' number of iMC (Integrated Memory Controller)
2157f4d257eSSai Praneeth Prakhya  * counters, get that 'n'. For each iMC counter get it's type and config.
2167f4d257eSSai Praneeth Prakhya  * Also, each counter has two configs, one for read and the other for write.
2177f4d257eSSai Praneeth Prakhya  * A config again has two parts, event and umask.
2187f4d257eSSai Praneeth Prakhya  * Enumerate all these details into an array of structures.
2197f4d257eSSai Praneeth Prakhya  *
2207f4d257eSSai Praneeth Prakhya  * Return: >= 0 on success. < 0 on failure.
2217f4d257eSSai Praneeth Prakhya  */
2227f4d257eSSai Praneeth Prakhya static int num_of_imcs(void)
2237f4d257eSSai Praneeth Prakhya {
2247f4d257eSSai Praneeth Prakhya 	unsigned int count = 0;
2257f4d257eSSai Praneeth Prakhya 	char imc_dir[512];
2267f4d257eSSai Praneeth Prakhya 	struct dirent *ep;
2277f4d257eSSai Praneeth Prakhya 	int ret;
2287f4d257eSSai Praneeth Prakhya 	DIR *dp;
2297f4d257eSSai Praneeth Prakhya 
2307f4d257eSSai Praneeth Prakhya 	dp = opendir(DYN_PMU_PATH);
2317f4d257eSSai Praneeth Prakhya 	if (dp) {
2327f4d257eSSai Praneeth Prakhya 		while ((ep = readdir(dp))) {
2337f4d257eSSai Praneeth Prakhya 			if (strstr(ep->d_name, UNCORE_IMC)) {
2347f4d257eSSai Praneeth Prakhya 				sprintf(imc_dir, "%s/%s/", DYN_PMU_PATH,
2357f4d257eSSai Praneeth Prakhya 					ep->d_name);
2367f4d257eSSai Praneeth Prakhya 				ret = read_from_imc_dir(imc_dir, count);
2377f4d257eSSai Praneeth Prakhya 				if (ret) {
2387f4d257eSSai Praneeth Prakhya 					closedir(dp);
2397f4d257eSSai Praneeth Prakhya 
2407f4d257eSSai Praneeth Prakhya 					return ret;
2417f4d257eSSai Praneeth Prakhya 				}
2427f4d257eSSai Praneeth Prakhya 				count++;
2437f4d257eSSai Praneeth Prakhya 			}
2447f4d257eSSai Praneeth Prakhya 		}
2457f4d257eSSai Praneeth Prakhya 		closedir(dp);
2467f4d257eSSai Praneeth Prakhya 		if (count == 0) {
2477f4d257eSSai Praneeth Prakhya 			perror("Unable find iMC counters!\n");
2487f4d257eSSai Praneeth Prakhya 
2497f4d257eSSai Praneeth Prakhya 			return -1;
2507f4d257eSSai Praneeth Prakhya 		}
2517f4d257eSSai Praneeth Prakhya 	} else {
2527f4d257eSSai Praneeth Prakhya 		perror("Unable to open PMU directory!\n");
2537f4d257eSSai Praneeth Prakhya 
2547f4d257eSSai Praneeth Prakhya 		return -1;
2557f4d257eSSai Praneeth Prakhya 	}
2567f4d257eSSai Praneeth Prakhya 
2577f4d257eSSai Praneeth Prakhya 	return count;
2587f4d257eSSai Praneeth Prakhya }
2597f4d257eSSai Praneeth Prakhya 
2607f4d257eSSai Praneeth Prakhya static int initialize_mem_bw_imc(void)
2617f4d257eSSai Praneeth Prakhya {
2627f4d257eSSai Praneeth Prakhya 	int imc, j;
2637f4d257eSSai Praneeth Prakhya 
2647f4d257eSSai Praneeth Prakhya 	imcs = num_of_imcs();
2657f4d257eSSai Praneeth Prakhya 	if (imcs <= 0)
2667f4d257eSSai Praneeth Prakhya 		return imcs;
2677f4d257eSSai Praneeth Prakhya 
2687f4d257eSSai Praneeth Prakhya 	/* Initialize perf_event_attr structures for all iMC's */
2697f4d257eSSai Praneeth Prakhya 	for (imc = 0; imc < imcs; imc++) {
2707f4d257eSSai Praneeth Prakhya 		for (j = 0; j < 2; j++)
2717f4d257eSSai Praneeth Prakhya 			membw_initialize_perf_event_attr(imc, j);
2727f4d257eSSai Praneeth Prakhya 	}
2737f4d257eSSai Praneeth Prakhya 
2747f4d257eSSai Praneeth Prakhya 	return 0;
2757f4d257eSSai Praneeth Prakhya }
2767f4d257eSSai Praneeth Prakhya 
2777f4d257eSSai Praneeth Prakhya /*
2787f4d257eSSai Praneeth Prakhya  * get_mem_bw_imc:	Memory band width as reported by iMC counters
2797f4d257eSSai Praneeth Prakhya  * @cpu_no:		CPU number that the benchmark PID is binded to
2807f4d257eSSai Praneeth Prakhya  * @bw_report:		Bandwidth report type (reads, writes)
2817f4d257eSSai Praneeth Prakhya  *
2827f4d257eSSai Praneeth Prakhya  * Memory B/W utilized by a process on a socket can be calculated using
2837f4d257eSSai Praneeth Prakhya  * iMC counters. Perf events are used to read these counters.
2847f4d257eSSai Praneeth Prakhya  *
2857f4d257eSSai Praneeth Prakhya  * Return: >= 0 on success. < 0 on failure.
2867f4d257eSSai Praneeth Prakhya  */
2877f4d257eSSai Praneeth Prakhya static float get_mem_bw_imc(int cpu_no, char *bw_report)
2887f4d257eSSai Praneeth Prakhya {
2897f4d257eSSai Praneeth Prakhya 	float reads, writes, of_mul_read, of_mul_write;
2907f4d257eSSai Praneeth Prakhya 	int imc, j, ret;
2917f4d257eSSai Praneeth Prakhya 
2927f4d257eSSai Praneeth Prakhya 	/* Start all iMC counters to log values (both read and write) */
2937f4d257eSSai Praneeth Prakhya 	reads = 0, writes = 0, of_mul_read = 1, of_mul_write = 1;
2947f4d257eSSai Praneeth Prakhya 	for (imc = 0; imc < imcs; imc++) {
2957f4d257eSSai Praneeth Prakhya 		for (j = 0; j < 2; j++) {
2967f4d257eSSai Praneeth Prakhya 			ret = open_perf_event(imc, cpu_no, j);
2977f4d257eSSai Praneeth Prakhya 			if (ret)
2987f4d257eSSai Praneeth Prakhya 				return -1;
2997f4d257eSSai Praneeth Prakhya 		}
3007f4d257eSSai Praneeth Prakhya 		for (j = 0; j < 2; j++)
3017f4d257eSSai Praneeth Prakhya 			membw_ioctl_perf_event_ioc_reset_enable(imc, j);
3027f4d257eSSai Praneeth Prakhya 	}
3037f4d257eSSai Praneeth Prakhya 
3047f4d257eSSai Praneeth Prakhya 	sleep(1);
3057f4d257eSSai Praneeth Prakhya 
3067f4d257eSSai Praneeth Prakhya 	/* Stop counters after a second to get results (both read and write) */
3077f4d257eSSai Praneeth Prakhya 	for (imc = 0; imc < imcs; imc++) {
3087f4d257eSSai Praneeth Prakhya 		for (j = 0; j < 2; j++)
3097f4d257eSSai Praneeth Prakhya 			membw_ioctl_perf_event_ioc_disable(imc, j);
3107f4d257eSSai Praneeth Prakhya 	}
3117f4d257eSSai Praneeth Prakhya 
3127f4d257eSSai Praneeth Prakhya 	/*
3137f4d257eSSai Praneeth Prakhya 	 * Get results which are stored in struct type imc_counter_config
3147f4d257eSSai Praneeth Prakhya 	 * Take over flow into consideration before calculating total b/w
3157f4d257eSSai Praneeth Prakhya 	 */
3167f4d257eSSai Praneeth Prakhya 	for (imc = 0; imc < imcs; imc++) {
3177f4d257eSSai Praneeth Prakhya 		struct imc_counter_config *r =
3187f4d257eSSai Praneeth Prakhya 			&imc_counters_config[imc][READ];
3197f4d257eSSai Praneeth Prakhya 		struct imc_counter_config *w =
3207f4d257eSSai Praneeth Prakhya 			&imc_counters_config[imc][WRITE];
3217f4d257eSSai Praneeth Prakhya 
3227f4d257eSSai Praneeth Prakhya 		if (read(r->fd, &r->return_value,
3237f4d257eSSai Praneeth Prakhya 			 sizeof(struct membw_read_format)) == -1) {
3247f4d257eSSai Praneeth Prakhya 			perror("Couldn't get read b/w through iMC");
3257f4d257eSSai Praneeth Prakhya 
3267f4d257eSSai Praneeth Prakhya 			return -1;
3277f4d257eSSai Praneeth Prakhya 		}
3287f4d257eSSai Praneeth Prakhya 
3297f4d257eSSai Praneeth Prakhya 		if (read(w->fd, &w->return_value,
3307f4d257eSSai Praneeth Prakhya 			 sizeof(struct membw_read_format)) == -1) {
3317f4d257eSSai Praneeth Prakhya 			perror("Couldn't get write bw through iMC");
3327f4d257eSSai Praneeth Prakhya 
3337f4d257eSSai Praneeth Prakhya 			return -1;
3347f4d257eSSai Praneeth Prakhya 		}
3357f4d257eSSai Praneeth Prakhya 
3367f4d257eSSai Praneeth Prakhya 		__u64 r_time_enabled = r->return_value.time_enabled;
3377f4d257eSSai Praneeth Prakhya 		__u64 r_time_running = r->return_value.time_running;
3387f4d257eSSai Praneeth Prakhya 
3397f4d257eSSai Praneeth Prakhya 		if (r_time_enabled != r_time_running)
3407f4d257eSSai Praneeth Prakhya 			of_mul_read = (float)r_time_enabled /
3417f4d257eSSai Praneeth Prakhya 					(float)r_time_running;
3427f4d257eSSai Praneeth Prakhya 
3437f4d257eSSai Praneeth Prakhya 		__u64 w_time_enabled = w->return_value.time_enabled;
3447f4d257eSSai Praneeth Prakhya 		__u64 w_time_running = w->return_value.time_running;
3457f4d257eSSai Praneeth Prakhya 
3467f4d257eSSai Praneeth Prakhya 		if (w_time_enabled != w_time_running)
3477f4d257eSSai Praneeth Prakhya 			of_mul_write = (float)w_time_enabled /
3487f4d257eSSai Praneeth Prakhya 					(float)w_time_running;
3497f4d257eSSai Praneeth Prakhya 		reads += r->return_value.value * of_mul_read * SCALE;
3507f4d257eSSai Praneeth Prakhya 		writes += w->return_value.value * of_mul_write * SCALE;
3517f4d257eSSai Praneeth Prakhya 	}
3527f4d257eSSai Praneeth Prakhya 
3537f4d257eSSai Praneeth Prakhya 	for (imc = 0; imc < imcs; imc++) {
3547f4d257eSSai Praneeth Prakhya 		close(imc_counters_config[imc][READ].fd);
3557f4d257eSSai Praneeth Prakhya 		close(imc_counters_config[imc][WRITE].fd);
3567f4d257eSSai Praneeth Prakhya 	}
3577f4d257eSSai Praneeth Prakhya 
3587f4d257eSSai Praneeth Prakhya 	if (strcmp(bw_report, "reads") == 0)
3597f4d257eSSai Praneeth Prakhya 		return reads;
3607f4d257eSSai Praneeth Prakhya 
3617f4d257eSSai Praneeth Prakhya 	if (strcmp(bw_report, "writes") == 0)
3627f4d257eSSai Praneeth Prakhya 		return writes;
3637f4d257eSSai Praneeth Prakhya 
3647f4d257eSSai Praneeth Prakhya 	return (reads + writes);
3657f4d257eSSai Praneeth Prakhya }
3667f4d257eSSai Praneeth Prakhya 
3677f4d257eSSai Praneeth Prakhya void set_mbm_path(const char *ctrlgrp, const char *mongrp, int resource_id)
3687f4d257eSSai Praneeth Prakhya {
3697f4d257eSSai Praneeth Prakhya 	if (ctrlgrp && mongrp)
3707f4d257eSSai Praneeth Prakhya 		sprintf(mbm_total_path, CON_MON_MBM_LOCAL_BYTES_PATH,
3717f4d257eSSai Praneeth Prakhya 			RESCTRL_PATH, ctrlgrp, mongrp, resource_id);
3727f4d257eSSai Praneeth Prakhya 	else if (!ctrlgrp && mongrp)
3737f4d257eSSai Praneeth Prakhya 		sprintf(mbm_total_path, MON_MBM_LOCAL_BYTES_PATH, RESCTRL_PATH,
3747f4d257eSSai Praneeth Prakhya 			mongrp, resource_id);
3757f4d257eSSai Praneeth Prakhya 	else if (ctrlgrp && !mongrp)
3767f4d257eSSai Praneeth Prakhya 		sprintf(mbm_total_path, CON_MBM_LOCAL_BYTES_PATH, RESCTRL_PATH,
3777f4d257eSSai Praneeth Prakhya 			ctrlgrp, resource_id);
3787f4d257eSSai Praneeth Prakhya 	else if (!ctrlgrp && !mongrp)
3797f4d257eSSai Praneeth Prakhya 		sprintf(mbm_total_path, MBM_LOCAL_BYTES_PATH, RESCTRL_PATH,
3807f4d257eSSai Praneeth Prakhya 			resource_id);
3817f4d257eSSai Praneeth Prakhya }
3827f4d257eSSai Praneeth Prakhya 
3837f4d257eSSai Praneeth Prakhya /*
3847f4d257eSSai Praneeth Prakhya  * initialize_mem_bw_resctrl:	Appropriately populate "mbm_total_path"
3857f4d257eSSai Praneeth Prakhya  * @ctrlgrp:			Name of the control monitor group (con_mon grp)
3867f4d257eSSai Praneeth Prakhya  * @mongrp:			Name of the monitor group (mon grp)
3877f4d257eSSai Praneeth Prakhya  * @cpu_no:			CPU number that the benchmark PID is binded to
3887f4d257eSSai Praneeth Prakhya  * @resctrl_val:		Resctrl feature (Eg: mbm, mba.. etc)
3897f4d257eSSai Praneeth Prakhya  */
3907f4d257eSSai Praneeth Prakhya static void initialize_mem_bw_resctrl(const char *ctrlgrp, const char *mongrp,
3917f4d257eSSai Praneeth Prakhya 				      int cpu_no, char *resctrl_val)
3927f4d257eSSai Praneeth Prakhya {
3937f4d257eSSai Praneeth Prakhya 	int resource_id;
3947f4d257eSSai Praneeth Prakhya 
3957f4d257eSSai Praneeth Prakhya 	if (get_resource_id(cpu_no, &resource_id) < 0) {
3967f4d257eSSai Praneeth Prakhya 		perror("Could not get resource_id");
3977f4d257eSSai Praneeth Prakhya 		return;
3987f4d257eSSai Praneeth Prakhya 	}
3997f4d257eSSai Praneeth Prakhya 
40024286736SFenghua Yu 	if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
4017f4d257eSSai Praneeth Prakhya 		set_mbm_path(ctrlgrp, mongrp, resource_id);
4027f4d257eSSai Praneeth Prakhya 
40324286736SFenghua Yu 	if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
4047f4d257eSSai Praneeth Prakhya 		if (ctrlgrp)
4057f4d257eSSai Praneeth Prakhya 			sprintf(mbm_total_path, CON_MBM_LOCAL_BYTES_PATH,
4067f4d257eSSai Praneeth Prakhya 				RESCTRL_PATH, ctrlgrp, resource_id);
4077f4d257eSSai Praneeth Prakhya 		else
4087f4d257eSSai Praneeth Prakhya 			sprintf(mbm_total_path, MBM_LOCAL_BYTES_PATH,
4097f4d257eSSai Praneeth Prakhya 				RESCTRL_PATH, resource_id);
4107f4d257eSSai Praneeth Prakhya 	}
4117f4d257eSSai Praneeth Prakhya }
4127f4d257eSSai Praneeth Prakhya 
4137f4d257eSSai Praneeth Prakhya /*
4147f4d257eSSai Praneeth Prakhya  * Get MBM Local bytes as reported by resctrl FS
4157f4d257eSSai Praneeth Prakhya  * For MBM,
4167f4d257eSSai Praneeth Prakhya  * 1. If con_mon grp and mon grp are given, then read from con_mon grp's mon grp
4177f4d257eSSai Praneeth Prakhya  * 2. If only con_mon grp is given, then read from con_mon grp
4187f4d257eSSai Praneeth Prakhya  * 3. If both are not given, then read from root con_mon grp
4197f4d257eSSai Praneeth Prakhya  * For MBA,
4207f4d257eSSai Praneeth Prakhya  * 1. If con_mon grp is given, then read from it
4217f4d257eSSai Praneeth Prakhya  * 2. If con_mon grp is not given, then read from root con_mon grp
4227f4d257eSSai Praneeth Prakhya  */
4237f4d257eSSai Praneeth Prakhya static unsigned long get_mem_bw_resctrl(void)
4247f4d257eSSai Praneeth Prakhya {
4257f4d257eSSai Praneeth Prakhya 	unsigned long mbm_total = 0;
4267f4d257eSSai Praneeth Prakhya 	FILE *fp;
4277f4d257eSSai Praneeth Prakhya 
4287f4d257eSSai Praneeth Prakhya 	fp = fopen(mbm_total_path, "r");
4297f4d257eSSai Praneeth Prakhya 	if (!fp) {
4307f4d257eSSai Praneeth Prakhya 		perror("Failed to open total bw file");
4317f4d257eSSai Praneeth Prakhya 
4327f4d257eSSai Praneeth Prakhya 		return -1;
4337f4d257eSSai Praneeth Prakhya 	}
4347f4d257eSSai Praneeth Prakhya 	if (fscanf(fp, "%lu", &mbm_total) <= 0) {
4357f4d257eSSai Praneeth Prakhya 		perror("Could not get mbm local bytes");
4367f4d257eSSai Praneeth Prakhya 		fclose(fp);
4377f4d257eSSai Praneeth Prakhya 
4387f4d257eSSai Praneeth Prakhya 		return -1;
4397f4d257eSSai Praneeth Prakhya 	}
4407f4d257eSSai Praneeth Prakhya 	fclose(fp);
4417f4d257eSSai Praneeth Prakhya 
4427f4d257eSSai Praneeth Prakhya 	return mbm_total;
4437f4d257eSSai Praneeth Prakhya }
4447f4d257eSSai Praneeth Prakhya 
4457f4d257eSSai Praneeth Prakhya pid_t bm_pid, ppid;
4467f4d257eSSai Praneeth Prakhya 
44778941183SFenghua Yu void ctrlc_handler(int signum, siginfo_t *info, void *ptr)
4487f4d257eSSai Praneeth Prakhya {
4497f4d257eSSai Praneeth Prakhya 	kill(bm_pid, SIGKILL);
450ecdbb911SFenghua Yu 	umount_resctrlfs();
451ecdbb911SFenghua Yu 	tests_cleanup();
4527f4d257eSSai Praneeth Prakhya 	printf("Ending\n\n");
4537f4d257eSSai Praneeth Prakhya 
4547f4d257eSSai Praneeth Prakhya 	exit(EXIT_SUCCESS);
4557f4d257eSSai Praneeth Prakhya }
4567f4d257eSSai Praneeth Prakhya 
4577f4d257eSSai Praneeth Prakhya /*
4587f4d257eSSai Praneeth Prakhya  * print_results_bw:	the memory bandwidth results are stored in a file
4597f4d257eSSai Praneeth Prakhya  * @filename:		file that stores the results
4607f4d257eSSai Praneeth Prakhya  * @bm_pid:		child pid that runs benchmark
4617f4d257eSSai Praneeth Prakhya  * @bw_imc:		perf imc counter value
4627f4d257eSSai Praneeth Prakhya  * @bw_resc:		memory bandwidth value
4637f4d257eSSai Praneeth Prakhya  *
4647f4d257eSSai Praneeth Prakhya  * Return:		0 on success. non-zero on failure.
4657f4d257eSSai Praneeth Prakhya  */
4667f4d257eSSai Praneeth Prakhya static int print_results_bw(char *filename,  int bm_pid, float bw_imc,
4677f4d257eSSai Praneeth Prakhya 			    unsigned long bw_resc)
4687f4d257eSSai Praneeth Prakhya {
4697f4d257eSSai Praneeth Prakhya 	unsigned long diff = fabs(bw_imc - bw_resc);
4707f4d257eSSai Praneeth Prakhya 	FILE *fp;
4717f4d257eSSai Praneeth Prakhya 
4727f4d257eSSai Praneeth Prakhya 	if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
4737f4d257eSSai Praneeth Prakhya 		printf("Pid: %d \t Mem_BW_iMC: %f \t ", bm_pid, bw_imc);
4747f4d257eSSai Praneeth Prakhya 		printf("Mem_BW_resc: %lu \t Difference: %lu\n", bw_resc, diff);
4757f4d257eSSai Praneeth Prakhya 	} else {
4767f4d257eSSai Praneeth Prakhya 		fp = fopen(filename, "a");
4777f4d257eSSai Praneeth Prakhya 		if (!fp) {
4787f4d257eSSai Praneeth Prakhya 			perror("Cannot open results file");
4797f4d257eSSai Praneeth Prakhya 
4807f4d257eSSai Praneeth Prakhya 			return errno;
4817f4d257eSSai Praneeth Prakhya 		}
4827f4d257eSSai Praneeth Prakhya 		if (fprintf(fp, "Pid: %d \t Mem_BW_iMC: %f \t Mem_BW_resc: %lu \t Difference: %lu\n",
4837f4d257eSSai Praneeth Prakhya 			    bm_pid, bw_imc, bw_resc, diff) <= 0) {
4847f4d257eSSai Praneeth Prakhya 			fclose(fp);
4857f4d257eSSai Praneeth Prakhya 			perror("Could not log results.");
4867f4d257eSSai Praneeth Prakhya 
4877f4d257eSSai Praneeth Prakhya 			return errno;
4887f4d257eSSai Praneeth Prakhya 		}
4897f4d257eSSai Praneeth Prakhya 		fclose(fp);
4907f4d257eSSai Praneeth Prakhya 	}
4917f4d257eSSai Praneeth Prakhya 
4927f4d257eSSai Praneeth Prakhya 	return 0;
4937f4d257eSSai Praneeth Prakhya }
4947f4d257eSSai Praneeth Prakhya 
495*2f320911SFenghua Yu static void set_cmt_path(const char *ctrlgrp, const char *mongrp, char sock_num)
49678941183SFenghua Yu {
49778941183SFenghua Yu 	if (strlen(ctrlgrp) && strlen(mongrp))
49878941183SFenghua Yu 		sprintf(llc_occup_path,	CON_MON_LCC_OCCUP_PATH,	RESCTRL_PATH,
49978941183SFenghua Yu 			ctrlgrp, mongrp, sock_num);
50078941183SFenghua Yu 	else if (!strlen(ctrlgrp) && strlen(mongrp))
50178941183SFenghua Yu 		sprintf(llc_occup_path,	MON_LCC_OCCUP_PATH, RESCTRL_PATH,
50278941183SFenghua Yu 			mongrp, sock_num);
50378941183SFenghua Yu 	else if (strlen(ctrlgrp) && !strlen(mongrp))
50478941183SFenghua Yu 		sprintf(llc_occup_path,	CON_LCC_OCCUP_PATH, RESCTRL_PATH,
50578941183SFenghua Yu 			ctrlgrp, sock_num);
50678941183SFenghua Yu 	else if (!strlen(ctrlgrp) && !strlen(mongrp))
50778941183SFenghua Yu 		sprintf(llc_occup_path, LCC_OCCUP_PATH,	RESCTRL_PATH, sock_num);
50878941183SFenghua Yu }
50978941183SFenghua Yu 
51078941183SFenghua Yu /*
51178941183SFenghua Yu  * initialize_llc_occu_resctrl:	Appropriately populate "llc_occup_path"
51278941183SFenghua Yu  * @ctrlgrp:			Name of the control monitor group (con_mon grp)
51378941183SFenghua Yu  * @mongrp:			Name of the monitor group (mon grp)
51478941183SFenghua Yu  * @cpu_no:			CPU number that the benchmark PID is binded to
515*2f320911SFenghua Yu  * @resctrl_val:		Resctrl feature (Eg: cat, cmt.. etc)
51678941183SFenghua Yu  */
51778941183SFenghua Yu static void initialize_llc_occu_resctrl(const char *ctrlgrp, const char *mongrp,
51878941183SFenghua Yu 					int cpu_no, char *resctrl_val)
51978941183SFenghua Yu {
52078941183SFenghua Yu 	int resource_id;
52178941183SFenghua Yu 
52278941183SFenghua Yu 	if (get_resource_id(cpu_no, &resource_id) < 0) {
52378941183SFenghua Yu 		perror("# Unable to resource_id");
52478941183SFenghua Yu 		return;
52578941183SFenghua Yu 	}
52678941183SFenghua Yu 
527*2f320911SFenghua Yu 	if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
528*2f320911SFenghua Yu 		set_cmt_path(ctrlgrp, mongrp, resource_id);
52978941183SFenghua Yu }
53078941183SFenghua Yu 
5317f4d257eSSai Praneeth Prakhya static int
5327f4d257eSSai Praneeth Prakhya measure_vals(struct resctrl_val_param *param, unsigned long *bw_resc_start)
5337f4d257eSSai Praneeth Prakhya {
5347f4d257eSSai Praneeth Prakhya 	unsigned long bw_imc, bw_resc, bw_resc_end;
5357f4d257eSSai Praneeth Prakhya 	int ret;
5367f4d257eSSai Praneeth Prakhya 
5377f4d257eSSai Praneeth Prakhya 	/*
5387f4d257eSSai Praneeth Prakhya 	 * Measure memory bandwidth from resctrl and from
5397f4d257eSSai Praneeth Prakhya 	 * another source which is perf imc value or could
5407f4d257eSSai Praneeth Prakhya 	 * be something else if perf imc event is not available.
5417f4d257eSSai Praneeth Prakhya 	 * Compare the two values to validate resctrl value.
5427f4d257eSSai Praneeth Prakhya 	 * It takes 1sec to measure the data.
5437f4d257eSSai Praneeth Prakhya 	 */
5447f4d257eSSai Praneeth Prakhya 	bw_imc = get_mem_bw_imc(param->cpu_no, param->bw_report);
5457f4d257eSSai Praneeth Prakhya 	if (bw_imc <= 0)
5467f4d257eSSai Praneeth Prakhya 		return bw_imc;
5477f4d257eSSai Praneeth Prakhya 
5487f4d257eSSai Praneeth Prakhya 	bw_resc_end = get_mem_bw_resctrl();
5497f4d257eSSai Praneeth Prakhya 	if (bw_resc_end <= 0)
5507f4d257eSSai Praneeth Prakhya 		return bw_resc_end;
5517f4d257eSSai Praneeth Prakhya 
5527f4d257eSSai Praneeth Prakhya 	bw_resc = (bw_resc_end - *bw_resc_start) / MB;
5537f4d257eSSai Praneeth Prakhya 	ret = print_results_bw(param->filename, bm_pid, bw_imc, bw_resc);
5547f4d257eSSai Praneeth Prakhya 	if (ret)
5557f4d257eSSai Praneeth Prakhya 		return ret;
5567f4d257eSSai Praneeth Prakhya 
5577f4d257eSSai Praneeth Prakhya 	*bw_resc_start = bw_resc_end;
5587f4d257eSSai Praneeth Prakhya 
5597f4d257eSSai Praneeth Prakhya 	return 0;
5607f4d257eSSai Praneeth Prakhya }
5617f4d257eSSai Praneeth Prakhya 
5627f4d257eSSai Praneeth Prakhya /*
5637f4d257eSSai Praneeth Prakhya  * resctrl_val:	execute benchmark and measure memory bandwidth on
5647f4d257eSSai Praneeth Prakhya  *			the benchmark
5657f4d257eSSai Praneeth Prakhya  * @benchmark_cmd:	benchmark command and its arguments
5667f4d257eSSai Praneeth Prakhya  * @param:		parameters passed to resctrl_val()
5677f4d257eSSai Praneeth Prakhya  *
5687f4d257eSSai Praneeth Prakhya  * Return:		0 on success. non-zero on failure.
5697f4d257eSSai Praneeth Prakhya  */
5707f4d257eSSai Praneeth Prakhya int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param)
5717f4d257eSSai Praneeth Prakhya {
5727f4d257eSSai Praneeth Prakhya 	char *resctrl_val = param->resctrl_val;
5737f4d257eSSai Praneeth Prakhya 	unsigned long bw_resc_start = 0;
5747f4d257eSSai Praneeth Prakhya 	struct sigaction sigact;
5757f4d257eSSai Praneeth Prakhya 	int ret = 0, pipefd[2];
5767f4d257eSSai Praneeth Prakhya 	char pipe_message = 0;
5777f4d257eSSai Praneeth Prakhya 	union sigval value;
5787f4d257eSSai Praneeth Prakhya 
5797f4d257eSSai Praneeth Prakhya 	if (strcmp(param->filename, "") == 0)
5807f4d257eSSai Praneeth Prakhya 		sprintf(param->filename, "stdio");
5817f4d257eSSai Praneeth Prakhya 
58224286736SFenghua Yu 	if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
58324286736SFenghua Yu 	    !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
5847f4d257eSSai Praneeth Prakhya 		ret = validate_bw_report_request(param->bw_report);
5857f4d257eSSai Praneeth Prakhya 		if (ret)
5867f4d257eSSai Praneeth Prakhya 			return ret;
5877f4d257eSSai Praneeth Prakhya 	}
5887f4d257eSSai Praneeth Prakhya 
5897f4d257eSSai Praneeth Prakhya 	ret = remount_resctrlfs(param->mum_resctrlfs);
5907f4d257eSSai Praneeth Prakhya 	if (ret)
5917f4d257eSSai Praneeth Prakhya 		return ret;
5927f4d257eSSai Praneeth Prakhya 
5937f4d257eSSai Praneeth Prakhya 	/*
5947f4d257eSSai Praneeth Prakhya 	 * If benchmark wasn't successfully started by child, then child should
5957f4d257eSSai Praneeth Prakhya 	 * kill parent, so save parent's pid
5967f4d257eSSai Praneeth Prakhya 	 */
5977f4d257eSSai Praneeth Prakhya 	ppid = getpid();
5987f4d257eSSai Praneeth Prakhya 
5997f4d257eSSai Praneeth Prakhya 	if (pipe(pipefd)) {
6007f4d257eSSai Praneeth Prakhya 		perror("# Unable to create pipe");
6017f4d257eSSai Praneeth Prakhya 
6027f4d257eSSai Praneeth Prakhya 		return -1;
6037f4d257eSSai Praneeth Prakhya 	}
6047f4d257eSSai Praneeth Prakhya 
6057f4d257eSSai Praneeth Prakhya 	/*
6067f4d257eSSai Praneeth Prakhya 	 * Fork to start benchmark, save child's pid so that it can be killed
6077f4d257eSSai Praneeth Prakhya 	 * when needed
6087f4d257eSSai Praneeth Prakhya 	 */
6097f4d257eSSai Praneeth Prakhya 	bm_pid = fork();
6107f4d257eSSai Praneeth Prakhya 	if (bm_pid == -1) {
6117f4d257eSSai Praneeth Prakhya 		perror("# Unable to fork");
6127f4d257eSSai Praneeth Prakhya 
6137f4d257eSSai Praneeth Prakhya 		return -1;
6147f4d257eSSai Praneeth Prakhya 	}
6157f4d257eSSai Praneeth Prakhya 
6167f4d257eSSai Praneeth Prakhya 	if (bm_pid == 0) {
6177f4d257eSSai Praneeth Prakhya 		/*
6187f4d257eSSai Praneeth Prakhya 		 * Mask all signals except SIGUSR1, parent uses SIGUSR1 to
6197f4d257eSSai Praneeth Prakhya 		 * start benchmark
6207f4d257eSSai Praneeth Prakhya 		 */
6217f4d257eSSai Praneeth Prakhya 		sigfillset(&sigact.sa_mask);
6227f4d257eSSai Praneeth Prakhya 		sigdelset(&sigact.sa_mask, SIGUSR1);
6237f4d257eSSai Praneeth Prakhya 
6247f4d257eSSai Praneeth Prakhya 		sigact.sa_sigaction = run_benchmark;
6257f4d257eSSai Praneeth Prakhya 		sigact.sa_flags = SA_SIGINFO;
6267f4d257eSSai Praneeth Prakhya 
6277f4d257eSSai Praneeth Prakhya 		/* Register for "SIGUSR1" signal from parent */
6287f4d257eSSai Praneeth Prakhya 		if (sigaction(SIGUSR1, &sigact, NULL))
6297f4d257eSSai Praneeth Prakhya 			PARENT_EXIT("Can't register child for signal");
6307f4d257eSSai Praneeth Prakhya 
6317f4d257eSSai Praneeth Prakhya 		/* Tell parent that child is ready */
6327f4d257eSSai Praneeth Prakhya 		close(pipefd[0]);
6337f4d257eSSai Praneeth Prakhya 		pipe_message = 1;
6347f4d257eSSai Praneeth Prakhya 		if (write(pipefd[1], &pipe_message, sizeof(pipe_message)) <
6357f4d257eSSai Praneeth Prakhya 		    sizeof(pipe_message)) {
6367f4d257eSSai Praneeth Prakhya 			perror("# failed signaling parent process");
6377f4d257eSSai Praneeth Prakhya 			close(pipefd[1]);
6387f4d257eSSai Praneeth Prakhya 			return -1;
6397f4d257eSSai Praneeth Prakhya 		}
6407f4d257eSSai Praneeth Prakhya 		close(pipefd[1]);
6417f4d257eSSai Praneeth Prakhya 
6427f4d257eSSai Praneeth Prakhya 		/* Suspend child until delivery of "SIGUSR1" from parent */
6437f4d257eSSai Praneeth Prakhya 		sigsuspend(&sigact.sa_mask);
6447f4d257eSSai Praneeth Prakhya 
6457f4d257eSSai Praneeth Prakhya 		PARENT_EXIT("Child is done");
6467f4d257eSSai Praneeth Prakhya 	}
6477f4d257eSSai Praneeth Prakhya 
6487f4d257eSSai Praneeth Prakhya 	printf("# benchmark PID: %d\n", bm_pid);
6497f4d257eSSai Praneeth Prakhya 
6507f4d257eSSai Praneeth Prakhya 	/*
6517f4d257eSSai Praneeth Prakhya 	 * Register CTRL-C handler for parent, as it has to kill benchmark
6527f4d257eSSai Praneeth Prakhya 	 * before exiting
6537f4d257eSSai Praneeth Prakhya 	 */
6547f4d257eSSai Praneeth Prakhya 	sigact.sa_sigaction = ctrlc_handler;
6557f4d257eSSai Praneeth Prakhya 	sigemptyset(&sigact.sa_mask);
6567f4d257eSSai Praneeth Prakhya 	sigact.sa_flags = SA_SIGINFO;
6577f4d257eSSai Praneeth Prakhya 	if (sigaction(SIGINT, &sigact, NULL) ||
6587f4d257eSSai Praneeth Prakhya 	    sigaction(SIGHUP, &sigact, NULL)) {
6597f4d257eSSai Praneeth Prakhya 		perror("# sigaction");
6607f4d257eSSai Praneeth Prakhya 		ret = errno;
6617f4d257eSSai Praneeth Prakhya 		goto out;
6627f4d257eSSai Praneeth Prakhya 	}
6637f4d257eSSai Praneeth Prakhya 
6647f4d257eSSai Praneeth Prakhya 	value.sival_ptr = benchmark_cmd;
6657f4d257eSSai Praneeth Prakhya 
6667f4d257eSSai Praneeth Prakhya 	/* Taskset benchmark to specified cpu */
6677f4d257eSSai Praneeth Prakhya 	ret = taskset_benchmark(bm_pid, param->cpu_no);
6687f4d257eSSai Praneeth Prakhya 	if (ret)
6697f4d257eSSai Praneeth Prakhya 		goto out;
6707f4d257eSSai Praneeth Prakhya 
6717f4d257eSSai Praneeth Prakhya 	/* Write benchmark to specified control&monitoring grp in resctrl FS */
6727f4d257eSSai Praneeth Prakhya 	ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
6737f4d257eSSai Praneeth Prakhya 				      resctrl_val);
6747f4d257eSSai Praneeth Prakhya 	if (ret)
6757f4d257eSSai Praneeth Prakhya 		goto out;
6767f4d257eSSai Praneeth Prakhya 
67724286736SFenghua Yu 	if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
67824286736SFenghua Yu 	    !strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
6797f4d257eSSai Praneeth Prakhya 		ret = initialize_mem_bw_imc();
6807f4d257eSSai Praneeth Prakhya 		if (ret)
6817f4d257eSSai Praneeth Prakhya 			goto out;
6827f4d257eSSai Praneeth Prakhya 
6837f4d257eSSai Praneeth Prakhya 		initialize_mem_bw_resctrl(param->ctrlgrp, param->mongrp,
6847f4d257eSSai Praneeth Prakhya 					  param->cpu_no, resctrl_val);
685*2f320911SFenghua Yu 	} else if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
68678941183SFenghua Yu 		initialize_llc_occu_resctrl(param->ctrlgrp, param->mongrp,
68778941183SFenghua Yu 					    param->cpu_no, resctrl_val);
6887f4d257eSSai Praneeth Prakhya 
6897f4d257eSSai Praneeth Prakhya 	/* Parent waits for child to be ready. */
6907f4d257eSSai Praneeth Prakhya 	close(pipefd[1]);
6917f4d257eSSai Praneeth Prakhya 	while (pipe_message != 1) {
6927f4d257eSSai Praneeth Prakhya 		if (read(pipefd[0], &pipe_message, sizeof(pipe_message)) <
6937f4d257eSSai Praneeth Prakhya 		    sizeof(pipe_message)) {
6947f4d257eSSai Praneeth Prakhya 			perror("# failed reading message from child process");
6957f4d257eSSai Praneeth Prakhya 			close(pipefd[0]);
6967f4d257eSSai Praneeth Prakhya 			goto out;
6977f4d257eSSai Praneeth Prakhya 		}
6987f4d257eSSai Praneeth Prakhya 	}
6997f4d257eSSai Praneeth Prakhya 	close(pipefd[0]);
7007f4d257eSSai Praneeth Prakhya 
7017f4d257eSSai Praneeth Prakhya 	/* Signal child to start benchmark */
7027f4d257eSSai Praneeth Prakhya 	if (sigqueue(bm_pid, SIGUSR1, value) == -1) {
7037f4d257eSSai Praneeth Prakhya 		perror("# sigqueue SIGUSR1 to child");
7047f4d257eSSai Praneeth Prakhya 		ret = errno;
7057f4d257eSSai Praneeth Prakhya 		goto out;
7067f4d257eSSai Praneeth Prakhya 	}
7077f4d257eSSai Praneeth Prakhya 
7087f4d257eSSai Praneeth Prakhya 	/* Give benchmark enough time to fully run */
7097f4d257eSSai Praneeth Prakhya 	sleep(1);
7107f4d257eSSai Praneeth Prakhya 
7117f4d257eSSai Praneeth Prakhya 	/* Test runs until the callback setup() tells the test to stop. */
7127f4d257eSSai Praneeth Prakhya 	while (1) {
71324286736SFenghua Yu 		if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
71424286736SFenghua Yu 		    !strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
7157f4d257eSSai Praneeth Prakhya 			ret = param->setup(1, param);
7167f4d257eSSai Praneeth Prakhya 			if (ret) {
7177f4d257eSSai Praneeth Prakhya 				ret = 0;
7187f4d257eSSai Praneeth Prakhya 				break;
7197f4d257eSSai Praneeth Prakhya 			}
7207f4d257eSSai Praneeth Prakhya 
7217f4d257eSSai Praneeth Prakhya 			ret = measure_vals(param, &bw_resc_start);
7227f4d257eSSai Praneeth Prakhya 			if (ret)
7237f4d257eSSai Praneeth Prakhya 				break;
724*2f320911SFenghua Yu 		} else if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) {
7257f4d257eSSai Praneeth Prakhya 			ret = param->setup(1, param);
7267f4d257eSSai Praneeth Prakhya 			if (ret) {
7277f4d257eSSai Praneeth Prakhya 				ret = 0;
7287f4d257eSSai Praneeth Prakhya 				break;
7297f4d257eSSai Praneeth Prakhya 			}
73078941183SFenghua Yu 			sleep(1);
73178941183SFenghua Yu 			ret = measure_cache_vals(param, bm_pid);
7327f4d257eSSai Praneeth Prakhya 			if (ret)
7337f4d257eSSai Praneeth Prakhya 				break;
7347f4d257eSSai Praneeth Prakhya 		} else {
7357f4d257eSSai Praneeth Prakhya 			break;
7367f4d257eSSai Praneeth Prakhya 		}
7377f4d257eSSai Praneeth Prakhya 	}
7387f4d257eSSai Praneeth Prakhya 
7397f4d257eSSai Praneeth Prakhya out:
7407f4d257eSSai Praneeth Prakhya 	kill(bm_pid, SIGKILL);
7417f4d257eSSai Praneeth Prakhya 	umount_resctrlfs();
7427f4d257eSSai Praneeth Prakhya 
7437f4d257eSSai Praneeth Prakhya 	return ret;
7447f4d257eSSai Praneeth Prakhya }
745