File Coverage

blib/lib/Linux/Statm/Tiny.pm
Criterion Covered Total %
statement 29 29 100.0
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 39 40 97.5


line stmt bran cond sub pod time code
1             package Linux::Statm::Tiny;
2              
3 3     3   1736 use v5.10.1;
  3         9  
4              
5 3     3   1255 use Moo;
  3         28220  
  3         10  
6 3     3   4605 use MooX::Aliases;
  3         12629  
  3         15  
7              
8 3     3   805 use Fcntl qw/ O_RDONLY /;
  3         6  
  3         200  
9 3     3   784 use POSIX qw/ ceil /;
  3         10352  
  3         14  
10 3     3   3760 use Types::Standard qw/ ArrayRef Int /;
  3         183750  
  3         22  
11              
12 3     3   2252 use constant page_size => POSIX::sysconf POSIX::_SC_PAGESIZE;
  3         6  
  3         1380  
13              
14             our $VERSION = '0.0603';
15              
16             # ABSTRACT: simple access to Linux /proc/../statm
17              
18              
19             has pid => (
20             is => 'lazy',
21             isa => Int,
22             default => sub { $$ },
23             );
24              
25              
26             has statm => (
27             is => 'lazy',
28             isa => ArrayRef[Int],
29             writer => 'refresh',
30             init_arg => undef,
31             );
32              
33             sub _build_statm {
34 2     2   7945 my ($self) = @_;
35 2         43 my $pid = $self->pid;
36 2 50       171 sysopen( my $fh, "/proc/${pid}/statm", O_RDONLY )
37             or die "Unable to open /proc/${pid}/statm: $!";
38 2         39 chomp(my $raw = <$fh>);
39 2         23 close $fh;
40 2         57 [ split / /, $raw ];
41             }
42              
43              
44             my %stats = (
45             size => 0,
46             resident => 1,
47             share => 2,
48             text => 3,
49             lib => 4,
50             data => 5,
51             dt => 6,
52             );
53              
54             my %aliases = (
55             size => 'vsz',
56             resident => 'rss',
57             );
58              
59             my %alts = ( # page_size multipliers
60             bytes => 1,
61             kb => 1024,
62             mb => 1048576,
63             );
64              
65             my @attrs;
66              
67             foreach my $attr (keys %stats) {
68              
69             my @aliases = ( "${attr}_pages" );
70             push @aliases, ( $aliases{$attr}, $aliases{$attr} . '_pages' )
71             if $aliases{$attr};
72              
73             has $attr => (
74             is => 'lazy',
75             isa => Int,
76             default => sub { shift->statm->[$stats{$attr}] },
77             init_arg => undef,
78             alias => \@aliases,
79             clearer => "_refresh_${attr}",
80             );
81              
82             push @attrs, $attr;
83              
84             foreach my $alt (keys %alts) {
85             has "${attr}_${alt}" => (
86             is => 'lazy',
87             isa => Int,
88             default => sub { my $self = shift;
89             ceil($self->$attr * page_size / $alts{$alt});
90             },
91             init_arg => undef,
92             clearer => "_refresh_${attr}_${alt}",
93             $aliases{$attr} ? ( alias => $aliases{$attr} . "_${alt}" ) : ( ),
94             );
95              
96             push @attrs, "${attr}_${alt}";
97             }
98              
99             }
100              
101             around refresh => sub {
102             my ($next, $self) = @_;
103             $self->$next( $self->_build_statm );
104             foreach my $attr (@attrs) {
105             my $meth = "_refresh_${attr}";
106             $self->$meth;
107             }
108             };
109              
110              
111              
112 3     3   1167 use namespace::autoclean 0.16;
  3         32436  
  3         14  
113              
114             1;
115              
116             __END__