aboutsummaryrefslogtreecommitdiffstats
path: root/awlsim/common/mlock.py
blob: 0ea7d6b2ed037f2cae8ed3b939423aa04259647d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
#
# AWL simulator - mlock support
#
# Copyright 2019 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *

from awlsim.common.util import *
from awlsim.common.env import *
from awlsim.common.exceptions import *

import platform
import os


__all__ = [
	"MLockWrapper",
	"AwlSimMLock",
]


class MLockWrapper(object):
	"""OS mlock wrapper.
	"""

	singleton = None

	if platform.machine().lower() in (
			"alpha",
			"ppc", "ppc64", "ppcle", "ppc64le",
			"sparc", "sparc64" ):
		MCL_CURRENT	= 0x2000
		MCL_FUTURE	= 0x4000
		MCL_ONFAULT	= 0x8000
	else:
		MCL_CURRENT	= 0x1
		MCL_FUTURE	= 0x2
		MCL_ONFAULT	= 0x4

	def __init__(self):
		self.__ffi = None
		self.__libc = None

		if not osIsLinux:
			printDebug("mlock() is only available on Linux.")
			return

		try:
			from cffi import FFI
		except ImportError as e:
			printWarning("Failed to import CFFI: %s\n"
				     "Cannot use mlock() via CFFI." % (
				     str(e)))
			return

		self.__ffi = FFI()
		# Use getattr to avoid Cython cdef compile error.
		getattr(self.__ffi, "cdef")("""
			int mlock(const void *addr, size_t len);
			int mlock2(const void *addr, size_t len, int flags);
			int munlock(const void *addr, size_t len);
			int mlockall(int flags);
			int munlockall(void);
		""")
		self.__libc = self.__ffi.dlopen(None)

	@classmethod
	def get(cls):
		s = cls.singleton
		if not s:
			s = cls.singleton = cls()
		return s

	@classmethod
	def mlockall(cls, flags):
		error = "mlockall() is not supported on this operating system."
		s = cls.get()
		if s.__libc:
			ret = s.__libc.mlockall(flags)
			printDebug("mlockall(%X) = %d" % (flags, ret))
			error = os.strerror(s.__ffi.errno) if ret else ""
		return error

	@classmethod
	def munlockall(cls):
		error = "munlockall() is not supported on this operating system."
		s = cls.get()
		if s.__libc:
			ret = s.__libc.munlockall()
			printDebug("munlockall() = %d" % (ret, ))
			error = os.strerror(s.__ffi.errno) if ret else ""
		return error

class AwlSimMLock(MLockWrapper):
	"""Awlsim memory locking handler.
	"""

	@classmethod
	def lockMemory(cls, lock=True):
		mlockMode = AwlSimEnv.getMLock()
		if mlockMode == AwlSimEnv.MLOCK_OFF:
			pass # Do nothing.
		elif mlockMode in (AwlSimEnv.MLOCK_ALL,
				   AwlSimEnv.MLOCK_FORCEALL):
			if lock:
				flags = cls.MCL_CURRENT | cls.MCL_FUTURE
				error = cls.mlockall(flags)
				if error:
					msg = ("Failed to mlockall(): %s. "
					       "Check AWLSIM_MLOCK environment variable.") % error
					if mlockMode == AwlSimEnv.MLOCK_FORCEALL:
						raise AwlSimError(msg)
					printError(msg)
			else:
				cls.munlockall()
		else:
			assert(0)
bues.ch cgit interface