Nafiul Huda commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,25 @@
|
|
| 1 |
import os
|
| 2 |
import psutil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
-
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def get_container_cpu_cores():
|
| 7 |
-
"""Reads cgroup limits to determine allocated vCPUs."""
|
| 8 |
try:
|
| 9 |
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r") as f:
|
| 10 |
quota = int(f.read().strip())
|
|
@@ -14,7 +29,6 @@ def get_container_cpu_cores():
|
|
| 14 |
return quota / period
|
| 15 |
except Exception:
|
| 16 |
pass
|
| 17 |
-
|
| 18 |
try:
|
| 19 |
with open("/sys/fs/cgroup/cpu.max", "r") as f:
|
| 20 |
parts = f.read().strip().split()
|
|
@@ -22,49 +36,88 @@ def get_container_cpu_cores():
|
|
| 22 |
return int(parts[0]) / int(parts[1])
|
| 23 |
except Exception:
|
| 24 |
pass
|
| 25 |
-
|
| 26 |
return os.cpu_count() or 1
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
process = psutil.Process(os.getpid())
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
f"--- YOUR CONTAINER LIMITS ---\n"
|
| 52 |
-
f"Max Allocated vCPUs: {allocated_cores:.2f} cores\n"
|
| 53 |
-
f"Max Allocated RAM: {allocated_ram:.1f} MB\n\n"
|
| 54 |
-
f"--- CURRENT LIVE USAGE ---\n"
|
| 55 |
-
f"Process RAM Usage: {current_ram:.1f} MB\n"
|
| 56 |
-
f"Process CPU Usage: {current_cpu:.1f}%"
|
| 57 |
-
)
|
| 58 |
-
return metrics
|
| 59 |
|
| 60 |
-
# Gradio
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
gr.Markdown("###
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
demo.load(fn=get_resources, outputs=output_text)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch(server_name="0.0.0.0")
|
|
|
|
| 1 |
import os
|
| 2 |
import psutil
|
| 3 |
+
import time
|
| 4 |
+
import grp
|
| 5 |
+
import numba
|
| 6 |
+
import numpy as np
|
| 7 |
+
import threading
|
| 8 |
import gradio as gr
|
| 9 |
+
import spaces
|
| 10 |
+
|
| 11 |
+
def get_cpu_model():
|
| 12 |
+
"""Extracts the actual CPU model name from /proc/cpuinfo."""
|
| 13 |
+
try:
|
| 14 |
+
with open("/proc/cpuinfo", "r") as f:
|
| 15 |
+
for line in f:
|
| 16 |
+
if "model name" in line:
|
| 17 |
+
return line.split(":")[1].strip()
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"Could not read CPU model: {str(e)}"
|
| 20 |
+
return "Unknown CPU Model"
|
| 21 |
|
| 22 |
def get_container_cpu_cores():
|
|
|
|
| 23 |
try:
|
| 24 |
with open("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r") as f:
|
| 25 |
quota = int(f.read().strip())
|
|
|
|
| 29 |
return quota / period
|
| 30 |
except Exception:
|
| 31 |
pass
|
|
|
|
| 32 |
try:
|
| 33 |
with open("/sys/fs/cgroup/cpu.max", "r") as f:
|
| 34 |
parts = f.read().strip().split()
|
|
|
|
| 36 |
return int(parts[0]) / int(parts[1])
|
| 37 |
except Exception:
|
| 38 |
pass
|
|
|
|
| 39 |
return os.cpu_count() or 1
|
| 40 |
|
| 41 |
+
# --- Stress Functions ---
|
| 42 |
+
|
| 43 |
+
def stress_cpu_worker(duration=5):
|
| 44 |
+
"""Keeps a single thread busy computing hashes/math."""
|
| 45 |
+
timeout = time.time() + duration
|
| 46 |
+
while time.time() < timeout:
|
| 47 |
+
_ = 123456789.0 * 987654321.0
|
| 48 |
+
|
| 49 |
+
@spaces.GPU(duration=30)
|
| 50 |
+
def run_heavy_benchmark():
|
| 51 |
+
log = []
|
| 52 |
+
|
| 53 |
+
# 1. Fetch CPU Profile Info
|
| 54 |
+
cpu_model = get_cpu_model()
|
| 55 |
+
reported_cores = os.cpu_count()
|
| 56 |
+
cgroup_cores = get_container_cpu_cores()
|
| 57 |
+
|
| 58 |
+
log.append(f"=== CPU IDENTIFICATION ===")
|
| 59 |
+
log.append(f"Model: {cpu_model}")
|
| 60 |
+
log.append(f"OS Reported Cores (Host total): {reported_cores}")
|
| 61 |
+
log.append(f"Cgroup Allowed Cores (Container quota): {cgroup_cores:.2f}\n")
|
| 62 |
+
|
| 63 |
+
# 2. Attempting 20 GB RAM Allocation
|
| 64 |
+
log.append(f"=== RAM ALLOCATION TEST (Targeting ~20 GB) ===")
|
| 65 |
+
try:
|
| 66 |
+
# 20 GB of float64 elements (8 bytes each) -> ~2.5 billion elements
|
| 67 |
+
elements = 2500000000
|
| 68 |
+
log.append("Allocating array...")
|
| 69 |
+
|
| 70 |
+
# We use empty to allocate virtual space, then fill to force physical allocation
|
| 71 |
+
ram_hog = np.empty(elements, dtype=np.float64)
|
| 72 |
+
ram_hog.fill(1.0)
|
| 73 |
+
|
| 74 |
+
process = psutil.Process(os.getpid())
|
| 75 |
+
current_ram = process.memory_info().rss / (1024 ** 2)
|
| 76 |
+
log.append(f"✅ Success! Array allocated.")
|
| 77 |
+
log.append(f"Active Process RAM: {current_ram:.1f} MB")
|
| 78 |
+
|
| 79 |
+
# Explicit clean up so we don't crash the container completely post-run
|
| 80 |
+
del ram_hog
|
| 81 |
+
log.append("Array cleared from memory.\n")
|
| 82 |
+
except MemoryError:
|
| 83 |
+
log.append("❌ Failed: Hit a hard MemoryError/OOM limit.\n")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
log.append(f"❌ Failed with exception: {str(e)}\n")
|
| 86 |
|
| 87 |
+
# 3. CPU 100% Load Test
|
| 88 |
+
log.append(f"=== CPU STRESS TEST (100% Target across active slices) ===")
|
| 89 |
+
threads = []
|
| 90 |
+
# Spawn threads matching host cores to maximize scheduler saturation
|
| 91 |
+
num_threads = reported_cores or 8
|
| 92 |
+
log.append(f"Spawning {num_threads} stress threads for 5 seconds...")
|
| 93 |
+
|
| 94 |
+
start_time = time.time()
|
| 95 |
+
for _ in range(num_threads):
|
| 96 |
+
t = threading.Thread(target=stress_cpu_worker, args=(5,))
|
| 97 |
+
threads.append(t)
|
| 98 |
+
t.start()
|
| 99 |
+
|
| 100 |
+
# Give threads a moment to spin up, then sample live usage
|
| 101 |
+
time.sleep(1.5)
|
| 102 |
process = psutil.Process(os.getpid())
|
| 103 |
+
live_cpu_pct = process.cpu_percent(interval=0.5)
|
| 104 |
+
log.append(f"Live measured CPU Usage during stress: {live_cpu_pct}%")
|
| 105 |
|
| 106 |
+
for t in threads:
|
| 107 |
+
t.join()
|
| 108 |
+
log.append(f"Stress threads finished execution successfully.")
|
| 109 |
|
| 110 |
+
return "\n".join(log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
# --- Gradio UI ---
|
| 113 |
with gr.Blocks() as demo:
|
| 114 |
+
gr.Markdown("### ZeroGPU Deep Hardware Profile & Load Tester")
|
| 115 |
+
gr.Markdown("Click the button below to run the heavy hardware test directly within the runtime environment.")
|
| 116 |
+
|
| 117 |
+
run_btn = gr.Button("Run 20GB RAM & 100% CPU Load Test", variant="primary")
|
| 118 |
+
output_log = gr.Textbox(label="Benchmark Terminal Log", lines=18)
|
| 119 |
|
| 120 |
+
run_btn.click(fn=run_heavy_benchmark, outputs=output_log)
|
|
|
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
demo.launch(server_name="0.0.0.0")
|