2010-08-30 11:40:19 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
import SocketServer
|
|
|
|
import thread
|
|
|
|
import time
|
|
|
|
import xen.lowlevel.xs
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
from qmemman import SystemState
|
2012-03-28 00:21:01 +02:00
|
|
|
import qmemman_algo
|
|
|
|
from ConfigParser import SafeConfigParser
|
|
|
|
from optparse import OptionParser
|
|
|
|
from qubesutils import parse_size
|
|
|
|
|
|
|
|
config_path = '/etc/qubes/qmemman.conf'
|
2012-07-20 16:32:17 +02:00
|
|
|
SOCK_PATH='/var/run/qubes/qmemman.sock'
|
|
|
|
LOG_PATH='/var/log/qubes/qmemman.log'
|
2010-08-30 11:40:19 +02:00
|
|
|
|
|
|
|
system_state = SystemState()
|
|
|
|
global_lock = thread.allocate_lock()
|
|
|
|
|
|
|
|
def only_in_first_list(l1, l2):
|
|
|
|
ret=[]
|
|
|
|
for i in l1:
|
|
|
|
if not i in l2:
|
|
|
|
ret.append(i)
|
|
|
|
return ret
|
|
|
|
|
2011-05-04 17:58:28 +02:00
|
|
|
def get_domain_meminfo_key(domain_id):
|
2010-08-30 11:40:19 +02:00
|
|
|
return '/local/domain/'+domain_id+'/memory/meminfo'
|
|
|
|
|
|
|
|
|
|
|
|
class WatchType:
|
|
|
|
def __init__(self, fn, param):
|
|
|
|
self.fn = fn
|
|
|
|
self.param = param
|
|
|
|
|
|
|
|
class XS_Watcher:
|
|
|
|
def __init__(self):
|
|
|
|
self.handle = xen.lowlevel.xs.xs()
|
2011-09-30 15:23:57 +02:00
|
|
|
self.handle.watch('@introduceDomain', WatchType(XS_Watcher.domain_list_changed, None))
|
|
|
|
self.handle.watch('@releaseDomain', WatchType(XS_Watcher.domain_list_changed, None))
|
2010-08-30 11:40:19 +02:00
|
|
|
self.watch_token_dict = {}
|
|
|
|
|
2011-05-04 17:58:28 +02:00
|
|
|
def domain_list_changed(self, param):
|
2010-08-30 11:40:19 +02:00
|
|
|
curr = self.handle.ls('', '/local/domain')
|
|
|
|
if curr == None:
|
|
|
|
return
|
|
|
|
global_lock.acquire()
|
|
|
|
for i in only_in_first_list(curr, self.watch_token_dict.keys()):
|
2011-05-04 17:58:28 +02:00
|
|
|
#new domain has been created
|
|
|
|
watch = WatchType(XS_Watcher.meminfo_changed, i)
|
2010-08-30 11:40:19 +02:00
|
|
|
self.watch_token_dict[i] = watch
|
2011-05-04 17:58:28 +02:00
|
|
|
self.handle.watch(get_domain_meminfo_key(i), watch)
|
2010-08-30 11:40:19 +02:00
|
|
|
system_state.add_domain(i)
|
|
|
|
for i in only_in_first_list(self.watch_token_dict.keys(), curr):
|
2011-05-04 17:58:28 +02:00
|
|
|
#domain destroyed
|
|
|
|
self.handle.unwatch(get_domain_meminfo_key(i), self.watch_token_dict[i])
|
2010-08-30 11:40:19 +02:00
|
|
|
self.watch_token_dict.pop(i)
|
|
|
|
system_state.del_domain(i)
|
|
|
|
global_lock.release()
|
2011-09-30 15:23:57 +02:00
|
|
|
system_state.do_balance()
|
2010-08-30 11:40:19 +02:00
|
|
|
|
2011-05-04 17:58:28 +02:00
|
|
|
def meminfo_changed(self, domain_id):
|
|
|
|
untrusted_meminfo_key = self.handle.read('', get_domain_meminfo_key(domain_id))
|
2011-05-04 17:10:01 +02:00
|
|
|
if untrusted_meminfo_key == None or untrusted_meminfo_key == '':
|
2010-08-30 11:40:19 +02:00
|
|
|
return
|
|
|
|
global_lock.acquire()
|
2011-05-04 17:10:01 +02:00
|
|
|
system_state.refresh_meminfo(domain_id, untrusted_meminfo_key)
|
2010-08-30 11:40:19 +02:00
|
|
|
global_lock.release()
|
|
|
|
|
|
|
|
def watch_loop(self):
|
|
|
|
# sys.stderr = file('/var/log/qubes/qfileexchgd.errors', 'a')
|
|
|
|
while True:
|
|
|
|
result = self.handle.read_watch()
|
|
|
|
token = result[1]
|
|
|
|
token.fn(self, token.param)
|
|
|
|
|
|
|
|
|
|
|
|
class QMemmanReqHandler(SocketServer.BaseRequestHandler):
|
|
|
|
"""
|
|
|
|
The RequestHandler class for our server.
|
|
|
|
|
|
|
|
It is instantiated once per connection to the server, and must
|
|
|
|
override the handle() method to implement communication to the
|
|
|
|
client.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def handle(self):
|
2010-08-30 14:50:48 +02:00
|
|
|
got_lock = False
|
2010-08-30 11:40:19 +02:00
|
|
|
# self.request is the TCP socket connected to the client
|
|
|
|
while True:
|
|
|
|
self.data = self.request.recv(1024).strip()
|
|
|
|
if len(self.data) == 0:
|
|
|
|
print 'EOF'
|
2010-08-30 14:50:48 +02:00
|
|
|
if got_lock:
|
|
|
|
global_lock.release()
|
2010-08-30 11:40:19 +02:00
|
|
|
return
|
2010-08-30 14:50:48 +02:00
|
|
|
if got_lock:
|
|
|
|
print 'Second request over qmemman.sock ?'
|
2010-08-30 11:40:19 +02:00
|
|
|
return
|
|
|
|
global_lock.acquire()
|
2010-08-30 14:50:48 +02:00
|
|
|
got_lock = True
|
2010-08-30 11:40:19 +02:00
|
|
|
if system_state.do_balloon(int(self.data)):
|
|
|
|
resp = "OK\n"
|
|
|
|
else:
|
|
|
|
resp = "FAIL\n"
|
|
|
|
self.request.send(resp)
|
|
|
|
|
|
|
|
|
2012-07-20 16:32:17 +02:00
|
|
|
def start_server(server):
|
2010-08-30 11:40:19 +02:00
|
|
|
server.serve_forever()
|
|
|
|
|
|
|
|
class QMemmanServer:
|
|
|
|
@staticmethod
|
|
|
|
def main():
|
2012-03-28 00:21:01 +02:00
|
|
|
usage = "usage: %prog [options]"
|
|
|
|
parser = OptionParser(usage)
|
|
|
|
parser.add_option("-c", "--config", action="store", dest="config", default=config_path)
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2012-07-20 16:32:17 +02:00
|
|
|
logfd = os.open(LOG_PATH, os.O_WRONLY|os.O_APPEND|os.O_CREAT, 0644)
|
|
|
|
if logfd < 0:
|
|
|
|
print sys.stderr, "ERROR: Failed to open log file (%s)" % LOG_PATH
|
|
|
|
exit(1)
|
|
|
|
# reinitialize python stdout/err
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
|
|
|
os.dup2(logfd, 1)
|
|
|
|
os.dup2(logfd, 2)
|
|
|
|
os.close(logfd)
|
|
|
|
devnull = os.open('/dev/null', os.O_RDONLY)
|
|
|
|
os.dup2(devnull, 0)
|
|
|
|
|
2012-03-28 00:21:01 +02:00
|
|
|
config = SafeConfigParser({
|
|
|
|
'vm-min-mem': str(qmemman_algo.MIN_PREFMEM),
|
|
|
|
'dom0-mem-boost': str(qmemman_algo.DOM0_MEM_BOOST),
|
|
|
|
'cache-margin-factor': str(qmemman_algo.CACHE_FACTOR)
|
|
|
|
})
|
|
|
|
config.read(options.config)
|
|
|
|
if config.has_section('global'):
|
|
|
|
qmemman_algo.MIN_PREFMEM = parse_size(config.get('global', 'vm-min-mem'))
|
|
|
|
qmemman_algo.DOM0_MEM_BOOST = parse_size(config.get('global', 'dom0-mem-boost'))
|
|
|
|
qmemman_algo.CACHE_FACTOR = config.getfloat('global', 'cache-margin-factor')
|
|
|
|
|
|
|
|
print "values: %s, %s, %s" % (str(qmemman_algo.MIN_PREFMEM), str(qmemman_algo.DOM0_MEM_BOOST), str(qmemman_algo.CACHE_FACTOR))
|
|
|
|
|
2012-07-20 16:32:17 +02:00
|
|
|
try:
|
|
|
|
os.unlink(SOCK_PATH)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
os.umask(0)
|
|
|
|
server = SocketServer.UnixStreamServer(SOCK_PATH, QMemmanReqHandler)
|
|
|
|
os.umask(077)
|
|
|
|
if os.fork() == 0:
|
|
|
|
thread.start_new_thread(start_server, tuple([server]))
|
|
|
|
XS_Watcher().watch_loop()
|