lib/cpu/usage_sampler.rb in cpu-0.0.0 vs lib/cpu/usage_sampler.rb in cpu-0.0.1
- old
+ new
@@ -1,20 +1,29 @@
module CPU
class UsageSampler
+ # Creates a new UsageSampler instance.
def initialize
sample
end
+ # Return all CPU::Usage instances stored in this UsageSampler, if any.
attr_reader :usages
+ # Returns a CPU::Usage instance, that sums up all CPU::Usage instances for
+ # all processors in this computer.
def sum_usages
@usages and @usages.values.inject(&:+)
end
+ # This regular expression is used to parse cpu times from the /proc/stat
+ # kernel file.
CPU = /^cpu(\d+)#{'\s+(\d+)' * 4}/
+ # This value is used to convert cpu times from jiffies into seconds.
USER_HZ = 100
+ # Take one sample of CPU usage data for every processor in this computer
+ # and store them in the +usages+ attribute.
def sample
total, @usages = nil, {}
timestamp = Time.now
File.foreach('/proc/stat') do |line|
case line