Statistics
| Branch: | Tag: | Revision:

root / lib / utils / mlock.py @ 364c350f

History | View | Annotate | Download (2.3 kB)

1
#
2
#
3

    
4
# Copyright (C) 2009, 2010, 2011 Google Inc.
5
#
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.
10
#
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.
15
#
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
19
# 02110-1301, USA.
20

    
21
"""Wrapper around mlockall(2).
22

23
"""
24

    
25
import os
26
import logging
27

    
28
from ganeti import errors
29

    
30
try:
31
  # pylint: disable=F0401
32
  import ctypes
33
except ImportError:
34
  ctypes = None
35

    
36

    
37
# Flags for mlockall(2) (from bits/mman.h)
38
_MCL_CURRENT = 1
39
_MCL_FUTURE = 2
40

    
41

    
42
def Mlockall(_ctypes=ctypes):
43
  """Lock current process' virtual address space into RAM.
44

45
  This is equivalent to the C call C{mlockall(MCL_CURRENT | MCL_FUTURE)}. See
46
  mlockall(2) for more details. This function requires the C{ctypes} module.
47

48
  @raises errors.NoCtypesError: If the C{ctypes} module is not found
49

50
  """
51
  if _ctypes is None:
52
    raise errors.NoCtypesError()
53

    
54
  try:
55
    libc = _ctypes.cdll.LoadLibrary("libc.so.6")
56
  except EnvironmentError, err:
57
    logging.error("Failure trying to load libc: %s", err)
58
    libc = None
59
  if libc is None:
60
    logging.error("Cannot set memory lock, ctypes cannot load libc")
61
    return
62

    
63
  # The ctypes module before Python 2.6 does not have built-in functionality to
64
  # access the global errno global (which, depending on the libc and build
65
  # options, is per thread), where function error codes are stored. Use GNU
66
  # libc's way to retrieve errno(3) instead, which is to use the pointer named
67
  # "__errno_location" (see errno.h and bits/errno.h).
68
  # pylint: disable=W0212
69
  libc.__errno_location.restype = _ctypes.POINTER(_ctypes.c_int)
70

    
71
  if libc.mlockall(_MCL_CURRENT | _MCL_FUTURE):
72
    # pylint: disable=W0212
73
    logging.error("Cannot set memory lock: %s",
74
                  os.strerror(libc.__errno_location().contents.value))
75
    return
76

    
77
  logging.debug("Memory lock set")