forked from gaurav-nelson/HunyuanVideo_MLX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_resources.py
151 lines (124 loc) · 5.63 KB
/
monitor_resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import psutil
import time
import os
import curses
from datetime import datetime
import subprocess
import mlx.core as mx
def get_metal_memory():
"""Get Metal GPU memory usage using system_profiler"""
try:
cmd = ['system_profiler', 'SPDisplaysDataType']
result = subprocess.run(cmd, capture_output=True, text=True)
# Parse the output to find Metal GPU memory
lines = result.stdout.split('\n')
total_mem = 0
for line in lines:
if 'VRAM' in line:
# Extract memory value in MB/GB and convert to GB
mem_str = line.split(':')[1].strip()
if 'MB' in mem_str:
total_mem = float(mem_str.replace('MB', '').strip()) / 1024
elif 'GB' in mem_str:
total_mem = float(mem_str.replace('GB', '').strip())
# Get current memory usage through psutil as an approximation
memory = psutil.virtual_memory()
used_mem = (memory.total - memory.available) / (1024 ** 3)
return {
'total': total_mem,
'used': min(used_mem, total_mem), # Cap at total memory
'free': max(0, total_mem - used_mem)
}
except Exception as e:
return None
def get_mlx_info():
"""Get MLX-specific information"""
try:
return {
'version': mx.__version__,
'backend': 'Metal',
'device': mx.default_device()
}
except Exception as e:
return None
def get_process_memory(pid):
"""Get memory usage for a specific process"""
try:
process = psutil.Process(pid)
return process.memory_info().rss / (1024 ** 3) # Convert to GB
except:
return 0
def main(stdscr):
# Set up colors
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
# Hide cursor
curses.curs_set(0)
# Get MLX info once
mlx_info = get_mlx_info()
# Get initial process list
python_processes = {p.pid: p.cmdline() for p in psutil.process_iter(['pid', 'cmdline'])
if 'python' in ' '.join(p.cmdline()).lower()}
while True:
try:
stdscr.clear()
# Get current time
current_time = datetime.now().strftime("%H:%M:%S")
# Get system memory
memory = psutil.virtual_memory()
memory_percent = memory.percent
# Get CPU usage
cpu_percent = psutil.cpu_percent(interval=1)
# Get Metal memory
metal_mem = get_metal_memory()
# Display header
stdscr.addstr(0, 0, f"HunyuanVideo MLX Resource Monitor - {current_time}", curses.color_pair(1) | curses.A_BOLD)
# Display MLX info
if mlx_info:
stdscr.addstr(2, 0, "MLX Configuration:", curses.A_BOLD)
stdscr.addstr(3, 2, f"Version: {mlx_info['version']}")
stdscr.addstr(4, 2, f"Backend: {mlx_info['backend']}")
stdscr.addstr(5, 2, f"Device: {mlx_info['device']}")
# Display system resources
stdscr.addstr(7, 0, "System Resources:", curses.A_BOLD)
# Display CPU usage
color = curses.color_pair(1) if cpu_percent < 70 else curses.color_pair(2) if cpu_percent < 90 else curses.color_pair(3)
stdscr.addstr(8, 2, f"CPU Usage: {cpu_percent:>5.1f}%", color)
# Display memory usage
color = curses.color_pair(1) if memory_percent < 70 else curses.color_pair(2) if memory_percent < 90 else curses.color_pair(3)
stdscr.addstr(9, 2, f"Memory: {memory.used/1024/1024/1024:>5.1f}GB / {memory.total/1024/1024/1024:>5.1f}GB ({memory_percent:>5.1f}%)", color)
# Display Metal memory if available
if metal_mem:
metal_percent = (metal_mem['used'] / metal_mem['total']) * 100 if metal_mem['total'] > 0 else 0
color = curses.color_pair(1) if metal_percent < 70 else curses.color_pair(2) if metal_percent < 90 else curses.color_pair(3)
stdscr.addstr(10, 2, f"Metal Memory: {metal_mem['used']:>5.1f}GB / {metal_mem['total']:>5.1f}GB ({metal_percent:>5.1f}%)", color)
# Display Python processes
stdscr.addstr(12, 0, "Python Processes:", curses.A_BOLD)
row = 13
for pid, cmdline in python_processes.items():
if psutil.pid_exists(pid):
mem_usage = get_process_memory(pid)
cmd_str = ' '.join(cmdline)
if 'sample_video_mps.py' in cmd_str:
color = curses.color_pair(1)
else:
color = curses.A_NORMAL
stdscr.addstr(row, 2, f"PID {pid:>6}: {mem_usage:>5.1f}GB - {cmd_str[:60]}", color)
row += 1
# Display help
stdscr.addstr(row + 2, 0, "Press 'q' to quit", curses.color_pair(2))
# Refresh the screen
stdscr.refresh()
# Check for 'q' key press
stdscr.timeout(1000) # Wait up to 1 second for key press
key = stdscr.getch()
if key == ord('q'):
break
except KeyboardInterrupt:
break
except curses.error:
pass
if __name__ == "__main__":
curses.wrapper(main)