[PEP8] Format code to comply with PEP8 style

This commit is contained in:
Maxim Velesyuk 2019-01-03 16:17:09 +00:00
parent e0daa984e2
commit 52dd97fb1e

112
nohang
View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3
"""A daemon that prevents OOM in Linux systems."""
from time import sleep, time
start_time = time()
import os
from operator import itemgetter
@ -12,6 +11,8 @@ from argparse import ArgumentParser
from sys import stdout
from signal import SIGKILL, SIGTERM
start_time = time()
sig_dict = {SIGKILL: 'SIGKILL',
SIGTERM: 'SIGTERM'}
@ -94,14 +95,6 @@ def check_zram():
return (mem_used_total_sum + disksize_sum * ZRAM_DISKSIZE_FACTOR) / 1024.0
def format_time(t):
t = int(t)
if t < 60:
@ -365,10 +358,8 @@ def send_notify_warn():
if root: # If nohang was started by root
# send notification to all active users with special script
# теперь можно напрямую уведомлять из кэша если он не устарел
Popen([
'/usr/bin/nohang_notify_low_mem',
'--mem', low_mem_percent,
@ -376,7 +367,6 @@ def send_notify_warn():
'--name', name
])
else: # Or by regular user
# send notification to user that runs this nohang
try:
@ -420,6 +410,7 @@ def send_notify(signal, name, pid):
except TimeoutExpired:
print('TimeoutExpired: ' + 'notify send signal')
def send_notify_etc(pid, name, command):
"""
Notificate about OOM Preventing.
@ -555,7 +546,6 @@ def find_victim_and_send_signal(signal):
uid = line.split('\t')[1]
continue
if n is vm_size_index:
vm_size = kib_to_mib(int(line.split('\t')[1][:-4]))
continue
@ -599,7 +589,8 @@ def find_victim_and_send_signal(signal):
except ValueError:
pass
oom_score = rline1('/proc/' + pid + '/oom_score') # тут может быть FileNotFoundError!
# тут может быть FileNotFoundError!
oom_score = rline1('/proc/' + pid + '/oom_score')
oom_score_adj = rline1('/proc/' + pid + '/oom_score_adj')
len_vm = len(str(vm_size))
@ -656,7 +647,6 @@ def find_victim_and_send_signal(signal):
victim_info, command, exit_status,
round(response_time * 1000))
# update stat_dict
key = "Run the command '\033[35m{}\033[0m'".format(command)
if key not in stat_dict:
@ -665,7 +655,6 @@ def find_victim_and_send_signal(signal):
new_value = stat_dict[key] + 1
stat_dict.update({key: new_value})
print(mem_info)
print(etc_info)
if gui_notifications:
@ -680,7 +669,8 @@ def find_victim_and_send_signal(signal):
round(response_time * 1000))
# update stat_dict
key = 'Send \033[35m{}\033[0m to \033[35m{}\033[0m'.format(sig_dict[signal], name)
key = 'Send \033[35m{}\033[0m to \033[35m{}\033[0m'.format(
sig_dict[signal], name)
if key not in stat_dict:
stat_dict.update({key: 1})
else:
@ -738,12 +728,10 @@ def sleep_after_check_mem():
else:
t = t_mem_zram
max_sleep_time = 1
if t > max_sleep_time:
t = 1
try:
if print_sleep_periods:
print('sleep', round(t, 2),
@ -968,14 +956,15 @@ try:
exit()
etc_dict[etc_name] = etc_command
# NEED VALIDATION!
if line.startswith('@PROCESSNAME_RE'):
a = line.partition('@PROCESSNAME_RE')[2].strip(' \n').partition('///')
a = line.partition('@PROCESSNAME_RE')[
2].strip(' \n').partition('///')
processname_re_list.append((a[0].strip(' '), a[2].strip(' ')))
if line.startswith('@CMDLINE_RE'):
a = line.partition('@CMDLINE_RE')[2].strip(' \n').partition('///')
a = line.partition('@CMDLINE_RE')[2].strip(
' \n').partition('///')
cmdline_re_list.append((a[0].strip(' '), a[2].strip(' ')))
if line.startswith('@UID_RE'):
@ -1020,8 +1009,6 @@ execute_the_command = conf_parse_bool('execute_the_command')
ignore_psi = conf_parse_bool('ignore_psi')
regex_matching = conf_parse_bool('regex_matching')
re_match_cmdline = conf_parse_bool('re_match_cmdline')
@ -1163,10 +1150,6 @@ else:
exit()
if 'min_delay_after_sigkill' in config_dict:
min_delay_after_sigkill = string_to_float_convert_test(
config_dict['min_delay_after_sigkill'])
@ -1181,12 +1164,6 @@ else:
exit()
if 'psi_avg10_sleep_time' in config_dict:
psi_avg10_sleep_time = string_to_float_convert_test(
config_dict['psi_avg10_sleep_time'])
@ -1201,9 +1178,6 @@ else:
exit()
if 'sigkill_psi_avg10' in config_dict:
sigkill_psi_avg10 = string_to_float_convert_test(
config_dict['sigkill_psi_avg10'])
@ -1218,8 +1192,6 @@ else:
exit()
if 'sigterm_psi_avg10' in config_dict:
sigterm_psi_avg10 = string_to_float_convert_test(
config_dict['sigterm_psi_avg10'])
@ -1234,14 +1206,6 @@ else:
exit()
if 'min_badness' in config_dict:
min_badness = string_to_int_convert_test(
config_dict['min_badness'])
@ -1378,9 +1342,6 @@ else:
mla_res = ''
if self_uid == 0:
root = True
decrease_res = 'OK'
@ -1431,7 +1392,7 @@ if print_config:
if decrease_oom_score_adj:
print('oom_score_adj_max: {}'.format(oom_score_adj_max))
print('\n4. Impact on the badness of processes via matching their' \
print('\n4. Impact on the badness of processes via matching their'
' names, cmdlines ir UIDs with regular expressions\n')
print('regex_matching: {}'.format(regex_matching))
@ -1527,18 +1488,6 @@ print('The duration of startup:',
print('Monitoring started!')
def save_env_cache():
z = '{}\n'.format(int(time()))
a = root_notify_env()
@ -1574,13 +1523,13 @@ def root_env_cache():
return save_env_cache()
delta_t = time() - int(cache[0])
if delta_t > cache_time:
print('cache time: {}, delta: {}, ' \
print('cache time: {}, delta: {}, '
'get new env and cache it'.format(
cache_time, round(delta_t)))
save_env_cache()
return root_notify_env()
else:
print('cache time: {}, delta: {}, ' \
print('cache time: {}, delta: {}, '
'get cached env'.format(
cache_time, round(delta_t)))
return cache[1]
@ -1592,25 +1541,11 @@ t2 = time()
# print(t2 - t1)
stdout.flush()
# exit()
sigterm_psi = sigterm_psi_avg10
sigkill_psi = sigkill_psi_avg10
# avg_min_time = 4
@ -1653,7 +1588,6 @@ while True:
# print('PSI is OK or psi_min_sleep_time_after_action did not pass')
pass
'''
if psi_support:
@ -1685,8 +1619,6 @@ while True:
print('PSI is OK or psi_min_sleep_time_after_action did not pass')
'''
mem_available, swap_total, swap_free = check_mem_and_swap()
# if swap_min_sigkill is set in percent
@ -1699,12 +1631,8 @@ while True:
if swap_warn_is_percent:
swap_min_warnings_kb = swap_total * swap_min_warnings_percent / 100.0
mem_used_zram = check_zram()
if print_mem_check_results:
# Calculate 'swap-column' width
@ -1736,16 +1664,12 @@ while True:
human(mem_used_zram, mem_len),
just_percent_mem(mem_used_zram / mem_total)))
# если swap_min_sigkill задан в абсолютной величине и Swap_total = 0
if swap_total > swap_min_sigkill_kb: # If swap_min_sigkill is absolute
swap_sigkill_pc = percent(swap_min_sigkill_kb / (swap_total + 0.1))
else:
swap_sigkill_pc = '-'
if swap_total > swap_min_sigterm_kb:
swap_sigterm_pc = percent(swap_min_sigterm_kb / (swap_total + 0.1))
else:
@ -1753,12 +1677,8 @@ while True:
# печатать так: SwapTotal = 0, ignore swapspace
swap_sigterm_pc = '-'
# далее пошло ветвление
# MEM SWAP KILL
if mem_available <= mem_min_sigkill_kb and \
swap_free <= swap_min_sigkill_kb:
@ -1856,7 +1776,3 @@ while True:
# SLEEP BETWEEN MEM CHECKS
else:
sleep_after_check_mem()