Sha256: 9cb93d0fd330656c9d30abde11c7ca072df3011b47a4c0a5ac187fe07f0f34e0

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module CPU
  class MSR
    # Returns true if the msr functionality is already available in the kernel
    # (either compiled into it or via a module).
    def self.available?
      File.exist?('/dev/cpu/0/msr')
    end

    # Loads the msr module and sleeps for a second afterwards.
    def self.load_module
      system "#{CPU.modprobe_path} msr"
      sleep 1
    end

    # Create a new wrapper for the msr kernel file associated with
    # +processor_id+.
    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

    # Returns the byte at +offset+ as an integer number.
    def [](offset)
      @io.sysseek(offset)
      data, = @io.sysread(8).unpack('q')
      data
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cpu-0.0.6 lib/cpu/msr.rb
cpu-0.0.4 lib/cpu/msr.rb
cpu-0.0.3 lib/cpu/msr.rb
cpu-0.0.2 lib/cpu/msr.rb