-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'johan/pxtree' into python
- Loading branch information
Showing
13 changed files
with
379 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
.Dd October 2, 2023 | ||
.Dt PXTREE 1 | ||
.Os | ||
.Sh NAME | ||
.Nm pxtree | ||
.Nd display running processes in a tree | ||
.Sh SYNOPSIS | ||
.\" FIXME: Other man pages don't need to use \p to break lines here, | ||
.\" and use the Nm macro for the command name. Why can't we? | ||
.Ic pxtree [ --debug ] [ SEARCH ] | ||
.Sh DESCRIPTION | ||
.Nm | ||
lists all running processes in a tree. | ||
.Pp | ||
Process names are followed by their PIDs in parentheses. If multiple processes | ||
have the same names, they will be coalesced into one entry marked with (3×) in | ||
bold, where | ||
.Sy 3 | ||
in this case is the number of duplicates. | ||
.Pp | ||
The optional | ||
.Cm SEARCH | ||
parameter can be: | ||
.Bl -bullet | ||
.It | ||
A PID (Process ID) | ||
.It | ||
A process name or part of one | ||
.It | ||
A user name | ||
.El | ||
.Pp | ||
Any search hits will be highlighted in bold. All parents and children of all | ||
search hits will always be listed. | ||
.Pp | ||
With the | ||
.Fl -debug | ||
flag, extra log messages are sometimes printed after | ||
.Nm | ||
finishes. | ||
.Sh PROCESS NAMING | ||
.Nm | ||
tries to be helpful about naming processes, and avoid printing names | ||
of various VMs. | ||
.Pp | ||
For example, if you do | ||
.Sy java -jar foo.jar , | ||
.Nm | ||
will show this process as | ||
.Sy foo.jar | ||
rather than | ||
.Sy java . | ||
.El | ||
.Sh SEE ALSO | ||
.Xr px 1 , | ||
.Xr ptop 1 | ||
.Sh HOMEPAGE | ||
.Nm | ||
lives at http://github.com/walles/px |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
from . import px_process | ||
from . import px_terminal | ||
|
||
from typing import Set, List, Optional, Iterable | ||
|
||
|
||
def tree(search: str) -> None: | ||
"""Print a process tree""" | ||
for line in _generate_tree(px_process.get_all(), search): | ||
print(line) | ||
|
||
|
||
def _generate_tree(processes: List[px_process.PxProcess], search: str) -> List[str]: | ||
# Only print subtrees needed for showing all search hits and their children. | ||
# | ||
# We do that by starting at the search hits and walking up the tree from all | ||
# of them, collecting each PID we see along the way. Then when we render the | ||
# tree, we only render those PIDs. | ||
show_pids: Set[int] = set() | ||
if search: | ||
for process in processes: | ||
if not process.match(search): | ||
continue | ||
|
||
_mark_children(process, show_pids) | ||
|
||
if process and process.parent: | ||
process = process.parent | ||
while process: | ||
if process.pid in show_pids: | ||
break | ||
show_pids.add(process.pid) | ||
if not process.parent: | ||
break | ||
process = process.parent | ||
|
||
if not processes: | ||
return [] | ||
|
||
lines = [] | ||
coalescer = Coalescer(0) | ||
lines += coalescer.submit(processes[0], search) | ||
lines += coalescer.flush() | ||
return lines + _generate_child_tree(processes[0].children, 1, search, show_pids) | ||
|
||
|
||
def _mark_children(process: px_process.PxProcess, show_pids: Set[int]) -> None: | ||
"""Recursively mark all children of a search hit as needing to be shown""" | ||
show_pids.add(process.pid) | ||
for child in process.children: | ||
_mark_children(child, show_pids) | ||
|
||
|
||
class Coalescer: | ||
def __init__(self, indent: int) -> None: | ||
self._base: Optional[px_process.PxProcess] = None | ||
self._count = 0 | ||
self._indent = indent | ||
|
||
def submit(self, process: px_process.PxProcess, search: str) -> List[str]: | ||
"""Returns an array of zero or more lines to be printed""" | ||
is_search_hit = search and process.match(search) | ||
has_children = bool(process.children) | ||
is_candidate = not is_search_hit and not has_children | ||
|
||
# If we can coalesce this, do it! | ||
if self._base and is_candidate and self._base.command == process.command: | ||
self._count += 1 | ||
return [] | ||
if not self._base and is_candidate: | ||
self._base = process | ||
self._count = 1 | ||
return [] | ||
|
||
return_me = [] | ||
|
||
# Otherwise, print the coalesced line if we have one | ||
if self._base: | ||
return_me += self.flush() | ||
|
||
# Can we coalesce this line? | ||
if is_candidate: | ||
self._base = process | ||
self._count = 1 | ||
return return_me | ||
|
||
# And print the current line | ||
if is_search_hit: | ||
return_me.append( | ||
f"{' ' * self._indent}{px_terminal.bold(process.command)}({process.pid})" | ||
) | ||
else: | ||
return_me.append(f"{' ' * self._indent}{process.command}({process.pid})") | ||
|
||
return return_me | ||
|
||
def flush(self) -> List[str]: | ||
if not self._base: | ||
return [] | ||
|
||
assert self._count > 0 | ||
|
||
return_me: str | ||
if self._count == 1: | ||
return_me = f"{' ' * self._indent}{self._base.command}({self._base.pid})" | ||
else: | ||
return_me = f"{' ' * self._indent}{self._base.command}... ({px_terminal.bold(f'{self._count}×')})" | ||
|
||
self._base = None | ||
self._count = 0 | ||
|
||
return [return_me] | ||
|
||
|
||
def _generate_child_tree( | ||
children: Iterable[px_process.PxProcess], | ||
indent: int, | ||
search: str, | ||
show_pids: Set[int], | ||
) -> List[str]: | ||
lines = [] | ||
|
||
coalescer = Coalescer(indent) | ||
for child in sorted( | ||
children, key=lambda p: (p.command.lower(), bool(p.children), p.pid) | ||
): | ||
if show_pids and child.pid not in show_pids: | ||
continue | ||
|
||
lines += coalescer.submit(child, search) | ||
lines += _generate_child_tree(child.children, indent + 1, search, show_pids) | ||
|
||
lines += coalescer.flush() | ||
return lines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
# Run pxtree from current sources | ||
|
||
PYTHONPATH=px:$(echo env/lib/python*/site-packages) python3 -m px.px --tree "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.