CpuUtil (CpuUtil v0.1.0)
CPU utility functions.
Functions to read and calculate CPU utilization for a given process.
NOTE: Only *nix systems supported.
Link to this section Summary
Functions
Calculate CPU utilization give 2 readings.
Get the cup_util for the given os_pid and number of cores.
Get the current OS PID
Read the CPU's average load.
Get the number of CPU Cores.
Get the current OS stat.
Read the OS stat data.
Get the current OS <PID> stat.
Get the total_time from the given list.
Get the total_time.
Link to this section Types
proc_pid_stat()
Specs
proc_pid_stat() :: %{
pid: integer(),
tcomm: binary(),
state: binary(),
ppid: integer(),
pgrp: integer(),
sid: integer(),
tty_nr: integer(),
tty_pgrp: integer(),
flags: integer(),
min_flt: integer(),
cmin_flt: integer(),
maj_flt: integer(),
cmaj_flt: integer(),
utime: integer(),
stime: integer(),
cutime: integer(),
cstime: integer()
}
util_stat()
Specs
util_stat() :: %{total: integer(), stats: proc_pid_stat()}
Link to this section Functions
calc_pid_util(prev, curr, cores \\ 1, precision \\ 1)
Calculate CPU utilization give 2 readings.
Algorithm
user_util = 100 (utime_after - utime_before) / (time_total_after - time_total_before); sys_util = 100 (stime_after - stime_before) / (time_total_after - time_total_before);
Usage
iex> pid = CpuUtil.os_pid()
iex> cores = CpuUtil.num_cores()
iex> u1 = CpuUtil.pid_util(pid)
iex> Process.sleep(1000)
iex> u2 = CpuUtil.pid_util(pid)
iex> CpuUtil.calc_pid_util(u1, u2, cores)
%{
user: 2.0,
sys: 0.5,
total: 2.5
}References
get_cpu_util(pid, cores \\ 1, time \\ 1)
Get the cup_util for the given os_pid and number of cores.
Blocks the calling process for time (1) seconds to collect the before and after samples.
getpid()
Specs
getpid() :: integer()
Get the current OS PID
loadavg(num \\ 1)
Specs
Read the CPU's average load.
num_cores()
Specs
num_cores() :: {:ok, integer()} | :error
Get the number of CPU Cores.
pid_util(pid)
Specs
Get the current OS stat.
- Read the total time from
/proc/stat - Read the PID stats from
/proc/<PID>/stat
Return a map:
%{
total: integer()
stats: proc_pid_stat()
} stat()
Specs
Read the OS stat data.
- Reads
/proc/stat - Pareses the first line ('cpu')
- Converts the numbers (string) to integers
stat(contents)
Specs
stat_pid(pid)
Specs
stat_pid(integer()) :: proc_pid_stat() | {:error, any()}
stat_pid(binary()) :: proc_pid_stat() | {:error, any()}
Get the current OS <PID> stat.
- Read
/proc/<PID>/stat(single line of space separated fields) - Parse the fields and convert and numbers (string) to integers
Returns a map of of the fields (atom keys) per the following definition:
- pid process id
- tcomm filename of the executable
- state state (R is running, S is sleeping, D is sleeping in an
uninterruptible wait, Z is zombie, T is traced or stopped)- ppid process id of the parent process
- pgrp pgrp of the process
- sid session id
- tty_nr tty the process uses
- tty_pgrp pgrp of the tty
- flags task flags
- min_flt number of minor faults
- cmin_flt number of minor faults with child's
- maj_flt number of major faults
- cmaj_flt number of major faults with child's
- utime user mode jiffies
- stime kernel mode jiffies
- cutime user mode jiffies with child's
- cstime kernel mode jiffies with child's
stat_total_time(list)
Specs
Get the total_time from the given list.
Takes the output of CpuUtil.stat/0 and returns the total time.
total_time()
Specs
total_time() :: integer()
Get the total_time.
Return the total time (from /proc/stat) as an integer.