4 # Copyright (C) 2009, 2010, 2011 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """Wrapper around mlockall(2).
28 from ganeti import errors
31 # pylint: disable-msg=F0401
37 # Flags for mlockall() (from bits/mman.h)
42 def Mlockall(_ctypes=ctypes):
43 """Lock current process' virtual address space into RAM.
45 This is equivalent to the C call mlockall(MCL_CURRENT|MCL_FUTURE),
46 see mlock(2) for more details. This function requires ctypes module.
48 @raises errors.NoCtypesError: if ctypes module is not found
52 raise errors.NoCtypesError()
54 libc = _ctypes.cdll.LoadLibrary("libc.so.6")
56 logging.error("Cannot set memory lock, ctypes cannot load libc")
59 # Some older version of the ctypes module don't have built-in functionality
60 # to access the errno global variable, where function error codes are stored.
61 # By declaring this variable as a pointer to an integer we can then access
62 # its value correctly, should the mlockall call fail, in order to see what
63 # the actual error code was.
64 # pylint: disable-msg=W0212
65 libc.__errno_location.restype = _ctypes.POINTER(_ctypes.c_int)
67 if libc.mlockall(_MCL_CURRENT | _MCL_FUTURE):
68 # pylint: disable-msg=W0212
69 logging.error("Cannot set memory lock: %s",
70 os.strerror(libc.__errno_location().contents.value))
73 logging.debug("Memory lock set")