Perl/timerfd.pm

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