perf: 内存使用率兼顾 docker cgroup limit

pull/7301/head
xinwen 2021-11-26 16:46:45 +08:00 committed by 老广
parent 3076b7a5e1
commit a313e011ef
1 changed files with 26 additions and 0 deletions

View File

@ -252,7 +252,33 @@ def get_cpu_load():
return float(single_cpu_load_1) return float(single_cpu_load_1)
def get_docker_mem_usage_if_limit():
try:
with open('/sys/fs/cgroup/memory/memory.limit_in_bytes') as f:
limit_in_bytes = int(f.readline())
total = psutil.virtual_memory().total
if limit_in_bytes >= total:
raise ValueError('Not limit')
with open('/sys/fs/cgroup/memory/memory.usage_in_bytes') as f:
usage_in_bytes = int(f.readline())
with open('/sys/fs/cgroup/memory/memory.stat') as f:
lines = f.readlines()
name, value = lines[33].split()
total_inactive_file = int(value)
return ((usage_in_bytes - total_inactive_file) / limit_in_bytes) * 100
except Exception as e:
logger.error(f'Get memory usage by docker limit: {e}')
return None
def get_memory_usage(): def get_memory_usage():
usage = get_docker_mem_usage_if_limit()
if usage is not None:
return usage
return psutil.virtual_memory().percent return psutil.virtual_memory().percent