service_scripts: Add schdule latency scripts

Signed-off-by: Hailong Liu <liuhailong@linux.alibaba.com>
This commit is contained in:
Hailong Liu 2022-03-17 13:17:37 +08:00
parent 8db61c2111
commit 429ce11a2d
2 changed files with 157 additions and 0 deletions

View File

@ -0,0 +1,49 @@
#!/usr/bin/python3
# coding=utf-8
import json
import sys
import os
import time
###################################################################
## 如前端输入
## {
## "实例IP":"192.168.1.101",
## "采样阈值":"10000",
## "诊断时长":"10",
## "诊断线程":"10002"
## }
## '{"实例IP":"192.168.1.101", "诊断时长":10, "采样阈值":10000, "诊断线程":10002}'
## 解析参数方法:
## sysak schedmoni 10000 -s 10 -p 10002
## 10000 时间阈值, 捕获延迟10000 us的事件
## -s 10 诊断时间10秒
## -p 10002 针对现场10002进行监控
##
######################################################################
args = json.loads(sys.argv[1], encoding="UTF-8")
result = {}
result['commands'] = []
cmd0 = {}
cmd0['instance'] = args["实例IP"]
arg_time = args["诊断时长"]
if arg_time:
arg_time = '-s ' + str(arg_time)
arg_thresh = args["采样阈值"]
arg_tid = args["诊断线程"]
if arg_tid:
arg_tid = '-p ' + str(arg_tid)
check_result_cmd = "0"
dump_log_cmd = "cat /var/log/sysak/schedmoni/runslow.log;\
echo \"\";cat /var/log/sysak/schedmoni/nosched.log;"
schedmoni_cmd = "sysak schedmoni "+str(arg_tid)+" "+str(arg_thresh)+" "+str(arg_time)+" 2>/dev/null"
print_result_cmd = "if [ "+check_result_cmd+" = \"0\" ]; then "+\
dump_log_cmd+"else echo \"fail\"; fi"
cmd0['cmd'] = schedmoni_cmd+" && "+print_result_cmd
result['commands'].append(cmd0)
data = json.dumps(result)
print(data)

View File

@ -0,0 +1,108 @@
#!/usr/bin/python3
# coding=utf-8
import sys
import os
import json
import random
import collections
items = ["time", "cpu", "comm", "tid", "latency", "lenth"]
sumary_runslw = {"cpus":[], "num":0, "avg":0.00, "max":0, "total":0}
sumary_runnsc = {"cpus":[], "num":0, "avg":0.00, "max":0, "total":0}
sumary_irqoff = {"cpus":[], "num":0, "avg":0.00, "max":0, "total":0}
class schedmoni(object):
def __init__(self, f):
super(schedmoni, self).__init__()
self._file = f
def seprator_data(self, line):
arry = line.split()
data = arry[0].split('(')
return data[1].strip(')')
def common_data(self, line):
tmpdic = {}
arry = line.split()
lenth = min(len(items), len(arry))
for i in range(lenth):
tmpdic[items[i]] = arry[i]
return tmpdic
def sumary_data(self, sumary, last_dic):
lat_sum = sumary[last_dic['mode']]
data = last_dic['data']
if data['cpu'] not in lat_sum['cpus']:
lat_sum['cpus'].append(data['cpu'])
lat_sum['total'] = lat_sum['total'] + int(data['latency'])
lat_sum['num'] = lat_sum['num'] + 1
lat_sum['avg'] = float(lat_sum['total'])/float(lat_sum['num'])
lat_sum['max'] = max(lat_sum['max'], int(data['latency']))
def class_identify(self, line):
if line[0:4]=='TIME': #seprator
data = self.seprator_data(line)
return 0, data
elif line[0:3]=='<0x': #private:stack trace
return 1, line
elif len(line) > 10: #common datas
tmp_dic = self.common_data(line)
return 2, tmp_dic
def proc(self):
top = []
sumary = {'mode':'sumary', 'nosch':sumary_runnsc, 'runslw':sumary_runslw, 'irqoff':sumary_irqoff}
dic = collections.OrderedDict()
dic['mode'] = None
dic['data'] = None
dic['stack'] = []
last_dic = {}
mode = 0
modstr = ""
for line in self._file.readlines():
if len(line) < 2:
continue
mode, val = self.class_identify(line)
if mode == 0:
if last_dic:
self.sumary_data(sumary, last_dic)
top.append(last_dic)
s = json.dumps(last_dic)
print(s)
modstr = val
last_dic = {}
continue
elif mode == 2:
if last_dic:
self.sumary_data(sumary, last_dic)
top.append(last_dic)
s = json.dumps(last_dic)
print(s)
dic['mode'] = modstr
dic['data'] = val
dic['stack'] = []
last_dic = dic
elif mode == 1:
last_dic['stack'].append(line)
continue
if last_dic:
self.sumary_data(sumary, last_dic)
top.append(last_dic)
s = json.dumps(last_dic)
print(s)
for mode in ["runslw", "nosch", "irqoff"]:
mode_dic = sumary[mode]
if mode_dic['total'] == 0:
del sumary[mode]
s = json.dumps(sumary)
print(s)
if __name__ == "__main__":
fname="/tmp/schedmoni_argv_"+str(random.random())
f=open(fname,"w+")
f.write(sys.argv[1])
f.close()
f=open(fname,"r")
c1 = schedmoni(f)
c1.proc()
f.close()
os.remove(fname)