Skip to content

Commit 6794060

Browse files
committed
(FACT-2806) Fix physicalprocessorcount
We need to read the physical_package_id for each cpu and count the distinct ids
1 parent 96db788 commit 6794060

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

lib/facter/resolvers/processors_resolver.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ def count_physical_processors(tokens)
6060
def physical_devices_count
6161
Dir.entries('/sys/devices/system/cpu')
6262
.select { |dir| dir =~ /cpu[0-9]+$/ }
63-
.count { |dir| File.exist?("/sys/devices/system/cpu/#{dir}/topology/physical_package_id") }
63+
.select { |dir| File.exist?("/sys/devices/system/cpu/#{dir}/topology/physical_package_id") }
64+
.map do |dir|
65+
Util::FileHelper.safe_read("/sys/devices/system/cpu/#{dir}/topology/physical_package_id").strip
66+
end
67+
.uniq.count
6468
end
6569

6670
def build_speed(tokens)

spec/facter/resolvers/processors_resolver_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
allow(File).to receive(:exist?)
6262
.with('/sys/devices/system/cpu/cpu1/topology/physical_package_id')
6363
.and_return(true)
64+
65+
allow(Facter::Util::FileHelper).to receive(:safe_read)
66+
.with('/sys/devices/system/cpu/cpu0/topology/physical_package_id')
67+
.and_return('0')
68+
69+
allow(Facter::Util::FileHelper).to receive(:safe_read)
70+
.with('/sys/devices/system/cpu/cpu1/topology/physical_package_id')
71+
.and_return('1')
6472
end
6573

6674
after do
@@ -112,6 +120,13 @@
112120
allow(Dir).to receive(:entries).with('/sys/devices/system/cpu').and_return(%w[cpu0 cpu1 cpuindex])
113121
allow(File).to receive(:exist?).with('/sys/devices/system/cpu/cpu0/topology/physical_package_id').and_return(true)
114122
allow(File).to receive(:exist?).with('/sys/devices/system/cpu/cpu1/topology/physical_package_id').and_return(true)
123+
allow(Facter::Util::FileHelper).to receive(:safe_read)
124+
.with('/sys/devices/system/cpu/cpu0/topology/physical_package_id')
125+
.and_return('0')
126+
127+
allow(Facter::Util::FileHelper).to receive(:safe_read)
128+
.with('/sys/devices/system/cpu/cpu1/topology/physical_package_id')
129+
.and_return('1')
115130
end
116131

117132
let(:speed) { 2_926_000_000 }
@@ -142,4 +157,31 @@
142157
expect(result).to eq(speed)
143158
end
144159
end
160+
161+
context 'when on arm64 architecture' do
162+
before do
163+
allow(Facter::Util::FileHelper).to receive(:safe_readlines)
164+
.with('/proc/cpuinfo')
165+
.and_return(load_fixture('cpuinfo_arm64').readlines)
166+
167+
allow(Dir).to receive(:entries).with('/sys/devices/system/cpu').and_return(%w[cpu0 cpu1 cpuindex])
168+
allow(File).to receive(:exist?).with('/sys/devices/system/cpu/cpu0/topology/physical_package_id').and_return(true)
169+
allow(File).to receive(:exist?).with('/sys/devices/system/cpu/cpu1/topology/physical_package_id').and_return(true)
170+
allow(Facter::Util::FileHelper).to receive(:safe_read)
171+
.with('/sys/devices/system/cpu/cpu0/topology/physical_package_id')
172+
.and_return('0')
173+
174+
allow(Facter::Util::FileHelper).to receive(:safe_read)
175+
.with('/sys/devices/system/cpu/cpu1/topology/physical_package_id')
176+
.and_return('0')
177+
end
178+
179+
let(:physical_processors) { 1 }
180+
181+
it 'returns physical_devices_count' do
182+
result = Facter::Resolvers::Linux::Processors.resolve(:physical_count)
183+
184+
expect(result).to eq(physical_processors)
185+
end
186+
end
145187
end

spec/fixtures/cpuinfo_arm64

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
processor : 0
2+
BogoMIPS : 200.00
3+
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics cpuid
4+
CPU implementer : 0x43
5+
CPU architecture: 8
6+
CPU variant : 0x1
7+
CPU part : 0x0a1
8+
CPU revision : 1
9+
10+
processor : 1
11+
BogoMIPS : 200.00
12+
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics cpuid
13+
CPU implementer : 0x43
14+
CPU architecture: 8
15+
CPU variant : 0x1
16+
CPU part : 0x0a1
17+
CPU revision : 1

0 commit comments

Comments
 (0)