fix mlockall
This commit is contained in:
parent
4d8660957b
commit
f3d2e4e099
57
nohang
57
nohang
@ -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,40 +45,29 @@ stop_cont = False
|
|||||||
stop_cont_warn = 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
|
# 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):
|
def pid_to_state(pid):
|
||||||
return rline1('/proc/' + pid + '/stat').rpartition(')')[2][1]
|
return rline1('/proc/' + pid + '/stat').rpartition(')')[2][1]
|
||||||
@ -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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user