psstree 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- psstree/__main__.py +37 -0
- psstree/tui.py +53 -0
- psstree/utils.py +134 -0
- psstree-0.1.0.dist-info/METADATA +14 -0
- psstree-0.1.0.dist-info/RECORD +8 -0
- psstree-0.1.0.dist-info/WHEEL +5 -0
- psstree-0.1.0.dist-info/licenses/LICENSE +21 -0
- psstree-0.1.0.dist-info/top_level.txt +1 -0
psstree/__main__.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
|
|
3
|
+
from .utils import Mapping
|
|
4
|
+
|
|
5
|
+
if __name__ == "__main__":
|
|
6
|
+
parser = argparse.ArgumentParser(description="gets process' PSS via smem, sorts it descendingly within their parent-child hierarchy, and displays it in an interactive TUI with collapsible sections")
|
|
7
|
+
|
|
8
|
+
parser.add_argument(
|
|
9
|
+
"--expand",
|
|
10
|
+
action="store_true",
|
|
11
|
+
help="[TUI only] expand all collapsible sections at start"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
parser.add_argument(
|
|
15
|
+
"--raw",
|
|
16
|
+
action="store_true",
|
|
17
|
+
help="disable TUI and dump formatted repr directly to stdout"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
args = parser.parse_args()
|
|
21
|
+
|
|
22
|
+
mapping = Mapping()
|
|
23
|
+
mapping.create(repr=True)
|
|
24
|
+
raw_output = mapping.repr
|
|
25
|
+
|
|
26
|
+
if args.raw:
|
|
27
|
+
print(raw_output, flush=True)
|
|
28
|
+
else:
|
|
29
|
+
try:
|
|
30
|
+
from .tui import TreeApp
|
|
31
|
+
app = TreeApp(mapping=mapping, expand=args.expand)
|
|
32
|
+
app.run()
|
|
33
|
+
except Exception:
|
|
34
|
+
print(raw_output, flush=True)
|
|
35
|
+
raise
|
|
36
|
+
|
|
37
|
+
|
psstree/tui.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from textual.widgets import Tree, Footer, Header
|
|
2
|
+
from textual.app import App
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from utils import Mapping
|
|
7
|
+
from textual.widget._tree import TreeNode
|
|
8
|
+
from textual.app import ComposeResult
|
|
9
|
+
|
|
10
|
+
__all__ = ["TreeApp"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def dfs(parent: "TreeNode", pid: int, mapping: "Mapping"):
|
|
15
|
+
proc = mapping.PROCS[pid]
|
|
16
|
+
node = parent.add(
|
|
17
|
+
label=proc.label,
|
|
18
|
+
data=proc,
|
|
19
|
+
)
|
|
20
|
+
for child in proc.children:
|
|
21
|
+
dfs(node, child, mapping)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class TreeApp(App):
|
|
25
|
+
BINDINGS = [
|
|
26
|
+
("q", "quit", "Quit"),
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
mapping: "Mapping",
|
|
32
|
+
expand: bool = False,
|
|
33
|
+
*args,
|
|
34
|
+
**kwargs
|
|
35
|
+
):
|
|
36
|
+
super().__init__(*args, **kwargs)
|
|
37
|
+
self.title = "Press q to quit"
|
|
38
|
+
self.mapping = mapping
|
|
39
|
+
self.expand = expand
|
|
40
|
+
|
|
41
|
+
def compose(self) -> "ComposeResult":
|
|
42
|
+
yield Header()
|
|
43
|
+
tree: Tree[str] = Tree(label="PSS (Proportional Set Size) `command(pid): <individual-PSS>/<total-PSS-including-all-children>`")
|
|
44
|
+
for root in self.mapping.ROOTS:
|
|
45
|
+
dfs(tree.root, root, self.mapping)
|
|
46
|
+
tree.root.expand()
|
|
47
|
+
for node in tree.root.children:
|
|
48
|
+
node.expand()
|
|
49
|
+
if self.expand:
|
|
50
|
+
tree.root.expand_all()
|
|
51
|
+
yield tree
|
|
52
|
+
yield Footer()
|
|
53
|
+
|
psstree/utils.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import subprocess
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from typing import List, Dict, Set, Optional
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Proc:
|
|
8
|
+
pid: int
|
|
9
|
+
ppid: int
|
|
10
|
+
comm: str
|
|
11
|
+
val: int
|
|
12
|
+
|
|
13
|
+
depth: int = 0
|
|
14
|
+
children: List[int] = field(default_factory=list)
|
|
15
|
+
traversal_order: List[str] = field(default_factory=list)
|
|
16
|
+
total: int = 0
|
|
17
|
+
label: str = "<??>"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_indents(depths):
|
|
21
|
+
size = len(depths)
|
|
22
|
+
|
|
23
|
+
# depths[ < 0 ] -> infty
|
|
24
|
+
# depths[ >= size ] -> -1
|
|
25
|
+
|
|
26
|
+
result = []
|
|
27
|
+
|
|
28
|
+
for idx in range(size):
|
|
29
|
+
if idx>0 and depths[idx-1] < depths[idx]:
|
|
30
|
+
start = " "*(depths[idx]-1) + "╰─"
|
|
31
|
+
if (idx+1)<size and depths[idx] <= depths[idx+1]:
|
|
32
|
+
start += "┬─"
|
|
33
|
+
else:
|
|
34
|
+
start += "──"
|
|
35
|
+
elif idx>0 and depths[idx-1] == depths[idx]:
|
|
36
|
+
start = " "*depths[idx]
|
|
37
|
+
if (idx+1)<size and depths[idx] <= depths[idx+1]:
|
|
38
|
+
start += "├─"
|
|
39
|
+
else:
|
|
40
|
+
start += "╰─"
|
|
41
|
+
elif idx==0 or depths[idx-1] > depths[idx]:
|
|
42
|
+
start = " "*depths[idx]
|
|
43
|
+
if (idx+1)<size and depths[idx] <= depths[idx+1]:
|
|
44
|
+
start += "┌─"
|
|
45
|
+
else:
|
|
46
|
+
start += "──"
|
|
47
|
+
|
|
48
|
+
result.append(start)
|
|
49
|
+
|
|
50
|
+
return result
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Mapping:
|
|
54
|
+
def __init__(self):
|
|
55
|
+
self.PROCS: Dict[int, Proc] = {}
|
|
56
|
+
self.ROOTS: List[int] = []
|
|
57
|
+
self.traversal_order: Optional[List[int]] = None
|
|
58
|
+
self.repr: str = "<??>"
|
|
59
|
+
|
|
60
|
+
def dfs(self, pid: int, depth: int, visited: Set[int], visiting: Set[int]):
|
|
61
|
+
if pid in visiting:
|
|
62
|
+
raise ValueError(f"circular reference \n{pid=}, {depth=}, {visited=}, {visiting=}, {self.ROOTS=}, {self.PROCS=}\n circular reference")
|
|
63
|
+
|
|
64
|
+
if pid in visited:
|
|
65
|
+
raise ValueError(f"multiple parents \n{pid=}, {depth=}, {visited=}, {visiting=}, {self.ROOTS=}, {self.PROCS=}\n multiple parents")
|
|
66
|
+
|
|
67
|
+
visiting.add(pid)
|
|
68
|
+
|
|
69
|
+
proc = self.PROCS[pid]
|
|
70
|
+
|
|
71
|
+
for child in proc.children:
|
|
72
|
+
self.dfs(child, depth+1, visited, visiting)
|
|
73
|
+
|
|
74
|
+
proc.depth = depth
|
|
75
|
+
proc.children.sort(key=lambda id: self.PROCS[id].total, reverse=True)
|
|
76
|
+
proc.traversal_order = sum((self.PROCS[child].traversal_order for child in proc.children), start=[pid])
|
|
77
|
+
proc.total = sum((self.PROCS[child].total for child in proc.children), start=proc.val)
|
|
78
|
+
proc.label = f"{proc.comm}({proc.pid}): {proc.val/1024:.2f}MB/{proc.total/1024:.2f}MB"
|
|
79
|
+
|
|
80
|
+
visiting.remove(pid)
|
|
81
|
+
visited.add(pid)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def create(self, repr=False):
|
|
85
|
+
self.PROCS.clear()
|
|
86
|
+
self.ROOTS.clear()
|
|
87
|
+
|
|
88
|
+
lines = [
|
|
89
|
+
line.strip().split()
|
|
90
|
+
for line in subprocess.check_output(
|
|
91
|
+
["smem", "-c", "pid pss", "-H"],
|
|
92
|
+
text=True
|
|
93
|
+
).splitlines()
|
|
94
|
+
if line.strip()
|
|
95
|
+
]
|
|
96
|
+
|
|
97
|
+
PSSs = {int(line[0]): int(line[1]) for line in lines}
|
|
98
|
+
|
|
99
|
+
lines = [
|
|
100
|
+
line.strip().split()
|
|
101
|
+
for line in subprocess.check_output(
|
|
102
|
+
["ps", "-eo", "pid,ppid,comm", "--no-header"],
|
|
103
|
+
text=True
|
|
104
|
+
).splitlines()
|
|
105
|
+
if line.strip()
|
|
106
|
+
]
|
|
107
|
+
|
|
108
|
+
for pid, ppid, comm in lines:
|
|
109
|
+
pid = int(pid)
|
|
110
|
+
ppid = int(ppid)
|
|
111
|
+
if pid not in PSSs:
|
|
112
|
+
continue
|
|
113
|
+
self.PROCS[pid] = Proc(pid=pid, ppid=ppid, comm=comm, val=PSSs[pid])
|
|
114
|
+
|
|
115
|
+
for proc in self.PROCS.values():
|
|
116
|
+
if proc.ppid in self.PROCS:
|
|
117
|
+
self.PROCS[proc.ppid].children.append(proc.pid)
|
|
118
|
+
else:
|
|
119
|
+
self.ROOTS.append(proc.pid)
|
|
120
|
+
|
|
121
|
+
for root in self.ROOTS:
|
|
122
|
+
self.dfs(root, 0, set(), set())
|
|
123
|
+
|
|
124
|
+
self.ROOTS.sort(key=lambda pid: self.PROCS[pid].total, reverse=True)
|
|
125
|
+
self.traversal_order = sum((self.PROCS[root].traversal_order for root in self.ROOTS), start=[])
|
|
126
|
+
|
|
127
|
+
if repr:
|
|
128
|
+
indents = get_indents([self.PROCS[pid].depth for pid in self.traversal_order])
|
|
129
|
+
|
|
130
|
+
self.repr = "\n".join([
|
|
131
|
+
indent + self.PROCS[pid].label
|
|
132
|
+
for indent, pid in zip(indents, self.traversal_order)
|
|
133
|
+
])
|
|
134
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: psstree
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: list process' PSS sorted descendingly within their parent-child hierarchy
|
|
5
|
+
Author: Anosrep Enilno
|
|
6
|
+
Project-URL: Repository, https://github.com/anosrepenilno/psstree
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: textual>=8.1.1
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# PSS-tree
|
|
14
|
+
gets process' PSS via smem, sorts it descendingly within their parent-child hierarchy, and displays it in an interactive TUI with collapsible sections
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
psstree/__main__.py,sha256=usGKnJai5BVcoKvVD2Rr6yajLl-idWKwzSv_cRTol2c,980
|
|
2
|
+
psstree/tui.py,sha256=qK1wWTjXOJBQjh0VnwojNSsORjVVtCTZ8eGMkHlux6I,1339
|
|
3
|
+
psstree/utils.py,sha256=jFl_Msa42RwO0cr3CCMUKVeUber81JONcSOa465zxns,4260
|
|
4
|
+
psstree-0.1.0.dist-info/licenses/LICENSE,sha256=fyRfZ3h3EKGD6oFFvtUw8t1ksjijbQJZOoR_usLM29U,1070
|
|
5
|
+
psstree-0.1.0.dist-info/METADATA,sha256=a1T7PiBXjwXAfsKaJ56FcnVzaBYOViIy-PbYyMkfFJw,524
|
|
6
|
+
psstree-0.1.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
7
|
+
psstree-0.1.0.dist-info/top_level.txt,sha256=bUhxyIBQf_rY3BCj0prniIHV0oHedmEr2CDC5t_QtpA,8
|
|
8
|
+
psstree-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 anosrepenilno
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
psstree
|