File Coverage

blib/lib/System/Introspector/Probe/Processes.pm
Criterion Covered Total %
statement 25 25 100.0
branch 1 2 50.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 31 33 93.9


line stmt bran cond sub pod time code
1             package System::Introspector::Probe::Processes;
2 1     1   21620 use Moo;
  1         14793  
  1         5  
3              
4 1         433 use System::Introspector::Util qw(
5             handle_from_command
6             transform_exceptions
7 1     1   2243 );
  1         4  
8              
9             # args is automatically included, since it has to be last
10             my @Included = qw(
11             blocked
12             c
13             class
14             cputime
15             egid egroup
16             etime
17             euid euser
18             fgid fgroup
19             flags
20             fuid fuser
21             ignored
22             lwp
23             nice
24             nlwp
25             pgid pgrp
26             pid ppid
27             pri
28             psr
29             rgid rgroup
30             rss
31             ruid ruser
32             sgid sgroup
33             sid
34             size
35             start_time
36             stat
37             suid suser
38             tid
39             time
40             tname
41             wchan
42             );
43              
44             sub gather {
45 1     1 0 54 my ($self) = @_;
46 1         13 my @names = (@Included, 'args');
47             return transform_exceptions {
48 1     1   4 my $pipe = $self->_open_ps_pipe;
49 1         9 my $spec = <$pipe>;
50 1         75 $spec =~ s{(?:^\s+|\s+$)}{}g;
51 1         41 my @fields = map lc, split m{\s+}, $spec;
52 1         8 my @rows;
53 1         14 while (defined( my $line = <$pipe> )) {
54 11         21 chomp $line;
55 11         947 $line =~ s{(?:^\s+|\s+$)}{}g;
56 11         188 my @values = split m{\s+}, $line, scalar @fields;
57 11         34 my %row;
58 11         292 @row{ @names } = @values;
59 11         113 push @rows, \%row;
60             }
61 29 50       99 return { processes => [ sort {
62 1         11 ($a->{args} cmp $b->{args})
63             ||
64             ($a->{pid} <=> $b->{pid})
65             } @rows ] };
66 1         11 };
67             }
68              
69             sub _open_ps_pipe {
70 1     1   2 my ($self) = @_;
71 1         12 return handle_from_command sprintf
72             'ps -eo %s',
73             join(',', @Included, 'args');
74             }
75              
76             1;
77              
78             __END__