Class: CPU::Processor

Inherits:
Object
  • Object
show all
Includes:
Shared
Defined in:
lib/cpu/processor.rb

Instance Attribute Summary collapse

Attributes included from Shared

#num_cores, #num_processors

Instance Method Summary collapse

Constructor Details

#initialize(processor_id, core_id = nil) ⇒ Processor

Returns a Processor instance for Processor processor_id. If this Processor doesn’t exist an InvalidProcessorIdError exception is thrown.



7
8
9
# File 'lib/cpu/processor.rb', line 7

def initialize(processor_id, core_id = nil)
  @processor_id, @core_id = processor_id, core_id
end

Instance Attribute Details

#core_idObject (readonly)

Returns the core_id of this Processor.



16
17
18
# File 'lib/cpu/processor.rb', line 16

def core_id
  @core_id
end

#processor_idObject (readonly)

Returns the processor_id of this Processor, an integer in (0..(n - 1)) for a n-core machine.



13
14
15
# File 'lib/cpu/processor.rb', line 13

def processor_id
  @processor_id
end

#temperatureObject

Returns the core temperature of this Processor as an integer number. This should work on all Core2 architecures if you set CPU.t_j_max to the correct value for your Processor. On i7 architectures (and newer?) it should work without any further configuration.



58
59
60
61
62
63
64
65
# File 'lib/cpu/processor.rb', line 58

def temperature
  if @temperature
    @temperature
  else
    my_t_j_max = t_j_max.nonzero? || CPU.t_j_max
    my_t_j_max - t_j_max_distance
  end
end

#usage(interval = 1, &block) ⇒ Object

Measure CPU usage for this Processor instance during the next interval or during the runtime of the given block. Іf CPU usage on all processors of the system should be measured, it’s better (=faster) to use the CPU.usage method instead.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cpu/processor.rb', line 29

def usage(interval = 1, &block)
  unless @usage
    if processor = CPU.usage(interval, &block).find {
      |p| p.processor_id == processor_id
    }
    then
      @usage = processor.usage
    end
  end
  @usage
end

Instance Method Details

#inspectObject



69
70
71
72
73
74
75
76
77
# File 'lib/cpu/processor.rb', line 69

def inspect
  if processor_id >= 0
    result = "#<#{self.class}: #@processor_id"
    result << " (core#@core_id)" if @core_id
    result << '>'
  else
    "#<#{self.class}: total>"
  end
end

#msrObject

Returns an msr object and caches it.



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

def msr
  @msr ||= MSR.new(processor_id)
end

#t_j_maxObject

This method returns the t_j_max temperature of this Processor if the processor supports it (e. g. Intel i7 architecture) as an integer number, otherwise 0 is returned.



50
51
52
# File 'lib/cpu/processor.rb', line 50

def t_j_max
  (msr[0x1a2] >> 16) & 0x7f
end

#t_j_max_distanceObject

Returns the distance between the core temperature of this Processor and its t_j_max temperature as an integer number.



43
44
45
# File 'lib/cpu/processor.rb', line 43

def t_j_max_distance
  (msr[0x19c] >> 16) & 0x7f
end