File Coverage

blib/lib/Linux/Info/MemStats.pm
Criterion Covered Total %
statement 35 38 92.1
branch 7 12 58.3
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 49 57 85.9


line stmt bran cond sub pod time code
1             package Linux::Info::MemStats;
2 2     2   16 use strict;
  2         6  
  2         68  
3 2     2   14 use warnings;
  2         5  
  2         75  
4 2     2   13 use Carp qw(croak);
  2         6  
  2         1140  
5             our $VERSION = '1.3'; # VERSION
6              
7             =head1 NAME
8              
9             Linux::Info::MemStats - Collect linux memory information.
10              
11             =head1 SYNOPSIS
12              
13             use Linux::Info::MemStats;
14              
15             my $lxs = Linux::Info::MemStats->new;
16             my $stat = $lxs->get;
17              
18             =head1 DESCRIPTION
19              
20             Linux::Info::MemStats gathers memory statistics from the virtual F filesystem (procfs).
21              
22             For more information read the documentation of the front-end module L.
23              
24             =head1 MEMORY INFORMATIONS
25              
26             Generated by F.
27              
28             memused - Total size of used memory in kilobytes.
29             memfree - Total size of free memory in kilobytes.
30             memusedper - Total size of used memory in percent.
31             memtotal - Total size of memory in kilobytes.
32             buffers - Total size of buffers used from memory in kilobytes.
33             cached - Total size of cached memory in kilobytes.
34             realfree - Total size of memory is real free (memfree + buffers + cached).
35             realfreeper - Total size of memory is real free in percent of total memory.
36             swapused - Total size of swap space is used is kilobytes.
37             swapfree - Total size of swap space is free in kilobytes.
38             swapusedper - Total size of swap space is used in percent.
39             swaptotal - Total size of swap space in kilobytes.
40             swapcached - Memory that once was swapped out, is swapped back in but still also is in the swapfile.
41             active - Memory that has been used more recently and usually not reclaimed unless absolutely necessary.
42             inactive - Memory which has been less recently used and is more eligible to be reclaimed for other purposes.
43             On earlier kernels (2.4) Inact_dirty + Inact_laundry + Inact_clean.
44              
45             The following statistics are only available by kernels from 2.6.
46              
47             slab - Total size of memory in kilobytes that used by kernel for data structure allocations.
48             dirty - Total size of memory pages in kilobytes that waits to be written back to disk.
49             mapped - Total size of memory in kilbytes that is mapped by devices or libraries with mmap.
50             writeback - Total size of memory that was written back to disk.
51             committed_as - The amount of memory presently allocated on the system.
52              
53             The following statistic is only available by kernels from 2.6.9.
54              
55             commitlimit - Total amount of memory currently available to be allocated on the system.
56              
57             =head1 METHODS
58              
59             =head2 new()
60              
61             Call C to create a new object.
62              
63             my $lxs = Linux::Info::MemStats->new;
64              
65             It's possible to set the path to the proc filesystem.
66              
67             Linux::Info::MemStats->new(
68             files => {
69             # This is the default
70             path => '/proc',
71             meminfo => 'meminfo',
72             }
73             );
74              
75             =head2 get()
76              
77             Call C to get the statistics. C returns the statistics as a hash reference.
78              
79             my $stat = $lxs->get;
80              
81             =head1 EXPORTS
82              
83             Nothing.
84              
85             =head1 SEE ALSO
86              
87             =over
88              
89             =item *
90              
91             B
92              
93             =item *
94              
95             L
96              
97             =back
98              
99             =head1 AUTHOR
100              
101             Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
102              
103             =head1 COPYRIGHT AND LICENSE
104              
105             This software is copyright (c) 2015 of Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
106              
107             This file is part of Linux Info project.
108              
109             Linux-Info is free software: you can redistribute it and/or modify
110             it under the terms of the GNU General Public License as published by
111             the Free Software Foundation, either version 3 of the License, or
112             (at your option) any later version.
113              
114             Linux-Info is distributed in the hope that it will be useful,
115             but WITHOUT ANY WARRANTY; without even the implied warranty of
116             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
117             GNU General Public License for more details.
118              
119             You should have received a copy of the GNU General Public License
120             along with Linux Info. If not, see .
121              
122             =cut
123              
124             sub new {
125 2     2 1 6 my $class = shift;
126 2 50       9 my $opts = ref($_[0]) ? shift : {@_};
127              
128 2         9 my %self = (
129             files => {
130             path => '/proc',
131             meminfo => 'meminfo',
132             }
133             );
134              
135 2         6 foreach my $file (keys %{ $opts->{files} }) {
  2         10  
136 0         0 $self{files}{$file} = $opts->{files}->{$file};
137             }
138              
139 2         14 return bless \%self, $class;
140             }
141              
142             sub get {
143 2     2 1 7 my $self = shift;
144 2         8 my $class = ref($self);
145 2         19 my $file = $self->{files};
146 2         5 my %meminfo = ();
147              
148 2 50       16 my $filename = $file->{path} ? "$file->{path}/$file->{meminfo}" : $file->{meminfo};
149 2 50       137 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
150              
151             # MemTotal: 1035648 kB
152             # MemFree: 15220 kB
153             # Buffers: 4280 kB
154             # Cached: 47664 kB
155             # SwapCached: 473988 kB
156             # Active: 661992 kB
157             # Inactive: 314312 kB
158             # HighTotal: 130884 kB
159             # HighFree: 264 kB
160             # LowTotal: 904764 kB
161             # LowFree: 14956 kB
162             # SwapTotal: 1951856 kB
163             # SwapFree: 1164864 kB
164             # Dirty: 520 kB
165             # Writeback: 0 kB
166             # AnonPages: 908892 kB
167             # Mapped: 34308 kB
168             # Slab: 19284 kB
169             # SReclaimable: 7532 kB
170             # SUnreclaim: 11752 kB
171             # PageTables: 3056 kB
172             # NFS_Unstable: 0 kB
173             # Bounce: 0 kB
174             # CommitLimit: 2469680 kB
175             # Committed_AS: 1699568 kB
176             # VmallocTotal: 114680 kB
177             # VmallocUsed: 12284 kB
178             # VmallocChunk: 100992 kB
179              
180             # kernel <= 2.4
181             # Inact_dirty: 138632 kB
182             # Inact_laundry: 35520 kB
183             # Inact_clean: 7544 kB
184              
185 2         379 while (my $line = <$fh>) {
186 84 100       378 if ($line =~ /^((?:Mem|Swap)(?:Total|Free)|Buffers|Cached|SwapCached|Active|Inactive|
    50          
187             Dirty|Writeback|Mapped|Slab|Commit(?:Limit|ted_AS)):\s*(\d+)/x) {
188 30         84 my ($n, $v) = ($1, $2);
189 30         56 $n =~ tr/A-Z/a-z/;
190 30         167 $meminfo{$n} = $v;
191             } elsif ($line =~ /^Inact_(?:dirty|laundry|clean):\s*(\d+)/) {
192 0         0 $meminfo{inactive} += $1;
193             }
194             }
195              
196 2         66 close($fh);
197              
198 2         49 $meminfo{memused} = sprintf('%u', $meminfo{memtotal} - $meminfo{memfree});
199 2         41 $meminfo{memusedper} = sprintf('%.2f', 100 * $meminfo{memused} / $meminfo{memtotal});
200 2         8 $meminfo{swapused} = sprintf('%u', $meminfo{swaptotal} - $meminfo{swapfree});
201 2         10 $meminfo{realfree} = sprintf('%u', $meminfo{memfree} + $meminfo{buffers} + $meminfo{cached});
202 2         11 $meminfo{realfreeper} = sprintf('%.2f', 100 * $meminfo{realfree} / $meminfo{memtotal});
203              
204             # maybe there is no swap space on the machine
205 2 50       18 if (!$meminfo{swaptotal}) {
206 0         0 $meminfo{swapusedper} = '0.00';
207             } else {
208 2         12 $meminfo{swapusedper} = sprintf('%.2f', 100 * $meminfo{swapused} / $meminfo{swaptotal});
209             }
210              
211 2         18 return \%meminfo;
212             }
213              
214             1;