[Fix] (Remote): order buffer writebacks by producing seq so a stale capture writeback cannot retire a newer GPU-write mark

This commit is contained in:
2026-10-12 06:46:04 -04:00
parent 9b5e15dfc8
commit bc3c4fbb6c
11 changed files with 150 additions and 19 deletions
+2 -2
View File
@@ -508,9 +508,9 @@ namespace MobileGL::MG_Remote::Client {
blob.Seg = Wire::kSegEvent;
blob.Offset = events.OffsetInSegment(bytes);
blob.Size = head->Size;
MG_Pipe::MGPipeClientOnBufferWriteback(
MG_Pipe::MGPipeClientOnBufferWritebackAt(
MG_Pipe::MGPipeHandle{head->Resource.Slot, head->Resource.Gen},
head->Offset, blob);
head->Offset, blob, head->ProducedAtSeq);
++delivered;
break;
}
@@ -31,7 +31,7 @@ namespace MobileGL::MG_Remote::Client {
ClientSession* session = ClientSession::Active();
if (session == nullptr) return 0;
// A quarter of the ring, not the MaxRecordBytes half: the record carries its own
// header and the 24-byte EventBufferWritebackHead beside the payload, and the ring
// header and the 32-byte EventBufferWritebackHead beside the payload, and the ring
// may still hold a few small events (gpu-written, gl-error) posted earlier in the
// same verb's apply. Each slice round-trips with its own barrier + drain, so the
// ring never holds more than one slice's bytes.
@@ -72,6 +72,11 @@ namespace MobileGL::MG_Remote::Client {
return MGPipeStageChunkBytesFor(session->StageCapacityBytes());
}
Uint64 GpuWriteOrderSeq() {
ClientSession* session = ClientSession::Active();
return session == nullptr ? 0 : session->LastPublishedSeq();
}
void AwaitBufferWriteback(BufferObject& buffer) {
// THE WAIT IS THE BARRIER'S WAIT (R-3). The reply-slot id IS the record's seq, so
// "appliedSeq reached my readback" and "my answer is back" are one condition, and
@@ -33,6 +33,12 @@ namespace MobileGL::MG_Remote::Client {
// caller writes, because the loop would be a hang in exactly that configuration.
void AwaitBufferWriteback(MG_State::GLState::BufferObject& buffer);
// The order a GPU-write mark is stamped with: the last command seq this session
// published, so the work the mark stands for is in a LATER record and only a writeback
// the server produced after that record can retire it (BufferObject::WritebackFromBackend).
// 0 with no session, where every writeback is synchronous and needs no ordering.
Uint64 GpuWriteOrderSeq();
// The slice a whole-buffer readback is cut into so one writeback event always fits
// SEG_EVENT. The writeback's bytes travel INLINE in the event record (P5c ev, CONTRACT-P5C
@@ -457,6 +457,7 @@ namespace MobileGL::MG_Remote::Server {
head.Resource = Transport::EventHandle{res.Slot, res.Gen};
head.Offset = offset;
head.Size = bytes.Size;
head.ProducedAtSeq = session.Consumer().ProducingSeq();
std::memcpy(slot, &head, sizeof(head));
if (bytes.Size != 0) {
std::memcpy(static_cast<Uint8*>(slot) + sizeof(head),
+11 -3
View File
@@ -73,12 +73,20 @@ namespace MobileGL::MG_Remote::Transport {
// makes "the destination is the client's shadow" (contract table 1 row 22)
// reachable without a second segment. `Size` is therefore the record's own
// tail length and is cross-checked against it.
//
// `ProducedAtSeq` is the SEG_CMD seq of the record whose apply posted the
// writeback (the record count the server had applied, when it was posted
// between records). Writebacks a client did not ask for - a transform
// feedback capture closing - drain whenever the client next waits, which
// can be after it has marked the same buffer written by a later record; the
// seq is what lets it tell an old writeback from the answer to its own.
struct EventBufferWritebackHead {
EventHandle Resource;
std::uint64_t Offset; // destination offset inside the resource
std::uint64_t Size; // inline byte count that follows
std::uint64_t Offset; // destination offset inside the resource
std::uint64_t Size; // inline byte count that follows
std::uint64_t ProducedAtSeq; // SEG_CMD seq the bytes are current as of
};
static_assert(sizeof(EventBufferWritebackHead) == 24, "wire shape");
static_assert(sizeof(EventBufferWritebackHead) == 32, "wire shape");
// OnGpuWritten(res, rangeCount, ranges). EventRange[RangeCount] follows.
struct EventGpuWrittenHead {
+20 -4
View File
@@ -466,10 +466,20 @@ namespace MobileGL::MG_Remote::Transport {
// A terminal rejection after Pop must not acknowledge an unapplied record.
// A bool callback returning false ends this consumer's session; it cannot retry
// the popped record. Existing void callbacks retain their original semantics.
if constexpr (std::is_same_v<std::invoke_result_t<Apply, const RingRecordView&>, bool>) {
if (!apply(view)) return false;
} else {
apply(view);
// ProducingSeq() names THIS record while its apply runs, so a reverse
// event the apply posts carries the seq of the record that produced it.
struct ApplyingScope {
bool& flag;
explicit ApplyingScope(bool& f) : flag(f) { flag = true; }
~ApplyingScope() { flag = false; }
};
{
const ApplyingScope applying(m_applying);
if constexpr (std::is_same_v<std::invoke_result_t<Apply, const RingRecordView&>, bool>) {
if (!apply(view)) return false;
} else {
apply(view);
}
}
++m_appliedSeq;
Watermark::AdvanceApplied(*m_control, m_appliedSeq);
@@ -535,6 +545,11 @@ namespace MobileGL::MG_Remote::Transport {
void NotifyClient();
std::uint64_t AppliedSeq() const { return m_appliedSeq; }
// The seq of the record whose apply is running, or - between records -
// the last one applied. Every byte the server reads back at this point is
// current as of exactly this record, which is what a writeback event
// stamps so the client can order it against its own later records.
std::uint64_t ProducingSeq() const { return m_applying ? m_appliedSeq + 1 : m_appliedSeq; }
// How many kRecBorrowSlot records this consumer has seen. Non-zero in P5
// is a finding, not a statistic.
std::uint64_t BorrowedRecordsSeen() const { return m_borrowedSeen; }
@@ -556,6 +571,7 @@ namespace MobileGL::MG_Remote::Transport {
std::uint64_t m_retirableCursor = 0;
std::uint64_t m_borrowedSeen = 0;
bool m_borrowHeld = false;
bool m_applying = false;
// Out of line so ApplyOne, which is a template in a header this layer
// keeps free of MobileGL/Includes.h, can still log.