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] 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