Method: LinuxStat::CPU.online

Defined in:
lib/linux_stat/cpu.rb

.onlineObject

Returns the total number of CPU online in the sysetm.

It will read /proc/stat to get the info.

If the info isn’t available, it reads /sys/devices/system/cpu/onfline and performs various job to get one Ruby array.

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



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

def online
	@@online_file ||= '/sys/devices/system/cpu/online'.freeze
	@@online_readable ||= File.readable?(@@online_file)

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

	ret = []

	if stat?
		IO.readlines(@@stat_file).map { |x|
			v = x.strip[/\Acpu\d*/] &.[](/\d/)
			ret << v.to_i if v
		}
	elsif @@online_readable
		IO.read(@@online_file).split(?,.freeze).each { |x|
			x.strip!
			c = x.split(?-.freeze).map(&:to_i)
			ret.concat(c.length == 2 ? Range.new(*c).to_a : c)
		}
	end

	ret
end