Class: Duffy::System
- Inherits:
-
Object
- Object
- Duffy::System
- Defined in:
- lib/duffy/system.rb
Overview
Depending on your hardware, you may want to make decisions in your code. I use this to figure out how many processes I can launch in parallel.
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.
-
.mem_available ⇒ Object
Memory available for use in Megabytes Darwin: vm_stat (Pages Free + Pages Inactive) Linux: Read /proc/meminfo.
-
.mem_total ⇒ Object
Total system memory in Megabytes Darwin: hw.memsize (bytes) Linux: Read /proc/meminfo.
-
.mem_used ⇒ Object
Memory used Subtract mem_available from mem_total.
-
.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
22 23 24 25 26 27 28 29 30 |
# File 'lib/duffy/system.rb', line 22 def cores case RUBY_PLATFORM when /linux/ then File.read('/proc/cpuinfo').scan(/(cpu cores).*(\d+)/)[0][1].to_i * cpus when /darwin/ then `sysctl -n hw.physicalcpu`.to_i else 1 end rescue 1 end |
.cpu_percent ⇒ Object
The system’s current CPU utilization. Darwin: Get a list of all processes’ CPU percentage and add them up. Linux: Read /proc/stat twice and take the difference to give cpu time used in that interval.
52 53 54 55 56 57 58 59 60 |
# File 'lib/duffy/system.rb', line 52 def cpu_percent case RUBY_PLATFORM when /darwin/ then `ps -A -o %cpu`.lines.map(&:to_f).inject(:+) / threads when /linux/ then proc_diff else 0 end rescue 0 end |
.cpus ⇒ Object
How many Physical CPUs do you have. Linux: Detected by counting unique physical IDs Mac: hw.packages
9 10 11 12 13 14 15 16 17 |
# File 'lib/duffy/system.rb', line 9 def cpus case RUBY_PLATFORM when /linux/ then File.read('/proc/cpuinfo').scan(/^physical id.*/).uniq.count when /darwin/ then `sysctl -n hw.packages`.to_i else 1 end rescue 1 end |
.mem_available ⇒ Object
Memory available for use in Megabytes Darwin: vm_stat (Pages Free + Pages Inactive) Linux: Read /proc/meminfo
78 79 80 81 82 83 84 85 86 |
# File 'lib/duffy/system.rb', line 78 def mem_available case RUBY_PLATFORM when /darwin/ then `vm_stat`.lines.grep(/(free:|inactive:)\s*(\d+)/){$2}.map(&:to_i).inject(:+) * 4 / 1024 when /linux/ then File.read("/proc/meminfo").lines.grep(/MemAvail|MemFree/).sort.first.split[1].to_i / 1024 else 0 end rescue 0 end |
.mem_total ⇒ Object
Total system memory in Megabytes Darwin: hw.memsize (bytes) Linux: Read /proc/meminfo
65 66 67 68 69 70 71 72 73 |
# File 'lib/duffy/system.rb', line 65 def mem_total case RUBY_PLATFORM when /darwin/ then `sysctl -n hw.memsize`.to_i / 1024 / 1024 when /linux/ then File.read("/proc/meminfo").scan(/MemTotal:\s*(\d+)/)[0][0].to_i / 1024 else 0 end rescue 0 end |
.mem_used ⇒ Object
Memory used Subtract mem_available from mem_total. This ignores file cache etc that is not actually used by programs.
91 92 93 |
# File 'lib/duffy/system.rb', line 91 def mem_used mem_total - mem_available 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.
45 46 47 |
# File 'lib/duffy/system.rb', line 45 def sane_load (cores + threads) / 2 end |
.threads ⇒ Object
How many threads does the system have.
33 34 35 36 37 38 39 40 41 |
# File 'lib/duffy/system.rb', line 33 def threads case RUBY_PLATFORM when /linux/ then File.read('/proc/cpuinfo').scan(/^processor\s*:/).size when /darwin/ then `sysctl -n hw.ncpu`.to_i else 1 end rescue 1 end |