xref: /linux/tools/testing/selftests/resctrl/resctrlfs.c (revision 2428673638ea28fa93d2a38b1c3e8d70122b00ee)
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 int tests_run;
14591a6e85SSai Praneeth Prakhya 
15591a6e85SSai Praneeth Prakhya static int find_resctrl_mount(char *buffer)
16591a6e85SSai Praneeth Prakhya {
17591a6e85SSai Praneeth Prakhya 	FILE *mounts;
18591a6e85SSai Praneeth Prakhya 	char line[256], *fs, *mntpoint;
19591a6e85SSai Praneeth Prakhya 
20591a6e85SSai Praneeth Prakhya 	mounts = fopen("/proc/mounts", "r");
21591a6e85SSai Praneeth Prakhya 	if (!mounts) {
22591a6e85SSai Praneeth Prakhya 		perror("/proc/mounts");
23591a6e85SSai Praneeth Prakhya 		return -ENXIO;
24591a6e85SSai Praneeth Prakhya 	}
25591a6e85SSai Praneeth Prakhya 	while (!feof(mounts)) {
26591a6e85SSai Praneeth Prakhya 		if (!fgets(line, 256, mounts))
27591a6e85SSai Praneeth Prakhya 			break;
28591a6e85SSai Praneeth Prakhya 		fs = strtok(line, " \t");
29591a6e85SSai Praneeth Prakhya 		if (!fs)
30591a6e85SSai Praneeth Prakhya 			continue;
31591a6e85SSai Praneeth Prakhya 		mntpoint = strtok(NULL, " \t");
32591a6e85SSai Praneeth Prakhya 		if (!mntpoint)
33591a6e85SSai Praneeth Prakhya 			continue;
34591a6e85SSai Praneeth Prakhya 		fs = strtok(NULL, " \t");
35591a6e85SSai Praneeth Prakhya 		if (!fs)
36591a6e85SSai Praneeth Prakhya 			continue;
37591a6e85SSai Praneeth Prakhya 		if (strcmp(fs, "resctrl"))
38591a6e85SSai Praneeth Prakhya 			continue;
39591a6e85SSai Praneeth Prakhya 
40591a6e85SSai Praneeth Prakhya 		fclose(mounts);
41591a6e85SSai Praneeth Prakhya 		if (buffer)
42591a6e85SSai Praneeth Prakhya 			strncpy(buffer, mntpoint, 256);
43591a6e85SSai Praneeth Prakhya 
44591a6e85SSai Praneeth Prakhya 		return 0;
45591a6e85SSai Praneeth Prakhya 	}
46591a6e85SSai Praneeth Prakhya 
47591a6e85SSai Praneeth Prakhya 	fclose(mounts);
48591a6e85SSai Praneeth Prakhya 
49591a6e85SSai Praneeth Prakhya 	return -ENOENT;
50591a6e85SSai Praneeth Prakhya }
51591a6e85SSai Praneeth Prakhya 
52591a6e85SSai Praneeth Prakhya /*
53591a6e85SSai Praneeth Prakhya  * remount_resctrlfs - Remount resctrl FS at /sys/fs/resctrl
54591a6e85SSai Praneeth Prakhya  * @mum_resctrlfs:	Should the resctrl FS be remounted?
55591a6e85SSai Praneeth Prakhya  *
56591a6e85SSai Praneeth Prakhya  * If not mounted, mount it.
57591a6e85SSai Praneeth Prakhya  * If mounted and mum_resctrlfs then remount resctrl FS.
58591a6e85SSai Praneeth Prakhya  * If mounted and !mum_resctrlfs then noop
59591a6e85SSai Praneeth Prakhya  *
60591a6e85SSai Praneeth Prakhya  * Return: 0 on success, non-zero on failure
61591a6e85SSai Praneeth Prakhya  */
62591a6e85SSai Praneeth Prakhya int remount_resctrlfs(bool mum_resctrlfs)
63591a6e85SSai Praneeth Prakhya {
64591a6e85SSai Praneeth Prakhya 	char mountpoint[256];
65591a6e85SSai Praneeth Prakhya 	int ret;
66591a6e85SSai Praneeth Prakhya 
67591a6e85SSai Praneeth Prakhya 	ret = find_resctrl_mount(mountpoint);
68591a6e85SSai Praneeth Prakhya 	if (ret)
69591a6e85SSai Praneeth Prakhya 		strcpy(mountpoint, RESCTRL_PATH);
70591a6e85SSai Praneeth Prakhya 
71591a6e85SSai Praneeth Prakhya 	if (!ret && mum_resctrlfs && umount(mountpoint)) {
72591a6e85SSai Praneeth Prakhya 		printf("not ok unmounting \"%s\"\n", mountpoint);
73591a6e85SSai Praneeth Prakhya 		perror("# umount");
74591a6e85SSai Praneeth Prakhya 		tests_run++;
75591a6e85SSai Praneeth Prakhya 	}
76591a6e85SSai Praneeth Prakhya 
77591a6e85SSai Praneeth Prakhya 	if (!ret && !mum_resctrlfs)
78591a6e85SSai Praneeth Prakhya 		return 0;
79591a6e85SSai Praneeth Prakhya 
80591a6e85SSai Praneeth Prakhya 	ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
81591a6e85SSai Praneeth Prakhya 	printf("%sok mounting resctrl to \"%s\"\n", ret ? "not " : "",
82591a6e85SSai Praneeth Prakhya 	       RESCTRL_PATH);
83591a6e85SSai Praneeth Prakhya 	if (ret)
84591a6e85SSai Praneeth Prakhya 		perror("# mount");
85591a6e85SSai Praneeth Prakhya 
86591a6e85SSai Praneeth Prakhya 	tests_run++;
87591a6e85SSai Praneeth Prakhya 
88591a6e85SSai Praneeth Prakhya 	return ret;
89591a6e85SSai Praneeth Prakhya }
90591a6e85SSai Praneeth Prakhya 
91591a6e85SSai Praneeth Prakhya int umount_resctrlfs(void)
92591a6e85SSai Praneeth Prakhya {
93591a6e85SSai Praneeth Prakhya 	if (umount(RESCTRL_PATH)) {
94591a6e85SSai Praneeth Prakhya 		perror("# Unable to umount resctrl");
95591a6e85SSai Praneeth Prakhya 
96591a6e85SSai Praneeth Prakhya 		return errno;
97591a6e85SSai Praneeth Prakhya 	}
98591a6e85SSai Praneeth Prakhya 
99591a6e85SSai Praneeth Prakhya 	return 0;
100591a6e85SSai Praneeth Prakhya }
101591a6e85SSai Praneeth Prakhya 
102591a6e85SSai Praneeth Prakhya /*
103591a6e85SSai Praneeth Prakhya  * get_resource_id - Get socket number/l3 id for a specified CPU
104591a6e85SSai Praneeth Prakhya  * @cpu_no:	CPU number
105591a6e85SSai Praneeth Prakhya  * @resource_id: Socket number or l3_id
106591a6e85SSai Praneeth Prakhya  *
107591a6e85SSai Praneeth Prakhya  * Return: >= 0 on success, < 0 on failure.
108591a6e85SSai Praneeth Prakhya  */
109591a6e85SSai Praneeth Prakhya int get_resource_id(int cpu_no, int *resource_id)
110591a6e85SSai Praneeth Prakhya {
111591a6e85SSai Praneeth Prakhya 	char phys_pkg_path[1024];
112591a6e85SSai Praneeth Prakhya 	FILE *fp;
113591a6e85SSai Praneeth Prakhya 
114c0327e1dSBabu Moger 	if (is_amd)
115c0327e1dSBabu Moger 		sprintf(phys_pkg_path, "%s%d/cache/index3/id",
116c0327e1dSBabu Moger 			PHYS_ID_PATH, cpu_no);
117c0327e1dSBabu Moger 	else
118591a6e85SSai Praneeth Prakhya 		sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
119591a6e85SSai Praneeth Prakhya 			PHYS_ID_PATH, cpu_no);
120c0327e1dSBabu Moger 
121591a6e85SSai Praneeth Prakhya 	fp = fopen(phys_pkg_path, "r");
122591a6e85SSai Praneeth Prakhya 	if (!fp) {
123591a6e85SSai Praneeth Prakhya 		perror("Failed to open physical_package_id");
124591a6e85SSai Praneeth Prakhya 
125591a6e85SSai Praneeth Prakhya 		return -1;
126591a6e85SSai Praneeth Prakhya 	}
127591a6e85SSai Praneeth Prakhya 	if (fscanf(fp, "%d", resource_id) <= 0) {
128591a6e85SSai Praneeth Prakhya 		perror("Could not get socket number or l3 id");
129591a6e85SSai Praneeth Prakhya 		fclose(fp);
130591a6e85SSai Praneeth Prakhya 
131591a6e85SSai Praneeth Prakhya 		return -1;
132591a6e85SSai Praneeth Prakhya 	}
133591a6e85SSai Praneeth Prakhya 	fclose(fp);
134591a6e85SSai Praneeth Prakhya 
135591a6e85SSai Praneeth Prakhya 	return 0;
136591a6e85SSai Praneeth Prakhya }
137591a6e85SSai Praneeth Prakhya 
138591a6e85SSai Praneeth Prakhya /*
13978941183SFenghua Yu  * get_cache_size - Get cache size for a specified CPU
14078941183SFenghua Yu  * @cpu_no:	CPU number
14178941183SFenghua Yu  * @cache_type:	Cache level L2/L3
14278941183SFenghua Yu  * @cache_size:	pointer to cache_size
14378941183SFenghua Yu  *
14478941183SFenghua Yu  * Return: = 0 on success, < 0 on failure.
14578941183SFenghua Yu  */
14678941183SFenghua Yu int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size)
14778941183SFenghua Yu {
14878941183SFenghua Yu 	char cache_path[1024], cache_str[64];
14978941183SFenghua Yu 	int length, i, cache_num;
15078941183SFenghua Yu 	FILE *fp;
15178941183SFenghua Yu 
15278941183SFenghua Yu 	if (!strcmp(cache_type, "L3")) {
15378941183SFenghua Yu 		cache_num = 3;
15478941183SFenghua Yu 	} else if (!strcmp(cache_type, "L2")) {
15578941183SFenghua Yu 		cache_num = 2;
15678941183SFenghua Yu 	} else {
15778941183SFenghua Yu 		perror("Invalid cache level");
15878941183SFenghua Yu 		return -1;
15978941183SFenghua Yu 	}
16078941183SFenghua Yu 
16178941183SFenghua Yu 	sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
16278941183SFenghua Yu 		cpu_no, cache_num);
16378941183SFenghua Yu 	fp = fopen(cache_path, "r");
16478941183SFenghua Yu 	if (!fp) {
16578941183SFenghua Yu 		perror("Failed to open cache size");
16678941183SFenghua Yu 
16778941183SFenghua Yu 		return -1;
16878941183SFenghua Yu 	}
16978941183SFenghua Yu 	if (fscanf(fp, "%s", cache_str) <= 0) {
17078941183SFenghua Yu 		perror("Could not get cache_size");
17178941183SFenghua Yu 		fclose(fp);
17278941183SFenghua Yu 
17378941183SFenghua Yu 		return -1;
17478941183SFenghua Yu 	}
17578941183SFenghua Yu 	fclose(fp);
17678941183SFenghua Yu 
17778941183SFenghua Yu 	length = (int)strlen(cache_str);
17878941183SFenghua Yu 
17978941183SFenghua Yu 	*cache_size = 0;
18078941183SFenghua Yu 
18178941183SFenghua Yu 	for (i = 0; i < length; i++) {
18278941183SFenghua Yu 		if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
18378941183SFenghua Yu 
18478941183SFenghua Yu 			*cache_size = *cache_size * 10 + (cache_str[i] - '0');
18578941183SFenghua Yu 
18678941183SFenghua Yu 		else if (cache_str[i] == 'K')
18778941183SFenghua Yu 
18878941183SFenghua Yu 			*cache_size = *cache_size * 1024;
18978941183SFenghua Yu 
19078941183SFenghua Yu 		else if (cache_str[i] == 'M')
19178941183SFenghua Yu 
19278941183SFenghua Yu 			*cache_size = *cache_size * 1024 * 1024;
19378941183SFenghua Yu 
19478941183SFenghua Yu 		else
19578941183SFenghua Yu 			break;
19678941183SFenghua Yu 	}
19778941183SFenghua Yu 
19878941183SFenghua Yu 	return 0;
19978941183SFenghua Yu }
20078941183SFenghua Yu 
20178941183SFenghua Yu #define CORE_SIBLINGS_PATH	"/sys/bus/cpu/devices/cpu"
20278941183SFenghua Yu 
20378941183SFenghua Yu /*
20478941183SFenghua Yu  * get_cbm_mask - Get cbm mask for given cache
20578941183SFenghua Yu  * @cache_type:	Cache level L2/L3
2068236c51dSFenghua Yu  * @cbm_mask:	cbm_mask returned as a string
20778941183SFenghua Yu  *
20878941183SFenghua Yu  * Return: = 0 on success, < 0 on failure.
20978941183SFenghua Yu  */
2108236c51dSFenghua Yu int get_cbm_mask(char *cache_type, char *cbm_mask)
21178941183SFenghua Yu {
21278941183SFenghua Yu 	char cbm_mask_path[1024];
21378941183SFenghua Yu 	FILE *fp;
21478941183SFenghua Yu 
2158236c51dSFenghua Yu 	if (!cbm_mask)
2168236c51dSFenghua Yu 		return -1;
2178236c51dSFenghua Yu 
21878941183SFenghua Yu 	sprintf(cbm_mask_path, "%s/%s/cbm_mask", CBM_MASK_PATH, cache_type);
21978941183SFenghua Yu 
22078941183SFenghua Yu 	fp = fopen(cbm_mask_path, "r");
22178941183SFenghua Yu 	if (!fp) {
22278941183SFenghua Yu 		perror("Failed to open cache level");
22378941183SFenghua Yu 
22478941183SFenghua Yu 		return -1;
22578941183SFenghua Yu 	}
22678941183SFenghua Yu 	if (fscanf(fp, "%s", cbm_mask) <= 0) {
22778941183SFenghua Yu 		perror("Could not get max cbm_mask");
22878941183SFenghua Yu 		fclose(fp);
22978941183SFenghua Yu 
23078941183SFenghua Yu 		return -1;
23178941183SFenghua Yu 	}
23278941183SFenghua Yu 	fclose(fp);
23378941183SFenghua Yu 
23478941183SFenghua Yu 	return 0;
23578941183SFenghua Yu }
23678941183SFenghua Yu 
23778941183SFenghua Yu /*
23878941183SFenghua Yu  * get_core_sibling - Get sibling core id from the same socket for given CPU
23978941183SFenghua Yu  * @cpu_no:	CPU number
24078941183SFenghua Yu  *
24178941183SFenghua Yu  * Return:	> 0 on success, < 0 on failure.
24278941183SFenghua Yu  */
24378941183SFenghua Yu int get_core_sibling(int cpu_no)
24478941183SFenghua Yu {
24578941183SFenghua Yu 	char core_siblings_path[1024], cpu_list_str[64];
24678941183SFenghua Yu 	int sibling_cpu_no = -1;
24778941183SFenghua Yu 	FILE *fp;
24878941183SFenghua Yu 
24978941183SFenghua Yu 	sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
25078941183SFenghua Yu 		CORE_SIBLINGS_PATH, cpu_no);
25178941183SFenghua Yu 
25278941183SFenghua Yu 	fp = fopen(core_siblings_path, "r");
25378941183SFenghua Yu 	if (!fp) {
25478941183SFenghua Yu 		perror("Failed to open core siblings path");
25578941183SFenghua Yu 
25678941183SFenghua Yu 		return -1;
25778941183SFenghua Yu 	}
25878941183SFenghua Yu 	if (fscanf(fp, "%s", cpu_list_str) <= 0) {
25978941183SFenghua Yu 		perror("Could not get core_siblings list");
26078941183SFenghua Yu 		fclose(fp);
26178941183SFenghua Yu 
26278941183SFenghua Yu 		return -1;
26378941183SFenghua Yu 	}
26478941183SFenghua Yu 	fclose(fp);
26578941183SFenghua Yu 
26678941183SFenghua Yu 	char *token = strtok(cpu_list_str, "-,");
26778941183SFenghua Yu 
26878941183SFenghua Yu 	while (token) {
26978941183SFenghua Yu 		sibling_cpu_no = atoi(token);
27078941183SFenghua Yu 		/* Skipping core 0 as we don't want to run test on core 0 */
27178941183SFenghua Yu 		if (sibling_cpu_no != 0)
27278941183SFenghua Yu 			break;
27378941183SFenghua Yu 		token = strtok(NULL, "-,");
27478941183SFenghua Yu 	}
27578941183SFenghua Yu 
27678941183SFenghua Yu 	return sibling_cpu_no;
27778941183SFenghua Yu }
27878941183SFenghua Yu 
27978941183SFenghua Yu /*
280591a6e85SSai Praneeth Prakhya  * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
281591a6e85SSai Praneeth Prakhya  * @bm_pid:	PID that should be binded
282591a6e85SSai Praneeth Prakhya  * @cpu_no:	CPU number at which the PID would be binded
283591a6e85SSai Praneeth Prakhya  *
284591a6e85SSai Praneeth Prakhya  * Return: 0 on success, non-zero on failure
285591a6e85SSai Praneeth Prakhya  */
286591a6e85SSai Praneeth Prakhya int taskset_benchmark(pid_t bm_pid, int cpu_no)
287591a6e85SSai Praneeth Prakhya {
288591a6e85SSai Praneeth Prakhya 	cpu_set_t my_set;
289591a6e85SSai Praneeth Prakhya 
290591a6e85SSai Praneeth Prakhya 	CPU_ZERO(&my_set);
291591a6e85SSai Praneeth Prakhya 	CPU_SET(cpu_no, &my_set);
292591a6e85SSai Praneeth Prakhya 
293591a6e85SSai Praneeth Prakhya 	if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
294591a6e85SSai Praneeth Prakhya 		perror("Unable to taskset benchmark");
295591a6e85SSai Praneeth Prakhya 
296591a6e85SSai Praneeth Prakhya 		return -1;
297591a6e85SSai Praneeth Prakhya 	}
298591a6e85SSai Praneeth Prakhya 
299591a6e85SSai Praneeth Prakhya 	return 0;
300591a6e85SSai Praneeth Prakhya }
301591a6e85SSai Praneeth Prakhya 
302591a6e85SSai Praneeth Prakhya /*
303591a6e85SSai Praneeth Prakhya  * run_benchmark - Run a specified benchmark or fill_buf (default benchmark)
304591a6e85SSai Praneeth Prakhya  *		   in specified signal. Direct benchmark stdio to /dev/null.
305591a6e85SSai Praneeth Prakhya  * @signum:	signal number
306591a6e85SSai Praneeth Prakhya  * @info:	signal info
307591a6e85SSai Praneeth Prakhya  * @ucontext:	user context in signal handling
308591a6e85SSai Praneeth Prakhya  *
309591a6e85SSai Praneeth Prakhya  * Return: void
310591a6e85SSai Praneeth Prakhya  */
311591a6e85SSai Praneeth Prakhya void run_benchmark(int signum, siginfo_t *info, void *ucontext)
312591a6e85SSai Praneeth Prakhya {
31378941183SFenghua Yu 	int operation, ret, malloc_and_init_memory, memflush;
31478941183SFenghua Yu 	unsigned long span, buffer_span;
315591a6e85SSai Praneeth Prakhya 	char **benchmark_cmd;
31678941183SFenghua Yu 	char resctrl_val[64];
317591a6e85SSai Praneeth Prakhya 	FILE *fp;
318591a6e85SSai Praneeth Prakhya 
319591a6e85SSai Praneeth Prakhya 	benchmark_cmd = info->si_ptr;
320591a6e85SSai Praneeth Prakhya 
321591a6e85SSai Praneeth Prakhya 	/*
322591a6e85SSai Praneeth Prakhya 	 * Direct stdio of child to /dev/null, so that only parent writes to
323591a6e85SSai Praneeth Prakhya 	 * stdio (console)
324591a6e85SSai Praneeth Prakhya 	 */
325591a6e85SSai Praneeth Prakhya 	fp = freopen("/dev/null", "w", stdout);
326591a6e85SSai Praneeth Prakhya 	if (!fp)
327591a6e85SSai Praneeth Prakhya 		PARENT_EXIT("Unable to direct benchmark status to /dev/null");
328591a6e85SSai Praneeth Prakhya 
329591a6e85SSai Praneeth Prakhya 	if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
330591a6e85SSai Praneeth Prakhya 		/* Execute default fill_buf benchmark */
331591a6e85SSai Praneeth Prakhya 		span = strtoul(benchmark_cmd[1], NULL, 10);
33278941183SFenghua Yu 		malloc_and_init_memory = atoi(benchmark_cmd[2]);
33378941183SFenghua Yu 		memflush =  atoi(benchmark_cmd[3]);
334591a6e85SSai Praneeth Prakhya 		operation = atoi(benchmark_cmd[4]);
33578941183SFenghua Yu 		sprintf(resctrl_val, "%s", benchmark_cmd[5]);
33678941183SFenghua Yu 
337*24286736SFenghua Yu 		if (strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
33878941183SFenghua Yu 			buffer_span = span * MB;
33978941183SFenghua Yu 		else
34078941183SFenghua Yu 			buffer_span = span;
34178941183SFenghua Yu 
34278941183SFenghua Yu 		if (run_fill_buf(buffer_span, malloc_and_init_memory, memflush,
34378941183SFenghua Yu 				 operation, resctrl_val))
344591a6e85SSai Praneeth Prakhya 			fprintf(stderr, "Error in running fill buffer\n");
345591a6e85SSai Praneeth Prakhya 	} else {
346591a6e85SSai Praneeth Prakhya 		/* Execute specified benchmark */
347591a6e85SSai Praneeth Prakhya 		ret = execvp(benchmark_cmd[0], benchmark_cmd);
348591a6e85SSai Praneeth Prakhya 		if (ret)
349591a6e85SSai Praneeth Prakhya 			perror("wrong\n");
350591a6e85SSai Praneeth Prakhya 	}
351591a6e85SSai Praneeth Prakhya 
352591a6e85SSai Praneeth Prakhya 	fclose(stdout);
353591a6e85SSai Praneeth Prakhya 	PARENT_EXIT("Unable to run specified benchmark");
354591a6e85SSai Praneeth Prakhya }
355591a6e85SSai Praneeth Prakhya 
356591a6e85SSai Praneeth Prakhya /*
357591a6e85SSai Praneeth Prakhya  * create_grp - Create a group only if one doesn't exist
358591a6e85SSai Praneeth Prakhya  * @grp_name:	Name of the group
359591a6e85SSai Praneeth Prakhya  * @grp:	Full path and name of the group
360591a6e85SSai Praneeth Prakhya  * @parent_grp:	Full path and name of the parent group
361591a6e85SSai Praneeth Prakhya  *
362591a6e85SSai Praneeth Prakhya  * Return: 0 on success, non-zero on failure
363591a6e85SSai Praneeth Prakhya  */
364591a6e85SSai Praneeth Prakhya static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
365591a6e85SSai Praneeth Prakhya {
366591a6e85SSai Praneeth Prakhya 	int found_grp = 0;
367591a6e85SSai Praneeth Prakhya 	struct dirent *ep;
368591a6e85SSai Praneeth Prakhya 	DIR *dp;
369591a6e85SSai Praneeth Prakhya 
37078941183SFenghua Yu 	/*
37178941183SFenghua Yu 	 * At this point, we are guaranteed to have resctrl FS mounted and if
37278941183SFenghua Yu 	 * length of grp_name == 0, it means, user wants to use root con_mon
37378941183SFenghua Yu 	 * grp, so do nothing
37478941183SFenghua Yu 	 */
37578941183SFenghua Yu 	if (strlen(grp_name) == 0)
37678941183SFenghua Yu 		return 0;
37778941183SFenghua Yu 
378591a6e85SSai Praneeth Prakhya 	/* Check if requested grp exists or not */
379591a6e85SSai Praneeth Prakhya 	dp = opendir(parent_grp);
380591a6e85SSai Praneeth Prakhya 	if (dp) {
381591a6e85SSai Praneeth Prakhya 		while ((ep = readdir(dp)) != NULL) {
382591a6e85SSai Praneeth Prakhya 			if (strcmp(ep->d_name, grp_name) == 0)
383591a6e85SSai Praneeth Prakhya 				found_grp = 1;
384591a6e85SSai Praneeth Prakhya 		}
385591a6e85SSai Praneeth Prakhya 		closedir(dp);
386591a6e85SSai Praneeth Prakhya 	} else {
387591a6e85SSai Praneeth Prakhya 		perror("Unable to open resctrl for group");
388591a6e85SSai Praneeth Prakhya 
389591a6e85SSai Praneeth Prakhya 		return -1;
390591a6e85SSai Praneeth Prakhya 	}
391591a6e85SSai Praneeth Prakhya 
392591a6e85SSai Praneeth Prakhya 	/* Requested grp doesn't exist, hence create it */
393591a6e85SSai Praneeth Prakhya 	if (found_grp == 0) {
394591a6e85SSai Praneeth Prakhya 		if (mkdir(grp, 0) == -1) {
395591a6e85SSai Praneeth Prakhya 			perror("Unable to create group");
396591a6e85SSai Praneeth Prakhya 
397591a6e85SSai Praneeth Prakhya 			return -1;
398591a6e85SSai Praneeth Prakhya 		}
399591a6e85SSai Praneeth Prakhya 	}
400591a6e85SSai Praneeth Prakhya 
401591a6e85SSai Praneeth Prakhya 	return 0;
402591a6e85SSai Praneeth Prakhya }
403591a6e85SSai Praneeth Prakhya 
404591a6e85SSai Praneeth Prakhya static int write_pid_to_tasks(char *tasks, pid_t pid)
405591a6e85SSai Praneeth Prakhya {
406591a6e85SSai Praneeth Prakhya 	FILE *fp;
407591a6e85SSai Praneeth Prakhya 
408591a6e85SSai Praneeth Prakhya 	fp = fopen(tasks, "w");
409591a6e85SSai Praneeth Prakhya 	if (!fp) {
410591a6e85SSai Praneeth Prakhya 		perror("Failed to open tasks file");
411591a6e85SSai Praneeth Prakhya 
412591a6e85SSai Praneeth Prakhya 		return -1;
413591a6e85SSai Praneeth Prakhya 	}
414591a6e85SSai Praneeth Prakhya 	if (fprintf(fp, "%d\n", pid) < 0) {
415591a6e85SSai Praneeth Prakhya 		perror("Failed to wr pid to tasks file");
416591a6e85SSai Praneeth Prakhya 		fclose(fp);
417591a6e85SSai Praneeth Prakhya 
418591a6e85SSai Praneeth Prakhya 		return -1;
419591a6e85SSai Praneeth Prakhya 	}
420591a6e85SSai Praneeth Prakhya 	fclose(fp);
421591a6e85SSai Praneeth Prakhya 
422591a6e85SSai Praneeth Prakhya 	return 0;
423591a6e85SSai Praneeth Prakhya }
424591a6e85SSai Praneeth Prakhya 
425591a6e85SSai Praneeth Prakhya /*
426591a6e85SSai Praneeth Prakhya  * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
427591a6e85SSai Praneeth Prakhya  * @bm_pid:		PID that should be written
428591a6e85SSai Praneeth Prakhya  * @ctrlgrp:		Name of the control monitor group (con_mon grp)
429591a6e85SSai Praneeth Prakhya  * @mongrp:		Name of the monitor group (mon grp)
430591a6e85SSai Praneeth Prakhya  * @resctrl_val:	Resctrl feature (Eg: mbm, mba.. etc)
431591a6e85SSai Praneeth Prakhya  *
432591a6e85SSai Praneeth Prakhya  * If a con_mon grp is requested, create it and write pid to it, otherwise
433591a6e85SSai Praneeth Prakhya  * write pid to root con_mon grp.
434591a6e85SSai Praneeth Prakhya  * If a mon grp is requested, create it and write pid to it, otherwise
435591a6e85SSai Praneeth Prakhya  * pid is not written, this means that pid is in con_mon grp and hence
436591a6e85SSai Praneeth Prakhya  * should consult con_mon grp's mon_data directory for results.
437591a6e85SSai Praneeth Prakhya  *
438591a6e85SSai Praneeth Prakhya  * Return: 0 on success, non-zero on failure
439591a6e85SSai Praneeth Prakhya  */
440591a6e85SSai Praneeth Prakhya int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
441591a6e85SSai Praneeth Prakhya 			    char *resctrl_val)
442591a6e85SSai Praneeth Prakhya {
443591a6e85SSai Praneeth Prakhya 	char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
444591a6e85SSai Praneeth Prakhya 	char tasks[1024];
445591a6e85SSai Praneeth Prakhya 	int ret = 0;
446591a6e85SSai Praneeth Prakhya 
447591a6e85SSai Praneeth Prakhya 	if (strlen(ctrlgrp))
448591a6e85SSai Praneeth Prakhya 		sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
449591a6e85SSai Praneeth Prakhya 	else
450591a6e85SSai Praneeth Prakhya 		sprintf(controlgroup, "%s", RESCTRL_PATH);
451591a6e85SSai Praneeth Prakhya 
452591a6e85SSai Praneeth Prakhya 	/* Create control and monitoring group and write pid into it */
453591a6e85SSai Praneeth Prakhya 	ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
454591a6e85SSai Praneeth Prakhya 	if (ret)
455591a6e85SSai Praneeth Prakhya 		goto out;
456591a6e85SSai Praneeth Prakhya 	sprintf(tasks, "%s/tasks", controlgroup);
457591a6e85SSai Praneeth Prakhya 	ret = write_pid_to_tasks(tasks, bm_pid);
458591a6e85SSai Praneeth Prakhya 	if (ret)
459591a6e85SSai Praneeth Prakhya 		goto out;
460591a6e85SSai Praneeth Prakhya 
46178941183SFenghua Yu 	/* Create mon grp and write pid into it for "mbm" and "cqm" test */
462*24286736SFenghua Yu 	if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)) ||
463*24286736SFenghua Yu 	    !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
46478941183SFenghua Yu 		if (strlen(mongrp)) {
465591a6e85SSai Praneeth Prakhya 			sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
466591a6e85SSai Praneeth Prakhya 			sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
467591a6e85SSai Praneeth Prakhya 			ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
468591a6e85SSai Praneeth Prakhya 			if (ret)
469591a6e85SSai Praneeth Prakhya 				goto out;
470591a6e85SSai Praneeth Prakhya 
471591a6e85SSai Praneeth Prakhya 			sprintf(tasks, "%s/mon_groups/%s/tasks",
472591a6e85SSai Praneeth Prakhya 				controlgroup, mongrp);
473591a6e85SSai Praneeth Prakhya 			ret = write_pid_to_tasks(tasks, bm_pid);
474591a6e85SSai Praneeth Prakhya 			if (ret)
475591a6e85SSai Praneeth Prakhya 				goto out;
476591a6e85SSai Praneeth Prakhya 		}
477591a6e85SSai Praneeth Prakhya 	}
478591a6e85SSai Praneeth Prakhya 
479591a6e85SSai Praneeth Prakhya out:
480591a6e85SSai Praneeth Prakhya 	printf("%sok writing benchmark parameters to resctrl FS\n",
481591a6e85SSai Praneeth Prakhya 	       ret ? "not " : "");
482591a6e85SSai Praneeth Prakhya 	if (ret)
483591a6e85SSai Praneeth Prakhya 		perror("# writing to resctrlfs");
484591a6e85SSai Praneeth Prakhya 
485591a6e85SSai Praneeth Prakhya 	tests_run++;
486591a6e85SSai Praneeth Prakhya 
487591a6e85SSai Praneeth Prakhya 	return ret;
488591a6e85SSai Praneeth Prakhya }
489591a6e85SSai Praneeth Prakhya 
490591a6e85SSai Praneeth Prakhya /*
491591a6e85SSai Praneeth Prakhya  * write_schemata - Update schemata of a con_mon grp
492591a6e85SSai Praneeth Prakhya  * @ctrlgrp:		Name of the con_mon grp
493591a6e85SSai Praneeth Prakhya  * @schemata:		Schemata that should be updated to
494591a6e85SSai Praneeth Prakhya  * @cpu_no:		CPU number that the benchmark PID is binded to
495591a6e85SSai Praneeth Prakhya  * @resctrl_val:	Resctrl feature (Eg: mbm, mba.. etc)
496591a6e85SSai Praneeth Prakhya  *
497591a6e85SSai Praneeth Prakhya  * Update schemata of a con_mon grp *only* if requested resctrl feature is
498591a6e85SSai Praneeth Prakhya  * allocation type
499591a6e85SSai Praneeth Prakhya  *
500591a6e85SSai Praneeth Prakhya  * Return: 0 on success, non-zero on failure
501591a6e85SSai Praneeth Prakhya  */
502591a6e85SSai Praneeth Prakhya int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
503591a6e85SSai Praneeth Prakhya {
504591a6e85SSai Praneeth Prakhya 	char controlgroup[1024], schema[1024], reason[64];
505591a6e85SSai Praneeth Prakhya 	int resource_id, ret = 0;
506591a6e85SSai Praneeth Prakhya 	FILE *fp;
507591a6e85SSai Praneeth Prakhya 
508*24286736SFenghua Yu 	if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
509*24286736SFenghua Yu 	    strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
510*24286736SFenghua Yu 	    strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
511591a6e85SSai Praneeth Prakhya 		return -ENOENT;
512591a6e85SSai Praneeth Prakhya 
513591a6e85SSai Praneeth Prakhya 	if (!schemata) {
514591a6e85SSai Praneeth Prakhya 		printf("# Skipping empty schemata update\n");
515591a6e85SSai Praneeth Prakhya 
516591a6e85SSai Praneeth Prakhya 		return -1;
517591a6e85SSai Praneeth Prakhya 	}
518591a6e85SSai Praneeth Prakhya 
519591a6e85SSai Praneeth Prakhya 	if (get_resource_id(cpu_no, &resource_id) < 0) {
520591a6e85SSai Praneeth Prakhya 		sprintf(reason, "Failed to get resource id");
521591a6e85SSai Praneeth Prakhya 		ret = -1;
522591a6e85SSai Praneeth Prakhya 
523591a6e85SSai Praneeth Prakhya 		goto out;
524591a6e85SSai Praneeth Prakhya 	}
525591a6e85SSai Praneeth Prakhya 
526591a6e85SSai Praneeth Prakhya 	if (strlen(ctrlgrp) != 0)
527591a6e85SSai Praneeth Prakhya 		sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
528591a6e85SSai Praneeth Prakhya 	else
529591a6e85SSai Praneeth Prakhya 		sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
530591a6e85SSai Praneeth Prakhya 
531*24286736SFenghua Yu 	if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
532*24286736SFenghua Yu 	    !strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
53378941183SFenghua Yu 		sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
534*24286736SFenghua Yu 	if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)))
535591a6e85SSai Praneeth Prakhya 		sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
536591a6e85SSai Praneeth Prakhya 
537591a6e85SSai Praneeth Prakhya 	fp = fopen(controlgroup, "w");
538591a6e85SSai Praneeth Prakhya 	if (!fp) {
539591a6e85SSai Praneeth Prakhya 		sprintf(reason, "Failed to open control group");
540591a6e85SSai Praneeth Prakhya 		ret = -1;
541591a6e85SSai Praneeth Prakhya 
542591a6e85SSai Praneeth Prakhya 		goto out;
543591a6e85SSai Praneeth Prakhya 	}
544591a6e85SSai Praneeth Prakhya 
545591a6e85SSai Praneeth Prakhya 	if (fprintf(fp, "%s\n", schema) < 0) {
546591a6e85SSai Praneeth Prakhya 		sprintf(reason, "Failed to write schemata in control group");
547591a6e85SSai Praneeth Prakhya 		fclose(fp);
548591a6e85SSai Praneeth Prakhya 		ret = -1;
549591a6e85SSai Praneeth Prakhya 
550591a6e85SSai Praneeth Prakhya 		goto out;
551591a6e85SSai Praneeth Prakhya 	}
552591a6e85SSai Praneeth Prakhya 	fclose(fp);
553591a6e85SSai Praneeth Prakhya 
554591a6e85SSai Praneeth Prakhya out:
555591a6e85SSai Praneeth Prakhya 	printf("%sok Write schema \"%s\" to resctrl FS%s%s\n",
556591a6e85SSai Praneeth Prakhya 	       ret ? "not " : "", schema, ret ? " # " : "",
557591a6e85SSai Praneeth Prakhya 	       ret ? reason : "");
558591a6e85SSai Praneeth Prakhya 	tests_run++;
559591a6e85SSai Praneeth Prakhya 
560591a6e85SSai Praneeth Prakhya 	return ret;
561591a6e85SSai Praneeth Prakhya }
562591a6e85SSai Praneeth Prakhya 
563ecdbb911SFenghua Yu bool check_resctrlfs_support(void)
564ecdbb911SFenghua Yu {
565ecdbb911SFenghua Yu 	FILE *inf = fopen("/proc/filesystems", "r");
566ecdbb911SFenghua Yu 	DIR *dp;
567ecdbb911SFenghua Yu 	char *res;
568ecdbb911SFenghua Yu 	bool ret = false;
569ecdbb911SFenghua Yu 
570ecdbb911SFenghua Yu 	if (!inf)
571ecdbb911SFenghua Yu 		return false;
572ecdbb911SFenghua Yu 
573ecdbb911SFenghua Yu 	res = fgrep(inf, "nodev\tresctrl\n");
574ecdbb911SFenghua Yu 
575ecdbb911SFenghua Yu 	if (res) {
576ecdbb911SFenghua Yu 		ret = true;
577ecdbb911SFenghua Yu 		free(res);
578ecdbb911SFenghua Yu 	}
579ecdbb911SFenghua Yu 
580ecdbb911SFenghua Yu 	fclose(inf);
581ecdbb911SFenghua Yu 
582ecdbb911SFenghua Yu 	printf("%sok kernel supports resctrl filesystem\n", ret ? "" : "not ");
583ecdbb911SFenghua Yu 	tests_run++;
584ecdbb911SFenghua Yu 
585ecdbb911SFenghua Yu 	dp = opendir(RESCTRL_PATH);
586ecdbb911SFenghua Yu 	printf("%sok resctrl mountpoint \"%s\" exists\n",
587ecdbb911SFenghua Yu 	       dp ? "" : "not ", RESCTRL_PATH);
588ecdbb911SFenghua Yu 	if (dp)
589ecdbb911SFenghua Yu 		closedir(dp);
590ecdbb911SFenghua Yu 	tests_run++;
591ecdbb911SFenghua Yu 
592ecdbb911SFenghua Yu 	printf("# resctrl filesystem %s mounted\n",
593ecdbb911SFenghua Yu 	       find_resctrl_mount(NULL) ? "not" : "is");
594ecdbb911SFenghua Yu 
595ecdbb911SFenghua Yu 	return ret;
596ecdbb911SFenghua Yu }
597ecdbb911SFenghua Yu 
598591a6e85SSai Praneeth Prakhya char *fgrep(FILE *inf, const char *str)
599591a6e85SSai Praneeth Prakhya {
600591a6e85SSai Praneeth Prakhya 	char line[256];
601591a6e85SSai Praneeth Prakhya 	int slen = strlen(str);
602591a6e85SSai Praneeth Prakhya 
603591a6e85SSai Praneeth Prakhya 	while (!feof(inf)) {
604591a6e85SSai Praneeth Prakhya 		if (!fgets(line, 256, inf))
605591a6e85SSai Praneeth Prakhya 			break;
606591a6e85SSai Praneeth Prakhya 		if (strncmp(line, str, slen))
607591a6e85SSai Praneeth Prakhya 			continue;
608591a6e85SSai Praneeth Prakhya 
609591a6e85SSai Praneeth Prakhya 		return strdup(line);
610591a6e85SSai Praneeth Prakhya 	}
611591a6e85SSai Praneeth Prakhya 
612591a6e85SSai Praneeth Prakhya 	return NULL;
613591a6e85SSai Praneeth Prakhya }
614591a6e85SSai Praneeth Prakhya 
615591a6e85SSai Praneeth Prakhya /*
616591a6e85SSai Praneeth Prakhya  * validate_resctrl_feature_request - Check if requested feature is valid.
617591a6e85SSai Praneeth Prakhya  * @resctrl_val:	Requested feature
618591a6e85SSai Praneeth Prakhya  *
619591a6e85SSai Praneeth Prakhya  * Return: 0 on success, non-zero on failure
620591a6e85SSai Praneeth Prakhya  */
621591a6e85SSai Praneeth Prakhya bool validate_resctrl_feature_request(char *resctrl_val)
622591a6e85SSai Praneeth Prakhya {
623591a6e85SSai Praneeth Prakhya 	FILE *inf = fopen("/proc/cpuinfo", "r");
624591a6e85SSai Praneeth Prakhya 	bool found = false;
625591a6e85SSai Praneeth Prakhya 	char *res;
626591a6e85SSai Praneeth Prakhya 
627591a6e85SSai Praneeth Prakhya 	if (!inf)
628591a6e85SSai Praneeth Prakhya 		return false;
629591a6e85SSai Praneeth Prakhya 
630591a6e85SSai Praneeth Prakhya 	res = fgrep(inf, "flags");
631591a6e85SSai Praneeth Prakhya 
632591a6e85SSai Praneeth Prakhya 	if (res) {
633591a6e85SSai Praneeth Prakhya 		char *s = strchr(res, ':');
634591a6e85SSai Praneeth Prakhya 
635591a6e85SSai Praneeth Prakhya 		found = s && !strstr(s, resctrl_val);
636591a6e85SSai Praneeth Prakhya 		free(res);
637591a6e85SSai Praneeth Prakhya 	}
638591a6e85SSai Praneeth Prakhya 	fclose(inf);
639591a6e85SSai Praneeth Prakhya 
640591a6e85SSai Praneeth Prakhya 	return found;
641591a6e85SSai Praneeth Prakhya }
642591a6e85SSai Praneeth Prakhya 
643ecdbb911SFenghua Yu int filter_dmesg(void)
644ecdbb911SFenghua Yu {
645ecdbb911SFenghua Yu 	char line[1024];
646ecdbb911SFenghua Yu 	FILE *fp;
647ecdbb911SFenghua Yu 	int pipefds[2];
648ecdbb911SFenghua Yu 	pid_t pid;
649ecdbb911SFenghua Yu 	int ret;
650ecdbb911SFenghua Yu 
651ecdbb911SFenghua Yu 	ret = pipe(pipefds);
652ecdbb911SFenghua Yu 	if (ret) {
653ecdbb911SFenghua Yu 		perror("pipe");
654ecdbb911SFenghua Yu 		return ret;
655ecdbb911SFenghua Yu 	}
656ecdbb911SFenghua Yu 	pid = fork();
657ecdbb911SFenghua Yu 	if (pid == 0) {
658ecdbb911SFenghua Yu 		close(pipefds[0]);
659ecdbb911SFenghua Yu 		dup2(pipefds[1], STDOUT_FILENO);
660ecdbb911SFenghua Yu 		execlp("dmesg", "dmesg", NULL);
661ecdbb911SFenghua Yu 		perror("executing dmesg");
662ecdbb911SFenghua Yu 		exit(1);
663ecdbb911SFenghua Yu 	}
664ecdbb911SFenghua Yu 	close(pipefds[1]);
665ecdbb911SFenghua Yu 	fp = fdopen(pipefds[0], "r");
666ecdbb911SFenghua Yu 	if (!fp) {
667ecdbb911SFenghua Yu 		perror("fdopen(pipe)");
668ecdbb911SFenghua Yu 		kill(pid, SIGTERM);
669ecdbb911SFenghua Yu 
670ecdbb911SFenghua Yu 		return -1;
671ecdbb911SFenghua Yu 	}
672ecdbb911SFenghua Yu 
673ecdbb911SFenghua Yu 	while (fgets(line, 1024, fp)) {
674ecdbb911SFenghua Yu 		if (strstr(line, "intel_rdt:"))
675ecdbb911SFenghua Yu 			printf("# dmesg: %s", line);
676ecdbb911SFenghua Yu 		if (strstr(line, "resctrl:"))
677ecdbb911SFenghua Yu 			printf("# dmesg: %s", line);
678ecdbb911SFenghua Yu 	}
679ecdbb911SFenghua Yu 	fclose(fp);
680ecdbb911SFenghua Yu 	waitpid(pid, NULL, 0);
681ecdbb911SFenghua Yu 
682ecdbb911SFenghua Yu 	return 0;
683ecdbb911SFenghua Yu }
684ecdbb911SFenghua Yu 
685591a6e85SSai Praneeth Prakhya int validate_bw_report_request(char *bw_report)
686591a6e85SSai Praneeth Prakhya {
687591a6e85SSai Praneeth Prakhya 	if (strcmp(bw_report, "reads") == 0)
688591a6e85SSai Praneeth Prakhya 		return 0;
689591a6e85SSai Praneeth Prakhya 	if (strcmp(bw_report, "writes") == 0)
690591a6e85SSai Praneeth Prakhya 		return 0;
691591a6e85SSai Praneeth Prakhya 	if (strcmp(bw_report, "nt-writes") == 0) {
692591a6e85SSai Praneeth Prakhya 		strcpy(bw_report, "writes");
693591a6e85SSai Praneeth Prakhya 		return 0;
694591a6e85SSai Praneeth Prakhya 	}
695591a6e85SSai Praneeth Prakhya 	if (strcmp(bw_report, "total") == 0)
696591a6e85SSai Praneeth Prakhya 		return 0;
697591a6e85SSai Praneeth Prakhya 
698591a6e85SSai Praneeth Prakhya 	fprintf(stderr, "Requested iMC B/W report type unavailable\n");
699591a6e85SSai Praneeth Prakhya 
700591a6e85SSai Praneeth Prakhya 	return -1;
701591a6e85SSai Praneeth Prakhya }
702591a6e85SSai Praneeth Prakhya 
703591a6e85SSai Praneeth Prakhya int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
704591a6e85SSai Praneeth Prakhya 		    int group_fd, unsigned long flags)
705591a6e85SSai Praneeth Prakhya {
706591a6e85SSai Praneeth Prakhya 	int ret;
707591a6e85SSai Praneeth Prakhya 
708591a6e85SSai Praneeth Prakhya 	ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
709591a6e85SSai Praneeth Prakhya 		      group_fd, flags);
710591a6e85SSai Praneeth Prakhya 	return ret;
711591a6e85SSai Praneeth Prakhya }
71278941183SFenghua Yu 
71378941183SFenghua Yu unsigned int count_bits(unsigned long n)
71478941183SFenghua Yu {
71578941183SFenghua Yu 	unsigned int count = 0;
71678941183SFenghua Yu 
71778941183SFenghua Yu 	while (n) {
71878941183SFenghua Yu 		count += n & 1;
71978941183SFenghua Yu 		n >>= 1;
72078941183SFenghua Yu 	}
72178941183SFenghua Yu 
72278941183SFenghua Yu 	return count;
72378941183SFenghua Yu }
724