line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Proc::CPUUsage; |
2
|
|
|
|
|
|
|
our $VERSION = '1.001'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
49679
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
68
|
|
7
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
63
|
|
8
|
2
|
|
|
2
|
|
1796
|
use BSD::Resource qw( getrusage ); |
|
2
|
|
|
|
|
10650
|
|
|
2
|
|
|
|
|
10
|
|
9
|
2
|
|
|
2
|
|
4074
|
use Time::HiRes qw( gettimeofday tv_interval ); |
|
2
|
|
|
|
|
4379
|
|
|
2
|
|
|
|
|
12
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
4
|
|
|
4
|
1
|
2408
|
my $class = shift; |
13
|
|
|
|
|
|
|
|
14
|
4
|
|
|
|
|
31
|
return bless [ [gettimeofday()], _cpu_time() ], $class; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub usage { |
18
|
5
|
|
|
5
|
1
|
1333968
|
my $self = $_[0]; |
19
|
5
|
|
|
|
|
22
|
my ($t0, $r0) = @$self; |
20
|
5
|
100
|
|
|
|
61
|
return unless defined $r0; |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
7
|
my ($dt, $dr, $t1, $r1); |
23
|
2
|
|
|
|
|
25
|
$t1 = [gettimeofday()]; |
24
|
2
|
|
|
|
|
21
|
$dt = tv_interval($t0, $t1); |
25
|
2
|
|
|
|
|
78
|
$self->[0] = $t1; |
26
|
|
|
|
|
|
|
|
27
|
2
|
|
|
|
|
11
|
$r1 = _cpu_time(); |
28
|
2
|
|
|
|
|
122
|
$dr = $r1 - $r0; |
29
|
2
|
|
|
|
|
7
|
$self->[1] = $r1; |
30
|
|
|
|
|
|
|
|
31
|
2
|
|
|
|
|
4321
|
return $dr/$dt; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _cpu_time { |
35
|
6
|
|
|
6
|
|
205
|
my ($utime, $stime) = getrusage(); |
36
|
6
|
100
|
100
|
|
|
787
|
return unless defined $utime && defined $stime; |
37
|
3
|
|
|
|
|
15
|
return $utime+$stime; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
1; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
__END__ |