2
# -*- coding: utf-8 -*-
5
# Copyright (C) 2013 ~ 2014 National University of Defense Technology(NUDT) & Kylin Ltd
6
# This program is free software: you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License version 3, as published
8
# by the Free Software Foundation.
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranties of
12
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13
# PURPOSE. See the GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License along
16
# with this program. If not, see <http://www.gnu.org/licenses/>.
26
def cleanup_memory(self):
27
'''force changed to disk, update the super block'''
28
pone = subprocess.Popen(["sync"], shell=True)
31
'''drop pagecache, dentries and inodes to free the memory'''
32
ptwo = subprocess.Popen(["echo 3 > /proc/sys/vm/drop_caches"], shell=True)
36
def get_cpu_percent(self, percpu = False):
37
return psutil.cpu_percent(0.1, percpu)
39
# get total memory + cache
40
def get_total_cmemory(self, symbol = "m"):
41
mem = psutil.phymem_usage()
43
return self.bytes2human(value, symbol)
45
# get used memory + cache
46
def get_used_cmemory(self, symbol = "m"):
47
mem = psutil.phymem_usage()
49
return self.bytes2human(value, symbol)
51
# get free memory + cache
52
def get_free_cmemory(self, symbol = "m"):
53
mem = psutil.phymem_usage()
55
return self.bytes2human(value, symbol)
58
def get_total_memory(self, symbol = "m"):
59
baseData = psutil.virtual_memory()
60
value = baseData.total
61
return self.bytes2human(value, symbol)
64
def get_used_memory(self, symbol = "m"):
65
baseData = psutil.virtual_memory()
66
value = baseData.total - baseData.available
67
return self.bytes2human(value, symbol)
70
def get_free_memory(self, symbol = "m"):
71
baseData = psutil.virtual_memory()
72
value = baseData.available
73
return self.bytes2human(value, symbol)
75
# get memory used percent
76
def get_memory_percent(self):
77
baseData = psutil.virtual_memory()
78
return baseData.percent
80
# get network total flow, return (up, down)
81
def get_network_flow_total(self, symbol = "k"):
82
network = psutil.network_io_counters()
83
sent = network.bytes_sent
84
recv = network.bytes_recv
89
k_up = float(sent) / 1024
91
ups = ups[0:ups.find(".") + 2]
93
k_down = float(recv) / 1024
95
downs = downs[0:downs.find(".") + 2]
101
# byte to human by symbol
102
def bytes2human(self, value, symbol):
106
value = value / 1024 / 1024
108
valuef = float(value) / 1024 / 1024 / 1024
110
value = values[0:values.find(".") + 2]
115
if __name__ == "__main__":
117
# print mmm.get_network_flow_total()
118
# print mmm.get_network_flow_total("b")
119
# mmm.cleanup_memory()
120
# print mmm.get_cpu_percent()
121
# print mmm.get_cpu_percent(True)
122
# print mmm.get_cpu_percent()
123
# print mmm.get_free_memory("m")
124
# print mmm.get_used_memory("g")
125
# print mmm.get_total_memory("g")
127
print(mmm.get_memory_percent())
128
print(mmm.get_total_memory())
129
print(mmm.get_used_memory())
130
print(mmm.get_free_memory())