class Facter::Resolvers::Linux::Processors

Constants

MHZ_TO_HZ

Private Class Methods

build_speed(tokens) click to toggle source
# File lib/facter/resolvers/processors.rb, line 71
def build_speed(tokens)
  build_speed_for_power_pc(tokens) if tokens.first.strip == 'clock'
  build_speed_for_x86(tokens) if tokens.first.strip == 'cpu MHz'
end
build_speed_for_power_pc(tokens) click to toggle source
# File lib/facter/resolvers/processors.rb, line 76
def build_speed_for_power_pc(tokens)
  speed = tokens.last.strip.match(/^(\d+).*MHz/)[1]
  @fact_list[:speed] = speed.to_i * MHZ_TO_HZ
end
build_speed_for_x86(tokens) click to toggle source
# File lib/facter/resolvers/processors.rb, line 81
def build_speed_for_x86(tokens)
  speed = tokens.last.strip.match(/^(\d+).*/)[1]
  @fact_list[:speed] = speed.to_i * MHZ_TO_HZ
end
construct_models_list(tokens) click to toggle source
# File lib/facter/resolvers/processors.rb, line 51
def construct_models_list(tokens)
  return unless tokens.first.strip == 'model name' || tokens.first.strip == 'cpu'

  @fact_list[:models] << tokens.last.strip
end
count_physical_processors(tokens) click to toggle source
# File lib/facter/resolvers/processors.rb, line 57
def count_physical_processors(tokens)
  @fact_list[:physical_processors] << tokens.last.strip.to_i if tokens.first.strip == 'physical id'
end
count_processors(tokens) click to toggle source
# File lib/facter/resolvers/processors.rb, line 47
def count_processors(tokens)
  @fact_list[:processors] += 1 if tokens.first.strip == 'processor'
end
physical_devices_count() click to toggle source
# File lib/facter/resolvers/processors.rb, line 61
def physical_devices_count
  Dir.entries('/sys/devices/system/cpu')
     .select { |dir| dir =~ /cpu[0-9]+$/ }
     .select { |dir| File.exist?("/sys/devices/system/cpu/#{dir}/topology/physical_package_id") }
     .map do |dir|
    Facter::Util::FileHelper.safe_read("/sys/devices/system/cpu/#{dir}/topology/physical_package_id").strip
  end
     .uniq.count
end
post_resolve(fact_name, _options) click to toggle source

:count :models :physical_count :speed

# File lib/facter/resolvers/processors.rb, line 19
def post_resolve(fact_name, _options)
  @fact_list.fetch(fact_name) { read_cpuinfo(fact_name) }
end
read_cpuinfo(fact_name) click to toggle source
# File lib/facter/resolvers/processors.rb, line 23
def read_cpuinfo(fact_name)
  cpuinfo_output = Facter::Util::FileHelper.safe_readlines('/proc/cpuinfo')
  return if cpuinfo_output.empty?

  read_processors(cpuinfo_output) # + model names

  @fact_list[:physical_count] = @fact_list[:physical_processors].uniq.length
  @fact_list[:physical_count] = physical_devices_count if @fact_list[:physical_count].zero?
  @fact_list[fact_name]
end
read_processors(cpuinfo_output) click to toggle source
# File lib/facter/resolvers/processors.rb, line 34
def read_processors(cpuinfo_output)
  @fact_list[:processors] = 0
  @fact_list[:models] = []
  @fact_list[:physical_processors] = []
  cpuinfo_output.each do |line|
    tokens = line.split(':')
    count_processors(tokens)
    construct_models_list(tokens)
    count_physical_processors(tokens)
    build_speed(tokens)
  end
end