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.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/linux_stat/cpu.rb', line 105

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