Print CPU usage at exit

This commit is contained in:
Alexey Avramov 2020-03-14 21:24:59 +09:00
parent 29287097b4
commit b4f41cfb58

View File

@ -3,7 +3,7 @@
import os import os
from ctypes import CDLL from ctypes import CDLL
from time import sleep, monotonic from time import sleep, monotonic, process_time
from operator import itemgetter from operator import itemgetter
from sys import stdout, stderr, argv, exit from sys import stdout, stderr, argv, exit
from re import search from re import search
@ -698,7 +698,7 @@ def pid_to_vm_size(pid):
return vm_size return vm_size
def signal_handler(signum, frame): def signal_handler_0000000000(signum, frame):
""" """
""" """
for i in sig_list: for i in sig_list:
@ -710,6 +710,26 @@ def signal_handler(signum, frame):
exit() exit()
def signal_handler(signum, frame):
"""
"""
for i in sig_list:
signal(i, signal_handler_inner)
log('Got the {} signal '.format(
sig_dict[signum]))
update_stat_dict_and_print(None)
m1 = monotonic()
pt1 = process_time()
ab = pt1 - pt0
perc = (pt1 - pt0) / (m1 - m0) * 100
log('CPU time since monitoring has started: {} ({}%); exit.'.format(
format_time(ab), round(perc, 3)))
exit()
def signal_handler_inner(signum, frame): def signal_handler_inner(signum, frame):
""" """
""" """
@ -1283,10 +1303,20 @@ def update_stat_dict_and_print(key):
if print_statistics: if print_statistics:
stats_msg = 'Total stat (what happened in the last {}):'.format( lsd = len(stat_dict)
if lsd == 0:
log('No corrective actions applied in the last {}'.format(
format_time(monotonic() - start_time)))
else:
stats_msg = 'What happened in the last {}:'.format(
format_time(monotonic() - start_time)) format_time(monotonic() - start_time))
for i in stat_dict: for i in stat_dict:
stats_msg += '\n {}: {}'.format(i, stat_dict[i]) stats_msg += '\n {}: {}'.format(i, stat_dict[i])
log(stats_msg) log(stats_msg)
@ -1369,18 +1399,18 @@ def format_time(t):
t = int(t) t = int(t)
if t < 60: if t < 60:
return '{} sec'.format(t) return '{}s'.format(t)
if t > 3600: if t > 3600:
h = t // 3600 h = t // 3600
s0 = t - h * 3600 s0 = t - h * 3600
m = s0 // 60 m = s0 // 60
s = s0 % 60 s = s0 % 60
return '{} h {} min {} sec'.format(h, m, s) return '{}h {}min {}s'.format(h, m, s)
m = t // 60 m = t // 60
s = t % 60 s = t % 60
return '{} min {} sec'.format(m, s) return '{}min {}s'.format(m, s)
def string_to_float_convert_test(string): def string_to_float_convert_test(string):
@ -2362,8 +2392,8 @@ def implement_corrective_action(
send_result = 'total response time: {} ms'.format( send_result = 'total response time: {} ms'.format(
round(response_time * 1000)) round(response_time * 1000))
preventing_oom_message = 'Implement a corrective action:' \ preventing_oom_message = 'Implementing a corrective action:' \
'\n Send {} to the victim; {}'.format( '\n Sending {} to the victim; {}'.format(
sig_dict[threshold], send_result) sig_dict[threshold], send_result)
if threshold is SIGKILL: if threshold is SIGKILL:
@ -2439,7 +2469,7 @@ def implement_corrective_action(
round(ma_mib, 1), round(sf_mib, 1))) round(ma_mib, 1), round(sf_mib, 1)))
if soft_match is False: if soft_match is False:
key = 'Send {} to {}'.format(sig_dict[threshold], name) key = 'Sending {} to {}'.format(sig_dict[threshold], name)
update_stat_dict_and_print(key) update_stat_dict_and_print(key)
else: else:
key = "Run the command '{}'".format(command) key = "Run the command '{}'".format(command)
@ -3649,6 +3679,10 @@ cmd_num_dict = dict()
cmd_num_dict['cmd_num'] = 0 cmd_num_dict['cmd_num'] = 0
m0 = monotonic()
pt0 = process_time()
while True: while True:
(masf_threshold, masf_info, mem_available, hard_threshold_min_swap_kb, (masf_threshold, masf_info, mem_available, hard_threshold_min_swap_kb,