]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
python: Add python bindings for cgroup_get_procs()
authorTom Hromatka <tom.hromatka@oracle.com>
Fri, 17 Mar 2023 15:23:59 +0000 (09:23 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 13 Apr 2023 13:51:41 +0000 (07:51 -0600)
Add python bindings for cgroup_get_procs().

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
(cherry picked from commit 65f3fce51873b54c6c75b392facedd3a9e8ea14f)

src/python/cgroup.pxd
src/python/libcgroup.pyx

index 26446cfd1659302a6a712ece45bb4993575df55c..4e48b8aca6c18160224bd42afcd38781bf0bc1e9 100644 (file)
@@ -105,4 +105,6 @@ cdef extern from "libcgroup.h":
     void cgroup_set_default_systemd_cgroup()
 
     int cgroup_compare_cgroup(cgroup *cgroup_a, cgroup *cgroup_b)
+
+    int cgroup_get_procs(char *name, char *controller, pid_t **pids, int *size)
 # vim: set et ts=4 sw=4:
index d56cdd8aa83823bd43876116643e5742483b92af..7e8a2d8d27fd5cc016150b843835adde33769ac6 100644 (file)
@@ -594,6 +594,39 @@ cdef class Cgroup:
         else:
             return False
 
+    def get_procs(self):
+        """Get the processes in this cgroup
+
+        Return:
+        Returns an integer list of PIDs
+
+        Description:
+        Invokes the libcgroup C function, cgroup_get_procs().
+        cgroup_get_procs() reads each controller's cgroup.procs file
+        in the cgroup sysfs.  The results are then combined together
+        into a standard Python list of integers.
+
+        Note:
+        Reads from the cgroup sysfs
+        """
+        pid_list = list()
+        cdef pid_t *pids
+        cdef int size
+
+        for ctrl_key in self.controllers:
+            ret = cgroup.cgroup_get_procs(c_str(self.name),
+                        c_str(self.controllers[ctrl_key].name), &pids, &size)
+            if ret is not 0:
+                raise RuntimeError("cgroup_get_procs failed: {}".format(ret))
+
+            for i in range(0, size):
+                pid_list.append(int(pids[i]))
+
+        # Remove duplicates
+        pid_list = [*set(pid_list)]
+
+        return pid_list
+
     def __dealloc__(self):
         cgroup.cgroup_free(&self._cgp);