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-ASN: 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 654C31F4B9 for ; Mon, 24 Jun 2019 02:52:59 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 03/57] ds: use and export monotonic now() Date: Mon, 24 Jun 2019 02:52:04 +0000 Message-Id: <20190624025258.25592-4-e@80x24.org> In-Reply-To: <20190624025258.25592-1-e@80x24.org> References: <20190624025258.25592-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: All of our internal timing code should use monotonic clocks for consistency against system clock adjustments. This can be shared by our Daemon and NNTP packages. --- lib/PublicInbox/DS.pm | 11 +++++++---- lib/PublicInbox/Daemon.pm | 9 ++++----- lib/PublicInbox/NNTP.pm | 4 +--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index 9e24ed78..e7db2034 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -17,10 +17,11 @@ package PublicInbox::DS; use strict; use bytes; use POSIX (); -use Time::HiRes (); use IO::Handle qw(); use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD); - +use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); +use parent qw(Exporter); +our @EXPORT_OK = qw(now); use warnings; use PublicInbox::Syscall qw(:epoll); @@ -115,7 +116,7 @@ sub AddTimer { my $class = shift; my ($secs, $coderef) = @_; - my $fire_time = Time::HiRes::time() + $secs; + my $fire_time = now() + $secs; my $timer = bless [$fire_time, $coderef], "PublicInbox::DS::Timer"; @@ -195,11 +196,13 @@ sub FirstTimeEventLoop { } } +sub now () { clock_gettime(CLOCK_MONOTONIC) } + # runs timers and returns milliseconds for next one, or next event loop sub RunTimers { return $LoopTimeout unless @Timers; - my $now = Time::HiRes::time(); + my $now = now(); # Run expired timers while (@Timers && $Timers[0][0] <= $now) { diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index 227ba5f9..b8d6b572 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -9,10 +9,9 @@ use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/; use IO::Handle; use IO::Socket; use Cwd qw/abs_path/; -use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); STDOUT->autoflush(1); STDERR->autoflush(1); -require PublicInbox::DS; +use PublicInbox::DS qw(now); require PublicInbox::EvCleanup; require POSIX; require PublicInbox::Listener; @@ -183,7 +182,7 @@ sub worker_quit { PublicInbox::DS->SetPostLoopCallback(sub { my ($dmap, undef) = @_; my $n = 0; - my $now = clock_gettime(CLOCK_MONOTONIC); + my $now = now(); foreach my $s (values %$dmap) { $s->can('busy') or next; @@ -195,9 +194,9 @@ sub worker_quit { } } if ($n) { - if (($warn + 5) < time) { + if (($warn + 5) < now()) { warn "$$ quitting, $n client(s) left\n"; - $warn = time; + $warn = now(); } unless (defined $proc_name) { $proc_name = (split(/\s+/, $0))[0]; diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm index 107cbe31..0a473e42 100644 --- a/lib/PublicInbox/NNTP.pm +++ b/lib/PublicInbox/NNTP.pm @@ -14,7 +14,7 @@ use PublicInbox::Git; require PublicInbox::EvCleanup; use Email::Simple; use POSIX qw(strftime); -use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); +PublicInbox::DS->import('now'); use Digest::SHA qw(sha1_hex); use Time::Local qw(timegm timelocal); use constant { @@ -25,8 +25,6 @@ use constant { r430 => '430 No article with that message-id', }; -sub now () { clock_gettime(CLOCK_MONOTONIC) }; - my @OVERVIEW = qw(Subject From Date Message-ID References Xref); my $OVERVIEW_FMT = join(":\r\n", @OVERVIEW, qw(Bytes Lines)) . ":\r\n"; my $LIST_HEADERS = join("\r\n", @OVERVIEW, -- EW