From 1b9906d6bfd45c98728b65eca34d8eb40f3287bc Mon Sep 17 00:00:00 2001 From: Florin Dragos Date: Thu, 8 Oct 2020 10:56:33 +0300 Subject: [PATCH] (FACT-2806) Fix physicalprocessorcount We need to read the physical_package_id for each cpu and count the distinct ids --- lib/facter/resolvers/processors_resolver.rb | 6 ++- .../resolvers/processors_resolver_spec.rb | 42 +++++++++++++++++++ spec/fixtures/cpuinfo_arm64 | 17 ++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/cpuinfo_arm64 diff --git a/lib/facter/resolvers/processors_resolver.rb b/lib/facter/resolvers/processors_resolver.rb index 448f860dd7..4ba3d1c381 100644 --- a/lib/facter/resolvers/processors_resolver.rb +++ b/lib/facter/resolvers/processors_resolver.rb @@ -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) diff --git a/spec/facter/resolvers/processors_resolver_spec.rb b/spec/facter/resolvers/processors_resolver_spec.rb index 889b51d9ea..246f3699c9 100644 --- a/spec/facter/resolvers/processors_resolver_spec.rb +++ b/spec/facter/resolvers/processors_resolver_spec.rb @@ -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 @@ -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 } @@ -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 diff --git a/spec/fixtures/cpuinfo_arm64 b/spec/fixtures/cpuinfo_arm64 new file mode 100644 index 0000000000..226c307d31 --- /dev/null +++ b/spec/fixtures/cpuinfo_arm64 @@ -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 \ No newline at end of file