From mboxrd@z Thu Jan 1 00:00:00 1970 From: Zheng Li Subject: [PATCH 2/8] oxenstored: add facilities to raise the max open fds uplimit Date: Mon, 15 Sep 2014 23:39:14 +0100 Message-ID: <1410820760-7994-3-git-send-email-dev@zheng.li> References: <1410820760-7994-1-git-send-email-dev@zheng.li> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XTexf-00072a-HA for xen-devel@lists.xenproject.org; Mon, 15 Sep 2014 22:41:31 +0000 In-Reply-To: <1410820760-7994-1-git-send-email-dev@zheng.li> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xenproject.org Cc: Dave Scott , Joe Jin , "Luis R. Rodriguez" , Luonengjun , Zheng Li , Fanhenglong , "Liuqiming (John)" , Ian Jackson List-Id: xen-devel@lists.xenproject.org To go beyond 1024 fds, we also need to raise the process limitation on max open fds (usually defaults to 1024). We need to know the system level max open fds so that we won't go above that. Simply setting the limit to RLIM_INFINITY doesn't work on Linux 3.x (EPERM), a patch on this went into the 2.x branch but not 3.x for some reason. Also, getting the system level nr_open is not very straightfoward on 3.x, which is mentioned in the comment inline. Signed-off-by: Zheng Li --- tools/ocaml/xenstored/select.ml | 15 +++++++++++++++ tools/ocaml/xenstored/select_stubs.c | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tools/ocaml/xenstored/select.ml b/tools/ocaml/xenstored/select.ml index 2c18c70..c19df41 100644 --- a/tools/ocaml/xenstored/select.ml +++ b/tools/ocaml/xenstored/select.ml @@ -20,6 +20,21 @@ type event = { } external select_on_poll: (Unix.file_descr * event) array -> int -> int = "stub_select_on_poll" +external set_fd_limit: int -> unit = "stub_set_fd_limit" + +(* The rlim_max given to setrlimit must not beyond system level nr_open, which + was defined as NR_OPEN in fs.h in Linux 2.x and then sysctl_nr_open variable + since 3.x. Unfortunately sysctl_nr_open was not exposed in either head files + or sysctl param vector. This function workarounds this by trying to read + from /proc directly and default to the old NR_OPEN value if that fails. + Fortunately, we'll need to call this at most once. +*) +let get_sys_fs_nr_open () = + try + let ch = open_in "/proc/sys/fs/nr_open" in + let v = int_of_string (input_line ch) in + close_in_noerr ch; v + with _ -> 1024 * 1024 let init_event () = {read = false; write = false; except = false} diff --git a/tools/ocaml/xenstored/select_stubs.c b/tools/ocaml/xenstored/select_stubs.c index a50f417..13efbac 100644 --- a/tools/ocaml/xenstored/select_stubs.c +++ b/tools/ocaml/xenstored/select_stubs.c @@ -66,3 +66,15 @@ CAMLprim value stub_select_on_poll(value fd_events, value timeo) { CAMLreturn(Val_int(rc)); } + + +CAMLprim value stub_set_fd_limit(value limit) { + + CAMLparam1(limit); + struct rlimit rl; + + rl.rlim_cur = rl.rlim_max = Int_val(limit); + if (setrlimit(RLIMIT_NOFILE, &rl) != 0) uerror("setrlimit", Nothing); + CAMLreturn(Val_unit); + +} -- 2.1.0