From: David Scott Subject: Re: [PATCH 1/8] oxenstored: add a poll-based select mechanism Date: Tue, 16 Sep 2014 10:01:23 +0100 Message-ID: References: <1410820760-7994-1-git-send-email-dev@zheng.li> <1410820760-7994-2-git-send-email-dev@zheng.li> Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: quoted-printable Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XTode-0003UX-6k for xen-devel@lists.xenproject.org; Tue, 16 Sep 2014 09:01:30 +0000 In-Reply-To: <1410820760-7994-2-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: Zheng Li Cc: Ian Jackson , Joe Jin , "Luis R. Rodriguez" , Luonengjun , Fanhenglong , "Liuqiming (John)" , xen-devel@lists.xenproject.org List-Id: xen-devel@lists.xenproject.org Hi, Thanks for sending these. On 15 Sep 2014, at 23:39, Zheng Li wrote: > Currently, oxenstored uses Unix.select underneath, so it doesn't work pro= perly > if given a FD number >=3D 1024. This is a scalability bottleneck for hosts > running large number of VMs. > = > To remove this limitation, we implemented a poll-based mechanism but with= the > same type signature as the Unix.select currently in use. So these two fun= ctions > can be interchangeable at any stage. > = > Signed-off-by: Zheng Li > --- > tools/ocaml/xenstored/Makefile | 9 +++-- > tools/ocaml/xenstored/select.ml | 49 ++++++++++++++++++++++++++ > tools/ocaml/xenstored/select.mli | 20 +++++++++++ > tools/ocaml/xenstored/select_stubs.c | 68 +++++++++++++++++++++++++++++++= +++++ > tools/ocaml/xenstored/xenstored.ml | 2 +- > 5 files changed, 144 insertions(+), 4 deletions(-) > create mode 100644 tools/ocaml/xenstored/select.ml > create mode 100644 tools/ocaml/xenstored/select.mli > create mode 100644 tools/ocaml/xenstored/select_stubs.c > = > diff --git a/tools/ocaml/xenstored/Makefile b/tools/ocaml/xenstored/Makef= ile > index 068e04a..47d5303 100644 > --- a/tools/ocaml/xenstored/Makefile > +++ b/tools/ocaml/xenstored/Makefile > @@ -15,10 +15,12 @@ OCAMLINCLUDE +=3D \ > -I $(OCAML_TOPLEVEL)/libs/xc \ > -I $(OCAML_TOPLEVEL)/libs/eventchn > = > -LIBS =3D syslog.cma syslog.cmxa > +LIBS =3D syslog.cma syslog.cmxa select.cma select.cmxa > syslog_OBJS =3D syslog > syslog_C_OBJS =3D syslog_stubs > -OCAML_LIBRARY =3D syslog > +select_OBJS =3D select > +select_C_OBJS =3D select_stubs > +OCAML_LIBRARY =3D syslog select > = > LIBS +=3D systemd.cma systemd.cmxa > systemd_OBJS =3D systemd > @@ -46,12 +48,13 @@ OBJS =3D define \ > process \ > xenstored > = > -INTF =3D symbol.cmi trie.cmi syslog.cmi systemd.cmi > +INTF =3D symbol.cmi trie.cmi syslog.cmi systemd.cmi select.cmi > = > XENSTOREDLIBS =3D \ > unix.cmxa \ > -ccopt -L -ccopt . syslog.cmxa \ > -ccopt -L -ccopt . systemd.cmxa \ > + -ccopt -L -ccopt . select.cmxa \ > -ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/mmap $(OCAML_TOPLEVEL)/libs/mmap= /xenmmap.cmxa \ > -ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/eventchn $(OCAML_TOPLEVEL)/libs/= eventchn/xeneventchn.cmxa \ > -ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xc $(OCAML_TOPLEVEL)/libs/xc/xen= ctrl.cmxa \ > diff --git a/tools/ocaml/xenstored/select.ml b/tools/ocaml/xenstored/sele= ct.ml > new file mode 100644 > index 0000000..2c18c70 > --- /dev/null > +++ b/tools/ocaml/xenstored/select.ml > @@ -0,0 +1,49 @@ > +(* > + * Copyright (C) 2014 Zheng Li > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU Lesser General Public License as publis= hed > + * by the Free Software Foundation; version 2.1 only. with the special > + * exception on linking described in file LICENSE. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU Lesser General Public License for more details. > + *) > + > + > +type event =3D { > + mutable read: bool; > + mutable write: bool; > + mutable except: bool; > +} If I understand correctly this is a representation of the POLL{IN,OUT,PRI} = bitmap in the C stubs =97 might be worth a short comment (to help me rememb= er in future) > + > +external select_on_poll: (Unix.file_descr * event) array -> int -> int = =3D "stub_select_on_poll" > + > +let init_event () =3D {read =3D false; write =3D false; except =3D false} > + > +let select in_fds out_fds exc_fds timeout =3D > + let h =3D Hashtbl.create 57 in > + let add_event event_set fd =3D > + let e =3D > + try Hashtbl.find h fd > + with Not_found -> > + let e =3D init_event () in > + Hashtbl.add h fd e; e in > + event_set e in > + List.iter (add_event (fun x -> x.read <- true)) in_fds; > + List.iter (add_event (fun x -> x.write <- true)) out_fds; > + List.iter (add_event (fun x -> x.except <- true)) exc_fds; > + let a =3D Array.make (Hashtbl.length h) (Unix.stdin, init_event ()) in Might be worth a comment to say that Unix.stdin is a dummy value, since all= entries in the array are overwritten below. > + let i =3D ref (-1) in > + Hashtbl.iter (fun fd event -> incr i; Array.set a !i (fd, event)) h; > + let n =3D select_on_poll a (int_of_float (timeout *. 1000.)) in > + let r =3D [], [], [] in > + if n =3D 0 then r else > + Array.fold_right > + (fun (fd, event) (r, w, x) -> > + (if event.read then fd :: r else r), > + (if event.write then fd :: w else w), > + (if event.except then fd :: x else x)) > + a r > diff --git a/tools/ocaml/xenstored/select.mli b/tools/ocaml/xenstored/sel= ect.mli > new file mode 100644 > index 0000000..1253d4e > --- /dev/null > +++ b/tools/ocaml/xenstored/select.mli > @@ -0,0 +1,20 @@ > +(* > + * Copyright (C) 2014 Zheng Li > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU Lesser General Public License as publis= hed > + * by the Free Software Foundation; version 2.1 only. with the special > + * exception on linking described in file LICENSE. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU Lesser General Public License for more details. > + *) > + > + > +(** Same interface and semantics as [Unix.select] but with an extra alte= rnative > + implementation based on poll. *) > +val select: > + Unix.file_descr list -> Unix.file_descr list -> Unix.file_descr list ->= float > + -> Unix.file_descr list * Unix.file_descr list * Unix.file_descr list > diff --git a/tools/ocaml/xenstored/select_stubs.c b/tools/ocaml/xenstored= /select_stubs.c > new file mode 100644 > index 0000000..a50f417 > --- /dev/null > +++ b/tools/ocaml/xenstored/select_stubs.c > @@ -0,0 +1,68 @@ > +/* > + * Copyright (C) 2014 Zheng Li > + * > + * This program is free software; you can redistribute it and/or modify > + * it under the terms of the GNU Lesser General Public License as publis= hed > + * by the Free Software Foundation; version 2.1 only. with the special > + * exception on linking described in file LICENSE. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU Lesser General Public License for more details. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +CAMLprim value stub_select_on_poll(value fd_events, value timeo) { > + > + CAMLparam2(fd_events, timeo); > + CAMLlocal1(events); > + int i, rc, c_len =3D Wosize_val(fd_events), c_timeo =3D Int_val(timeo); > + struct pollfd c_fds[c_len]; = > + > + > + for (i =3D 0; i < c_len; i++) { > + > + events =3D Field(Field(fd_events, i), 1); > + > + c_fds[i].fd =3D Int_val(Field(Field(fd_events, i), 0)); > + c_fds[i].events =3D c_fds[i].revents =3D 0; > + c_fds[i].events |=3D Bool_val(Field(events, 0)) ? POLLIN : 0; > + c_fds[i].events |=3D Bool_val(Field(events, 1)) ? POLLOUT: 0; > + c_fds[i].events |=3D Bool_val(Field(events, 2)) ? POLLPRI: 0; > + > + }; > + > + caml_enter_blocking_section(); > + rc =3D poll(c_fds, c_len, c_timeo); > + caml_leave_blocking_section(); > + > + if (rc < 0) uerror("select", Nothing); Perhaps the error string should be =93poll=94 so it would be obvious from t= he Unix_error which implementation failed? > + > + if (rc > 0) { > + > + for (i =3D 0; i < c_len; i++) { > + > + events =3D Field(Field(fd_events, i), 1); > + > + if (c_fds[i].revents & POLLNVAL) unix_error(EBADF, "select", Nothing); > + Field(events, 0) =3D Val_bool(c_fds[i].events | POLLIN && c_fds[i].r= events & (POLLIN |POLLHUP|POLLERR)); > + Field(events, 1) =3D Val_bool(c_fds[i].events | POLLOUT && c_fds[i].r= events & (POLLOUT|POLLHUP|POLLERR)); > + Field(events, 2) =3D Val_bool(c_fds[i].revents & POLLPRI); > + = > + } > + > + } > + > + CAMLreturn(Val_int(rc)); > +} > diff --git a/tools/ocaml/xenstored/xenstored.ml b/tools/ocaml/xenstored/x= enstored.ml > index 1c02f2f..bfa488f 100644 > --- a/tools/ocaml/xenstored/xenstored.ml > +++ b/tools/ocaml/xenstored/xenstored.ml > @@ -368,7 +368,7 @@ let _ =3D > let timeout =3D if List.length mw > 0 then 0. else -1. in > let rset, wset, _ =3D > try > - Unix.select (spec_fds @ inset) outset [] timeout > + Select.select (spec_fds @ inset) outset [] timeout > with Unix.Unix_error(Unix.EINTR, _, _) -> > [], [], [] in > let sfds, cfds =3D > -- = > 2.1.0 > = > = > _______________________________________________ > Xen-devel mailing list > Xen-devel@lists.xen.org > http://lists.xen.org/xen-devel