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