class FFWD::Statistics::SystemStatistics
Constants
- MEMINFO_FILE
- PID_SMAPS_FILE
- PID_STAT_FILE
- STAT_FILE
Public Class Methods
new(opts={})
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 97 def initialize opts={} @cpu_prev = nil end
Public Instance Methods
check()
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 116 def check if not File.file? PID_SMAPS_FILE log.error "File does not exist: #{PID_SMAPS_FILE} (is this a linux system?)" return false end if not File.file? PID_STAT_FILE log.error "File does not exist: #{PID_STAT_FILE} (is this a linux system?)" return false end if not File.file? STAT_FILE log.error "File does not exist: #{STAT_FILE} (is this a linux system?)" return false end if not File.file? MEMINFO_FILE log.error "File does not exist: #{MEMINFO_FILE} (is this a linux system?)" return false end return true end
collect(channel) { |"cpu-#{key}", "%", value| ... }
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 101 def collect channel cpu = cpu_use memory = memory_use cpu.each do |key, value| yield "cpu-#{key}", "%", value end memory.each do |key, value| yield "memory-#{key}", "B", value end channel << {:memory => memory} end
cpu_use()
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 150 def cpu_use stat = read_pid_stat current = { :system => stat[:stime], :user => stat[:utime], :total => read_stat_total } prev = @cpu_prev if @cpu_prev.nil? @cpu_prev = prev = current else @cpu_prev = current end total = current[:total] - prev[:total] system = current[:system] - prev[:system] user = current[:user] - prev[:user] if total == 0 return {:user => 0, :system => 0} end {:user => (user / total).round(3), :system => (system / total).round(3)} end
memory_use()
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 140 def memory_use result = {:resident => 0, :total => read_total_memory} read_smaps do |smap| result[:resident] += smap.rss end result end
Private Instance Methods
read_pid_stat()
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 180 def read_pid_stat File.open(PID_STAT_FILE) do |f| stat = f.readline.split(' ').map(&:strip) return {:utime => stat[13].to_i, :stime => stat[14].to_i} end end
read_smaps() { |smap| ... }
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 207 def read_smaps File.open(PID_SMAPS_FILE) do |f| smap = {} loop do break if f.eof? unless smap.empty? yield SMAP.new(smap) smap = {} end loop do break if f.eof? line = f.readline.strip case line when /^[0-9a-f]+-[0-9a-f]+ / break else key, value = line.split(':', 2) next unless SMAP::KEY_MAPPING[key] smap[key] = (SMAP::TYPE_MAP[key] || SMAP::DEFAULT_TYPE).call(value.strip) end end end end end
read_stat_total()
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 187 def read_stat_total File.open(STAT_FILE) do |f| f.each do |line| next unless line.start_with? "cpu " stat = line.split(' ').map(&:strip).map(&:to_i) return stat.reduce(&:+) end end end
read_total_memory()
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 197 def read_total_memory File.open(MEMINFO_FILE) do |f| f.each do |line| next unless line.start_with? "MemTotal:" total = line.split(' ').map(&:strip) return total[1].to_i * 1000 end end end
smaps_read_entry(f)
click to toggle source
# File lib/ffwd/statistics/system_statistics.rb, line 237 def smaps_read_entry f result = {} loop do break if f.eof? line = f.readline.strip case line when /^[0-9a-f]+-[0-9a-f]+ / break else result[key] = (SMAP::TYPE_MAP[key] || SMAP::DEFAULT_TYPE).call(value.strip) end end result end