Sha256: 25faacf4910ddf36758c1290ceb819f13eac2e1e55d80b14f2251dfb5636dbdf

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module God
  module System
    class SlashProcPoller < PortablePoller
      @@kb_per_page = 4 # TODO: Need to make this portable
      @@hertz = 100
      @@total_mem = nil
      
      def initialize(pid)
        super(pid)
        
        unless @@total_mem # in K
          File.open("/proc/meminfo") do |f|
            @@total_mem = f.gets.split[1]
          end
        end
      end
      
      def memory
        stat[:rss].to_i * @@kb_per_page
      end
      
      def percent_memory
        (memory / @@total_mem.to_f) * 100
      end
      
      # TODO: Change this to calculate the wma instead
      def percent_cpu
        stats = stat
        total_time = stats[:utime].to_i + stats[:stime].to_i # in jiffies
        seconds = uptime - stats[:starttime].to_i / @@hertz
        if seconds == 0
          0
        else
          ((total_time * 1000 / @@hertz) / seconds) / 10
        end
      end
      
      private
      
      # in seconds
      def uptime
        File.read('/proc/uptime').split[0].to_f
      end
      
      def stat
        stats = {}
        stats[:pid], stats[:comm], stats[:state], stats[:ppid], stats[:pgrp],
        stats[:session], stats[:tty_nr], stats[:tpgid], stats[:flags],
        stats[:minflt], stats[:cminflt], stats[:majflt], stats[:cmajflt],
        stats[:utime], stats[:stime], stats[:cutime], stats[:cstime],
        stats[:priority], stats[:nice], _, stats[:itrealvalue],
        stats[:starttime], stats[:vsize], stats[:rss], stats[:rlim],
        stats[:startcode], stats[:endcode], stats[:startstack], stats[:kstkesp],
        stats[:kstkeip], stats[:signal], stats[:blocked], stats[:sigignore],
        stats[:sigcatch], stats[:wchan], stats[:nswap], stats[:cnswap],
        stats[:exit_signal], stats[:processor], stats[:rt_priority],
        stats[:policy] = File.read("/proc/#{@pid}/stat").split
        stats
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
god-0.7.6 lib/god/system/slash_proc_poller.rb