Sha256: 96e3dc01a0d5540b1fd051453704ed4c507cfb8ff30faba1bca195fbcca3d48b

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    module Linux
      class Processors < BaseResolver
        @log = Facter::Log.new(self)
        @semaphore = Mutex.new
        @fact_list ||= {}
        class << self
          # :count
          # :models
          # :physical_count

          private

          def post_resolve(fact_name)
            @fact_list.fetch(fact_name) { read_cpuinfo(fact_name) }
          end

          def read_cpuinfo(fact_name)
            return unless File.readable?('/proc/cpuinfo')

            cpuinfo_output = File.read('/proc/cpuinfo')
            read_processors(cpuinfo_output) # + model names

            @fact_list[:physical_count] = @fact_list[:physical_processors].uniq.length
            @fact_list[fact_name]
          end

          def read_processors(cpuinfo_output)
            @fact_list[:processors] = 0
            @fact_list[:models] = []
            @fact_list[:physical_processors] = []
            cpuinfo_output.each_line do |line|
              tokens = line.split(':')
              count_processors(tokens)
              construct_models_list(tokens)
              count_physical_processors(tokens)
            end
          end

          def count_processors(tokens)
            @fact_list[:processors] += 1 if tokens.first.strip == 'processor'
          end

          def construct_models_list(tokens)
            @fact_list[:models] << tokens.last.strip if tokens.first.strip == 'model name'
          end

          def count_physical_processors(tokens)
            @fact_list[:physical_processors] << tokens.last.strip.to_i if tokens.first.strip == 'physical id'
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facter-4.0.15 lib/resolvers/processors_resolver.rb
facter-4.0.14 lib/resolvers/processors_resolver.rb
facter-4.0.13 lib/resolvers/processors_resolver.rb
facter-4.0.12 lib/resolvers/processors_resolver.rb
facter-4.0.11 lib/resolvers/processors_resolver.rb
facter-4.0.10.pre lib/resolvers/processors_resolver.rb