Perl/timerfd.pm

filter: Statementstmt 91.4% 75 / 82 Branchbran 50.0% 8 / 16 Conditioncond 71.4% 10 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 82.8% 116 / 140 CC 16 SCAR 14.5
CC
16
Cov
83%
settime
8.1
new
6.2
set_ticks
5.6
CRAP 4.3
« Perl/getrandom.pm ↑ Summary Perl/mq.pm »
1 package Linux::Perl::timerfd;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Linux::Perl::timerfd
8  
9 =head1 SYNOPSIS
10  
11 my $tfd = Linux::Perl::timerfd->new(
12 clockid => 'REALTIME',
13 flags => [ 'NONBLOCK', 'CLOEXEC' ],
14 );
15  
16 #or, e.g., Linux::Perl::timerfd::x86_64
17  
18 my $fd = $tfd->fileno();
19  
20 ($old_interval, $old_value) = $tfd->settime(
21 interval => $interval_seconds,
22 value => $value_seconds,
23 flags => [ 'ABSTIME', 'CANCEL_ON_SET' ],
24 );
25  
26 my ($interval, $value) = $tfd->gettime();
27  
28 $tfd->set_ticks(12);
29  
30 my $read = $tfd->read();
31  
32 =head1 DESCRIPTION
33  
34 This is an interface to the C<timerfd_*> family of system calls.
35  
36 This class inherits from L<Linux::Perl::Base::TimerEventFD>.
37  
38 =cut
39  
40 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 3 ▶use parent 'Linux::Perl::Base::TimerEventFD';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 3 ▶use Call::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 3 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use Linux::Perl::Endian;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use Linux::Perl::ParseFlags;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 3 ▶use Linux::Perl::TimeSpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 use constant {
53 3 _clock_REALTIME => 0,
54 _clock_MONOTONIC => 1,
55 _clock_BOOTTIME => 7,
56 _clock_REALTIME_ALARM => 8,
57 _clock_BOOTTIME_ALARM => 9,
58  
59 _ENOTTY => 25, #constant for Linux?
60 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 #----------------------------------------------------------------------
63  
64 =head1 METHODS
65  
66 =head2 I<CLASS>->new( %OPTS )
67  
68 %OPTS is:
69  
70 =over
71  
72 =item * C<clockid> - One of: C<REALTIME>, C<MONOTONIC>, C<BOOTTIME>,
73 C<REALTIME_ALARM>, or C<BOOTTIME_ALARM>. Not all kernel versions support
74 all of these; check C<man 2 timerfd_create> for your system.
75  
76 =item * C<flags> - Optional, an array reference of any or all of:
77 C<NONBLOCK>, C<CLOEXEC>.
78  
79 This follows the same practice as L<Linux::Perl::eventfd> regarding
80 CLOEXEC and C<$^F>.
81  
82 =back
83  
84 =cut
85  
86 sub new {
87 12 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=6 SCAR=18.3
Pod documented
88  
89 12 ▶ my $clockid_str = $opts{'clockid'} || die 'Need “clockid”!';
Condition50%1/2
$opts{'clockid'} || die("Need \342\200\234clockid\342\200\235!")
l!l
120
MC/DC0%0/1
$opts{'clockid'} || die("Need \342\200\234clockid\342\200\235!")
$opts{'clockid'}
Truth table50%1/2
A
A= $opts{'clockid'}
Aresult
11
00
90 12 my $clockid = $class->can("_clock_$clockid_str");
91 12 ▶ if (!$clockid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($clockid) 0 12 12
92 0 die "Unknown “clockid”: “$clockid_str”!";
93 }
94  
95 12 $clockid = $clockid->();
96  
97 12 my $flags = Linux::Perl::ParseFlags::parse( $class, $opts{'flags'} );
98  
99 12 ▶ my $arch_module = $class->can('NR_timerfd_create') && $class;
Condition66%2/3
$class->can('NR_timerfd_create') && $class
!ll&&!rl&&r
606
MC/DC50%1/2
$class->can('NR_timerfd_create') && $class
$class->can('NR_timerfd_create') $class
Truth table66%2/3
A && B
A= $class->can('NR_timerfd_create')B= $class
ABresult
0X0
100
111
100 12 ▶ $arch_module ||= do {
Condition66%2/3
$arch_module ||= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
l!l&&r!l&&!r
660
MC/DC0%0/2
$arch_module ||= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
$arch_module do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
Truth table66%2/3
A ||= B
A= $arch_moduleB= do { require Linux::Perl::ArchLoader; Linux::Perl::ArchLoader::get_arch_module($class) }
ABresult
1X1
011
000
101 6 require Linux::Perl::ArchLoader;
102 6 Linux::Perl::ArchLoader::get_arch_module($class);
103 };
104  
105 12 my $fd = Linux::Perl::call( $arch_module->NR_timerfd_create(), 0 + $clockid, $flags );
106  
107 #Force CLOEXEC if the flag was given.
108 12 ▶ local $^F = 0 if $flags & $arch_module->_flag_CLOEXEC();
Branch: 1 branch
BranchTrueFalseTotal
if $flags & $arch_module->_flag_CLOEXEC 2 10 12
109  
110 12 open my $fh, '+<&=' . $fd;
111  
112 12 return bless [$fh], $arch_module;
113 }
114  
115 #----------------------------------------------------------------------
116  
117 =head2 $OBJ = I<OBJ>->settime( %OPTS )
118  
119 =head2 ($old_interval, $old_value) = I<OBJ>->settime( %OPTS )
120  
121 See C<man 2 timerfd_settime> for details about what this does.
122  
123 %OPTS is:
124  
125 =over
126  
127 =item * C<value> - in seconds.
128  
129 =item * C<interval> - in seconds. Must be falsy if C<value> is falsy.
130 (Rationale: C<timerfd_settime> will ignore C<interval> if C<value>
131 is zero. This seems unintuitive, so we avoid that situation
132 altogether.)
133  
134 =item * C<flags> - Optional, arrayref. Accepted values are
135 C<ABSTIME> and C<CANCEL_ON_SET>. Your kernel may not support
136 all of these; check C<man 2 timerfd_settime> for details.
137  
138 =back
139  
140 In scalar context this returns the object. This facilitates easy
141 setting of the value on instantiation.
142  
143 In list context it returns the previous interval and value.
144  
145 =cut
146  
147 sub settime {
148 6 ▶ my ($self, %opts) = @_;
Subroutine settime: called CC=7 SCAR=20.9
Pod documented
149  
150 my $flags = Linux::Perl::ParseFlags::parse(
151 'Linux::Perl::timerfd::_set_flags',
152 6 $opts{'flags'},
153 );
154  
155 6 ▶ if (!$opts{'value'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"value"}) 0 6 6
156 0 ▶ if ($opts{'interval'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"interval"}) 0 0 0
157 0 die "“interval” is ignored if “value” is 0.";
158 }
159  
160 0 $opts{'value'} = 0;
161 }
162  
163 6 ▶ $opts{'interval'} ||= 0;
Condition100%2/2
$opts{"interval"} ||= 0
l!l
42
MC/DC100%1/1
$opts{"interval"} ||= 0
$opts{"interval"}
Truth table100%2/2
A
A= $opts{"interval"}
Aresult
11
00
164  
165 6 ▶ my $int_packed = Linux::Perl::TimeSpec::from_float( $opts{'interval'} || 0 );
Condition100%2/2
$opts{'interval'} || 0
l!l
42
MC/DC100%1/1
$opts{'interval'} || 0
$opts{'interval'}
Truth table100%2/2
A
A= $opts{'interval'}
Aresult
11
00
166 6 ▶ my $val_packed = Linux::Perl::TimeSpec::from_float( $opts{'value'} || 0 );
Condition50%1/2
$opts{'value'} || 0
l!l
60
MC/DC0%0/1
$opts{'value'} || 0
$opts{'value'}
Truth table50%1/2
A
A= $opts{'value'}
Aresult
11
00
167  
168 6 my $new_packed = $int_packed . $val_packed;
169 6 my $old_packed = ("\0") x length $new_packed;
170  
171 6 Linux::Perl::call( $self->NR_timerfd_settime(), 0 + $self->fileno(), 0 + $flags, $new_packed, $old_packed );
172  
173 6 ▶ return wantarray ? _parse_itimerspec($old_packed) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 4 6
174 }
175  
176 #----------------------------------------------------------------------
177  
178 =head2 ($old_interval, $old_value) = I<OBJ>->gettime()
179  
180 Returns the old C<interval> and C<value>, in seconds.
181  
182 =cut
183  
184 sub gettime {
185 2 ▶ my ($self) = @_;
Subroutine gettime: called CC=1 SCAR=0.0
Pod documented
186  
187 2 Call::Context::must_be_list();
188  
189 2 my $packed = ( Linux::Perl::TimeSpec::from_float(0) ) x 2;
190  
191 2 Linux::Perl::call( $self->NR_timerfd_gettime(), 0 + $self->fileno(), $packed );
192  
193 2 return _parse_itimerspec($packed);
194 }
195  
196 #----------------------------------------------------------------------
197  
198 =head2 my $ok_yn = I<OBJ>->set_ticks( $NUM_TICKS )
199  
200 See C<man 2 timerfd_create> (look for C<TFD_IOC_SET_TICKS>) for details
201 on what this does.
202  
203 This returns truthy if the operation succeeded and falsy if
204 the system does not support this operation. (Any other failure
205 will prompt an exception to be thrown.)
206  
207 =cut
208  
209 # man 2 ioctl_list
210 3 ▶use constant _TFD_IOC_SET_TICKS => 0x40085400;
Subroutine BEGIN: called CC=1 SCAR=0.0
211  
212 sub set_ticks {
213 2 ▶ my ($self, $num_ticks) = @_;
Subroutine set_ticks: called CC=4 SCAR=17.3
Pod documented
214  
215 2 my $buf = "\0" x 8;
216  
217 2 ▶ if ($self->_PERL_CAN_64BIT()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_PERL_CAN_64BIT) { } 2 0 2
218 2 $buf = pack 'Q', $num_ticks;
219 }
220 elsif (Linux::Perl::Endian::SYSTEM_IS_BIG_ENDIAN) {
221 $buf = ("\0" x 4) . pack('N', $num_ticks);
222 }
223 else {
224 0 $buf = pack('V', $num_ticks) . ("\0" x 4);
225 }
226  
227 2 local $!;
228 2 ▶ return 1 if ioctl( $self->[0], _TFD_IOC_SET_TICKS(), $buf );
Branch: 1 branch
BranchTrueFalseTotal
if ioctl $self->[0], 1074287616, $buf 2 0 2
229  
230 0 ▶ return !1 if $! == _ENOTTY(); #falsy
Branch: 1 branch
BranchTrueFalseTotal
if $! == 25 0 0 0
231  
232 0 die "ioctl($self->[0][0], TFD_IOC_SET_TICKS): $!";
233 }
234  
235 #----------------------------------------------------------------------
236  
237 =head2 $expirations = I<OBJ>->read()
238  
239 See C<man 2 timerfd_create> for details on what this returns.
240 Sets C<$!> and returns undef on error.
241  
242 =cut
243  
244 3 *read = __PACKAGE__->can('_read');
245  
246 #----------------------------------------------------------------------
247  
248 sub _parse_itimerspec {
249 4 ▶ my ($packed) = @_;
Subroutine _parse_itimerspec: called CC=2 SCAR=6.9
250  
251 4 my $tslen = length($packed) / 2;
252 4 my ($int, $val) = unpack "a${tslen}a${tslen}", $packed;
253 4 $_ = Linux::Perl::TimeSpec::to_float($_) for ($int, $val);
254  
255 4 return ($int, $val);
256 }
257  
258 #----------------------------------------------------------------------
259  
260 package Linux::Perl::timerfd::_set_flags;
261  
262 use constant {
263 3 _flag_ABSTIME => 1,
264 _flag_CANCEL_ON_SET => 2,
265 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
266  
267 3 1;
« Perl/getrandom.pm ↑ Summary Perl/mq.pm »