From: Eric Wong Date: Fri, 29 Nov 2024 23:54:00 +0000 (+0000) Subject: send_cmd: throttle `sleeping on sendmsg' messages X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19b80910041f1d0fcf437f035f5cb6aedfef78ab;p=thirdparty%2Fpublic-inbox.git send_cmd: throttle `sleeping on sendmsg' messages Emitting a message every 100ms was too much for busy machines. Throttle it to 1.6s to avoid flooding user terminals by emitting every 16 sleeps. A power-of-two (16) was chosen for its optimization potential via bitwise AND. Since perl(1) doesn't do this optimization, we open code the bitwise AND. I don't assume an optimizing compiler for Inline::C, either, since I find working in C far more enjoyable with optimizations off. --- diff --git a/lib/PublicInbox/CmdIPC4.pm b/lib/PublicInbox/CmdIPC4.pm index bdf17a3f5..af49f36d3 100644 --- a/lib/PublicInbox/CmdIPC4.pm +++ b/lib/PublicInbox/CmdIPC4.pm @@ -12,7 +12,8 @@ sub sendmsg_retry ($) { return 1 if $!{EINTR}; return unless ($!{ENOMEM} || $!{ENOBUFS} || $!{ETOOMANYREFS}); return if $_[0]-- == 0; - warn "# sleeping on sendmsg: $! ($_[0] tries left)\n"; + # n.b. `N & (power-of-two - 1)' is a faster `N % power-of-two' + warn "# sleeping on sendmsg: $! ($_[0] tries left)\n" if !($_[0] & 15); select(undef, undef, undef, 0.1); 1; } diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm index 1c6cc5bec..d818af0f1 100644 --- a/lib/PublicInbox/Spawn.pm +++ b/lib/PublicInbox/Spawn.pm @@ -184,8 +184,10 @@ static int sendmsg_retry(long *tries) case EINTR: PERL_ASYNC_CHECK(); return 1; case ENOBUFS: case ENOMEM: case ETOOMANYREFS: if (*tries-- == 0) return 0; - fprintf(stderr, "# sleeping on sendmsg: %s (%ld tries left)\n", - strerror(err), *tries); + if (!(*tries & 15)) + fprintf(stderr, + "# sleeping on sendmsg: %s (%ld tries left)\n", + strerror(err), *tries); nanosleep(&req, NULL); PERL_ASYNC_CHECK(); return 1;