From 6984e7ae8137121cfe0b2e35e84a03e0d67a92a2 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Sat, 6 Jul 2019 14:31:04 -0700 Subject: [PATCH 1/2] Use Ruby's Etc.nprocessors if available MRI has had Etc.nprocessors since Ruby 2.2, which uses: - sched_getaffinity(): Linux - sysconf(_SC_NPROCESSORS_ONLN): GNU/Linux, NetBSD, FreeBSD, OpenBSD, DragonFly BSD, OpenIndiana, Mac OS X, AIX - GetSystemInfo(): Win32 This should be faster than the alternatives of reading from /proc/cpuinfo, using WIN32OLE, or spawning a new process. This also has the advantage of reporting processor count based on CPU affinity which /usr/bin/nproc did, but reading /proc/cpuinfo does not. When using this value for controlling the level of parallelism, we should ideally be using the processes CPU affinity. --- lib/concurrent/utility/processor_counter.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/concurrent/utility/processor_counter.rb b/lib/concurrent/utility/processor_counter.rb index 6d6ae8dea..13ce2828f 100644 --- a/lib/concurrent/utility/processor_counter.rb +++ b/lib/concurrent/utility/processor_counter.rb @@ -22,6 +22,8 @@ def initialize # occasionally poll this property." Subsequently the result will NOT be # memoized under JRuby. # + # Ruby's Etc.nprocessors will be used if available (MRI 2.2+). + # # On Windows the Win32 API will be queried for the # `NumberOfLogicalProcessors from Win32_Processor`. This will return the # total number "logical processors for the current instance of the @@ -76,6 +78,8 @@ def physical_processor_count def compute_processor_count if Concurrent.on_jruby? java.lang.Runtime.getRuntime.availableProcessors + elsif defined?(Etc) && Etc.respond_to?(:nprocessors) && (nprocessor = Etc.nprocessors rescue nil) + nprocessor else os_name = RbConfig::CONFIG["target_os"] if os_name =~ /mingw|mswin/ From 9c067bb9927908f5ff19d3fa0160ce119e90aa07 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Sun, 7 Jul 2019 22:11:01 -0700 Subject: [PATCH 2/2] Always require etc for ProcessorCounter --- lib/concurrent/utility/processor_counter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/concurrent/utility/processor_counter.rb b/lib/concurrent/utility/processor_counter.rb index 13ce2828f..531ca0a3c 100644 --- a/lib/concurrent/utility/processor_counter.rb +++ b/lib/concurrent/utility/processor_counter.rb @@ -1,3 +1,4 @@ +require 'etc' require 'rbconfig' require 'concurrent/delay' @@ -78,7 +79,7 @@ def physical_processor_count def compute_processor_count if Concurrent.on_jruby? java.lang.Runtime.getRuntime.availableProcessors - elsif defined?(Etc) && Etc.respond_to?(:nprocessors) && (nprocessor = Etc.nprocessors rescue nil) + elsif Etc.respond_to?(:nprocessors) && (nprocessor = Etc.nprocessors rescue nil) nprocessor else os_name = RbConfig::CONFIG["target_os"]