From 5ed1bc40f2df15cc474a64833febb800a7f19eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Wed, 23 Sep 2026 11:08:35 +0200 Subject: [PATCH 1/2] util: genElapsedTime parses the timing line wherever it sits on the line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit genElapsedTime.py writes the per-stage summary, the only place ORFS reports a hash of a stage's result. It read the elapsed time positionally: strip the line, delete "Elapsed time: ", split on "[h:]", on the first "." and on ":". That works only when the timing line starts the log line. Give it a prefix, as a RUN_CMD wrapper that stamps log lines does ("[ 123.45] "), and the prefix survives the delete, the split on "." cuts at the prefix's own decimal point, and the line falls to "Elapsed time not understood". The row is then skipped: elapsed, peak memory and the sha1sum column vanish, "No elapsed time found" does not fire because the line was found, and the exit status is 0. Match the timing line with a regex anchored on its format instead, hours optional as run_command.py only emits them past an hour. An unprefixed line gives exactly the number it did before, fraction of a second dropped as before, and an unparseable line still reaches "not understood". test_genElapsedTime_parses_output asserted only that the stage name appeared, which it does even when the row is dropped; it now asserts the elapsed and memory columns. Two cases are added: a prefixed line, which fails before this change, and the hours form. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Øyvind Harboe --- flow/test/test_run_command.py | 50 +++++++++++++++++++++++++++++++++++ flow/util/genElapsedTime.py | 37 +++++++++++++------------- 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/flow/test/test_run_command.py b/flow/test/test_run_command.py index c71f3c8b0e..149940afa2 100644 --- a/flow/test/test_run_command.py +++ b/flow/test/test_run_command.py @@ -283,6 +283,56 @@ def test_genElapsedTime_parses_output(self): genElapsedTime.scan_logs(["--logDir", log_dir, "--noHeader"]) output = mock_out.getvalue() self.assertIn("1_test", output) + # The elapsed column is the point of the table; a line that + # parses to nothing drops the whole row, exit status 0. + self.assertRegex(output, r"1_test\s+\S*\s+\d+\s+\d+") + + def test_genElapsedTime_parses_prefixed_output(self): + """Verify genElapsedTime.py can parse a timing line behind a log prefix.""" + sys.path.insert( + 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "util") + ) + import genElapsedTime + from io import StringIO + from unittest.mock import patch + + # A RUN_CMD wrapper may stamp every log line -- an elapsed-seconds + # prefix, say -- so the timing line is not at the start of its line. + log_dir = self.tmp_dir.name + log_file = os.path.join(log_dir, "1_test.log") + with open(log_file, "w") as f: + f.write("[ 0.001] some output\n") + f.write( + "[ 123.456] Elapsed time: 0:04.26[h:]min:sec. " + "CPU time: user 4.08 sys 0.17 (99%). Peak memory: 671508KB.\n" + ) + with patch("sys.stdout", new_callable=StringIO) as mock_out: + genElapsedTime.scan_logs(["--logDir", log_dir, "--noHeader"]) + output = mock_out.getvalue() + # 0:04.26 is 4 seconds, and 671508KB is 655MB; the prefix is not + # part of either. + self.assertRegex(output, r"1_test\s+\S*\s+4\s+655") + + def test_genElapsedTime_parses_hours(self): + """Verify genElapsedTime.py understands the optional hours field.""" + sys.path.insert( + 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "util") + ) + import genElapsedTime + from io import StringIO + from unittest.mock import patch + + log_dir = self.tmp_dir.name + log_file = os.path.join(log_dir, "1_test.log") + with open(log_file, "w") as f: + f.write( + "Elapsed time: 1:01:01.00[h:]min:sec. " + "CPU time: user 4.08 sys 0.17 (99%). Peak memory: 671508KB.\n" + ) + with patch("sys.stdout", new_callable=StringIO) as mock_out: + genElapsedTime.scan_logs(["--logDir", log_dir, "--noHeader"]) + output = mock_out.getvalue() + self.assertRegex(output, r"1_test\s+\S*\s+3661\s+655") if __name__ == "__main__": diff --git a/flow/util/genElapsedTime.py b/flow/util/genElapsedTime.py index 209404c394..3336b651ea 100755 --- a/flow/util/genElapsedTime.py +++ b/flow/util/genElapsedTime.py @@ -8,6 +8,7 @@ import hashlib import pathlib import os +import re import sys # Parse and validate arguments @@ -21,6 +22,15 @@ RESULT_EXTS = [".v", ".rtlil", ".odb", ".def", ".spef", ".gds", ".sdc"] +# The timing line run_command.py emits, matched wherever it appears in +# the line rather than from its start. A RUN_CMD wrapper is free to +# prefix every log line -- a per-line elapsed stamp, "[ 123.45] " -- +# and such a prefix must not change what this reports. Hours are +# optional, as _format_elapsed only emits them past an hour, and the +# fraction of a second is matched so it can be discarded. +ELAPSED_RE = re.compile(r"Elapsed time: (?:(\d+):)?(\d+):(\d+)(?:\.\d+)?\[h:\]min:sec") + + def get_hashes(f): """Return [(ext, sha1), ...] for every result file alongside log `f` whose extension is in RESULT_EXTS. A yosys stage typically @@ -77,24 +87,15 @@ def print_log_dir_times(logdir, args): # Elapsed time: 0:04.26[h:]min:sec. CPU time: user 4.08 sys 0.17 (99%). Peak memory: 671508KB. if "Elapsed time" in line: found = True - # Extract the portion that has the time - timePor = line.strip().replace("Elapsed time: ", "") - # Remove the units from the time portion - timePor = timePor.split("[h:]", 1)[0] - # Remove any fraction of a second - timePor = timePor.split(".", 1)[0] - # Calculate elapsed time that has this format 'h:m:s' - timeList = timePor.split(":") - if len(timeList) == 2: - # Only minutes and seconds are present - elapsedTime = int(timeList[0]) * 60 + int(timeList[1]) - elif len(timeList) == 3: - # Hours, minutes, and seconds are present - elapsedTime = ( - int(timeList[0]) * 3600 - + int(timeList[1]) * 60 - + int(timeList[2]) - ) + # Match the 'h:m:s' time against the known format, + # so anything the line is prefixed with is ignored. + timeMatch = ELAPSED_RE.search(line) + if timeMatch: + hours, minutes, seconds = timeMatch.groups() + # Any fraction of a second is dropped. + elapsedTime = int(minutes) * 60 + int(seconds) + if hours is not None: + elapsedTime += int(hours) * 3600 else: print( "Elapsed time not understood in", str(line), file=sys.stderr From 341991d1855f9961b3f1eb967af985317d7e5583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Wed, 23 Sep 2026 11:14:19 +0200 Subject: [PATCH 2/2] flow: flow.sh runs the stage through RUN_CMD, like the rest of the flow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit variables.mk defines RUN_CMD as the way a logged tool invocation is run, and every logged target in flow/Makefile goes through it: yosys metrics, generate_abstract, merge, drc, cdl, lvs, `run`. The exception was flow.sh, which every stage goes through, and which spelled out `$PYTHON_EXE $SCRIPTS_DIR/run_command.py` by hand. RUN_CMD governed the peripheral logs and none of the stage logs; an override (to stamp lines, tee elsewhere, wrap the tool) silently missed floorplan, place, cts, grt and route. flow.sh now runs the stage through RUN_CMD, defaulting to the command it spelled out, so a plain `make` is unchanged. Depends on the previous commit: flow.sh parses the stage log back with genElapsedTime.py, which until then dropped the summary row for a line a wrapper had prefixed. flow/test/test_flow_sh.py runs flow.sh with stub tools: with RUN_CMD unset the stage log has its timing line and summary row; with RUN_CMD set to a recording wrapper the stage goes through it and the summary row is still there. The second fails before this change. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Øyvind Harboe --- flow/scripts/flow.sh | 7 ++- flow/test/test_flow_sh.py | 96 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 flow/test/test_flow_sh.py diff --git a/flow/scripts/flow.sh b/flow/scripts/flow.sh index 241e92fe6c..3351900486 100755 --- a/flow/scripts/flow.sh +++ b/flow/scripts/flow.sh @@ -3,6 +3,11 @@ set -euo pipefail mkdir -p "$RESULTS_DIR" "$LOG_DIR" "$REPORTS_DIR" "$OBJECTS_DIR" +# The rest of the flow runs logged commands through RUN_CMD; do the same +# here so an override reaches the stage logs too. Unset -- a plain `make` +# -- keeps exactly the command this script used to spell out inline. +RUN_CMD="${RUN_CMD:-$PYTHON_EXE $SCRIPTS_DIR/run_command.py}" + echo "Running $2.tcl, stage $1" ( @@ -11,7 +16,7 @@ echo "Running $2.tcl, stage $1" eval "$OPENROAD_EXE $OPENROAD_ARGS -exit \"$SCRIPTS_DIR/noop.tcl\"" \ >"$LOG_DIR/$1.tmp.log" 2>&1 - $PYTHON_EXE "$SCRIPTS_DIR/run_command.py" --log "$(realpath "$LOG_DIR/$1.tmp.log")" --append --tee -- \ + $RUN_CMD --log "$(realpath "$LOG_DIR/$1.tmp.log")" --append --tee -- \ $OPENROAD_CMD -no_splash "$SCRIPTS_DIR/$2.tcl" -metrics "$LOG_DIR/$1.json" ) diff --git a/flow/test/test_flow_sh.py b/flow/test/test_flow_sh.py new file mode 100644 index 0000000000..aa944e96b9 --- /dev/null +++ b/flow/test/test_flow_sh.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +"""flow.sh runs every stage, and runs it through RUN_CMD like the rest of +the flow. With RUN_CMD unset, a stage runs exactly as before; with it set, +the stage goes through it and the per-stage summary still comes out. + +The tools are stubbed: OPENROAD_EXE is `true` and OPENROAD_CMD is `echo`, +so the stage is quick and needs nothing installed. +""" + +import os +import subprocess +import sys +import tempfile +import textwrap +import unittest + +FLOW_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") +SCRIPTS_DIR = os.path.join(FLOW_DIR, "scripts") +UTILS_DIR = os.path.join(FLOW_DIR, "util") + + +class TestFlowSh(unittest.TestCase): + def setUp(self): + self.tmp_dir = tempfile.TemporaryDirectory() + tmp = self.tmp_dir.name + self.log_dir = os.path.join(tmp, "logs") + self.env = dict( + os.environ, + RESULTS_DIR=os.path.join(tmp, "results"), + LOG_DIR=self.log_dir, + REPORTS_DIR=os.path.join(tmp, "reports"), + OBJECTS_DIR=os.path.join(tmp, "objects"), + SCRIPTS_DIR=SCRIPTS_DIR, + UTILS_DIR=UTILS_DIR, + PYTHON_EXE=sys.executable, + OPENROAD_EXE="true", + OPENROAD_ARGS="", + OPENROAD_CMD="echo", + ) + self.env.pop("RUN_CMD", None) + + def tearDown(self): + self.tmp_dir.cleanup() + + def run_stage(self): + result = subprocess.run( + [ + "bash", + os.path.join(SCRIPTS_DIR, "flow.sh"), + "2_1_floorplan", + "floorplan", + ], + env=self.env, + capture_output=True, + text=True, + ) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + with open(os.path.join(self.log_dir, "2_1_floorplan.log")) as f: + return f.read() + + def assert_summary(self, log): + # genElapsedTime.py's row for the stage: name, elapsed, peak memory. + rows = [line for line in log.splitlines() if line.startswith("2_1_floorplan")] + self.assertEqual(len(rows), 1, log) + self.assertGreaterEqual(len(rows[0].split()), 3, rows[0]) + + def test_run_cmd_unset_runs_the_stage_as_before(self): + log = self.run_stage() + self.assertIn("Elapsed time:", log) + self.assert_summary(log) + + def test_run_cmd_runs_the_stage(self): + # A RUN_CMD that records that it ran, then runs run_command.py. + marker = os.path.join(self.tmp_dir.name, "run_cmd_ran") + wrapper = os.path.join(self.tmp_dir.name, "run_cmd.py") + with open(wrapper, "w") as f: + f.write(textwrap.dedent(f"""\ + import os, sys + with open({marker!r}, "a") as m: + m.write(" ".join(sys.argv[1:]) + "\\n") + run_command = os.path.join({SCRIPTS_DIR!r}, "run_command.py") + os.execv(sys.executable, [sys.executable, run_command] + sys.argv[1:]) + """)) + self.env["RUN_CMD"] = f"{sys.executable} {wrapper}" + + log = self.run_stage() + + self.assertTrue(os.path.exists(marker), "RUN_CMD did not run the stage") + with open(marker) as f: + self.assertIn("floorplan.tcl", f.read()) + self.assertIn("Elapsed time:", log) + self.assert_summary(log) + + +if __name__ == "__main__": + unittest.main()