Skip to content

Commit

Permalink
(FACT-2806) Fix physicalprocessorcount
Browse files Browse the repository at this point in the history
We need to read the physical_package_id for each cpu and count the distinct ids
  • Loading branch information
florindragos committed Oct 8, 2020
1 parent 96db788 commit 6794060
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/facter/resolvers/processors_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ def count_physical_processors(tokens)
def physical_devices_count
Dir.entries('/sys/devices/system/cpu')
.select { |dir| dir =~ /cpu[0-9]+$/ }
.count { |dir| File.exist?("/sys/devices/system/cpu/#{dir}/topology/physical_package_id") }
.select { |dir| File.exist?("/sys/devices/system/cpu/#{dir}/topology/physical_package_id") }
.map do |dir|
Util::FileHelper.safe_read("/sys/devices/system/cpu/#{dir}/topology/physical_package_id").strip
end
.uniq.count
end

def build_speed(tokens)
Expand Down
42 changes: 42 additions & 0 deletions spec/facter/resolvers/processors_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
allow(File).to receive(:exist?)
.with('/sys/devices/system/cpu/cpu1/topology/physical_package_id')
.and_return(true)

allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/devices/system/cpu/cpu0/topology/physical_package_id')
.and_return('0')

allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/devices/system/cpu/cpu1/topology/physical_package_id')
.and_return('1')
end

after do
Expand Down Expand Up @@ -112,6 +120,13 @@
allow(Dir).to receive(:entries).with('/sys/devices/system/cpu').and_return(%w[cpu0 cpu1 cpuindex])
allow(File).to receive(:exist?).with('/sys/devices/system/cpu/cpu0/topology/physical_package_id').and_return(true)
allow(File).to receive(:exist?).with('/sys/devices/system/cpu/cpu1/topology/physical_package_id').and_return(true)
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/devices/system/cpu/cpu0/topology/physical_package_id')
.and_return('0')

allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/devices/system/cpu/cpu1/topology/physical_package_id')
.and_return('1')
end

let(:speed) { 2_926_000_000 }
Expand Down Expand Up @@ -142,4 +157,31 @@
expect(result).to eq(speed)
end
end

context 'when on arm64 architecture' do
before do
allow(Facter::Util::FileHelper).to receive(:safe_readlines)
.with('/proc/cpuinfo')
.and_return(load_fixture('cpuinfo_arm64').readlines)

allow(Dir).to receive(:entries).with('/sys/devices/system/cpu').and_return(%w[cpu0 cpu1 cpuindex])
allow(File).to receive(:exist?).with('/sys/devices/system/cpu/cpu0/topology/physical_package_id').and_return(true)
allow(File).to receive(:exist?).with('/sys/devices/system/cpu/cpu1/topology/physical_package_id').and_return(true)
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/devices/system/cpu/cpu0/topology/physical_package_id')
.and_return('0')

allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/sys/devices/system/cpu/cpu1/topology/physical_package_id')
.and_return('0')
end

let(:physical_processors) { 1 }

it 'returns physical_devices_count' do
result = Facter::Resolvers::Linux::Processors.resolve(:physical_count)

expect(result).to eq(physical_processors)
end
end
end
17 changes: 17 additions & 0 deletions spec/fixtures/cpuinfo_arm64
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
processor : 0
BogoMIPS : 200.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics cpuid
CPU implementer : 0x43
CPU architecture: 8
CPU variant : 0x1
CPU part : 0x0a1
CPU revision : 1

processor : 1
BogoMIPS : 200.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics cpuid
CPU implementer : 0x43
CPU architecture: 8
CPU variant : 0x1
CPU part : 0x0a1
CPU revision : 1

0 comments on commit 6794060

Please sign in to comment.