sg_get_process_stats(3)

NAME
sg_get_process_stats, sg_get_process_stats_r, sg_get_process_count, sg_get_process_count_of, sg_get_process_count_r, sg_free_process_count, sg_process_compare_name, sg_process_compare_pid, sg_process_compare_uid, sg_process_compare_gid, sg_process_compare_size, sg_process_compare_res, sg_process_compare_cpu, sg_process_compare_time - get process statistics

SYNOPSIS
\FC#include <statgrab.h>\F[]

\FCsg_process_stats *sg_get_process_stats\F[]
\FC(size_t *entries);\F[]

\FCsg_process_stats *sg_get_process_stats_r\F[]
\FC(size_t *entries);\F[]

\FCsg_error sg_free_process_stats\F[]
\FC(sg_process_stats *data);\F[]

\FCsg_process_count *sg_get_process_count\F[]
\FC(void);\F[]

\FCsg_process_count *sg_get_process_count_of\F[]
\FC(sg_process_count_source pcs);\F[]

\FCsg_process_count *sg_get_process_count_r\F[]
\FC(sg_process_stats const *whereof);\F[]

\FCsg_error sg_free_process_count\F[]
\FC(sg_process_count *data);\F[]

\FCint sg_process_compare_name\F[]
\FC(const void *va, const void *vb);\F[]

\FCint sg_process_compare_pid\F[]
\FC(const void *va, const void *vb);\F[]

\FCint sg_process_compare_uid\F[]
\FC(const void *va, const void *vb);\F[]

\FCint sg_process_compare_gid\F[]
\FC(const void *va, const void *vb);\F[]

\FCint sg_process_compare_size\F[]
\FC(const void *va, const void *vb);\F[]

\FCint sg_process_compare_res\F[]
\FC(const void *va, const void *vb);\F[]

\FCint sg_process_compare_cpu\F[]
\FC(const void *va, const void *vb);\F[]

\FCint sg_process_compare_time\F[]
\FC(const void *va, const void *vb);\F[]

DESCRIPTION
The \FCsg_get_process_stats\F[] functions provide statistics about the currently running processes. Both functions, \FCsg_get_process_stats\F[]() and \FCsg_get_process_stats_r\F[](), take an optional \FCentries\F[] parameter, which points (when given) to a size_t to take the number of returned vector entries.

The functions \FCsg_get_process_count_of\F[]() and \FCsg_get_process_count_r\F[]() provide an aggregated view of the process table - they deliver the amount of processes per process state. The \FCsg_get_process_count\F[]() is in fact a preprocessor macro for backward compatibility and calls \FCsg_get_process_count_of\F[]() with the parameter \FCpcs\F[] of sg_entire_process_count to emulate the behavior until 0.17.

API Shortcut
function | returns | data owner
sg_get_process_stats | \FCsg_process_stats\F[] * | libstatgrab (thread local)
sg_get_process_stats_r | \FCsg_process_stats\F[] * | caller
sg_get_process_count_of | \FCsg_process_count\F[] * | libstatgrab (thread local)
sg_get_process_count_r | \FCsg_process_count\F[] * | caller

The \FCsg_process_stats\F[] vectors received from \FCsg_get_process_stats_r\F[]() or the \FCsg_process_count\F[] summaries received from \FCsg_get_process_count_r\F[] must be freed using \FCsg_free_process_stats\F[]() or \FCsg_free_process_count\F[](), respectively. The caller is responsible for doing it when the data isn't needed any more.

\FCsg_process_compare_name\F[]
\FCsg_process_compare_pid\F[]
\FCsg_process_compare_uid\F[]
\FCsg_process_compare_gid\F[]
\FCsg_process_compare_size\F[]
\FCsg_process_compare_res\F[]
\FCsg_process_compare_cpu\F[]
\FCsg_process_compare_time\F[]

These functions compare two sg_process_stats entries, and return an int to represent which one is greater. The main use of these functions is to be passed to qsort to sort the sg_process_stats by the given type.

Example

\FC
size_t entries;
sg_process_stats *proc_stats = NULL;
while( NULL != ( proc_stats = sg_get_process_stats_r(&entries) ) ) {
    /* order entries by comparing the process identifier */
    qsort( proc_stats, entries, sizeof(proc_stats[0]), &sg_process_compare_pid );
    show_proc_stats( proc_stats );
    sg_free_process_stats( proc_stats );
}
        \F[]

RETURN VALUES
The structure returned by sg_get_process_stats is of type \FCsg_process_stats\F[].

\FC
typedef struct {
        char *process_name;
        char *proctitle;

        pid_t pid;    /* process identifier */
        pid_t parent; /* Parent pid */
        pid_t pgid;   /* process id of process group leader */
        pid_t sessid; /* session id of the session the process belongs to */

        uid_t uid;
        uid_t euid;
        gid_t gid;
        gid_t egid;

        unsigned long long context_switches;
        unsigned long long voluntary_context_switches;
        unsigned long long involuntary_context_switches;
        unsigned long long proc_size; /* in bytes */
        unsigned long long proc_resident; /* in bytes */
        time_t start_time; /* When was the process started */
        time_t time_spent; /* time running in seconds */
        double cpu_percent;
        int nice;
        sg_process_state state;

        time_t systime;
} sg_process_stats;
    \F[]

\FC
typedef enum {
        SG_PROCESS_STATE_RUNNING,
        SG_PROCESS_STATE_SLEEPING,
        SG_PROCESS_STATE_STOPPED,
        SG_PROCESS_STATE_ZOMBIE,
        SG_PROCESS_STATE_UNKNOWN
} sg_process_state;
    \F[]

\FCprocess_name\F[]
    The name of the command that was run. The content of this field heavily depends on the underlying operating system, some store the basename the executable passes to the \FCexec\F[](2) system call, some the entire path. Most OS restrict the size of this field - some like the *BSD family to a very low value of 15 bytes.

    This field is usually immutable for userland processes.

\FCproctitle\F[]
    The command line (the "title") of the process. Take note - this can be modified by the process, so isn't guaranteed to be the original command line.

\FCpid\F[]
    The process ID.

\FCparent\F[]
    The parent process ID.

\FCpgid\F[]
    The process ID of the process group leader.

\FCsessid\F[]
    Session id of the session the process belongs to.

\FCuid\F[]
    The ID of the user the process is running as.

\FCeuid\F[]
    The ID of the effective user the process is running as.

\FCgid\F[]
    The ID of the group the process is running as.

\FCegid\F[]
    The ID of the effective group the process is running as.

\FCcontext_switches\F[]
    The number of context switches of this process (voluntary and involuntary).

\FCvoluntary_context_switches\F[]
    The number of voluntary context switches of this process (eg. by calling \FCsched_yield\F[]() or \FCsleep\F[]()).

\FCinvoluntary_context_switches\F[]
    The number of involuntary context switches of this process (eg. time slice exhausted or signal sent).

\FCproc_size\F[]
    The virtual memory size of the process in bytes.

\FCproc_resident\F[]
    The size of the process that's resident in memory.

\FCstart_time\F[]
    The time when the process has been started in seconds since epoch.

\FCtime_spent\F[]
    The number of seconds the process has been running (user+system time, without time spent by child processes).

\FCcpu_percent\F[]
    The current percentage of CPU the process is using.

\FCnice\F[]
    The nice value of the process.

\FCstate\F[]
    The current state of the process. See sg_process_state for permitted values.

\FCsystime\F[]
    The time in seconds since epoch of the moment where the present statistic has been created. This might be (but doesn't have to be) the same moment for all returned entries, regardless whether they're fetched with one snapshot or puzzled from some kind of procfs.

The structure returned by sg_get_process_count_of and sg_get_process_count_r is of type \FCsg_process_count\F[].

\FC
typedef enum sg_process_count_source {
        sg_entire_process_count,
        sg_last_process_count
} sg_process_count_source;
    \F[]

\FC
typedef struct{
        unsigned long long total;
        unsigned long long running;
        unsigned long long sleeping;
        unsigned long long stopped;
        unsigned long long zombie;
        unsigned long long unknown;

        time_t systime;
}sg_process_count;
    \F[]

\FCtotal\F[]
    The total number of processes.

\FCrunning\F[]
    The number of running processes.

\FCsleeping\F[]
    The number of sleeping processes.

\FCstopped\F[]
    The number of stopped processes.

\FCzombie\F[]
    The number of zombie processes.

\FCunknown\F[]
    The number of processes not matching any of above named categories.

\FCsystime\F[]
    The time in seconds since epoch of the moment where the present statistic has been created.

BUGS
The very first call of \FCsg_get_process_count_of\F[](sg_last_process_count) will return the same as \FCsg_get_process_count_of\F[](sg_entire_process_count).

The compare functions exist rather for backward compatibility than for functionality enhancements. Limited flexibility (e.g. reverse order) and lack of optimising opportunities for the compiler leads to the recommendation to implement the required compare routines locally.

SEE ALSO
statgrab(3)

WEBSITE
⟨https://libstatgrab.org/⟩