]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add function to get the cgroup mode
authorTom Hromatka <tom.hromatka@oracle.com>
Tue, 18 Oct 2022 19:27:06 +0000 (19:27 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Tue, 25 Oct 2022 21:17:14 +0000 (15:17 -0600)
Add a function to the Cgroup class to get the cgroup mode.  This
function is analogous to the C function, cgroup_setup_mode() and
can be used to verify the behavior of cgroup_setup_mode().

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
Reviewed-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
tests/ftests/cgroup.py

index 9c4fd5cd39f2ccb1e751535cffcf21dd9c4cca85..01b9faa9c2a59ab69e7fb9f75a87c3e5c956163f 100644 (file)
@@ -2,7 +2,7 @@
 #
 # Cgroup class for the libcgroup functional tests
 #
-# Copyright (c) 2019-2021 Oracle and/or its affiliates.
+# Copyright (c) 2019-2022 Oracle and/or its affiliates.
 # Author: Tom Hromatka <tom.hromatka@oracle.com>
 #
 
@@ -10,6 +10,7 @@ from container import ContainerError
 from controller import Controller
 from run import Run, RunError
 import multiprocessing as mp
+from libcgroup import Mode
 from enum import Enum
 import consts
 import utils
@@ -951,6 +952,48 @@ class Cgroup(object):
         Cgroup.set(config, cgname, setting, value)
         Cgroup.get_and_validate(config, cgname, setting, value)
 
+    @staticmethod
+    def get_cgroup_mode(config):
+        mount_list = Cgroup.get_cgroup_mounts(config, True)
+
+        legacy = False
+        unified = False
+
+        for mount in mount_list:
+            # As best I can tell, python doesn't have an easy way to access the statfs() f_type
+            # field.  Let's make our best guess at the cgroup version (legacy, hybrid, or
+            # unified) by the presence of certain files/folders
+
+            cpu_ctrl = os.path.join(os.path.dirname(mount.mount_point), 'cpu,cpuacct')
+            if os.path.exists(cpu_ctrl):
+                legacy = True
+
+            limit_file = os.path.join(mount.mount_point, 'memory.limit_in_bytes')
+            if os.path.exists(limit_file):
+                legacy = True
+
+            subtree_file = os.path.join(mount.mount_point, 'cgroup.subtree_control')
+            if os.path.exists(subtree_file):
+                unified = True
+
+            subtree_file = os.path.join(os.path.dirname(mount.mount_point),
+                                        'cgroup.subtree_control')
+            if os.path.exists(subtree_file):
+                unified = True
+
+            subtree_file = os.path.join(os.path.dirname(mount.mount_point), 'unified',
+                                        'cgroup.subtree_control')
+            if os.path.exists(subtree_file):
+                unified = True
+
+        if legacy and unified:
+            return Mode.CGROUP_MODE_HYBRID
+        elif legacy:
+            return Mode.CGROUP_MODE_LEGACY
+        elif unified:
+            return Mode.CGROUP_MODE_UNIFIED
+        else:
+            raise CgroupError('Unknown cgroup mode')
 
 class CgroupError(Exception):
     def __init__(self, message):