Debian Codegraph @

tools/tracing/rtla/src/utils.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <[email protected]>
4 */
5
6 #define _GNU_SOURCE
7 #ifdef HAVE_LIBCPUPOWER_SUPPORT
8 #include <cpuidle.h>
9 #endif /* HAVE_LIBCPUPOWER_SUPPORT */
10 #include <dirent.h>
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <ctype.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <sched.h>
19 #include <stdio.h>
20 #include <limits.h>
21
22 #include "utils.h"
23
24 #define MAX_MSG_LENGTH 1024
25 int config_debug;
26
27 /*
28 * err_msg - print an error message to the stderr
29 */
30 void err_msg(const char *fmt, ...)
31 {
32 char message[MAX_MSG_LENGTH];
33 va_list ap;
34
35 va_start(ap, fmt);
36 vsnprintf(message, sizeof(message), fmt, ap);
37 va_end(ap);
38
39 fprintf(stderr, "%s", message);
40 }
41
42 /*
43 * debug_msg - print a debug message to stderr if debug is set
44 */
45 void debug_msg(const char *fmt, ...)
46 {
47 char message[MAX_MSG_LENGTH];
48 va_list ap;
49
50 if (!config_debug)
51 return;
52
53 va_start(ap, fmt);
54 vsnprintf(message, sizeof(message), fmt, ap);
55 va_end(ap);
56
57 fprintf(stderr, "%s", message);
58 }
59
60 /*
61 * fatal - print an error message and EOL to stderr and exit with ERROR
62 */
63 void fatal(const char *fmt, ...)
64 {
65 va_list ap;
66
67 va_start(ap, fmt);
68 vfprintf(stderr, fmt, ap);
69 va_end(ap);
70 fprintf(stderr, "\n");
71
72 exit(ERROR);
73 }
74
75 /*
76 * get_llong_from_str - get a long long int from a string
77 */
78 long long get_llong_from_str(char *start)
79 {
80 long long value;
81 char *end;
82
83 errno = 0;
84 value = strtoll(start, &end, 10);
85 if (errno || start == end)
86 return -1;
87
88 return value;
89 }
90
91 /*
92 * get_duration - fill output with a human readable duration since start_time
93 */
94 void get_duration(time_t start_time, char *output, int output_size)
95 {
96 time_t now = time(NULL);
97 struct tm *tm_info;
98 time_t duration;
99
100 duration = difftime(now, start_time);
101 tm_info = gmtime(&duration);
102
103 snprintf(output, output_size, "%3d %02d:%02d:%02d",
104 tm_info->tm_yday,
105 tm_info->tm_hour,
106 tm_info->tm_min,
107 tm_info->tm_sec);
108 }
109
110 /*
111 * parse_cpu_set - parse a cpu_list filling cpu_set_t argument
112 *
113 * Receives a cpu list, like 1-3,5 (cpus 1, 2, 3, 5), and then set
114 * filling cpu_set_t argument.
115 *
116 * Returns 0 on success, 1 otherwise.
117 */
118 int parse_cpu_set(char *cpu_list, cpu_set_t *set)
119 {
120 const char *p;
121 int end_cpu;
122 int nr_cpus;
123 int cpu;
124 int i;
125
126 CPU_ZERO(set);
127
128 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
129
130 for (p = cpu_list; *p; ) {
131 cpu = atoi(p);
132 if (cpu < 0 || (!cpu && *p != '0') || cpu >= nr_cpus)
133 goto err;
134
135 while (isdigit(*p))
136 p++;
137 if (*p == '-') {
138 p++;
139 end_cpu = atoi(p);
140 if (end_cpu < cpu || (!end_cpu && *p != '0') || end_cpu >= nr_cpus)
141 goto err;
142 while (isdigit(*p))
143 p++;
144 } else
145 end_cpu = cpu;
146
147 if (cpu == end_cpu) {
148 debug_msg("cpu_set: adding cpu %d\n", cpu);
149 CPU_SET(cpu, set);
150 } else {
151 for (i = cpu; i <= end_cpu; i++) {
152 debug_msg("cpu_set: adding cpu %d\n", i);
153 CPU_SET(i, set);
154 }
155 }
156
157 if (*p == ',')
158 p++;
159 }
160
161 return 0;
162 err:
163 debug_msg("Error parsing the cpu set %s\n", cpu_list);
164 return 1;
165 }
166
167 /*
168 * parse_duration - parse duration with s/m/h/d suffix converting it to seconds
169 */
170 long parse_seconds_duration(char *val)
171 {
172 char *end;
173 long t;
174
175 t = strtol(val, &end, 10);
176
177 if (end) {
178 switch (*end) {
179 case 's':
180 case 'S':
181 break;
182 case 'm':
183 case 'M':
184 t *= 60;
185 break;
186 case 'h':
187 case 'H':
188 t *= 60 * 60;
189 break;
190
191 case 'd':
192 case 'D':
193 t *= 24 * 60 * 60;
194 break;
195 }
196 }
197
198 return t;
199 }
200
201 /*
202 * parse_ns_duration - parse duration with ns/us/ms/s converting it to nanoseconds
203 */
204 long parse_ns_duration(char *val)
205 {
206 char *end;
207 long t;
208
209 t = strtol(val, &end, 10);
210
211 if (end) {
212 if (!strncmp(end, "ns", 2)) {
213 return t;
214 } else if (!strncmp(end, "us", 2)) {
215 t *= 1000;
216 return t;
217 } else if (!strncmp(end, "ms", 2)) {
218 t *= 1000 * 1000;
219 return t;
220 } else if (!strncmp(end, "s", 1)) {
221 t *= 1000 * 1000 * 1000;
222 return t;
223 }
224 return -1;
225 }
226
227 return t;
228 }
229
230 /*
231 * This is a set of helper functions to use SCHED_DEADLINE.
232 */
233 #ifndef __NR_sched_setattr
234 # ifdef __x86_64__
235 # define __NR_sched_setattr 314
236 # elif __i386__
237 # define __NR_sched_setattr 351
238 # elif __arm__
239 # define __NR_sched_setattr 380
240 # elif __aarch64__ || __riscv
241 # define __NR_sched_setattr 274
242 # elif __powerpc__
243 # define __NR_sched_setattr 355
244 # elif __s390x__
245 # define __NR_sched_setattr 345
246 # elif __loongarch__
247 # define __NR_sched_setattr 274
248 # endif
249 #endif
250
251 #define SCHED_DEADLINE 6
252
253 static inline int syscall_sched_setattr(pid_t pid, const struct sched_attr *attr,
254 unsigned int flags) {
255 return syscall(__NR_sched_setattr, pid, attr, flags);
256 }
257
258 int __set_sched_attr(int pid, struct sched_attr *attr)
259 {
260 int flags = 0;
261 int retval;
262
263 retval = syscall_sched_setattr(pid, attr, flags);
264 if (retval < 0) {
265 err_msg("Failed to set sched attributes to the pid %d: %s\n",
266 pid, strerror(errno));
267 return 1;
268 }
269
270 return 0;
271 }
272
273 /*
274 * procfs_is_workload_pid - check if a procfs entry contains a comm_prefix* comm
275 *
276 * Check if the procfs entry is a directory of a process, and then check if the
277 * process has a comm with the prefix set in char *comm_prefix. As the
278 * current users of this function only check for kernel threads, there is no
279 * need to check for the threads for the process.
280 *
281 * Return: True if the proc_entry contains a comm file with comm_prefix*.
282 * Otherwise returns false.
283 */
284 static int procfs_is_workload_pid(const char *comm_prefix, struct dirent *proc_entry)
285 {
286 char buffer[MAX_PATH];
287 int comm_fd, retval;
288 char *t_name;
289
290 if (proc_entry->d_type != DT_DIR)
291 return 0;
292
293 if (*proc_entry->d_name == '.')
294 return 0;
295
296 /* check if the string is a pid */
297 for (t_name = proc_entry->d_name; t_name; t_name++) {
298 if (!isdigit(*t_name))
299 break;
300 }
301
302 if (*t_name != '\0')
303 return 0;
304
305 snprintf(buffer, MAX_PATH, "/proc/%s/comm", proc_entry->d_name);
306 comm_fd = open(buffer, O_RDONLY);
307 if (comm_fd < 0)
308 return 0;
309
310 memset(buffer, 0, MAX_PATH);
311 retval = read(comm_fd, buffer, MAX_PATH);
312
313 close(comm_fd);
314
315 if (retval <= 0)
316 return 0;
317
318 buffer[MAX_PATH-1] = '\0';
319 retval = strncmp(comm_prefix, buffer, strlen(comm_prefix));
320 if (retval)
321 return 0;
322
323 /* comm already have \n */
324 debug_msg("Found workload pid:%s comm:%s", proc_entry->d_name, buffer);
325
326 return 1;
327 }
328
329 /*
330 * set_comm_sched_attr - set sched params to threads starting with char *comm_prefix
331 *
332 * This function uses procfs to list the currently running threads and then set the
333 * sched_attr *attr to the threads that start with char *comm_prefix. It is
334 * mainly used to set the priority to the kernel threads created by the
335 * tracers.
336 */
337 int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr)
338 {
339 struct dirent *proc_entry;
340 DIR *procfs;
341 int retval;
342 int pid;
343
344 if (strlen(comm_prefix) >= MAX_PATH) {
345 err_msg("Command prefix is too long: %d < strlen(%s)\n",
346 MAX_PATH, comm_prefix);
347 return 1;
348 }
349
350 procfs = opendir("/proc");
351 if (!procfs) {
352 err_msg("Could not open procfs\n");
353 return 1;
354 }
355
356 while ((proc_entry = readdir(procfs))) {
357
358 retval = procfs_is_workload_pid(comm_prefix, proc_entry);
359 if (!retval)
360 continue;
361
362 if (strtoi(proc_entry->d_name, &pid)) {
363 err_msg("'%s' is not a valid pid", proc_entry->d_name);
364 retval = 1;
365 goto out;
366 }
367 /* procfs_is_workload_pid confirmed it is a pid */
368 retval = __set_sched_attr(pid, attr);
369 if (retval) {
370 err_msg("Error setting sched attributes for pid:%s\n", proc_entry->d_name);
371 goto out;
372 }
373
374 debug_msg("Set sched attributes for pid:%s\n", proc_entry->d_name);
375 }
376
377 retval = 0;
378 out:
379 closedir(procfs);
380 return retval;
381 }
382
383 #define INVALID_VAL (~0L)
384 static long get_long_ns_after_colon(char *start)
385 {
386 long val = INVALID_VAL;
387
388 /* find the ":" */
389 start = strstr(start, ":");
390 if (!start)
391 return -1;
392
393 /* skip ":" */
394 start++;
395 val = parse_ns_duration(start);
396
397 return val;
398 }
399
400 static long get_long_after_colon(char *start)
401 {
402 long val = INVALID_VAL;
403
404 /* find the ":" */
405 start = strstr(start, ":");
406 if (!start)
407 return -1;
408
409 /* skip ":" */
410 start++;
411 val = get_llong_from_str(start);
412
413 return val;
414 }
415
416 /*
417 * parse priority in the format:
418 * SCHED_OTHER:
419 * o:<prio>
420 * O:<prio>
421 * SCHED_RR:
422 * r:<prio>
423 * R:<prio>
424 * SCHED_FIFO:
425 * f:<prio>
426 * F:<prio>
427 * SCHED_DEADLINE:
428 * d:runtime:period
429 * D:runtime:period
430 */
431 int parse_prio(char *arg, struct sched_attr *sched_param)
432 {
433 long prio;
434 long runtime;
435 long period;
436
437 memset(sched_param, 0, sizeof(*sched_param));
438 sched_param->size = sizeof(*sched_param);
439
440 switch (arg[0]) {
441 case 'd':
442 case 'D':
443 /* d:runtime:period */
444 if (strlen(arg) < 4)
445 return -1;
446
447 runtime = get_long_ns_after_colon(arg);
448 if (runtime == INVALID_VAL)
449 return -1;
450
451 period = get_long_ns_after_colon(&arg[2]);
452 if (period == INVALID_VAL)
453 return -1;
454
455 if (runtime > period)
456 return -1;
457
458 sched_param->sched_policy = SCHED_DEADLINE;
459 sched_param->sched_runtime = runtime;
460 sched_param->sched_deadline = period;
461 sched_param->sched_period = period;
462 break;
463 case 'f':
464 case 'F':
465 /* f:prio */
466 prio = get_long_after_colon(arg);
467 if (prio == INVALID_VAL)
468 return -1;
469
470 if (prio < sched_get_priority_min(SCHED_FIFO))
471 return -1;
472 if (prio > sched_get_priority_max(SCHED_FIFO))
473 return -1;
474
475 sched_param->sched_policy = SCHED_FIFO;
476 sched_param->sched_priority = prio;
477 break;
478 case 'r':
479 case 'R':
480 /* r:prio */
481 prio = get_long_after_colon(arg);
482 if (prio == INVALID_VAL)
483 return -1;
484
485 if (prio < sched_get_priority_min(SCHED_RR))
486 return -1;
487 if (prio > sched_get_priority_max(SCHED_RR))
488 return -1;
489
490 sched_param->sched_policy = SCHED_RR;
491 sched_param->sched_priority = prio;
492 break;
493 case 'o':
494 case 'O':
495 /* o:prio */
496 prio = get_long_after_colon(arg);
497 if (prio == INVALID_VAL)
498 return -1;
499
500 if (prio < MIN_NICE)
501 return -1;
502 if (prio > MAX_NICE)
503 return -1;
504
505 sched_param->sched_policy = SCHED_OTHER;
506 sched_param->sched_nice = prio;
507 break;
508 default:
509 return -1;
510 }
511 return 0;
512 }
513
514 /*
515 * set_cpu_dma_latency - set the /dev/cpu_dma_latecy
516 *
517 * This is used to reduce the exit from idle latency. The value
518 * will be reset once the file descriptor of /dev/cpu_dma_latecy
519 * is closed.
520 *
521 * Return: the /dev/cpu_dma_latecy file descriptor
522 */
523 int set_cpu_dma_latency(int32_t latency)
524 {
525 int retval;
526 int fd;
527
528 fd = open("/dev/cpu_dma_latency", O_RDWR);
529 if (fd < 0) {
530 err_msg("Error opening /dev/cpu_dma_latency\n");
531 return -1;
532 }
533
534 retval = write(fd, &latency, 4);
535 if (retval < 1) {
536 err_msg("Error setting /dev/cpu_dma_latency\n");
537 close(fd);
538 return -1;
539 }
540
541 debug_msg("Set /dev/cpu_dma_latency to %d\n", latency);
542
543 return fd;
544 }
545
546 #ifdef HAVE_LIBCPUPOWER_SUPPORT
547 static unsigned int **saved_cpu_idle_disable_state;
548 static size_t saved_cpu_idle_disable_state_alloc_ctr;
549
550 /*
551 * save_cpu_idle_state_disable - save disable for all idle states of a cpu
552 *
553 * Saves the current disable of all idle states of a cpu, to be subsequently
554 * restored via restore_cpu_idle_disable_state.
555 *
556 * Return: idle state count on success, negative on error
557 */
558 int save_cpu_idle_disable_state(unsigned int cpu)
559 {
560 unsigned int nr_states;
561 unsigned int state;
562 int disabled;
563 int nr_cpus;
564
565 nr_states = cpuidle_state_count(cpu);
566
567 if (nr_states == 0)
568 return 0;
569
570 if (saved_cpu_idle_disable_state == NULL) {
571 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
572 saved_cpu_idle_disable_state = calloc(nr_cpus, sizeof(unsigned int *));
573 if (!saved_cpu_idle_disable_state)
574 return -1;
575 }
576
577 saved_cpu_idle_disable_state[cpu] = calloc(nr_states, sizeof(unsigned int));
578 if (!saved_cpu_idle_disable_state[cpu])
579 return -1;
580 saved_cpu_idle_disable_state_alloc_ctr++;
581
582 for (state = 0; state < nr_states; state++) {
583 disabled = cpuidle_is_state_disabled(cpu, state);
584 if (disabled < 0)
585 return disabled;
586 saved_cpu_idle_disable_state[cpu][state] = disabled;
587 }
588
589 return nr_states;
590 }
591
592 /*
593 * restore_cpu_idle_disable_state - restore disable for all idle states of a cpu
594 *
595 * Restores the current disable state of all idle states of a cpu that was
596 * previously saved by save_cpu_idle_disable_state.
597 *
598 * Return: idle state count on success, negative on error
599 */
600 int restore_cpu_idle_disable_state(unsigned int cpu)
601 {
602 unsigned int nr_states;
603 unsigned int state;
604 int disabled;
605 int result;
606
607 nr_states = cpuidle_state_count(cpu);
608
609 if (nr_states == 0)
610 return 0;
611
612 if (!saved_cpu_idle_disable_state)
613 return -1;
614
615 for (state = 0; state < nr_states; state++) {
616 if (!saved_cpu_idle_disable_state[cpu])
617 return -1;
618 disabled = saved_cpu_idle_disable_state[cpu][state];
619 result = cpuidle_state_disable(cpu, state, disabled);
620 if (result < 0)
621 return result;
622 }
623
624 free(saved_cpu_idle_disable_state[cpu]);
625 saved_cpu_idle_disable_state[cpu] = NULL;
626 saved_cpu_idle_disable_state_alloc_ctr--;
627 if (saved_cpu_idle_disable_state_alloc_ctr == 0) {
628 free(saved_cpu_idle_disable_state);
629 saved_cpu_idle_disable_state = NULL;
630 }
631
632 return nr_states;
633 }
634
635 /*
636 * free_cpu_idle_disable_states - free saved idle state disable for all cpus
637 *
638 * Frees the memory used for storing cpu idle state disable for all cpus
639 * and states.
640 *
641 * Normally, the memory is freed automatically in
642 * restore_cpu_idle_disable_state; this is mostly for cleaning up after an
643 * error.
644 */
645 void free_cpu_idle_disable_states(void)
646 {
647 int cpu;
648 int nr_cpus;
649
650 if (!saved_cpu_idle_disable_state)
651 return;
652
653 nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
654
655 for (cpu = 0; cpu < nr_cpus; cpu++) {
656 free(saved_cpu_idle_disable_state[cpu]);
657 saved_cpu_idle_disable_state[cpu] = NULL;
658 }
659
660 free(saved_cpu_idle_disable_state);
661 saved_cpu_idle_disable_state = NULL;
662 }
663
664 /*
665 * set_deepest_cpu_idle_state - limit idle state of cpu
666 *
667 * Disables all idle states deeper than the one given in
668 * deepest_state (assuming states with higher number are deeper).
669 *
670 * This is used to reduce the exit from idle latency. Unlike
671 * set_cpu_dma_latency, it can disable idle states per cpu.
672 *
673 * Return: idle state count on success, negative on error
674 */
675 int set_deepest_cpu_idle_state(unsigned int cpu, unsigned int deepest_state)
676 {
677 unsigned int nr_states;
678 unsigned int state;
679 int result;
680
681 nr_states = cpuidle_state_count(cpu);
682
683 for (state = deepest_state + 1; state < nr_states; state++) {
684 result = cpuidle_state_disable(cpu, state, 1);
685 if (result < 0)
686 return result;
687 }
688
689 return nr_states;
690 }
691 #endif /* HAVE_LIBCPUPOWER_SUPPORT */
692
693 #define _STR(x) #x
694 #define STR(x) _STR(x)
695
696 /*
697 * find_mount - find a the mount point of a given fs
698 *
699 * Returns 0 if mount is not found, otherwise return 1 and fill mp
700 * with the mount point.
701 */
702 static const int find_mount(const char *fs, char *mp, int sizeof_mp)
703 {
704 char mount_point[MAX_PATH+1];
705 char type[100];
706 int found = 0;
707 FILE *fp;
708
709 fp = fopen("/proc/mounts", "r");
710 if (!fp)
711 return 0;
712
713 while (fscanf(fp, "%*s %" STR(MAX_PATH) "s %99s %*s %*d %*d\n", mount_point, type) == 2) {
714 if (strcmp(type, fs) == 0) {
715 found = 1;
716 break;
717 }
718 }
719 fclose(fp);
720
721 if (!found)
722 return 0;
723
724 memset(mp, 0, sizeof_mp);
725 strncpy(mp, mount_point, sizeof_mp - 1);
726
727 debug_msg("Fs %s found at %s\n", fs, mp);
728 return 1;
729 }
730
731 /*
732 * get_self_cgroup - get the current thread cgroup path
733 *
734 * Parse /proc/$$/cgroup file to get the thread's cgroup. As an example of line to parse:
735 *
736 * 0::/user.slice/user-0.slice/session-3.scope'\n'
737 *
738 * This function is interested in the content after the second : and before the '\n'.
739 *
740 * Returns 1 if a string was found, 0 otherwise.
741 */
742 static int get_self_cgroup(char *self_cg, int sizeof_self_cg)
743 {
744 char path[MAX_PATH], *start;
745 int fd, retval;
746
747 snprintf(path, MAX_PATH, "/proc/%d/cgroup", getpid());
748
749 fd = open(path, O_RDONLY);
750 if (fd < 0)
751 return 0;
752
753 memset(path, 0, sizeof(path));
754 retval = read(fd, path, MAX_PATH);
755
756 close(fd);
757
758 if (retval <= 0)
759 return 0;
760
761 path[MAX_PATH-1] = '\0';
762 start = path;
763
764 start = strstr(start, ":");
765 if (!start)
766 return 0;
767
768 /* skip ":" */
769 start++;
770
771 start = strstr(start, ":");
772 if (!start)
773 return 0;
774
775 /* skip ":" */
776 start++;
777
778 if (strlen(start) >= sizeof_self_cg)
779 return 0;
780
781 snprintf(self_cg, sizeof_self_cg, "%s", start);
782
783 /* Swap '\n' with '\0' */
784 start = strstr(self_cg, "\n");
785
786 /* there must be '\n' */
787 if (!start)
788 return 0;
789
790 /* ok, it found a string after the second : and before the \n */
791 *start = '\0';
792
793 return 1;
794 }
795
796 /*
797 * open_cgroup_procs - Open the cgroup.procs file for the given cgroup
798 *
799 * If cgroup argument is not NULL, the cgroup.procs file for that cgroup
800 * will be opened. Otherwise, the cgroup of the calling, i.e., rtla, thread
801 * will be used.
802 *
803 * Supports cgroup v2.
804 *
805 * Returns the file descriptor on success, -1 otherwise.
806 */
807 static int open_cgroup_procs(const char *cgroup)
808 {
809 char cgroup_path[MAX_PATH - strlen("/cgroup.procs")];
810 char cgroup_procs[MAX_PATH];
811 int retval;
812 int cg_fd;
813
814 retval = find_mount("cgroup2", cgroup_path, sizeof(cgroup_path));
815 if (!retval) {
816 err_msg("Did not find cgroupv2 mount point\n");
817 return -1;
818 }
819
820 if (!cgroup) {
821 retval = get_self_cgroup(&cgroup_path[strlen(cgroup_path)],
822 sizeof(cgroup_path) - strlen(cgroup_path));
823 if (!retval) {
824 err_msg("Did not find self cgroup\n");
825 return -1;
826 }
827 } else {
828 snprintf(&cgroup_path[strlen(cgroup_path)],
829 sizeof(cgroup_path) - strlen(cgroup_path), "%s/", cgroup);
830 }
831
832 snprintf(cgroup_procs, MAX_PATH, "%s/cgroup.procs", cgroup_path);
833
834 debug_msg("Using cgroup path at: %s\n", cgroup_procs);
835
836 cg_fd = open(cgroup_procs, O_RDWR);
837 if (cg_fd < 0)
838 return -1;
839
840 return cg_fd;
841 }
842
843 /*
844 * set_pid_cgroup - Set cgroup to pid_t pid
845 *
846 * If cgroup argument is not NULL, the threads will move to the given cgroup.
847 * Otherwise, the cgroup of the calling, i.e., rtla, thread will be used.
848 *
849 * Supports cgroup v2.
850 *
851 * Returns 1 on success, 0 otherwise.
852 */
853 int set_pid_cgroup(pid_t pid, const char *cgroup)
854 {
855 char pid_str[24];
856 int retval;
857 int cg_fd;
858
859 cg_fd = open_cgroup_procs(cgroup);
860 if (cg_fd < 0)
861 return 0;
862
863 snprintf(pid_str, sizeof(pid_str), "%d\n", pid);
864
865 retval = write(cg_fd, pid_str, strlen(pid_str));
866 if (retval < 0)
867 err_msg("Error setting cgroup attributes for pid:%s - %s\n",
868 pid_str, strerror(errno));
869 else
870 debug_msg("Set cgroup attributes for pid:%s\n", pid_str);
871
872 close(cg_fd);
873
874 return (retval >= 0);
875 }
876
877 /**
878 * set_comm_cgroup - Set cgroup to threads starting with char *comm_prefix
879 *
880 * If cgroup argument is not NULL, the threads will move to the given cgroup.
881 * Otherwise, the cgroup of the calling, i.e., rtla, thread will be used.
882 *
883 * Supports cgroup v2.
884 *
885 * Returns 1 on success, 0 otherwise.
886 */
887 int set_comm_cgroup(const char *comm_prefix, const char *cgroup)
888 {
889 struct dirent *proc_entry;
890 DIR *procfs;
891 int retval;
892 int cg_fd;
893
894 if (strlen(comm_prefix) >= MAX_PATH) {
895 err_msg("Command prefix is too long: %d < strlen(%s)\n",
896 MAX_PATH, comm_prefix);
897 return 0;
898 }
899
900 cg_fd = open_cgroup_procs(cgroup);
901 if (cg_fd < 0)
902 return 0;
903
904 procfs = opendir("/proc");
905 if (!procfs) {
906 err_msg("Could not open procfs\n");
907 goto out_cg;
908 }
909
910 while ((proc_entry = readdir(procfs))) {
911
912 retval = procfs_is_workload_pid(comm_prefix, proc_entry);
913 if (!retval)
914 continue;
915
916 retval = write(cg_fd, proc_entry->d_name, strlen(proc_entry->d_name));
917 if (retval < 0) {
918 err_msg("Error setting cgroup attributes for pid:%s - %s\n",
919 proc_entry->d_name, strerror(errno));
920 goto out_procfs;
921 }
922
923 debug_msg("Set cgroup attributes for pid:%s\n", proc_entry->d_name);
924 }
925
926 closedir(procfs);
927 close(cg_fd);
928 return 1;
929
930 out_procfs:
931 closedir(procfs);
932 out_cg:
933 close(cg_fd);
934 return 0;
935 }
936
937 /**
938 * auto_house_keeping - Automatically move rtla out of measurement threads
939 *
940 * Try to move rtla away from the tracer, if possible.
941 *
942 * Returns 1 on success, 0 otherwise.
943 */
944 int auto_house_keeping(cpu_set_t *monitored_cpus)
945 {
946 cpu_set_t rtla_cpus, house_keeping_cpus;
947 int retval;
948
949 /* first get the CPUs in which rtla can actually run. */
950 retval = sched_getaffinity(getpid(), sizeof(rtla_cpus), &rtla_cpus);
951 if (retval == -1) {
952 debug_msg("Could not get rtla affinity, rtla might run with the threads!\n");
953 return 0;
954 }
955
956 /* then check if the existing setup is already good. */
957 CPU_AND(&house_keeping_cpus, &rtla_cpus, monitored_cpus);
958 if (!CPU_COUNT(&house_keeping_cpus)) {
959 debug_msg("rtla and the monitored CPUs do not share CPUs.");
960 debug_msg("Skipping auto house-keeping\n");
961 return 1;
962 }
963
964 /* remove the intersection */
965 CPU_XOR(&house_keeping_cpus, &rtla_cpus, monitored_cpus);
966
967 /* get only those that rtla can run */
968 CPU_AND(&house_keeping_cpus, &house_keeping_cpus, &rtla_cpus);
969
970 /* is there any cpu left? */
971 if (!CPU_COUNT(&house_keeping_cpus)) {
972 debug_msg("Could not find any CPU for auto house-keeping\n");
973 return 0;
974 }
975
976 retval = sched_setaffinity(getpid(), sizeof(house_keeping_cpus), &house_keeping_cpus);
977 if (retval == -1) {
978 debug_msg("Could not set affinity for auto house-keeping\n");
979 return 0;
980 }
981
982 debug_msg("rtla automatically moved to an auto house-keeping cpu set\n");
983
984 return 1;
985 }
986
987 /**
988 * parse_optional_arg - Parse optional argument value
989 *
990 * Parse optional argument value, which can be in the form of:
991 * -sarg, -s/--long=arg, -s/--long arg
992 *
993 * Returns arg value if found, NULL otherwise.
994 */
995 char *parse_optional_arg(int argc, char **argv)
996 {
997 if (optarg) {
998 if (optarg[0] == '=') {
999 /* skip the = */
1000 return &optarg[1];
1001 } else {
1002 return optarg;
1003 }
1004 /* parse argument of form -s [arg] and --long [arg]*/
1005 } else if (optind < argc && argv[optind][0] != '-') {
1006 /* consume optind */
1007 return argv[optind++];
1008 } else {
1009 return NULL;
1010 }
1011 }
1012
1013 /*
1014 * strtoi - convert string to integer with error checking
1015 *
1016 * Returns 0 on success, -1 if conversion fails or result is out of int range.
1017 */
1018 int strtoi(const char *s, int *res)
1019 {
1020 char *end_ptr;
1021 long lres;
1022
1023 if (!*s)
1024 return -1;
1025
1026 errno = 0;
1027 lres = strtol(s, &end_ptr, 0);
1028 if (errno || *end_ptr || lres > INT_MAX || lres < INT_MIN)
1029 return -1;
1030
1031 *res = (int) lres;
1032 return 0;
1033 }