Unverified Commit 26373edb authored by guoshzhao's avatar guoshzhao Committed by GitHub
Browse files

Monitor - Fix the cgroup version checking logic. (#502)

**Description**
Looks `grep cgroup /proc/filesystems` doesn't work for NDv4 whose cgroup
version is v1, but the result of this command got v2 for NDv4. Instead,
checking the file existence to judge the cgroup version.
parent 97c9a41f
......@@ -38,16 +38,7 @@ def __init__(self, container_name, sample_duration, sample_interval, output_file
self.__unit_MiByte = 1024 * 1024 * 1.0
self.__output_handler = open(self.__output_file, 'a')
self.__cgroup = 1
output = run_command('grep cgroup /proc/filesystems', quiet=True)
if output.returncode != 0:
logger.error('Failed to check the cgroup version, will assume using cgroup V1.')
else:
if 'cgroup2' in output.stdout:
self.__cgroup = 2
logger.info('cgroup version: {}.'.format(self.__cgroup))
def __preprocess(self):
"""Preprocess/preparation operations before the monitoring.
......@@ -77,13 +68,15 @@ def __preprocess(self):
container_pid = output.stdout
try:
if self.__cgroup == 1:
self._cpu_file = glob.glob('/sys/fs/cgroup/cpuacct/docker/{}*/cpuacct.stat'.format(container_id))[0]
cpu_file_cgroup_v1 = glob.glob('/sys/fs/cgroup/cpuacct/docker/{}*/cpuacct.stat'.format(container_id))
if len(cpu_file_cgroup_v1) > 0:
self._cpu_file = cpu_file_cgroup_v1[0]
self._mem_file = glob.glob(
'/sys/fs/cgroup/memory/docker/{}*/memory.usage_in_bytes'.format(container_id)
)[0]
self._net_file = '/proc/{}/net/dev'.format(container_pid)
else:
self.__cgroup = 2
self._cpu_file = glob.glob(
'/sys/fs/cgroup/system.slice/docker-{}*.scope/cpu.stat'.format(container_id)
)[0]
......@@ -99,10 +92,12 @@ def __preprocess(self):
)
return False
else:
if self.__cgroup == 1:
self._cpu_file = '/sys/fs/cgroup/cpuacct/cpuacct.stat'
cpu_file_cgroup_v1 = '/sys/fs/cgroup/cpuacct/cpuacct.stat'
if os.path.exists(cpu_file_cgroup_v1):
self._cpu_file = cpu_file_cgroup_v1
self._mem_file = '/sys/fs/cgroup/memory/memory.usage_in_bytes'
else:
self.__cgroup = 2
self._cpu_file = '/sys/fs/cgroup/cpu.stat'
self._mem_file = '/sys/fs/cgroup/memory.stat'
self._net_file = '/proc/net/dev'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment