In preparation for adding non-static functionality to the Cgroup class, move the cgroup version enumerations to their own class. (Enums and __init__() can collide in strange ways.)
Signed-off-by: Tom Hromatka <tom.hroma...@oracle.com> --- ftests/001-cgget-basic_cgget_v1.py | 5 ++- ftests/002-cgdelete-recursive_delete.py | 6 ++-- ftests/003-cgget-basic_cgget_v2.py | 4 +-- ftests/cgroup.py | 42 +++++++++++++------------ 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/ftests/001-cgget-basic_cgget_v1.py b/ftests/001-cgget-basic_cgget_v1.py index 87da4bc67725..1c1016b1a4a8 100755 --- a/ftests/001-cgget-basic_cgget_v1.py +++ b/ftests/001-cgget-basic_cgget_v1.py @@ -20,7 +20,7 @@ # along with this library; if not, see <http://www.gnu.org/licenses>. # -from cgroup import Cgroup +from cgroup import Cgroup, CgroupVersion import consts import ftests import os @@ -36,8 +36,7 @@ def prereqs(config): result = consts.TEST_PASSED cause = None - # This test was written for a cgroup v1 cpu controller only - if Cgroup.version('cpu') != Cgroup.CGROUP_V1: + if CgroupVersion.get_version('cpu') != CgroupVersion.CGROUP_V1: result = consts.TEST_SKIPPED cause = "This test requires the cgroup v1 cpu controller" diff --git a/ftests/002-cgdelete-recursive_delete.py b/ftests/002-cgdelete-recursive_delete.py index 755b87a6f626..220a763a87a0 100755 --- a/ftests/002-cgdelete-recursive_delete.py +++ b/ftests/002-cgdelete-recursive_delete.py @@ -20,7 +20,7 @@ # along with this library; if not, see <http://www.gnu.org/licenses>. # -from cgroup import Cgroup +from cgroup import Cgroup, CgroupVersion import consts import ftests import os @@ -41,8 +41,8 @@ def setup(config): Cgroup.create(config, CONTROLLER, os.path.join(PARENT, CHILD)) Cgroup.create(config, CONTROLLER, os.path.join(PARENT, CHILD, GRANDCHILD)) - version = Cgroup.version(CONTROLLER) - if version == Cgroup.CGROUP_V1: + version = CgroupVersion.get_version(CONTROLLER) + if version == CgroupVersion.CGROUP_V1: # cgdelete in a cgroup v1 controller should be able to move a process # from a child cgroup to its parent. # diff --git a/ftests/003-cgget-basic_cgget_v2.py b/ftests/003-cgget-basic_cgget_v2.py index 9eb558dd9936..54727fe59c40 100755 --- a/ftests/003-cgget-basic_cgget_v2.py +++ b/ftests/003-cgget-basic_cgget_v2.py @@ -20,7 +20,7 @@ # along with this library; if not, see <http://www.gnu.org/licenses>. # -from cgroup import Cgroup +from cgroup import Cgroup, CgroupVersion import consts import ftests import os @@ -36,7 +36,7 @@ def prereqs(config): result = consts.TEST_PASSED cause = None - if Cgroup.version('cpuset') != Cgroup.CGROUP_V2: + if CgroupVersion.get_version('cpuset') != CgroupVersion.CGROUP_V2: result = consts.TEST_SKIPPED cause = "This test requires the cgroup v2 cpuset controller" diff --git a/ftests/cgroup.py b/ftests/cgroup.py index ec5c6821c9d1..146ced711a46 100644 --- a/ftests/cgroup.py +++ b/ftests/cgroup.py @@ -24,11 +24,32 @@ from enum import Enum import os from run import Run -class Cgroup(Enum): +class CgroupVersion(Enum): CGROUP_UNK = 0 CGROUP_V1 = 1 CGROUP_V2 = 2 + # given a controller name, get the cgroup version of the controller + @staticmethod + def get_version(controller): + with open('/proc/mounts', 'r') as mntf: + for line in mntf.readlines(): + mnt_path = line.split()[1] + + if line.split()[0] == 'cgroup': + for option in line.split()[3].split(','): + if option == controller: + return CgroupVersion.CGROUP_V1 + elif line.split()[0] == 'cgroup2': + with open(os.path.join(mnt_path, 'cgroup.controllers'), 'r') as ctrlf: + controllers = ctrlf.readline() + for ctrl in controllers.split(): + if ctrl == controller: + return CgroupVersion.CGROUP_V2 + + return CgroupVersion.CGROUP_UNK + +class Cgroup(object): @staticmethod def build_cmd_path(in_container, cmd): if in_container: @@ -176,25 +197,6 @@ class Cgroup(Enum): return ret - @staticmethod - def version(controller): - with open('/proc/mounts', 'r') as mntf: - for line in mntf.readlines(): - mnt_path = line.split()[1] - - if line.split()[0] == 'cgroup': - for option in line.split()[3].split(','): - if option == controller: - return Cgroup.CGROUP_V1 - elif line.split()[0] == 'cgroup2': - with open(os.path.join(mnt_path, 'cgroup.controllers'), 'r') as ctrlf: - controllers = ctrlf.readline() - for ctrl in controllers.split(): - if ctrl == controller: - return Cgroup.CGROUP_V2 - - return Cgroup.CGROUP_UNK - @staticmethod def classify(config, controller, cgname, pid_list, sticky=False, cancel_sticky=False, in_container=True): -- 2.25.4 _______________________________________________ Libcg-devel mailing list Libcg-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/libcg-devel