summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKrzysztof Wilczynski <krzysztof.wilczynski@linux.com>2011-01-02 03:27:59 +0000
committerAdrien Thebo <adrien.thebo@gmail.com>2011-04-01 18:19:11 -0700
commitcb52b06d4ce0bc2d7b7f6dbfb8ff2a882b63d107 (patch)
treec9f771a1eb3243c8d99065420b5824e5b7689de5 /lib
parent3efa9d717572d7a9a2136c50cd863ff9d4c27372 (diff)
downloadfacter-cb52b06d4ce0bc2d7b7f6dbfb8ff2a882b63d107.tar.gz
facter-cb52b06d4ce0bc2d7b7f6dbfb8ff2a882b63d107.tar.xz
facter-cb52b06d4ce0bc2d7b7f6dbfb8ff2a882b63d107.zip
Fix. Using sysfs file system entries to count the number of physical CPUs. Fall-back to "/proc/cpuinfo" included for backward-compatibility with legacy systems.
Diffstat (limited to 'lib')
-rw-r--r--lib/facter/physicalprocessorcount.rb45
1 files changed, 39 insertions, 6 deletions
diff --git a/lib/facter/physicalprocessorcount.rb b/lib/facter/physicalprocessorcount.rb
index 9c59614..86d0749 100644
--- a/lib/facter/physicalprocessorcount.rb
+++ b/lib/facter/physicalprocessorcount.rb
@@ -3,16 +3,49 @@
# 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
+Facter.add('physicalprocessorcount') do
+ confine :kernel => :linux
- setcode do
- ppcount = Facter::Util::Resolution.exec('grep "physical id" /proc/cpuinfo|cut -d: -f 2|sort -u|wc -l')
+ 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"
+ Dir[lookup_pattern].map { |i| File.read(i).strip }.uniq.size
+ 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 ...
+ #
+ File.read('/proc/cpuinfo').scan(/physical.+:\s(\d+)/).uniq.size
end
+ end
end