From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 2077020083 for ; Thu, 31 Dec 2020 13:51:58 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 33/36] lei: avoid Spawn package when starting daemon Date: Thu, 31 Dec 2020 13:51:51 +0000 Message-Id: <20201231135154.6070-34-e@80x24.org> In-Reply-To: <20201231135154.6070-1-e@80x24.org> References: <20201231135154.6070-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Spawn was designed to speed up process spawning inside long-lived daemons with largish memory usage. It does not help for short-lived scripts which only exist to start and connect to a daemon. This change actually speeds up initial lei startup from ~190ms to ~140ms(!). Normal usage once the daemon is running is unaffected, at <20ms for help text. While we're in the area, simplify Cwd error message generation, too. --- lib/PublicInbox/LEI.pm | 10 +++++----- script/lei | 17 ++++++----------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 03302f8a..b84e24ef 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -675,7 +675,7 @@ sub lazy_start { require IO::FDPass; require PublicInbox::Listener; require PublicInbox::EOFpipe; - (-p STDOUT && -p STDERR) or die "E: stdout+stderr must be pipes\n"; + (-p STDOUT) or die "E: stdout must be a pipe\n"; open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!"; POSIX::setsid() > 0 or die "setsid: $!"; my $pid = fork // die "fork: $!"; @@ -740,10 +740,9 @@ sub lazy_start { $n; # true: continue, false: stop }); - # STDIN was redirected to /dev/null above, closing STDOUT and - # STDERR will cause the calling `lei' client process to finish - # reading <$daemon> pipe. - open STDOUT, '>&STDIN' or die "redirect stdout failed: $!"; + # STDIN was redirected to /dev/null above, closing STDERR and + # STDOUT will cause the calling `lei' client process to finish + # reading the <$daemon> pipe. openlog($path, 'pid', 'user'); local $SIG{__WARN__} = sub { syslog('warning', "@_") }; my $owner_pid = $$; @@ -751,6 +750,7 @@ sub lazy_start { syslog('crit', "$@") if $@ && $$ == $owner_pid; }); open STDERR, '>&STDIN' or die "redirect stderr failed: $!"; + open STDOUT, '>&STDIN' or die "redirect stdout failed: $!"; # $daemon pipe to `lei' closed, main loop begins: PublicInbox::DS->EventLoop; @$on_destroy = (); # cancel on_destroy if we get here diff --git a/script/lei b/script/lei index ceaf1e00..0457adfd 100755 --- a/script/lei +++ b/script/lei @@ -21,18 +21,13 @@ if (my ($sock, $pwd) = eval { my $addr = pack_sockaddr_un($path); socket(my $sock, AF_UNIX, SOCK_STREAM, 0) or die "socket: $!"; unless (connect($sock, $addr)) { # start the daemon if not started - my $cmd = [ $^X, qw[-MPublicInbox::LEI + local $ENV{PERL5LIB} = join(':', @INC); + open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI -E PublicInbox::LEI::lazy_start(@ARGV)], - $path, $! + 0 ]; - my $env = { PERL5LIB => join(':', @INC) }; - pipe(my ($daemon, $w)) or die "pipe: $!"; - my $opt = { 1 => $w, 2 => $w }; - require PublicInbox::Spawn; - my $pid = PublicInbox::Spawn::spawn($cmd, $env, $opt); - $opt = $w = undef; + $path, $! + 0) or die "popen: $!"; while (<$daemon>) { warn $_ } # EOF when STDERR is redirected - waitpid($pid, 0) or warn <<""; -lei-daemon could not start, PID:$pid exited with \$?=$? + close($daemon) or warn <<""; +lei-daemon could not start, exited with \$?=$? # try connecting again anyways, unlink+bind may be racy unless (connect($sock, $addr)) { @@ -43,8 +38,8 @@ Falling back to (slow) one-shot mode } } require Cwd; - my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=".($ENV{PWD}//'').": $!"; my $pwd = $ENV{PWD} // ''; + my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=$pwd): $!"; if ($pwd ne $cwd) { # prefer ENV{PWD} if it's a symlink to real cwd my @st_cwd = stat($cwd) or die "stat(cwd=$cwd): $!"; my @st_pwd = stat($pwd); # PWD invalid, use cwd