forked from li900309/ProcStat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
procStat.py
175 lines (142 loc) · 4.54 KB
/
procStat.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#coding=utf-8
import os
import glob
import time
import shutil
import getopt
import sys
import re
import numpy
import pylab
import json
import csv
def LOG(aLogStr):
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())+':'+aLogStr)
def execCmd(cmd):
LOG(cmd)
o = os.popen(cmd)
r = o.read()
o.close()
return r
def adbShellCmd(cmd):
return execCmd('adb shell ' + '\"' + cmd + '\"')
kswapdStart = 0
def parseProc():
global kswapdStart
matchPs = r"(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\w+)\s+\w+\s+(\w)\s+(.*)"
procList = []
procs = adbShellCmd('ps').splitlines()
for line in procs:
proc = {}
match =re.match(matchPs, line)
if (match == None):
continue
proc['user'],proc['pid'],proc['ppid'],proc['vss'],proc['rss'],proc['wchan'],proc['stat'],proc['name'] = match.groups()
if (proc['name'].startswith('kswapd')):
ret = adbShellCmd('cat /proc/%s/stat'%proc['pid']).split()
kswapdRtTotal = int(ret[14]) + int(ret[13])
if (kswapdStart == 0):
kswapdStart = kswapdRtTotal
kswapdRun = kswapdRtTotal - kswapdStart
proc['kswapdRT'] = kswapdRun
procList.append(proc)
return procList
def countUserProc():
usrProcNo = 0
sysProcNo = 0
procList = parseProc()
for proc in procList:
if re.match('u[0-9]', proc['user']) != None:
usrProcNo += 1
if (proc['name'].startswith('kswapd')):
kswapdRunTime = proc['kswapdRT']
sysProcNo = procList.__len__() - usrProcNo
#return usrProcNo, sysProcNo
return dict(usrProc=usrProcNo, sysProc=sysProcNo, kswapdRunTime=kswapdRunTime)
def getOOMScore():
oomCmd = r"""ls /proc|grep ^[1-9]|while read l;do read comm < /proc/$l/comm;read oom < /proc/$l/oom_score_adj;if [ $oom -gt 0 ]; then printf "%d\\t%s\\t%d\\n" $l $comm $oom;fi;done"""
oom = []
ret = adbShellCmd(oomCmd).splitlines()
oom = [x for x in ret if (x.__len__() > 0 and x.split()[0].isdigit())]
oomList = []
for line in oom:
proc = {}
line = line.split()
proc['pid'] = int(line[0])
proc['comm'] = line[1]
proc['oom_score'] = int(line[2])
oomList.append(proc)
oomList.sort(key=lambda x: x['oom_score'])
return oomList
def oomScoreStet(oomList = None):
if oomList == None:
oomList = getOOMScore()
oomStat = {}
for x in oomList:
lambda x:x['oom_score']
def getMemInfo():
ret = adbShellCmd('cat /proc/meminfo').splitlines()
memInfo = {}
for line in ret:
line = line.split()
try:
item,value = line[0:2]
memInfo[item] = value
except:
pass
return memInfo
def getVmStat():
ret = adbShellCmd('cat /proc/vmstat').splitlines()
vmstat = {}
for line in ret:
line = line.split()
try:
item,value = line[0:2]
vmstat[item] = value
except:
pass
return vmstat
def getLmkStat():
ret = adbShellCmd('cat /sys/module/lowmemorykiller/parameters/lmk_stat*').splitlines()
try:
lmk_stat_kill, lmk_stat_kill_level, lmk_stat_unkill = [x for x in ret if x.__len__() > 0]
except:
return dict()
return dict(lmk_stat_kill = lmk_stat_kill,
lmk_stat_kill_level=lmk_stat_kill_level,
lmk_stat_unkill=lmk_stat_unkill)
def getAllSysInfo():
vmstat = getVmStat()
meminfo = getMemInfo()
proc = countUserProc()
lmkstat = getLmkStat()
procInfo = dict()
procInfo.update(vmstat)
procInfo.update(meminfo)
procInfo.update(proc)
procInfo.update(lmkstat)
return procInfo
def logging(tag, path = './', interval = 1, duration = 1800):
localtime = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
os.chdir(path)
path = os.path.join(path, tag+'_'+localtime+'.csv')
LOG('Logging into file'+path)
fd = open(path, 'w+')
csvwrt = csv.DictWriter(fd, fieldnames=None,dialect='unix')
starttime = time.time()
curtime = starttime
endtime = starttime + duration
while (curtime < endtime) :
curtime = time.time()
logitem = dict(time=(curtime-starttime).__round__(2))
logitem.update(getAllSysInfo())
if csvwrt.fieldnames == None:
csvwrt.fieldnames = logitem.keys()
csvwrt.writeheader()
csvwrt.writerow(logitem)
while (time.time() < curtime + interval):
time.sleep(0.01)
fd.close()
if __name__ == '__main__':
logging(tag='mem')