code structure optimization

This commit is contained in:
Alexey Avramov 2018-12-25 03:36:16 +09:00
parent dc7637b35f
commit 39119f4611
2 changed files with 130 additions and 57 deletions

160
nohang
View File

@ -15,14 +15,11 @@ from signal import SIGKILL, SIGTERM
sig_dict = {SIGKILL: 'SIGKILL',
SIGTERM: 'SIGTERM'}
'''
nm = 30
nc = nm + 1
'''
self_uid = os.geteuid()
self_pid = str(os.getpid())
# not implemented
wait_time = 2
cache_time = 30
cache_path = '/dev/shm/nohang_env_cache'
@ -32,6 +29,65 @@ cache_path = '/dev/shm/nohang_env_cache'
# function definition section
def check_mem():
"""find mem_available"""
return int(rline1('/proc/meminfo').split(':')[1].strip(' kB\n'))
def check_mem_and_swap():
"""find mem_available, swap_total, swap_free"""
with open('/proc/meminfo') as f:
for n, line in enumerate(f):
if n is 2:
mem_available = int(line.split(':')[1].strip(' kB\n'))
continue
if n is swap_total_index:
swap_total = int(line.split(':')[1].strip(' kB\n'))
continue
if n is swap_free_index:
swap_free = int(line.split(':')[1].strip(' kB\n'))
break
return mem_available, swap_total, swap_free
def check_zram():
"""find MemUsedZram"""
disksize_sum = 0
mem_used_total_sum = 0
for dev in os.listdir('/sys/block'):
if dev.startswith('zram'):
stat = zram_stat(dev)
disksize_sum += int(stat[0])
mem_used_total_sum += int(stat[1])
ZRAM_DISKSIZE_FACTOR = 0.0042
# Означает, что при задани zram disksize = 1 GiB доступная память
# уменьшится на 0.0042 GiB.
# Найден экспериментально, требует уточнения с разными ядрами и архитектурами.
# На небольших дисксайзах (до гигабайта) может быть больше, до 0.0045.
# Создатель модуля zram утверждает, что ZRAM_DISKSIZE_FACTOR доожен быть 0.001:
# ("zram uses about 0.1% of the size of the disk"
# - https://www.kernel.org/doc/Documentation/blockdev/zram.txt),
# но это утверждение противоречит опытным данным.
# ZRAM_DISKSIZE_FACTOR = deltaMemAvailavle / disksize
# found experimentally
return (mem_used_total_sum + disksize_sum * ZRAM_DISKSIZE_FACTOR) / 1024.0
def format_time(t):
t = int(t)
if t < 60:
@ -1454,23 +1510,19 @@ stdout.flush()
#exit()
##########################################################################
while True:
# find mem_available, swap_total, swap_free
with open('/proc/meminfo') as f:
for n, line in enumerate(f):
if n is 2:
mem_available = int(line.split(':')[1].strip(' kB\n'))
continue
if n is swap_total_index:
swap_total = int(line.split(':')[1].strip(' kB\n'))
continue
if n is swap_free_index:
swap_free = int(line.split(':')[1].strip(' kB\n'))
break
t1 = time()
mem_available, swap_total, swap_free = check_mem_and_swap()
t2 = time()
# if swap_min_sigkill is set in percent
if swap_kill_is_percent:
@ -1482,29 +1534,11 @@ while True:
if swap_warn_is_percent:
swap_min_warnings_kb = swap_total * swap_min_warnings_percent / 100.0
# find MemUsedZram
disksize_sum = 0
mem_used_total_sum = 0
for dev in os.listdir('/sys/block'):
if dev.startswith('zram'):
stat = zram_stat(dev)
disksize_sum += int(stat[0])
mem_used_total_sum += int(stat[1])
t3 = time()
# Означает, что при задани zram disksize = 10000M доступная память
# уменьшится на 42 MiB.
# Найден экспериментально, требует уточнения с разными ядрами и архитектурами.
# На небольших дисксайзах (до гигабайта) может быть больше, до 0.0045.
# Создатель модуля zram утверждает, что ZRAM_DISKSIZE_FACTOR доожен быть 0.001
# ("zram uses about 0.1% of the size of the disk"
# - https://www.kernel.org/doc/Documentation/blockdev/zram.txt),
# но это утверждение противоречит опытным данным.
# ZRAM_DISKSIZE_FACTOR = deltaMemAvailavle / disksize
# found experimentally
ZRAM_DISKSIZE_FACTOR = 0.0042
mem_used_zram = (
mem_used_total_sum + disksize_sum * ZRAM_DISKSIZE_FACTOR
) / 1024.0
mem_used_zram = check_zram()
t4 = time()
if print_mem_check_results:
@ -1534,14 +1568,7 @@ while True:
human(mem_used_zram, mem_len),
just_percent_mem(mem_used_zram / mem_total)))
'''
if nc > nm:
nc = 0
print('MemAvailable, MiB:', human(mem_available, mem_len))
else:
nc += 1
'''
t5 = time()
# если swap_min_sigkill задан в абсолютной величине и Swap_total = 0
if swap_total > swap_min_sigkill_kb: # If swap_min_sigkill is absolute
@ -1553,13 +1580,11 @@ while True:
swap_sigterm_pc = percent(swap_min_sigterm_kb / (swap_total + 0.1))
else:
# СТОИТ ПЕЧАТАТЬ СВОП ТОЛЬКО ПРИ SwapTotal > 0
# нет, печатать так: SwapTotal = 0, ignore swapspace
# печатать так: SwapTotal = 0, ignore swapspace
swap_sigterm_pc = '-'
# Limits overdrafting checks
# If overdrafted - try to prevent OOM
# else - just sleep
t6 = time()
# MEM SWAP KILL
if mem_available <= mem_min_sigkill_kb and \
@ -1644,8 +1669,41 @@ while True:
if warn_timer > min_time_between_warnings:
send_notify_warn()
warn_timer = 0
sleep_after_check_mem()
# SLEEP BETWEEN MEM CHECKS
else:
sleep_after_check_mem()
#break
t7 = time()
d = (t7 - t1) * 1000000
print(d, 'total')
d = (t2 - t1) * 1000000
print(d, 'mem+swap')
d = (t4 - t3) * 1000000
print(d, 'zram')
t22 = time()
e = check_mem()
t33 = time()
d = (t33 - t22) * 1000000
print(d, 'mem')
d = (t5 - t4) * 1000000
print(d, 'print meminfo')

View File

@ -31,6 +31,14 @@
Just read the description of the parameters and edit the values.
Please restart the program after editing the config.
#####################################################################
Not imlemented.
$IGNORE_SWAPSPACE = FALSE
$IGNORE_ZRAM = TRUE
#####################################################################
1. Thresholds below which a signal should be sent to the victim
@ -106,8 +114,8 @@ min_badness = 10
Valid values are non-negative floating-point numbers.
min_delay_after_sigterm = 0.1
min_delay_after_sigkill = 0.4
min_delay_after_sigterm = 0.2
min_delay_after_sigkill = 0.5
Процессы браузера chromium обычно имеют oom_score_adj
200 или 300. Это приводит к тому, что процессы хрома умирают
@ -278,7 +286,8 @@ zram_max_warnings = 40 %
It is disabled by default because the value mlockall = True in
Fedora 28 causes the process to increase memory consumption by
200 MiB. On Debian 8 and 9 there is no such problem.
200 MiB. On Debian 8, Debian 9, Ubuntu 16.04 there is no such
problem. Other distros is not tested.
mlockall = False
@ -291,6 +300,10 @@ mlockall = False
niceness = -9
Возможно этот параметр можно убрать, потому что теперь запрет
самоубийства включен по умолчанию: nohang исключает себя
из поиска жертв.
Set oom_score_adj for the nohang process.
Valid values are integers from the range [-1000; 1000].
Setting the values to -1000 will prohibit suicide.
@ -300,6 +313,8 @@ oom_score_adj = -10
Read `man ionice` to understand the following parameters.
Setting the True value requires the root privileges.
Не замечено большой пользы от этой опции.
realtime_ionice = False
'For realtime and best-effort, 0-7 are valid data