Last active
December 18, 2015 16:49
-
-
Save jimweirich/5813834 to your computer and use it in GitHub Desktop.
Can people run this script and see if it gives accurate count of CPUs on their system. Report results in the comments please. Thanks! Oh! And don't forget to report what kind of system you are running this on (linux, windows, mac, etc.). UPDATE: Revised version that uses the Java runtime if running under JRuby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rbconfig' | |
# Based on a script at: | |
# http://stackoverflow.com/questions/891537/ruby-detect-number-of-cpus-installed | |
class CpuCounter | |
def self.count | |
new.count | |
end | |
def count | |
if defined?(Java::Java) | |
count_via_java_runtime | |
else | |
case RbConfig::CONFIG['host_os'] | |
when /darwin9/ | |
count_via_hwprefs_cpu_count | |
when /darwin/ | |
count_via_hwprefs_thread_count || count_via_sysctl | |
when /linux/ | |
count_via_cpuinfo | |
when /freebsd/ | |
count_via_sysctl | |
when /mswin|mingw/ | |
count_via_win32 | |
end | |
end | |
end | |
def count_via_java_runtime | |
Java::Java.lang.Runtime.getRuntime.availableProcessors | |
end | |
def count_via_win32 | |
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 | |
def count_via_cpuinfo | |
open('/proc/cpuinfo') { |f| f.readlines }.grep(/processor/).size | |
end | |
def count_via_hwprefs_thread_count | |
run 'hwprefs', 'thread_count' | |
end | |
def count_via_hwprefs_cpu_count | |
run 'hwprefs', 'cpu_count' | |
end | |
def count_via_sysctl | |
run 'sysctl', '-n hw.ncpu' | |
end | |
def run(command, args) | |
cmd = resolve_command(command) | |
if cmd | |
`#{cmd} #{args}`.to_i | |
else | |
nil | |
end | |
end | |
def resolve_command(command) | |
try_command("/sbin", command) || | |
try_command("/usr/sbin", command) || | |
in_path_command(command) | |
end | |
def try_command(dir, command) | |
path = File.join(dir, command) | |
File.exist?(path) ? path : nil | |
end | |
def in_path_command(command) | |
`which #{command}` != '' ? command : nil | |
end | |
end | |
if $0 == __FILE__ | |
puts "Number of CPUs = #{CpuCounter.count}" | |
end |
13" MacBook Air, Mid 2012
OS X 10.8.4 (12E55)
2 GHz Intel Core i7
~ ruby -v
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]
~ ruby cpu.rb
Number of CPUs = 4
~ sysctl hw | grep cpu
hw.ncpu: 4
hw.activecpu: 4
hw.physicalcpu: 2
hw.physicalcpu_max: 2
hw.logicalcpu: 4
hw.logicalcpu_max: 4
hw.cputype: 7
hw.cpusubtype: 4
hw.cpu64bit_capable: 1
hw.cpufamily: 526772277
hw.cpufrequency: 2000000000
hw.cpufrequency_min: 2000000000
hw.cpufrequency_max: 2000000000
hw.cputhreadtype: 1
hw.ncpu = 4
hw.cpufrequency = 2000000000
hw.availcpu = 4
➜ ~ ruby cpu.rb
Number of CPUs = 2
➜ ~ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
➜ ~ ruby cpu.rb
Number of CPUs = 2
➜ ~ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 42
model name : Intel(R) Pentium(R) CPU B940 @ 2.00GHz
stepping : 7
microcode : 0x17
cpu MHz : 800.000
cache size : 2048 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave lahf_lm arat epb xsaveopt pln pts dtherm
bogomips : 3990.92
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
processor : 1
vendor_id : GenuineIntel
cpu family : 6
model : 42
model name : Intel(R) Pentium(R) CPU B940 @ 2.00GHz
stepping : 7
microcode : 0x17
cpu MHz : 800.000
cache size : 2048 KB
physical id : 0
siblings : 2
core id : 1
cpu cores : 2
apicid : 2
initial apicid : 2
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave lahf_lm arat epb xsaveopt pln pts dtherm
bogomips : 3990.92
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
➜ ~ uname -a
Linux yoda 3.9-1-amd64 #1 SMP Debian 3.9.6-1 x86_64 GNU/Linux
Processor Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz, 2668 Mhz, 4 Core(s), 8 Logical Processor(s)
Windows 7 Ultimate Service Pack 1
C:\Users\Scott>ruby -v
ruby 2.0.0p0 (2013-02-24) [x64-mingw32]
C:\Users\Scott\Desktop>ruby cpu.rb
Number of CPUs = 4
Looks correct on my end. Ubuntu 12.04-LTS. AMD 8 core CPU.
ryan@nas gist5813834-353ada2ef3e2ba9f144a996a853826208eda1404 % ruby -v
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
ryan@nas gist5813834-353ada2ef3e2ba9f144a996a853826208eda1404 % ruby cpu.rb
Number of CPUs = 8
ryan@nas gist5813834-353ada2ef3e2ba9f144a996a853826208eda1404 % uname -a
Linux nas 3.2.0-40-generic #64-Ubuntu SMP Mon Mar 25 21:22:10 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
ryan@nas gist5813834-353ada2ef3e2ba9f144a996a853826208eda1404 % cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 21
model : 2
model name : AMD FX(tm)-8320 Eight-Core Processor
stepping : 0
microcode : 0x600081c
cpu MHz : 1400.000
cache size : 2048 KB
physical id : 0
siblings : 8
core id : 0
cpu cores : 4
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold bmi1
bogomips : 7046.96
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro
processor : 1
vendor_id : AuthenticAMD
cpu family : 21
model : 2
model name : AMD FX(tm)-8320 Eight-Core Processor
stepping : 0
microcode : 0x600081c
cpu MHz : 1400.000
cache size : 2048 KB
physical id : 0
siblings : 8
core id : 3
cpu cores : 4
apicid : 1
initial apicid : 3
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold bmi1
bogomips : 7046.88
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro
processor : 2
vendor_id : AuthenticAMD
cpu family : 21
model : 2
model name : AMD FX(tm)-8320 Eight-Core Processor
stepping : 0
microcode : 0x600081c
cpu MHz : 1400.000
cache size : 2048 KB
physical id : 0
siblings : 8
core id : 5
cpu cores : 4
apicid : 2
initial apicid : 5
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold bmi1
bogomips : 7046.89
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro
processor : 3
vendor_id : AuthenticAMD
cpu family : 21
model : 2
model name : AMD FX(tm)-8320 Eight-Core Processor
stepping : 0
microcode : 0x600081c
cpu MHz : 1400.000
cache size : 2048 KB
physical id : 0
siblings : 8
core id : 1
cpu cores : 4
apicid : 3
initial apicid : 1
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold bmi1
bogomips : 7046.89
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro
processor : 4
vendor_id : AuthenticAMD
cpu family : 21
model : 2
model name : AMD FX(tm)-8320 Eight-Core Processor
stepping : 0
microcode : 0x600081c
cpu MHz : 1400.000
cache size : 2048 KB
physical id : 0
siblings : 8
core id : 2
cpu cores : 4
apicid : 4
initial apicid : 2
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold bmi1
bogomips : 7046.89
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro
processor : 5
vendor_id : AuthenticAMD
cpu family : 21
model : 2
model name : AMD FX(tm)-8320 Eight-Core Processor
stepping : 0
microcode : 0x600081c
cpu MHz : 1400.000
cache size : 2048 KB
physical id : 0
siblings : 8
core id : 4
cpu cores : 4
apicid : 5
initial apicid : 4
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold bmi1
bogomips : 7046.86
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro
processor : 6
vendor_id : AuthenticAMD
cpu family : 21
model : 2
model name : AMD FX(tm)-8320 Eight-Core Processor
stepping : 0
microcode : 0x600081c
cpu MHz : 3500.000
cache size : 2048 KB
physical id : 0
siblings : 8
core id : 6
cpu cores : 4
apicid : 6
initial apicid : 6
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold bmi1
bogomips : 7046.86
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro
processor : 7
vendor_id : AuthenticAMD
cpu family : 21
model : 2
model name : AMD FX(tm)-8320 Eight-Core Processor
stepping : 0
microcode : 0x600081c
cpu MHz : 1400.000
cache size : 2048 KB
physical id : 0
siblings : 8
core id : 7
cpu cores : 4
apicid : 7
initial apicid : 7
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx f16c lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs xop skinit wdt lwp fma4 tce nodeid_msr tbm topoext perfctr_core arat cpb npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold bmi1
bogomips : 7046.87
TLB size : 1536 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm 100mhzsteps hwpstate cpb eff_freq_ro
ryan@nas gist5813834-353ada2ef3e2ba9f144a996a853826208eda1404 %
Looks correct here too:
$ ruby cpu.rb
Number of CPUs = 8
$ uname -a
Linux notebook 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Number of CPUs = 8
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro8,2
Processor Name: Intel Core i7
Processor Speed: 2.2 GHz
Number of Processors: 1
Total Number of Cores: 4
L2 Cache (per Core): 256 KB
L3 Cache: 6 MB
Memory: 8 GB
Boot ROM Version: MBP81.0047.B27
SMC Version (system): 1.69f4
Serial Number (system): C02FD1CCDF8X
Hardware UUID: BB5533F1-A94D-527B-BB0C-EB5543ACC6E1
Sudden Motion Sensor:
State: Enabled