On Tue, May 10, 2022 at 11:57:04AM +0200, Daniel Bristot de Oliveira wrote: > +static int procfs_is_workload_pid(const char *comm_prefix, struct dirent *proc_entry) > +{ > + char buffer[MAX_PATH]; > + int comm_fd, retval; > + char *t_name; > + > + if (proc_entry->d_type != DT_DIR) > + return 0; > + > + if (*proc_entry->d_name == '.') > + return 0; > + > + /* check if the string is a pid */ > + for (t_name = proc_entry->d_name; t_name; t_name++) { > + if (!isdigit(*t_name)) > + break; > + } > + > + if (*t_name != '\0') > + return 0; > + > + snprintf(buffer, MAX_PATH, "/proc/%s/comm", proc_entry->d_name); > + comm_fd = open(buffer, O_RDONLY); > + if (comm_fd < 0) > + return 0; > + > + memset(buffer, 0, MAX_PATH); > + retval = read(comm_fd, buffer, MAX_PATH); > + > + close(comm_fd); > + > + if (retval <= 0) > + return 0; > + > + retval = strncmp(comm_prefix, buffer, strlen(comm_prefix)); > + if (retval) > + return 0; > + > + /* comm already have \n */ > + debug_msg("Found workload pid:%s comm:%s", proc_entry->d_name, buffer); > + > + return 1; > +} > + > +/* > + * set_comm_sched_attr - set sched params to threads starting with char *comm_prefix > + * > + * This function uses /procfs to list the currently running threads and then set the > + * sched_attr *attr to the threads that start with char *comm_prefix. It is > * mainly used to set the priority to the kernel threads created by the > * tracers. > */ > -int set_comm_sched_attr(const char *comm, struct sched_attr *attr) > +int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr) > { > - int flags = PROC_FILLCOM | PROC_FILLSTAT; > - PROCTAB *ptp; > - proc_t task; > + struct dirent *proc_entry; > + DIR *procfs; > int retval; > > - ptp = openproc(flags); > - if (!ptp) { > - err_msg("error openproc()\n"); > - return -ENOENT; > + if (strlen(comm_prefix) >= MAX_PATH) { > + err_msg("Command prefix is too long: %d < strlen(%s)\n", > + MAX_PATH, comm_prefix); > + exit(EINVAL); > } > > - memset(&task, 0, sizeof(task)); > + procfs = opendir("/proc"); Maybe you want to check if procfs is a valid handle before using it. > + > + while ((proc_entry = readdir(procfs))) { > > - while (readproc(ptp, &task)) { > - retval = strncmp(comm, task.cmd, strlen(comm)); > - if (retval) > + retval = procfs_is_workload_pid(comm_prefix, proc_entry); > + if (!retval) > continue; > - retval = __set_sched_attr(task.tid, attr); > - if (retval) > + > + /* procfs_is_workload_pid confirmed it is a pid */ > + retval = __set_sched_attr(atoi(proc_entry->d_name), attr); > + if (retval) { > + err_msg("Error setting sched attributes for pid:%s\n", proc_entry->d_name); > goto out_err; > - } > + } > > - closeproc(ptp); > + debug_msg("Set sched attributes for pid:%s\n", proc_entry->d_name); > + } > return 0; > > out_err: > - closeproc(ptp); > + closedir(procfs); > return 1; > } > > diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h > index fa08e374870a..5571afd3b549 100644 > --- a/tools/tracing/rtla/src/utils.h > +++ b/tools/tracing/rtla/src/utils.h > @@ -6,6 +6,7 @@ > * '18446744073709551615\0' > */ > #define BUFF_U64_STR_SIZE 24 > +#define MAX_PATH 1024 linux/limits.h defined MAX_PATH as well, though as 4096. Maybe use that definition? If you want to make it dynamic you could use fpathconf(fd, _PC_PATH_MAX) but one could argue that is over engineering :) Thanks for the fix. Anyway just nitpicking. Reviewed-by: Daniel Wagner <dwagner@xxxxxxx>