| |
| from __future__ import annotations |
|
|
| import re |
| import subprocess |
| import sys |
| from collections import Counter |
|
|
|
|
| def run(cmd: list[str]) -> str: |
| proc = subprocess.run(cmd, check=False, capture_output=True, text=True) |
| if proc.returncode != 0: |
| raise RuntimeError(f"command failed: {' '.join(cmd)}\n{proc.stderr.strip()}") |
| return proc.stdout |
|
|
|
|
| def count_free_gpus(partition: str) -> tuple[int, int]: |
| nodes_out = run(["sinfo", "-h", "-N", "-p", partition, "-o", "%N"]) |
| nodes = [line.strip() for line in nodes_out.splitlines() if line.strip()] |
| total = 0 |
| free = 0 |
| for node in nodes: |
| node_out = run(["scontrol", "show", "node", node, "-o"]).strip() |
| if not node_out: |
| continue |
| state_m = re.search(r"\bState=([^ ]+)", node_out) |
| state = state_m.group(1).lower() if state_m else "" |
| if any(flag in state for flag in ("drain", "drained", "down", "fail", "inval")): |
| continue |
| cfg_m = re.search(r"\bCfgTRES=.*?(?:,|^)gres/gpu=(\d+)", node_out) |
| alloc_m = re.search(r"\bAllocTRES=.*?(?:,|^)gres/gpu=(\d+)", node_out) |
| node_total = int(cfg_m.group(1)) if cfg_m else 0 |
| node_used = int(alloc_m.group(1)) if alloc_m else 0 |
| total += node_total |
| free += max(0, node_total - node_used) |
| return free, total |
|
|
|
|
| def count_cpu_capacity(partition: str) -> tuple[int, int, int]: |
| out = run(["sinfo", "-h", "-N", "-p", partition, "-o", "%N|%C|%t"]) |
| free_cpu = 0 |
| total_cpu = 0 |
| nodes_with_free_cpu = 0 |
| for line in out.splitlines(): |
| if not line.strip() or "|" not in line: |
| continue |
| _node, cpu_field, state = line.split("|", 2) |
| state = state.strip().lower() |
| if any(flag in state for flag in ("drain", "drained", "down", "fail", "inval")): |
| continue |
| parts = cpu_field.split("/") |
| if len(parts) != 4: |
| continue |
| _alloc, idle, _other, total = (int(x) for x in parts) |
| free_cpu += idle |
| total_cpu += total |
| if idle > 0: |
| nodes_with_free_cpu += 1 |
| return free_cpu, total_cpu, nodes_with_free_cpu |
|
|
|
|
| def user_job_breakdown(user: str) -> Counter[tuple[str, str]]: |
| out = run(["squeue", "-u", user, "-h", "-o", "%j|%T"]) |
| counter: Counter[tuple[str, str]] = Counter() |
| for line in out.splitlines(): |
| if not line.strip() or "|" not in line: |
| continue |
| job, state = line.split("|", 1) |
| counter[(job.strip(), state.strip())] += 1 |
| return counter |
|
|
|
|
| def main() -> int: |
| user = sys.argv[1] if len(sys.argv) > 1 else "sf895" |
| cpu_free, cpu_total, cpu_nodes = count_cpu_capacity("main") |
| gpu_parts = ["gpu", "gpu-redhat", "cgpu"] |
| print(f"user={user}") |
| print(f"cpu_partition=main free_cpu={cpu_free} total_cpu={cpu_total} nodes_with_free_cpu={cpu_nodes}") |
| for part in gpu_parts: |
| free, total = count_free_gpus(part) |
| print(f"gpu_partition={part} free_gpu={free} total_gpu={total}") |
| jobs = user_job_breakdown(user) |
| for (job, state), count in sorted(jobs.items()): |
| print(f"user_jobs job={job} state={state} count={count}") |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|