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 30, 2024
1 parent 81a6e12 commit a09d504
Show file tree
Hide file tree
Showing 3 changed files with 51 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
22 changes: 22 additions & 0 deletions lib/facter/resolvers/processors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'set'

module Facter
module Resolvers
module Linux
Expand All @@ -12,6 +14,7 @@ class Processors < BaseResolver

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

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

def count_processors(tokens)
Expand Down Expand Up @@ -84,6 +91,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].add('x86_64-v1') if (%w[cmov cx8 fpu fxsr lm mmx syscall sse2] - flags).empty?
@fact_list[:extensions].add('x86_64-v2') if (%w[cx16 lahf_lm popcnt sse4_1 sse4_2 ssse3] - flags).empty?
@fact_list[:extensions].add('x86_64-v3') if (%w[abm avx avx2 bmi1 bmi2 f16c fma movbe xsave] - flags).empty?
@fact_list[:extensions].add('x86_64-v4') if (%w[avx512f avx512bw avx512cd avx512dq avx512vl] - flags).empty?
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/facter/resolvers/processors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
end

let(:speed) { 2_294_000_000 }
let(:extensions) do
%w[x86_64 x86_64-v1 x86_64-v2 x86_64-v3]
end

it 'returns number of processors' do
result = Facter::Resolvers::Linux::Processors.resolve(:processors)
Expand All @@ -45,6 +48,16 @@

expect(result).to eq(speed)
end

it 'returns extensions supported' do
allow(Facter::Resolvers::Uname).to receive(:resolve)
.with(:processor)
.and_return('x86_64')

result = Facter::Resolvers::Linux::Processors.resolve(:extensions)

expect(result).to eq(extensions)
end
end

context 'when cpuinfo file is readable but no physical id' do
Expand Down

0 comments on commit a09d504

Please sign in to comment.