From ea9cb03ca398ad232fc524bcc2974ffd91f3e3ec Mon Sep 17 00:00:00 2001 From: Steven Woods Date: Fri, 13 Jan 2023 11:08:10 +0000 Subject: [PATCH] CP-32622: Replace select with polly in child --- ocaml/forkexecd/src/child.ml | 52 +++++++++++++++++++++++------------- ocaml/forkexecd/src/dune | 1 + 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/ocaml/forkexecd/src/child.ml b/ocaml/forkexecd/src/child.ml index 8c07413a720..5a7f122050c 100644 --- a/ocaml/forkexecd/src/child.ml +++ b/ocaml/forkexecd/src/child.ml @@ -59,28 +59,42 @@ let handle_comms_sock comms_sock state = state let handle_comms_no_fd_sock2 comms_sock fd_sock state = - debug "Selecting in handle_comms_no_fd_sock2" ; - let ready, _, _ = Unix.select [comms_sock; fd_sock] [] [] (-1.0) in - debug "Done" ; - if List.mem fd_sock ready then ( - debug "fd sock" ; - let fd_sock2, _ = Unix.accept fd_sock in - {state with fd_sock2= Some fd_sock2} - ) else ( - debug "comms sock" ; - handle_comms_sock comms_sock state + debug "Using epoll in handle_comms_no_fd_sock2" ; + let epoll = Polly.create () in + List.iter + (fun fd -> Polly.add epoll fd Polly.Events.inp) + [comms_sock; fd_sock] ; + (* Although there are two fds, we set max_fds to 1 here as we only want this + function to trigger once so that we get one return value *) + Polly.wait_fold epoll 1 (-1) state (fun _ fd _ _ -> + debug "Done" ; + if fd_sock = fd then ( + debug "fd sock" ; + let fd_sock2, _ = Unix.accept fd_sock in + {state with fd_sock2= Some fd_sock2} + ) else ( + debug "comms sock" ; + handle_comms_sock comms_sock state + ) ) let handle_comms_with_fd_sock2 comms_sock _fd_sock fd_sock2 state = - debug "Selecting in handle_comms_with_fd_sock2" ; - let ready, _, _ = Unix.select [comms_sock; fd_sock2] [] [] (-1.0) in - debug "Done" ; - if List.mem fd_sock2 ready then ( - debug "fd sock2" ; - handle_fd_sock fd_sock2 state - ) else ( - debug "comms sock" ; - handle_comms_sock comms_sock state + debug "Using epoll in handle_comms_with_fd_sock2" ; + let epoll = Polly.create () in + List.iter + (fun fd -> Polly.add epoll fd Polly.Events.inp) + [comms_sock; fd_sock2] ; + (* Although there are two fds, we set max_fds to 1 here as we only want this + function to trigger once so that we get one return value *) + Polly.wait_fold epoll 1 (-1) state (fun _ fd _ _ -> + debug "Done" ; + if fd_sock2 = fd then ( + debug "fd sock2" ; + handle_fd_sock fd_sock2 state + ) else ( + debug "comms sock" ; + handle_comms_sock comms_sock state + ) ) let handle_comms comms_sock fd_sock state = diff --git a/ocaml/forkexecd/src/dune b/ocaml/forkexecd/src/dune index 56d6b3c974e..e94f761651f 100644 --- a/ocaml/forkexecd/src/dune +++ b/ocaml/forkexecd/src/dune @@ -5,6 +5,7 @@ astring fd-send-recv forkexec + polly systemd uuid xapi-log