CIRCT 24.0.0git
Loading...
Searching...
No Matches
test_backpressure.py
Go to the documentation of this file.
1# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2# See https://llvm.org/LICENSE.txt for license information.
3# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4"""Cosim integration tests for channel backpressure (cosim protocol v4 flow
5control), using the DUTs in `hw/backpressure.py`.
6
7 * Host -> simulation: with the loopback's gate closed the hardware never
8 consumes `in`, so `try_write` must start failing after roughly the
9 default in-flight limit (16) plus the endpoint's internal buffering, and
10 recover once the gate opens.
11
12 * Simulation -> host: with `stream` connected with a small buffer and not
13 read, the producer must stall (its `Cosim_Endpoint_ToHost` deasserts
14 ready) instead of pushing everything into host memory.
15
16 * Void simulation -> host: a backed-up void stream must deliver every
17 message once drained, on both native cosim and the one-item DMA engines.
18 The DMA engine retains a rejected message and re-delivers it later, which
19 must not trip the zero-width placeholder check.
20"""
21
22from __future__ import annotations
23
24import time
25
26import esiaccel
27from esiaccel.accelerator import AcceleratorConnection
28from esiaccel.cosim.pytest import cosim_test
29
30from .conftest import HW_DIR
31
32# Must match RpcClient::DefaultToServerMaxInFlight.
33DEFAULT_TO_SERVER_MAX_IN_FLIGHT = 16
34# Slack for messages absorbed by hardware buffering (cosim endpoints, BSP
35# pipeline stages) beyond the cosim flow-control in-flight limits.
36HW_SLACK = 8
37
38
39def _fill(try_write, start: int) -> int:
40 """Call `try_write(value)` with increasing values from `start` until it keeps
41 failing for a while, so credits for anything the hardware takes in the
42 meantime are observed. Returns the next unwritten value."""
43 value = start
44 quiet_since = time.monotonic()
45 while time.monotonic() - quiet_since < 1.0:
46 if try_write(value):
47 value += 1
48 quiet_since = time.monotonic()
49 assert value - start < 1000, "try_write never applied backpressure"
50 else:
51 time.sleep(0.01)
52 return value
53
54
55def _wait_stable(read_value, settle: float = 1.0, timeout: float = 60.0):
56 """Poll `read_value()` until it stops changing for `settle` seconds."""
57 deadline = time.monotonic() + timeout
58 last = read_value()
59 last_change = time.monotonic()
60 while time.monotonic() - last_change < settle:
61 assert time.monotonic() < deadline, "value never settled"
62 time.sleep(0.1)
63 cur = read_value()
64 if cur != last:
65 last, last_change = cur, time.monotonic()
66 return last
67
68
69@cosim_test(HW_DIR / "backpressure.py")
71
72 def test_host_to_sim(self, conn: AcceleratorConnection) -> None:
73 acc = conn.build_accelerator()
74 dut = acc.children[esiaccel.AppID("loopback")]
75 gate = dut.ports[esiaccel.AppID("gate")]
76 gate.connect()
77 inp = dut.ports[esiaccel.AppID("in")].write_port("data").connect()
78 # Unbounded, so the loopback output never stalls the input.
79 out = dut.ports[esiaccel.AppID("out")].read_port("data").connect(
80 buffer_size=0)
81
82 # Gate closed (reset state): writes must back up.
83 accepted = _fill(inp.try_write, 0)
84 assert DEFAULT_TO_SERVER_MAX_IN_FLIGHT <= accepted <= (
85 DEFAULT_TO_SERVER_MAX_IN_FLIGHT + HW_SLACK), accepted
86 assert not inp.try_write(accepted)
87
88 # Open the gate: everything accepted so far comes out, in order.
89 gate.write(1)
90 for i in range(accepted):
91 assert out.read() == i
92
93 # With the consumer running, blocking writes flow despite the limit.
94 total = accepted + 200
95 for i in range(accepted, total):
96 inp.write(i)
97 for i in range(accepted, total):
98 assert out.read() == i
99
100 # Close the gate again and check try_write recovers once it reopens.
101 gate.write(0)
102 stalled_at = _fill(inp.try_write, total)
103 assert stalled_at - total <= DEFAULT_TO_SERVER_MAX_IN_FLIGHT + HW_SLACK
104 gate.write(1)
105 for i in range(total, stalled_at):
106 assert out.read() == i
107 deadline = time.monotonic() + 30
108 while not inp.try_write(stalled_at):
109 assert time.monotonic() < deadline, "try_write never recovered"
110 time.sleep(0.01)
111 assert out.read() == stalled_at
112
113 def test_sim_to_host(self, conn: AcceleratorConnection) -> None:
114 acc = conn.build_accelerator()
115 dut = acc.children[esiaccel.AppID("producer")]
116 produce = dut.ports[esiaccel.AppID("produce")]
117 query = dut.ports[esiaccel.AppID("query")]
118 sent = dut.ports[esiaccel.AppID("sent")]
119 for p in (produce, query, sent):
120 p.connect()
121 # Small buffer: both the host polling queue and the in-flight limit.
122 buffer = 4
123 stream = dut.ports[esiaccel.AppID("stream")].read_port("data").connect(
124 buffer_size=buffer)
125
126 def sent_count() -> int:
127 query.write(0)
128 return sent.read().result()
129
130 n = 1000
131 produce.write(n)
132 stalled = _wait_stable(sent_count)
133 # Host polling queue + in-flight limit + hardware buffering.
134 assert 2 * buffer <= stalled <= 2 * buffer + HW_SLACK, stalled
135
136 # Draining the stream lets the producer finish, with nothing lost.
137 for i in range(n):
138 assert stream.read() == i
139 assert sent_count() == n
140
141 def test_void_sim_to_host(self, conn: AcceleratorConnection) -> None:
143
144
145def _check_void_sim_to_host(conn: AcceleratorConnection) -> None:
146 """Back up a void stream so the transport has to retry delivering a message
147 the host rejected, then drain it and check nothing was lost."""
148 acc = conn.build_accelerator()
149 dut = acc.children[esiaccel.AppID("void_producer")]
150 produce = dut.ports[esiaccel.AppID("produce")]
151 query = dut.ports[esiaccel.AppID("query")]
152 sent = dut.ports[esiaccel.AppID("sent")]
153 for p in (produce, query, sent):
154 p.connect()
155 stream = dut.ports[esiaccel.AppID("stream")].read_port("data").connect(
156 buffer_size=1)
157
158 def sent_count() -> int:
159 query.write(0)
160 return sent.read().result()
161
162 n = 50
163 produce.write(n)
164 stalled = _wait_stable(sent_count)
165 assert stalled < n, stalled
166
167 for _ in range(n):
168 assert stream.read() is None
169 assert sent_count() == n
170
171
172@cosim_test(HW_DIR / "backpressure.py", args=("{tmp_dir}", "cosim_dma"))
174 """Backpressure through the one-item DMA engines, which retain a message the
175 host rejects and re-deliver it on a later poll."""
176
177 def test_void_sim_to_host(self, conn: AcceleratorConnection) -> None:
None test_void_sim_to_host(self, AcceleratorConnection conn)
None test_sim_to_host(self, AcceleratorConnection conn)
None test_void_sim_to_host(self, AcceleratorConnection conn)
None test_host_to_sim(self, AcceleratorConnection conn)
None _check_void_sim_to_host(AcceleratorConnection conn)
_wait_stable(read_value, float settle=1.0, float timeout=60.0)