From b5966cd4eb16fce9a9322dd7af8216c29e617886 Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Tue, 20 Dec 2022 22:38:43 +0000 Subject: [PATCH] python: Add python bindings for several functions 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 Reviewed-by: Kamalesh Babulal --- src/python/cgroup.pxd | 10 ++++++++++ src/python/libcgroup.pyx | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd index 0723e01a..7c71607b 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd @@ -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: diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index 539a6df6..ea3203d2 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -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); -- 2.47.2