Sha256: 5770f371e0194906ca9322b7ecef97d9ed307debc8ba0d819b55c8b9c456df0e

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

require 'celluloid'

class Eye::SystemResources

  # cached system resources
  class << self

    def memory(pid)
      ps_aux[pid].try :[], :rss
    end

    def cpu(pid)
      ps_aux[pid].try :[], :cpu
    end

    def childs(parent_pid)
      parent_pid = parent_pid.to_i

      childs = []
      ps_aux.each do |pid, h|
        childs << pid if h[:ppid] == parent_pid
      end

      childs
    end

    def cmd(pid)
      ps_aux[pid].try :[], :cmd
    end

    def start_time(pid)
      ps_aux[pid].try :[], :start_time
    end

    def resources(pid)
      return {} unless ps_aux[pid]

      { :memory => memory(pid), 
        :cpu => cpu(pid), 
        :start_time => start_time(pid),
        :cmd => cmd(pid),
        :pid => pid
      }
    end

    # initialize actor, call 1 time before using
    def setup
      @actor ||= PsAxActor.new
    end
    
  private
  
    def ps_aux
      setup
      @actor.get
    end

  end

  class PsAxActor
    include Celluloid

    UPDATE_INTERVAL = 5 # seconds

    def initialize
      set
    end

    def get
      set! if @at + UPDATE_INTERVAL < Time.now
      @ps_aux
    end

  private

    def set
      @ps_aux = Eye::System.ps_aux
      @at = Time.now
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eye-0.2.2 lib/eye/system_resources.rb
eye-0.2.1 lib/eye/system_resources.rb
eye-0.2 lib/eye/system_resources.rb
eye-0.1.11 lib/eye/system_resources.rb