The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Linux::Info::Processes - Collect Linux process statistics.

VERSION

version 2.16

SYNOPSIS

    use Linux::Info::Processes;

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

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

PROCESS STATISTICS

Generated by /proc/<pid>/stat, /proc/<pid>/status, /proc/<pid>/cmdline and getpwuid().

Note that if /etc/passwd isn't readable, the key owner is set to 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.

statm

Generated by /proc/<pid>/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. For example, if the pagesize of your system is 4kb:

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

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

io

Generated by /proc/<pid>/io.

Permissions on this file have changed with versions of the kernel, opt out from trying to read the file by setting the file to a false value, like:

    files => { io => q{} }
  • rchar: bytes read from storage (might have been from pagecache).

  • wchar: bytes written.

  • syscr: number of read syscalls.

  • syscw: number of write syscalls.

  • read_bytes: bytes really fetched from storage layer.

  • write_bytes: bytes sent to the storage layer.

  • cancelled_write_bytes: refer to docs.

limits

Generated by /proc/<pid>/limits.

Often readable only by self and root, opt in to trying to read the file by setting the file to limits, like:

    files => { limits => 'limits' }

An array with (soft_limit, hard_limit, units) is provided for the limits listed.

This may vary between kernels. Some examples are:

  • max_address_space: the maximum amount of virtual memory available to the shell.

  • max_core_file_size: the maximum size of core files created.

  • max_processes: the maximum number of processes available to a single user.

  • max_open_files: the maximum number of open file descriptors.

See Documentation/filesystems/proc.txt for more information.

METHODS

new()

Call new() to create a new object.

    my $lxs = Linux::Info::Processes->new;

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

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

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

    my $lxs = Linux::Info::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',
            limits  => 'limits',
        }
    );

If you want to enable io and limits information about the processes, you need to enabled it explicity:

    my $lxs = Linux::Info::Processes->new(enabled => {
        io => 1, limits => 1
    });

Remember that the process executing Linux::Info::Processes requires rights to read io and limits.

init()

Call init() to initialize the statistics.

    $lxs->init;

get()

Call get() to get the statistics. 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.

raw()

Get raw values.

EXPORTS

Nothing.

SEE ALSO

AUTHOR

Alceu Rodrigues de Freitas Junior <glasswalk3r@yahoo.com.br>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2015 by Alceu Rodrigues de Freitas Junior.

This is free software, licensed under:

  The GNU General Public License, Version 3, June 2007