From 7aaa2fdc724b081aa508f42107602dff0a3c8457 Mon Sep 17 00:00:00 2001 From: Alexey Avramov Date: Wed, 2 Jan 2019 16:34:28 +0900 Subject: [PATCH] fix psi support --- nohang | 37 +++++++++++++++++++++++++++---------- nohang.conf | 21 ++++++++++++++++----- b02 => psi-monitor | 23 +++++++++++++++-------- 3 files changed, 58 insertions(+), 23 deletions(-) rename b02 => psi-monitor (70%) diff --git a/nohang b/nohang index a4ddb4d..afb8d23 100755 --- a/nohang +++ b/nohang @@ -27,8 +27,10 @@ wait_time = 2 cache_time = 30 cache_path = '/dev/shm/nohang_env_cache' -psi_support = os.path.exists('/memory') +psi_path = '/proc/pressure/memory' + +psi_support = os.path.exists(psi_path) @@ -41,7 +43,7 @@ psi_support = os.path.exists('/memory') def psi_mem_some_avg_total(): if psi_support: - return float(rline1('/memory').rpartition('=')[2]) + return float(rline1(psi_path).rpartition('=')[2]) @@ -1541,23 +1543,38 @@ avg_min_time = 3 ########################################################################## - -ta0 = time() -a0 = psi_mem_some_avg_total() +if psi_support: + ta0 = time() + a0 = psi_mem_some_avg_total() while True: - if psi_support: ta1= time() - a1 = psi_mem_some_avg_total() - avg = (a1 - a0) / (ta1 - ta0) / 100 - a0 = a1 + dt = ta1 - ta0 - print('PSI mem avg:', round(avg, 2)) + if dt >= avg_min_time: + a1 = psi_mem_some_avg_total() + avg = (a1 - a0) / (ta1 - ta0) / 100 + + print('PSI mem avg:', round(avg, 2)) + ta0 = ta1 + a0 = a1 + + + if avg >= sigkill_psi: + time0 = time() + mem_info = 'avg ({}) > sigkill_psi ({})'.format(avg, sigkill_psi) + find_victim_and_send_signal(SIGKILL) + elif avg >= sigterm_psi: + time0 = time() + mem_info = 'avg ({}) > sigterm_psi ({})'.format(avg, sigterm_psi) + find_victim_and_send_signal(SIGTERM) + else: + print('PSI is OK') diff --git a/nohang.conf b/nohang.conf index 20fa3bc..f912c32 100644 --- a/nohang.conf +++ b/nohang.conf @@ -39,6 +39,17 @@ $IGNORE_SWAPSPACE = FALSE $IGNORE_ZRAM = TRUE +$IGNORE_PSI = TRUE +$SIGKILL_PSI_AVG = 80 +$SIGTERM_PSI_AVG = 60 +$PSI_AVG_TIME = 3 + + + + + + + ##################################################################### 1. Thresholds below which a signal should be sent to the victim @@ -90,9 +101,9 @@ zram_max_sigkill = 55 % Valid values are positive floating-point numbers. -rate_mem = 6 -rate_swap = 3 -rate_zram = 1 +rate_mem = 60 +rate_swap = 30 +rate_zram = 10 See also https://github.com/rfjakob/earlyoom/issues/61 @@ -289,7 +300,7 @@ zram_max_warnings = 40 % 200 MiB. On Debian 8, Debian 9, Ubuntu 16.04 there is no such problem. Other distros is not tested. -mlockall = False +mlockall = True Установка отрицательных значений niceness и oom_score_adj требует наличия root прав. @@ -336,7 +347,7 @@ print_config = False Print memory check results. Valid values are True and False. -print_mem_check_results = False +print_mem_check_results = True Print sleep periods between memory checks. Valid values are True and False. diff --git a/b02 b/psi-monitor similarity index 70% rename from b02 rename to psi-monitor index 54135bf..50fa0ea 100755 --- a/b02 +++ b/psi-monitor @@ -1,33 +1,34 @@ #!/usr/bin/env python3 from time import sleep, time - import os +from sys import stdout -from sys import stdout, stderr +mlockall = True + +if mlockall: + from ctypes import CDLL + CDLL('libc.so.6').mlockall(3) psi_path = '/proc/pressure/memory' psi_support = os.path.exists(psi_path) - def rline1(path): """read 1st line from path.""" with open(path) as f: for line in f: return line[:-1] - def psi_mem_some_avg_total(): return float(rline1(psi_path).rpartition('=')[2]) -avg_min_time = 0.1 +avg_min_time = 1 if psi_support: ta0 = time() a0 = psi_mem_some_avg_total() - while True: if psi_support: @@ -38,11 +39,17 @@ while True: if dt >= avg_min_time: a1 = psi_mem_some_avg_total() - avg = (a1 - a0) / (ta1 - ta0) / 100 + avg = (a1 - a0) / (ta1 - ta0) / 10000 + print('avg time:', round(dt, 1)) print('PSI mem avg:', round(avg, 2)) + print(rline1(psi_path), '\n') ta0 = ta1 a0 = a1 stdout.flush() - sleep(1) + sleep(0.1) + + + +