-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_and_mem.py
42 lines (35 loc) · 1.32 KB
/
cpu_and_mem.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
import threading
import psutil
class CPUandMEM_Print(threading.Thread):
def __init__(self, pid, log_file):
threading.Thread.__init__(self)
self.pid = pid
self.log_file = log_file
self.keep_running = True
self.mem_readings = []
self.cpu_readings = []
def get_cpumem(self, pid):
"""
Gets process memory and cpu usage values.
:param pid:
:return:
"""
process = psutil.Process(pid)
cpu_percent = psutil.cpu_percent()
mem_percent = process.memory_percent()
return (cpu_percent, mem_percent)
def run(self):
while self.keep_running:
cpu_and_mem = self.get_cpumem(self.pid)
self.mem_readings.append(cpu_and_mem[0])
self.cpu_readings.append(cpu_and_mem[1])
with open(self.log_file, 'ab+') as log:
log_entry = "%.2f\t%.2f\n" % (cpu_and_mem)
log.write(str.encode(log_entry))
def stop(self):
self.keep_running = False
mean_cpu = sum(self.cpu_readings) / float(len(self.cpu_readings))
mean_mem = sum(self.mem_readings) / float(len(self.mem_readings))
with open(self.log_file, 'ab') as log:
log_entry = "%.2f\t%.2f\n" % (mean_cpu, mean_mem)
# log.write(str.encode(log_entry))