File Coverage

blib/lib/Linux/Statm/Tiny.pm
Criterion Covered Total %
statement 20 20 100.0
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 27 28 96.4


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