summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Turnbull <james@lovedthanlost.net>2011-04-06 02:12:07 +1000
committerJames Turnbull <james@lovedthanlost.net>2011-04-06 02:12:07 +1000
commitfc930d8c26c81075eb35ae728777f190d5af1402 (patch)
tree6deb4c8ae52f60f0c515d2cf67c42aab4d625efc
parente42e57c1a5674d77dfa14e10ade9592f5d2052e7 (diff)
parentc2ff8330f4d705ded3f251e170df4bd973af3d43 (diff)
downloadfacter-fc930d8c26c81075eb35ae728777f190d5af1402.tar.gz
facter-fc930d8c26c81075eb35ae728777f190d5af1402.tar.xz
facter-fc930d8c26c81075eb35ae728777f190d5af1402.zip
Fixed #5135 - Facter returns physicalprocessorcount => 0 on a physical computer
-rw-r--r--lib/facter/physicalprocessorcount.rb53
-rw-r--r--spec/unit/physicalprocessorcount_spec.rb40
2 files changed, 87 insertions, 6 deletions
diff --git a/lib/facter/physicalprocessorcount.rb b/lib/facter/physicalprocessorcount.rb
index 9c59614..c6145dc 100644
--- a/lib/facter/physicalprocessorcount.rb
+++ b/lib/facter/physicalprocessorcount.rb
@@ -3,16 +3,57 @@
# Purpose: Return the number of physical processors.
#
# Resolution:
-# On linux, parses the output of '/proc/cpuinfo' for the number of unique
-# lines with "physical id" in them.
+#
+# Attempts to use sysfs to get the physical IDs of the processors. Falls
+# back to /proc/cpuinfo and "physical id" if sysfs is not available.
#
# Caveats:
#
+Facter.add('physicalprocessorcount') do
+ confine :kernel => :linux
+
+ setcode do
+ sysfs_cpu_directory = '/sys/devices/system/cpu' # This should always be there ...
+
+ if File.exists?(sysfs_cpu_directory)
+ #
+ # We assume that the sysfs file system has the correct number of entries
+ # under the "/sys/device/system/cpu" directory and if so then we process
+ # content of the file "physical_package_id" located inside the "topology"
+ # directory in each of the per-CPU sub-directories.
+ #
+ # As per Linux Kernel documentation and the file "cputopology.txt" located
+ # inside the "/usr/src/linux/Documentation" directory we can find following
+ # short explanation:
+ #
+ # (...)
+ #
+ # 1) /sys/devices/system/cpu/cpuX/topology/physical_package_id:
+ #
+ # physical package id of cpuX. Typically corresponds to a physical
+ # socket number, but the actual value is architecture and platform
+ # dependent.
+ #
+ # (...)
+ #
+ lookup_pattern = "#{sysfs_cpu_directory}" +
+ "/cpu*/topology/physical_package_id"
+
+ ids = Dir.glob(lookup_pattern).collect { |f| Facter::Util::Resolution.exec("cat #{f}")}
+
+ ids = ids.join if ids.is_a?(Array)
+ ids.scan(/\d+/).uniq.size
-Facter.add("physicalprocessorcount") do
- confine :kernel => :linux
+ else
+ #
+ # Try to count number of CPUs using the proc file system next ...
+ #
+ # We assume that /proc/cpuinfo has what we need and is so then we need
+ # to make sure that we only count unique entries ...
+ #
+ str = Facter::Util::Resolution.exec("grep 'physical.\\+:' /proc/cpuinfo")
- setcode do
- ppcount = Facter::Util::Resolution.exec('grep "physical id" /proc/cpuinfo|cut -d: -f 2|sort -u|wc -l')
+ if not str.nil? then str.scan(/\d+/).uniq.size; end
end
+ end
end
diff --git a/spec/unit/physicalprocessorcount_spec.rb b/spec/unit/physicalprocessorcount_spec.rb
new file mode 100644
index 0000000..260788b
--- /dev/null
+++ b/spec/unit/physicalprocessorcount_spec.rb
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+require 'facter'
+
+describe "Physical processor count facts" do
+ before do
+ Facter.loadfacts
+ end
+ before do
+ Facter.clear
+ end
+ it "should return one physical CPU" do
+ Facter.fact(:kernel).stubs(:value).returns("Linux")
+ File.stubs(:exists?).with('/sys/devices/system/cpu').returns(true)
+ Dir.stubs(:glob).with("/sys/devices/system/cpu/cpu*/topology/physical_package_id").returns("/sys/devices/system/cpu/cpu0/topology/physical_package_id")
+ Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu0/topology/physical_package_id").returns("0")
+
+ Facter.fact(:physicalprocessorcount).value.should == 1
+ end
+
+ it "should return four physical CPUs" do
+ Facter.fact(:kernel).stubs(:value).returns("Linux")
+ File.stubs(:exists?).with('/sys/devices/system/cpu').returns(true)
+ Dir.stubs(:glob).with("/sys/devices/system/cpu/cpu*/topology/physical_package_id").returns(%w{
+ /sys/devices/system/cpu/cpu0/topology/physical_package_id
+ /sys/devices/system/cpu/cpu1/topology/physical_package_id
+ /sys/devices/system/cpu/cpu2/topology/physical_package_id
+ /sys/devices/system/cpu/cpu3/topology/physical_package_id
+ })
+
+ Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu0/topology/physical_package_id").returns("0")
+ Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu1/topology/physical_package_id").returns("1")
+ Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu2/topology/physical_package_id").returns("2")
+ Facter::Util::Resolution.stubs(:exec).with("cat /sys/devices/system/cpu/cpu3/topology/physical_package_id").returns("3")
+
+ Facter.fact(:physicalprocessorcount).value.should == 4
+ end
+end