Sha256: c47c3b190494915da62782df35b52d2380ffb3c88aa845189cfe9d0f2abe8567
Contents?: true
Size: 1.36 KB
Versions: 5
Compression:
Stored size: 1.36 KB
Contents
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 when /^cpu[^\d]/ next when CPU times = $~.captures processor_id = times[0] = times[0].to_i (1...times.size).each { |i| times[i] = times[i].to_f / USER_HZ } times << timestamp << timestamp @usages[processor_id] = Usage.new(self, *times) else break end end if @usages.empty? raise NoSampleDataError, "could not sample measurement data" end self end end end
Version data entries
5 entries across 5 versions & 1 rubygems
Version | Path |
---|---|
cpu-0.0.6 | lib/cpu/usage_sampler.rb |
cpu-0.0.4 | lib/cpu/usage_sampler.rb |
cpu-0.0.3 | lib/cpu/usage_sampler.rb |
cpu-0.0.2 | lib/cpu/usage_sampler.rb |
cpu-0.0.1 | lib/cpu/usage_sampler.rb |