Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address PR comments #12

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions azurelinuxagent/ga/cgroupapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def get_cgroup_api():
log_cgroup_info("Using cgroup v1 for resource enforcement and monitoring")
return cgroup_api

raise CGroupsException("Detected unknown cgroup mode: {0}".format(root_hierarchy_mode))
raise CGroupsException("{0} has an unexpected file type: {1}".format(CGROUP_FILE_SYSTEM_ROOT, root_hierarchy_mode))


class _SystemdCgroupApi(object):
Expand Down Expand Up @@ -256,7 +256,7 @@ def get_process_cgroup_paths(self, process_id):

return cpu_cgroup_path, memory_cgroup_path

def get_process_cgroup_relative_paths(self, process_id): # pylint: disable=W0613
def get_process_cgroup_relative_paths(self, process_id):
"""
Cgroup version specific. Returns a tuple with the path of the cpu and memory cgroups for the given process
(relative to the root path of the corresponding controller).
Expand Down Expand Up @@ -330,9 +330,9 @@ def are_mountpoints_systemd_created(self):
"""
cpu_mountpoint = self._cgroup_mountpoints.get('cpu,cpuacct')
memory_mountpoint = self._cgroup_mountpoints.get('memory')
if cpu_mountpoint is not None and cpu_mountpoint != '/sys/fs/cgroup/cpu,cpuacct':
if cpu_mountpoint is not None and cpu_mountpoint != os.path.join(CGROUP_FILE_SYSTEM_ROOT, 'cpu,cpuacct'):
return False
if memory_mountpoint is not None and memory_mountpoint != '/sys/fs/cgroup/memory':
if memory_mountpoint is not None and memory_mountpoint != os.path.join(CGROUP_FILE_SYSTEM_ROOT, 'memory'):
return False
return True

Expand Down Expand Up @@ -448,7 +448,7 @@ class SystemdCgroupApiv2(_SystemdCgroupApi):
def __init__(self):
super(SystemdCgroupApiv2, self).__init__()
self._root_cgroup_path = self._get_root_cgroup_path()
self._controllers_enabled_at_root = self._get_controllers_enabled_at_root()
self._controllers_enabled_at_root = self._get_controllers_enabled_at_root(self._root_cgroup_path) if self._root_cgroup_path is not None else []

@staticmethod
def _get_root_cgroup_path():
Expand All @@ -472,7 +472,8 @@ def _get_root_cgroup_path():
return root_cgroup_path
return None

def _get_controllers_enabled_at_root(self):
@staticmethod
def _get_controllers_enabled_at_root(root_cgroup_path):
"""
Returns a list of the controllers enabled at the root cgroup. The cgroup.subtree_control file at the root shows
a space separated list of the controllers which are enabled to control resource distribution from the root
Expand All @@ -483,10 +484,9 @@ def _get_controllers_enabled_at_root(self):
cpuset cpu io memory hugetlb pids rdma misc
"""
controllers_enabled_at_root = []
if self._root_cgroup_path is not None:
enabled_controllers_file = os.path.join(self._root_cgroup_path, 'cgroup.subtree_control')
if os.path.exists(enabled_controllers_file):
controllers_enabled_at_root = fileutil.read_file(enabled_controllers_file).rstrip().split(" ")
enabled_controllers_file = os.path.join(root_cgroup_path, 'cgroup.subtree_control')
if os.path.exists(enabled_controllers_file):
controllers_enabled_at_root = fileutil.read_file(enabled_controllers_file).rstrip().split()
return controllers_enabled_at_root

def get_controller_root_paths(self):
Expand All @@ -511,7 +511,7 @@ def get_process_cgroup_relative_paths(self, process_id):
cpu_path = None
memory_path = None
for line in fileutil.read_file("/proc/{0}/cgroup".format(process_id)).splitlines():
match = re.match(r'\d+::(?P<path>\S+)', line)
match = re.match(r'0::(?P<path>\S+)', line)
if match is not None:
path = match.group('path').lstrip('/') if match.group('path') != '/' else None
memory_path = path
Expand Down
14 changes: 7 additions & 7 deletions azurelinuxagent/ga/cgroupconfigurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ def initialize(self):

log_cgroup_info("systemd version: {0}".format(systemd.get_version()))

if not self.__check_no_legacy_cgroups():
return

# Determine which version of the Cgroup Api should be used. If the correct version can't be determined,
# do not enable resource monitoring/enforcement.
try:
Expand All @@ -174,7 +177,8 @@ def initialize(self):
log_cgroup_warning("Unable to determine which cgroup version to use: {0}".format(ustr(e)), send_event=True)
return

if not self.__check_no_legacy_cgroups():
if self.using_cgroup_v2():
log_cgroup_info("Agent and extensions resource monitoring is not currently supported on cgroup v2")
return

agent_unit_name = systemd.get_agent_unit_name()
Expand All @@ -185,10 +189,6 @@ def initialize(self):

self.__setup_azure_slice()

if self.cgroup_v2_enabled():
log_cgroup_info("Agent and extensions resource monitoring is not currently supported on cgroup v2")
return

cpu_controller_root, memory_controller_root = self.__get_cgroup_controller_roots()
self._agent_cpu_cgroup_path, self._agent_memory_cgroup_path = self.__get_agent_cgroup_paths(agent_slice,
cpu_controller_root,
Expand Down Expand Up @@ -469,7 +469,7 @@ def agent_enabled(self):
def extensions_enabled(self):
return self._extensions_cgroups_enabled

def cgroup_v2_enabled(self):
def using_cgroup_v2(self):
return isinstance(self._cgroups_api, SystemdCgroupApiv2)

def enable(self):
Expand Down Expand Up @@ -624,7 +624,7 @@ def _check_processes_in_agent_cgroup(self):
agent_commands.update(shellutil.get_running_commands())
systemd_run_commands = set()
systemd_run_commands.update(self._cgroups_api.get_systemd_run_commands())
agent_cgroup = self._cgroups_api.get_processes_in_cgroup(self._agent_cpu_cgroup_path)
agent_cgroup = self._cgroups_api.get_processes_in_cgroup(cgroup_path)
# get the running commands again in case new commands started or completed while we were fetching the processes in the cgroup;
agent_commands.update(shellutil.get_running_commands())
systemd_run_commands.update(self._cgroups_api.get_systemd_run_commands())
Expand Down
6 changes: 3 additions & 3 deletions tests/ga/test_cgroupapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_get_cgroup_api_raises_exception_when_cgroup_mode_cannot_be_determined(s
with patch('azurelinuxagent.common.utils.shellutil.run_command', return_value=unknown_cgroup_type):
with self.assertRaises(CGroupsException) as context:
get_cgroup_api()
self.assertTrue("Detected unknown cgroup mode: {0}".format(unknown_cgroup_type) in str(context.exception))
self.assertTrue("/sys/fs/cgroup has an unexpected file type: {0}".format(unknown_cgroup_type) in str(context.exception))

def test_get_systemd_version_should_return_a_version_number(self):
# We expect same behavior for v1 and v2
Expand Down Expand Up @@ -357,13 +357,13 @@ class SystemdCgroupsApiv2TestCase(AgentTestCase):
def test_get_controllers_enabled_at_root_should_return_list_of_enabled_controllers(self):
with mock_cgroup_v2_environment(self.tmp_dir):
cgroup_api = get_cgroup_api()
self.assertEqual(cgroup_api._get_controllers_enabled_at_root(), ['cpuset', 'cpu', 'io', 'memory', 'pids'])
self.assertEqual(cgroup_api._get_controllers_enabled_at_root('/sys/fs/cgroup'), ['cpuset', 'cpu', 'io', 'memory', 'pids'])

def test_get_controllers_enabled_at_root_should_return_empty_list_if_root_cgroup_path_is_None(self):
with mock_cgroup_v2_environment(self.tmp_dir):
with patch('azurelinuxagent.ga.cgroupapi.SystemdCgroupApiv2._get_root_cgroup_path', return_value=None):
cgroup_api = get_cgroup_api()
self.assertEqual(cgroup_api._get_controllers_enabled_at_root(), [])
self.assertEqual(cgroup_api._controllers_enabled_at_root, [])

def test_get_root_cgroup_path_should_return_v2_cgroup_root(self):
with mock_cgroup_v2_environment(self.tmp_dir):
Expand Down
Loading