|
1 | 1 | require 'rbconfig' |
| 2 | +require 'concurrent/delay' |
2 | 3 |
|
3 | 4 | module Concurrent |
4 | 5 |
|
5 | | - # Number of processors seen by the OS and used for process scheduling. For performance |
6 | | - # reasons the calculated value will be memoized on the first call. |
7 | | - # |
8 | | - # When running under JRuby the Java runtime call `java.lang.Runtime.getRuntime.availableProcessors` |
9 | | - # will be used. According to the Java documentation this "value may change |
10 | | - # during a particular invocation of the virtual machine... [applications] |
11 | | - # should therefore occasionally poll this property." Subsequently the result |
12 | | - # will NOT be memoized under JRuby. |
13 | | - # |
14 | | - # On Windows the Win32 API will be queried for the `NumberOfLogicalProcessors from Win32_Processor`. |
15 | | - # This will return the total number "logical processors for the current instance of the processor", |
16 | | - # which taked into account hyperthreading. |
17 | | - # |
18 | | - # * AIX: /usr/sbin/pmcycles (AIX 5+), /usr/sbin/lsdev |
19 | | - # * BSD: /sbin/sysctl |
20 | | - # * Cygwin: /proc/cpuinfo |
21 | | - # * Darwin: /usr/bin/hwprefs, /usr/sbin/sysctl |
22 | | - # * HP-UX: /usr/sbin/ioscan |
23 | | - # * IRIX: /usr/sbin/sysconf |
24 | | - # * Linux: /proc/cpuinfo |
25 | | - # * Minix 3+: /proc/cpuinfo |
26 | | - # * Solaris: /usr/sbin/psrinfo |
27 | | - # * Tru64 UNIX: /usr/sbin/psrinfo |
28 | | - # * UnixWare: /usr/sbin/psrinfo |
29 | | - # |
30 | | - # @return [Integer] number of processors seen by the OS or Java runtime |
31 | | - # |
32 | | - # @see https://github.com/grosser/parallel/blob/4fc8b89d08c7091fe0419ca8fba1ec3ce5a8d185/lib/parallel.rb |
33 | | - # |
34 | | - # @see http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#availableProcessors() |
35 | | - # @see http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx |
36 | | - def processor_count |
37 | | - if RUBY_PLATFORM == 'java' |
38 | | - java.lang.Runtime.getRuntime.availableProcessors |
39 | | - else |
40 | | - @@processor_count ||= begin |
41 | | - os_name = RbConfig::CONFIG["target_os"] |
42 | | - if os_name =~ /mingw|mswin/ |
43 | | - require 'win32ole' |
44 | | - result = WIN32OLE.connect("winmgmts://").ExecQuery( |
45 | | - "select NumberOfLogicalProcessors from Win32_Processor") |
46 | | - result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+) |
47 | | - elsif File.readable?("/proc/cpuinfo") |
48 | | - IO.read("/proc/cpuinfo").scan(/^processor/).size |
49 | | - elsif File.executable?("/usr/bin/hwprefs") |
50 | | - IO.popen("/usr/bin/hwprefs thread_count").read.to_i |
51 | | - elsif File.executable?("/usr/sbin/psrinfo") |
52 | | - IO.popen("/usr/sbin/psrinfo").read.scan(/^.*on-*line/).size |
53 | | - elsif File.executable?("/usr/sbin/ioscan") |
54 | | - IO.popen("/usr/sbin/ioscan -kC processor") do |out| |
55 | | - out.read.scan(/^.*processor/).size |
56 | | - end |
57 | | - elsif File.executable?("/usr/sbin/pmcycles") |
58 | | - IO.popen("/usr/sbin/pmcycles -m").read.count("\n") |
59 | | - elsif File.executable?("/usr/sbin/lsdev") |
60 | | - IO.popen("/usr/sbin/lsdev -Cc processor -S 1").read.count("\n") |
61 | | - elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/i |
62 | | - IO.popen("/usr/sbin/sysconf NPROC_ONLN").read.to_i |
63 | | - elsif File.executable?("/usr/sbin/sysctl") |
64 | | - IO.popen("/usr/sbin/sysctl -n hw.ncpu").read.to_i |
65 | | - elsif File.executable?("/sbin/sysctl") |
66 | | - IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i |
67 | | - else |
68 | | - 1 |
69 | | - end |
70 | | - end |
| 6 | + class ProcessorCounter |
| 7 | + def initialize |
| 8 | + @processor_count = Delay.new { compute_processor_count } |
| 9 | + @physical_processor_count = Delay.new { compute_physical_processor_count } |
71 | 10 | end |
72 | | - rescue |
73 | | - return 1 |
| 11 | + |
| 12 | + # Number of processors seen by the OS and used for process scheduling. For performance |
| 13 | + # reasons the calculated value will be memoized on the first call. |
| 14 | + # |
| 15 | + # When running under JRuby the Java runtime call `java.lang.Runtime.getRuntime.availableProcessors` |
| 16 | + # will be used. According to the Java documentation this "value may change |
| 17 | + # during a particular invocation of the virtual machine... [applications] |
| 18 | + # should therefore occasionally poll this property." Subsequently the result |
| 19 | + # will NOT be memoized under JRuby. |
| 20 | + # |
| 21 | + # On Windows the Win32 API will be queried for the `NumberOfLogicalProcessors from Win32_Processor`. |
| 22 | + # This will return the total number "logical processors for the current instance of the processor", |
| 23 | + # which taked into account hyperthreading. |
| 24 | + # |
| 25 | + # * AIX: /usr/sbin/pmcycles (AIX 5+), /usr/sbin/lsdev |
| 26 | + # * BSD: /sbin/sysctl |
| 27 | + # * Cygwin: /proc/cpuinfo |
| 28 | + # * Darwin: /usr/bin/hwprefs, /usr/sbin/sysctl |
| 29 | + # * HP-UX: /usr/sbin/ioscan |
| 30 | + # * IRIX: /usr/sbin/sysconf |
| 31 | + # * Linux: /proc/cpuinfo |
| 32 | + # * Minix 3+: /proc/cpuinfo |
| 33 | + # * Solaris: /usr/sbin/psrinfo |
| 34 | + # * Tru64 UNIX: /usr/sbin/psrinfo |
| 35 | + # * UnixWare: /usr/sbin/psrinfo |
| 36 | + # |
| 37 | + # @return [Integer] number of processors seen by the OS or Java runtime |
| 38 | + # |
| 39 | + # @see https://github.com/grosser/parallel/blob/4fc8b89d08c7091fe0419ca8fba1ec3ce5a8d185/lib/parallel.rb |
| 40 | + # |
| 41 | + # @see http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#availableProcessors() |
| 42 | + # @see http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx |
| 43 | + def processor_count |
| 44 | + @processor_count.value |
| 45 | + end |
| 46 | + |
| 47 | + # Number of physical processor cores on the current system. For performance reasons |
| 48 | + # the calculated value will be memoized on the first call. |
| 49 | + # |
| 50 | + # On Windows the Win32 API will be queried for the `NumberOfCores from Win32_Processor`. |
| 51 | + # This will return the total number "of cores for the current instance of the processor." |
| 52 | + # On Unix-like operating systems either the `hwprefs` or `sysctl` utility will be called |
| 53 | + # in a subshell and the returned value will be used. In the rare case where none of these |
| 54 | + # methods work or an exception is raised the function will simply return 1. |
| 55 | + # |
| 56 | + # @return [Integer] number physical processor cores on the current system |
| 57 | + # |
| 58 | + # @see https://github.com/grosser/parallel/blob/4fc8b89d08c7091fe0419ca8fba1ec3ce5a8d185/lib/parallel.rb |
| 59 | + # |
| 60 | + # @see http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx |
| 61 | + # @see http://www.unix.com/man-page/osx/1/HWPREFS/ |
| 62 | + # @see http://linux.die.net/man/8/sysctl |
| 63 | + def physical_processor_count |
| 64 | + @physical_processor_count.value |
| 65 | + end |
| 66 | + |
| 67 | + private |
| 68 | + |
| 69 | + def compute_processor_count |
| 70 | + if RUBY_PLATFORM == 'java' |
| 71 | + java.lang.Runtime.getRuntime.availableProcessors |
| 72 | + else |
| 73 | + os_name = RbConfig::CONFIG["target_os"] |
| 74 | + if os_name =~ /mingw|mswin/ |
| 75 | + require 'win32ole' |
| 76 | + result = WIN32OLE.connect("winmgmts://").ExecQuery( |
| 77 | + "select NumberOfLogicalProcessors from Win32_Processor") |
| 78 | + result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+) |
| 79 | + elsif File.readable?("/proc/cpuinfo") |
| 80 | + IO.read("/proc/cpuinfo").scan(/^processor/).size |
| 81 | + elsif File.executable?("/usr/bin/hwprefs") |
| 82 | + IO.popen("/usr/bin/hwprefs thread_count").read.to_i |
| 83 | + elsif File.executable?("/usr/sbin/psrinfo") |
| 84 | + IO.popen("/usr/sbin/psrinfo").read.scan(/^.*on-*line/).size |
| 85 | + elsif File.executable?("/usr/sbin/ioscan") |
| 86 | + IO.popen("/usr/sbin/ioscan -kC processor") do |out| |
| 87 | + out.read.scan(/^.*processor/).size |
| 88 | + end |
| 89 | + elsif File.executable?("/usr/sbin/pmcycles") |
| 90 | + IO.popen("/usr/sbin/pmcycles -m").read.count("\n") |
| 91 | + elsif File.executable?("/usr/sbin/lsdev") |
| 92 | + IO.popen("/usr/sbin/lsdev -Cc processor -S 1").read.count("\n") |
| 93 | + elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/i |
| 94 | + IO.popen("/usr/sbin/sysconf NPROC_ONLN").read.to_i |
| 95 | + elsif File.executable?("/usr/sbin/sysctl") |
| 96 | + IO.popen("/usr/sbin/sysctl -n hw.ncpu").read.to_i |
| 97 | + elsif File.executable?("/sbin/sysctl") |
| 98 | + IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i |
| 99 | + else |
| 100 | + 1 |
| 101 | + end |
| 102 | + end |
| 103 | + rescue |
| 104 | + return 1 |
| 105 | + end |
| 106 | + |
| 107 | + def compute_physical_processor_count |
| 108 | + ppc = case RbConfig::CONFIG["target_os"] |
| 109 | + when /darwin1/ |
| 110 | + IO.popen("/usr/sbin/sysctl -n hw.physicalcpu").read.to_i |
| 111 | + when /linux/ |
| 112 | + cores = {} # unique physical ID / core ID combinations |
| 113 | + phy = 0 |
| 114 | + IO.read("/proc/cpuinfo").scan(/^physical id.*|^core id.*/) do |ln| |
| 115 | + if ln.start_with?("physical") |
| 116 | + phy = ln[/\d+/] |
| 117 | + elsif ln.start_with?("core") |
| 118 | + cid = phy + ":" + ln[/\d+/] |
| 119 | + cores[cid] = true if not cores[cid] |
| 120 | + end |
| 121 | + end |
| 122 | + cores.count |
| 123 | + when /mswin|mingw/ |
| 124 | + require 'win32ole' |
| 125 | + result_set = WIN32OLE.connect("winmgmts://").ExecQuery( |
| 126 | + "select NumberOfCores from Win32_Processor") |
| 127 | + result_set.to_enum.collect(&:NumberOfCores).reduce(:+) |
| 128 | + else |
| 129 | + processor_count |
| 130 | + end |
| 131 | + # fall back to logical count if physical info is invalid |
| 132 | + ppc > 0 ? ppc : processor_count |
| 133 | + rescue |
| 134 | + return 1 |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + # create the default ProcessorCounter on load |
| 139 | + @processor_counter = ProcessorCounter.new |
| 140 | + singleton_class.send :attr_reader, :processor_counter |
| 141 | + |
| 142 | + def self.processor_count |
| 143 | + processor_counter.processor_count |
74 | 144 | end |
75 | | - module_function :processor_count |
76 | 145 |
|
77 | | - # Number of physical processor cores on the current system. For performance reasons |
78 | | - # the calculated value will be memoized on the first call. |
79 | | - # |
80 | | - # On Windows the Win32 API will be queried for the `NumberOfCores from Win32_Processor`. |
81 | | - # This will return the total number "of cores for the current instance of the processor." |
82 | | - # On Unix-like operating systems either the `hwprefs` or `sysctl` utility will be called |
83 | | - # in a subshell and the returned value will be used. In the rare case where none of these |
84 | | - # methods work or an exception is raised the function will simply return 1. |
85 | | - # |
86 | | - # @return [Integer] number physical processor cores on the current system |
87 | | - # |
88 | | - # @see https://github.com/grosser/parallel/blob/4fc8b89d08c7091fe0419ca8fba1ec3ce5a8d185/lib/parallel.rb |
89 | | - # |
90 | | - # @see http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx |
91 | | - # @see http://www.unix.com/man-page/osx/1/HWPREFS/ |
92 | | - # @see http://linux.die.net/man/8/sysctl |
93 | | - def physical_processor_count |
94 | | - @@physical_processor_count ||= begin |
95 | | - ppc = case RbConfig::CONFIG["target_os"] |
96 | | - when /darwin1/ |
97 | | - IO.popen("/usr/sbin/sysctl -n hw.physicalcpu").read.to_i |
98 | | - when /linux/ |
99 | | - cores = {} # unique physical ID / core ID combinations |
100 | | - phy = 0 |
101 | | - IO.read("/proc/cpuinfo").scan(/^physical id.*|^core id.*/) do |ln| |
102 | | - if ln.start_with?("physical") |
103 | | - phy = ln[/\d+/] |
104 | | - elsif ln.start_with?("core") |
105 | | - cid = phy + ":" + ln[/\d+/] |
106 | | - cores[cid] = true if not cores[cid] |
107 | | - end |
108 | | - end |
109 | | - cores.count |
110 | | - when /mswin|mingw/ |
111 | | - require 'win32ole' |
112 | | - result_set = WIN32OLE.connect("winmgmts://").ExecQuery( |
113 | | - "select NumberOfCores from Win32_Processor") |
114 | | - result_set.to_enum.collect(&:NumberOfCores).reduce(:+) |
115 | | - else |
116 | | - processor_count |
117 | | - end |
118 | | - # fall back to logical count if physical info is invalid |
119 | | - ppc > 0 ? ppc : processor_count |
120 | | - end |
121 | | - rescue |
122 | | - return 1 |
| 146 | + def self.physical_processor_count |
| 147 | + processor_counter.physical_processor_count |
123 | 148 | end |
124 | | - module_function :physical_processor_count |
| 149 | + |
125 | 150 | end |
0 commit comments