Class: Duffy::System
- Inherits:
-
Object
- Object
- Duffy::System
- Defined in:
- lib/duffy/system.rb
Overview
This is linux specific (for now) and will return 1 for everything on other systems.
Class Method Summary collapse
-
.cores ⇒ Object
How many actual CPU cores do we have not including Hyperthreading Linux: “cpu cores” in cpuinfo is on a per physical processor basis, so we multiply by the number of CPUs Mac: hw.physicalcpu.
-
.cpu_percent ⇒ Object
The system’s current CPU utilization.
-
.cpus ⇒ Object
How many Physical CPUs do you have.
-
.sane_load ⇒ Object
What is a sane number of threads to use for data processing.
-
.threads ⇒ Object
How many threads does the system have.
Class Method Details
.cores ⇒ Object
How many actual CPU cores do we have not including Hyperthreading Linux: “cpu cores” in cpuinfo is on a per physical processor basis, so we multiply by the number of CPUs Mac: hw.physicalcpu
25 26 27 28 29 30 31 |
# File 'lib/duffy/system.rb', line 25 def cores case RUBY_PLATFORM when /linux/ then (File.read('/proc/cpuinfo').scan(/^cpu cores.*/).first.scan(/\d+$/).first.to_i rescue 1) * cpus when /darwin/ then `sysctl -n hw.physicalcpu`.to_i rescue 1 else 1 end end |
.cpu_percent ⇒ Object
The system’s current CPU utilization. Darwin: Get a list of all processes’ CPU percentage and add them up. Accurate to a couple percent vs. Activity Monitor. Linux: Read /proc/stat twice and take the difference to give cpu time used in that interval.
51 52 53 54 55 56 57 |
# File 'lib/duffy/system.rb', line 51 def cpu_percent case RUBY_PLATFORM when /darwin/ then (`ps -A -o %cpu`.lines.map(&:to_f).inject(:+) rescue 0) / threads when /linux/ then proc_diff rescue 0 else 0 end end |
.cpus ⇒ Object
How many Physical CPUs do you have. Linux: Detected by counting unique physical IDs Mac: hw.packages
14 15 16 17 18 19 20 |
# File 'lib/duffy/system.rb', line 14 def cpus case RUBY_PLATFORM when /linux/ then File.read('/proc/cpuinfo').scan(/^physical id.*/).uniq.count rescue 1 when /darwin/ then `sysctl -n hw.packages`.to_i rescue 1 else 1 end end |
.sane_load ⇒ Object
What is a sane number of threads to use for data processing. Only using physical cores is a waste, using all logical cores decreases performance.
44 45 46 |
# File 'lib/duffy/system.rb', line 44 def sane_load (cores + threads) / 2 end |
.threads ⇒ Object
How many threads does the system have.
34 35 36 37 38 39 40 |
# File 'lib/duffy/system.rb', line 34 def threads case RUBY_PLATFORM when /linux/ then File.read('/proc/cpuinfo').scan(/^processor\s*:/).size rescue 1 when /darwin/ then `sysctl -n hw.ncpu`.to_i rescue 1 else 1 end end |