Determine the number of installed CPUs

I already found a solution for "Most unixes" through cat /proc/cpuinfo , but a pure-Ruby solution would be better.

+32
ruby
May 21 '09 at 5:42 a.m.
source share
12 answers

I am currently using this which covers all OS. https://github.com/grosser/parallel/blob/master/lib/parallel.rb#L63

  def self.processor_count case RbConfig::CONFIG['host_os'] when /darwin9/ `hwprefs cpu_count`.to_i when /darwin/ ((`which hwprefs` != '') ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i when /linux/ `cat /proc/cpuinfo | grep processor | wc -l`.to_i when /freebsd/ `sysctl -n hw.ncpu`.to_i when /mswin|mingw/ require 'win32ole' wmi = WIN32OLE.connect("winmgmts://") cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # TODO count hyper-threaded in this cpu.to_enum.first.NumberOfCores end end 
+25
Jun 21 2018-11-11T00:
source share

EDIT: now the rails come with a simultaneous ruby ​​as a dependency, so maybe this is the best solution;

 $ gem install concurrent-ruby $ irb irb(main):001:0> require 'concurrent' => true irb(main):002:0> Concurrent.processor_count => 8 irb(main):003:0> Concurrent.physical_processor_count => 4 

see http://ruby-concurrency.imtqy.com/concurrent-ruby/root/Concurrent.html for more information.

and here is the previous answer;

 $ gem install facter $ irb irb(main):001:0> require 'facter' => true irb(main):002:0> puts Facter.value('processors')['count'] 4 => nil irb(main):003:0> 

This focker gem is the best if you want to get other facts about the system, it is not platform specific and designed for this purpose.

UPDATE: Updated to include Nathan Kleyn's hint about changing api.

+36
May 21 '09 at 8:23 a.m.
source share

In Ruby version 2.2.3, the etc module in Ruby stdlib offers nprocessors , which returns the number of processors. It should be noted that if the ruby ​​is assigned to a subset of the processor cores, Etc.nprocessors will return only the number of cores that Ruby has access to. In addition, as noted in seanlinsley , this will lead to the return of virtual cores instead of physical cores, which may lead to a mismatch of the expected value.

 require 'etc' p Etc.nprocessors #=> 4 
+25
Dec 11 '15 at 2:30
source share

with JRuby, you can test it with the following Java code:

  Runtime runtime = Runtime.getRuntime(); int numberOfProcessors = runtime.availableProcessors(); 
+10
May 21 '09 at 6:39
source share

Here is the implementation for Linux, OSX, Windows, and BSD: https://gist.github.com/1009994

Source:

 module System extend self def cpu_count return Java::Java.lang.Runtime.getRuntime.availableProcessors if defined? Java::Java return File.read('/proc/cpuinfo').scan(/^processor\s*:/).size if File.exist? '/proc/cpuinfo' require 'win32ole' WIN32OLE.connect("winmgmts://").ExecQuery("select * from Win32_ComputerSystem").NumberOfProcessors rescue LoadError Integer `sysctl -n hw.ncpu 2>/dev/null` rescue 1 end end System.cpu_count # => 2 
+9
Jun 06 2018-11-11T00:
source share

Of course, if you can cat it, you can open, read and close it using standard language functions without resorting to calling system() .

You just need to determine which platform you are dynamically on, and either:

  • use /proc/cpuinfo "file" for Linux; or
  • interacts with WMI for Windows.

This last line can use:

 require 'win32ole' wmi = WIN32OLE.connect("winmgmts://") info = wmi.ExecQuery ("select * from Win32_ComputerSystem") 

Then use the information element of NumberOfProcessors .

+5
May 21 '09 at 5:46 a.m.
source share

On linux, you can also use nproc , which is cleaner than other subshell solutions. I wanted the tramp to give the same number of processors to the virtual machine as the host. I added this to the Vagrantfile :

 vb.cpus = `nproc`.to_i 
+3
Mar 11 '15 at 17:19
source share

I tried using Facter , but found it a bit slow. I tried the gem system and found it much faster. It is also very easy to use: System::CPU.count .

+2
Oct 19 '13 at 8:52
source share

on Mac:

thiago-pradis-macbook: ~ tchandy $ hwprefs cpu_count

2

+1
Jul 31 '09 at 23:19
source share

@grosser:

 when / linux /  
   `grep -c processor /proc/cpuinfo`.to_i

http://www.partmaps.org/era/unix/award.html#cat
http://www.partmaps.org/era/unix/award.html#wc

+1
Jul 07 2018-11-11T00:
source share

I recently found something that may need to be taken into account. You can deactivate processors (take them offline) and then facter processorcount (plus some other methods above) gives the wrong result. You can count the processor lines in / proc / cpuinfo if you do it right. If you just populate the array with procs index numbers, if you have spaces in procs (like in, procs 0,1,2,10,11,12 are active, all the rest up to 20 say inactive), it will automatically spring 3- 9 into existence (sort of), at least Array # array will report 13 in this case. You will need to do #compact to get the number of active processors. However, if you need complete processors, it might be better to look at / sys / devices / system / cpu [0-9] and calculate this. This will give you the total number of processors, but not how many (or which) are active.

Just something to think about. I am trying to skip a patch to a factor in order to add an active processor account and a totalprocessorcount fact.

0
May 9 '13 at 17:48
source share

A combination of @grosser and @paxdiablo answers, since win32_computersystem does not have processor information on my system (winxp); this works though:

 require 'win32ole' wmi = WIN32OLE.connect("winmgmts://") info = wmi.ExecQuery ("select NumberOfCores from Win32_processor") puts info.to_enum.first.NumberOfCores 

To find out what is available on your system, run this from powershell (in this case, I used 1.0):

 Get-WmiObject -list 

(you might want to connect to grep if you have cygwin installed)

0
Jan 08 '15 at 14:22
source share



All Articles