Statistics
| Branch: | Tag: | Revision:

root / lib / utils / mlock.py @ adc523ab

History | View | Annotate | Download (2.3 kB)

1 36a4acd4 Michael Hanselmann
#
2 36a4acd4 Michael Hanselmann
#
3 36a4acd4 Michael Hanselmann
4 36a4acd4 Michael Hanselmann
# Copyright (C) 2009, 2010, 2011 Google Inc.
5 36a4acd4 Michael Hanselmann
#
6 36a4acd4 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 36a4acd4 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 36a4acd4 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 36a4acd4 Michael Hanselmann
# (at your option) any later version.
10 36a4acd4 Michael Hanselmann
#
11 36a4acd4 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 36a4acd4 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 36a4acd4 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 36a4acd4 Michael Hanselmann
# General Public License for more details.
15 36a4acd4 Michael Hanselmann
#
16 36a4acd4 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 36a4acd4 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 36a4acd4 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 36a4acd4 Michael Hanselmann
# 02110-1301, USA.
20 36a4acd4 Michael Hanselmann
21 36a4acd4 Michael Hanselmann
"""Wrapper around mlockall(2).
22 36a4acd4 Michael Hanselmann

23 36a4acd4 Michael Hanselmann
"""
24 36a4acd4 Michael Hanselmann
25 36a4acd4 Michael Hanselmann
import os
26 36a4acd4 Michael Hanselmann
import logging
27 36a4acd4 Michael Hanselmann
28 36a4acd4 Michael Hanselmann
from ganeti import errors
29 36a4acd4 Michael Hanselmann
30 36a4acd4 Michael Hanselmann
try:
31 36a4acd4 Michael Hanselmann
  # pylint: disable-msg=F0401
32 36a4acd4 Michael Hanselmann
  import ctypes
33 36a4acd4 Michael Hanselmann
except ImportError:
34 36a4acd4 Michael Hanselmann
  ctypes = None
35 36a4acd4 Michael Hanselmann
36 36a4acd4 Michael Hanselmann
37 36a4acd4 Michael Hanselmann
# Flags for mlockall() (from bits/mman.h)
38 36a4acd4 Michael Hanselmann
_MCL_CURRENT = 1
39 36a4acd4 Michael Hanselmann
_MCL_FUTURE = 2
40 36a4acd4 Michael Hanselmann
41 36a4acd4 Michael Hanselmann
42 36a4acd4 Michael Hanselmann
def Mlockall(_ctypes=ctypes):
43 36a4acd4 Michael Hanselmann
  """Lock current process' virtual address space into RAM.
44 36a4acd4 Michael Hanselmann

45 36a4acd4 Michael Hanselmann
  This is equivalent to the C call mlockall(MCL_CURRENT|MCL_FUTURE),
46 36a4acd4 Michael Hanselmann
  see mlock(2) for more details. This function requires ctypes module.
47 36a4acd4 Michael Hanselmann

48 36a4acd4 Michael Hanselmann
  @raises errors.NoCtypesError: if ctypes module is not found
49 36a4acd4 Michael Hanselmann

50 36a4acd4 Michael Hanselmann
  """
51 36a4acd4 Michael Hanselmann
  if _ctypes is None:
52 36a4acd4 Michael Hanselmann
    raise errors.NoCtypesError()
53 36a4acd4 Michael Hanselmann
54 adc523ab Iustin Pop
  try:
55 adc523ab Iustin Pop
    libc = _ctypes.cdll.LoadLibrary("libc.so.6")
56 adc523ab Iustin Pop
  except EnvironmentError, err:
57 adc523ab Iustin Pop
    logging.error("Failure trying to load libc: %s", err)
58 adc523ab Iustin Pop
    libc = None
59 36a4acd4 Michael Hanselmann
  if libc is None:
60 36a4acd4 Michael Hanselmann
    logging.error("Cannot set memory lock, ctypes cannot load libc")
61 36a4acd4 Michael Hanselmann
    return
62 36a4acd4 Michael Hanselmann
63 36a4acd4 Michael Hanselmann
  # Some older version of the ctypes module don't have built-in functionality
64 36a4acd4 Michael Hanselmann
  # to access the errno global variable, where function error codes are stored.
65 36a4acd4 Michael Hanselmann
  # By declaring this variable as a pointer to an integer we can then access
66 36a4acd4 Michael Hanselmann
  # its value correctly, should the mlockall call fail, in order to see what
67 36a4acd4 Michael Hanselmann
  # the actual error code was.
68 36a4acd4 Michael Hanselmann
  # pylint: disable-msg=W0212
69 36a4acd4 Michael Hanselmann
  libc.__errno_location.restype = _ctypes.POINTER(_ctypes.c_int)
70 36a4acd4 Michael Hanselmann
71 36a4acd4 Michael Hanselmann
  if libc.mlockall(_MCL_CURRENT | _MCL_FUTURE):
72 36a4acd4 Michael Hanselmann
    # pylint: disable-msg=W0212
73 36a4acd4 Michael Hanselmann
    logging.error("Cannot set memory lock: %s",
74 36a4acd4 Michael Hanselmann
                  os.strerror(libc.__errno_location().contents.value))
75 36a4acd4 Michael Hanselmann
    return
76 36a4acd4 Michael Hanselmann
77 36a4acd4 Michael Hanselmann
  logging.debug("Memory lock set")