Initial commit
This commit is contained in:
51
profile.txt
Normal file
51
profile.txt
Normal file
@@ -0,0 +1,51 @@
|
||||
Wrote profile results to 'main.py.lprof'
|
||||
Timer unit: 1e-06 s
|
||||
|
||||
Total time: 0.465667 s
|
||||
File: main.py
|
||||
Function: get_process_swap_usage at line 29
|
||||
|
||||
Line # Hits Time Per Hit % Time Line Contents
|
||||
==============================================================
|
||||
29 @profile
|
||||
30 def get_process_swap_usage() -> None:
|
||||
31 1 4.7 4.7 0.0 print(f"{'PID':>6} {'Name':<25} {'Swap (MB)':>10}")
|
||||
32 1 1.9 1.9 0.0 print("-" * 45)
|
||||
33
|
||||
34 1 1.5 1.5 0.0 total_swap_used = 0
|
||||
35
|
||||
36 1 1.5 1.5 0.0 swap_used: list[ProcessSwapInfo] = []
|
||||
37
|
||||
38 476 428514.1 900.2 92.0 for proc in psutil.process_iter(['pid', 'name', 'memory_full_info']):
|
||||
39 475 894.0 1.9 0.2 try:
|
||||
40 950 19757.6 20.8 4.2 with proc.oneshot():
|
||||
41 475 878.1 1.8 0.2 mem_info = proc.info['memory_full_info']
|
||||
42 475 942.4 2.0 0.2 s: int = getattr(mem_info, "swap", 0)
|
||||
43 475 880.2 1.9 0.2 if s > 0:
|
||||
44 167 10376.3 62.1 2.2 swap_used.append(ProcessSwapInfo(proc.pid, proc.name(), s))
|
||||
45 167 320.2 1.9 0.1 total_swap_used += s
|
||||
46 except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
47 continue
|
||||
48
|
||||
49 1 97.1 97.1 0.0 swap_used.sort(key=lambda x: x.swap)
|
||||
50
|
||||
51 1 2.4 2.4 0.0 def add_to_group(groups: dict[ProcessName, ProcessSwapGroup], process: ProcessSwapInfo) -> dict[ProcessName, ProcessSwapGroup]:
|
||||
52 if process.name not in groups:
|
||||
53 groups[process.name] = ProcessSwapGroup(process.name)
|
||||
54 groups[process.name].processes.append(process)
|
||||
55 return groups
|
||||
56
|
||||
57 2 247.9 124.0 0.1 swap_used_groups: list[ProcessSwapGroup] = sorted(
|
||||
58 1 548.6 548.6 0.1 (item for item in reduce(add_to_group, swap_used, dict()).values()),
|
||||
59 1 2.4 2.4 0.0 key=lambda x: x.total_swap
|
||||
60 )
|
||||
61
|
||||
62 103 191.7 1.9 0.0 for group in swap_used_groups:
|
||||
63 102 617.0 6.0 0.1 print(f"{group.name:<32} {bytes_to_mb(group.total_swap):10.2f}")
|
||||
64 269 489.7 1.8 0.1 for item in group.processes:
|
||||
65 167 679.6 4.1 0.1 print(f"{item.pid:>32} {bytes_to_mb(item.swap):10.2f}")
|
||||
66 102 210.4 2.1 0.0 print()
|
||||
67
|
||||
68 1 2.4 2.4 0.0 print("-" * 45)
|
||||
69 1 5.2 5.2 0.0 print(f"{'Total swap used by processes:':<32} {bytes_to_mb(total_swap_used):10.2f} MB")
|
||||
|
||||
Reference in New Issue
Block a user