Update flask_server.py

This commit is contained in:
MacRimi
2025-10-09 20:18:49 +02:00
parent eb954fb10d
commit a4cb9a8923

View File

@@ -1717,137 +1717,152 @@ def get_detailed_gpu_info(gpu):
print(f"[v0] Collected {len(json_objects)} JSON objects total", flush=True) print(f"[v0] Collected {len(json_objects)} JSON objects total", flush=True)
if not any('clients' in obj for obj in json_objects): try:
try: print(f"[v0] Starting JSON parsing...", flush=True)
stderr_output = process.stderr.read()
if stderr_output:
print(f"[v0] intel_gpu_top stderr (no clients found): {stderr_output}", flush=True)
except:
pass
best_json = None
for json_obj in json_objects:
if 'clients' in json_obj and len(json_obj['clients']) > 0:
best_json = json_obj
print(f"[v0] Using JSON object with {len(json_obj['clients'])} client(s)", flush=True)
break
if not best_json and json_objects:
best_json = json_objects[0]
print(f"[v0] No JSON with clients found, using first JSON object as fallback", flush=True)
if best_json:
print(f"[v0] Parsing selected JSON object...", flush=True)
data_retrieved = False
# Initialize engine totals if not any('clients' in obj for obj in json_objects):
engine_totals = { try:
'Render/3D': 0.0, stderr_output = process.stderr.read()
'Blitter': 0.0, if stderr_output:
'Video': 0.0, print(f"[v0] intel_gpu_top stderr (no clients found): {stderr_output}", flush=True)
'VideoEnhance': 0.0 except:
} pass
client_engine_totals = {
'Render/3D': 0.0,
'Blitter': 0.0,
'Video': 0.0,
'VideoEnhance': 0.0
}
# Parse clients section (processes using GPU) best_json = None
if 'clients' in best_json: for json_obj in json_objects:
print(f"[v0] Parsing clients section...", flush=True) if 'clients' in json_obj and len(json_obj['clients']) > 0:
clients = best_json['clients'] best_json = json_obj
processes = [] print(f"[v0] Using JSON object with {len(json_obj['clients'])} client(s)", flush=True)
break
if not best_json and json_objects:
best_json = json_objects[0]
print(f"[v0] No JSON with clients found, using first JSON object as fallback", flush=True)
if best_json:
print(f"[v0] Parsing selected JSON object...", flush=True)
data_retrieved = False
for client_id, client_data in clients.items(): # Initialize engine totals
process_info = { engine_totals = {
'name': client_data.get('name', 'Unknown'), 'Render/3D': 0.0,
'pid': client_data.get('pid', 'Unknown'), 'Blitter': 0.0,
'memory': { 'Video': 0.0,
'total': client_data.get('memory', {}).get('system', {}).get('total', 0), 'VideoEnhance': 0.0
'shared': client_data.get('memory', {}).get('system', {}).get('shared', 0), }
'resident': client_data.get('memory', {}).get('system', {}).get('resident', 0) client_engine_totals = {
}, 'Render/3D': 0.0,
'engines': {} 'Blitter': 0.0,
} 'Video': 0.0,
'VideoEnhance': 0.0
}
# Parse clients section (processes using GPU)
if 'clients' in best_json:
print(f"[v0] Parsing clients section...", flush=True)
clients = best_json['clients']
processes = []
# Parse engine utilization for this process for client_id, client_data in clients.items():
engine_classes = client_data.get('engine-classes', {}) process_info = {
for engine_name, engine_data in engine_classes.items(): 'name': client_data.get('name', 'Unknown'),
busy_value = float(engine_data.get('busy', 0)) 'pid': client_data.get('pid', 'Unknown'),
process_info['engines'][engine_name] = f"{busy_value:.1f}%" 'memory': {
'total': client_data.get('memory', {}).get('system', {}).get('total', 0),
'shared': client_data.get('memory', {}).get('system', {}).get('shared', 0),
'resident': client_data.get('memory', {}).get('system', {}).get('resident', 0)
},
'engines': {}
}
# Sum up engine utilization across all processes # Parse engine utilization for this process
if engine_name in client_engine_totals: engine_classes = client_data.get('engine-classes', {})
client_engine_totals[engine_name] += busy_value for engine_name, engine_data in engine_classes.items():
busy_value = float(engine_data.get('busy', 0))
process_info['engines'][engine_name] = f"{busy_value:.1f}%"
# Sum up engine utilization across all processes
if engine_name in client_engine_totals:
client_engine_totals[engine_name] += busy_value
processes.append(process_info)
print(f"[v0] Added process: {process_info['name']} (PID: {process_info['pid']})", flush=True)
processes.append(process_info) detailed_info['processes'] = processes
print(f"[v0] Added process: {process_info['name']} (PID: {process_info['pid']})", flush=True) print(f"[v0] Total processes found: {len(processes)}", flush=True)
else:
print(f"[v0] WARNING: No 'clients' section in selected JSON", flush=True)
detailed_info['processes'] = processes
print(f"[v0] Total processes found: {len(processes)}", flush=True)
else:
print(f"[v0] WARNING: No 'clients' section in selected JSON", flush=True)
# Parse global engines section
if 'engines' in best_json:
print(f"[v0] Parsing engines section...", flush=True) print(f"[v0] Parsing engines section...", flush=True)
engines = best_json['engines'] # Parse global engines section
if 'engines' in best_json:
for engine_name, engine_data in engines.items(): engines = best_json['engines']
# Remove the /0 suffix if present
clean_name = engine_name.replace('/0', '')
busy_value = float(engine_data.get('busy', 0))
if clean_name in engine_totals: for engine_name, engine_data in engines.items():
engine_totals[clean_name] = busy_value # Remove the /0 suffix if present
clean_name = engine_name.replace('/0', '')
# Use client engine totals if available, otherwise use global engines busy_value = float(engine_data.get('busy', 0))
final_engines = client_engine_totals if any(v > 0 for v in client_engine_totals.values()) else engine_totals
if clean_name in engine_totals:
detailed_info['engine_render'] = f"{final_engines['Render/3D']:.1f}%" engine_totals[clean_name] = busy_value
detailed_info['engine_blitter'] = f"{final_engines['Blitter']:.1f}%"
detailed_info['engine_video'] = f"{final_engines['Video']:.1f}%" print(f"[v0] Calculating final engine values...", flush=True)
detailed_info['engine_video_enhance'] = f"{final_engines['VideoEnhance']:.1f}%" # Use client engine totals if available, otherwise use global engines
final_engines = client_engine_totals if any(v > 0 for v in client_engine_totals.values()) else engine_totals
# Calculate overall GPU utilization (max of all engines)
max_utilization = max(final_engines.values()) detailed_info['engine_render'] = f"{final_engines['Render/3D']:.1f}%"
detailed_info['utilization_gpu'] = f"{max_utilization:.1f}%" detailed_info['engine_blitter'] = f"{final_engines['Blitter']:.1f}%"
detailed_info['engine_video'] = f"{final_engines['Video']:.1f}%"
# Parse frequency detailed_info['engine_video_enhance'] = f"{final_engines['VideoEnhance']:.1f}%"
if 'frequency' in best_json:
freq_data = best_json['frequency'] # Calculate overall GPU utilization (max of all engines)
actual_freq = freq_data.get('actual', 0) max_utilization = max(final_engines.values())
detailed_info['clock_graphics'] = f"{actual_freq} MHz" detailed_info['utilization_gpu'] = f"{max_utilization:.1f}%"
data_retrieved = True
print(f"[v0] Parsing frequency...", flush=True)
# Parse power # Parse frequency
if 'power' in best_json: if 'frequency' in best_json:
power_data = best_json['power'] freq_data = best_json['frequency']
gpu_power = power_data.get('GPU', 0) actual_freq = freq_data.get('actual', 0)
package_power = power_data.get('Package', 0) detailed_info['clock_graphics'] = f"{actual_freq} MHz"
detailed_info['power_draw'] = f"{gpu_power:.2f} W" data_retrieved = True
detailed_info['power_limit'] = f"{package_power:.2f} W"
data_retrieved = True print(f"[v0] Parsing power...", flush=True)
# Parse power
if data_retrieved: if 'power' in best_json:
detailed_info['has_monitoring_tool'] = True power_data = best_json['power']
print(f"[v0] Intel GPU monitoring successful", flush=True) gpu_power = power_data.get('GPU', 0)
print(f"[v0] - Utilization: {detailed_info['utilization_gpu']}", flush=True) package_power = power_data.get('Package', 0)
print(f"[v0] - Engines: R={detailed_info['engine_render']}, B={detailed_info['engine_blitter']}, V={detailed_info['engine_video']}, VE={detailed_info['engine_video_enhance']}", flush=True) detailed_info['power_draw'] = f"{gpu_power:.2f} W"
print(f"[v0] - Processes: {len(detailed_info['processes'])}", flush=True) detailed_info['power_limit'] = f"{package_power:.2f} W"
data_retrieved = True
print(f"[v0] Finalizing Intel GPU data...", flush=True)
if data_retrieved:
detailed_info['has_monitoring_tool'] = True
print(f"[v0] Intel GPU monitoring successful", flush=True)
print(f"[v0] - Utilization: {detailed_info['utilization_gpu']}", flush=True)
print(f"[v0] - Engines: R={detailed_info['engine_render']}, B={detailed_info['engine_blitter']}, V={detailed_info['engine_video']}, VE={detailed_info['engine_video_enhance']}", flush=True)
print(f"[v0] - Processes: {len(detailed_info['processes'])}", flush=True)
else:
print(f"[v0] WARNING: No data retrieved from intel_gpu_top", flush=True)
else: else:
print(f"[v0] WARNING: No data retrieved from intel_gpu_top", flush=True) print(f"[v0] WARNING: No valid JSON objects found", flush=True)
else: # Check stderr for errors
print(f"[v0] WARNING: No valid JSON objects found", flush=True) try:
# Check stderr for errors stderr_output = process.stderr.read()
try: if stderr_output:
stderr_output = process.stderr.read() print(f"[v0] intel_gpu_top stderr: {stderr_output}", flush=True)
if stderr_output: except:
print(f"[v0] intel_gpu_top stderr: {stderr_output}", flush=True) pass
except:
pass print(f"[v0] JSON parsing completed successfully", flush=True)
except Exception as parse_error:
print(f"[v0] CRITICAL ERROR during JSON parsing: {parse_error}", flush=True)
import traceback
traceback.print_exc()
print(f"[v0] Returning partial data due to parsing error", flush=True)
except Exception as e: except Exception as e:
print(f"[v0] Error running intel_gpu_top: {e}", flush=True) print(f"[v0] Error running intel_gpu_top: {e}", flush=True)