handle signals

This commit is contained in:
Alexey Avramov 2019-04-23 02:33:40 +09:00
parent 9e6aafce89
commit bd05de7e7f
2 changed files with 39 additions and 12 deletions

37
nohang
View File

@ -6,13 +6,16 @@ from ctypes import CDLL
from time import sleep, time from time import sleep, time
from operator import itemgetter from operator import itemgetter
from sys import stdout, stderr, argv, exit, version from sys import stdout, stderr, argv, exit, version
from signal import SIGKILL, SIGTERM from signal import (signal,
SIGKILL, SIGTERM, SIGINT, SIGQUIT,
SIGHUP, SIGABRT, SIGSEGV, SIGBUS)
from re import search from re import search
from sre_constants import error as invalid_re from sre_constants import error as invalid_re
start_time = time() start_time = time()
help_mess = """usage: nohang [-h] [-v] [-t] [-p] [-c CONFIG] help_mess = """usage: nohang [-h] [-v] [-t] [-p] [-c CONFIG]
optional arguments: optional arguments:
@ -84,6 +87,16 @@ cgroup_v1_index, cgroup_v2_index = find_cgroup_indexes()
# define functions # define functions
def signal_handler(sig, frame):
log('Got signal {}'.format(sig))
update_stat_dict_and_print(None)
log('Exit')
stdout.flush()
exit()
def write(path, string): def write(path, string):
""" """
""" """
@ -607,6 +620,12 @@ def mlockall():
def update_stat_dict_and_print(key): def update_stat_dict_and_print(key):
""" """
""" """
if key is not None:
if key not in stat_dict: if key not in stat_dict:
stat_dict.update({key: 1}) stat_dict.update({key: 1})
@ -616,6 +635,7 @@ def update_stat_dict_and_print(key):
new_value = stat_dict[key] + 1 new_value = stat_dict[key] + 1
stat_dict.update({key: new_value}) stat_dict.update({key: new_value})
if print_total_stat: if print_total_stat:
stats_msg = 'Total stat (what happened in the last {}):'.format( stats_msg = 'Total stat (what happened in the last {}):'.format(
@ -1655,11 +1675,7 @@ def sleep_after_check_mem():
except OSError: # OSError: [Errno 105] No buffer space available except OSError: # OSError: [Errno 105] No buffer space available
pass pass
try:
sleep(t) sleep(t)
except KeyboardInterrupt:
exit(1)
def calculate_percent(arg_key): def calculate_percent(arg_key):
""" """
@ -2654,6 +2670,17 @@ if print_mem_check_results:
report0 = 0 report0 = 0
signal(SIGTERM, signal_handler)
signal(SIGINT, signal_handler)
signal(SIGQUIT, signal_handler)
signal(SIGHUP, signal_handler)
signal(SIGABRT, signal_handler)
signal(SIGSEGV, signal_handler)
signal(SIGBUS, signal_handler)
while True: while True:
if psi_support and not ignore_psi: if psi_support and not ignore_psi:

View File

@ -271,7 +271,7 @@ zram_max_warnings = 40 %
Valid values are floating-point numbers from the range [1; 300]. Valid values are floating-point numbers from the range [1; 300].
min_time_between_warnings = 60 min_time_between_warnings = 15
Ampersands (&) will be replaced with asterisks (*) in process Ampersands (&) will be replaced with asterisks (*) in process
names and in commands. names and in commands.