-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoolsMemory.py
101 lines (81 loc) · 2.68 KB
/
toolsMemory.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import time,sys
class FreeMemLinux(object):
"""
modified from:
http://stackoverflow.com/questions/17718449/determine-free-ram-in-python
Non-cross platform way to get free memory on Linux. Note that this code
uses the key word 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
"""
def __init__(self, unit='b'):
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._faktor()
def _faktor(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
if self.unit == "b":
# bits
return 1024*8
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
class mem(object):
def __init__(self,unit="b"):
self.unit=unit
pass
def updatefree(self):
m = FreeMemLinux(unit=self.unit)
return m.user_free
free = property(updatefree)