xref: /openbmc/linux/tools/tracing/rtla/src/utils.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
1b1696371SDaniel Bristot de Oliveira // SPDX-License-Identifier: GPL-2.0
2b1696371SDaniel Bristot de Oliveira /*
3b1696371SDaniel Bristot de Oliveira  * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
4b1696371SDaniel Bristot de Oliveira  */
5b1696371SDaniel Bristot de Oliveira 
6272ced25SDaniel Bristot de Oliveira #define _GNU_SOURCE
7dada03dbSDaniel Bristot de Oliveira #include <dirent.h>
8b1696371SDaniel Bristot de Oliveira #include <stdarg.h>
9b1696371SDaniel Bristot de Oliveira #include <stdlib.h>
10b1696371SDaniel Bristot de Oliveira #include <string.h>
11b1696371SDaniel Bristot de Oliveira #include <unistd.h>
12b1696371SDaniel Bristot de Oliveira #include <ctype.h>
13b1696371SDaniel Bristot de Oliveira #include <errno.h>
147d0dc957SDaniel Bristot de Oliveira #include <fcntl.h>
15b1696371SDaniel Bristot de Oliveira #include <sched.h>
16b1696371SDaniel Bristot de Oliveira #include <stdio.h>
17b1696371SDaniel Bristot de Oliveira 
18b1696371SDaniel Bristot de Oliveira #include "utils.h"
19b1696371SDaniel Bristot de Oliveira 
20b1696371SDaniel Bristot de Oliveira #define MAX_MSG_LENGTH	1024
21b1696371SDaniel Bristot de Oliveira int config_debug;
22b1696371SDaniel Bristot de Oliveira 
23b1696371SDaniel Bristot de Oliveira /*
24b1696371SDaniel Bristot de Oliveira  * err_msg - print an error message to the stderr
25b1696371SDaniel Bristot de Oliveira  */
err_msg(const char * fmt,...)26b1696371SDaniel Bristot de Oliveira void err_msg(const char *fmt, ...)
27b1696371SDaniel Bristot de Oliveira {
28b1696371SDaniel Bristot de Oliveira 	char message[MAX_MSG_LENGTH];
29b1696371SDaniel Bristot de Oliveira 	va_list ap;
30b1696371SDaniel Bristot de Oliveira 
31b1696371SDaniel Bristot de Oliveira 	va_start(ap, fmt);
32b1696371SDaniel Bristot de Oliveira 	vsnprintf(message, sizeof(message), fmt, ap);
33b1696371SDaniel Bristot de Oliveira 	va_end(ap);
34b1696371SDaniel Bristot de Oliveira 
35b1696371SDaniel Bristot de Oliveira 	fprintf(stderr, "%s", message);
36b1696371SDaniel Bristot de Oliveira }
37b1696371SDaniel Bristot de Oliveira 
38b1696371SDaniel Bristot de Oliveira /*
39b1696371SDaniel Bristot de Oliveira  * debug_msg - print a debug message to stderr if debug is set
40b1696371SDaniel Bristot de Oliveira  */
debug_msg(const char * fmt,...)41b1696371SDaniel Bristot de Oliveira void debug_msg(const char *fmt, ...)
42b1696371SDaniel Bristot de Oliveira {
43b1696371SDaniel Bristot de Oliveira 	char message[MAX_MSG_LENGTH];
44b1696371SDaniel Bristot de Oliveira 	va_list ap;
45b1696371SDaniel Bristot de Oliveira 
46b1696371SDaniel Bristot de Oliveira 	if (!config_debug)
47b1696371SDaniel Bristot de Oliveira 		return;
48b1696371SDaniel Bristot de Oliveira 
49b1696371SDaniel Bristot de Oliveira 	va_start(ap, fmt);
50b1696371SDaniel Bristot de Oliveira 	vsnprintf(message, sizeof(message), fmt, ap);
51b1696371SDaniel Bristot de Oliveira 	va_end(ap);
52b1696371SDaniel Bristot de Oliveira 
53b1696371SDaniel Bristot de Oliveira 	fprintf(stderr, "%s", message);
54b1696371SDaniel Bristot de Oliveira }
55b1696371SDaniel Bristot de Oliveira 
56b1696371SDaniel Bristot de Oliveira /*
57b1696371SDaniel Bristot de Oliveira  * get_llong_from_str - get a long long int from a string
58b1696371SDaniel Bristot de Oliveira  */
get_llong_from_str(char * start)59b1696371SDaniel Bristot de Oliveira long long get_llong_from_str(char *start)
60b1696371SDaniel Bristot de Oliveira {
61b1696371SDaniel Bristot de Oliveira 	long long value;
62b1696371SDaniel Bristot de Oliveira 	char *end;
63b1696371SDaniel Bristot de Oliveira 
64b1696371SDaniel Bristot de Oliveira 	errno = 0;
65b1696371SDaniel Bristot de Oliveira 	value = strtoll(start, &end, 10);
66b1696371SDaniel Bristot de Oliveira 	if (errno || start == end)
67b1696371SDaniel Bristot de Oliveira 		return -1;
68b1696371SDaniel Bristot de Oliveira 
69b1696371SDaniel Bristot de Oliveira 	return value;
70b1696371SDaniel Bristot de Oliveira }
71b1696371SDaniel Bristot de Oliveira 
72b1696371SDaniel Bristot de Oliveira /*
73b1696371SDaniel Bristot de Oliveira  * get_duration - fill output with a human readable duration since start_time
74b1696371SDaniel Bristot de Oliveira  */
get_duration(time_t start_time,char * output,int output_size)75b1696371SDaniel Bristot de Oliveira void get_duration(time_t start_time, char *output, int output_size)
76b1696371SDaniel Bristot de Oliveira {
77b1696371SDaniel Bristot de Oliveira 	time_t now = time(NULL);
78b1696371SDaniel Bristot de Oliveira 	struct tm *tm_info;
79b1696371SDaniel Bristot de Oliveira 	time_t duration;
80b1696371SDaniel Bristot de Oliveira 
81b1696371SDaniel Bristot de Oliveira 	duration = difftime(now, start_time);
82f35491b8SDaniel Bristot de Oliveira 	tm_info = gmtime(&duration);
83b1696371SDaniel Bristot de Oliveira 
84b1696371SDaniel Bristot de Oliveira 	snprintf(output, output_size, "%3d %02d:%02d:%02d",
85b1696371SDaniel Bristot de Oliveira 			tm_info->tm_yday,
86f35491b8SDaniel Bristot de Oliveira 			tm_info->tm_hour,
87b1696371SDaniel Bristot de Oliveira 			tm_info->tm_min,
88b1696371SDaniel Bristot de Oliveira 			tm_info->tm_sec);
89b1696371SDaniel Bristot de Oliveira }
90b1696371SDaniel Bristot de Oliveira 
91b1696371SDaniel Bristot de Oliveira /*
92272ced25SDaniel Bristot de Oliveira  * parse_cpu_set - parse a cpu_list filling cpu_set_t argument
93272ced25SDaniel Bristot de Oliveira  *
94272ced25SDaniel Bristot de Oliveira  * Receives a cpu list, like 1-3,5 (cpus 1, 2, 3, 5), and then set
95272ced25SDaniel Bristot de Oliveira  * filling cpu_set_t argument.
96272ced25SDaniel Bristot de Oliveira  *
97272ced25SDaniel Bristot de Oliveira  * Returns 1 on success, 0 otherwise.
98272ced25SDaniel Bristot de Oliveira  */
parse_cpu_set(char * cpu_list,cpu_set_t * set)99272ced25SDaniel Bristot de Oliveira int parse_cpu_set(char *cpu_list, cpu_set_t *set)
100272ced25SDaniel Bristot de Oliveira {
101272ced25SDaniel Bristot de Oliveira 	const char *p;
102272ced25SDaniel Bristot de Oliveira 	int end_cpu;
103272ced25SDaniel Bristot de Oliveira 	int nr_cpus;
104272ced25SDaniel Bristot de Oliveira 	int cpu;
105272ced25SDaniel Bristot de Oliveira 	int i;
106272ced25SDaniel Bristot de Oliveira 
107272ced25SDaniel Bristot de Oliveira 	CPU_ZERO(set);
108272ced25SDaniel Bristot de Oliveira 
109272ced25SDaniel Bristot de Oliveira 	nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
110272ced25SDaniel Bristot de Oliveira 
111272ced25SDaniel Bristot de Oliveira 	for (p = cpu_list; *p; ) {
112272ced25SDaniel Bristot de Oliveira 		cpu = atoi(p);
113272ced25SDaniel Bristot de Oliveira 		if (cpu < 0 || (!cpu && *p != '0') || cpu >= nr_cpus)
114272ced25SDaniel Bristot de Oliveira 			goto err;
115272ced25SDaniel Bristot de Oliveira 
116272ced25SDaniel Bristot de Oliveira 		while (isdigit(*p))
117272ced25SDaniel Bristot de Oliveira 			p++;
118272ced25SDaniel Bristot de Oliveira 		if (*p == '-') {
119272ced25SDaniel Bristot de Oliveira 			p++;
120272ced25SDaniel Bristot de Oliveira 			end_cpu = atoi(p);
121272ced25SDaniel Bristot de Oliveira 			if (end_cpu < cpu || (!end_cpu && *p != '0') || end_cpu >= nr_cpus)
122272ced25SDaniel Bristot de Oliveira 				goto err;
123272ced25SDaniel Bristot de Oliveira 			while (isdigit(*p))
124272ced25SDaniel Bristot de Oliveira 				p++;
125272ced25SDaniel Bristot de Oliveira 		} else
126272ced25SDaniel Bristot de Oliveira 			end_cpu = cpu;
127272ced25SDaniel Bristot de Oliveira 
128272ced25SDaniel Bristot de Oliveira 		if (cpu == end_cpu) {
129272ced25SDaniel Bristot de Oliveira 			debug_msg("cpu_set: adding cpu %d\n", cpu);
130272ced25SDaniel Bristot de Oliveira 			CPU_SET(cpu, set);
131272ced25SDaniel Bristot de Oliveira 		} else {
132272ced25SDaniel Bristot de Oliveira 			for (i = cpu; i <= end_cpu; i++) {
133272ced25SDaniel Bristot de Oliveira 				debug_msg("cpu_set: adding cpu %d\n", i);
134272ced25SDaniel Bristot de Oliveira 				CPU_SET(i, set);
135272ced25SDaniel Bristot de Oliveira 			}
136272ced25SDaniel Bristot de Oliveira 		}
137272ced25SDaniel Bristot de Oliveira 
138272ced25SDaniel Bristot de Oliveira 		if (*p == ',')
139272ced25SDaniel Bristot de Oliveira 			p++;
140272ced25SDaniel Bristot de Oliveira 	}
141272ced25SDaniel Bristot de Oliveira 
142272ced25SDaniel Bristot de Oliveira 	return 0;
143272ced25SDaniel Bristot de Oliveira err:
144272ced25SDaniel Bristot de Oliveira 	debug_msg("Error parsing the cpu set %s\n", cpu_list);
145272ced25SDaniel Bristot de Oliveira 	return 1;
146272ced25SDaniel Bristot de Oliveira }
147272ced25SDaniel Bristot de Oliveira 
148272ced25SDaniel Bristot de Oliveira /*
149b1696371SDaniel Bristot de Oliveira  * parse_duration - parse duration with s/m/h/d suffix converting it to seconds
150b1696371SDaniel Bristot de Oliveira  */
parse_seconds_duration(char * val)151b1696371SDaniel Bristot de Oliveira long parse_seconds_duration(char *val)
152b1696371SDaniel Bristot de Oliveira {
153b1696371SDaniel Bristot de Oliveira 	char *end;
154b1696371SDaniel Bristot de Oliveira 	long t;
155b1696371SDaniel Bristot de Oliveira 
156b1696371SDaniel Bristot de Oliveira 	t = strtol(val, &end, 10);
157b1696371SDaniel Bristot de Oliveira 
158b1696371SDaniel Bristot de Oliveira 	if (end) {
159b1696371SDaniel Bristot de Oliveira 		switch (*end) {
160b1696371SDaniel Bristot de Oliveira 		case 's':
161b1696371SDaniel Bristot de Oliveira 		case 'S':
162b1696371SDaniel Bristot de Oliveira 			break;
163b1696371SDaniel Bristot de Oliveira 		case 'm':
164b1696371SDaniel Bristot de Oliveira 		case 'M':
165b1696371SDaniel Bristot de Oliveira 			t *= 60;
166b1696371SDaniel Bristot de Oliveira 			break;
167b1696371SDaniel Bristot de Oliveira 		case 'h':
168b1696371SDaniel Bristot de Oliveira 		case 'H':
169b1696371SDaniel Bristot de Oliveira 			t *= 60 * 60;
170b1696371SDaniel Bristot de Oliveira 			break;
171b1696371SDaniel Bristot de Oliveira 
172b1696371SDaniel Bristot de Oliveira 		case 'd':
173b1696371SDaniel Bristot de Oliveira 		case 'D':
174b1696371SDaniel Bristot de Oliveira 			t *= 24 * 60 * 60;
175b1696371SDaniel Bristot de Oliveira 			break;
176b1696371SDaniel Bristot de Oliveira 		}
177b1696371SDaniel Bristot de Oliveira 	}
178b1696371SDaniel Bristot de Oliveira 
179b1696371SDaniel Bristot de Oliveira 	return t;
180b1696371SDaniel Bristot de Oliveira }
181b1696371SDaniel Bristot de Oliveira 
182b1696371SDaniel Bristot de Oliveira /*
183b1696371SDaniel Bristot de Oliveira  * parse_ns_duration - parse duration with ns/us/ms/s converting it to nanoseconds
184b1696371SDaniel Bristot de Oliveira  */
parse_ns_duration(char * val)185b1696371SDaniel Bristot de Oliveira long parse_ns_duration(char *val)
186b1696371SDaniel Bristot de Oliveira {
187b1696371SDaniel Bristot de Oliveira 	char *end;
188b1696371SDaniel Bristot de Oliveira 	long t;
189b1696371SDaniel Bristot de Oliveira 
190b1696371SDaniel Bristot de Oliveira 	t = strtol(val, &end, 10);
191b1696371SDaniel Bristot de Oliveira 
192b1696371SDaniel Bristot de Oliveira 	if (end) {
193b1696371SDaniel Bristot de Oliveira 		if (!strncmp(end, "ns", 2)) {
194b1696371SDaniel Bristot de Oliveira 			return t;
195b1696371SDaniel Bristot de Oliveira 		} else if (!strncmp(end, "us", 2)) {
196b1696371SDaniel Bristot de Oliveira 			t *= 1000;
197b1696371SDaniel Bristot de Oliveira 			return t;
198b1696371SDaniel Bristot de Oliveira 		} else if (!strncmp(end, "ms", 2)) {
199b1696371SDaniel Bristot de Oliveira 			t *= 1000 * 1000;
200b1696371SDaniel Bristot de Oliveira 			return t;
201b1696371SDaniel Bristot de Oliveira 		} else if (!strncmp(end, "s", 1)) {
202b1696371SDaniel Bristot de Oliveira 			t *= 1000 * 1000 * 1000;
203b1696371SDaniel Bristot de Oliveira 			return t;
204b1696371SDaniel Bristot de Oliveira 		}
205b1696371SDaniel Bristot de Oliveira 		return -1;
206b1696371SDaniel Bristot de Oliveira 	}
207b1696371SDaniel Bristot de Oliveira 
208b1696371SDaniel Bristot de Oliveira 	return t;
209b1696371SDaniel Bristot de Oliveira }
210b1696371SDaniel Bristot de Oliveira 
211b1696371SDaniel Bristot de Oliveira /*
212b1696371SDaniel Bristot de Oliveira  * This is a set of helper functions to use SCHED_DEADLINE.
213b1696371SDaniel Bristot de Oliveira  */
214b1696371SDaniel Bristot de Oliveira #ifdef __x86_64__
215b1696371SDaniel Bristot de Oliveira # define __NR_sched_setattr	314
216b1696371SDaniel Bristot de Oliveira # define __NR_sched_getattr	315
217b1696371SDaniel Bristot de Oliveira #elif __i386__
218b1696371SDaniel Bristot de Oliveira # define __NR_sched_setattr	351
219b1696371SDaniel Bristot de Oliveira # define __NR_sched_getattr	352
220b1696371SDaniel Bristot de Oliveira #elif __arm__
221b1696371SDaniel Bristot de Oliveira # define __NR_sched_setattr	380
222b1696371SDaniel Bristot de Oliveira # define __NR_sched_getattr	381
223dd0b15bdSAndreas Schwab #elif __aarch64__ || __riscv
224b1696371SDaniel Bristot de Oliveira # define __NR_sched_setattr	274
225b1696371SDaniel Bristot de Oliveira # define __NR_sched_getattr	275
226b1696371SDaniel Bristot de Oliveira #elif __powerpc__
227b1696371SDaniel Bristot de Oliveira # define __NR_sched_setattr	355
228b1696371SDaniel Bristot de Oliveira # define __NR_sched_getattr	356
229b1696371SDaniel Bristot de Oliveira #elif __s390x__
230b1696371SDaniel Bristot de Oliveira # define __NR_sched_setattr	345
231b1696371SDaniel Bristot de Oliveira # define __NR_sched_getattr	346
232b1696371SDaniel Bristot de Oliveira #endif
233b1696371SDaniel Bristot de Oliveira 
234b1696371SDaniel Bristot de Oliveira #define SCHED_DEADLINE		6
235b1696371SDaniel Bristot de Oliveira 
syscall_sched_setattr(pid_t pid,const struct sched_attr * attr,unsigned int flags)236*987abe82SJan Stancek static inline int syscall_sched_setattr(pid_t pid, const struct sched_attr *attr,
237b1696371SDaniel Bristot de Oliveira 				unsigned int flags) {
238b1696371SDaniel Bristot de Oliveira 	return syscall(__NR_sched_setattr, pid, attr, flags);
239b1696371SDaniel Bristot de Oliveira }
240b1696371SDaniel Bristot de Oliveira 
__set_sched_attr(int pid,struct sched_attr * attr)241b1696371SDaniel Bristot de Oliveira int __set_sched_attr(int pid, struct sched_attr *attr)
242b1696371SDaniel Bristot de Oliveira {
243b1696371SDaniel Bristot de Oliveira 	int flags = 0;
244b1696371SDaniel Bristot de Oliveira 	int retval;
245b1696371SDaniel Bristot de Oliveira 
246*987abe82SJan Stancek 	retval = syscall_sched_setattr(pid, attr, flags);
247b1696371SDaniel Bristot de Oliveira 	if (retval < 0) {
248941a53c3SDaniel Bristot de Oliveira 		err_msg("Failed to set sched attributes to the pid %d: %s\n",
249b1696371SDaniel Bristot de Oliveira 			pid, strerror(errno));
250b1696371SDaniel Bristot de Oliveira 		return 1;
251b1696371SDaniel Bristot de Oliveira 	}
252b1696371SDaniel Bristot de Oliveira 
253b1696371SDaniel Bristot de Oliveira 	return 0;
254b1696371SDaniel Bristot de Oliveira }
255dada03dbSDaniel Bristot de Oliveira 
256b1696371SDaniel Bristot de Oliveira /*
257dada03dbSDaniel Bristot de Oliveira  * procfs_is_workload_pid - check if a procfs entry contains a comm_prefix* comm
258b1696371SDaniel Bristot de Oliveira  *
259dada03dbSDaniel Bristot de Oliveira  * Check if the procfs entry is a directory of a process, and then check if the
260dada03dbSDaniel Bristot de Oliveira  * process has a comm with the prefix set in char *comm_prefix. As the
261dada03dbSDaniel Bristot de Oliveira  * current users of this function only check for kernel threads, there is no
262dada03dbSDaniel Bristot de Oliveira  * need to check for the threads for the process.
263dada03dbSDaniel Bristot de Oliveira  *
264dada03dbSDaniel Bristot de Oliveira  * Return: True if the proc_entry contains a comm file with comm_prefix*.
265dada03dbSDaniel Bristot de Oliveira  * Otherwise returns false.
266dada03dbSDaniel Bristot de Oliveira  */
procfs_is_workload_pid(const char * comm_prefix,struct dirent * proc_entry)267dada03dbSDaniel Bristot de Oliveira static int procfs_is_workload_pid(const char *comm_prefix, struct dirent *proc_entry)
268dada03dbSDaniel Bristot de Oliveira {
269dada03dbSDaniel Bristot de Oliveira 	char buffer[MAX_PATH];
270dada03dbSDaniel Bristot de Oliveira 	int comm_fd, retval;
271dada03dbSDaniel Bristot de Oliveira 	char *t_name;
272dada03dbSDaniel Bristot de Oliveira 
273dada03dbSDaniel Bristot de Oliveira 	if (proc_entry->d_type != DT_DIR)
274dada03dbSDaniel Bristot de Oliveira 		return 0;
275dada03dbSDaniel Bristot de Oliveira 
276dada03dbSDaniel Bristot de Oliveira 	if (*proc_entry->d_name == '.')
277dada03dbSDaniel Bristot de Oliveira 		return 0;
278dada03dbSDaniel Bristot de Oliveira 
279dada03dbSDaniel Bristot de Oliveira 	/* check if the string is a pid */
280dada03dbSDaniel Bristot de Oliveira 	for (t_name = proc_entry->d_name; t_name; t_name++) {
281dada03dbSDaniel Bristot de Oliveira 		if (!isdigit(*t_name))
282dada03dbSDaniel Bristot de Oliveira 			break;
283dada03dbSDaniel Bristot de Oliveira 	}
284dada03dbSDaniel Bristot de Oliveira 
285dada03dbSDaniel Bristot de Oliveira 	if (*t_name != '\0')
286dada03dbSDaniel Bristot de Oliveira 		return 0;
287dada03dbSDaniel Bristot de Oliveira 
288dada03dbSDaniel Bristot de Oliveira 	snprintf(buffer, MAX_PATH, "/proc/%s/comm", proc_entry->d_name);
289dada03dbSDaniel Bristot de Oliveira 	comm_fd = open(buffer, O_RDONLY);
290dada03dbSDaniel Bristot de Oliveira 	if (comm_fd < 0)
291dada03dbSDaniel Bristot de Oliveira 		return 0;
292dada03dbSDaniel Bristot de Oliveira 
293dada03dbSDaniel Bristot de Oliveira 	memset(buffer, 0, MAX_PATH);
294dada03dbSDaniel Bristot de Oliveira 	retval = read(comm_fd, buffer, MAX_PATH);
295dada03dbSDaniel Bristot de Oliveira 
296dada03dbSDaniel Bristot de Oliveira 	close(comm_fd);
297dada03dbSDaniel Bristot de Oliveira 
298dada03dbSDaniel Bristot de Oliveira 	if (retval <= 0)
299dada03dbSDaniel Bristot de Oliveira 		return 0;
300dada03dbSDaniel Bristot de Oliveira 
301dada03dbSDaniel Bristot de Oliveira 	retval = strncmp(comm_prefix, buffer, strlen(comm_prefix));
302dada03dbSDaniel Bristot de Oliveira 	if (retval)
303dada03dbSDaniel Bristot de Oliveira 		return 0;
304dada03dbSDaniel Bristot de Oliveira 
305dada03dbSDaniel Bristot de Oliveira 	/* comm already have \n */
306dada03dbSDaniel Bristot de Oliveira 	debug_msg("Found workload pid:%s comm:%s", proc_entry->d_name, buffer);
307dada03dbSDaniel Bristot de Oliveira 
308dada03dbSDaniel Bristot de Oliveira 	return 1;
309dada03dbSDaniel Bristot de Oliveira }
310dada03dbSDaniel Bristot de Oliveira 
311dada03dbSDaniel Bristot de Oliveira /*
312dada03dbSDaniel Bristot de Oliveira  * set_comm_sched_attr - set sched params to threads starting with char *comm_prefix
313dada03dbSDaniel Bristot de Oliveira  *
314dada03dbSDaniel Bristot de Oliveira  * This function uses procfs to list the currently running threads and then set the
315dada03dbSDaniel Bristot de Oliveira  * sched_attr *attr to the threads that start with char *comm_prefix. It is
316b1696371SDaniel Bristot de Oliveira  * mainly used to set the priority to the kernel threads created by the
317b1696371SDaniel Bristot de Oliveira  * tracers.
318b1696371SDaniel Bristot de Oliveira  */
set_comm_sched_attr(const char * comm_prefix,struct sched_attr * attr)319dada03dbSDaniel Bristot de Oliveira int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr)
320b1696371SDaniel Bristot de Oliveira {
321dada03dbSDaniel Bristot de Oliveira 	struct dirent *proc_entry;
322dada03dbSDaniel Bristot de Oliveira 	DIR *procfs;
323b1696371SDaniel Bristot de Oliveira 	int retval;
324b1696371SDaniel Bristot de Oliveira 
325dada03dbSDaniel Bristot de Oliveira 	if (strlen(comm_prefix) >= MAX_PATH) {
326dada03dbSDaniel Bristot de Oliveira 		err_msg("Command prefix is too long: %d < strlen(%s)\n",
327dada03dbSDaniel Bristot de Oliveira 			MAX_PATH, comm_prefix);
328dada03dbSDaniel Bristot de Oliveira 		return 1;
329b1696371SDaniel Bristot de Oliveira 	}
330b1696371SDaniel Bristot de Oliveira 
331dada03dbSDaniel Bristot de Oliveira 	procfs = opendir("/proc");
332dada03dbSDaniel Bristot de Oliveira 	if (!procfs) {
333dada03dbSDaniel Bristot de Oliveira 		err_msg("Could not open procfs\n");
334dada03dbSDaniel Bristot de Oliveira 		return 1;
335dada03dbSDaniel Bristot de Oliveira 	}
336b1696371SDaniel Bristot de Oliveira 
337dada03dbSDaniel Bristot de Oliveira 	while ((proc_entry = readdir(procfs))) {
338dada03dbSDaniel Bristot de Oliveira 
339dada03dbSDaniel Bristot de Oliveira 		retval = procfs_is_workload_pid(comm_prefix, proc_entry);
340dada03dbSDaniel Bristot de Oliveira 		if (!retval)
341b1696371SDaniel Bristot de Oliveira 			continue;
342dada03dbSDaniel Bristot de Oliveira 
343dada03dbSDaniel Bristot de Oliveira 		/* procfs_is_workload_pid confirmed it is a pid */
344dada03dbSDaniel Bristot de Oliveira 		retval = __set_sched_attr(atoi(proc_entry->d_name), attr);
345dada03dbSDaniel Bristot de Oliveira 		if (retval) {
346dada03dbSDaniel Bristot de Oliveira 			err_msg("Error setting sched attributes for pid:%s\n", proc_entry->d_name);
347b1696371SDaniel Bristot de Oliveira 			goto out_err;
348b1696371SDaniel Bristot de Oliveira 		}
349b1696371SDaniel Bristot de Oliveira 
350dada03dbSDaniel Bristot de Oliveira 		debug_msg("Set sched attributes for pid:%s\n", proc_entry->d_name);
351dada03dbSDaniel Bristot de Oliveira 	}
352b1696371SDaniel Bristot de Oliveira 	return 0;
353b1696371SDaniel Bristot de Oliveira 
354b1696371SDaniel Bristot de Oliveira out_err:
355dada03dbSDaniel Bristot de Oliveira 	closedir(procfs);
356b1696371SDaniel Bristot de Oliveira 	return 1;
357b1696371SDaniel Bristot de Oliveira }
358b1696371SDaniel Bristot de Oliveira 
359b1696371SDaniel Bristot de Oliveira #define INVALID_VAL	(~0L)
get_long_ns_after_colon(char * start)360b1696371SDaniel Bristot de Oliveira static long get_long_ns_after_colon(char *start)
361b1696371SDaniel Bristot de Oliveira {
362b1696371SDaniel Bristot de Oliveira 	long val = INVALID_VAL;
363b1696371SDaniel Bristot de Oliveira 
364b1696371SDaniel Bristot de Oliveira 	/* find the ":" */
365b1696371SDaniel Bristot de Oliveira 	start = strstr(start, ":");
366b1696371SDaniel Bristot de Oliveira 	if (!start)
367b1696371SDaniel Bristot de Oliveira 		return -1;
368b1696371SDaniel Bristot de Oliveira 
369b1696371SDaniel Bristot de Oliveira 	/* skip ":" */
370b1696371SDaniel Bristot de Oliveira 	start++;
371b1696371SDaniel Bristot de Oliveira 	val = parse_ns_duration(start);
372b1696371SDaniel Bristot de Oliveira 
373b1696371SDaniel Bristot de Oliveira 	return val;
374b1696371SDaniel Bristot de Oliveira }
375b1696371SDaniel Bristot de Oliveira 
get_long_after_colon(char * start)376b1696371SDaniel Bristot de Oliveira static long get_long_after_colon(char *start)
377b1696371SDaniel Bristot de Oliveira {
378b1696371SDaniel Bristot de Oliveira 	long val = INVALID_VAL;
379b1696371SDaniel Bristot de Oliveira 
380b1696371SDaniel Bristot de Oliveira 	/* find the ":" */
381b1696371SDaniel Bristot de Oliveira 	start = strstr(start, ":");
382b1696371SDaniel Bristot de Oliveira 	if (!start)
383b1696371SDaniel Bristot de Oliveira 		return -1;
384b1696371SDaniel Bristot de Oliveira 
385b1696371SDaniel Bristot de Oliveira 	/* skip ":" */
386b1696371SDaniel Bristot de Oliveira 	start++;
387b1696371SDaniel Bristot de Oliveira 	val = get_llong_from_str(start);
388b1696371SDaniel Bristot de Oliveira 
389b1696371SDaniel Bristot de Oliveira 	return val;
390b1696371SDaniel Bristot de Oliveira }
391b1696371SDaniel Bristot de Oliveira 
392b1696371SDaniel Bristot de Oliveira /*
393b1696371SDaniel Bristot de Oliveira  * parse priority in the format:
394b1696371SDaniel Bristot de Oliveira  * SCHED_OTHER:
395b1696371SDaniel Bristot de Oliveira  *		o:<prio>
396b1696371SDaniel Bristot de Oliveira  *		O:<prio>
397b1696371SDaniel Bristot de Oliveira  * SCHED_RR:
398b1696371SDaniel Bristot de Oliveira  *		r:<prio>
399b1696371SDaniel Bristot de Oliveira  *		R:<prio>
400b1696371SDaniel Bristot de Oliveira  * SCHED_FIFO:
401b1696371SDaniel Bristot de Oliveira  *		f:<prio>
402b1696371SDaniel Bristot de Oliveira  *		F:<prio>
403b1696371SDaniel Bristot de Oliveira  * SCHED_DEADLINE:
404b1696371SDaniel Bristot de Oliveira  *		d:runtime:period
405b1696371SDaniel Bristot de Oliveira  *		D:runtime:period
406b1696371SDaniel Bristot de Oliveira  */
parse_prio(char * arg,struct sched_attr * sched_param)407b1696371SDaniel Bristot de Oliveira int parse_prio(char *arg, struct sched_attr *sched_param)
408b1696371SDaniel Bristot de Oliveira {
409b1696371SDaniel Bristot de Oliveira 	long prio;
410b1696371SDaniel Bristot de Oliveira 	long runtime;
411b1696371SDaniel Bristot de Oliveira 	long period;
412b1696371SDaniel Bristot de Oliveira 
413b1696371SDaniel Bristot de Oliveira 	memset(sched_param, 0, sizeof(*sched_param));
414b1696371SDaniel Bristot de Oliveira 	sched_param->size = sizeof(*sched_param);
415b1696371SDaniel Bristot de Oliveira 
416b1696371SDaniel Bristot de Oliveira 	switch (arg[0]) {
417b1696371SDaniel Bristot de Oliveira 	case 'd':
418b1696371SDaniel Bristot de Oliveira 	case 'D':
419b1696371SDaniel Bristot de Oliveira 		/* d:runtime:period */
420b1696371SDaniel Bristot de Oliveira 		if (strlen(arg) < 4)
421b1696371SDaniel Bristot de Oliveira 			return -1;
422b1696371SDaniel Bristot de Oliveira 
423b1696371SDaniel Bristot de Oliveira 		runtime = get_long_ns_after_colon(arg);
424b1696371SDaniel Bristot de Oliveira 		if (runtime == INVALID_VAL)
425b1696371SDaniel Bristot de Oliveira 			return -1;
426b1696371SDaniel Bristot de Oliveira 
427b1696371SDaniel Bristot de Oliveira 		period = get_long_ns_after_colon(&arg[2]);
428b1696371SDaniel Bristot de Oliveira 		if (period == INVALID_VAL)
429b1696371SDaniel Bristot de Oliveira 			return -1;
430b1696371SDaniel Bristot de Oliveira 
431b1696371SDaniel Bristot de Oliveira 		if (runtime > period)
432b1696371SDaniel Bristot de Oliveira 			return -1;
433b1696371SDaniel Bristot de Oliveira 
434b1696371SDaniel Bristot de Oliveira 		sched_param->sched_policy   = SCHED_DEADLINE;
435b1696371SDaniel Bristot de Oliveira 		sched_param->sched_runtime  = runtime;
436b1696371SDaniel Bristot de Oliveira 		sched_param->sched_deadline = period;
437b1696371SDaniel Bristot de Oliveira 		sched_param->sched_period   = period;
438b1696371SDaniel Bristot de Oliveira 		break;
439b1696371SDaniel Bristot de Oliveira 	case 'f':
440b1696371SDaniel Bristot de Oliveira 	case 'F':
441b1696371SDaniel Bristot de Oliveira 		/* f:prio */
442b1696371SDaniel Bristot de Oliveira 		prio = get_long_after_colon(arg);
443b1696371SDaniel Bristot de Oliveira 		if (prio == INVALID_VAL)
444b1696371SDaniel Bristot de Oliveira 			return -1;
445b1696371SDaniel Bristot de Oliveira 
446b1696371SDaniel Bristot de Oliveira 		if (prio < sched_get_priority_min(SCHED_FIFO))
447b1696371SDaniel Bristot de Oliveira 			return -1;
448b1696371SDaniel Bristot de Oliveira 		if (prio > sched_get_priority_max(SCHED_FIFO))
449b1696371SDaniel Bristot de Oliveira 			return -1;
450b1696371SDaniel Bristot de Oliveira 
451b1696371SDaniel Bristot de Oliveira 		sched_param->sched_policy   = SCHED_FIFO;
452b1696371SDaniel Bristot de Oliveira 		sched_param->sched_priority = prio;
453b1696371SDaniel Bristot de Oliveira 		break;
454b1696371SDaniel Bristot de Oliveira 	case 'r':
455b1696371SDaniel Bristot de Oliveira 	case 'R':
456b1696371SDaniel Bristot de Oliveira 		/* r:prio */
457b1696371SDaniel Bristot de Oliveira 		prio = get_long_after_colon(arg);
458b1696371SDaniel Bristot de Oliveira 		if (prio == INVALID_VAL)
459b1696371SDaniel Bristot de Oliveira 			return -1;
460b1696371SDaniel Bristot de Oliveira 
461b1696371SDaniel Bristot de Oliveira 		if (prio < sched_get_priority_min(SCHED_RR))
462b1696371SDaniel Bristot de Oliveira 			return -1;
463b1696371SDaniel Bristot de Oliveira 		if (prio > sched_get_priority_max(SCHED_RR))
464b1696371SDaniel Bristot de Oliveira 			return -1;
465b1696371SDaniel Bristot de Oliveira 
466b1696371SDaniel Bristot de Oliveira 		sched_param->sched_policy   = SCHED_RR;
467b1696371SDaniel Bristot de Oliveira 		sched_param->sched_priority = prio;
468b1696371SDaniel Bristot de Oliveira 		break;
469b1696371SDaniel Bristot de Oliveira 	case 'o':
470b1696371SDaniel Bristot de Oliveira 	case 'O':
471b1696371SDaniel Bristot de Oliveira 		/* o:prio */
472b1696371SDaniel Bristot de Oliveira 		prio = get_long_after_colon(arg);
473b1696371SDaniel Bristot de Oliveira 		if (prio == INVALID_VAL)
474b1696371SDaniel Bristot de Oliveira 			return -1;
475b1696371SDaniel Bristot de Oliveira 
476daa5e6a4Slimingming3 		if (prio < MIN_NICE)
477b1696371SDaniel Bristot de Oliveira 			return -1;
478daa5e6a4Slimingming3 		if (prio > MAX_NICE)
479b1696371SDaniel Bristot de Oliveira 			return -1;
480b1696371SDaniel Bristot de Oliveira 
481b1696371SDaniel Bristot de Oliveira 		sched_param->sched_policy   = SCHED_OTHER;
482daa5e6a4Slimingming3 		sched_param->sched_nice = prio;
483b1696371SDaniel Bristot de Oliveira 		break;
484b1696371SDaniel Bristot de Oliveira 	default:
485b1696371SDaniel Bristot de Oliveira 		return -1;
486b1696371SDaniel Bristot de Oliveira 	}
487b1696371SDaniel Bristot de Oliveira 	return 0;
488b1696371SDaniel Bristot de Oliveira }
4897d0dc957SDaniel Bristot de Oliveira 
4907d0dc957SDaniel Bristot de Oliveira /*
4917d0dc957SDaniel Bristot de Oliveira  * set_cpu_dma_latency - set the /dev/cpu_dma_latecy
4927d0dc957SDaniel Bristot de Oliveira  *
4937d0dc957SDaniel Bristot de Oliveira  * This is used to reduce the exit from idle latency. The value
4947d0dc957SDaniel Bristot de Oliveira  * will be reset once the file descriptor of /dev/cpu_dma_latecy
4957d0dc957SDaniel Bristot de Oliveira  * is closed.
4967d0dc957SDaniel Bristot de Oliveira  *
4977d0dc957SDaniel Bristot de Oliveira  * Return: the /dev/cpu_dma_latecy file descriptor
4987d0dc957SDaniel Bristot de Oliveira  */
set_cpu_dma_latency(int32_t latency)4997d0dc957SDaniel Bristot de Oliveira int set_cpu_dma_latency(int32_t latency)
5007d0dc957SDaniel Bristot de Oliveira {
5017d0dc957SDaniel Bristot de Oliveira 	int retval;
5027d0dc957SDaniel Bristot de Oliveira 	int fd;
5037d0dc957SDaniel Bristot de Oliveira 
5047d0dc957SDaniel Bristot de Oliveira 	fd = open("/dev/cpu_dma_latency", O_RDWR);
5057d0dc957SDaniel Bristot de Oliveira 	if (fd < 0) {
5067d0dc957SDaniel Bristot de Oliveira 		err_msg("Error opening /dev/cpu_dma_latency\n");
5077d0dc957SDaniel Bristot de Oliveira 		return -1;
5087d0dc957SDaniel Bristot de Oliveira 	}
5097d0dc957SDaniel Bristot de Oliveira 
5107d0dc957SDaniel Bristot de Oliveira 	retval = write(fd, &latency, 4);
5117d0dc957SDaniel Bristot de Oliveira 	if (retval < 1) {
5127d0dc957SDaniel Bristot de Oliveira 		err_msg("Error setting /dev/cpu_dma_latency\n");
5137d0dc957SDaniel Bristot de Oliveira 		close(fd);
5147d0dc957SDaniel Bristot de Oliveira 		return -1;
5157d0dc957SDaniel Bristot de Oliveira 	}
5167d0dc957SDaniel Bristot de Oliveira 
5177d0dc957SDaniel Bristot de Oliveira 	debug_msg("Set /dev/cpu_dma_latency to %d\n", latency);
5187d0dc957SDaniel Bristot de Oliveira 
5197d0dc957SDaniel Bristot de Oliveira 	return fd;
5207d0dc957SDaniel Bristot de Oliveira }
521a957cbc0SDaniel Bristot de Oliveira 
522a957cbc0SDaniel Bristot de Oliveira #define _STR(x) #x
523a957cbc0SDaniel Bristot de Oliveira #define STR(x) _STR(x)
524a957cbc0SDaniel Bristot de Oliveira 
525a957cbc0SDaniel Bristot de Oliveira /*
526a957cbc0SDaniel Bristot de Oliveira  * find_mount - find a the mount point of a given fs
527a957cbc0SDaniel Bristot de Oliveira  *
528a957cbc0SDaniel Bristot de Oliveira  * Returns 0 if mount is not found, otherwise return 1 and fill mp
529a957cbc0SDaniel Bristot de Oliveira  * with the mount point.
530a957cbc0SDaniel Bristot de Oliveira  */
find_mount(const char * fs,char * mp,int sizeof_mp)531a957cbc0SDaniel Bristot de Oliveira static const int find_mount(const char *fs, char *mp, int sizeof_mp)
532a957cbc0SDaniel Bristot de Oliveira {
5338a585914SDaniel Bristot de Oliveira 	char mount_point[MAX_PATH+1];
534a957cbc0SDaniel Bristot de Oliveira 	char type[100];
53558d47044SColin Ian King 	int found = 0;
536a957cbc0SDaniel Bristot de Oliveira 	FILE *fp;
537a957cbc0SDaniel Bristot de Oliveira 
538a957cbc0SDaniel Bristot de Oliveira 	fp = fopen("/proc/mounts", "r");
539a957cbc0SDaniel Bristot de Oliveira 	if (!fp)
540a957cbc0SDaniel Bristot de Oliveira 		return 0;
541a957cbc0SDaniel Bristot de Oliveira 
542a957cbc0SDaniel Bristot de Oliveira 	while (fscanf(fp, "%*s %" STR(MAX_PATH) "s %99s %*s %*d %*d\n",	mount_point, type) == 2) {
543a957cbc0SDaniel Bristot de Oliveira 		if (strcmp(type, fs) == 0) {
544a957cbc0SDaniel Bristot de Oliveira 			found = 1;
545a957cbc0SDaniel Bristot de Oliveira 			break;
546a957cbc0SDaniel Bristot de Oliveira 		}
547a957cbc0SDaniel Bristot de Oliveira 	}
548a957cbc0SDaniel Bristot de Oliveira 	fclose(fp);
549a957cbc0SDaniel Bristot de Oliveira 
550a957cbc0SDaniel Bristot de Oliveira 	if (!found)
551a957cbc0SDaniel Bristot de Oliveira 		return 0;
552a957cbc0SDaniel Bristot de Oliveira 
553a957cbc0SDaniel Bristot de Oliveira 	memset(mp, 0, sizeof_mp);
554a957cbc0SDaniel Bristot de Oliveira 	strncpy(mp, mount_point, sizeof_mp - 1);
555a957cbc0SDaniel Bristot de Oliveira 
556a957cbc0SDaniel Bristot de Oliveira 	debug_msg("Fs %s found at %s\n", fs, mp);
557a957cbc0SDaniel Bristot de Oliveira 	return 1;
558a957cbc0SDaniel Bristot de Oliveira }
559a957cbc0SDaniel Bristot de Oliveira 
560a957cbc0SDaniel Bristot de Oliveira /*
561a957cbc0SDaniel Bristot de Oliveira  * get_self_cgroup - get the current thread cgroup path
562a957cbc0SDaniel Bristot de Oliveira  *
563a957cbc0SDaniel Bristot de Oliveira  * Parse /proc/$$/cgroup file to get the thread's cgroup. As an example of line to parse:
564a957cbc0SDaniel Bristot de Oliveira  *
565a957cbc0SDaniel Bristot de Oliveira  * 0::/user.slice/user-0.slice/session-3.scope'\n'
566a957cbc0SDaniel Bristot de Oliveira  *
567a957cbc0SDaniel Bristot de Oliveira  * This function is interested in the content after the second : and before the '\n'.
568a957cbc0SDaniel Bristot de Oliveira  *
569a957cbc0SDaniel Bristot de Oliveira  * Returns 1 if a string was found, 0 otherwise.
570a957cbc0SDaniel Bristot de Oliveira  */
get_self_cgroup(char * self_cg,int sizeof_self_cg)571a957cbc0SDaniel Bristot de Oliveira static int get_self_cgroup(char *self_cg, int sizeof_self_cg)
572a957cbc0SDaniel Bristot de Oliveira {
573a957cbc0SDaniel Bristot de Oliveira 	char path[MAX_PATH], *start;
574a957cbc0SDaniel Bristot de Oliveira 	int fd, retval;
575a957cbc0SDaniel Bristot de Oliveira 
576a957cbc0SDaniel Bristot de Oliveira 	snprintf(path, MAX_PATH, "/proc/%d/cgroup", getpid());
577a957cbc0SDaniel Bristot de Oliveira 
578a957cbc0SDaniel Bristot de Oliveira 	fd = open(path, O_RDONLY);
579a957cbc0SDaniel Bristot de Oliveira 	if (fd < 0)
580a957cbc0SDaniel Bristot de Oliveira 		return 0;
581a957cbc0SDaniel Bristot de Oliveira 
582a957cbc0SDaniel Bristot de Oliveira 	retval = read(fd, path, MAX_PATH);
583a957cbc0SDaniel Bristot de Oliveira 
584a957cbc0SDaniel Bristot de Oliveira 	close(fd);
585a957cbc0SDaniel Bristot de Oliveira 
586a957cbc0SDaniel Bristot de Oliveira 	if (retval <= 0)
587a957cbc0SDaniel Bristot de Oliveira 		return 0;
588a957cbc0SDaniel Bristot de Oliveira 
589a957cbc0SDaniel Bristot de Oliveira 	start = path;
590a957cbc0SDaniel Bristot de Oliveira 
591a957cbc0SDaniel Bristot de Oliveira 	start = strstr(start, ":");
592a957cbc0SDaniel Bristot de Oliveira 	if (!start)
593a957cbc0SDaniel Bristot de Oliveira 		return 0;
594a957cbc0SDaniel Bristot de Oliveira 
595a957cbc0SDaniel Bristot de Oliveira 	/* skip ":" */
596a957cbc0SDaniel Bristot de Oliveira 	start++;
597a957cbc0SDaniel Bristot de Oliveira 
598a957cbc0SDaniel Bristot de Oliveira 	start = strstr(start, ":");
599a957cbc0SDaniel Bristot de Oliveira 	if (!start)
600a957cbc0SDaniel Bristot de Oliveira 		return 0;
601a957cbc0SDaniel Bristot de Oliveira 
602a957cbc0SDaniel Bristot de Oliveira 	/* skip ":" */
603a957cbc0SDaniel Bristot de Oliveira 	start++;
604a957cbc0SDaniel Bristot de Oliveira 
605a957cbc0SDaniel Bristot de Oliveira 	if (strlen(start) >= sizeof_self_cg)
606a957cbc0SDaniel Bristot de Oliveira 		return 0;
607a957cbc0SDaniel Bristot de Oliveira 
608a957cbc0SDaniel Bristot de Oliveira 	snprintf(self_cg, sizeof_self_cg, "%s", start);
609a957cbc0SDaniel Bristot de Oliveira 
610a957cbc0SDaniel Bristot de Oliveira 	/* Swap '\n' with '\0' */
611a957cbc0SDaniel Bristot de Oliveira 	start = strstr(self_cg, "\n");
612a957cbc0SDaniel Bristot de Oliveira 
613a957cbc0SDaniel Bristot de Oliveira 	/* there must be '\n' */
614a957cbc0SDaniel Bristot de Oliveira 	if (!start)
615a957cbc0SDaniel Bristot de Oliveira 		return 0;
616a957cbc0SDaniel Bristot de Oliveira 
617a957cbc0SDaniel Bristot de Oliveira 	/* ok, it found a string after the second : and before the \n */
618a957cbc0SDaniel Bristot de Oliveira 	*start = '\0';
619a957cbc0SDaniel Bristot de Oliveira 
620a957cbc0SDaniel Bristot de Oliveira 	return 1;
621a957cbc0SDaniel Bristot de Oliveira }
622a957cbc0SDaniel Bristot de Oliveira 
623cdca4f4eSDaniel Bristot de Oliveira /*
624cdca4f4eSDaniel Bristot de Oliveira  * set_comm_cgroup - Set cgroup to pid_t pid
625cdca4f4eSDaniel Bristot de Oliveira  *
626cdca4f4eSDaniel Bristot de Oliveira  * If cgroup argument is not NULL, the threads will move to the given cgroup.
627cdca4f4eSDaniel Bristot de Oliveira  * Otherwise, the cgroup of the calling, i.e., rtla, thread will be used.
628cdca4f4eSDaniel Bristot de Oliveira  *
629cdca4f4eSDaniel Bristot de Oliveira  * Supports cgroup v2.
630cdca4f4eSDaniel Bristot de Oliveira  *
631cdca4f4eSDaniel Bristot de Oliveira  * Returns 1 on success, 0 otherwise.
632cdca4f4eSDaniel Bristot de Oliveira  */
set_pid_cgroup(pid_t pid,const char * cgroup)633cdca4f4eSDaniel Bristot de Oliveira int set_pid_cgroup(pid_t pid, const char *cgroup)
634cdca4f4eSDaniel Bristot de Oliveira {
635cdca4f4eSDaniel Bristot de Oliveira 	char cgroup_path[MAX_PATH - strlen("/cgroup.procs")];
636cdca4f4eSDaniel Bristot de Oliveira 	char cgroup_procs[MAX_PATH];
637cdca4f4eSDaniel Bristot de Oliveira 	char pid_str[24];
638cdca4f4eSDaniel Bristot de Oliveira 	int retval;
639cdca4f4eSDaniel Bristot de Oliveira 	int cg_fd;
640cdca4f4eSDaniel Bristot de Oliveira 
641cdca4f4eSDaniel Bristot de Oliveira 	retval = find_mount("cgroup2", cgroup_path, sizeof(cgroup_path));
642cdca4f4eSDaniel Bristot de Oliveira 	if (!retval) {
643cdca4f4eSDaniel Bristot de Oliveira 		err_msg("Did not find cgroupv2 mount point\n");
644cdca4f4eSDaniel Bristot de Oliveira 		return 0;
645cdca4f4eSDaniel Bristot de Oliveira 	}
646cdca4f4eSDaniel Bristot de Oliveira 
647cdca4f4eSDaniel Bristot de Oliveira 	if (!cgroup) {
648cdca4f4eSDaniel Bristot de Oliveira 		retval = get_self_cgroup(&cgroup_path[strlen(cgroup_path)],
649cdca4f4eSDaniel Bristot de Oliveira 				sizeof(cgroup_path) - strlen(cgroup_path));
650cdca4f4eSDaniel Bristot de Oliveira 		if (!retval) {
651cdca4f4eSDaniel Bristot de Oliveira 			err_msg("Did not find self cgroup\n");
652cdca4f4eSDaniel Bristot de Oliveira 			return 0;
653cdca4f4eSDaniel Bristot de Oliveira 		}
654cdca4f4eSDaniel Bristot de Oliveira 	} else {
655cdca4f4eSDaniel Bristot de Oliveira 		snprintf(&cgroup_path[strlen(cgroup_path)],
656cdca4f4eSDaniel Bristot de Oliveira 				sizeof(cgroup_path) - strlen(cgroup_path), "%s/", cgroup);
657cdca4f4eSDaniel Bristot de Oliveira 	}
658cdca4f4eSDaniel Bristot de Oliveira 
659cdca4f4eSDaniel Bristot de Oliveira 	snprintf(cgroup_procs, MAX_PATH, "%s/cgroup.procs", cgroup_path);
660cdca4f4eSDaniel Bristot de Oliveira 
661cdca4f4eSDaniel Bristot de Oliveira 	debug_msg("Using cgroup path at: %s\n", cgroup_procs);
662cdca4f4eSDaniel Bristot de Oliveira 
663cdca4f4eSDaniel Bristot de Oliveira 	cg_fd = open(cgroup_procs, O_RDWR);
664cdca4f4eSDaniel Bristot de Oliveira 	if (cg_fd < 0)
665cdca4f4eSDaniel Bristot de Oliveira 		return 0;
666cdca4f4eSDaniel Bristot de Oliveira 
667cdca4f4eSDaniel Bristot de Oliveira 	snprintf(pid_str, sizeof(pid_str), "%d\n", pid);
668cdca4f4eSDaniel Bristot de Oliveira 
669cdca4f4eSDaniel Bristot de Oliveira 	retval = write(cg_fd, pid_str, strlen(pid_str));
670cdca4f4eSDaniel Bristot de Oliveira 	if (retval < 0)
671cdca4f4eSDaniel Bristot de Oliveira 		err_msg("Error setting cgroup attributes for pid:%s - %s\n",
672cdca4f4eSDaniel Bristot de Oliveira 				pid_str, strerror(errno));
673cdca4f4eSDaniel Bristot de Oliveira 	else
674cdca4f4eSDaniel Bristot de Oliveira 		debug_msg("Set cgroup attributes for pid:%s\n", pid_str);
675cdca4f4eSDaniel Bristot de Oliveira 
676cdca4f4eSDaniel Bristot de Oliveira 	close(cg_fd);
677cdca4f4eSDaniel Bristot de Oliveira 
678cdca4f4eSDaniel Bristot de Oliveira 	return (retval >= 0);
679cdca4f4eSDaniel Bristot de Oliveira }
680cdca4f4eSDaniel Bristot de Oliveira 
681a957cbc0SDaniel Bristot de Oliveira /**
682a957cbc0SDaniel Bristot de Oliveira  * set_comm_cgroup - Set cgroup to threads starting with char *comm_prefix
683a957cbc0SDaniel Bristot de Oliveira  *
684a957cbc0SDaniel Bristot de Oliveira  * If cgroup argument is not NULL, the threads will move to the given cgroup.
685a957cbc0SDaniel Bristot de Oliveira  * Otherwise, the cgroup of the calling, i.e., rtla, thread will be used.
686a957cbc0SDaniel Bristot de Oliveira  *
687a957cbc0SDaniel Bristot de Oliveira  * Supports cgroup v2.
688a957cbc0SDaniel Bristot de Oliveira  *
689a957cbc0SDaniel Bristot de Oliveira  * Returns 1 on success, 0 otherwise.
690a957cbc0SDaniel Bristot de Oliveira  */
set_comm_cgroup(const char * comm_prefix,const char * cgroup)691a957cbc0SDaniel Bristot de Oliveira int set_comm_cgroup(const char *comm_prefix, const char *cgroup)
692a957cbc0SDaniel Bristot de Oliveira {
693a957cbc0SDaniel Bristot de Oliveira 	char cgroup_path[MAX_PATH - strlen("/cgroup.procs")];
694a957cbc0SDaniel Bristot de Oliveira 	char cgroup_procs[MAX_PATH];
695a957cbc0SDaniel Bristot de Oliveira 	struct dirent *proc_entry;
696a957cbc0SDaniel Bristot de Oliveira 	DIR *procfs;
697a957cbc0SDaniel Bristot de Oliveira 	int retval;
698a957cbc0SDaniel Bristot de Oliveira 	int cg_fd;
699a957cbc0SDaniel Bristot de Oliveira 
700a957cbc0SDaniel Bristot de Oliveira 	if (strlen(comm_prefix) >= MAX_PATH) {
701a957cbc0SDaniel Bristot de Oliveira 		err_msg("Command prefix is too long: %d < strlen(%s)\n",
702a957cbc0SDaniel Bristot de Oliveira 			MAX_PATH, comm_prefix);
703a957cbc0SDaniel Bristot de Oliveira 		return 0;
704a957cbc0SDaniel Bristot de Oliveira 	}
705a957cbc0SDaniel Bristot de Oliveira 
706a957cbc0SDaniel Bristot de Oliveira 	retval = find_mount("cgroup2", cgroup_path, sizeof(cgroup_path));
707a957cbc0SDaniel Bristot de Oliveira 	if (!retval) {
708a957cbc0SDaniel Bristot de Oliveira 		err_msg("Did not find cgroupv2 mount point\n");
709a957cbc0SDaniel Bristot de Oliveira 		return 0;
710a957cbc0SDaniel Bristot de Oliveira 	}
711a957cbc0SDaniel Bristot de Oliveira 
712a957cbc0SDaniel Bristot de Oliveira 	if (!cgroup) {
713a957cbc0SDaniel Bristot de Oliveira 		retval = get_self_cgroup(&cgroup_path[strlen(cgroup_path)],
714a957cbc0SDaniel Bristot de Oliveira 				sizeof(cgroup_path) - strlen(cgroup_path));
715a957cbc0SDaniel Bristot de Oliveira 		if (!retval) {
716a957cbc0SDaniel Bristot de Oliveira 			err_msg("Did not find self cgroup\n");
717a957cbc0SDaniel Bristot de Oliveira 			return 0;
718a957cbc0SDaniel Bristot de Oliveira 		}
719a957cbc0SDaniel Bristot de Oliveira 	} else {
720a957cbc0SDaniel Bristot de Oliveira 		snprintf(&cgroup_path[strlen(cgroup_path)],
721a957cbc0SDaniel Bristot de Oliveira 				sizeof(cgroup_path) - strlen(cgroup_path), "%s/", cgroup);
722a957cbc0SDaniel Bristot de Oliveira 	}
723a957cbc0SDaniel Bristot de Oliveira 
724a957cbc0SDaniel Bristot de Oliveira 	snprintf(cgroup_procs, MAX_PATH, "%s/cgroup.procs", cgroup_path);
725a957cbc0SDaniel Bristot de Oliveira 
726a957cbc0SDaniel Bristot de Oliveira 	debug_msg("Using cgroup path at: %s\n", cgroup_procs);
727a957cbc0SDaniel Bristot de Oliveira 
728a957cbc0SDaniel Bristot de Oliveira 	cg_fd = open(cgroup_procs, O_RDWR);
729a957cbc0SDaniel Bristot de Oliveira 	if (cg_fd < 0)
730a957cbc0SDaniel Bristot de Oliveira 		return 0;
731a957cbc0SDaniel Bristot de Oliveira 
732a957cbc0SDaniel Bristot de Oliveira 	procfs = opendir("/proc");
733a957cbc0SDaniel Bristot de Oliveira 	if (!procfs) {
734a957cbc0SDaniel Bristot de Oliveira 		err_msg("Could not open procfs\n");
735a957cbc0SDaniel Bristot de Oliveira 		goto out_cg;
736a957cbc0SDaniel Bristot de Oliveira 	}
737a957cbc0SDaniel Bristot de Oliveira 
738a957cbc0SDaniel Bristot de Oliveira 	while ((proc_entry = readdir(procfs))) {
739a957cbc0SDaniel Bristot de Oliveira 
740a957cbc0SDaniel Bristot de Oliveira 		retval = procfs_is_workload_pid(comm_prefix, proc_entry);
741a957cbc0SDaniel Bristot de Oliveira 		if (!retval)
742a957cbc0SDaniel Bristot de Oliveira 			continue;
743a957cbc0SDaniel Bristot de Oliveira 
744a957cbc0SDaniel Bristot de Oliveira 		retval = write(cg_fd, proc_entry->d_name, strlen(proc_entry->d_name));
745a957cbc0SDaniel Bristot de Oliveira 		if (retval < 0) {
746a957cbc0SDaniel Bristot de Oliveira 			err_msg("Error setting cgroup attributes for pid:%s - %s\n",
747a957cbc0SDaniel Bristot de Oliveira 				proc_entry->d_name, strerror(errno));
748a957cbc0SDaniel Bristot de Oliveira 			goto out_procfs;
749a957cbc0SDaniel Bristot de Oliveira 		}
750a957cbc0SDaniel Bristot de Oliveira 
751a957cbc0SDaniel Bristot de Oliveira 		debug_msg("Set cgroup attributes for pid:%s\n", proc_entry->d_name);
752a957cbc0SDaniel Bristot de Oliveira 	}
753a957cbc0SDaniel Bristot de Oliveira 
754a957cbc0SDaniel Bristot de Oliveira 	closedir(procfs);
755a957cbc0SDaniel Bristot de Oliveira 	close(cg_fd);
756a957cbc0SDaniel Bristot de Oliveira 	return 1;
757a957cbc0SDaniel Bristot de Oliveira 
758a957cbc0SDaniel Bristot de Oliveira out_procfs:
759a957cbc0SDaniel Bristot de Oliveira 	closedir(procfs);
760a957cbc0SDaniel Bristot de Oliveira out_cg:
761a957cbc0SDaniel Bristot de Oliveira 	close(cg_fd);
762a957cbc0SDaniel Bristot de Oliveira 	return 0;
763a957cbc0SDaniel Bristot de Oliveira }
764c58a3f8cSDaniel Bristot de Oliveira 
765c58a3f8cSDaniel Bristot de Oliveira /**
766c58a3f8cSDaniel Bristot de Oliveira  * auto_house_keeping - Automatically move rtla out of measurement threads
767c58a3f8cSDaniel Bristot de Oliveira  *
768c58a3f8cSDaniel Bristot de Oliveira  * Try to move rtla away from the tracer, if possible.
769c58a3f8cSDaniel Bristot de Oliveira  *
770c58a3f8cSDaniel Bristot de Oliveira  * Returns 1 on success, 0 otherwise.
771c58a3f8cSDaniel Bristot de Oliveira  */
auto_house_keeping(cpu_set_t * monitored_cpus)772c58a3f8cSDaniel Bristot de Oliveira int auto_house_keeping(cpu_set_t *monitored_cpus)
773c58a3f8cSDaniel Bristot de Oliveira {
774c58a3f8cSDaniel Bristot de Oliveira 	cpu_set_t rtla_cpus, house_keeping_cpus;
775c58a3f8cSDaniel Bristot de Oliveira 	int retval;
776c58a3f8cSDaniel Bristot de Oliveira 
777c58a3f8cSDaniel Bristot de Oliveira 	/* first get the CPUs in which rtla can actually run. */
778c58a3f8cSDaniel Bristot de Oliveira 	retval = sched_getaffinity(getpid(), sizeof(rtla_cpus), &rtla_cpus);
779c58a3f8cSDaniel Bristot de Oliveira 	if (retval == -1) {
780c58a3f8cSDaniel Bristot de Oliveira 		debug_msg("Could not get rtla affinity, rtla might run with the threads!\n");
781c58a3f8cSDaniel Bristot de Oliveira 		return 0;
782c58a3f8cSDaniel Bristot de Oliveira 	}
783c58a3f8cSDaniel Bristot de Oliveira 
784c58a3f8cSDaniel Bristot de Oliveira 	/* then check if the existing setup is already good. */
785c58a3f8cSDaniel Bristot de Oliveira 	CPU_AND(&house_keeping_cpus, &rtla_cpus, monitored_cpus);
786c58a3f8cSDaniel Bristot de Oliveira 	if (!CPU_COUNT(&house_keeping_cpus)) {
787c58a3f8cSDaniel Bristot de Oliveira 		debug_msg("rtla and the monitored CPUs do not share CPUs.");
788c58a3f8cSDaniel Bristot de Oliveira 		debug_msg("Skipping auto house-keeping\n");
789c58a3f8cSDaniel Bristot de Oliveira 		return 1;
790c58a3f8cSDaniel Bristot de Oliveira 	}
791c58a3f8cSDaniel Bristot de Oliveira 
792c58a3f8cSDaniel Bristot de Oliveira 	/* remove the intersection */
793c58a3f8cSDaniel Bristot de Oliveira 	CPU_XOR(&house_keeping_cpus, &rtla_cpus, monitored_cpus);
794c58a3f8cSDaniel Bristot de Oliveira 
795c58a3f8cSDaniel Bristot de Oliveira 	/* get only those that rtla can run */
796c58a3f8cSDaniel Bristot de Oliveira 	CPU_AND(&house_keeping_cpus, &house_keeping_cpus, &rtla_cpus);
797c58a3f8cSDaniel Bristot de Oliveira 
798c58a3f8cSDaniel Bristot de Oliveira 	/* is there any cpu left? */
799c58a3f8cSDaniel Bristot de Oliveira 	if (!CPU_COUNT(&house_keeping_cpus)) {
800c58a3f8cSDaniel Bristot de Oliveira 		debug_msg("Could not find any CPU for auto house-keeping\n");
801c58a3f8cSDaniel Bristot de Oliveira 		return 0;
802c58a3f8cSDaniel Bristot de Oliveira 	}
803c58a3f8cSDaniel Bristot de Oliveira 
804c58a3f8cSDaniel Bristot de Oliveira 	retval = sched_setaffinity(getpid(), sizeof(house_keeping_cpus), &house_keeping_cpus);
805c58a3f8cSDaniel Bristot de Oliveira 	if (retval == -1) {
806c58a3f8cSDaniel Bristot de Oliveira 		debug_msg("Could not set affinity for auto house-keeping\n");
807c58a3f8cSDaniel Bristot de Oliveira 		return 0;
808c58a3f8cSDaniel Bristot de Oliveira 	}
809c58a3f8cSDaniel Bristot de Oliveira 
810c58a3f8cSDaniel Bristot de Oliveira 	debug_msg("rtla automatically moved to an auto house-keeping cpu set\n");
811c58a3f8cSDaniel Bristot de Oliveira 
812c58a3f8cSDaniel Bristot de Oliveira 	return 1;
813c58a3f8cSDaniel Bristot de Oliveira }
814