Skip to content

Commit

Permalink
kftop: Add sort option by average time
Browse files Browse the repository at this point in the history
Signed-off-by: iipeace <[email protected]>
  • Loading branch information
iipeace committed Nov 26, 2023
1 parent 5c6378a commit d27e514
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 90 deletions.
10 changes: 5 additions & 5 deletions guider/guider.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'''
[ Threshold Description ]

- RESOURCE
- TARGET
- cpu: total, idle, user, kernel, irq, iowait, nrCore, steal, temp, pri
- core: total, idle, user, kernel, irq, iowait, steal, temp, online
curFreq, minFreq, maxFreq, governor
Expand All @@ -27,23 +27,23 @@
- cgroup: cpu, throttle, memory, read, write, cpuDelay, memDelay, ioDelay
- battery: per, left, plugged

- TARGET
- SCOPE
- SYSTEM: system resources
- *NAME: ID/NAME for specific tasks, devices, events (support regex)
- *NAME/ID: NAME/ID for specific tasks, devices, events (support regex)
- KERNEL: kernel log
- DLT: DLT log
- JOURNAL: systemd journal log
- SYSLOG: syslog log
- TRACE: ftrace log
- LOGCAT: android log
- FILE: file event log
- FILE: file access log
- SYSCALL: syscall
- NATIVE: native function for C, C++, Rust, Go
- PYTHON: python function
- DBUS: D-Bus message
- SIGNAL: signal

- FIELD
- ATTRIBUTE
< common >
- apply: activation flag (true/false)
- after: uptime condition for event handling
Expand Down
179 changes: 94 additions & 85 deletions guider/guider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__credits__ = "Peace Lee"
__license__ = "GPLv2"
__version__ = "3.9.8"
__revision__ = "231124"
__revision__ = "231126"
__maintainer__ = "Peace Lee"
__email__ = "[email protected]"
__repository__ = "https://github.com/iipeace/guider"
Expand Down Expand Up @@ -26192,6 +26192,33 @@ def loadLibcObj(exit=False):
except:
return False

@staticmethod
def getCpuUsageStr(obj, diff, color=True):
# get CPU usage for me #
if diff >= 1:
cpuUsage = obj.getCpuUsage(system=True)
else:
cpuUsage = [0, 0, 0, 100]

ttime = cpuUsage[0] / diff
utime = cpuUsage[1] / diff
stime = cpuUsage[2] / diff

# get CPU usage for myself #
mcpu = "%d%%" % ttime
if color:
mcpu = UtilMgr.convCpuColor(ttime, mcpu)
mcpuStr = "%s(U%d%%+S%d%%)" % (mcpu, utime, stime)

# get CPU usage for system #
ctime = 100 - (cpuUsage[3] / diff)
ctime = ctime if ctime > 0 else 0
sysCpuStr = "%d%%" % ctime
if color:
sysCpuStr = UtilMgr.convCpuColor(ctime, sysCpuStr)

return mcpuStr, sysCpuStr

@staticmethod
def shrinkHeap():
if not SysMgr.isLinux:
Expand Down Expand Up @@ -36372,6 +36399,7 @@ def _getDesc(s, t=0):
-m <ROWS:COLS:SYSTEM> set terminal size
-a show all functions
-g <FUNC> set function filter
-c <CORE> set core filter
-Q print all rows in a stream
-q <NAME{:VALUE}> set environment variables
-E <DIR> set cache dir path
Expand All @@ -36386,9 +36414,16 @@ def _getDesc(s, t=0):
- {2:1} with function filter
# {0:1} {1:1} -g "*write*, sched*"

- {2:1} only for specific core
# {0:1} {1:1} -c 0,1,2

- {2:1} with per-core classification
# {0:1} {1:1} -q PERCORE

- {2:1} after sorting
# {0:1} {1:1} -q SORT:hit
# {0:1} {1:1} -q SORT:total
# {0:1} {1:1} -q SORT:average
""".format(
cmd,
mode,
Expand Down Expand Up @@ -59427,7 +59462,7 @@ def _applyFuncFilter(val=""):
# get sort attr #
if "SORT" in SysMgr.environList:
sortval = SysMgr.environList["SORT"][0].lower()
if not sortval in ("hit", "total"):
if not sortval in ("hit", "total", "average"):
SysMgr.printErr("no sort value '%s'" % sortval)
sys.exit(0)
else:
Expand Down Expand Up @@ -59466,6 +59501,11 @@ def _applyStats(node, path):
if not node in curCoreStat:
curCoreStat[node] = {}

perCoreStat = "PERCORE" in SysMgr.environList
core = node.strip("function")
if SysMgr.customCmd and not core in SysMgr.customCmd:
return

for l in statList[2:]:
name, hit, total, _, avg, _, s2, _ = l.strip().split()
if name == "schedule":
Expand All @@ -59491,9 +59531,11 @@ def _applyStats(node, path):
if not prevCoreStat:
continue

tname = "[%s]%s" % (core, name) if perCoreStat else name

# init total stats #
if not name in totalStat:
totalStat[name] = {
if not tname in totalStat:
totalStat[tname] = {
"hit": 0,
"total": 0,
}
Expand All @@ -59502,21 +59544,21 @@ def _applyStats(node, path):
try:
# new func #
if not name in prevCoreStat[node]:
totalStat[name]["hit"] += hit
totalStat[name]["total"] += total
totalStat[tname]["hit"] += hit
totalStat[tname]["total"] += total
continue

prevStat = prevCoreStat[node][name]
curStat = curCoreStat[node][name]

# no change #
if curStat["hit"] == prevStat["hit"]:
totalStat.pop(name, None)
totalStat.pop(tname, None)
continue

# diff #
totalStat[name]["hit"] += hit - prevStat["hit"]
totalStat[name]["total"] += total - prevStat["total"]
totalStat[tname]["hit"] += hit - prevStat["hit"]
totalStat[tname]["total"] += total - prevStat["total"]
except SystemExit:
sys.exit(0)
except:
Expand All @@ -59529,31 +59571,17 @@ def _printStats():

# get CPU usage #
try:
diff = SysMgr.uptimeDiff

# get CPU usage for me #
if diff >= 1:
cpuUsage = TaskAnalyzer.dbgObj.getCpuUsage(system=True)
else:
cpuUsage = [0, 0, 0, 100]
ttime = cpuUsage[0] / diff
utime = cpuUsage[1] / diff
stime = cpuUsage[2] / diff
mcpu = "%d%%" % ttime
mcpuStr = "%s(U%d%%+S%d%%)" % (mcpu, utime, stime)

# get CPU usage for system #
ctime = 100 - (cpuUsage[3] / diff)
ctime = ctime if ctime > 0 else 0
sysCpuStr = "%d%%" % ctime

# get available memory for system #
sysMemStr = SysMgr.getAvailMemInfo()
mcpuStr, sysCpuStr = SysMgr.getCpuUsageStr(
TaskAnalyzer.dbgObj, SysMgr.uptimeDiff
)
except SystemExit:
sys.exit(0)
except:
SysMgr.printErr("failed to get my CPU usage", True)
mcpuStr = sysCpuStr = sysMemStr = "?"
mcpuStr = sysCpuStr = "?"

# get available memory for system #
sysMemStr = SysMgr.getAvailMemInfo()

# print header stats #
SysMgr.addPrint(
Expand Down Expand Up @@ -59592,13 +59620,22 @@ def _printStats():

convNum = UtilMgr.convNum

# get average time for each function #
if sortval == "average":
for n in totalStat:
item = totalStat[n]
item["average"] = item["total"] / item["hit"]

# print stats #
for name, vals in sorted(
totalStat.items(), key=lambda e: e[1][sortval], reverse=True
):
hit = vals["hit"]
total = vals["total"]
avg = total / hit
if "average" in vals:
avg = vals["average"]
else:
avg = total / hit
per = UtilMgr.convNum(long((total / maxTime) * 100))

ret = SysMgr.addPrint(
Expand Down Expand Up @@ -77281,28 +77318,17 @@ def _checkRepeatCnt():

if DbusMgr.dbgObj:
# get CPU usage for myself #
cpuUsage = DbusMgr.dbgObj.getCpuUsage(system=True)
diff = SysMgr.uptimeDiff
ttime = cpuUsage[0] / diff
utime = cpuUsage[1] / diff
stime = cpuUsage[2] / diff
mcpu = "%d%%" % ttime
mcpu = UtilMgr.convCpuColor(ttime, mcpu)
mcpuStr = "%s(U%d%%+S%d%%)" % (mcpu, utime, stime)
mcpuStr, sysCpuStr = SysMgr.getCpuUsageStr(
DbusMgr.dbgObj, SysMgr.uptimeDiff
)

# get memory usage for myself #
rssStr = DbusMgr.dbgObj.getMemUsage()

# get CPU usage for system #
ctime = 100 - (cpuUsage[3] / diff)
ctime = ctime if ctime > 0 else 0
sysCpuStr = "%d%%" % ctime
sysCpuStr = UtilMgr.convCpuColor(ctime, sysCpuStr)

# get available memory for system #
sysMemStr = SysMgr.getAvailMemInfo()
else:
mcpuStr = rssStr = sysCpuStr = sysMemStr = "?"
mcpuStr = rssStr = sysCpuStr = "?"

# get available memory for system #
sysMemStr = SysMgr.getAvailMemInfo()

# set error #
nrErr = prevDbusData["totalErr"]
Expand Down Expand Up @@ -78622,24 +78648,13 @@ def printIntervalSummary(force=False):
# update CPU usage #
if DltAnalyzer.dbgObj:
# get CPU usage for myself #
cpuUsage = DltAnalyzer.dbgObj.getCpuUsage(system=True)
diff = SysMgr.uptimeDiff
ttime = cpuUsage[0] / diff
utime = cpuUsage[1] / diff
stime = cpuUsage[2] / diff
mcpu = "%d%%" % ttime
mcpu = UtilMgr.convCpuColor(ttime, mcpu)
mcpuStr = "%s(U%d%%+S%d%%)" % (mcpu, utime, stime)
mcpuStr, sysCpuStr = SysMgr.getCpuUsageStr(
DltAnalyzer.dbgObj, SysMgr.uptimeDiff
)

# get memory usage for myself #
rssStr = DltAnalyzer.dbgObj.getMemUsage()

# get CPU usage for system #
ctime = 100 - (cpuUsage[3] / diff)
ctime = ctime if ctime > 0 else 0
sysCpuStr = "%d%%" % ctime
sysCpuStr = UtilMgr.convCpuColor(ctime, sysCpuStr)

# get available memory for system #
sysMemStr = SysMgr.getAvailMemInfo()

Expand Down Expand Up @@ -87213,15 +87228,15 @@ def _finishPrint(self, needStop=False, term=False, flush=True):
ttimeStr = UtilMgr.convCpuColor(ttime, "%d%%" % ttime)
cpuStr = "%s(U%d%%+S%d%%)" % (ttimeStr, utime, stime)

# get memory usage for target #
rssStr = self.getMemUsage()

# get CPU usage for system #
ctime = 100 - (cpuUsage[3] / diff)
ctime = ctime if ctime > 0 else 0
sysCpuStr = "%d%%" % ctime
sysCpuStr = UtilMgr.convCpuColor(ctime, sysCpuStr)

# get memory usage for target #
rssStr = self.getMemUsage()

# get available memory for system #
sysMemStr = SysMgr.getAvailMemInfo()

Expand Down Expand Up @@ -125732,20 +125747,9 @@ def printFileStat(self, filters):
diff = 0.01

# get CPU usage for myself #
if diff >= 1:
cpuUsage = TaskAnalyzer.dbgObj.getCpuUsage(system=True)
else:
cpuUsage = [0, 0, 0, 100]
ttime = cpuUsage[0] / diff
utime = cpuUsage[1] / diff
stime = cpuUsage[2] / diff
mcpu = "%d%%" % ttime
mcpuStr = "%s(U%d%%+S%d%%)" % (mcpu, utime, stime)

# get CPU usage for system #
ctime = 100 - (cpuUsage[3] / diff)
ctime = ctime if ctime > 0 else 0
sysCpuStr = "%d%%" % ctime
mcpuStr, sysCpuStr = SysMgr.getCpuUsageStr(
TaskAnalyzer.dbgObj, diff
)

# get available memory for system #
sysMemStr = SysMgr.getAvailMemInfo()
Expand Down Expand Up @@ -137495,14 +137499,19 @@ def checkThreshold(
td = SysMgr.thrData

# get threshold attributes #
comval = {}
comval = []
if attr in td[resource]:
comval = td[resource][attr]
else:
for val in td[resource]:
if UtilMgr.isValidStr(attr, [val]):
comval = td[resource][val]
break
if not UtilMgr.isValidStr(attr, [val]):
continue
if type(td[resource][val]) is list:
comval += td[resource][val]
else:
comval.append(td[resource][val])
if not comval:
return False

# get previous usages #
if intval:
Expand Down Expand Up @@ -137606,7 +137615,7 @@ def _getTarget(addval, elist):
# check thresholds #
if type(comval) is list:
for comitem in comval:
_checkThreshold(comitem)
ret = _checkThreshold(comitem)
else:
_checkThreshold(comval)

Expand Down

0 comments on commit d27e514

Please sign in to comment.