[CRIU] [PATCH 3/5] deduplication: look up for old pages in previous snapshot

Tikhomirov Pavel snorcht at gmail.com
Fri Oct 11 12:57:33 PDT 2013


old snapshot from "parent" symlink, and pids from pagemap-PID.img files

Signed-off-by: Tikhomirov Pavel <snorcht at gmail.com>
---
 cr-dedup.c          |  130 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 include/page-read.h |    1 +
 page-read.c         |   22 +++++++--
 3 files changed, 149 insertions(+), 4 deletions(-)
 mode change 100644 => 100755 cr-dedup.c
 mode change 100644 => 100755 include/page-read.h
 mode change 100644 => 100755 page-read.c

diff --git a/cr-dedup.c b/cr-dedup.c
old mode 100644
new mode 100755
index 4f959dd..a51d90d
--- a/cr-dedup.c
+++ b/cr-dedup.c
@@ -1,9 +1,137 @@
 #include <unistd.h>
 
 #include "crtools.h"
+#include "page-read.h"
+#include "restorer.h"
 #include "cr-dedup.h"
 
-int cr_dedup(void)
+int _cr_dedup(DIR * dirp, int pid);
+
+int cr_dedup(void) 
 {
+	int ret;
+	int pid;
+	DIR * dirp;
+	struct dirent *ent;
+	
+	dirp = opendir("parent");
+	if (dirp == NULL) {
+		ret = -1;
+		return ret;
+	}
+
+	while (1) {
+		ent = readdir(dirp);
+		if (ent == NULL)
+			break;
+
+		ret = sscanf(ent->d_name, "pagemap-%d.img", &pid);
+		if (ret == 1) {
+			pr_info("pid=%d\n", pid);
+			ret = _cr_dedup(dirp, pid);
+			if (ret < 0)
+				return ret;
+		}
+	}
+
+	return 0;
+}
+
+int _cr_dedup(DIR * dirp, int pid)
+{
+	int ret, predump_fd = -1;
+	struct page_read pr, pr2;
+	unsigned long off;
+	struct iovec iov, iov2;	
+	
+	ret = open_page_read(pid, &pr);
+	if (ret)
+		return -1;
+	
+	predump_fd = dirfd(dirp);
+	if (predump_fd == -1) {
+		ret = -1;
+		goto exit;
+	}
+
+	ret = open_page_rw_dedup(predump_fd, pid, &pr2);
+	if (ret)
+		goto exit;
+
+	ret = pr.get_pagemap(&pr, &iov);
+	if (ret <= 0)
+		goto exit;
+
+	ret = pr2.get_pagemap(&pr2, &iov2);
+	if (ret <= 0)
+		goto exit;
+
+	off = 0;
+	while (1) {
+		unsigned long i_off;
+		long i_len;
+
+		i_off = (unsigned long)max(iov.iov_base, iov2.iov_base) - (unsigned long)iov2.iov_base;
+		i_len = (long)(min(iov.iov_base + iov.iov_len, iov2.iov_base + iov2.iov_len) - max(iov.iov_base, iov2.iov_base));
+		if (i_len <= 0) {
+			pr_info("No pagemaps intersection\n");
+
+			if (iov.iov_base < iov2.iov_base) {
+				ret = pr.get_pagemap(&pr, &iov);
+				if (ret <= 0)
+					break;
+			} else {
+				if (!pr2.pe->in_parent)
+					off += pr2.pe->nr_pages;
+				ret = pr2.get_pagemap(&pr2, &iov2);
+				if (ret <= 0)
+					break;
+			}
+			continue;
+		}
+
+		if (iov.iov_base + iov.iov_len == iov2.iov_base + iov2.iov_len) {
+			ret = pr.get_pagemap(&pr, &iov);
+                        if (ret <= 0)
+                                break;
+
+			if (!pr2.pe->in_parent)
+                                off += pr2.pe->nr_pages;
+                        ret = pr2.get_pagemap(&pr2, &iov2);
+                        if (ret <= 0)
+                                break;
+			continue;
+		}
+
+		if (iov.iov_base + iov.iov_len < iov2.iov_base + iov2.iov_len) {
+			ret = pr.get_pagemap(&pr, &iov);
+			if (ret <= 0)
+				break;
+		} else {
+			if (!pr2.pe->in_parent)
+				off += pr2.pe->nr_pages;
+			ret = pr2.get_pagemap(&pr2, &iov2);
+			if (ret <= 0)
+				break;
+		}
+	}
+exit:
+	pr_info("Dedup test END\n");
+
+	pr.close(&pr);
+	pr2.close(&pr2);
+
+	if (dirp) {	
+		ret = closedir(dirp);
+		if (ret == -1)
+			return ret;
+	}
+
+	if (predump_fd >= 0) {
+		ret = close(predump_fd);
+		if (ret == -1)
+			return ret;
+	}
+
 	return 0;
 }
diff --git a/include/page-read.h b/include/page-read.h
old mode 100644
new mode 100755
index b19ea69..47c1562
--- a/include/page-read.h
+++ b/include/page-read.h
@@ -66,4 +66,5 @@ struct page_read {
 };
 
 int open_page_read(int pid, struct page_read *);
+int open_page_rw_dedup(int dfd, int pid, struct page_read *);
 #endif
diff --git a/page-read.c b/page-read.c
old mode 100644
new mode 100755
index 78b1303..52c9b12
--- a/page-read.c
+++ b/page-read.c
@@ -181,13 +181,16 @@ err_cl:
 	return -1;
 }
 
-static int open_page_read_at(int dfd, int pid, struct page_read *pr)
+static int open_page_rw_at(int dfd, int pid, struct page_read *pr, int rw)
 {
 	pr->pe = NULL;
 
 	pr->fd = open_image_at(dfd, CR_FD_PAGEMAP, O_RSTR, (long)pid);
 	if (pr->fd < 0) {
-		pr->fd_pg = open_image_at(dfd, CR_FD_PAGES_OLD, O_RSTR, pid);
+		if (rw)
+			pr->fd_pg = open_image_at(dfd, CR_FD_PAGES_OLD, O_RDWR, pid);
+		else
+			pr->fd_pg = open_image_at(dfd, CR_FD_PAGES_OLD, O_RSTR, pid);
 		if (pr->fd_pg < 0)
 			return -1;
 
@@ -203,7 +206,10 @@ static int open_page_read_at(int dfd, int pid, struct page_read *pr)
 			return -1;
 		}
 
-		pr->fd_pg = open_pages_image_at(dfd, O_RSTR, pr->fd);
+		if (rw)
+			pr->fd_pg = open_pages_image_at(dfd, O_RDWR, pr->fd);
+		else
+			pr->fd_pg = open_pages_image_at(dfd, O_RSTR, pr->fd);
 		if (pr->fd_pg < 0) {
 			close_page_read(pr);
 			return -1;
@@ -223,7 +229,17 @@ static int open_page_read_at(int dfd, int pid, struct page_read *pr)
 	return 0;
 }
 
+static int open_page_read_at(int dfd, int pid, struct page_read *pr)
+{
+	return open_page_rw_at(dfd, pid, pr, 0);
+}
+
 int open_page_read(int pid, struct page_read *pr)
 {
 	return open_page_read_at(get_service_fd(IMG_FD_OFF), pid, pr);
 }
+
+int open_page_rw_dedup(int dfd, int pid, struct page_read *pr)
+{
+	return open_page_rw_at(dfd, pid, pr, 1);
+}
-- 
1.7.9.5



More information about the CRIU mailing list