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