struct Hardware::PID

Overview

All informations related to the Processes running on your sytem, defined by Process IDentifiers.

pid = Hardware::PID.new           # Default is Process.pid
app = Hardware::PID.new "firefox" # Take the first matching PID

loop do
  sleep 1
  pid.cpu_usage      # => 1.5
  app.cpu_usage.to_i # => 4
end

Defined in:

hardware/pid.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(pid : Int32 = Process.pid, cpu_total : Bool = true) #

Creates a new Hardware::PID Set to false to avoid setting #cpu_total_current (useful if lots of Hardware::PID are used)


[View source]
def self.new(executable : String, cpu_total = true) #

Creates a new Hardware::PID by finding the executable's pid.


[View source]

Class Method Detail

def self.all(cpu_total = false, &) : Nil #

Yields a Hardware::PID for each PID present on the system.


[View source]
def self.cpu_total_current : Int32 #

Used to avoid duplicate operations when lots of Hardware::PID are created (like a top implementation)


[View source]
def self.cpu_total_current=(cpu_total_current : Int32) #

Used to avoid duplicate operations when lots of Hardware::PID are created (like a top implementation)


[View source]
def self.get_pids(executable : String) : Array(Int32) #

Return all pids corresponding of a given executable name.


[View source]

Instance Method Detail

def cmdline : String #

Returns /proc/``#pid``/cmdline.


[View source]
def command : String #

Returns a String representation of /proc/``#pid``/cmdline.


[View source]
def cpu_time(children = false) : Int32 #

Returns the CPU time without including ones from children processes.


[View source]
def cpu_time_previous : Int32 #

Previous #cpu_time.


[View source]
def cpu_time_previous=(cpu_time_previous : Int32) #

Previous #cpu_time.


[View source]
def cpu_total_previous : Int32 #

Previous CPU.new.total.


[View source]
def cpu_total_previous=(cpu_total_previous : Int32) #

Previous CPU.new.total.


[View source]
def cpu_usage : Float32 #

Returns the CPU used in percentage.


[View source]
def exe : String #

Returns /proc/``#pid``/exe if readable.


[View source]
def exists? : Bool #

[View source]
def memory : Int32 #

Returns the actual memory used by the process.


[View source]
def name : String #

Returns the PID name based on #exe or #cmdline.


[View source]
def net : Net #

Returns Hardware::Net for #pid


[View source]
def pid : Int32 #

Pid number


[View source]
def stat : Stat #

Returns a parsed /proc/``#pid``/stat.


[View source]
def statm : Array(Int32) #

Returns a parsed /proc/``#pid``/statm.


[View source]
def status : Hash(String, String) #

Returns a parsed /proc/``#pid``/status.


[View source]