[CRIU] [PATCH 1/7] p.haul: cleanup code

Nikita Spiridonov nspiridonov at odin.com
Tue Oct 6 08:28:29 PDT 2015


Rename some variables to simplify code understanding.

Signed-off-by: Nikita Spiridonov <nspiridonov at odin.com>
---
 phaul/criu_api.py       |   17 ++++-----
 phaul/images.py         |   12 +++---
 phaul/mstats.py         |    2 +-
 phaul/p_haul_iters.py   |   89 +++++++++++++++++++++++++----------------------
 phaul/p_haul_service.py |   30 ++++++++--------
 5 files changed, 77 insertions(+), 73 deletions(-)

diff --git a/phaul/criu_api.py b/phaul/criu_api.py
index 725b2db..81d22b1 100644
--- a/phaul/criu_api.py
+++ b/phaul/criu_api.py
@@ -8,8 +8,7 @@ import os
 import subprocess
 import logging
 import util
-import pycriu.rpc as cr_rpc
-import pycriu.images as images
+import pycriu
 import criu_req
 
 criu_binary = "criu"
@@ -46,9 +45,9 @@ class criu_conn:
 		self.verb = level
 
 	def _recv_resp(self):
-		resp = cr_rpc.criu_resp()
+		resp = pycriu.rpc.criu_resp()
 		resp.ParseFromString(self._cs.recv(1024))
-		if not resp.type in (cr_rpc.NOTIFY, self._last_req):
+		if not resp.type in (pycriu.rpc.NOTIFY, self._last_req):
 			raise Exception("CRIU RPC error (%d/%d)" % (resp.type, self._last_req))
 
 		return resp
@@ -63,8 +62,8 @@ class criu_conn:
 		return self._recv_resp()
 
 	def ack_notify(self, success = True):
-		req = cr_rpc.criu_req()
-		req.type = cr_rpc.NOTIFY
+		req = pycriu.rpc.criu_req()
+		req.type = pycriu.rpc.NOTIFY
 		req.notify_success = True
 		self._cs.send(req.SerializeToString())
 
@@ -79,9 +78,9 @@ class criu_conn:
 
 def criu_get_stats(img, file_name):
 	with open(os.path.join(img.work_dir(), file_name)) as f:
-		stats_dict = images.load(f)
-		stats = images.stats_pb2.stats_entry()
-		images.pb2dict.dict2pb(stats_dict['entries'][0], stats)
+		stats_dict = pycriu.images.load(f)
+		stats = pycriu.images.stats_pb2.stats_entry()
+		pycriu.images.pb2dict.dict2pb(stats_dict['entries'][0], stats)
 		return stats
 
 def criu_get_dstats(img):
diff --git a/phaul/images.py b/phaul/images.py
index b5bd27c..ac6e133 100644
--- a/phaul/images.py
+++ b/phaul/images.py
@@ -134,7 +134,7 @@ class phaul_images:
 	# Images transfer
 	# Are there better ways for doing this?
 
-	def sync_imgs_to_target(self, th, htype, sock):
+	def sync_imgs_to_target(self, target_host, htype, sock):
 		# Pre-dump doesn't generate any images (yet?)
 		# so copy only those from the top dir
 		logging.info("Sending images to target")
@@ -142,7 +142,7 @@ class phaul_images:
 		start = time.time()
 		cdir = self.image_dir()
 
-		th.start_accept_images(phaul_images.IMGDIR)
+		target_host.start_accept_images(phaul_images.IMGDIR)
 		tf = img_tar(sock, cdir)
 
 		logging.info("\tPack")
@@ -154,16 +154,16 @@ class phaul_images:
 			tf.add(himg[1], himg[0])
 
 		tf.close()
-		th.stop_accept_images()
+		target_host.stop_accept_images()
 
 		self.sync_time = time.time() - start
 
-	def send_cpuinfo(self, th, sock):
-		th.start_accept_images(phaul_images.WDIR)
+	def send_cpuinfo(self, target_host, sock):
+		target_host.start_accept_images(phaul_images.WDIR)
 		tf = img_tar(sock, self.work_dir())
 		tf.add(criu_api.cpuinfo_img_name)
 		tf.close()
-		th.stop_accept_images()
+		target_host.stop_accept_images()
 
 	def start_accept_images(self, dir_id, sk):
 		if dir_id == phaul_images.WDIR:
diff --git a/phaul/mstats.py b/phaul/mstats.py
index dad9747..00ec6f1 100644
--- a/phaul/mstats.py
+++ b/phaul/mstats.py
@@ -13,7 +13,7 @@ class migration_stats:
 		self._start_time = time.time()
 
 	def stop(self, iters):
-		self._rst_time = iters.th.restore_time()
+		self._rst_time = iters.get_target_host().restore_time()
 		self._img_sync_time = iters.img.img_sync_time()
 		self._end_time = time.time()
 
diff --git a/phaul/p_haul_iters.py b/phaul/p_haul_iters.py
index f23e799..a7bcfe8 100644
--- a/phaul/p_haul_iters.py
+++ b/phaul/p_haul_iters.py
@@ -6,7 +6,7 @@ import logging
 import images
 import mstats
 import xem_rpc
-import pycriu.rpc as cr_rpc
+import pycriu
 import criu_api
 import criu_req
 import p_haul_type
@@ -24,17 +24,13 @@ phaul_iter_grow_max = 10
 
 class phaul_iter_worker:
 	def __init__(self, p_type, host):
-		self._mstat = mstats.migration_stats()
-		self.iteration = 0
-		self.prev_stats = None
-
 		logging.info("Connecting to target host")
-		self.th = xem_rpc.rpc_proxy(host)
-		self.data_sk = self.th.open_socket("datask")
+		self.target_host = xem_rpc.rpc_proxy(host)
+		self.data_socket = self.target_host.open_socket("datask")
 
 		logging.info("Setting up local")
 		self.img = images.phaul_images("dmp")
-		self.criu = criu_api.criu_conn(self.data_sk)
+		self.criu_connection = criu_api.criu_conn(self.data_socket)
 		self.htype = p_haul_type.get_src(p_type)
 		if not self.htype:
 			raise Exception("No htype driver found")
@@ -47,11 +43,14 @@ class phaul_iter_worker:
 		self.fs.set_target_host(host[0])
 
 		logging.info("Setting up remote")
-		self.th.setup(p_type)
+		self.target_host.setup(p_type)
+
+	def get_target_host(self):
+		return self.target_host
 
 	def set_options(self, opts):
-		self.th.set_options(opts)
-		self.criu.verbose(opts["verbose"])
+		self.target_host.set_options(opts)
+		self.criu_connection.verbose(opts["verbose"])
 		self.img.set_options(opts)
 		self.htype.set_options(opts)
 		self.__force = opts["force"]
@@ -61,19 +60,24 @@ class phaul_iter_worker:
 
 		logging.info("\t`- Dumping CPU info")
 		req = criu_req.make_cpuinfo_dump_req(self.htype, self.img)
-		resp = self.criu.send_req(req)
+		resp = self.criu_connection.send_req(req)
 		if not resp.success:
 			raise Exception("Can't dump cpuinfo")
 
 		logging.info("\t`- Sending CPU info")
-		self.img.send_cpuinfo(self.th, self.data_sk)
+		self.img.send_cpuinfo(self.target_host, self.data_socket)
 
 		logging.info("\t`- Checking CPU info")
-		if not self.th.check_cpuinfo():
+		if not self.target_host.check_cpuinfo():
 			raise Exception("CPUs mismatch")
 
 	def start_migration(self):
-		self._mstat.start()
+
+		migration_stats = mstats.migration_stats()
+		prev_dstats = None
+		iter_index = 0
+
+		migration_stats.start()
 
 		if not self.__force:
 			self.validate_cpu()
@@ -85,25 +89,25 @@ class phaul_iter_worker:
 		logging.info("Starting iterations")
 
 		while True:
-			logging.info("* Iteration %d", self.iteration)
+			logging.info("* Iteration %d", iter_index)
 
-			self.th.start_iter()
+			self.target_host.start_iter()
 			self.img.new_image_dir()
 
 			logging.info("\tIssuing pre-dump command to service")
 
 			req = criu_req.make_predump_req(
-				self.pid, self.htype, self.img, self.criu, self.fs)
-			resp = self.criu.send_req(req)
+				self.pid, self.htype, self.img, self.criu_connection, self.fs)
+			resp = self.criu_connection.send_req(req)
 			if not resp.success:
 				raise Exception("Pre-dump failed")
 
 			logging.info("\tPre-dump succeeded")
 
-			self.th.end_iter()
+			self.target_host.end_iter()
 
-			stats = criu_api.criu_get_dstats(self.img)
-			self._mstat.iteration(stats)
+			dstats = criu_api.criu_get_dstats(self.img)
+			migration_stats.iteration(dstats)
 
 			#
 			# Need to decide whether we do next iteration
@@ -113,23 +117,23 @@ class phaul_iter_worker:
 
 			logging.info("Checking iteration progress:")
 
-			if stats.pages_written <= phaul_iter_min_size:
+			if dstats.pages_written <= phaul_iter_min_size:
 				logging.info("\t> Small dump")
 				break;
 
-			if self.prev_stats:
-				w_add = stats.pages_written - self.prev_stats.pages_written
-				w_add = w_add * 100 / self.prev_stats.pages_written
+			if prev_dstats:
+				w_add = dstats.pages_written - prev_dstats.pages_written
+				w_add = w_add * 100 / prev_dstats.pages_written
 				if w_add > phaul_iter_grow_max:
 					logging.info("\t> Iteration grows")
 					break
 
-			if self.iteration >= phaul_iter_max:
+			if iter_index >= phaul_iter_max:
 				logging.info("\t> Too many iterations")
 				break
 
-			self.iteration += 1
-			self.prev_stats = stats
+			iter_index += 1
+			prev_dstats = dstats
 			logging.info("\t> Proceed to next iteration")
 
 			self.fs.next_iteration()
@@ -141,16 +145,16 @@ class phaul_iter_worker:
 
 		logging.info("Final dump and restore")
 
-		self.th.start_iter()
+		self.target_host.start_iter()
 		self.img.new_image_dir()
 
 		logging.info("\tIssuing dump command to service")
 
 		req = criu_req.make_dump_req(
-			self.pid, self.htype, self.img, self.criu, self.fs)
-		resp = self.criu.send_req(req)
+			self.pid, self.htype, self.img, self.criu_connection, self.fs)
+		resp = self.criu_connection.send_req(req)
 		while True:
-			if resp.type != cr_rpc.NOTIFY:
+			if resp.type != pycriu.rpc.NOTIFY:
 				raise Exception("Dump failed")
 
 			if resp.notify.script == "post-dump":
@@ -167,10 +171,10 @@ class phaul_iter_worker:
 				self.htype.net_unlock()
 
 			logging.info("\t\tNotify (%s)", resp.notify.script)
-			resp = self.criu.ack_notify()
+			resp = self.criu_connection.ack_notify()
 
 		logging.info("Dump complete")
-		self.th.end_iter()
+		self.target_host.end_iter()
 
 		#
 		# Dump is complete -- go to target node,
@@ -180,10 +184,11 @@ class phaul_iter_worker:
 
 		logging.info("Final FS and images sync")
 		self.fs.stop_migration()
-		self.img.sync_imgs_to_target(self.th, self.htype, self.data_sk)
+		self.img.sync_imgs_to_target(self.target_host, self.htype,
+			self.data_socket)
 
 		logging.info("Asking target host to restore")
-		self.th.restore_from_images()
+		self.target_host.restore_from_images()
 
 		#
 		# Ack the notify after restore -- CRIU would
@@ -191,14 +196,14 @@ class phaul_iter_worker:
 		# DUMP/success message
 		#
 
-		resp = self.criu.ack_notify()
+		resp = self.criu_connection.ack_notify()
 		if not resp.success:
 			raise Exception("Dump screwed up")
 
 		self.htype.umount()
 
-		stats = criu_api.criu_get_dstats(self.img)
-		self._mstat.iteration(stats)
-		self._mstat.stop(self)
+		dstats = criu_api.criu_get_dstats(self.img)
+		migration_stats.iteration(dstats)
+		migration_stats.stop(self)
 		self.img.close()
-		self.criu.close()
+		self.criu_connection.close()
diff --git a/phaul/p_haul_service.py b/phaul/p_haul_service.py
index f2cadb9..fd3c611 100644
--- a/phaul/p_haul_service.py
+++ b/phaul/p_haul_service.py
@@ -4,7 +4,6 @@
 
 import logging
 import xem_rpc
-import pycriu.rpc as cr_rpc
 import images
 import criu_api
 import criu_req
@@ -15,18 +14,18 @@ class phaul_service:
 		logging.info("Connected")
 		self.dump_iter = 0
 		self.restored = False
-		self.criu = None
-		self.data_sk = None
+		self.criu_connection = None
+		self.data_socket = None
 		self.img = None
 		self.htype = None
 
 	def on_disconnect(self):
 		logging.info("Disconnected")
-		if self.criu:
-			self.criu.close()
+		if self.criu_connection:
+			self.criu_connection.close()
 
-		if self.data_sk:
-			self.data_sk.close()
+		if self.data_socket:
+			self.data_socket.close()
 
 		if self.htype and not self.restored:
 			self.htype.umount()
@@ -38,17 +37,17 @@ class phaul_service:
 			self.img.close()
 
 	def on_socket_open(self, sk, uname):
-		self.data_sk = sk
+		self.data_socket = sk
 		logging.info("Data socket (%s) accepted", uname)
 
 	def rpc_setup(self, htype_id):
 		logging.info("Setting up service side %s", htype_id)
 		self.img = images.phaul_images("rst")
-		self.criu = criu_api.criu_conn(self.data_sk)
+		self.criu_connection = criu_api.criu_conn(self.data_socket)
 		self.htype = p_haul_type.get_dst(htype_id)
 
 	def rpc_set_options(self, opts):
-		self.criu.verbose(opts["verbose"])
+		self.criu_connection.verbose(opts["verbose"])
 		self.img.set_options(opts)
 		self.htype.set_options(opts)
 
@@ -56,8 +55,9 @@ class phaul_service:
 		logging.info("Starting page server for iter %d", self.dump_iter)
 
 		logging.info("\tSending criu rpc req")
-		req = criu_req.make_page_server_req(self.htype, self.img, self.criu)
-		resp = self.criu.send_req(req)
+		req = criu_req.make_page_server_req(self.htype, self.img,
+			self.criu_connection)
+		resp = self.criu_connection.send_req(req)
 		if not resp.success:
 			raise Exception("Failed to start page server")
 
@@ -72,7 +72,7 @@ class phaul_service:
 		pass
 
 	def rpc_start_accept_images(self, dir_id):
-		self.img.start_accept_images(dir_id, self.data_sk)
+		self.img.start_accept_images(dir_id, self.data_socket)
 
 	def rpc_stop_accept_images(self):
 		self.img.stop_accept_images()
@@ -80,14 +80,14 @@ class phaul_service:
 	def rpc_check_cpuinfo(self):
 		logging.info("Checking cpuinfo")
 		req = criu_req.make_cpuinfo_check_req(self.htype, self.img)
-		resp = self.criu.send_req(req)
+		resp = self.criu_connection.send_req(req)
 		logging.info("\t`- %s", resp.success)
 		return resp.success
 
 	def rpc_restore_from_images(self):
 		logging.info("Restoring from images")
 		self.htype.put_meta_images(self.img.image_dir())
-		self.htype.final_restore(self.img, self.criu)
+		self.htype.final_restore(self.img, self.criu_connection)
 		logging.info("Restore succeeded")
 		self.restored = True
 
-- 
1.7.1



More information about the CRIU mailing list