summaryrefslogtreecommitdiffstats
path: root/lib/facter/physicalprocessorcount.rb
blob: 144183c734d3da21c896ae3c2020052d02c39edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Fact: physicalprocessorcount
#
# Purpose: Return the number of physical processors.
#
# Resolution:
#   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"

      Facter::Util::Resolution.exec(
        "cat #{lookup_pattern}"
      ).scan(/\d+/).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 ...
      #
      Facter::Util::Resolution.exec(
        "grep 'physical.\\+:' /proc/cpuinfo"
      ).scan(/\d+/).uniq.size
    end
  end
end