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

Conversation

yurenchen000
Copy link

@yurenchen000 yurenchen000 commented Nov 27, 2022

This PR based on @ashamza 's work
hishamhm/htop#973 (comment)
#76 (comment)

// co-authors added in commits seem doesn't link to github users

The only thing I did was update the patch to fit the current version code.

@vlada-dudr
Copy link

Exactly what I wanted. Works good for me.

ProcessList.c Outdated
@@ -370,6 +370,16 @@ void ProcessList_collapseAllBranches(ProcessList* this) {
}
}

static void ProcessList_filterChildern(ProcessList *this, pid_t pid, Hashtable *processFilter) {
Copy link
Member

Choose a reason for hiding this comment

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

Typo Childern -> Children

Copy link
Author

Choose a reason for hiding this comment

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

fixed it

ProcessList.c Outdated
continue;

if (this->settings->showChildrenInFilter)
ProcessList_filterChildern(this, p->pid, filteredProcesses);
Copy link
Member

Choose a reason for hiding this comment

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

Typo Childern -> Children

Copy link
Author

Choose a reason for hiding this comment

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

fixed

Copy link
Member

@fasterit fasterit left a comment

Choose a reason for hiding this comment

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

Very nice work @yurenchen000.
This should have a key binding to be quickly toggled as well. May be Shift-F4 + Ctrl-F4?
(Marked the typo Childern, please fix)

@yurenchen000
Copy link
Author

yurenchen000 commented Jan 15, 2023

Very nice work @yurenchen000. This should have a key binding to be quickly toggled as well. May be Shift-F4 + Ctrl-F4? (Marked the typo Childern, please fix)

got it, I'll fix typo right now.

but I'm not sure if I can add keybinding for it

Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

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

I think those options should probably be exclusive: Either show parents or children, but showing both does not make any sense with filtering.

ProcessList.c Show resolved Hide resolved
code style format

Co-authored-by: BenBE <[email protected]>
@yurenchen000
Copy link
Author

yurenchen000 commented Jan 16, 2023

I think those options should probably be exclusive: Either show parents or children, but showing both does not make any sense with filtering.

I think it has practical uses, imagine this scenario:

  • filter by bash
    • view It's parents (ancestors): who opened it, a ssh or tmux
    • view It's children (descendants): what's it doing, run vim or some service

// equivalent to viewing its family tree

@BenBE
Copy link
Member

BenBE commented Jan 16, 2023

I was assuming this refered to the whole tree, not just the next level in each direction. In that case having both makes sense.

Copy link
Contributor

@tanriol tanriol left a comment

Choose a reason for hiding this comment

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

There's a couple of problems with this implementation. Also, I'm a bit worried about growing another thing dealing with process trees completely separate from tree view implementation. Would be nice to share the hard part in some reasonable way.

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).

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

@tanriol
Copy link
Contributor

tanriol commented Jan 16, 2023

Also, do these options make sense in flat view or are they intended for tree mode only?

@yurenchen000
Copy link
Author

yurenchen000 commented Jan 17, 2023

I was assuming this refered to the whole tree, not just the next level in each direction. In that case having both makes sense.

@BenBE
agree, use whole tree is my primary usage,
if matched item can highlight it'll perfect

@yurenchen000
Copy link
Author

Also, do these options make sense in flat view or are they intended for tree mode only?

@tanriol
yes, The current implementation can filter normally in non-tree view,
But I also think it doesn't make much sense in non-tree view.

Did you mean that limit this functionality to tree view mode,
And figure out a way to reuse the process tree information from the tree view?

@BenBE
Copy link
Member

BenBE commented Oct 8, 2023

Also, do these options make sense in flat view or are they intended for tree mode only?

Depending on the task it can make sense to have this available outside tree view too.

@thapakazi
Copy link

thapakazi commented Jul 4, 2024

anyone working on this ?

for now I am achieving similar with:

process_name=tmux; 
watch -n 1 -d "pgrep $process_name | xargs -I {} pstree -p {}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Completely new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants