• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jdantonio / concurrent-ruby / #690

20 May 2014 10:46AM UTC coverage: 92.28% (-5.1%) from 97.353%
#690

push

jdantonio
Merge pull request #87 from jdantonio/removed-actor-context-spike

Removed ActorContext spike.

2 of 2 new or added lines in 1 file covered. (100.0%)

128 existing lines in 11 files now uncovered.

2283 of 2474 relevant lines covered (92.28%)

618.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.28
/lib/concurrent/utility/processor_count.rb
1
require 'rbconfig'
1✔
2

3
module Concurrent
1✔
4

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
1✔
37
    if RUBY_PLATFORM == 'java'
1,389✔
UNCOV
38
      java.lang.Runtime.getRuntime.availableProcessors
×
39
    else
40
      @@processor_count ||= begin
1,389✔
41
                              os_name = RbConfig::CONFIG["target_os"]
1✔
42
                              if os_name =~ /mingw|mswin/
1✔
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")
1✔
48
                                IO.read("/proc/cpuinfo").scan(/^processor/).size
1✔
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
71
    end
72
  rescue
73
    return 1
×
74
  end
75
  module_function :processor_count
1✔
76

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
1✔
94
    @@physical_processor_count ||= begin
2✔
95
                                     ppc = case RbConfig::CONFIG["target_os"]
1✔
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
1✔
100
                                             phy = 0
1✔
101
                                             IO.read("/proc/cpuinfo").scan(/^physical id.*|^core id.*/) do |ln|
1✔
102
                                               if ln.start_with?("physical")
128✔
103
                                                 phy = ln[/\d+/]
64✔
104
                                               elsif ln.start_with?("core")
64✔
105
                                                 cid = phy + ":" + ln[/\d+/]
64✔
106
                                                 cores[cid] = true if not cores[cid]
64✔
107
                                               end
108
                                             end
109
                                             cores.count
1✔
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
1✔
120
                                   end
121
  rescue
122
    return 1
×
123
  end
124
  module_function :physical_processor_count
1✔
125
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc