-
Notifications
You must be signed in to change notification settings - Fork 0
/
fml.py
84 lines (69 loc) · 2.43 KB
/
fml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class FreeMemLinux(object):
"""
Non-cross platform way to get free memory on Linux. Note that this code
uses the `with ... as`, which is conditionally Python 2.5 compatible!
If for some reason you still have Python 2.5 on your system add in the
head of your code, before all imports:
from __future__ import with_statement
source : https://stackoverflow.com/questions/17718449/determine-free-ram-in-python
"""
def __init__(self, unit='kB'):
with open('/proc/meminfo', 'r') as mem:
lines = mem.readlines()
self._tot = int(lines[0].split()[1])
self._free = int(lines[1].split()[1])
self._buff = int(lines[2].split()[1])
self._cached = int(lines[3].split()[1])
self._shared = int(lines[20].split()[1])
self._swapt = int(lines[14].split()[1])
self._swapf = int(lines[15].split()[1])
self._swapu = self._swapt - self._swapf
self.unit = unit
self._convert = self._factor()
def _factor(self):
"""determine the convertion factor"""
if self.unit == 'kB':
return 1
if self.unit == 'k':
return 1024.0
if self.unit == 'MB':
return 1/1024.0
if self.unit == 'GB':
return 1/1024.0/1024.0
if self.unit == '%':
return 1.0/self._tot
else:
raise Exception("Unit not understood")
@property
def total(self):
return self._convert * self._tot
@property
def used(self):
return self._convert * (self._tot - self._free)
@property
def used_real(self):
"""memory used which is not cache or buffers"""
return self._convert * (self._tot - self._free -
self._buff - self._cached)
@property
def shared(self):
return self._convert * (self._tot - self._free)
@property
def buffers(self):
return self._convert * (self._buff)
@property
def cached(self):
return self._convert * self._cached
@property
def user_free(self):
"""This is the free memory available for the user"""
return self._convert *(self._free + self._buff + self._cached)
@property
def swap(self):
return self._convert * self._swapt
@property
def swap_free(self):
return self._convert * self._swapf
@property
def swap_used(self):
return self._convert * self._swapu