Sha256: 9d0a5f19e933b72190405b89defb7f49d555439b6ca7e45a1987fe236e8f7578

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

module CPU
  class Processor
    include Shared

    # Returns a Processor instance for Processor +processor_id+. If this
    # Processor doesn't exist an InvalidProcessorIdError exception is thrown.
    def initialize(processor_id, core_id = nil)
      @processor_id, @core_id = processor_id, core_id
    end

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

    # Returns the core_id of this Processor.
    attr_reader :core_id

    # Returns an msr object and caches it.
    def msr
      @msr ||= MSR.new(processor_id)
    end

    attr_writer :usage

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

    # Returns the distance between the core temperature of this Processor and
    # its t_j_max temperature as an integer number.
    def t_j_max_distance
      (msr[0x19c] >> 16) & 0x7f
    end

    # 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.
    def t_j_max
      (msr[0x1a2] >> 16) & 0x7f
    end

    # 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.
    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

    attr_writer :temperature

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cpu-0.0.0 lib/cpu/processor.rb