diff --git a/nohang b/nohang index be97fdd..aef85b5 100755 --- a/nohang +++ b/nohang @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """A daemon that prevents OOM in Linux systems.""" import os +import ctypes from time import sleep, time from operator import itemgetter from sys import stdout @@ -44,40 +45,29 @@ stop_cont = False stop_cont_warn = False - - - - - -''' -# define MCL_CURRENT 1 /* Lock all currently mapped pages. */ -# define MCL_FUTURE 2 /* Lock all additions to address - space. */ -# define MCL_ONFAULT 4 /* Lock all pages that are - faulted in. */ -https://code.woboq.org/gcc/include/bits/mman-linux.h.html -''' -import ctypes - -MCL_CURRENT = 1 -MCL_FUTURE = 2 -MCL_ONFAULT = 4 - -libc = ctypes.CDLL('libc.so.6', use_errno=True) - -def mlockall(flags=MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT): - result = libc.mlockall(flags) - if result != 0: - raise Exception("cannot lock memmory, errno=%s" % ctypes.get_errno()) - -mlockall() - - ########################################################################## # define functions +def mlockall(): + + MCL_CURRENT = 1 + MCL_FUTURE = 2 + MCL_ONFAULT = 4 + + libc = ctypes.CDLL('libc.so.6', use_errno=True) + + result = libc.mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT) + if result != 0: + result = libc.mlockall(MCL_CURRENT|MCL_FUTURE) + if result != 0: + print('Can not lock all memory') + else: + print('All memory locked with MCL_CURRENT|MCL_FUTURE') + else: + print('All memory locked with MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT') + def pid_to_state(pid): return rline1('/proc/' + pid + '/stat').rpartition(')')[2][1] @@ -395,6 +385,9 @@ def notify_send_wait(title, body): def notify_helper(title, body): '''GUI notification with UID = 0''' + + # os.system(notify_helper_path + ' foo bar &') + with Popen([notify_helper_path, title, body]) as proc: try: proc.wait(timeout=wait_time) @@ -1028,6 +1021,12 @@ def calculate_percent(arg_key): ########################################################################## + +mlockall() + + + + # find mem_total # find positions of SwapFree and SwapTotal in /proc/meminfo