Revision 57ca011e

b/Makefile.am
273 273
utils_PYTHON = \
274 274
	lib/utils/__init__.py \
275 275
	lib/utils/algo.py \
276
	lib/utils/cfunc.py \
276 277
	lib/utils/filelock.py \
277 278
	lib/utils/hash.py \
278 279
	lib/utils/io.py \
279 280
	lib/utils/log.py \
280
	lib/utils/mlock.py \
281 281
	lib/utils/nodesetup.py \
282 282
	lib/utils/process.py \
283 283
	lib/utils/retry.py \
......
724 724
	test/ganeti.tools.ensure_dirs_unittest.py \
725 725
	test/ganeti.uidpool_unittest.py \
726 726
	test/ganeti.utils.algo_unittest.py \
727
	test/ganeti.utils.cfunc_unittest.py \
727 728
	test/ganeti.utils.filelock_unittest.py \
728 729
	test/ganeti.utils.hash_unittest.py \
729 730
	test/ganeti.utils.io_unittest.py \
730 731
	test/ganeti.utils.log_unittest.py \
731
	test/ganeti.utils.mlock_unittest.py \
732 732
	test/ganeti.utils.nodesetup_unittest.py \
733 733
	test/ganeti.utils.process_unittest.py \
734 734
	test/ganeti.utils.retry_unittest.py \
b/lib/utils/__init__.py
43 43
from ganeti import compat
44 44

  
45 45
from ganeti.utils.algo import *
46
from ganeti.utils.cfunc import *
46 47
from ganeti.utils.filelock import *
47 48
from ganeti.utils.hash import *
48 49
from ganeti.utils.io import *
49 50
from ganeti.utils.log import *
50
from ganeti.utils.mlock import *
51 51
from ganeti.utils.nodesetup import *
52 52
from ganeti.utils.process import *
53 53
from ganeti.utils.retry import *
b/lib/utils/cfunc.py
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() (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 mlockall(MCL_CURRENT|MCL_FUTURE),
46
  see mlock(2) for more details. This function requires ctypes module.
47

  
48
  @raises errors.NoCtypesError: if 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
  # Some older version of the ctypes module don't have built-in functionality
64
  # to access the errno global variable, where function error codes are stored.
65
  # By declaring this variable as a pointer to an integer we can then access
66
  # its value correctly, should the mlockall call fail, in order to see what
67
  # the actual error code was.
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")
/dev/null
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() (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 mlockall(MCL_CURRENT|MCL_FUTURE),
46
  see mlock(2) for more details. This function requires ctypes module.
47

  
48
  @raises errors.NoCtypesError: if 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
  # Some older version of the ctypes module don't have built-in functionality
64
  # to access the errno global variable, where function error codes are stored.
65
  # By declaring this variable as a pointer to an integer we can then access
66
  # its value correctly, should the mlockall call fail, in order to see what
67
  # the actual error code was.
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")
b/test/ganeti.utils.cfunc_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 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

  
22
"""Script for testing utils.Mlockall
23

  
24
This test is run in a separate process because it changes memory behaviour.
25

  
26
"""
27

  
28
import unittest
29

  
30
from ganeti import utils
31
from ganeti import errors
32

  
33
import testutils
34

  
35

  
36
# WARNING: The following tests modify the memory behaviour at runtime. Don't
37
# add unrelated tests here.
38

  
39

  
40
class TestMlockallWithCtypes(unittest.TestCase):
41
  """Whether Mlockall() works if ctypes is present.
42

  
43
  """
44

  
45
  def test(self):
46
    if utils.ctypes:
47
      utils.Mlockall()
48

  
49

  
50
class TestMlockallWithNoCtypes(unittest.TestCase):
51
  """Whether Mlockall() raises an error if ctypes is not present.
52

  
53
  """
54

  
55
  def test(self):
56
    self.assertRaises(errors.NoCtypesError, utils.Mlockall, _ctypes=None)
57

  
58

  
59
if __name__ == "__main__":
60
  testutils.GanetiTestProgram()
/dev/null
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 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

  
22
"""Script for testing utils.Mlockall
23

  
24
This test is run in a separate process because it changes memory behaviour.
25

  
26
"""
27

  
28
import unittest
29

  
30
from ganeti import utils
31
from ganeti import errors
32

  
33
import testutils
34

  
35

  
36
# WARNING: The following tests modify the memory behaviour at runtime. Don't
37
# add unrelated tests here.
38

  
39

  
40
class TestMlockallWithCtypes(unittest.TestCase):
41
  """Whether Mlockall() works if ctypes is present.
42

  
43
  """
44

  
45
  def test(self):
46
    if utils.ctypes:
47
      utils.Mlockall()
48

  
49

  
50
class TestMlockallWithNoCtypes(unittest.TestCase):
51
  """Whether Mlockall() raises an error if ctypes is not present.
52

  
53
  """
54

  
55
  def test(self):
56
    self.assertRaises(errors.NoCtypesError, utils.Mlockall, _ctypes=None)
57

  
58

  
59
if __name__ == "__main__":
60
  testutils.GanetiTestProgram()

Also available in: Unified diff