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
Signed-off-by: Pat Riehecky <[email protected]>
  • Loading branch information
jcpunk committed Feb 13, 2024
1 parent f0a6704 commit a513dbb
Show file tree
Hide file tree
Showing 5 changed files with 68 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
1 change: 1 addition & 0 deletions lib/facter/framework/core/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@
require_relative '../../facts/linux/processor'
require_relative '../../facts/linux/processors/cores'
require_relative '../../facts/linux/processors/count'
require_relative '../../facts/linux/processors/extensions'
require_relative '../../facts/linux/processors/isa'
require_relative '../../facts/linux/processors/models'
require_relative '../../facts/linux/processors/physicalcount'
Expand Down
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
25 changes: 25 additions & 0 deletions spec/facter/resolvers/processors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe Facter::Resolvers::Linux::Processors do
after do
Facter::Resolvers::Linux::Processors.invalidate_cache
Facter::Resolvers::Uname.invalidate_cache
end

context 'when on x86 architecture' do
Expand All @@ -21,6 +22,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 +49,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 Expand Up @@ -73,6 +87,7 @@

after do
Facter::Resolvers::Linux::Processors.invalidate_cache
Facter::Resolvers::Uname.invalidate_cache
end

let(:physical_processors) { 2 }
Expand Down Expand Up @@ -129,6 +144,11 @@
.and_return('1')
end

after do
Facter::Resolvers::Linux::Processors.invalidate_cache
Facter::Resolvers::Uname.invalidate_cache
end

let(:speed) { 2_926_000_000 }
let(:physical_processors) { 2 }

Expand Down Expand Up @@ -176,6 +196,11 @@
.and_return('0')
end

after do
Facter::Resolvers::Linux::Processors.invalidate_cache
Facter::Resolvers::Uname.invalidate_cache
end

let(:physical_processors) { 1 }

it 'returns physical_devices_count' do
Expand Down
4 changes: 4 additions & 0 deletions spec/facter/resolvers/uname_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
Darwin Kernel Version 18.2.0: Fri Oct 5 19:41:49 PDT 2018; root:xnu-4903.221.2~2/RELEASE_X86_64')
end

after do
Facter::Resolvers::Uname.invalidate_cache
end

it 'returns machine' do
expect(uname_resolver.resolve(:machine)).to eq('x86_64')
end
Expand Down

0 comments on commit a513dbb

Please sign in to comment.