Method: LinuxStat::CPU.count_online

Defined in:
lib/linux_stat/cpu.rb

.count_onlineObject

Returns the total number of CPU online in the sysetm.

It first reads /proc/stat, if that fails, it will read /sys/devices/system/cpu/online, if that fails it will open /proc/cpuinfo. If neither of the procedures work, it will get the LinuxStat::Sysconf.processor_online

It opens /sys/devices/system/cpu/offline and performs various job to get one Ruby array.

If the information isn’t available, it will return an empty Array.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/linux_stat/cpu.rb', line 135

def count_online
  @@cpuinfo_file ||= '/proc/cpuinfo'.freeze
  @@cpuinfo_readable ||= File.readable?(@@cpuinfo_file)

  @@stat_file ||= '/proc/stat'.freeze

  # Not much slow, not blazing fast, somewhat reliable
  get_online = online

  if !get_online.empty?
    get_online.length
  elsif @@cpuinfo_readable
    # Way slower but reliable!
    IO.readlines(@@cpuinfo_file).count { |x| x.strip[/\Aprocessor.*\d*\z/] }
  else
    # Way faster but absolutely unrealiable!
    LinuxStat::Sysconf.processor_online
  end
end