[CRIU] [PATCH 1/3] p.haul: move all criu requests-related code to separate file

Nikita Spiridonov nspiridonov at odin.com
Fri Sep 4 04:15:21 PDT 2015


Move all requests-related code from p_haul_iters and p_haul_service
to separate file. This refactoring is needed to implement
module-specific dump/restore requests processing.

Signed-off-by: Nikita Spiridonov <nspiridonov at odin.com>
---
 phaul/criu_api.py       |   12 +----
 phaul/criu_req.py       |  108 +++++++++++++++++++++++++++++++++++++++++++++++
 phaul/p_haul_iters.py   |   56 +++----------------------
 phaul/p_haul_service.py |   55 ++----------------------
 4 files changed, 120 insertions(+), 111 deletions(-)
 create mode 100644 phaul/criu_req.py

diff --git a/phaul/criu_api.py b/phaul/criu_api.py
index f03e266..1b7781d 100644
--- a/phaul/criu_api.py
+++ b/phaul/criu_api.py
@@ -9,18 +9,10 @@ import util
 import subprocess
 import pycriu.rpc as cr_rpc
 import pycriu.images as images
+import criu_req
 
 criu_binary = "criu"
 
-req_types = {
-	cr_rpc.DUMP: "dump",
-	cr_rpc.PRE_DUMP: "pre_dump",
-	cr_rpc.PAGE_SERVER: "page_server",
-	cr_rpc.RESTORE: "restore",
-	cr_rpc.CPUINFO_DUMP: "cpuinfo-dump",
-	cr_rpc.CPUINFO_CHECK: "cpuinfo-check",
-}
-
 cpuinfo_img_name = "cpuinfo.img"
 
 def_verb = 2
@@ -62,7 +54,7 @@ class criu_conn:
 
 	def send_req(self, req):
 		req.opts.log_level = self.verb
-		req.opts.log_file = "criu_%s.%d.log" % (req_types[req.type], self._iter)
+		req.opts.log_file = "criu_%s.%d.log" % (criu_req.get_name(req), self._iter)
 		self._cs.send(req.SerializeToString())
 		self._iter += 1
 		self._last_req = req.type
diff --git a/phaul/criu_req.py b/phaul/criu_req.py
new file mode 100644
index 0000000..fc03093
--- /dev/null
+++ b/phaul/criu_req.py
@@ -0,0 +1,108 @@
+#
+# CRIU requests creation and initialization helper methods
+#
+
+import pycriu.rpc
+
+_names = {
+	pycriu.rpc.DUMP: "dump",
+	pycriu.rpc.PRE_DUMP: "pre_dump",
+	pycriu.rpc.PAGE_SERVER: "page_server",
+	pycriu.rpc.RESTORE: "restore",
+	pycriu.rpc.CPUINFO_DUMP: "cpuinfo-dump",
+	pycriu.rpc.CPUINFO_CHECK: "cpuinfo-check",
+}
+
+def get_name(req):
+	"""Return printable request name"""
+	return _names[req.type]
+
+def _make_req(typ, htype):
+	"""Prepare generic criu request"""
+	req = pycriu.rpc.criu_req()
+	req.type = typ
+	htype.adjust_criu_req(req)
+	return req
+
+def make_cpuinfo_dump_req(htype, img):
+	"""Prepare cpuinfo dump criu request (source side)"""
+	req = _make_req(pycriu.rpc.CPUINFO_DUMP, htype)
+	req.opts.images_dir_fd = img.work_dir_fd()
+	req.keep_open = True
+	return req
+
+def _make_common_dump_req(typ, pid, htype, img, connection, fs):
+	"""Prepare common criu request for pre-dump or dump (source side)"""
+
+	req = _make_req(typ, htype)
+	req.opts.pid = pid
+	req.opts.ps.fd = connection.mem_sk_fileno()
+	req.opts.track_mem = True
+
+	req.opts.images_dir_fd = img.image_dir_fd()
+	req.opts.work_dir_fd = img.work_dir_fd()
+	p_img = img.prev_image_dir()
+	if p_img:
+		req.opts.parent_img = p_img
+	if not fs.persistent_inodes():
+		req.opts.force_irmap = True
+
+	return req
+
+def make_predump_req(pid, htype, img, connection, fs):
+	"""Prepare pre-dump criu request (source side)"""
+	return _make_common_dump_req(
+		pycriu.rpc.PRE_DUMP, pid, htype, img, connection, fs)
+
+def make_dump_req(pid, htype, img, connection, fs):
+	"""Prepare dump criu request (source side)"""
+	req = _make_common_dump_req(
+		pycriu.rpc.DUMP, pid, htype, img, connection, fs)
+	req.opts.notify_scripts = True
+	req.opts.file_locks = True
+	req.opts.evasive_devices = True
+	req.opts.link_remap = True
+	if htype.can_migrate_tcp():
+		req.opts.tcp_established = True
+	return req
+
+def make_page_server_req(htype, img, connection):
+	"""Prepare page server criu request (destination side)"""
+
+	req = _make_req(pycriu.rpc.PAGE_SERVER, htype)
+	req.keep_open = True
+	req.opts.ps.fd = connection.mem_sk_fileno()
+	req.opts.images_dir_fd = img.image_dir_fd()
+	req.opts.work_dir_fd = img.work_dir_fd()
+
+	p_img = img.prev_image_dir()
+	if p_img:
+		req.opts.parent_img = p_img
+
+	return req
+
+def make_cpuinfo_check_req(htype, img):
+	"""Prepare cpuinfo check criu request (destination side)"""
+	req = _make_req(pycriu.rpc.CPUINFO_CHECK, htype)
+	req.keep_open = True
+	req.opts.images_dir_fd = img.work_dir_fd()
+	return req
+
+def make_restore_req(htype, img, nroot):
+	"""Prepare restore criu request (destination side)"""
+
+	req = _make_req(pycriu.rpc.RESTORE, htype)
+	req.opts.images_dir_fd = img.image_dir_fd()
+	req.opts.work_dir_fd = img.work_dir_fd()
+	req.opts.notify_scripts = True
+
+	if htype.can_migrate_tcp():
+		req.opts.tcp_established = True
+
+	for veth in htype.veths():
+		req.opts.veths.add(if_in = veth.name, if_out = veth.pair)
+
+	if nroot:
+		req.opts.root = nroot
+
+	return req
diff --git a/phaul/p_haul_iters.py b/phaul/p_haul_iters.py
index 3a2585a..bca28de 100644
--- a/phaul/p_haul_iters.py
+++ b/phaul/p_haul_iters.py
@@ -7,6 +7,7 @@ import mstats
 import xem_rpc
 import pycriu.rpc as cr_rpc
 import criu_api
+import criu_req
 import p_haul_type
 
 # Constants for iterations management
@@ -58,7 +59,7 @@ class phaul_iter_worker:
 		print "Checking CPU compatibility"
 
 		print "  `- Dumping CPU info"
-		req = self.__make_cpuinfo_dump_req()
+		req = criu_req.make_cpuinfo_dump_req(self.htype, self.img)
 		resp = self.criu.send_req(req)
 		if not resp.success:
 			raise Exception("Can't dump cpuinfo")
@@ -90,7 +91,8 @@ class phaul_iter_worker:
 
 			print "\tIssuing pre-dump command to service"
 
-			req = self.__make_predump_req()
+			req = criu_req.make_predump_req(
+				self.pid, self.htype, self.img, self.criu, self.fs)
 			resp = self.criu.send_req(req)
 			if not resp.success:
 				raise Exception("Pre-dump failed")
@@ -143,7 +145,8 @@ class phaul_iter_worker:
 
 		print "\tIssuing dump command to service"
 
-		req = self.__make_dump_req()
+		req = criu_req.make_dump_req(
+			self.pid, self.htype, self.img, self.criu, self.fs)
 		resp = self.criu.send_req(req)
 		while True:
 			if resp.type != cr_rpc.NOTIFY:
@@ -198,50 +201,3 @@ class phaul_iter_worker:
 		self._mstat.stop(self)
 		self.img.close()
 		self.criu.close()
-
-	def __make_req(self, typ):
-		"""Prepare generic criu request"""
-		req = cr_rpc.criu_req()
-		req.type = typ
-		self.htype.adjust_criu_req(req)
-		return req
-
-	def __make_common_dump_req(self, typ):
-		"""Prepare common criu request for pre-dump or dump"""
-
-		req = self.__make_req(typ)
-		req.opts.pid = self.pid
-		req.opts.ps.fd = self.criu.mem_sk_fileno()
-		req.opts.track_mem = True
-
-		req.opts.images_dir_fd = self.img.image_dir_fd()
-		req.opts.work_dir_fd = self.img.work_dir_fd()
-		p_img = self.img.prev_image_dir()
-		if p_img:
-			req.opts.parent_img = p_img
-		if not self.fs.persistent_inodes():
-			req.opts.force_irmap = True
-
-		return req
-
-	def __make_cpuinfo_dump_req(self):
-		"""Prepare cpuinfo dump criu request"""
-		req = self.__make_req(cr_rpc.CPUINFO_DUMP)
-		req.opts.images_dir_fd = self.img.work_dir_fd()
-		req.keep_open = True
-		return req
-
-	def __make_predump_req(self):
-		"""Prepare pre-dump criu request"""
-		return self.__make_common_dump_req(cr_rpc.PRE_DUMP)
-
-	def __make_dump_req(self):
-		"""Prepare dump criu request"""
-		req = self.__make_common_dump_req(cr_rpc.DUMP)
-		req.opts.notify_scripts = True
-		req.opts.file_locks = True
-		req.opts.evasive_devices = True
-		req.opts.link_remap = True
-		if self.htype.can_migrate_tcp():
-			req.opts.tcp_established = True
-		return req
diff --git a/phaul/p_haul_service.py b/phaul/p_haul_service.py
index c274fd7..bed34c2 100644
--- a/phaul/p_haul_service.py
+++ b/phaul/p_haul_service.py
@@ -6,6 +6,7 @@ import xem_rpc
 import pycriu.rpc as cr_rpc
 import images
 import criu_api
+import criu_req
 import p_haul_type
 
 class phaul_service:
@@ -54,7 +55,7 @@ class phaul_service:
 		print "Starting page server for iter %d" % self.dump_iter
 
 		print "\tSending criu rpc req"
-		req = self.__make_page_server_req()
+		req = criu_req.make_page_server_req(self.htype, self.img, self.criu)
 		resp = self.criu.send_req(req)
 		if not resp.success:
 			raise Exception("Failed to start page server")
@@ -77,7 +78,7 @@ class phaul_service:
 
 	def rpc_check_cpuinfo(self):
 		print "Checking cpuinfo"
-		req = self.__make_cpuinfo_check_req()
+		req = criu_req.make_cpuinfo_check_req(self.htype, self.img)
 		resp = self.criu.send_req(req)
 		print "   `-", resp.success
 		return resp.success
@@ -90,7 +91,7 @@ class phaul_service:
 		if nroot:
 			print "Restore root set to %s" % nroot
 
-		req = self.__make_restore_req(nroot)
+		req = criu_req.make_restore_req(self.htype, self.img, nroot)
 		resp = self.criu.send_req(req)
 		while True:
 			if resp.type == cr_rpc.NOTIFY:
@@ -124,51 +125,3 @@ class phaul_service:
 	def rpc_restore_time(self):
 		stats = criu_api.criu_get_rstats(self.img)
 		return stats.restore_time
-
-	def __make_req(self, typ):
-		"""Prepare generic criu request"""
-		req = cr_rpc.criu_req()
-		req.type = typ
-		self.htype.adjust_criu_req(req)
-		return req
-
-	def __make_page_server_req(self):
-		"""Prepare page server criu request"""
-
-		req = self.__make_req(cr_rpc.PAGE_SERVER)
-		req.keep_open = True
-		req.opts.ps.fd = self.criu.mem_sk_fileno()
-		req.opts.images_dir_fd = self.img.image_dir_fd()
-		req.opts.work_dir_fd = self.img.work_dir_fd()
-
-		p_img = self.img.prev_image_dir()
-		if p_img:
-			req.opts.parent_img = p_img
-
-		return req
-
-	def __make_cpuinfo_check_req(self):
-		"""Prepare cpuinfo check criu request"""
-		req = self.__make_req(cr_rpc.CPUINFO_CHECK)
-		req.keep_open = True
-		req.opts.images_dir_fd = self.img.work_dir_fd()
-		return req
-
-	def __make_restore_req(self, nroot):
-		"""Prepare restore criu request"""
-
-		req = self.__make_req(cr_rpc.RESTORE)
-		req.opts.images_dir_fd = self.img.image_dir_fd()
-		req.opts.work_dir_fd = self.img.work_dir_fd()
-		req.opts.notify_scripts = True
-
-		if self.htype.can_migrate_tcp():
-			req.opts.tcp_established = True
-
-		for veth in self.htype.veths():
-			req.opts.veths.add(if_in = veth.name, if_out = veth.pair)
-
-		if nroot:
-			req.opts.root = nroot
-
-		return req
-- 
1.7.1



More information about the CRIU mailing list