From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 0DB711F47A; Tue, 20 Aug 2024 18:40:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1724179260; bh=gDGs4ObQ5KU+W7xcMdZTKfYrTcRS9YssDDmRxIX1Yh8=; h=Date:From:To:Subject:References:In-Reply-To:From; b=V2ICLROGgo7zVf8L/NzeezVcjV8vqaQhTRoSB4iX/bAw5N6i2T0k3/Dz2lb8B7u08 CNp/w9WBeNZIFSgMzm9DDqjSJkquNJIk+QFuuh3dix2Y+5yb85w4p4rm7hut/wvOoE NHDI/sJgnIA++rNsz3bBvbl8GcMPc1SZmsPkJquA= Date: Tue, 20 Aug 2024 18:40:59 +0000 From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 6/5] t/sigfd: reduce getpid() calls and hash lookups Message-ID: <20240820184059.M814830@dcvr> References: <20240820103522.3548609-1-e@80x24.org> <20240820103522.3548609-6-e@80x24.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20240820103522.3548609-6-e@80x24.org> List-Id: getpid() is no longer cached by glibc, syscalls are more expensive nowadays, so only call it once per test. The additional hash table depth is no longer necessary since there's no longer a difference between signal dispatch methods now that Sigfd uses the global %SIG. --- t/sigfd.t | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/t/sigfd.t b/t/sigfd.t index eab85ed7..91d11034 100644 --- a/t/sigfd.t +++ b/t/sigfd.t @@ -18,15 +18,16 @@ SKIP: { my $old = PublicInbox::DS::block_signals(); my $hit = {}; my $sig = {}; - local $SIG{USR2} = sub { $hit->{USR2}->{normal}++ }; - local $SIG{HUP} = sub { $hit->{HUP}->{normal}++ }; - local $SIG{TERM} = sub { $hit->{TERM}->{normal}++ }; - local $SIG{INT} = sub { $hit->{INT}->{normal}++ }; - local $SIG{WINCH} = sub { $hit->{WINCH}->{normal}++ }; + local $SIG{USR2} = sub { $hit->{USR2}++ }; + local $SIG{HUP} = sub { $hit->{HUP}++ }; + local $SIG{TERM} = sub { $hit->{TERM}++ }; + local $SIG{INT} = sub { $hit->{INT}++ }; + local $SIG{WINCH} = sub { $hit->{WINCH}++ }; for my $s (qw(USR2 HUP TERM INT WINCH)) { $sig->{$s} = sub { die "SHOULD NOT BE CALLED ($s)" } } - kill 'USR2', $$ or die "kill $!"; + my $PID = $$; + kill 'USR2', $PID; ok(!defined($hit->{USR2}), 'no USR2 yet') or diag explain($hit); PublicInbox::DS->Reset; ok($PublicInbox::Syscall::SIGNUM{WINCH}, 'SIGWINCH number defined'); @@ -35,9 +36,9 @@ SKIP: { $linux_sigfd = 1 if $^O eq 'linux'; $has_sigfd = 1; ok($sigfd, 'Sigfd->new works'); - kill('HUP', $$) or die "kill $!"; - kill('INT', $$) or die "kill $!"; - kill('WINCH', $$) or die "kill $!"; + kill 'HUP', $PID; + kill 'INT', $PID; + kill 'WINCH', $PID; my $fd = fileno($sigfd->{sock}); ok($fd >= 0, 'fileno(Sigfd->{sock}) works'); my $rvec = ''; @@ -45,12 +46,12 @@ SKIP: { is(select($rvec, undef, undef, undef), 1, 'select() works'); ok($sigfd->wait_once, 'wait_once reported success'); for my $s (qw(HUP INT)) { - is($hit->{$s}->{normal}, 1, "sigfd fired $s"); + is $hit->{$s}, 1, "sigfd fired $s"; } SKIP: { skip 'Linux sigfd-only behavior', 1 if !$linux_sigfd; - is($hit->{USR2}->{normal}, 1, - 'USR2 sent before signalfd created received'); + is $hit->{USR2}, 1, + 'USR2 sent before signalfd created received'; } PublicInbox::DS->Reset; $sigfd = undef; @@ -59,38 +60,38 @@ SKIP: { ok($nbsig, 'Sigfd->new SFD_NONBLOCK works'); is($nbsig->wait_once, undef, 'nonblocking ->wait_once'); ok($! == Errno::EAGAIN, 'got EAGAIN'); - kill('HUP', $$) or die "kill $!"; + kill 'HUP', $PID; local @PublicInbox::DS::post_loop_do = (sub {}); # loop once PublicInbox::DS::event_loop(); - is($hit->{HUP}->{normal}, 2, 'HUP sigfd fired in event loop') or + is $hit->{HUP}, 2, 'HUP sigfd fired in event loop' or diag explain($hit); # sometimes fails on FreeBSD 11.x - kill('TERM', $$) or die "kill $!"; - kill('HUP', $$) or die "kill $!"; + kill 'TERM', $PID; + kill 'HUP', $PID; PublicInbox::DS::event_loop(); PublicInbox::DS->Reset; - is($hit->{TERM}->{normal}, 1, 'TERM sigfd fired in event loop'); - is($hit->{HUP}->{normal}, 3, 'HUP sigfd fired in event loop'); - ok($hit->{WINCH}->{normal}, 'WINCH sigfd fired in event loop'); + is $hit->{TERM}, 1, 'TERM sigfd fired in event loop'; + is $hit->{HUP}, 3, 'HUP sigfd fired in event loop'; + ok $hit->{WINCH}, 'WINCH sigfd fired in event loop'; my $restore = PublicInbox::DS::allow_sigs 'HUP'; - kill 'HUP', $$; + kill 'HUP', $PID; select undef, undef, undef, 0; - is $hit->{HUP}->{normal}, 4, 'HUP sigfd fired after allow_sigs'; + is $hit->{HUP}, 4, 'HUP sigfd fired after allow_sigs'; undef $restore; - kill 'HUP', $$; + kill 'HUP', $PID; vec($rvec = '', fileno($nbsig->{sock}), 1) = 1; ok select($rvec, undef, undef, 1), 'select reports sigfd readiness'; - is $hit->{HUP}->{normal}, 4, 'HUP not fired when sigs blocked'; + is $hit->{HUP}, 4, 'HUP not fired when sigs blocked'; $nbsig->event_step; - is $hit->{HUP}->{normal}, 5, 'HUP fires only on ->event_step'; + is $hit->{HUP}, 5, 'HUP fires only on ->event_step'; - kill 'HUP', $$; - is $hit->{HUP}->{normal}, 5, 'HUP not fired, yet'; + kill 'HUP', $PID; + is $hit->{HUP}, 5, 'HUP not fired, yet'; $restore = PublicInbox::DS::allow_sigs 'HUP'; select(undef, undef, undef, 0); - is $hit->{HUP}->{normal}, 6, 'HUP fires from allow_sigs'; + is $hit->{HUP}, 6, 'HUP fires from allow_sigs'; } else { skip('signalfd disabled?', 10); }