ProcessList_buildTree: lookup parent via hashtable

While this change does not significantly affect performance, it removes
the internal requirement to have the process list sorted by PID.
This commit is contained in:
Denis Lisov 2021-12-13 01:34:27 +03:00 committed by BenBE
parent a3a7958721
commit 1a403eb7eb
1 changed files with 8 additions and 20 deletions

View File

@ -433,36 +433,24 @@ static void ProcessList_buildTree(ProcessList* this) {
} }
pid_t ppid = Process_getParentPid(process); pid_t ppid = Process_getParentPid(process);
bool isRoot = false;
// Bisect the process vector to find parent
int l = 0;
int r = size;
// If PID corresponds with PPID (e.g. "kernel_task" (PID:0, PPID:0) // If PID corresponds with PPID (e.g. "kernel_task" (PID:0, PPID:0)
// on Mac OS X 10.11.6) cancel bisecting and regard this process as // on Mac OS X 10.11.6) regard this process as root.
// root.
if (process->pid == ppid) if (process->pid == ppid)
r = 0; isRoot = true;
// On Linux both the init process (pid 1) and the root UMH kernel thread (pid 2) // On Linux both the init process (pid 1) and the root UMH kernel thread (pid 2)
// use a ppid of 0. As that PID can't exist, we can skip searching for it. // use a ppid of 0. As that PID can't exist, we can skip searching for it.
if (!ppid) if (!ppid)
r = 0; isRoot = true;
while (l < r) { // Lookup the parent via the processTable hashtable not modified in buildTree
int c = (l + r) / 2; if (ProcessList_findProcess(this, ppid) == NULL)
pid_t pid = ((Process*)Vector_get(this->processes, c))->pid; isRoot = true;
if (ppid == pid) {
break;
} else if (ppid < pid) {
r = c;
} else {
l = c + 1;
}
}
// If parent not found, then construct the tree with this node as root // If parent not found, then construct the tree with this node as root
if (l >= r) { if (isRoot) {
process = (Process*)Vector_take(this->processes, i); process = (Process*)Vector_take(this->processes, i);
process->indent = 0; process->indent = 0;
process->tree_depth = 0; process->tree_depth = 0;