Skip to content

Commit

Permalink
fix the cgroup version checking logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
guoshzhao committed Mar 30, 2023
1 parent 97c9a41 commit 571a3a3
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions superbench/monitor/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import glob
import sched
import multiprocessing
from pathlib import Path

from superbench.common.utils import logger, run_command
from superbench.common.utils import device_manager as dm
Expand Down Expand Up @@ -38,16 +39,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 +69,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 +93,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 571a3a3

Please sign in to comment.