Sha256: 685499b5c8cf0689b9c0013ebd3c8db0fc72391c3fad58b97f804f558eaf63cf

Contents?: true

Size: 952 Bytes

Versions: 1

Compression:

Stored size: 952 Bytes

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
        @io = IO.new IO.sysopen('/dev/cpu/%d/msr' % processor_id, 'rb')
      rescue Errno::ENOENT
        raise InvalidProcessorIdError, "'#{processor_id}' is not a valid processor_id on this machine"
      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

1 entries across 1 versions & 1 rubygems

Version Path
cpu-0.0.1 lib/cpu/msr.rb