"""Analyze GPU utilization A/B test results."""
import csv, json, statistics, os

def gpu_analysis(path, label, groups):
    rows = list(csv.DictReader(open(path)))
    by_gpu = {}
    for r in rows:
        g = int(r["gpu"])
        by_gpu.setdefault(g, []).append(float(r["util_pct"]))

    n = len(rows) // 8
    print(f"\n{'='*70}")
    print(f"  {label} ({n} time points)")
    print(f"{'='*70}")
    for gname, indices in groups.items():
        vals = []
        for i in indices:
            vals.extend(by_gpu.get(i, []))
        if vals:
            s = sorted(vals)
            p = lambda q: s[min(int(q*len(s)), len(s)-1)]
            nz = sum(1 for v in vals if v > 0)
            print(f"  {gname}:")
            print(f"    mean={statistics.fmean(vals):.1f}%  p50={p(.5):.0f}%  p90={p(.9):.0f}%  max={max(vals):.0f}%")
            print(f"    active_samples={nz}/{len(vals)} ({nz*100//len(vals)}%)")

    for g in sorted(by_gpu.keys()):
        vals = by_gpu[g]
        nz = sum(1 for v in vals if v > 0)
        print(f"    GPU {g}: mean={statistics.fmean(vals):.1f}%  max={max(vals):.0f}%  active={nz*100//len(vals)}%")

def metrics_analysis(path, label):
    rows = [json.loads(l) for l in open(path)]
    ok = [r for r in rows if not r.get("error")]
    err = [r for r in rows if r.get("error")]
    ttfts = sorted([r["ttft_s"] for r in ok if r.get("ttft_s")])
    tpots = sorted([r["tpot_s"] for r in ok if r.get("tpot_s") and r["tpot_s"]>0])
    lats = sorted([r["latency_s"] for r in ok])
    p = lambda v,q: v[min(int(q*len(v)),len(v)-1)] if v else 0

    print(f"\n  {label}: {len(ok)}/{len(rows)} OK, {len(err)} err")
    if ttfts: print(f"    TTFT p50={p(ttfts,.5):.3f}  p90={p(ttfts,.9):.3f}")
    if tpots: print(f"    TPOT p50={p(tpots,.5):.3f}  p90={p(tpots,.9):.3f}")
    if lats:  print(f"    E2E  p50={p(lats,.5):.3f}  p90={p(lats,.9):.3f}")

gpu_analysis("outputs/gpu_ab_combined/gpu_util.csv", "COMBINED TP=1 DP=8 (cache-aware)",
    {"All GPUs": list(range(8))})

gpu_analysis("outputs/gpu_ab_pdsep/gpu_util.csv", "PD-SEP TP=1 4P+4D (cache-aware Mooncake)",
    {"Prefill (GPU 0-3)": [0,1,2,3], "Decode (GPU 4-7)": [4,5,6,7], "All GPUs": list(range(8))})

print(f"\n{'='*70}")
print(f"  LATENCY COMPARISON")
print(f"{'='*70}")
metrics_analysis("outputs/gpu_ab_combined/metrics.jsonl", "COMBINED")
metrics_analysis("outputs/gpu_ab_pdsep/metrics.jsonl", "PD-SEP")

print(f"\n{'='*70}")
print(f"  BOTTLENECK SUMMARY")
print(f"{'='*70}")
print("""
  1. DECODE GPU UNDERUTILIZATION
     PD-Sep decode GPUs: mean ~20%, max ~47%
     Combined GPUs:      mean ~30%, max 100%
     -> Decode is memory-bound, GPU compute wasted on dedicated decode GPUs
     -> 4 GPUs reserved for decode never exceed 50% utilization

  2. PREFILL GPU BURSTINESS
     PD-Sep prefill: high util when active (~86% p50), but idle ~48% of time
     Combined: more evenly distributed, active 64% of time
     -> await-prefill serializes P then D, creating idle gaps between requests

  3. KV TRANSFER OVERHEAD
     TTFT(PD-Sep) - TTFT(Combined) = pure KV transfer + proxy routing cost
     This penalty grows with input length (more KV to transfer)

  4. RESOURCE PARTITIONING INEFFICIENCY
     PD-Sep: fixed 4P+4D split cannot adapt to workload phase
     Combined: 8 GPUs flexibly serve both P and D based on demand
""")
