[CRIU] [PATCH 1/2] Speed up service-fd retrieval

Cyrill Gorcunov gorcunov at openvz.org
Wed Sep 12 12:14:57 EDT 2012


We're using get_service_fd in file engine,
better to make it fast. This patch caches
the limits system provides us, instead of
calling getrlimit() every time.

This patch introduces is_service_fd helper
which will be used instead of get_service_fd
where it make sense.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 crtools.c         |    3 +++
 include/crtools.h |   12 +++++++++---
 util.c            |   20 ++++++++++++++++++--
 3 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/crtools.c b/crtools.c
index 901a6c7..1a5eb6d 100644
--- a/crtools.c
+++ b/crtools.c
@@ -73,6 +73,9 @@ int main(int argc, char *argv[])
 	opts.final_state = TASK_DEAD;
 	INIT_LIST_HEAD(&opts.veth_pairs);
 
+	if (init_service_fd())
+		return -1;
+
 	while (1) {
 		static struct option long_opts[] = {
 			{ "tree", required_argument, 0, 't' },
diff --git a/include/crtools.h b/include/crtools.h
index ad36c8c..81eca5b 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -100,16 +100,22 @@ struct cr_options {
 
 extern struct cr_options opts;
 
-enum {
-	LOG_FD_OFF = 1,
+enum sfd_type {
+	SERVICE_FD_MIN,
+
+	LOG_FD_OFF,
 	LOG_DIR_FD_OFF,
 	IMG_FD_OFF,
 	SELF_EXE_FD_OFF,
 	PROC_FD_OFF,
 	CTL_TTY_OFF,
+
+	SERVICE_FD_MAX
 };
 
-int get_service_fd(int type);
+extern int init_service_fd(void);
+extern int get_service_fd(enum sfd_type type);
+extern bool is_service_fd(int fd, enum sfd_type type);
 
 /* file descriptors template */
 struct cr_fd_desc_tmpl {
diff --git a/util.c b/util.c
index d45311b..0e047a1 100644
--- a/util.c
+++ b/util.c
@@ -259,7 +259,9 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...)
 	return openat(dirfd, path, flags);
 }
 
-int get_service_fd(int type)
+static int service_fd_rlim_cur;
+
+int init_service_fd(void)
 {
 	struct rlimit rlimit;
 
@@ -273,7 +275,21 @@ int get_service_fd(int type)
 		return -1;
 	}
 
-	return rlimit.rlim_cur - type;
+	service_fd_rlim_cur = (int)rlimit.rlim_cur;
+	BUG_ON(service_fd_rlim_cur < SERVICE_FD_MAX);
+
+	return 0;
+}
+
+int get_service_fd(enum sfd_type type)
+{
+	BUG_ON((int)type <= SERVICE_FD_MIN || (int)type >= SERVICE_FD_MAX);
+	return service_fd_rlim_cur - type;
+}
+
+bool is_service_fd(int fd, enum sfd_type type)
+{
+	return fd == get_service_fd(type);
 }
 
 int copy_file(int fd_in, int fd_out, size_t bytes)
-- 
1.7.7.6



More information about the CRIU mailing list