Skip to content

Commit

Permalink
feat(processor.extensions) Add support for CPU flag detection
Browse files Browse the repository at this point in the history
This should add in an extra set of attributes for finding your microarch
or CPU extensions.

Signed-off-by: Pat Riehecky <[email protected]>
  • Loading branch information
jcpunk committed Jan 9, 2024
1 parent 81a6e12 commit b0a0915
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/facter/facts/linux/processors/extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Facts
module Linux
module Processors
class Extensions
FACT_NAME = 'processors.extensions'

def call_the_resolver
fact_value = Facter::Resolvers::Linux::Processors.resolve(:extensions)
Facter::ResolvedFact.new(FACT_NAME, fact_value)
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/facter/resolvers/processors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Processors < BaseResolver

class << self
# :count
# :extensions
# :models
# :physical_count
# :speed
Expand All @@ -34,6 +35,7 @@ def read_cpuinfo(fact_name)
end

def read_processors(cpuinfo_output)
@fact_list[:extensions] = [Facter::Resolvers::Uname.resolve(:processor)]
@fact_list[:processors] = 0
@fact_list[:models] = []
@fact_list[:physical_processors] = []
Expand All @@ -43,7 +45,9 @@ def read_processors(cpuinfo_output)
construct_models_list(tokens)
count_physical_processors(tokens)
build_speed(tokens)
check_extensions(tokens)
end
@fact_list[:extensions].sort!.uniq!
end

def count_processors(tokens)
Expand Down Expand Up @@ -84,6 +88,21 @@ def build_speed_for_x86(tokens)
speed = tokens.last.strip.match(/^(\d+).*/)[1]
@fact_list[:speed] = speed.to_i * MHZ_TO_HZ
end

def check_extensions(tokens)
return unless tokens.first.strip == 'flags'

flags = tokens.last.split(' ')

# TODO: As we gain support for other arches, change the guard
# so we only check the flags for the corosponding arches
return unless @fact_list[:extensions].include?('x86_64')

@fact_list[:extensions].append('x86_64-v1') if (%w[cmov cx8 fpu fxsr lm mmx syscall sse2] - flags).empty?
@fact_list[:extensions].append('x86_64-v2') if (%w[cx16 lahf_lm popcnt sse4_1 sse4_2 ssse3] - flags).empty?
@fact_list[:extensions].append('x86_64-v3') if (%w[avx avx2 bmi1 bmi2 f16c fma abm movbe xsave] - flags).empty?
@fact_list[:extensions].append('x86_64-v4') if (%w[avx512f avx512bw avx512cd avx512dq avx512vl] - flags).empty?
end
end
end
end
Expand Down

0 comments on commit b0a0915

Please sign in to comment.