检测安装的 CPU 数量 [英] Detect number of CPUs installed
本文介绍了检测安装的 CPU 数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经通过 cat/proc/cpuinfo
找到了大多数 unixes"的解决方案,但纯 Ruby 解决方案会更好.
I already found a solution for "Most unixes" via cat /proc/cpuinfo
, but a pure-Ruby solution would be nicer.
推荐答案
我目前正在使用这个,它涵盖了所有 os.https://github.com/grosser/parallel/blob/master/lib/parallel.rb#L63
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
这篇关于检测安装的 CPU 数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文