()
| 31 | |
| 32 | |
| 33 | def main() -> int: |
| 34 | args = parse_args() |
| 35 | metrics_path = Path(args.metrics) |
| 36 | out_path = Path(args.output) |
| 37 | |
| 38 | if not metrics_path.is_file(): |
| 39 | raise FileNotFoundError(f"metrics file not found: {metrics_path}") |
| 40 | |
| 41 | with metrics_path.open("r", encoding="utf-8") as f: |
| 42 | payload = json.load(f) |
| 43 | |
| 44 | samples = payload.get("monitor_samples", []) |
| 45 | if not isinstance(samples, list): |
| 46 | raise ValueError(f"invalid metrics format: monitor_samples must be a list, got {type(samples)}") |
| 47 | |
| 48 | grouped_samples = defaultdict(list) |
| 49 | for item in samples: |
| 50 | if not isinstance(item, dict): |
| 51 | continue |
| 52 | if item.get("status") != "ok": |
| 53 | continue |
| 54 | sample_ts = item.get("sample_ts_from_global_start_s") |
| 55 | if sample_ts is None: |
| 56 | continue |
| 57 | try: |
| 58 | grouped_samples[float(sample_ts)].append(item) |
| 59 | except (TypeError, ValueError): |
| 60 | continue |
| 61 | |
| 62 | rows = [] |
| 63 | for sample_ts in sorted(grouped_samples): |
| 64 | group = grouped_samples[sample_ts] |
| 65 | gpu_utils = [] |
| 66 | mem_utils = [] |
| 67 | for item in group: |
| 68 | gpu_util = item.get("gpu_utilization") |
| 69 | mem_used = item.get("gpu_memory_used_mb") |
| 70 | mem_total = item.get("gpu_memory_total_mb") |
| 71 | if gpu_util is not None: |
| 72 | gpu_utils.append(float(gpu_util)) |
| 73 | if mem_used is not None and mem_total: |
| 74 | mem_utils.append(float(mem_used) / float(mem_total) * 100.0) |
| 75 | |
| 76 | if not gpu_utils or not mem_utils: |
| 77 | continue |
| 78 | |
| 79 | rows.append( |
| 80 | { |
| 81 | "time_from_start_s": _fmt_float3(sample_ts), |
| 82 | "avg_gpu_utilization": _fmt_float3(sum(gpu_utils) / len(gpu_utils)), |
| 83 | "avg_gpu_memory_occupancy_rate": _fmt_float3(sum(mem_utils) / len(mem_utils)), |
| 84 | } |
| 85 | ) |
| 86 | |
| 87 | out_path.parent.mkdir(parents=True, exist_ok=True) |
| 88 | with out_path.open("w", encoding="utf-8", newline="") as f: |
| 89 | writer = csv.DictWriter( |
| 90 | f, |
no test coverage detected