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

Link to this type

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()
}

Specs

util_stat() :: %{total: integer(), stats: proc_pid_stat()}

Link to this section Functions

Link to this function

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

Link to this function

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.

Specs

getpid() :: integer()

Get the current OS PID

Link to this function

loadavg(num \\ 1)

Specs

loadavg(integer()) :: binary()

Read the CPU's average load.

Specs

num_cores() :: {:ok, integer()} | :error

Get the number of CPU Cores.

Specs

pid_util(integer()) :: util_stat()

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()
}

Specs

stat() :: list() | {:error, any()}

Read the OS stat data.

  • Reads /proc/stat
  • Pareses the first line ('cpu')
  • Converts the numbers (string) to integers

Specs

stat(binary()) :: list() | {:error, any()}

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
Link to this function

stat_total_time(list)

Specs

stat_total_time(list()) :: integer()

Get the total_time from the given list.

Takes the output of CpuUtil.stat/0 and returns the total time.

Specs

total_time() :: integer()

Get the total_time.

Return the total time (from /proc/stat) as an integer.