|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import json |
| 7 | +import struct |
| 8 | +import sys |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from executorch.backends.arm.scripts import etdump_to_chrome_trace |
| 13 | +from executorch.devtools.etdump import schema_flatcc as flatcc |
| 14 | +from executorch.devtools.etdump.serialize import serialize_to_etdump_flatcc |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def etdump_path(tmp_path): |
| 19 | + event = flatcc.ProfileEvent( |
| 20 | + name="op", |
| 21 | + chain_index=0, |
| 22 | + instruction_id=0, |
| 23 | + delegate_debug_id_int=-1, |
| 24 | + delegate_debug_id_str="", |
| 25 | + delegate_debug_metadata=b"", |
| 26 | + start_time=1000, |
| 27 | + end_time=1500, |
| 28 | + ) |
| 29 | + dump = flatcc.ETDumpFlatCC( |
| 30 | + version=0, |
| 31 | + run_data=[ |
| 32 | + flatcc.RunData( |
| 33 | + name="Execute", |
| 34 | + bundled_input_index=-1, |
| 35 | + allocators=[], |
| 36 | + events=[ |
| 37 | + flatcc.Event( |
| 38 | + profile_event=event, allocation_event=None, debug_event=None |
| 39 | + ) |
| 40 | + ], |
| 41 | + ) |
| 42 | + ], |
| 43 | + ) |
| 44 | + path = tmp_path / "etdump.bin" |
| 45 | + data = serialize_to_etdump_flatcc(dump) |
| 46 | + path.write_bytes(struct.pack("<I", len(data)) + data) |
| 47 | + return path |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "scale,frequency,timestamp,duration", |
| 52 | + [ |
| 53 | + ("cycles", "500000000", 2.0, 1.0), |
| 54 | + ("cycles", "1000000000", 1.0, 0.5), |
| 55 | + ("ns", None, 1.0, 0.5), |
| 56 | + ("us", None, 1000.0, 500.0), |
| 57 | + ("ms", None, 1000000.0, 500000.0), |
| 58 | + ("s", None, 1000000000.0, 500000000.0), |
| 59 | + ], |
| 60 | +) |
| 61 | +def test_trace_uses_microseconds( |
| 62 | + monkeypatch, tmp_path, etdump_path, scale, frequency, timestamp, duration |
| 63 | +): |
| 64 | + output = tmp_path / "trace.json" |
| 65 | + command = [ |
| 66 | + "etdump_to_chrome_trace", |
| 67 | + "--etdump_path", |
| 68 | + str(etdump_path), |
| 69 | + "--output", |
| 70 | + str(output), |
| 71 | + "--source_time_scale", |
| 72 | + scale, |
| 73 | + ] |
| 74 | + if frequency is not None: |
| 75 | + command += ["--clock_frequency_hz", frequency] |
| 76 | + monkeypatch.setattr(sys, "argv", command) |
| 77 | + |
| 78 | + etdump_to_chrome_trace.main() |
| 79 | + |
| 80 | + events = json.loads(output.read_text())["traceEvents"] |
| 81 | + event = next(event for event in events if event["ph"] == "X") |
| 82 | + assert event["ts"] == pytest.approx(timestamp) |
| 83 | + assert event["dur"] == pytest.approx(duration) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize("frequency", [None, "0", "-1", "nan", "inf"]) |
| 87 | +def test_cycle_trace_requires_valid_frequency(monkeypatch, tmp_path, frequency, capsys): |
| 88 | + command = [ |
| 89 | + "etdump_to_chrome_trace", |
| 90 | + "--etdump_path", |
| 91 | + str(tmp_path / "missing.bin"), |
| 92 | + "--output", |
| 93 | + str(tmp_path / "trace.json"), |
| 94 | + "--source_time_scale", |
| 95 | + "cycles", |
| 96 | + ] |
| 97 | + if frequency is not None: |
| 98 | + command += ["--clock_frequency_hz", frequency] |
| 99 | + monkeypatch.setattr(sys, "argv", command) |
| 100 | + |
| 101 | + with pytest.raises(SystemExit) as error: |
| 102 | + etdump_to_chrome_trace.main() |
| 103 | + |
| 104 | + assert error.value.code == 2 |
| 105 | + assert "positive finite --clock_frequency_hz" in capsys.readouterr().err |
0 commit comments