Revision 4b6fa0bf lib/utils.py

b/lib/utils.py
57 57
  import sha
58 58
  sha1 = sha.new
59 59

  
60
try:
61
  import ctypes
62
except ImportError:
63
  ctypes = None
64

  
60 65
from ganeti import errors
61 66
from ganeti import constants
62 67

  
......
83 88
_STRUCT_UCRED = "iII"
84 89
_STRUCT_UCRED_SIZE = struct.calcsize(_STRUCT_UCRED)
85 90

  
91
# Flags for mlockall() (from bits/mman.h)
92
_MCL_CURRENT = 1
93
_MCL_FUTURE = 2
94

  
86 95

  
87 96
class RunResult(object):
88 97
  """Holds the result of running external programs.
......
1739 1748
    _CloseFDNoErr(fd)
1740 1749

  
1741 1750

  
1751
def Mlockall():
1752
  """Lock current process' virtual address space into RAM.
1753

  
1754
  This is equivalent to the C call mlockall(MCL_CURRENT|MCL_FUTURE),
1755
  see mlock(2) for more details. This function requires ctypes module.
1756

  
1757
  """
1758
  if ctypes is None:
1759
    logging.warning("Cannot set memory lock, ctypes module not found")
1760
    return
1761

  
1762
  libc = ctypes.cdll.LoadLibrary("libc.so.6")
1763
  if libc is None:
1764
    logging.error("Cannot set memory lock, ctypes cannot load libc")
1765
    return
1766

  
1767
  # Some older version of the ctypes module don't have built-in functionality
1768
  # to access the errno global variable, where function error codes are stored.
1769
  # By declaring this variable as a pointer to an integer we can then access
1770
  # its value correctly, should the mlockall call fail, in order to see what
1771
  # the actual error code was.
1772
  libc.__errno_location.restype = ctypes.POINTER(ctypes.c_int)
1773

  
1774
  if libc.mlockall(_MCL_CURRENT | _MCL_FUTURE):
1775
    logging.error("Cannot set memory lock: %s" %
1776
                  os.strerror(libc.__errno_location().contents.value))
1777
    return
1778

  
1779
  logging.debug("Memory lock set")
1780

  
1781

  
1742 1782
def Daemonize(logfile):
1743 1783
  """Daemonize the current process.
1744 1784

  

Also available in: Unified diff