Skip to content

Commit

Permalink
top: Reduce CPU usage for printing cgroup stat
Browse files Browse the repository at this point in the history
Signed-off-by: iipeace <[email protected]>
  • Loading branch information
iipeace committed Sep 21, 2023
1 parent fa884a1 commit bf8b7f2
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 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__ = "230920"
__revision__ = "230921"
__maintainer__ = "Peace Lee"
__email__ = "[email protected]"
__repository__ = "https://github.com/iipeace/guider"
Expand Down Expand Up @@ -873,6 +873,7 @@ class ConfigMgr(object):
RESSET = (
"SAVECPU",
"SAVEMEM",
"SAVEMEMPROC",
"SAVEBLOCK",
"SAVEPID",
"SAVERUNTIME",
Expand Down Expand Up @@ -33969,7 +33970,7 @@ def printHelp(force=False, isExit=True):
# {0:1} {1:1} -q PRINTCG:"cpu+cpuacct+memory+blkio"
# {0:1} {1:1} -q PRINTCG:"all"
# {0:1} {1:1} -q PRINTCG, MAXCGROWS:5
# {0:1} {1:1} -q PRINTCG, NOPRESSURE
# {0:1} {1:1} -q PRINTCG, NOPRESSURE, SKIPNOTASK

- {3:1} {2:1} with cgroup usage only for specific groups
# {0:1} {1:1} -q PRINTCG, CGFILTER:"*user.slice"
Expand Down Expand Up @@ -35909,6 +35910,9 @@ def printHelp(force=False, isExit=True):
- {2:1} except for pressure info
# {0:1} {1:1} -q NOPRESSURE

- {2:1} except for no task group
# {0:1} {1:1} -q SKIPNOTASK

- {2:1} after mounting all cgroup subsystems
# {0:1} {1:1} -q MOUNTCG

Expand Down Expand Up @@ -39344,8 +39348,8 @@ def printHelp(force=False, isExit=True):
- {2:1} with tasks
# {0:1} {1:1} -a

- {2:1} with only effective groups
# {0:1} {1:1} -q ONLYEFFECT
- {2:1} except for no task group
# {0:1} {1:1} -q SKIPNOTASK

- {2:1} with tasks only for specific type
# {0:1} {1:1} -a -q ONLYPROC
Expand Down Expand Up @@ -45662,7 +45666,7 @@ def freeDirsCnt(add=1):
# get file size #
size = UtilMgr.getFileSize(fpath, False)

msg = "'%s' [%s] to keep file count '%s' for '%s' [%s]" % (
msg = "'%s' [%s] to keep file count (%s) for '%s' [%s]" % (
fpath,
convSize(size),
limitCnt,
Expand Down Expand Up @@ -65959,7 +65963,13 @@ def doDecompress(args=[], ffilter=[], verb=True):

# check exist file #
if os.path.exists(outfile):
SysMgr.backupFile(outfile)
# backup file #
newFile = SysMgr.backupFile(outfile)

# replace input file #
if os.path.realpath(outfile) == item:
item = newFile

newline = False
else:
newline = True
Expand Down Expand Up @@ -72015,6 +72025,8 @@ def _updateValues(dirpath, subfiles, item):

def _getPaths(root, path):
for dirpath, subdirs, subfiles in path:
UtilMgr.printProgress()

# update subdir #
for item in subdirs:
subdir = os.path.join(dirpath, item)
Expand Down Expand Up @@ -72091,6 +72103,8 @@ def printCgroupInfo(self, printTitle=True, progress=False, cmd=False):

# get cgroup list #
try:
SysMgr.printInfo("start collecting cgroups...")

cgroupTree = self.getCgroupTree()
if not cgroupTree:
return
Expand Down Expand Up @@ -72177,7 +72191,7 @@ def _printDirTree(root, depth, totals={}, parent="", res=""):

# get options #
repComm = "REPCOMM" in SysMgr.environList
onlyEffect = "ONLYEFFECT" in SysMgr.environList
skipNoTask = "SKIPNOTASK" in SysMgr.environList

# sum values for subdirs #
newTotals = {}
Expand Down Expand Up @@ -72793,7 +72807,7 @@ def _getIoStat(pid, task):
# leap node #
else:
if (
onlyEffect
skipNoTask
and nrProcs in ("0", 0)
and nrTasks in ("0", 0)
and not tempSubdir
Expand Down Expand Up @@ -103564,7 +103578,7 @@ def _addEntryOldState(cmd, args, isExtended=False):
# decompress gnu_debugdata section #
try:
if (
e_shdbgdata >= 0
e_shdbgdata < 0
or not ".gnu_debugdata" in self.attr["sectionHeader"]
):
raise ValueError("no .gnu_debugdata")
Expand Down Expand Up @@ -125560,11 +125574,24 @@ def _getStats(root, path, sub, dftDepth, isVer2):
# register subsystem #
root.setdefault(sub, {})

# check skip condition #
skipNoTask = "SKIPNOTASK" in SysMgr.environList

for dirpath, subdirs, subfiles in path:
# check depth #
if SysMgr.funcDepth > 0:
depth = dirpath.count("/") - dftDepth
if depth >= SysMgr.funcDepth:
del subdirs[:]
continue

# check no task group #
if skipNoTask and "tasks" in subfiles:
taskfile = os.path.join(dirpath, "tasks")
fd = SysMgr.getFd(taskfile, "r")
fd.seek(0)
stat = fd.read()
if not stat:
continue

# update subfiles #
Expand Down Expand Up @@ -133874,7 +133901,7 @@ def handleEventCmd(self, cmd, source, user):
name = "USER" if user else source

# set command name #
ocmd = cmd.split(":", 1)[0]
ocmd = cmd.split(":", 1)[0].split("#", 1)[0]

# SAVE #
if ocmd in ("SAVE", "SAVERAW", "SAVEMIN") or ocmd in ConfigMgr.RESSET:
Expand Down Expand Up @@ -134673,8 +134700,11 @@ def handleSaveCmd(self, cmd, event, form=None):
if form == "SAVECPU":
SysMgr.totalEnable = True
SysMgr.sort = "c"
elif form == "SAVEMEM":
elif form in ("SAVEMEM", "SAVEMEMPROC"):
SysMgr.memEnable = True
SysMgr.sort = "m"
if form == "SAVEMEM":
SysMgr.addEnvironVar("NOSMAPS")
elif form == "SAVEBLOCK":
SysMgr.sort = "b"
elif form == "SAVEPID":
Expand Down

0 comments on commit bf8b7f2

Please sign in to comment.