lib/celluloid/cpu_counter.rb in celluloid-0.16.0.pre vs lib/celluloid/cpu_counter.rb in celluloid-0.16.0.pre2

- old
+ new

@@ -1,26 +1,33 @@ -require 'rbconfig' - module Celluloid module CPUCounter - case RbConfig::CONFIG['host_os'][/^[A-Za-z]+/] - when 'darwin' - @cores = Integer(`/usr/sbin/sysctl hw.ncpu`[/\d+/]) - when 'linux' - @cores = if File.exists?("/sys/devices/system/cpu/present") - File.read("/sys/devices/system/cpu/present").split('-').last.to_i+1 - else - Dir["/sys/devices/system/cpu/cpu*"].select { |n| n=~/cpu\d+/ }.count + class << self + def cores + @cores ||= count_cores end - when 'mingw', 'mswin' - @cores = Integer(ENV["NUMBER_OF_PROCESSORS"][/\d+/]) - when 'freebsd' - @cores = Integer(`sysctl hw.ncpu`[/\d+/]) - else - @cores = nil - end - def self.cores; @cores; end - end -end + private + def count_cores + result = from_env || from_sysdev || from_sysctl + Integer(result.to_s[/\d+/], 10) if result + end + def from_env + result = ENV['NUMBER_OF_PROCESSORS'] + result if result + end + + def from_sysdev + ::IO.read('/sys/devices/system/cpu/present').split('-').last.to_i + 1 + rescue Errno::ENOENT + result = Dir['/sys/devices/system/cpu/cpu*'].count { |n| n =~ /cpu\d+/ } + result unless result.zero? + end + + def from_sysctl + result = `sysctl -n hw.ncpu` + result if $?.success? + end + end + end +end