Module: LinuxStat::Memory

Defined in:
lib/linux_stat/memory.rb

Overview

Shows various Memory related information of the current system.

Class Method Summary collapse

Class Method Details

.availableObject

Returns the total memory details reported by /proc/meminfo. The value is in Kilobyte.

It retuns an Integer but if the info is not available, it will return nil



62
63
64
65
# File 'lib/linux_stat/memory.rb', line 62

def available
	return nil unless meminfo?
	IO.foreach('/proc/meminfo'.freeze).first(3)[-1].split[1].to_i
end

.freeObject

Returns the free memory reported by LS::Sysinfo.freeram() The value is in Kilobyte.

It retuns an Integer but if the info is not available, it will return nil.



52
53
54
55
# File 'lib/linux_stat/memory.rb', line 52

def free
	v = LinuxStat::Sysinfo.freeram
	v ? v.fdiv(1024).to_i : nil
end

.percent_availableObject

Returns the percentage of memory used reported by /proc/meminfo.

It retuns an Integer but if the info is not available, it will return nil



93
94
95
96
97
# File 'lib/linux_stat/memory.rb', line 93

def percent_available
	return nil unless meminfo?
	memory = IO.foreach('/proc/meminfo'.freeze).first(3)
	memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
end

.percent_usedObject

Returns the percentage of memory used reported by /proc/meminfo.

It retuns an Integer but if the info is not available, it will return nil



82
83
84
85
86
87
# File 'lib/linux_stat/memory.rb', line 82

def percent_used
	return nil unless meminfo?
	memory = IO.foreach('/proc/meminfo'.freeze).first(3)
	total = memory[0].split[1].to_i
	total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
end

.statObject

Returns the memory details reported by /proc/meminfo. In this format:

{:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}

The values are in Kilobyte.

If the statistics is not available, it will return an empty Hash.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/linux_stat/memory.rb', line 13

def stat
	return {} unless meminfo?

	memory = IO.foreach('/proc/meminfo'.freeze).first(3)

	total = memory[0].split[1].to_i
	available = memory[2].split[1].to_i
	used = total - available

	percent_used = total == 0 ? 0.0 : used.*(100).fdiv(total).round(2)
	percent_available = total == 0 ? 0.0 : available.*(100).fdiv(total).round(2)

	# We have all the methods, but each methods reads the same file
	#
	# So better to use the above calculation
	{
		total: total,
		used: used,
		available: available,
		percent_used: percent_used,
		percent_available: percent_available
	}
end

.totalObject

Returns the total memory reported by LS::Sysinfo.totalram() The value is in Kilobyte.

It retuns an Integer but if the info is not available, it will return nil.



42
43
44
45
# File 'lib/linux_stat/memory.rb', line 42

def total
	v = LinuxStat::Sysinfo.totalram
	v ? v.fdiv(1024).to_i : nil
end

.usedObject

Returns the amount of memory used reported by /proc/meminfo. The value is in Kilobyte.

It retuns an Integer but if the info is not available, it will return nil.



72
73
74
75
76
# File 'lib/linux_stat/memory.rb', line 72

def used
	return nil unless meminfo?
	memory = IO.foreach('/proc/meminfo'.freeze).first(3)
	memory[0].split[1].to_i - memory[2].split[1].to_i
end