Statistics
| Branch: | Tag: | Revision:

root / lib / utils / mlock.py @ 8dc76d54

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 b459a848 Andrea Spadaccini
  # pylint: disable=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 142c1ad0 Guido Trotter
# 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 142c1ad0 Guido Trotter
  This is equivalent to the C call mlockall(MCL_CURRENT|MCL_FUTURE),
46 142c1ad0 Guido Trotter
  see mlock(2) for more details. This function requires ctypes module.
47 36a4acd4 Michael Hanselmann

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