Module: CPU

Extended by:
Enumerable
Defined in:
lib/cpu.rb,
lib/cpu/msr.rb,
lib/cpu/load.rb,
lib/cpu/usage.rb,
lib/cpu/shared.rb,
lib/cpu/version.rb,
lib/cpu/processor.rb,
lib/cpu/usage_sampler.rb

Overview

This module provides (read) access to the Model Specific Registers of Intel CPUs on Linux.

Defined Under Namespace

Modules: Shared Classes: CPUError, InvalidProcessorIdError, Load, MSR, NoSampleDataError, Processor, Usage, UsageSampler

Constant Summary collapse

VERSION =

CPU version

'0.0.6'
VERSION_ARRAY =

:nodoc:

VERSION.split('.').map(&:to_i)
VERSION_MAJOR =

:nodoc:

VERSION_ARRAY[0]
VERSION_MINOR =

:nodoc:

VERSION_ARRAY[1]
VERSION_BUILD =

:nodoc:

VERSION_ARRAY[2]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.modprobe_pathObject

The path to the modprobe binary which is used to load the required module if necessary.



35
36
37
# File 'lib/cpu.rb', line 35

def modprobe_path
  @modprobe_path
end

.t_j_maxObject

The t_j_max temperature of that is used as a default for this Processor if it cannot be queried (e.g. Core2 architecture). It defaults to 95 which is the correct value for Core2 Duo E8400 (Wolfsdale). Be sure to set the correct value for your Core2 CPU here, otherwise your temperature measurements will be incorrect.



19
20
21
# File 'lib/cpu.rb', line 19

def t_j_max
  @t_j_max
end

Class Method Details

.coresObject

Return all Processor instances identified by a distinct core.



83
84
85
# File 'lib/cpu.rb', line 83

def cores
  each_core.to_a
end

.each_core(&block) ⇒ Object

Iterate over every Processor instance identified by a distinct core.



78
79
80
# File 'lib/cpu.rb', line 78

def each_core(&block)
  processors.uniq_by(&:core_id).each(&block)
end

.each_processor(&block) ⇒ Object Also known as: each

Iterate over each Processor instance for this machine and yield to the block for each of them.



89
90
91
# File 'lib/cpu.rb', line 89

def each_processor(&block)
  processors.each(&block)
end

.loadObject



97
98
99
# File 'lib/cpu.rb', line 97

def load
  Load.new
end

.num_coresObject

Return the number of cores in the processor(s) of this computer.



70
71
72
73
# File 'lib/cpu.rb', line 70

def num_cores
  @num_cores or processors
  @num_cores
end

.num_processorsObject

Return the number of processors in this computer.



64
65
66
67
# File 'lib/cpu.rb', line 64

def num_processors
  @num_processors or processors
  @num_processors
end

.processorsObject Also known as: to_a

Return an array of all Processor instances for this machine.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cpu.rb', line 41

def processors
  cpu_cores = {}
  cpuinfos = File.read('/proc/cpuinfo').chomp.split(/^$/)
  processor_ids =
    cpuinfos.map { |l| l[/processor\s*:\s*(\d+)/, 1].to_i rescue nil }
  core_ids = cpuinfos.map { |l| l[/core id\s*:\s*(\d+)/, 1].to_i rescue nil }
  processor_ids.zip(core_ids) do |processor_id, core_id|
    cpu_cores[processor_id] = core_id
  end
  processors = Dir.open('/dev/cpu').inject([]) do |ps, processor_id|
    processor_id =~ /\A\d+\Z/ or next ps
    ps << processor_id.to_i
  end
  processors.extend Tins::UniqBy
  processors.sort!
  @num_processors = processors.size
  @num_cores      = cpu_cores.invert.size
  processors.map! do |processor_id|
    Processor.new(processor_id, cpu_cores[processor_id])
  end
end

.sum_usage_processor(interval = 1, &block) ⇒ Object

Return a single Processor instance, measure CPU usage during the next interval seconds or during the runtime of the given block, and then sum up the CPU usage in this instance.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cpu.rb', line 121

def sum_usage_processor(interval = 1, &block)
  processors = usage(interval, &block)
  processor = Processor.new(-1, -1)
  processor.num_processors = processor.num_cores = 1
  begin
    processor.temperature = processors.map(&:temperature).max
  rescue NoSampleDataError
    processor.temperature = nil
  end
  processor.usage = processors.map(&:usage).inject { |s, u| s + u }
  processor.usage.num_processors = processor.usage.num_cores = 1
  processor.freeze
  processor
end

.usage(interval = 1) ⇒ Object

Return an array of all Processor instances for this machine, but also measure CPU usage during the next interval seconds or during the runtime of the given block.



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cpu.rb', line 104

def usage(interval = 1)
  before_usage = UsageSampler.new
  if block_given?
    yield
  else
    sleep interval
  end
  after_usage = UsageSampler.new
  processors.each do |processor|
    processor.usage = after_usage.usages[processor.processor_id] -
      before_usage.usages[processor.processor_id]
  end
end