Skip to content

Commit

Permalink
Linux: reorder some calls in LinuxProcessList_recurseProcTree()
Browse files Browse the repository at this point in the history
Improve maintainability by reordering calls in
LinuxProcessList_recurseProcTree() to group them by side-effect or
interdependency.
  • Loading branch information
cgzones committed Apr 17, 2023
1 parent 5d6cecb commit 5b3e444
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions linux/LinuxProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -1636,11 +1636,6 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
continue;
}

const bool scanMainThread = !hideUserlandThreads && !Process_isKernelThread(proc) && !mainTask;

if (ss->flags & PROCESS_FLAG_IO)
LinuxProcessList_readIoFile(lp, procFd, scanMainThread, pl->realtimeMs);

if (!LinuxProcessList_readStatmFile(lp, procFd, mainTask))
goto errorReadingProcess;

Expand Down Expand Up @@ -1669,20 +1664,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
proc->mergedCommand.lastUpdate = 0;
}

if ((ss->flags & PROCESS_FLAG_LINUX_SMAPS) && !Process_isKernelThread(proc)) {
if (!mainTask) {
// Read smaps file of each process only every second pass to improve performance
static int smaps_flag = 0;
if ((pid & 1) == smaps_flag) {
LinuxProcessList_readSmapsFile(lp, procFd, this->haveSmapsRollup);
}
if (pid == 1) {
smaps_flag = !smaps_flag;
}
} else {
lp->m_pss = ((const LinuxProcess*)mainTask)->m_pss;
}
}
const bool scanMainThread = !hideUserlandThreads && !Process_isKernelThread(proc) && !mainTask;

char statCommand[MAX_NAME + 1];
unsigned long long int lasttimes = (lp->utime + lp->stime);
Expand All @@ -1699,10 +1681,6 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
proc->tty_name = LinuxProcessList_updateTtyDevice(this->ttyDrivers, proc->tty_nr);
}

if (ss->flags & PROCESS_FLAG_LINUX_IOPRIO) {
LinuxProcess_updateIOPriority(lp);
}

/*
* - period might be 0 after system sleep
* - stime/utime might drop when switching to scan the main thread instead of the whole process
Expand Down Expand Up @@ -1776,6 +1754,27 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
}
}

if ((ss->flags & PROCESS_FLAG_LINUX_SMAPS) && !Process_isKernelThread(proc)) {
if (!mainTask) {
// Read smaps file of each process only every second pass to improve performance
static int smaps_flag = 0;
if ((pid & 1) == smaps_flag) {
LinuxProcessList_readSmapsFile(lp, procFd, this->haveSmapsRollup);
}
if (pid == 1) {
smaps_flag = !smaps_flag;
}
} else {
lp->m_pss = ((const LinuxProcess*)mainTask)->m_pss;
lp->m_swap = ((const LinuxProcess*)mainTask)->m_swap;
lp->m_psswp = ((const LinuxProcess*)mainTask)->m_psswp;
}
}

if (ss->flags & PROCESS_FLAG_IO) {
LinuxProcessList_readIoFile(lp, procFd, scanMainThread, pl->realtimeMs);
}

#ifdef HAVE_DELAYACCT
if (ss->flags & PROCESS_FLAG_LINUX_DELAYACCT) {
LinuxProcessList_readDelayAcctData(this, lp);
Expand All @@ -1786,6 +1785,10 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
LinuxProcessList_readOomData(lp, procFd, mainTask);
}

if (ss->flags & PROCESS_FLAG_LINUX_IOPRIO) {
LinuxProcess_updateIOPriority(lp);
}

if (ss->flags & PROCESS_FLAG_LINUX_SECATTR) {
LinuxProcessList_readSecattrData(lp, procFd, mainTask);
}
Expand All @@ -1804,15 +1807,15 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, openat_arg_
}
#endif

/*
* Final section after all data has been gathered
*/

if (!proc->cmdline && statCommand[0] &&
(proc->state == ZOMBIE || Process_isKernelThread(proc) || settings->showThreadNames)) {
Process_updateCmdline(proc, statCommand, 0, strlen(statCommand));
}

/*
* Final section after all data has been gathered
*/

proc->updated = true;
Compat_openatArgClose(procFd);

Expand Down

0 comments on commit 5b3e444

Please sign in to comment.