mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
60 lines
2.5 KiB
Python
60 lines
2.5 KiB
Python
"""Current host, process and hierarchical cgroup memory admission."""
|
|
from pathlib import Path
|
|
|
|
|
|
def _cgroup_available_bytes(root=Path('/sys/fs/cgroup'), membership=Path('/proc/self/cgroup')):
|
|
if not root.exists():
|
|
return None
|
|
records = membership.read_text().splitlines() if membership.exists() else []
|
|
hierarchy = root if ((root / 'cgroup.controllers').exists() or (root / 'memory.max').exists()) else root / 'memory'
|
|
v2 = hierarchy == root
|
|
relative = ''
|
|
for line in records:
|
|
_, controllers, path = line.split(':', 2)
|
|
if (v2 and not controllers) or (not v2 and 'memory' in controllers.split(',')):
|
|
relative = path.lstrip('/')
|
|
break
|
|
# A container cgroup namespace may expose the current group as its mount
|
|
# root, while membership still contains a host-relative path.
|
|
candidate = hierarchy / relative
|
|
current = candidate if '..' not in Path(relative).parts and candidate.exists() else hierarchy
|
|
limits = []
|
|
while current.is_relative_to(hierarchy):
|
|
limit_file = current / ('memory.max' if v2 else 'memory.limit_in_bytes')
|
|
usage_file = current / ('memory.current' if v2 else 'memory.usage_in_bytes')
|
|
if limit_file.exists():
|
|
raw = limit_file.read_text().strip()
|
|
if raw != 'max':
|
|
limit = int(raw)
|
|
usage = int(usage_file.read_text().strip())
|
|
if limit < 0 or usage < 0:
|
|
raise RuntimeError('invalid cgroup memory accounting')
|
|
limits.append(max(limit - usage, 0))
|
|
if current == hierarchy:
|
|
break
|
|
current = current.parent
|
|
return min(limits) if limits else None
|
|
|
|
|
|
def available_host_bytes():
|
|
"""Fail on unreadable accounting instead of inventing available capacity."""
|
|
import psutil
|
|
|
|
memory = psutil.virtual_memory()
|
|
available = int(memory.available)
|
|
if not 0 <= available <= memory.total:
|
|
raise RuntimeError('invalid host memory accounting')
|
|
candidates = [available]
|
|
cgroup = _cgroup_available_bytes()
|
|
if cgroup is not None:
|
|
candidates.append(cgroup)
|
|
try:
|
|
import resource
|
|
except ImportError:
|
|
resource = None
|
|
if resource is not None and hasattr(resource, 'RLIMIT_AS'):
|
|
soft, _ = resource.getrlimit(resource.RLIMIT_AS)
|
|
if soft not in (-1, resource.RLIM_INFINITY):
|
|
candidates.append(max(int(soft) - int(psutil.Process().memory_info().vms), 0))
|
|
return min(candidates)
|