Skip to content

Commit

Permalink
Cgroups api refactor (#6)
Browse files Browse the repository at this point in the history
* Initial changes for log collector cgroups v2 support

* Fix pylint issues

* Fix pylint issues

* Fix pylint issues

* Check that both controllers are mounted in the chosen cgroups version for log collector

* Fix regex

* Update test_agent unit tests

* Fix unit tests

* Update format strings

* Fix broken cgroupconfigurator unit tests

* pyling

* Fix cgroups api unit tests

* Ignore unused args

* Ignore unused args

* Add cgroup configurator tests

* v2 required check in parent cgroup

* unit tests is_controller_enabled

* Fix test failure and pylint:

* pylint

* Update agent checks

* Fix controller enable logic and unit tests

* Remove changes to collect logs

* Fix pylint

* Add e2e test for v2
  • Loading branch information
maddieford authored Mar 15, 2024
1 parent ee6eb7d commit 9501707
Show file tree
Hide file tree
Showing 23 changed files with 1,278 additions and 393 deletions.
28 changes: 20 additions & 8 deletions azurelinuxagent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import threading
from azurelinuxagent.ga import logcollector, cgroupconfigurator
from azurelinuxagent.ga.cgroup import AGENT_LOG_COLLECTOR, CpuCgroup, MemoryCgroup
from azurelinuxagent.ga.cgroupapi import SystemdCgroupsApi
from azurelinuxagent.ga.cgroupapi import get_cgroup_api
from azurelinuxagent.ga.cgroupstelemetry import log_cgroup_warning

import azurelinuxagent.common.conf as conf
import azurelinuxagent.common.event as event
Expand Down Expand Up @@ -206,18 +207,29 @@ def collect_logs(self, is_full_mode):

# Check the cgroups unit
log_collector_monitor = None
cgroups_api = SystemdCgroupsApi()
cpu_cgroup_path, memory_cgroup_path = cgroups_api.get_process_cgroup_paths("self")
cgroups_api = get_cgroup_api()
cpu_cgroup_path = None
memory_cgroup_path = None
if CollectLogsHandler.is_enabled_monitor_cgroups_check():
cpu_slice_matches = (cgroupconfigurator.LOGCOLLECTOR_SLICE in cpu_cgroup_path)
memory_slice_matches = (cgroupconfigurator.LOGCOLLECTOR_SLICE in memory_cgroup_path)
if cgroups_api is None:
log_cgroup_warning("Unable to determine what version of cgroups to use for log collector resource "
"monitoring and enforcement.")
sys.exit(logcollector.INVALID_CGROUPS_ERRCODE)

cpu_cgroup_path, memory_cgroup_path = cgroups_api.get_process_cgroup_paths("self")
cpu_slice_matches = False
memory_slice_matches = False
if cpu_cgroup_path is not None:
cpu_slice_matches = (cgroupconfigurator.LOGCOLLECTOR_SLICE in cpu_cgroup_path)
if memory_cgroup_path is not None:
memory_slice_matches = (cgroupconfigurator.LOGCOLLECTOR_SLICE in memory_cgroup_path)

if not cpu_slice_matches or not memory_slice_matches:
logger.info("The Log Collector process is not in the proper cgroups:")
log_cgroup_warning("The Log Collector process is not in the proper cgroups:", send_event=False)
if not cpu_slice_matches:
logger.info("\tunexpected cpu slice")
log_cgroup_warning("\tunexpected cpu slice", send_event=False)
if not memory_slice_matches:
logger.info("\tunexpected memory slice")
log_cgroup_warning("\tunexpected memory slice", send_event=False)

sys.exit(logcollector.INVALID_CGROUPS_ERRCODE)

Expand Down
403 changes: 293 additions & 110 deletions azurelinuxagent/ga/cgroupapi.py

Large diffs are not rendered by default.

173 changes: 81 additions & 92 deletions azurelinuxagent/ga/cgroupconfigurator.py

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions azurelinuxagent/ga/cgroupstelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@
import threading

from azurelinuxagent.common import logger
from azurelinuxagent.common.event import WALAEventOperation, add_event
from azurelinuxagent.ga.cgroup import CpuCgroup
from azurelinuxagent.common.future import ustr


def log_cgroup_info(formatted_string, op=WALAEventOperation.CGroupsInfo, send_event=True):
logger.info("[CGI] " + formatted_string)
if send_event:
add_event(op=op, message=formatted_string)


def log_cgroup_warning(formatted_string, op=WALAEventOperation.CGroupsInfo, send_event=True):
logger.info("[CGW] " + formatted_string) # log as INFO for now, in the future it should be logged as WARNING
if send_event:
add_event(op=op, message=formatted_string, is_success=False, log_event=False)


class CGroupsTelemetry(object):
"""
"""
Expand Down
7 changes: 0 additions & 7 deletions tests/data/cgroups/sys_fs_cgroup_unified_cgroup.controllers

This file was deleted.

File renamed without changes.
File renamed without changes.
Empty file.
12 changes: 12 additions & 0 deletions tests/data/cgroups/v1_and_v2/proc_pid_cgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
12:devices:/system.slice/Microsoft.A.Sample.Extension_1.0.1_aeac05dc-8c24-4542-95f2-a0d6be1c5ba7.scope
11:perf_event:/
10:rdma:/
9:blkio:/system.slice/Microsoft.A.Sample.Extension_1.0.1_aeac05dc-8c24-4542-95f2-a0d6be1c5ba7.scope
8:net_cls,net_prio:/
7:freezer:/
6:hugetlb:/
4:cpuset:/
3:cpu,cpuacct:/system.slice/Microsoft.A.Sample.Extension_1.0.1_aeac05dc-8c24-4542-95f2-a0d6be1c5ba7.scope
2:pids:/system.slice/Microsoft.A.Sample.Extension_1.0.1_aeac05dc-8c24-4542-95f2-a0d6be1c5ba7.scope
1:name=systemd:/system.slice/Microsoft.A.Sample.Extension_1.0.1_aeac05dc-8c24-4542-95f2-a0d6be1c5ba7.scope
0::/system.slice/Microsoft.A.Sample.Extension_1.0.1_aeac05dc-8c24-4542-95f2-a0d6be1c5ba7.scope
12 changes: 12 additions & 0 deletions tests/data/cgroups/v1_and_v2/proc_self_cgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
12:blkio:/system.slice/walinuxagent.service
11:cpu,cpuacct:/system.slice/walinuxagent.service
10:devices:/system.slice/walinuxagent.service
9:pids:/system.slice/walinuxagent.service
7:freezer:/
6:hugetlb:/
5:perf_event:/
4:net_cls,net_prio:/
3:cpuset:/
2:rdma:/
1:name=systemd:/system.slice/walinuxagent.service
0::/system.slice/walinuxagent.service
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
memory
1 change: 1 addition & 0 deletions tests/data/cgroups/v2/proc_pid_cgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0::/system.slice/Microsoft.A.Sample.Extension_1.0.1_aeac05dc-8c24-4542-95f2-a0d6be1c5ba7.scope
1 change: 1 addition & 0 deletions tests/data/cgroups/v2/proc_self_cgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0::/system.slice/walinuxagent.service
1 change: 1 addition & 0 deletions tests/data/cgroups/v2/sys_fs_cgroup_cgroup.subtree_control
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpuset cpu io memory pids
Empty file.
Loading

0 comments on commit 9501707

Please sign in to comment.