File Coverage

blib/lib/Nix/Proc/Meminfo.pm
Criterion Covered Total %
statement 8 8 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 11 11 100.0


line stmt bran cond sub pod time code
1              
2 1     1   305472 use v5.42;
  1         5  
3 1     1   8 use feature 'class';
  1         1  
  1         244  
4 1     1   9 no warnings 'experimental::class';
  1         7  
  1         967  
5              
6             class Nix::Proc::Meminfo 0.11{
7              
8             field $file_uri :param = "/proc/meminfo";
9              
10             my method line_clean($line){
11             my ($param, $value, $unit) = split " ", $line;
12             $unit //= "";
13              
14             #remove the colon
15             $param =~ s/\://;
16              
17             #remove leading and trailing whitespace just in case
18             $param = trim($param);
19             $value = trim($value);
20             $unit = trim($unit);
21              
22             return ($param, $value, $unit);
23             }
24              
25             method get_all(){
26             my %all;
27             open(my $meminfo, "<", $file_uri) or die "cannot open $file_uri $!";
28             foreach my $line (<$meminfo>){
29             my ($param, $value, $unit) = $self->&line_clean($line);
30             $all{$param}{value} = $value;
31             $all{$param}{unit} = $unit;
32             }
33             close $meminfo;
34             return \%all;
35             }
36              
37             method get($search){
38             my %all;
39             open(my $meminfo, "<", $file_uri) or die "cannot open $file_uri $!";
40             foreach my $line (<$meminfo>){
41             my ($param, $value, $unit) = $self->&line_clean($line);
42              
43             if ($param eq $search){
44             $all{'value'} = $value;
45             $all{'unit'} = $unit;
46             close $meminfo;
47             return \%all;
48             }
49             }
50             close $meminfo;
51             return false;
52             }
53             }
54              
55             =pod
56              
57             =head1 NAME
58              
59             Nix::Proc::Meminfo - access /proc/meminfo with core classes
60              
61             =head1 SYNOPSIS
62              
63             #!/usr/bin/env perl
64              
65             use v5.42;
66             use Nix::Proc::Meminfo;
67              
68             my $proc = Nix::Proc::Meminfo->new();
69              
70             # optionally you can provide a parameter for a custom location
71             $proc = Nix::Proc::Meminfo->new( file_uri => '/proc/meminfo' );
72              
73             # return a hash ref with all meminfo parameters as keys, each with a "value" and "unit"
74             my $mi = $proc->get_all();
75             say "MemTotal: ".$mi->{'MemTotal'}{'value'};
76              
77             # alternatively you can just retrieve a single value from /proc/meminfo wrather than load all of them
78             # which will save a tiny little bit of memory and possibly time
79             say "Active(file): " . $proc->get('Active(file)')->{'value'};
80              
81             # the units are also provided and will typically be KB or an empty string
82             say "DirectMap4k: " . $proc->get('DirectMap4k')->{'unit'};
83              
84             =head1 DESCRIPTION
85              
86             Nix::Proc::Meminfo provides access to the linux /proc/meminfo file which contains real time
87             data about current memory usage provided directly by the kernel. This module requires a minimum 5.42 version of perl
88             because it uses modern perl features throughout it's code. This module is written using the core class feature.
89              
90             The module is parameter agnostic. Meaning it simply parses the meminfo file on your system the way it is presented by the Linux kernel. Some Linux kernels that have been modified or are ancient, may not provide certain parameters. It is the duty of the
91             user to understand what is provided by a modified kernel.
92              
93             =head1 METHODS
94              
95             =head2 new
96              
97             C
98              
99             Object constructor that accepts a single optional argument C allowing the user to change the location
100             the object looks for the meminfo file. This obviously defaults to C where it typically should be.
101              
102             =head2 get
103              
104             C
105              
106             Accepts a string that is the case sensitive name of the parameter found in the meminfo file.
107             Returns either C if the parameter was not found or a hash ref with two keys:
108              
109             C and C. Where C is probably the number you are looking for
110             and C is typically either "KB" or an empty string.
111              
112             =head2 get_all
113              
114             C
115              
116             Accepts no arguments. Returns a hash reference where the parameters of your meminfo file are keys and values are hashes containing C and C keys respectively.
117              
118             =head1 SEE ALSO
119              
120             Study what you might expect to find in your C file:
121              
122             L
123              
124             =head1 LICENSE
125              
126             Copyright (C) 2025 Joshua S. Day
127              
128             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
129              
130             =cut