fix mlockall

This commit is contained in:
Alexey Avramov 2019-02-11 07:09:58 +09:00
parent 4d8660957b
commit f3d2e4e099

45
nohang
View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""A daemon that prevents OOM in Linux systems.""" """A daemon that prevents OOM in Linux systems."""
import os import os
import ctypes
from time import sleep, time from time import sleep, time
from operator import itemgetter from operator import itemgetter
from sys import stdout from sys import stdout
@ -44,20 +45,12 @@ stop_cont = False
stop_cont_warn = False stop_cont_warn = False
##########################################################################
# define functions
def mlockall():
'''
# 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_CURRENT = 1
MCL_FUTURE = 2 MCL_FUTURE = 2
@ -65,18 +58,15 @@ MCL_ONFAULT = 4
libc = ctypes.CDLL('libc.so.6', use_errno=True) libc = ctypes.CDLL('libc.so.6', use_errno=True)
def mlockall(flags=MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT): result = libc.mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT)
result = libc.mlockall(flags)
if result != 0: if result != 0:
raise Exception("cannot lock memmory, errno=%s" % ctypes.get_errno()) result = libc.mlockall(MCL_CURRENT|MCL_FUTURE)
if result != 0:
mlockall() 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')
# define functions
def pid_to_state(pid): def pid_to_state(pid):
@ -395,6 +385,9 @@ def notify_send_wait(title, body):
def notify_helper(title, body): def notify_helper(title, body):
'''GUI notification with UID = 0''' '''GUI notification with UID = 0'''
# os.system(notify_helper_path + ' foo bar &')
with Popen([notify_helper_path, title, body]) as proc: with Popen([notify_helper_path, title, body]) as proc:
try: try:
proc.wait(timeout=wait_time) proc.wait(timeout=wait_time)
@ -1028,6 +1021,12 @@ def calculate_percent(arg_key):
########################################################################## ##########################################################################
mlockall()
# find mem_total # find mem_total
# find positions of SwapFree and SwapTotal in /proc/meminfo # find positions of SwapFree and SwapTotal in /proc/meminfo