Class: FFWD::Statistics::SystemStatistics

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/ffwd/statistics/system_statistics.rb

Constant Summary collapse

PID_SMAPS_FILE =
'/proc/self/smaps'
PID_STAT_FILE =
'/proc/self/stat'
STAT_FILE =
'/proc/stat'
MEMINFO_FILE =
'/proc/meminfo'

Instance Method Summary collapse

Methods included from Logging

included, #log

Constructor Details

#initialize(opts = {}) ⇒ SystemStatistics

Returns a new instance of SystemStatistics.



97
98
99
# File 'lib/ffwd/statistics/system_statistics.rb', line 97

def initialize opts={}
  @cpu_prev = nil
end

Instance Method Details

#checkObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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_useObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 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_useObject



140
141
142
143
144
145
146
147
148
# 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