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