~gandelman-a/ubuntu/precise/facter/merge922788

« back to all changes in this revision

Viewing changes to lib/facter/physicalprocessorcount.rb

  • Committer: Package Import Robot
  • Author(s): Adam Gandelman
  • Date: 2011-10-18 10:32:42 UTC
  • mfrom: (1.3.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: package-import@ubuntu.com-20111018103242-ag8i8vejfp8v7b1b
Tags: upstream-1.6.1
ImportĀ upstreamĀ versionĀ 1.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Facter.add("physicalprocessorcount") do
2
 
    confine :kernel => :linux
3
 
 
4
 
    setcode do
5
 
        ppcount = Facter::Util::Resolution.exec('grep "physical id" /proc/cpuinfo|cut -d: -f 2|sort -u|wc -l')
 
1
# Fact: physicalprocessorcount
 
2
#
 
3
# Purpose: Return the number of physical processors.
 
4
#
 
5
# Resolution:
 
6
#
 
7
#   Attempts to use sysfs to get the physical IDs of the processors. Falls
 
8
#   back to /proc/cpuinfo and "physical id" if sysfs is not available.
 
9
#
 
10
# Caveats:
 
11
#
 
12
Facter.add('physicalprocessorcount') do
 
13
  confine :kernel => :linux
 
14
 
 
15
  setcode do
 
16
    sysfs_cpu_directory = '/sys/devices/system/cpu' # This should always be there ...
 
17
 
 
18
    if File.exists?(sysfs_cpu_directory)
 
19
      #
 
20
      # We assume that the sysfs file system has the correct number of entries
 
21
      # under the "/sys/device/system/cpu" directory and if so then we process
 
22
      # content of the file "physical_package_id" located inside the "topology"
 
23
      # directory in each of the per-CPU sub-directories.
 
24
      #
 
25
      # As per Linux Kernel documentation and the file "cputopology.txt" located
 
26
      # inside the "/usr/src/linux/Documentation" directory we can find following
 
27
      # short explanation:
 
28
      #
 
29
      # (...)
 
30
      #
 
31
      # 1) /sys/devices/system/cpu/cpuX/topology/physical_package_id:
 
32
      #
 
33
      #         physical package id of cpuX. Typically corresponds to a physical
 
34
      #         socket number, but the actual value is architecture and platform
 
35
      #         dependent.
 
36
      #
 
37
      # (...)
 
38
      #
 
39
      lookup_pattern = "#{sysfs_cpu_directory}" +
 
40
        "/cpu*/topology/physical_package_id"
 
41
 
 
42
      Dir.glob(lookup_pattern).collect { |f| Facter::Util::Resolution.exec("cat #{f}")}.uniq.size
 
43
 
 
44
    else
 
45
      #
 
46
      # Try to count number of CPUs using the proc file system next ...
 
47
      #
 
48
      # We assume that /proc/cpuinfo has what we need and is so then we need
 
49
      # to make sure that we only count unique entries ...
 
50
      #
 
51
      str = Facter::Util::Resolution.exec("grep 'physical.\\+:' /proc/cpuinfo")
 
52
 
 
53
      if str then str.scan(/\d+/).uniq.size; end
6
54
    end
 
55
  end
 
56
end
 
57
 
 
58
Facter.add('physicalprocessorcount') do
 
59
  confine :kernel => :windows
 
60
  setcode do
 
61
    require 'facter/util/wmi'
 
62
    Facter::Util::WMI.execquery("select Name from Win32_Processor").Count
 
63
  end
7
64
end