Class: CPU::MSR

Inherits:
Object
  • Object
show all
Defined in:
lib/cpu/msr.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor_id) ⇒ MSR

Create a new wrapper for the msr kernel file associated with processor_id.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cpu/msr.rb', line 17

def initialize(processor_id)
  self.class.available? or self.class.load_module
  begin
    name = '/dev/cpu/%d/msr' % processor_id
    @io = IO.new IO.sysopen(name, 'rb')
  rescue Errno::ENOENT
    raise InvalidProcessorIdError, "'#{processor_id}' is not a valid processor_id on this machine"
  rescue StandardError => e
    raise NoSampleDataError, "could not read temperature from #{name}: #{e}"
  end
end

Class Method Details

.available?Boolean

Returns true if the msr functionality is already available in the kernel (either compiled into it or via a module).

Returns:

  • (Boolean)


5
6
7
# File 'lib/cpu/msr.rb', line 5

def self.available?
  File.exist?('/dev/cpu/0/msr')
end

.load_moduleObject

Loads the msr module and sleeps for a second afterwards.



10
11
12
13
# File 'lib/cpu/msr.rb', line 10

def self.load_module
  system "#{CPU.modprobe_path} msr"
  sleep 1
end

Instance Method Details

#[](offset) ⇒ Object

Returns the byte at offset as an integer number.



30
31
32
33
34
# File 'lib/cpu/msr.rb', line 30

def [](offset)
  @io.sysseek(offset)
  data, = @io.sysread(8).unpack('q')
  data
end