[CRIU] [PATCH] service-fd: Rework it to use in hight performance code

Cyrill Gorcunov gorcunov at openvz.org
Fri Sep 7 06:06:14 EDT 2012


We will need service fd testing in our file engine,
thus we need to escape calling getrlimit which degradates
performance.

For this sake we simly cache rlim value we need and
reuse it where needed.

Note the is_service_fd currently takes @type of @fd,
I'll need this feature for tty restore code.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 crtools.c         |    3 +++
 include/crtools.h |   13 ++++++++++---
 include/util.h    |    4 ++++
 util.c            |   23 +++++++++++++++++++++--
 4 files changed, 38 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 8ad8ce8..efbe912 100644
--- a/include/crtools.h
+++ b/include/crtools.h
@@ -99,14 +99,21 @@ struct cr_options {
 extern struct cr_options opts;
 
 enum {
-	LOG_FD_OFF = 1,
+	/*
+	 * Never change it to any other
+	 * value than zero, otherwise fix
+	 * up code in service fd handling.
+	 */
+	SERVICE_FD_OFF_MIN,
+
+	LOG_FD_OFF,
 	LOG_DIR_FD_OFF,
 	IMG_FD_OFF,
 	SELF_EXE_FD_OFF,
 	PROC_FD_OFF,
-};
 
-int get_service_fd(int type);
+	SERVICE_FD_OFF_MAX
+};
 
 /* file descriptors template */
 struct cr_fd_desc_tmpl {
diff --git a/include/util.h b/include/util.h
index 1daf451..d0a0bc8 100644
--- a/include/util.h
+++ b/include/util.h
@@ -321,4 +321,8 @@ static inline int read_img_str(int fd, char **pstr, int size)
 extern void *shmalloc(size_t bytes);
 extern void shfree_last(void *ptr);
 
+extern int init_service_fd(void);
+extern int get_service_fd(int type);
+extern int is_service_fd(int fd, int type);
+
 #endif /* UTIL_H_ */
diff --git a/util.c b/util.c
index 1fad14c..ec56ff5 100644
--- a/util.c
+++ b/util.c
@@ -224,7 +224,10 @@ 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;
+static int service_fd_rlim_min;
+
+int init_service_fd(void)
 {
 	struct rlimit rlimit;
 
@@ -238,7 +241,23 @@ int get_service_fd(int type)
 		return -1;
 	}
 
-	return rlimit.rlim_cur - type;
+	service_fd_rlim_cur = (int)rlimit.rlim_cur;
+	service_fd_rlim_min = service_fd_rlim_cur - SERVICE_FD_OFF_MAX;
+
+	return 0;
+}
+
+int get_service_fd(int type)
+{
+	return service_fd_rlim_cur - type;
+}
+
+int is_service_fd(int fd, int type)
+{
+	if (type > SERVICE_FD_OFF_MIN)
+		return fd == get_service_fd(type);
+
+	return fd > service_fd_rlim_min;
 }
 
 int copy_file(int fd_in, int fd_out, size_t bytes)
-- 
1.7.7.6



More information about the CRIU mailing list