Sha256: 53c9c14bdd694b5ebc7d283d64b7d56f5f2dd9eb2e0d9180feb4b708609db5bf

Contents?: true

Size: 1.16 KB

Versions: 7

Compression:

Stored size: 1.16 KB

Contents

module God
  module System
  
    class Process
      def initialize(pid)
        @pid = pid.to_i
      end
      
      # Return true if this process is running, false otherwise
      def exists?
        system("kill -0 #{@pid} &> /dev/null")
      end
      
      # Memory usage in kilobytes (resident set size)
      def memory
        ps_int('rss')
      end
      
      # Percentage memory usage
      def percent_memory
        ps_float('%mem')
      end
      
      # Percentage CPU usage
      def percent_cpu
        ps_float('%cpu')
      end
      
      # Seconds of CPU time (accumulated cpu time, user + system)
      def cpu_time
        time_string_to_seconds(ps_string('time'))
      end
      
      private
      
      def ps_int(keyword)
        `ps -o #{keyword}= -p #{@pid}`.to_i
      end
      
      def ps_float(keyword)
        `ps -o #{keyword}= -p #{@pid}`.to_f
      end
      
      def ps_string(keyword)
        `ps -o #{keyword}= -p #{@pid}`.strip
      end
      
      def time_string_to_seconds(text)
        _, minutes, seconds, useconds = *text.match(/(\d+):(\d{2}).(\d{2})/)
        (minutes.to_i * 60) + seconds.to_i
      end
    end
  
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
god-0.3.0 lib/god/system/process.rb
god-0.4.1 lib/god/system/process.rb
god-0.4.3 lib/god/system/process.rb
god-0.6.0 lib/god/system/process.rb
god-0.4.0 lib/god/system/process.rb
god-0.5.0 lib/god/system/process.rb
god-0.7.0 lib/god/system/process.rb