Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to display parents and children processes of filtered pro… #1139

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions DisplayOptionsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
Panel_add(super, (Object*) CheckItem_newByRef("- Tree view is collapsed by default", &(settings->ss->allBranchesCollapsed)));
Panel_add(super, (Object*) TextItem_new("Global options:"));
Panel_add(super, (Object*) CheckItem_newByRef("Show tabs for screens", &(settings->screenTabs)));
Panel_add(super, (Object*) CheckItem_newByRef("Show parent processes during filter", &(settings->showParentsInFilter)));
Panel_add(super, (Object*) CheckItem_newByRef("Show children processes during filter", &(settings->showChildrenInFilter)));
Panel_add(super, (Object*) CheckItem_newByRef("Shadow other users' processes", &(settings->shadowOtherUsers)));
Panel_add(super, (Object*) CheckItem_newByRef("Hide kernel threads", &(settings->hideKernelThreads)));
Panel_add(super, (Object*) CheckItem_newByRef("Hide userland process threads", &(settings->hideUserlandThreads)));
Expand Down
33 changes: 32 additions & 1 deletion ProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ void ProcessList_collapseAllBranches(ProcessList* this) {
}
}

static void ProcessList_filterChildren(ProcessList *this, pid_t pid, Hashtable *processFilter) {
for (int i = Vector_size(this->processes) - 1; i >= 0; i--) {
Process *p = (Process*) (Vector_get(this->processes, i));
if (p->pid != pid && Process_isChildOf(p, pid)) {
Hashtable_put(processFilter, p->pid, (void*) 1);
ProcessList_filterChildren(this, p->pid, processFilter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks quadratic to me (for every descendant of the target process iterate over all processes/threads). Could we do better?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an idea how to do better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even a single loop over processes with walking up from every process would be better (although it would need some cycle detection too) as it can easily be made worst case linear in the total process number. I personally think something like the tree mode "sort by parent" would be even better, but I'm a bit out of context at the moment and not sure how feasible it would be to share that part instead of reimplementing it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could make the hit detection (in subtree/any parent) in O(1) if we tracked some item ranges for nested sets (similar to what Joomla is doing for its menus). Unfortunately, when we tried to implement this in-place while building the tree, this made tree creation kinda slow (O(n²)). One option could be to calculate the nested set ranges on the fly from the Table->displayList based on the indent columns; takes at most O(n*m) with m being the maximum tree depth (which is fixed at 32).

}
}
}

void ProcessList_rebuildPanel(ProcessList* this) {
ProcessList_updateDisplayList(this);

Expand All @@ -392,14 +402,34 @@ void ProcessList_rebuildPanel(ProcessList* this) {

const int processCount = Vector_size(this->displayList);
int idx = 0;

// Mark Children and Parent for F4-Filter
Hashtable* filteredProcesses = Hashtable_new(processCount, false);
for (int i = 0; i < processCount; i++) {
Process* p = (Process*)Vector_get(this->processes, i);
pid_t ppid = Process_getParentPid(p);

if (incFilter && !(String_contains_i(Process_getCommand(p), incFilter, true)))
continue;

if (this->settings->showChildrenInFilter)
ProcessList_filterChildren(this, p->pid, filteredProcesses);

do {
yurenchen000 marked this conversation as resolved.
Show resolved Hide resolved
Hashtable_put(filteredProcesses, p->pid, (void*) 1);
ppid = Process_getParentPid(p);
p = Hashtable_get(this->processTable, ppid);
} while (this->settings->showParentsInFilter && p && p->pid != p->ppid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we know from #1017 (which I still need to rebase/reimplement), in some cases two processes may be parents of each other and thus this loop becomes an infinite one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it,
I will keep an eye on the progress of that issue

}

bool foundFollowed = false;

for (int i = 0; i < processCount; i++) {
Process* p = (Process*) Vector_get(this->displayList, i);

if ( (!p->show)
|| (this->userId != (uid_t) -1 && (p->st_uid != this->userId))
|| (incFilter && !(String_contains_i(Process_getCommand(p), incFilter, true)))
|| (incFilter && !Hashtable_get(filteredProcesses, p->pid))
|| (this->pidMatchList && !Hashtable_get(this->pidMatchList, p->tgid)) )
continue;

Expand Down Expand Up @@ -428,6 +458,7 @@ void ProcessList_rebuildPanel(ProcessList* this) {

this->panel->scrollV = currScrollV;
}
Hashtable_delete(filteredProcesses);
}

Process* ProcessList_getProcess(ProcessList* this, pid_t pid, bool* preExisting, Process_New constructor) {
Expand Down
8 changes: 8 additions & 0 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini
// old (no screen) naming also supported for backwards compatibility
screen = Settings_defaultScreens(this);
screen->allBranchesCollapsed = atoi(option[1]);
} else if (String_eq(option[0], "show_parents_in_filter")) {
this->showParentsInFilter = atoi(option[1]);
} else if (String_eq(option[0], "show_children_in_filter")) {
this->showChildrenInFilter = atoi(option[1]);
} else if (String_eq(option[0], "hide_kernel_threads")) {
this->hideKernelThreads = atoi(option[1]);
} else if (String_eq(option[0], "hide_userland_threads")) {
Expand Down Expand Up @@ -624,6 +628,8 @@ int Settings_write(const Settings* this, bool onCrash) {
// Legacy compatibility with older versions of htop
printSettingInteger("tree_view", this->screens[0]->treeView);
// This "-1" is for compatibility with the older enum format.
printSettingInteger("show_parents_in_filter", this->showParentsInFilter);
printSettingInteger("show_children_in_filter", this->showChildrenInFilter);
printSettingInteger("sort_key", this->screens[0]->sortKey - 1);
printSettingInteger("tree_sort_key", this->screens[0]->treeSortKey - 1);
printSettingInteger("sort_direction", this->screens[0]->direction);
Expand Down Expand Up @@ -673,6 +679,8 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicColumns)
this->hideKernelThreads = true;
this->hideUserlandThreads = false;
this->hideRunningInContainer = false;
this->showParentsInFilter = false;
this->showChildrenInFilter = false;
this->highlightBaseName = false;
this->highlightDeletedExe = true;
this->highlightMegabytes = true;
Expand Down
2 changes: 2 additions & 0 deletions Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ typedef struct Settings_ {
bool showCPUTemperature;
bool degreeFahrenheit;
#endif
bool showParentsInFilter;
bool showChildrenInFilter;
bool showProgramPath;
bool shadowOtherUsers;
bool showThreadNames;
Expand Down