Class: Concurrent::ProcessorCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent/utility/processor_count.rb

Instance Method Summary collapse

Constructor Details

#initializeProcessorCounter

Returns a new instance of ProcessorCounter.



8
9
10
11
12
# File 'lib/concurrent/utility/processor_count.rb', line 8

def initialize
  immediate_executor        = ImmediateExecutor.new
  @processor_count          = Delay.new(executor: immediate_executor) { compute_processor_count }
  @physical_processor_count = Delay.new(executor: immediate_executor) { compute_physical_processor_count }
end

Instance Method Details

#physical_processor_countInteger

Number of physical processor cores on the current system. For performance reasons the calculated value will be memoized on the first call.

On Windows the Win32 API will be queried for the ‘NumberOfCores from Win32_Processor`. This will return the total number “of cores for the current instance of the processor.” On Unix-like operating systems either the `hwprefs` or `sysctl` utility will be called in a subshell and the returned value will be used. In the rare case where none of these methods work or an exception is raised the function will simply return 1.



65
66
67
# File 'lib/concurrent/utility/processor_count.rb', line 65

def physical_processor_count
  @physical_processor_count.value
end

#processor_countInteger

Number of processors seen by the OS and used for process scheduling. For performance reasons the calculated value will be memoized on the first call.

When running under JRuby the Java runtime call ‘java.lang.Runtime.getRuntime.availableProcessors` will be used. According to the Java documentation this “value may change during a particular invocation of the virtual machine… [applications] should therefore occasionally poll this property.” Subsequently the result will NOT be memoized under JRuby.

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 processor”, which taked into account hyperthreading.

  • AIX: /usr/sbin/pmcycles (AIX 5+), /usr/sbin/lsdev

  • BSD: /sbin/sysctl

  • Cygwin: /proc/cpuinfo

  • Darwin: /usr/bin/hwprefs, /usr/sbin/sysctl

  • HP-UX: /usr/sbin/ioscan

  • IRIX: /usr/sbin/sysconf

  • Linux: /proc/cpuinfo

  • Minix 3+: /proc/cpuinfo

  • Solaris: /usr/sbin/psrinfo

  • Tru64 UNIX: /usr/sbin/psrinfo

  • UnixWare: /usr/sbin/psrinfo



45
46
47
# File 'lib/concurrent/utility/processor_count.rb', line 45

def processor_count
  @processor_count.value
end