Skip to content

Commit

Permalink
Monitor - Fix the cgroup version checking logic. (#502)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
guoshzhao authored Apr 3, 2023
1 parent 97c9a41 commit 26373ed
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions superbench/monitor/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand All @@ -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'
Expand Down

0 comments on commit 26373ed

Please sign in to comment.