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', sig_dict = {SIGKILL: 'SIGKILL',
SIGTERM: 'SIGTERM'} SIGTERM: 'SIGTERM'}
'''
nm = 30
nc = nm + 1
'''
self_uid = os.geteuid() self_uid = os.geteuid()
self_pid = str(os.getpid()) self_pid = str(os.getpid())
# not implemented
wait_time = 2 wait_time = 2
cache_time = 30 cache_time = 30
cache_path = '/dev/shm/nohang_env_cache' cache_path = '/dev/shm/nohang_env_cache'
@ -32,6 +29,65 @@ cache_path = '/dev/shm/nohang_env_cache'
# function definition section # 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): def format_time(t):
t = int(t) t = int(t)
if t < 60: if t < 60:
@ -1454,23 +1510,19 @@ stdout.flush()
#exit() #exit()
########################################################################## ##########################################################################
while True: while True:
# find mem_available, swap_total, swap_free t1 = time()
with open('/proc/meminfo') as f:
for n, line in enumerate(f): mem_available, swap_total, swap_free = check_mem_and_swap()
if n is 2:
mem_available = int(line.split(':')[1].strip(' kB\n')) t2 = time()
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
# if swap_min_sigkill is set in percent # if swap_min_sigkill is set in percent
if swap_kill_is_percent: if swap_kill_is_percent:
@ -1482,29 +1534,11 @@ while True:
if swap_warn_is_percent: if swap_warn_is_percent:
swap_min_warnings_kb = swap_total * swap_min_warnings_percent / 100.0 swap_min_warnings_kb = swap_total * swap_min_warnings_percent / 100.0
# find MemUsedZram t3 = time()
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 = 10000M доступная память mem_used_zram = check_zram()
# уменьшится на 42 MiB.
# Найден экспериментально, требует уточнения с разными ядрами и архитектурами. t4 = time()
# На небольших дисксайзах (до гигабайта) может быть больше, до 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
if print_mem_check_results: if print_mem_check_results:
@ -1534,14 +1568,7 @@ while True:
human(mem_used_zram, mem_len), human(mem_used_zram, mem_len),
just_percent_mem(mem_used_zram / mem_total))) just_percent_mem(mem_used_zram / mem_total)))
''' t5 = time()
if nc > nm:
nc = 0
print('MemAvailable, MiB:', human(mem_available, mem_len))
else:
nc += 1
'''
# если swap_min_sigkill задан в абсолютной величине и Swap_total = 0 # если swap_min_sigkill задан в абсолютной величине и Swap_total = 0
if swap_total > swap_min_sigkill_kb: # If swap_min_sigkill is absolute 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)) swap_sigterm_pc = percent(swap_min_sigterm_kb / (swap_total + 0.1))
else: else:
# СТОИТ ПЕЧАТАТЬ СВОП ТОЛЬКО ПРИ SwapTotal > 0 # печатать так: SwapTotal = 0, ignore swapspace
# нет, печатать так: SwapTotal = 0, ignore swapspace
swap_sigterm_pc = '-' swap_sigterm_pc = '-'
# Limits overdrafting checks t6 = time()
# If overdrafted - try to prevent OOM
# else - just sleep
# MEM SWAP KILL # MEM SWAP KILL
if mem_available <= mem_min_sigkill_kb and \ if mem_available <= mem_min_sigkill_kb and \
@ -1644,8 +1669,41 @@ while True:
if warn_timer > min_time_between_warnings: if warn_timer > min_time_between_warnings:
send_notify_warn() send_notify_warn()
warn_timer = 0 warn_timer = 0
sleep_after_check_mem() sleep_after_check_mem()
# SLEEP BETWEEN MEM CHECKS # SLEEP BETWEEN MEM CHECKS
else: else:
sleep_after_check_mem() 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. Just read the description of the parameters and edit the values.
Please restart the program after editing the config. 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 1. Thresholds below which a signal should be sent to the victim
@ -44,12 +52,12 @@
MemAvailable levels. MemAvailable levels.
mem_min_sigterm = 10 % mem_min_sigterm = 10 %
mem_min_sigkill = 5 % mem_min_sigkill = 5 %
SwapFree levels. SwapFree levels.
swap_min_sigterm = 10 % swap_min_sigterm = 10 %
swap_min_sigkill = 5 % swap_min_sigkill = 5 %
Specifying the total share of zram in memory, if exceeded the Specifying the total share of zram in memory, if exceeded the
corresponding signals are sent. As the share of zram in memory corresponding signals are sent. As the share of zram in memory
@ -82,7 +90,7 @@ zram_max_sigkill = 55 %
Valid values are positive floating-point numbers. Valid values are positive floating-point numbers.
rate_mem = 6 rate_mem = 6
rate_swap = 3 rate_swap = 3
rate_zram = 1 rate_zram = 1
@ -106,8 +114,8 @@ min_badness = 10
Valid values are non-negative floating-point numbers. Valid values are non-negative floating-point numbers.
min_delay_after_sigterm = 0.1 min_delay_after_sigterm = 0.2
min_delay_after_sigkill = 0.4 min_delay_after_sigkill = 0.5
Процессы браузера chromium обычно имеют oom_score_adj Процессы браузера chromium обычно имеют oom_score_adj
200 или 300. Это приводит к тому, что процессы хрома умирают 200 или 300. Это приводит к тому, что процессы хрома умирают
@ -278,7 +286,8 @@ zram_max_warnings = 40 %
It is disabled by default because the value mlockall = True in It is disabled by default because the value mlockall = True in
Fedora 28 causes the process to increase memory consumption by 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 mlockall = False
@ -291,6 +300,10 @@ mlockall = False
niceness = -9 niceness = -9
Возможно этот параметр можно убрать, потому что теперь запрет
самоубийства включен по умолчанию: nohang исключает себя
из поиска жертв.
Set oom_score_adj for the nohang process. Set oom_score_adj for the nohang process.
Valid values are integers from the range [-1000; 1000]. Valid values are integers from the range [-1000; 1000].
Setting the values to -1000 will prohibit suicide. Setting the values to -1000 will prohibit suicide.
@ -300,6 +313,8 @@ oom_score_adj = -10
Read `man ionice` to understand the following parameters. Read `man ionice` to understand the following parameters.
Setting the True value requires the root privileges. Setting the True value requires the root privileges.
Не замечено большой пользы от этой опции.
realtime_ionice = False realtime_ionice = False
'For realtime and best-effort, 0-7 are valid data 'For realtime and best-effort, 0-7 are valid data