From 45cc85eea484d9c7d88a33ee9a6deb88c0a4752d Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Thu, 13 Sep 2012 14:45:06 +0400 Subject: [PATCH] Speed up service-fd retrieval 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 Signed-off-by: Pavel Emelyanov --- 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 901a6c72a..1a5eb6dae 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 ad36c8c9b..81eca5b0d 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 d45311b52..0e047a19c 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)