fix code style, fix bug with using python 3.4
This commit is contained in:
parent
337634a7cc
commit
591f9e07a7
73
nohang
73
nohang
@ -1,27 +1,14 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""A daemon that prevents OOM in Linux systems."""
|
"""A daemon that prevents OOM in Linux systems."""
|
||||||
import os
|
import os
|
||||||
import signal
|
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from time import sleep, time
|
from time import sleep, time
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from sys import stdout
|
from sys import stdout
|
||||||
import re
|
from signal import SIGKILL, SIGTERM
|
||||||
|
|
||||||
|
|
||||||
sig_dict = {signal.SIGKILL: 'SIGKILL',
|
|
||||||
signal.SIGTERM: 'SIGTERM'}
|
|
||||||
|
|
||||||
# directory where the script is running
|
|
||||||
cd = os.getcwd()
|
|
||||||
|
|
||||||
# where to look for a config if not specified via the -c/--config option
|
|
||||||
default_configs = (cd + '/nohang.conf', '/etc/nohang/nohang.conf')
|
|
||||||
|
|
||||||
# universal message if config is invalid
|
|
||||||
conf_err_mess = '\nSet up the path to the valid conf' \
|
|
||||||
'ig file with -c/--config option!\nExit'
|
|
||||||
|
|
||||||
|
sig_dict = {SIGKILL: 'SIGKILL',
|
||||||
|
SIGTERM: 'SIGTERM'}
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
@ -129,7 +116,7 @@ def func_decrease_oom_score_adj(oom_score_adj_max):
|
|||||||
Stupid function, must be remaked
|
Stupid function, must be remaked
|
||||||
"""
|
"""
|
||||||
for i in os.listdir('/proc'):
|
for i in os.listdir('/proc'):
|
||||||
if i.isdigit() is not True:
|
if i.isdigit() is False:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
oom_score_adj = int(rline1('/proc/' + i + '/oom_score_adj'))
|
oom_score_adj = int(rline1('/proc/' + i + '/oom_score_adj'))
|
||||||
@ -339,7 +326,7 @@ def sleep_after_send_signal(signal):
|
|||||||
|
|
||||||
signal: sent signal
|
signal: sent signal
|
||||||
"""
|
"""
|
||||||
if signal is signal.SIGKILL:
|
if signal is SIGKILL:
|
||||||
if print_sleep_periods:
|
if print_sleep_periods:
|
||||||
print(' sleep', min_delay_after_sigkill)
|
print(' sleep', min_delay_after_sigkill)
|
||||||
sleep(min_delay_after_sigterm)
|
sleep(min_delay_after_sigterm)
|
||||||
@ -378,7 +365,7 @@ def find_victim_and_send_signal(signal):
|
|||||||
|
|
||||||
for pid in os.listdir('/proc'):
|
for pid in os.listdir('/proc'):
|
||||||
# только директории, имена которых состоят только из цифр, за исключением /proc/1/
|
# только директории, имена которых состоят только из цифр, за исключением /proc/1/
|
||||||
if pid[0].isdecimal() is not True or pid == '1':
|
if pid[0].isdecimal() is False or pid == '1':
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -422,7 +409,7 @@ def find_victim_and_send_signal(signal):
|
|||||||
else:
|
else:
|
||||||
|
|
||||||
for pid in os.listdir('/proc'):
|
for pid in os.listdir('/proc'):
|
||||||
if pid[0].isdecimal() is not True:
|
if pid[0].isdecimal() is False:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
badness = int(rline1('/proc/' + pid + '/oom_score'))
|
badness = int(rline1('/proc/' + pid + '/oom_score'))
|
||||||
@ -472,7 +459,7 @@ def find_victim_and_send_signal(signal):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if execute_the_command and signal is signal.SIGTERM and name in etc_dict:
|
if execute_the_command and signal is SIGTERM and name in etc_dict:
|
||||||
command = etc_dict[name]
|
command = etc_dict[name]
|
||||||
exit_status = os.system(etc_dict[name])
|
exit_status = os.system(etc_dict[name])
|
||||||
response_time = time() - time0
|
response_time = time() - time0
|
||||||
@ -556,17 +543,17 @@ def fattest():
|
|||||||
if regex_matching:
|
if regex_matching:
|
||||||
|
|
||||||
for pid in os.listdir('/proc'):
|
for pid in os.listdir('/proc'):
|
||||||
if pid[0].isdecimal() is not True:
|
if pid[0].isdecimal() is False:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
badness = int(rline1('/proc/' + pid + '/oom_score'))
|
badness = int(rline1('/proc/' + pid + '/oom_score'))
|
||||||
name = pid_to_name(pid)
|
name = pid_to_name(pid)
|
||||||
|
|
||||||
if fullmatch(avoid_regex, name) is not None:
|
if search(avoid_regex, name) is not None:
|
||||||
badness = int(badness / avoid_factor)
|
badness = int(badness / avoid_factor)
|
||||||
|
|
||||||
if fullmatch(prefer_regex, name) is not None:
|
if search(prefer_regex, name) is not None:
|
||||||
badness = int((badness + 1) * prefer_factor)
|
badness = int((badness + 1) * prefer_factor)
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@ -578,7 +565,7 @@ def fattest():
|
|||||||
else:
|
else:
|
||||||
|
|
||||||
for pid in os.listdir('/proc'):
|
for pid in os.listdir('/proc'):
|
||||||
if pid[0].isdecimal() is not True:
|
if pid[0].isdecimal() is False:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
badness = int(rline1('/proc/' + pid + '/oom_score'))
|
badness = int(rline1('/proc/' + pid + '/oom_score'))
|
||||||
@ -696,6 +683,18 @@ uid_index = status_names.index('Uid')
|
|||||||
|
|
||||||
# Configurations
|
# Configurations
|
||||||
|
|
||||||
|
|
||||||
|
# directory where the script is running
|
||||||
|
cd = os.getcwd()
|
||||||
|
|
||||||
|
# where to look for a config if not specified via the -c/--config option
|
||||||
|
default_configs = (cd + '/nohang.conf', '/etc/nohang/nohang.conf')
|
||||||
|
|
||||||
|
# universal message if config is invalid
|
||||||
|
conf_err_mess = '\nSet up the path to the valid conf' \
|
||||||
|
'ig file with -c/--config option!\nExit'
|
||||||
|
|
||||||
|
|
||||||
# Cmd argparse
|
# Cmd argparse
|
||||||
parser = ArgumentParser()
|
parser = ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@ -1233,12 +1232,11 @@ mem_len = len(str(round(mem_total / 1024.0)))
|
|||||||
|
|
||||||
if gui_notifications or gui_low_memory_warnings:
|
if gui_notifications or gui_low_memory_warnings:
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
notify_sig_dict = {signal.SIGKILL: 'Killing',
|
notify_sig_dict = {SIGKILL: 'Killing',
|
||||||
signal.SIGTERM: 'Terminating'}
|
SIGTERM: 'Terminating'}
|
||||||
|
|
||||||
|
|
||||||
if regex_matching:
|
if regex_matching:
|
||||||
from re import fullmatch
|
from re import search
|
||||||
|
|
||||||
|
|
||||||
rate_mem = rate_mem * 1048576
|
rate_mem = rate_mem * 1048576
|
||||||
@ -1296,16 +1294,15 @@ while True:
|
|||||||
# уменьшится на 42 MiB.
|
# уменьшится на 42 MiB.
|
||||||
# Найден экспериментально, требует уточнения с разными ядрами и архитектурами.
|
# Найден экспериментально, требует уточнения с разными ядрами и архитектурами.
|
||||||
# На небольших дисксайзах (до гигабайта) может быть больше, до 0.0045.
|
# На небольших дисксайзах (до гигабайта) может быть больше, до 0.0045.
|
||||||
# Создатель модуля zram утверждает, что zram_disksize_factor доожен быть 0.001
|
# Создатель модуля zram утверждает, что ZRAM_DISKSIZE_FACTOR доожен быть 0.001
|
||||||
# ("zram uses about 0.1% of the size of the disk"
|
# ("zram uses about 0.1% of the size of the disk"
|
||||||
# - https://www.kernel.org/doc/Documentation/blockdev/zram.txt),
|
# - https://www.kernel.org/doc/Documentation/blockdev/zram.txt),
|
||||||
# но это утверждение противоречит опытным данным.
|
# но это утверждение противоречит опытным данным.
|
||||||
# zram_disksize_factor = deltaMemAvailavle / disksize
|
# ZRAM_DISKSIZE_FACTOR = deltaMemAvailavle / disksize
|
||||||
# found experimentally
|
# found experimentally
|
||||||
zram_disksize_factor = 0.0042
|
ZRAM_DISKSIZE_FACTOR = 0.0042
|
||||||
|
|
||||||
mem_used_zram = (
|
mem_used_zram = (
|
||||||
mem_used_total_sum + disksize_sum * zram_disksize_factor
|
mem_used_total_sum + disksize_sum * ZRAM_DISKSIZE_FACTOR
|
||||||
) / 1024.0
|
) / 1024.0
|
||||||
|
|
||||||
|
|
||||||
@ -1373,7 +1370,7 @@ while True:
|
|||||||
kib_to_mib(swap_min_sigkill_kb),
|
kib_to_mib(swap_min_sigkill_kb),
|
||||||
swap_sigkill_pc)
|
swap_sigkill_pc)
|
||||||
|
|
||||||
find_victim_and_send_signal(signal.SIGKILL)
|
find_victim_and_send_signal(SIGKILL)
|
||||||
|
|
||||||
# ZRAM KILL
|
# ZRAM KILL
|
||||||
elif mem_used_zram >= zram_max_sigkill_kb:
|
elif mem_used_zram >= zram_max_sigkill_kb:
|
||||||
@ -1385,7 +1382,7 @@ while True:
|
|||||||
kib_to_mib(zram_max_sigkill_kb),
|
kib_to_mib(zram_max_sigkill_kb),
|
||||||
percent(zram_max_sigkill_kb / mem_total))
|
percent(zram_max_sigkill_kb / mem_total))
|
||||||
|
|
||||||
find_victim_and_send_signal(signal.SIGKILL)
|
find_victim_and_send_signal(SIGKILL)
|
||||||
|
|
||||||
# MEM SWAP TERM
|
# MEM SWAP TERM
|
||||||
elif mem_available <= mem_min_sigterm_kb and swap_free <= swap_min_sigterm_kb:
|
elif mem_available <= mem_min_sigterm_kb and swap_free <= swap_min_sigterm_kb:
|
||||||
@ -1411,7 +1408,7 @@ while True:
|
|||||||
kib_to_mib(swap_min_sigterm_kb),
|
kib_to_mib(swap_min_sigterm_kb),
|
||||||
swap_sigterm_pc)
|
swap_sigterm_pc)
|
||||||
|
|
||||||
find_victim_and_send_signal(signal.SIGTERM)
|
find_victim_and_send_signal(SIGTERM)
|
||||||
|
|
||||||
# ZRAM TERM
|
# ZRAM TERM
|
||||||
elif mem_used_zram >= zram_max_sigterm_kb:
|
elif mem_used_zram >= zram_max_sigterm_kb:
|
||||||
@ -1424,7 +1421,7 @@ while True:
|
|||||||
kib_to_mib(zram_max_sigterm_kb),
|
kib_to_mib(zram_max_sigterm_kb),
|
||||||
percent(zram_max_sigterm_kb / mem_total))
|
percent(zram_max_sigterm_kb / mem_total))
|
||||||
|
|
||||||
find_victim_and_send_signal(signal.SIGTERM)
|
find_victim_and_send_signal(SIGTERM)
|
||||||
|
|
||||||
# LOW MEMORY WARNINGS
|
# LOW MEMORY WARNINGS
|
||||||
elif gui_low_memory_warnings:
|
elif gui_low_memory_warnings:
|
||||||
|
Loading…
Reference in New Issue
Block a user