From d27e5146556ea206327f560b5e871b71bf595399 Mon Sep 17 00:00:00 2001 From: iipeace Date: Sun, 26 Nov 2023 22:52:26 +0900 Subject: [PATCH] kftop: Add sort option by average time Signed-off-by: iipeace --- guider/guider.conf | 10 +-- guider/guider.py | 179 ++++++++++++++++++++++++--------------------- 2 files changed, 99 insertions(+), 90 deletions(-) diff --git a/guider/guider.conf b/guider/guider.conf index 3d6ec401..57db1751 100644 --- a/guider/guider.conf +++ b/guider/guider.conf @@ -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 @@ -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 diff --git a/guider/guider.py b/guider/guider.py index 5c4b97eb..507a85c5 100755 --- a/guider/guider.py +++ b/guider/guider.py @@ -7,7 +7,7 @@ __credits__ = "Peace Lee" __license__ = "GPLv2" __version__ = "3.9.8" -__revision__ = "231124" +__revision__ = "231126" __maintainer__ = "Peace Lee" __email__ = "iipeace5@gmail.com" __repository__ = "https://github.com/iipeace/guider" @@ -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: @@ -36372,6 +36399,7 @@ def _getDesc(s, t=0): -m set terminal size -a show all functions -g set function filter + -c set core filter -Q print all rows in a stream -q set environment variables -E set cache dir path @@ -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, @@ -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: @@ -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": @@ -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, } @@ -59502,8 +59544,8 @@ 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] @@ -59511,12 +59553,12 @@ def _applyStats(node, path): # 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: @@ -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( @@ -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( @@ -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"] @@ -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() @@ -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() @@ -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() @@ -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: @@ -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)