feat(oom-sort): add -r/--refresh flag

This commit is contained in:
actionless 2018-12-16 19:10:49 +01:00
parent 646ffb9d46
commit 0b2be659d1

View File

@ -7,6 +7,7 @@ sort processes by oom_score
from os import listdir from os import listdir
from argparse import ArgumentParser from argparse import ArgumentParser
from time import sleep
def parse_arguments(): def parse_arguments():
@ -28,14 +29,31 @@ def parse_arguments():
default=None, default=None,
type=str type=str
) )
parser.add_argument(
'--refresh',
'-r',
help='refresh interval (0 to disable); default: 0. '
'Use it with --num/-n to also limit the output length',
default=0,
type=int
)
return parser.parse_args() return parser.parse_args()
def human_readable(num): def human_readable(num):
'''KiB to MiB''' """
KiB to MiB
"""
return str(round(num / 1024.0)).rjust(6, ' ') return str(round(num / 1024.0)).rjust(6, ' ')
def clear_screen():
"""
print ANSI sequence to clear the screen
"""
print('\033c')
class TableIndexes: # pylint: disable=too-few-public-methods class TableIndexes: # pylint: disable=too-few-public-methods
""" """
table headers from /proc/*/status for further table headers from /proc/*/status for further
@ -149,6 +167,8 @@ class Application:
if self.display_cmdline is None: if self.display_cmdline is None:
self.display_cmdline = 99999 self.display_cmdline = 99999
self.refresh_interval = args.refresh
def print_stats(self): def print_stats(self):
""" """
print processes stats sorted by OOM score print processes stats sorted by OOM score
@ -180,11 +200,29 @@ class Application:
) )
) )
def oom_top(self):
"""
show `top`-like refreshing stats
"""
while True:
try:
clear_screen()
print("Refreshing each {} seconds, press <Ctrl+C> to interrupt:".format(
self.refresh_interval
))
self.print_stats()
sleep(self.refresh_interval)
except KeyboardInterrupt:
break
def main(self): def main(self):
""" """
application entrypoint application entrypoint
""" """
self.print_stats() if not self.refresh_interval:
self.print_stats()
else:
self.oom_top()
if __name__ == "__main__": if __name__ == "__main__":