]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
python: Add python bindings for several functions
authorTom Hromatka <tom.hromatka@oracle.com>
Tue, 20 Dec 2022 22:38:43 +0000 (22:38 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 6 Jan 2023 15:28:47 +0000 (08:28 -0700)
Add python bindings for several C functions:
cgroup_get_cgroup()
cgroup_delete_cgroup()
cgroup_get_controller_count()
cgroup_get_controller_by_index()
cgroup_get_controller_name()

Also add two python methods to wrap cgroup_get_cgroup() and cgroup_delete() to
make these interfaces more pythonic.  The other three C functions are used
internally to implement these pythonic methods.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
src/python/cgroup.pxd
src/python/libcgroup.pyx

index 0723e01ab64ce0440c15802eccdb30eef47bd7fb..7c71607b4fdc22247b782f30d0f59a74f7382e78 100644 (file)
@@ -79,4 +79,14 @@ cdef extern from "libcgroup.h":
     int cgroup_create_scope(const char * const scope_name, const char * const slice_name,
                             const cgroup_systemd_scope_opts * const opts)
 
+    int cgroup_get_cgroup(cgroup *cg)
+
+    int cgroup_delete_cgroup(cgroup *cg, int ignore_migration)
+
+    int cgroup_get_controller_count(cgroup *cgroup)
+
+    cgroup_controller *cgroup_get_controller_by_index(cgroup *cgroup, int index)
+
+    char *cgroup_get_controller_name(cgroup_controller *controller)
+
 # vim: set et ts=4 sw=4:
index 539a6df66b3ee567d8f963a9df8382c57a786d93..ea3203d26859232bf3d410ce21d8beeadcfa8629 100644 (file)
@@ -372,6 +372,39 @@ cdef class Cgroup:
         if ret is not 0:
             raise RuntimeError("cgroup_create_scope failed: {}".format(ret))
 
+    def get(self):
+        """Get the cgroup information from the cgroup sysfs
+
+        Description:
+        Read the cgroup data from the cgroup sysfs filesystem
+        """
+        cdef cgroup.cgroup_controller *ctrl_ptr
+
+        ret = cgroup.cgroup_get_cgroup(self._cgp)
+        if ret is not 0:
+            raise RuntimeError("cgroup_get_cgroup failed: {}".format(ret))
+
+        ctrl_cnt = cgroup.cgroup_get_controller_count(self._cgp)
+        for i in range(0, ctrl_cnt):
+            ctrl_ptr = cgroup.cgroup_get_controller_by_index(self._cgp, i)
+            ctrl_name = cgroup.cgroup_get_controller_name(ctrl_ptr).decode('ascii')
+            self.controllers[ctrl_name] = Controller(ctrl_name)
+
+        self._pythonize_cgroup()
+
+    def delete(self, ignore_migration=True):
+        """Delete the cgroup from the cgroup sysfs
+
+        Arguments:
+        ignore_migration - ignore errors caused by migration of tasks to parent cgroup
+
+        Description:
+        Delete the cgroup from the cgroup sysfs filesystem
+        """
+        ret = cgroup.cgroup_delete_cgroup(self._cgp, ignore_migration)
+        if ret is not 0:
+            raise RuntimeError("cgroup_delete_cgroup failed: {}".format(ret))
+
     def __dealloc__(self):
         cgroup.cgroup_free(&self._cgp);