Sys-Statistics-Linux

 view release on metacpan or  search on metacpan

lib/Sys/Statistics/Linux/Processes.pm  view on Meta::CPAN

=head1 NAME

Sys::Statistics::Linux::Processes - Collect linux process statistics.

=head1 SYNOPSIS

    use Sys::Statistics::Linux::Processes;

    my $lxs = Sys::Statistics::Linux::Processes->new;
    # or Sys::Statistics::Linux::Processes->new(pids => \@pids)

    $lxs->init;
    sleep 1;
    my $stat = $lxs->get;

=head1 DESCRIPTION

Sys::Statistics::Linux::Processes gathers process information from the virtual
F</proc> filesystem (procfs).

For more information read the documentation of the front-end module
L<Sys::Statistics::Linux>.

=head1 PROCESS STATISTICS

Generated by F</proc/E<lt>pidE<gt>/stat>, F</proc/E<lt>pidE<gt>/status>,
F</proc/E<lt>pidE<gt>/cmdline> and F<getpwuid()>.

Note that if F</etc/passwd> isn't readable, the key owner is set to F<N/a>.

    ppid      -  The parent process ID of the process.
    nlwp      -  The number of light weight processes that runs by this process.
    owner     -  The owner name of the process.
    pgrp      -  The group ID of the process.
    state     -  The status of the process.
    session   -  The session ID of the process.
    ttynr     -  The tty the process use.
    minflt    -  The number of minor faults the process made.
    cminflt   -  The number of minor faults the child process made.
    mayflt    -  The number of mayor faults the process made.
    cmayflt   -  The number of mayor faults the child process made.
    stime     -  The number of jiffies the process have beed scheduled in kernel mode.
    utime     -  The number of jiffies the process have beed scheduled in user mode.
    ttime     -  The number of jiffies the process have beed scheduled (user + kernel).
    cstime    -  The number of jiffies the process waited for childrens have been scheduled in kernel mode.
    cutime    -  The number of jiffies the process waited for childrens have been scheduled in user mode.
    prior     -  The priority of the process (+15).
    nice      -  The nice level of the process.
    sttime    -  The time in jiffies the process started after system boot.
    actime    -  The time in D:H:M:S (days, hours, minutes, seconds) the process is active.
    vsize     -  The size of virtual memory of the process.
    nswap     -  The size of swap space of the process.
    cnswap    -  The size of swap space of the childrens of the process.
    cpu       -  The CPU number the process was last executed on.
    wchan     -  The "channel" in which the process is waiting.
    fd        -  This is a subhash containing each file which the process has open, named by its file descriptor.
                 0 is standard input, 1 standard output, 2 standard error, etc. Because only the owner or root
                 can read /proc/<pid>/fd this hash could be empty.
    cmd       -  Command of the process.
    cmdline   -  Command line of the process.

Generated by F</proc/E<lt>pidE<gt>/statm>. All statistics provides information
about memory in pages:

    size      -  The total program size of the process.
    resident  -  Number of resident set size, this includes the text, data and stack space.
    share     -  Total size of shared pages of the process.
    trs       -  Total text size of the process.
    drs       -  Total data/stack size of the process.
    lrs       -  Total library size of the process.
    dtp       -  Total size of dirty pages of the process (unused since kernel 2.6).

It's possible to convert pages to bytes or kilobytes. Example - if the pagesize of your
system is 4kb:

    $Sys::Statistics::Linux::Processes::PAGES_TO_BYTES =    0; # pages (default)
    $Sys::Statistics::Linux::Processes::PAGES_TO_BYTES =    4; # convert to kilobytes
    $Sys::Statistics::Linux::Processes::PAGES_TO_BYTES = 4096; # convert to bytes

    # or with
    Sys::Statistics::Linux::Processes->new(pages_to_bytes => 4096);

Generated by F</proc/E<lt>pidE<gt>/io>.

    rchar                 -  Bytes read from storage (might have been from pagecache).
    wchar                 -  Bytes written.
    syscr                 -  Number of read syscalls.

lib/Sys/Statistics/Linux/Processes.pm  view on Meta::CPAN


=head1 METHODS

=head2 new()

Call C<new()> to create a new object.

    my $lxs = Sys::Statistics::Linux::Processes->new;

It's possible to handoff an array reference with a PID list.

    my $lxs = Sys::Statistics::Linux::Processes->new(pids => [ 1, 2, 3 ]);

It's also possible to set the path to the proc filesystem.

     Sys::Statistics::Linux::Processes->new(
        files => {
            # This is the default
            path    => '/proc',
            uptime  => 'uptime',
            stat    => 'stat',
            statm   => 'statm',
            status  => 'status',
            cmdline => 'cmdline',
            wchan   => 'wchan',
            fd      => 'fd',
            io      => 'io',
        }
    );

=head2 init()

Call C<init()> to initialize the statistics.

    $lxs->init;

=head2 get()

Call C<get()> to get the statistics. C<get()> returns the statistics as a hash reference.

    my $stat = $lxs->get;

Note:

Processes that were created between the call of init() and get() are returned as well,
but the keys minflt, cminflt, mayflt, cmayflt, utime, stime, cutime, and cstime are set
to the value 0.00 because there are no inititial values to calculate the deltas.

=head2 raw()

Get raw values.

=head1 EXPORTS

No exports.

=head1 SEE ALSO

B<proc(5)>

B<perldoc -f getpwuid>

=head1 REPORTING BUGS

Please report all bugs to <jschulz.cpan(at)bloonix.de>.

=head1 AUTHOR

Jonny Schulz <jschulz.cpan(at)bloonix.de>.

=head1 COPYRIGHT

Copyright (c) 2006, 2007 by Jonny Schulz. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

package Sys::Statistics::Linux::Processes;

use strict;
use warnings;
use Time::HiRes;
use constant NUMBER => qr/^-{0,1}\d+(?:\.\d+){0,1}\z/;

our $VERSION = "0.38";
our $PAGES_TO_BYTES = 0;

sub new {
    my $class = shift;
    my $opts  = ref($_[0]) ? shift : {@_};

    my %self = (
        files => {
            path    => '/proc',
            uptime  => 'uptime',
            stat    => 'stat',
            statm   => 'statm',
            status  => 'status',
            cmdline => 'cmdline',
            wchan   => 'wchan',
            fd      => 'fd',
            io      => 'io',
        },
    );

    if (defined $opts->{pids}) {
        if (ref($opts->{pids}) ne 'ARRAY') {
            die "the PIDs must be passed as a array reference to new()";
        }

        foreach my $pid (@{$opts->{pids}}) {
            if ($pid !~ /^\d+\z/) {
                die "PID '$pid' is not a number";
            }
        }

        $self{pids} = $opts->{pids};
    }

lib/Sys/Statistics/Linux/Processes.pm  view on Meta::CPAN

    open my $fh, '<', "$file->{path}/$pid/$file->{statm}"
        or return undef;

    my @line = split /\s+/, <$fh>;

    if (@line < 7) {
        return undef;
    }

    my $ptb = $self->{pages_to_bytes} || $PAGES_TO_BYTES;

    if ($ptb) {
        @stat{qw(size resident share trs lrs drs dtp)} = map { $_ * $ptb } @line;
    } else {
        @stat{qw(size resident share trs lrs drs dtp)} = @line;
    }

    close($fh);
    return \%stat;
}

sub _get_stat {
    my ($self, $pid) = @_;
    my $file = $self->{files};
    my %stat = ();

    open my $fh, '<', "$file->{path}/$pid/$file->{stat}"
        or return undef;

    my @line = split /\s+/, <$fh>;

    if (@line < 38) {
        return undef;
    }

    @stat{qw(
        cmd     state   ppid    pgrp    session ttynr   minflt
        cminflt mayflt  cmayflt utime   stime   cutime  cstime
        prior   nice    nlwp    sttime  vsize   nswap   cnswap
        cpu
    )} = @line[1..6,9..19,21..22,35..36,38];

    my $uptime = $self->_uptime;
    my ($d, $h, $m, $s) = $self->_calsec(sprintf('%li', $uptime - $stat{sttime} / 100));
    $stat{actime} = "$d:".sprintf('%02d:%02d:%02d', $h, $m, $s);

    close($fh);
    return \%stat;
}

sub _get_owner {
    my ($self, $pid) = @_;
    my $file = $self->{files};
    my $owner = "N/a";

    open my $fh, '<', "$file->{path}/$pid/$file->{status}"
        or return undef;

    while (my $line = <$fh>) {
        if ($line =~ /^Uid:(?:\s+|\t+)(\d+)/) {
            $owner = getpwuid($1) || "N/a";
            last;
        }
    }

    close($fh);
    return $owner;
}

sub _get_cmdline {
    my ($self, $pid) = @_;
    my $file = $self->{files};

    open my $fh, '<', "$file->{path}/$pid/$file->{cmdline}"
        or return undef;

    my $cmdline = <$fh>;
    close $fh;

    if (!defined $cmdline) {
        $cmdline = "N/a";
    }

    $cmdline =~ s/\0/ /g;
    $cmdline =~ s/^\s+//;
    $cmdline =~ s/\s+$//;
    chomp $cmdline;
    return $cmdline;
}

sub _get_wchan {
    my ($self, $pid) = @_;
    my $file = $self->{files};

    open my $fh, '<', "$file->{path}/$pid/$file->{wchan}"
        or return undef;

    my $wchan = <$fh>;
    close $fh;

    if (!defined $wchan) {
        $wchan = defined;
    }

    chomp $wchan;
    return $wchan;
}

sub _get_io {
    my ($self, $pid) = @_;
    my $file = $self->{files};
    my %stat = ();

    if (open my $fh, '<', "$file->{path}/$pid/$file->{io}") {
        while (my $line = <$fh>) {
            if ($line =~ /^([a-z_]+):\s+(\d+)/) {
                $stat{$1} = $2;
            }
        }

        close($fh);



( run in 4.341 seconds using v1.01-cache-2.11-cpan-524268b4103 )