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() 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