| 1 | package Linux::Event::Clock; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | our $VERSION = '0.011'; | |||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use constant NS_PER_S => 1_000_000_000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use constant NS_PER_MS => 1_000_000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use constant NS_PER_US => 1_000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | sub new { | ||||||||||||
| 16 | 1 | ▶ | my ($class, %opt) = @_; | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||
| 17 | |||||||||||||
| 18 | 1 | ▶ | my $clock = $opt{clock} // 'monotonic'; | ||||||||||
|
Condition50%1/2
$opt{"clock"} // "monotonic"
MC/DC0%0/1
$opt{"clock"} // "monotonic"
$opt{"clock"}
Truth table50%1/2
A
A= $opt{"clock"}
| |||||||||||||
| 19 | 1 | ▶ | $clock eq 'monotonic' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | or croak "clock must be 'monotonic' (got '$clock')"; | ||||||||||||
| 21 | |||||||||||||
| 22 | 1 | my $self = bless { | |||||||||||
| 23 | clock => $clock, | ||||||||||||
| 24 | now_ns => 0, | ||||||||||||
| 25 | gen => 0, | ||||||||||||
| 26 | }, $class; | ||||||||||||
| 27 | |||||||||||||
| 28 | 1 | $self->tick; # prime cache | |||||||||||
| 29 | 1 | return $self; | |||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | sub tick { | ||||||||||||
| 33 | 2 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
tick: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 34 | |||||||||||||
| 35 | 2 | my $t = clock_gettime(CLOCK_MONOTONIC); | |||||||||||
| 36 | 2 | my $ns = int($t * NS_PER_S); | |||||||||||
| 37 | |||||||||||||
| 38 | 2 | $self->{now_ns} = $ns; | |||||||||||
| 39 | 2 | $self->{gen}++; | |||||||||||
| 40 | |||||||||||||
| 41 | 2 | return $ns; | |||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | sub generation { | ||||||||||||
| 45 | 2 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
generation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 46 | 2 | return $self->{gen}; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | sub now_ns { | ||||||||||||
| 50 | 2 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
now_ns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 51 | 2 | return $self->{now_ns}; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | sub now_s { | ||||||||||||
| 55 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
now_s: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 56 | 0 | return $self->{now_ns} / NS_PER_S; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | sub monotonic_ns { | ||||||||||||
| 60 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
monotonic_ns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 61 | 0 | my $t = clock_gettime(CLOCK_MONOTONIC); | |||||||||||
| 62 | 0 | return int($t * NS_PER_S); | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | sub deadline_after { | ||||||||||||
| 66 | 0 | ▶ | my ($self, $seconds) = @_; | ||||||||||
|
Subroutine
deadline_after: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 67 | 0 | ▶ | defined $seconds or croak "deadline_after requires seconds"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | return $self->{now_ns} + int($seconds * NS_PER_S); | |||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | sub deadline_after_ms { | ||||||||||||
| 72 | 0 | ▶ | my ($self, $ms) = @_; | ||||||||||
|
Subroutine
deadline_after_ms: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 73 | 0 | ▶ | defined $ms or croak "deadline_after_ms requires ms"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 0 | return $self->{now_ns} + ($ms * NS_PER_MS); | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | sub deadline_after_us { | ||||||||||||
| 78 | 0 | ▶ | my ($self, $us) = @_; | ||||||||||
|
Subroutine
deadline_after_us: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 79 | 0 | ▶ | defined $us or croak "deadline_after_us requires us"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 0 | return $self->{now_ns} + ($us * NS_PER_US); | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | sub deadline_in_ns { | ||||||||||||
| 84 | 0 | ▶ | my ($self, $delta_ns) = @_; | ||||||||||
|
Subroutine
deadline_in_ns: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 85 | 0 | ▶ | defined $delta_ns or croak "deadline_in_ns requires delta_ns"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | return $self->{now_ns} + $delta_ns; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | sub expired_ns { | ||||||||||||
| 90 | 0 | ▶ | my ($self, $deadline_ns) = @_; | ||||||||||
|
Subroutine
expired_ns: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 91 | 0 | ▶ | defined $deadline_ns or croak "expired_ns requires deadline_ns"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 0 | ▶ | return $deadline_ns <= $self->{now_ns} ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | sub remaining_ns { | ||||||||||||
| 96 | 0 | ▶ | my ($self, $deadline_ns) = @_; | ||||||||||
|
Subroutine
remaining_ns: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 97 | 0 | ▶ | defined $deadline_ns or croak "remaining_ns requires deadline_ns"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | 0 | my $rem = $deadline_ns - $self->{now_ns}; | |||||||||||
| 99 | 0 | ▶ | return $rem > 0 ? $rem : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 1 | 1; | |||||||||||
| 103 | |||||||||||||
| 104 | __END__ | ||||||||||||