mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-10-10 20:06:18 +00:00
Update flask_server.py
This commit is contained in:
@@ -1641,6 +1641,7 @@ def get_detailed_gpu_info(gpu):
|
||||
output_lines = []
|
||||
start_time = time.time()
|
||||
timeout_seconds = 3.0
|
||||
json_objects_found = 0
|
||||
|
||||
while time.time() - start_time < timeout_seconds:
|
||||
if process.poll() is not None:
|
||||
@@ -1655,126 +1656,139 @@ def get_detailed_gpu_info(gpu):
|
||||
if len(output_lines) <= 10:
|
||||
print(f"[v0] Received line {len(output_lines)}: {line.strip()[:100]}")
|
||||
|
||||
# intel_gpu_top outputs individual JSON objects, not necessarily wrapped in array
|
||||
|
||||
# Find the first opening brace
|
||||
object_start = output.find('{')
|
||||
if object_start == -1:
|
||||
continue
|
||||
|
||||
# Count braces to find complete object
|
||||
brace_count = 0
|
||||
object_end = -1
|
||||
|
||||
for i in range(object_start, len(output)):
|
||||
if output[i] == '{':
|
||||
brace_count += 1
|
||||
elif output[i] == '}':
|
||||
brace_count -= 1
|
||||
if brace_count == 0:
|
||||
object_end = i + 1
|
||||
break
|
||||
|
||||
if object_end > object_start:
|
||||
# Extract the first complete JSON object
|
||||
json_str = output[object_start:object_end]
|
||||
print(f"[v0] Found complete JSON object ({len(json_str)} chars)")
|
||||
print(f"[v0] JSON preview (first 300 chars): {json_str[:300]}")
|
||||
# Find all complete JSON objects
|
||||
search_start = 0
|
||||
while True:
|
||||
object_start = output.find('{', search_start)
|
||||
if object_start == -1:
|
||||
break
|
||||
|
||||
try:
|
||||
json_data = json.loads(json_str)
|
||||
print(f"[v0] Successfully parsed JSON from intel_gpu_top")
|
||||
print(f"[v0] JSON keys: {list(json_data.keys())}")
|
||||
# Count braces to find complete object
|
||||
brace_count = 0
|
||||
object_end = -1
|
||||
|
||||
for i in range(object_start, len(output)):
|
||||
if output[i] == '{':
|
||||
brace_count += 1
|
||||
elif output[i] == '}':
|
||||
brace_count -= 1
|
||||
if brace_count == 0:
|
||||
object_end = i + 1
|
||||
break
|
||||
|
||||
if object_end > object_start:
|
||||
json_objects_found += 1
|
||||
json_str = output[object_start:object_end]
|
||||
|
||||
if json_objects_found == 1:
|
||||
print(f"[v0] Found first JSON object ({len(json_str)} chars) - skipping (baseline)")
|
||||
search_start = object_end
|
||||
continue
|
||||
|
||||
print(f"[v0] Found second JSON object ({len(json_str)} chars) - using this one")
|
||||
print(f"[v0] JSON preview (first 300 chars): {json_str[:300]}")
|
||||
|
||||
try:
|
||||
json_data = json.loads(json_str)
|
||||
print(f"[v0] Successfully parsed JSON from intel_gpu_top")
|
||||
print(f"[v0] JSON keys: {list(json_data.keys())}")
|
||||
|
||||
# Parse frequency data
|
||||
if 'frequency' in json_data:
|
||||
freq = json_data['frequency']
|
||||
if 'actual' in freq:
|
||||
detailed_info['clock_graphics'] = f"{freq['actual']:.0f} MHz"
|
||||
data_retrieved = True
|
||||
if 'requested' in freq:
|
||||
detailed_info['clock_max'] = f"{freq['requested']:.0f} MHz"
|
||||
data_retrieved = True
|
||||
|
||||
# Parse power data
|
||||
if 'power' in json_data:
|
||||
power = json_data['power']
|
||||
if 'GPU' in power:
|
||||
detailed_info['power_draw'] = f"{power['GPU']:.2f} W"
|
||||
data_retrieved = True
|
||||
if 'Package' in power:
|
||||
detailed_info['power_limit'] = f"{power['Package']:.2f} W"
|
||||
data_retrieved = True
|
||||
|
||||
# Parse RC6 state
|
||||
if 'rc6' in json_data:
|
||||
rc6_value = json_data['rc6'].get('value', 0)
|
||||
detailed_info['power_state'] = f"RC6: {rc6_value:.1f}%"
|
||||
data_retrieved = True
|
||||
|
||||
# Parse interrupts
|
||||
if 'interrupts' in json_data:
|
||||
irq_count = json_data['interrupts'].get('count', 0)
|
||||
detailed_info['irq_rate'] = int(irq_count)
|
||||
data_retrieved = True
|
||||
|
||||
# Parse engines and calculate utilization
|
||||
if 'engines' in json_data:
|
||||
engines_data = json_data['engines']
|
||||
engine_map = {
|
||||
'Render/3D': 'engine_render',
|
||||
'Blitter': 'engine_blitter',
|
||||
'Video': 'engine_video',
|
||||
'VideoEnhance': 'engine_video_enhance'
|
||||
}
|
||||
|
||||
engine_values = []
|
||||
for engine_name, key in engine_map.items():
|
||||
if engine_name in engines_data:
|
||||
busy_value = engines_data[engine_name].get('busy', 0)
|
||||
detailed_info[key] = float(busy_value)
|
||||
engine_values.append(busy_value)
|
||||
# Parse frequency data
|
||||
if 'frequency' in json_data:
|
||||
freq = json_data['frequency']
|
||||
if 'actual' in freq:
|
||||
detailed_info['clock_graphics'] = f"{freq['actual']:.0f} MHz"
|
||||
data_retrieved = True
|
||||
if 'requested' in freq:
|
||||
detailed_info['clock_max'] = f"{freq['requested']:.0f} MHz"
|
||||
data_retrieved = True
|
||||
|
||||
# Calculate overall GPU utilization
|
||||
if engine_values:
|
||||
avg_utilization = sum(engine_values) / len(engine_values)
|
||||
detailed_info['utilization_gpu'] = f"{avg_utilization:.1f}%"
|
||||
|
||||
# Parse client processes
|
||||
if 'clients' in json_data:
|
||||
clients_data = json_data['clients']
|
||||
processes = []
|
||||
for client_id, client_info in clients_data.items():
|
||||
process_info = {
|
||||
'name': client_info.get('name', 'Unknown'),
|
||||
'pid': client_info.get('pid', 'N/A')
|
||||
# Parse power data
|
||||
if 'power' in json_data:
|
||||
power = json_data['power']
|
||||
if 'GPU' in power:
|
||||
detailed_info['power_draw'] = f"{power['GPU']:.2f} W"
|
||||
data_retrieved = True
|
||||
if 'Package' in power:
|
||||
detailed_info['power_limit'] = f"{power['Package']:.2f} W"
|
||||
data_retrieved = True
|
||||
|
||||
# Parse RC6 state
|
||||
if 'rc6' in json_data:
|
||||
rc6_value = json_data['rc6'].get('value', 0)
|
||||
detailed_info['power_state'] = f"RC6: {rc6_value:.1f}%"
|
||||
data_retrieved = True
|
||||
|
||||
# Parse interrupts
|
||||
if 'interrupts' in json_data:
|
||||
irq_count = json_data['interrupts'].get('count', 0)
|
||||
detailed_info['irq_rate'] = int(irq_count)
|
||||
data_retrieved = True
|
||||
|
||||
# Parse engines and calculate utilization
|
||||
if 'engines' in json_data:
|
||||
engines_data = json_data['engines']
|
||||
engine_map = {
|
||||
'Render/3D': 'engine_render',
|
||||
'Blitter': 'engine_blitter',
|
||||
'Video': 'engine_video',
|
||||
'VideoEnhance': 'engine_video_enhance'
|
||||
}
|
||||
|
||||
if 'memory' in client_info and 'system' in client_info['memory']:
|
||||
mem_info = client_info['memory']['system']
|
||||
if 'resident' in mem_info:
|
||||
mem_mb = int(mem_info['resident']) / (1024 * 1024)
|
||||
process_info['memory_used'] = f"{mem_mb:.0f} MB"
|
||||
engine_values = []
|
||||
for engine_name, key in engine_map.items():
|
||||
if engine_name in engines_data:
|
||||
busy_value = engines_data[engine_name].get('busy', 0)
|
||||
detailed_info[key] = float(busy_value)
|
||||
engine_values.append(busy_value)
|
||||
data_retrieved = True
|
||||
|
||||
if 'engine-classes' in client_info:
|
||||
engine_classes = client_info['engine-classes']
|
||||
if 'Render/3D' in engine_classes:
|
||||
render_busy = engine_classes['Render/3D'].get('busy', '0')
|
||||
process_info['gpu_utilization'] = f"{float(render_busy):.1f}%"
|
||||
|
||||
processes.append(process_info)
|
||||
# Calculate overall GPU utilization
|
||||
if engine_values:
|
||||
avg_utilization = sum(engine_values) / len(engine_values)
|
||||
detailed_info['utilization_gpu'] = f"{avg_utilization:.1f}%"
|
||||
|
||||
if processes:
|
||||
detailed_info['processes'] = processes
|
||||
data_retrieved = True
|
||||
|
||||
print(f"[v0] Intel GPU data retrieved successfully")
|
||||
# Parse client processes
|
||||
if 'clients' in json_data:
|
||||
clients_data = json_data['clients']
|
||||
processes = []
|
||||
for client_id, client_info in clients_data.items():
|
||||
process_info = {
|
||||
'name': client_info.get('name', 'Unknown'),
|
||||
'pid': client_info.get('pid', 'N/A')
|
||||
}
|
||||
|
||||
if 'memory' in client_info and 'system' in client_info['memory']:
|
||||
mem_info = client_info['memory']['system']
|
||||
if 'resident' in mem_info:
|
||||
mem_mb = int(mem_info['resident']) / (1024 * 1024)
|
||||
process_info['memory_used'] = f"{mem_mb:.0f} MB"
|
||||
|
||||
if 'engine-classes' in client_info:
|
||||
engine_classes = client_info['engine-classes']
|
||||
if 'Render/3D' in engine_classes:
|
||||
render_busy = engine_classes['Render/3D'].get('busy', '0')
|
||||
process_info['gpu_utilization'] = f"{float(render_busy):.1f}%"
|
||||
|
||||
processes.append(process_info)
|
||||
|
||||
if processes:
|
||||
detailed_info['processes'] = processes
|
||||
data_retrieved = True
|
||||
|
||||
print(f"[v0] Intel GPU data retrieved successfully")
|
||||
break
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"[v0] JSON decode error: {e}")
|
||||
search_start = object_end
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"[v0] JSON decode error: {e}")
|
||||
pass
|
||||
|
||||
# If we found and parsed the second object, break out of the main loop
|
||||
if json_objects_found >= 2 and data_retrieved:
|
||||
break
|
||||
else:
|
||||
time.sleep(0.1)
|
||||
|
||||
|
Reference in New Issue
Block a user