#!/usr/bin/env python3 """Compare endpoint slack across flow steps. Snapshots are written at the end of every flow step (e.g. 3_3_place_gp) when REPORT_ENDPOINT_SLACK=1, as $REPORTS_DIR/_endpoint_slack.json, with the setup and hold slack of every timing endpoint. Examples: endpointSlack.py list reports/nangate45/gcd/base endpointSlack.py track reports/nangate45/gcd/base endpointSlack.py compare A_endpoint_slack.json B_endpoint_slack.json -o cmp.html endpointSlack.py export reports/nangate45/gcd/base -o slack.csv """ import argparse import csv import glob import html import json import os import statistics import sys CHECKS = ("setup", "hold") UNITS = {"s": 1.0, "ns": 1e9, "ps": 1e12} SUFFIX = "_endpoint_slack.json" def load_snapshot(path): """Load a snapshot file. Returns a dict with metadata, "endpoints" (all endpoint names) and per check a dict endpoint -> slack in seconds for constrained endpoints. """ with open(path) as f: data = json.load(f) unit = data.get("time_unit", 1.0) name = os.path.basename(path) if name.endswith(SUFFIX): name = name[: -len(SUFFIX)] snapshot = { "file": path, "name": name, "design": data.get("design", ""), "step": data.get("step", name), "parasitics": data.get("parasitics", ""), "endpoints": set(data["endpoints"]), } for i, check in enumerate(CHECKS): snapshot[check] = { ep: slacks[i] * unit for ep, slacks in data["endpoints"].items() if slacks[i] is not None } return snapshot def find_snapshots(reports_dir): """Snapshot files in execution (modification time) order.""" files = glob.glob(os.path.join(reports_dir, "*" + SUFFIX)) return sorted(files, key=lambda f: (os.path.getmtime(f), f)) def load_dir(reports_dir): return [load_snapshot(f) for f in find_snapshots(reports_dir)] def summary(slacks): """WNS, TNS and number of violating endpoints of a slack dict.""" values = list(slacks.values()) negative = [s for s in values if s < 0] return { "count": len(values), "wns": min(values) if values else None, "tns": sum(negative), "violations": len(negative), } def worst_endpoints(slacks, n): return set(sorted(slacks, key=slacks.get)[:n]) def _ranks(values): """Ranks starting at 1, with tied values given their average rank.""" order = sorted(range(len(values)), key=values.__getitem__) ranks = [0.0] * len(values) i = 0 while i < len(order): j = i while j + 1 < len(order) and values[order[j + 1]] == values[order[i]]: j += 1 for k in range(i, j + 1): ranks[order[k]] = (i + j) / 2 + 1 i = j + 1 return ranks def spearman(x, y): """Spearman rank correlation, None if undefined (n < 2 or constant).""" if len(x) < 2: return None try: return statistics.correlation(_ranks(x), _ranks(y)) except statistics.StatisticsError: return None def _ratio(num, den): return num / den if den else None def metrics(sa, sb, critical=1000): """Agreement between the slacks of two steps for one check. sa, sb: endpoint -> slack of the endpoints constrained in both steps. The critical set is the union of the worst `critical` endpoints of A and of B, excluding endpoints with positive slack in both steps, which are not being optimized. Correlation and error are measured on it. """ top_a = worst_endpoints(sa, critical) top_b = worst_endpoints(sb, critical) crit = [ep for ep in top_a | top_b if sa[ep] <= 0 or sb[ep] <= 0] x = [sa[ep] for ep in crit] y = [sb[ep] for ep in crit] deltas = [b - a for a, b in zip(x, y)] viol_a = {ep for ep, s in sa.items() if s < 0} viol_b = {ep for ep, s in sb.items() if s < 0} return { "critical": critical, "n": len(crit), "spearman": spearman(x, y), "bias": statistics.mean(deltas) if deltas else None, "mae": statistics.mean(abs(d) for d in deltas) if deltas else None, "rmse": (statistics.mean(d * d for d in deltas) ** 0.5 if deltas else None), "max_abs": max(abs(d) for d in deltas) if deltas else None, # Fraction of the critical set in the worst `critical` of both steps "top_agreement": _ratio( sum(ep in top_a and ep in top_b for ep in crit), len(crit) ), # Of B's violators, the fraction already violating in A "violator_recall": _ratio(len(viol_a & viol_b), len(viol_b)), # Of A's violators, the fraction still violating in B "violator_precision": _ratio(len(viol_a & viol_b), len(viol_a)), } def compare(a, b, check, worst=None, critical=1000): """Compare one check (setup or hold) of two snapshots. Unconstrained endpoints (e.g. outputs of CTS dummy loads) are ignored. Returns a dict with: only_a / only_b: constrained endpoints in only one netlist, [(endpoint, slack)] constrained_only_a / constrained_only_b: endpoints in both netlists but constrained in only one, [(endpoint, slack)] both: [(endpoint, slack_a, slack_b)], restricted to the union of the worst `worst` endpoints of A and of B if `worst` is given metrics: metrics() of all endpoints constrained in both """ sa, sb = a[check], b[check] both = [ep for ep in sa if ep in sb] m = metrics({ep: sa[ep] for ep in both}, {ep: sb[ep] for ep in both}, critical) if worst: keep = worst_endpoints(sa, worst) | worst_endpoints(sb, worst) both = [ep for ep in both if ep in keep] return { "only_a": [(ep, sa[ep]) for ep in sa if ep not in b["endpoints"]], "only_b": [(ep, sb[ep]) for ep in sb if ep not in a["endpoints"]], "constrained_only_a": [ (ep, sa[ep]) for ep in sa if ep not in sb and ep in b["endpoints"] ], "constrained_only_b": [ (ep, sb[ep]) for ep in sb if ep not in sa and ep in a["endpoints"] ], "both": [(ep, sa[ep], sb[ep]) for ep in both], "metrics": m, } def movement(both, tolerance=0.0): """Counts of how endpoint slack changed between A and B. improved/degraded/unchanged exclude endpoints with positive slack in both steps, which are not being optimized. Changes of at most `tolerance` (seconds) count as unchanged. """ crit = [(s_a, s_b) for _, s_a, s_b in both if s_a <= 0 or s_b <= 0] return { "improved": sum(s_b - s_a > tolerance for s_a, s_b in crit), "degraded": sum(s_a - s_b > tolerance for s_a, s_b in crit), "unchanged": sum(abs(s_b - s_a) <= tolerance for s_a, s_b in crit), "newly_failing": sum(s_a >= 0 > s_b for _, s_a, s_b in both), "newly_passing": sum(s_b >= 0 > s_a for _, s_a, s_b in both), } # Digits after the decimal point for each time unit DIGITS = {"s": 12, "ns": 3, "ps": 1} def _fmt(value, scale, digits): return "-" if value is None else f"{value * scale:.{digits}f}" def _num(value, scale): return None if value is None else float(f"{value * scale:.6g}") def _label(snapshot): """Step name, with the parasitics model the step was timed with.""" if snapshot["parasitics"]: return f"{snapshot['name']} ({snapshot['parasitics']} parasitics)" return snapshot["name"] def _summary_lines(cmp, check, scale, unit, digits, tolerance=0.0): m = movement(cmp["both"], tolerance) lines = [ f"{check:5s} both={len(cmp['both'])} onlyA={len(cmp['only_a'])}" f" onlyB={len(cmp['only_b'])}" f" constrained only in A={len(cmp['constrained_only_a'])}" f" only in B={len(cmp['constrained_only_b'])}", f" not positive in both: improved={m['improved']}" f" degraded={m['degraded']} unchanged={m['unchanged']}" f" newly failing={m['newly_failing']}" f" newly passing={m['newly_passing']}", ] if cmp["both"]: deltas = [(s_b - s_a) * scale for _, s_a, s_b in cmp["both"]] lines.append( f" slack delta (B-A) {unit}:" f" mean={statistics.mean(deltas):.{digits}f}" f" median={statistics.median(deltas):.{digits}f}" f" min={min(deltas):.{digits}f} max={max(deltas):.{digits}f}" ) mt = cmp["metrics"] lines.append( f" critical (worst {mt['critical']} of A or B, not positive in" f" both): n={mt['n']} spearman={_fmt_num(mt['spearman'])}" f" top agreement={_fmt_num(mt['top_agreement'])}" ) lines.append( f" {unit}: bias={_fmt(mt['bias'], scale, digits)}" f" MAE={_fmt(mt['mae'], scale, digits)}" f" RMSE={_fmt(mt['rmse'], scale, digits)}" f" max|delta|={_fmt(mt['max_abs'], scale, digits)}" ) lines.append( f" violators: recall={_fmt_num(mt['violator_recall'])}" f" precision={_fmt_num(mt['violator_precision'])}" ) return lines def _fmt_num(value): return "-" if value is None else f"{value:.3f}" def cmd_list(args): scale = UNITS[args.unit] header = f"{'step':32s} {'parasitics':14s} {'endpoints':>9s}" for check in CHECKS: header += f" {check + ' WNS':>10s} {check + ' TNS':>10s} {'#viol':>6s}" print(header + f" ({args.unit})") for s in load_dir(args.reports_dir): line = f"{s['name']:32s} {s['parasitics']:14s} {len(s['endpoints']):9d}" for check in CHECKS: sm = summary(s[check]) line += ( f" {_fmt(sm['wns'], scale, args.digits):>10s}" f" {_fmt(sm['tns'], scale, args.digits):>10s}" f" {sm['violations']:6d}" ) print(line) def _pair_file(a, b): return f"{a['name']}__{b['name']}.html" def cmd_track(args): scale = UNITS[args.unit] snaps = load_dir(args.reports_dir) pairs = list(zip(snaps, snaps[1:])) if args.html: os.makedirs(args.html, exist_ok=True) rows = [] for i, (a, b) in enumerate(pairs): print(f"{_label(a)} -> {_label(b)}") cmps = { check: compare(a, b, check, args.worst, args.critical) for check in CHECKS } for check in CHECKS: for line in _summary_lines( cmps[check], check, scale, args.unit, args.digits, args.tolerance ): print(" " + line) rows.append((a, b, cmps)) if args.html: links = [("index", "index.html")] if i > 0: links.insert(0, ("previous", _pair_file(*pairs[i - 1]))) if i + 1 < len(pairs): links.append(("next", _pair_file(*pairs[i + 1]))) page = compare_html( a, b, cmps, scale, args.unit, args.worst, links, args.digits, args.tolerance, ) with open(os.path.join(args.html, _pair_file(a, b)), "w") as f: f.write(page) if args.html: index = os.path.join(args.html, "index.html") with open(index, "w") as f: f.write( track_index_html( rows, scale, args.unit, args.worst, args.critical, args.digits, args.tolerance, ) ) print(f"Wrote {len(pairs)} comparisons to {args.html}, see {index}") def track_index_html(rows, scale, unit, worst, critical=1000, digits=3, tolerance=0.0): """Index of step-to-step comparisons with a summary of each.""" design = rows[0][0]["design"] if rows else "" # Cells are strings, or (before, after) pairs shown as "before -> after" table = [] for a, b, cmps in rows: # Endpoints with positive slack in both steps are not considered changed = considered = improved = 0 cells = [] for check in CHECKS: cmp = cmps[check] m = movement(cmp["both"], tolerance) sa, sb = summary(a[check]), summary(b[check]) added_removed = sum( s <= 0 for key in ( "only_a", "only_b", "constrained_only_a", "constrained_only_b", ) for _, s in cmp[key] ) changed += m["improved"] + m["degraded"] + added_removed considered += m["improved"] + m["degraded"] + m["unchanged"] considered += added_removed improved += m["improved"] cells += [ (_fmt(sa["wns"], scale, digits), _fmt(sb["wns"], scale, digits)), (_fmt(sa["tns"], scale, digits), _fmt(sb["tns"], scale, digits)), str(m["improved"]), str(m["degraded"]), str(m["newly_failing"]), str(m["newly_passing"]), _fmt_num(cmp["metrics"]["spearman"]), _fmt_num(cmp["metrics"]["top_agreement"]), _fmt(cmp["metrics"]["mae"], scale, digits), ] if not changed: row_class = "same" elif improved == considered: row_class = "better" else: row_class = "" table.append((a, b, row_class, cells)) # Pad both sides of the pairs to the column width so they line up widths = {} for _, _, _, cells in table: for i, c in enumerate(cells): if isinstance(c, tuple): w = widths.get(i, (0, 0)) widths[i] = (max(w[0], len(c[0])), max(w[1], len(c[1]))) def cell(i, c): if isinstance(c, tuple): wa, wb = widths[i] c = ( f"{html.escape(c[0])}" f" → " f"{html.escape(c[1])}" ) else: c = html.escape(c) return f"{c}" body = "" for a, b, row_class, cells in table: link = ( f"" f"{html.escape(a['name'])} → {html.escape(b['name'])}" ) parasitics = a["parasitics"] if b["parasitics"] != a["parasitics"]: parasitics += " → " + b["parasitics"] cls = f" class='{row_class}'" if row_class else "" tds = "".join(cell(i, c) for i, c in enumerate(cells)) body += f"{link}{parasitics}{tds}\n" head = "".join(f"{check}" for check in CHECKS) sub = "".join( "WNSTNSimproveddegraded" "newly failingnewly passing" "Spearmantop agreementMAE" for _ in CHECKS ) note = ( f" Only the worst {worst} endpoints of each pair are compared." if worst else "" ) return ( TRACK_INDEX_TEMPLATE.replace( "__TITLE__", html.escape(f"{design} endpoint slack by step") ) .replace("__UNIT__", unit) .replace("__NOTE__", html.escape(note)) .replace("__CRITICAL__", str(critical)) .replace("__TOL__", f"{tolerance * scale:g} {unit}") .replace("__HEAD__", head) .replace("__SUB__", sub) .replace("__BODY__", body) ) TRACK_INDEX_TEMPLATE = """ __TITLE__

__TITLE__

Each row compares a step with the next one (__UNIT__). Endpoints with positive slack in both steps are not considered, and slack changes of at most __TOL__ count as unchanged. Grey rows: no considered endpoint changed. Green rows: every considered endpoint improved (setup and hold). Spearman, top agreement and MAE are measured on the critical set: the worst __CRITICAL__ endpoints of either step, excluding those with positive slack in both.__NOTE__
__HEAD____SUB__ __BODY__
stepsparasitics
""" def cmd_compare(args): scale = UNITS[args.unit] a = load_snapshot(args.a) b = load_snapshot(args.b) print(f"A: {_label(a)}\nB: {_label(b)}") if args.worst: print(f"Endpoints in both restricted to the worst {args.worst} of A or B") cmps = {check: compare(a, b, check, args.worst, args.critical) for check in CHECKS} for check in CHECKS: for line in _summary_lines( cmps[check], check, scale, args.unit, args.digits, args.tolerance ): print(line) if args.output: with open(args.output, "w") as f: f.write( compare_html( a, b, cmps, scale, args.unit, args.worst, digits=args.digits, tolerance=args.tolerance, ) ) print(f"Wrote {args.output}") def cmd_export(args): scale = UNITS[args.unit] out = open(args.output, "w", newline="") if args.output else sys.stdout writer = csv.writer(out) writer.writerow( [ "order", "step", "parasitics", "endpoint", f"setup_{args.unit}", f"hold_{args.unit}", ] ) for order, s in enumerate(load_dir(args.reports_dir)): for ep in sorted(s["endpoints"]): writer.writerow( [ order, s["step"], s["parasitics"], ep, _num(s["setup"].get(ep), scale), _num(s["hold"].get(ep), scale), ] ) if args.output: out.close() def compare_html(a, b, cmps, scale, unit, worst, links=(), digits=3, tolerance=0.0): """Self-contained interactive HTML for a two-snapshot comparison. links: optional (label, href) navigation links shown above the title. """ data = {} for check, cmp in cmps.items(): data[check] = { "both": [ [ep, _num(s_a, scale), _num(s_b, scale)] for ep, s_a, s_b in cmp["both"] ], "only_a": sorted( ([ep, _num(s, scale)] for ep, s in cmp["only_a"]), key=lambda r: r[0] ), "only_b": sorted( ([ep, _num(s, scale)] for ep, s in cmp["only_b"]), key=lambda r: r[0] ), "constrained_only_a": len(cmp["constrained_only_a"]), "constrained_only_b": len(cmp["constrained_only_b"]), "summary_a": { k: _num(v, scale) if k in ("wns", "tns") else v for k, v in summary(a[check]).items() }, "summary_b": { k: _num(v, scale) if k in ("wns", "tns") else v for k, v in summary(b[check]).items() }, "metrics": { k: _num(v, scale) if k in ("bias", "mae", "rmse", "max_abs") else v for k, v in cmp["metrics"].items() }, } title = f"{a['design']} {a['name']} vs {b['name']}" note = f" Only the worst {worst} endpoints of A or B are plotted." if worst else "" return ( HTML_TEMPLATE.replace("__TITLE__", html.escape(title)) .replace("__A__", html.escape(a["name"])) .replace("__B__", html.escape(b["name"])) .replace("__PAR_A__", html.escape(a["parasitics"] or "unknown")) .replace("__PAR_B__", html.escape(b["parasitics"] or "unknown")) .replace("__NOTE__", html.escape(note)) .replace( "__NAV__", " | ".join( f"{html.escape(label)}" for label, href in links ), ) .replace("__UNIT__", unit) .replace("__DIGITS__", str(digits)) .replace("__TOL__", repr(tolerance * scale)) .replace("__DATA__", json.dumps(data).replace(" __TITLE__

__TITLE__

Endpoint slack: x = A (__A__), y = B (__B__), __UNIT__. Parasitics: A __PAR_A__, B __PAR_B__.__NOTE__ Drag to zoom, double-click to reset.
Improved and degraded exclude endpoints with positive slack in both steps.

Most degraded

Excluding endpoints with positive slack in both

Most improved

Excluding endpoints with positive slack in both

Endpoints only in A (__A__)

Endpoints only in B (__B__)

""" def main(argv=None): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) sub = parser.add_subparsers(dest="cmd", required=True) def common(p, worst=True): p.add_argument("--unit", choices=UNITS, default="ns") p.add_argument( "--digits", type=int, help="digits after the decimal point for times" " (default: 3 for ns, 1 for ps)", ) p.add_argument( "--tolerance", type=float, help="slack changes of at most this much (in --unit) count as" " unchanged (default: half the last displayed digit, so a change" " shown as 0 is unchanged)", ) if worst: p.add_argument( "--worst", type=int, metavar="N", help="only compare the worst N endpoints of A or B", ) p.add_argument( "--critical", type=int, default=1000, metavar="K", help="correlation and error are measured on the worst K" " endpoints of A or B, excluding those with positive slack in" " both (default 1000)", ) p = sub.add_parser("list", help="list snapshots with WNS/TNS per step") p.add_argument("reports_dir") common(p, worst=False) p.set_defaults(func=cmd_list) p = sub.add_parser("track", help="compare consecutive snapshots") p.add_argument("reports_dir") p.add_argument( "--html", metavar="DIR", help="write an HTML scatter for every step vs. its successor and an" " index.html to DIR", ) common(p) p.set_defaults(func=cmd_track) p = sub.add_parser("compare", help="compare two snapshots") p.add_argument("a", help="earlier snapshot (x axis)") p.add_argument("b", help="later snapshot (y axis)") p.add_argument("-o", "--output", help="write interactive HTML scatter") common(p) p.set_defaults(func=cmd_compare) p = sub.add_parser("export", help="flatten all snapshots to CSV") p.add_argument("reports_dir") p.add_argument("-o", "--output", help="CSV file (default stdout)") common(p, worst=False) p.set_defaults(func=cmd_export) args = parser.parse_args(argv) if args.digits is None: args.digits = DIGITS[args.unit] if args.tolerance is None: args.tolerance = 0.5 * 10**-args.digits # Internally slack is in seconds args.tolerance /= UNITS[args.unit] args.func(args) if __name__ == "__main__": main()