# clkwin.py CSV CARDS START END [APP]: per-second SM min/max, pstates, mem, util for cards in [START,END] (HH:MM:SS, the csv date), and the share of samples at/below APP with no limit reason.
import sys, collections
f, cards, a, b = sys.argv[1], set(sys.argv[2].split(",")), sys.argv[3], sys.argv[4]
app = int(sys.argv[5]) if len(sys.argv) > 5 else None
per = collections.OrderedDict(); low = tot = 0
for ln in open(f):
    p = [x.strip() for x in ln.split(",")]
    if len(p) < 9 or p[1] not in cards: continue
    t = p[0].split()[1][:8]
    if not (a <= t <= b): continue
    sm = int(p[3].split()[0]); mem = int(p[4].split()[0]); u = int(p[5].split()[0]); rs = int(p[8], 16)
    k = (t, p[1]); d = per.setdefault(k, [10**9, 0, set(), set(), []])
    d[0] = min(d[0], sm); d[1] = max(d[1], sm); d[2].add(p[2]); d[3].add(mem); d[4].append(u)
    if app is not None and p[2] != "P8":
        tot += 1; low += (sm <= app and not rs & 0xEC)
for (t, c), d in per.items():
    print(t, "card", c, "|".join(sorted(d[2])), f"SM {d[0]}-{d[1]}", "MEM", "|".join(map(str, sorted(d[3]))), f"util {min(d[4])}-{max(d[4])}%")
if app is not None: print(f"samples at/below app clock {app} with no limit reason: {low}/{tot}")
