Improve PSI files validation (fix #74)

This commit is contained in:
Alexey Avramov 2020-01-02 00:14:15 +09:00
parent 93e16e084a
commit d563565a2a
3 changed files with 51 additions and 6 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""A highly configurable OOM prevention daemon.""" """A sophisticated low memory handler."""
import os import os
from ctypes import CDLL from ctypes import CDLL
@ -665,6 +665,22 @@ def print_version():
exit() exit()
def psi_file_mem_to_metrics(psi_path):
"""
"""
with open(psi_path) as f:
psi_list = f.readlines()
some_list, full_list = psi_list[0].split(' '), psi_list[1].split(' ')
some_avg10 = some_list[1].split('=')[1]
some_avg60 = some_list[2].split('=')[1]
some_avg300 = some_list[3].split('=')[1]
full_avg10 = full_list[1].split('=')[1]
full_avg60 = full_list[2].split('=')[1]
full_avg300 = full_list[3].split('=')[1]
return (some_avg10, some_avg60, some_avg300,
full_avg10, full_avg60, full_avg300)
def pid_to_cgroup_v1(pid): def pid_to_cgroup_v1(pid):
""" """
""" """
@ -2881,8 +2897,21 @@ debug_threading = conf_parse_bool('debug_threading')
psi_checking_enabled = conf_parse_bool('psi_checking_enabled') psi_checking_enabled = conf_parse_bool('psi_checking_enabled')
ignore_psi = not psi_checking_enabled ignore_psi = not psi_checking_enabled
if psi_checking_enabled:
try:
psi_file_mem_to_metrics('/proc/pressure/memory')
except Exception as e:
print('WARNING: PSI metrics are not provided by the kernel: {}'.format(
e))
ignore_psi = True
zram_checking_enabled = conf_parse_bool('zram_checking_enabled') zram_checking_enabled = conf_parse_bool('zram_checking_enabled')
ignore_zram = not zram_checking_enabled ignore_zram = not zram_checking_enabled
@ -3171,6 +3200,15 @@ else:
if 'psi_path' in config_dict: if 'psi_path' in config_dict:
psi_path = config_dict['psi_path'] psi_path = config_dict['psi_path']
if not ignore_psi:
try:
psi_file_mem_to_metrics(psi_path)
except Exception as e:
print('WARNING: invalid psi_path "{}": {}'.format(
psi_path, e))
ignore_psi = True
else: else:
errprint('psi_path is not in config\nExit') errprint('psi_path is not in config\nExit')
exit(1) exit(1)

View File

@ -73,8 +73,12 @@ else:
psi_support = os.path.exists(psi_path) psi_support = os.path.exists(psi_path)
if not psi_support:
print('PSI is not supported, /proc/pressure/memory does not exist. Exit.') try:
psi_path_to_metrics(psi_path)
except Exception as e:
print('ERROR: {}'.format(e))
print('PSI metrics are not provided by the kernel. Exit.')
exit(1) exit(1)

View File

@ -193,9 +193,12 @@ if log_file is not None:
log('log file: {}'.format(log_file)) log('log file: {}'.format(log_file))
if not os.path.exists('/proc/pressure'): try:
log('ERROR: /proc/pressure does not exist, PSI is not supported, exit') psi_file_mem_to_metrics('/proc/pressure/memory')
exit() except Exception as e:
print('ERROR: {}'.format(e))
print('PSI metrics are not provided by the kernel. Exit.')
exit(1)
if target == 'SYSTEM_WIDE': if target == 'SYSTEM_WIDE':