Skip to content

Commit 2a3a253

Browse files
makelinuxlenticularis39
authored andcommitted
tools/rtla: Deduplicate cgroup path opening code
Both set_pid_cgroup() and set_comm_cgroup() functions contain identical code for opening the cgroup.procs file. Extract this common code into a new helper function open_cgroup_procs() to reduce code duplication and improve maintainability. Signed-off-by: Costa Shulyupin <costa.shul@redhat.com> Link: https://lore.kernel.org/r/20251224125058.1771519-1-costa.shul@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 0576be4 commit 2a3a253

1 file changed

Lines changed: 32 additions & 33 deletions

File tree

tools/tracing/rtla/src/utils.c

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -784,35 +784,35 @@ static int get_self_cgroup(char *self_cg, int sizeof_self_cg)
784784
}
785785

786786
/*
787-
* set_comm_cgroup - Set cgroup to pid_t pid
787+
* open_cgroup_procs - Open the cgroup.procs file for the given cgroup
788788
*
789-
* If cgroup argument is not NULL, the threads will move to the given cgroup.
790-
* Otherwise, the cgroup of the calling, i.e., rtla, thread will be used.
789+
* If cgroup argument is not NULL, the cgroup.procs file for that cgroup
790+
* will be opened. Otherwise, the cgroup of the calling, i.e., rtla, thread
791+
* will be used.
791792
*
792793
* Supports cgroup v2.
793794
*
794-
* Returns 1 on success, 0 otherwise.
795+
* Returns the file descriptor on success, -1 otherwise.
795796
*/
796-
int set_pid_cgroup(pid_t pid, const char *cgroup)
797+
static int open_cgroup_procs(const char *cgroup)
797798
{
798799
char cgroup_path[MAX_PATH - strlen("/cgroup.procs")];
799800
char cgroup_procs[MAX_PATH];
800-
char pid_str[24];
801801
int retval;
802802
int cg_fd;
803803

804804
retval = find_mount("cgroup2", cgroup_path, sizeof(cgroup_path));
805805
if (!retval) {
806806
err_msg("Did not find cgroupv2 mount point\n");
807-
return 0;
807+
return -1;
808808
}
809809

810810
if (!cgroup) {
811811
retval = get_self_cgroup(&cgroup_path[strlen(cgroup_path)],
812812
sizeof(cgroup_path) - strlen(cgroup_path));
813813
if (!retval) {
814814
err_msg("Did not find self cgroup\n");
815-
return 0;
815+
return -1;
816816
}
817817
} else {
818818
snprintf(&cgroup_path[strlen(cgroup_path)],
@@ -824,6 +824,29 @@ int set_pid_cgroup(pid_t pid, const char *cgroup)
824824
debug_msg("Using cgroup path at: %s\n", cgroup_procs);
825825

826826
cg_fd = open(cgroup_procs, O_RDWR);
827+
if (cg_fd < 0)
828+
return -1;
829+
830+
return cg_fd;
831+
}
832+
833+
/*
834+
* set_pid_cgroup - Set cgroup to pid_t pid
835+
*
836+
* If cgroup argument is not NULL, the threads will move to the given cgroup.
837+
* Otherwise, the cgroup of the calling, i.e., rtla, thread will be used.
838+
*
839+
* Supports cgroup v2.
840+
*
841+
* Returns 1 on success, 0 otherwise.
842+
*/
843+
int set_pid_cgroup(pid_t pid, const char *cgroup)
844+
{
845+
char pid_str[24];
846+
int retval;
847+
int cg_fd;
848+
849+
cg_fd = open_cgroup_procs(cgroup);
827850
if (cg_fd < 0)
828851
return 0;
829852

@@ -853,8 +876,6 @@ int set_pid_cgroup(pid_t pid, const char *cgroup)
853876
*/
854877
int set_comm_cgroup(const char *comm_prefix, const char *cgroup)
855878
{
856-
char cgroup_path[MAX_PATH - strlen("/cgroup.procs")];
857-
char cgroup_procs[MAX_PATH];
858879
struct dirent *proc_entry;
859880
DIR *procfs;
860881
int retval;
@@ -866,29 +887,7 @@ int set_comm_cgroup(const char *comm_prefix, const char *cgroup)
866887
return 0;
867888
}
868889

869-
retval = find_mount("cgroup2", cgroup_path, sizeof(cgroup_path));
870-
if (!retval) {
871-
err_msg("Did not find cgroupv2 mount point\n");
872-
return 0;
873-
}
874-
875-
if (!cgroup) {
876-
retval = get_self_cgroup(&cgroup_path[strlen(cgroup_path)],
877-
sizeof(cgroup_path) - strlen(cgroup_path));
878-
if (!retval) {
879-
err_msg("Did not find self cgroup\n");
880-
return 0;
881-
}
882-
} else {
883-
snprintf(&cgroup_path[strlen(cgroup_path)],
884-
sizeof(cgroup_path) - strlen(cgroup_path), "%s/", cgroup);
885-
}
886-
887-
snprintf(cgroup_procs, MAX_PATH, "%s/cgroup.procs", cgroup_path);
888-
889-
debug_msg("Using cgroup path at: %s\n", cgroup_procs);
890-
891-
cg_fd = open(cgroup_procs, O_RDWR);
890+
cg_fd = open_cgroup_procs(cgroup);
892891
if (cg_fd < 0)
893892
return 0;
894893

0 commit comments

Comments
 (0)