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.1'
- VERSION_ARRAY =
:nodoc:
VERSION.split(/\./).map { |x| x.to_i }
- VERSION_MAJOR =
:nodoc:
VERSION_ARRAY[0]
- VERSION_MINOR =
:nodoc:
VERSION_ARRAY[1]
- VERSION_BUILD =
:nodoc:
VERSION_ARRAY[2]
Class Attribute Summary collapse
-
.modprobe_path ⇒ Object
The path to the modprobe binary which is used to load the required module if necessary.
-
.t_j_max ⇒ Object
The t_j_max temperature of that is used as a default for this Processor if it cannot be queried (e.g. Core2 architecture).
Class Method Summary collapse
-
.cores ⇒ Object
Return all Processor instances identified by a distinct core.
-
.each_core(&block) ⇒ Object
Iterate over every Processor instance identified by a distinct core.
-
.each_processor(&block) ⇒ Object
(also: each)
Iterate over each Processor instance for this machine and yield to the block for each of them.
- .load ⇒ Object
-
.num_cores ⇒ Object
Return the number of cores in the processor(s) of this computer.
-
.num_processors ⇒ Object
Return the number of processors in this computer.
-
.processors ⇒ Object
(also: to_a)
Return an array of all Processor instances for this machine.
-
.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. -
.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.
Class Attribute Details
.modprobe_path ⇒ Object
The path to the modprobe binary which is used to load the required module if necessary.
34 35 36 |
# File 'lib/cpu.rb', line 34 def modprobe_path @modprobe_path end |
.t_j_max ⇒ Object
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.
18 19 20 |
# File 'lib/cpu.rb', line 18 def t_j_max @t_j_max end |
Class Method Details
.cores ⇒ Object
Return all Processor instances identified by a distinct core.
82 83 84 |
# File 'lib/cpu.rb', line 82 def cores each_core.to_a end |
.each_core(&block) ⇒ Object
Iterate over every Processor instance identified by a distinct core.
77 78 79 |
# File 'lib/cpu.rb', line 77 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.
88 89 90 |
# File 'lib/cpu.rb', line 88 def each_processor(&block) processors.each(&block) end |
.num_cores ⇒ Object
Return the number of cores in the processor(s) of this computer.
69 70 71 72 |
# File 'lib/cpu.rb', line 69 def num_cores @num_cores or processors @num_cores end |
.num_processors ⇒ Object
Return the number of processors in this computer.
63 64 65 66 |
# File 'lib/cpu.rb', line 63 def num_processors @num_processors or processors @num_processors end |
.processors ⇒ Object Also known as: to_a
Return an array of all Processor instances for this machine.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cpu.rb', line 40 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 Spruz::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.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cpu.rb', line 120 def sum_usage_processor(interval = 1, &block) processors = usage(interval, &block) processor = Processor.new -1, -1 processor.num_processors = processor.num_cores = 1 processor.temperature = processors.map(&:temperature).max 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/cpu.rb', line 103 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 |