-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpuUsage2.py
95 lines (64 loc) · 2.4 KB
/
cpuUsage2.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
#!/usr/bin/python
import multiprocessing
import subprocess
import time
import sys
import json
import os.path
PERIOD = int(sys.argv[1]) if ((len(sys.argv) > 1 and int(sys.argv[1]) > 20)) else 20
CORENUMBER = multiprocessing.cpu_count()
class Meassure:
'This is a meassure'
count = list()
def __init__(self, coreUsage, processList):
self.time = time.strftime("%d/%m/%Y %H:%M:%S")
self.coreUsage = coreUsage
self.processList = processList
def getCoreUsage(self):
return self.coreUsage
def getTime(self):
return self.time
def getMeassure():
'Runs top and get the data as a Meassure object'
info = subprocess.check_output(['top','-b', '-d 5', '-n 2'])
# Parser
coreInfo = info.split("\n")[19+CORENUMBER:(2*CORENUMBER+19)]
coreInfo = [i.split("usuario")[0].split(":")[1].strip() for i in coreInfo]
processList = info.split("\n")[(2*CORENUMBER+23):-1]
processList = [filter(None, i.split(" ")) for i in processList]
processList = [(i[8], i[11]) for i in processList]
# End Parser
meassure = Meassure(coreInfo, processList)
return meassure
def main():
# Initializing output files
pwd = subprocess.check_output(["pwd"])[:-1]
dump = pwd +"/dump.log"
csv = pwd +"/coreUsage.csv"
header = [("core "+str(i)) for i in range(0, CORENUMBER) ]
header = ["time"] + header
with open(csv, "w") as csv_data:
csv_data.write(", ".join(header))
csv_data.write("\n")
csv_data.close()
with open(dump, "w") as json_data:
json_data.write("[]")
json_data.close()
# Launch the logger
while(1):
m = getMeassure()
curDump = [m.__dict__]
with open(dump) as json_data:
storedDump = json.load(json_data)
curDump = storedDump + curDump
json_data.close()
with open(dump, "w") as json_data:
json_data.write(json.dumps(curDump))
json_data.close()
with open(csv, "a") as csv_data:
body = [ i.replace(",", ".") for i in m.getCoreUsage() ]
body = m.getTime() + ", " + ", ".join(body)+"\n"
csv_data.write(body)
csv_data.close()
time.sleep(PERIOD)
main()