[Chore] (MG_Backend, MG_Backend/DirectGLES, MG_Backend/DirectVulkan, MG_Backend/MGPipe, MG_Backend/Record, MG_Benchmark/Buffer, MG_Benchmark/Container, MG_Benchmark/Driver, MG_Benchmark/Program, MG_Benchmark/ShaderCache, MG_Benchmark/Transpile, MG_Gbm, MG_Impl, MG_Impl/CGLImpl, MG_Impl/DyldInterpose, MG_Impl/EGLImpl, MG_Impl/GLImpl, MG_Impl/GLXImpl, MG_Impl/NSOpenGLImpl, MG_Impl/Pipe, MG_Impl/WGLImpl, MG_IntegrationTest, MG_IntegrationTest/Harness, MG_IntegrationTest/Scenarios, MG_Pipe, MG_Remote, MG_Remote/Client, MG_Remote/Protocol, MG_Remote/Server, MG_Remote/Transport, MG_Remote/Wire, MG_State/EGLState, MG_State/GLState, MG_Test, MG_Test/Backend, MG_Test/BackendLoader, MG_Test/Buffer, MG_Test/EGLState, MG_Test/Framebuffer, MG_Test/Gbm, MG_Test/Pipe, MG_Test/Pipeline, MG_Test/Program, MG_Test/Query, MG_Test/SelfTest, MG_Test/ShaderTranspiler, MG_Test/State, MG_Test/Swapchain, MG_Test/Texture, MG_Test/Util, MG_Test/VertexArray, MG_Test/Wire, MG_Util/Async, MG_Util/BackendLoaders, MG_Util/Converters, MG_Util/Damage, MG_Util/Debug, MG_Util/Math, MG_Util/Metrics, MG_Util/SelfTest, MG_Util/ShaderTranspiler, MG_Util/Texture, MG_Util/X11, MobileGL, scripts/data, tools/cts, tools/device_bench, tools/spikes, tools/trace_replay, tools/wgl-smoke): clang-format every C and C++ source

- Layout only: token streams match up to split literals, continuations and using order
- The size totals fall, so the structure baseline is lowered
This commit is contained in:
2026-10-16 06:30:38 -04:00
parent 76b10cc35b
commit f4994ee36e
747 changed files with 35842 additions and 36261 deletions
+16 -15
View File
@@ -144,9 +144,7 @@ namespace MobileGL::MG_Remote {
}
// How many elements of `elementBytes` could still possibly be there. The gate a
// length-prefixed array is held to before it reserves anything.
Uint64 RoomFor(Uint64 elementBytes) const {
return elementBytes == 0 ? 0 : m_left / elementBytes;
}
Uint64 RoomFor(Uint64 elementBytes) const { return elementBytes == 0 ? 0 : m_left / elementBytes; }
Bool Ok() const { return m_ok; }
Uint64 Left() const { return m_left; }
// Trailing bytes the format does not account for are a REFUSAL: they mean the
@@ -244,8 +242,8 @@ namespace MobileGL::MG_Remote {
return false;
}
if (version != kFormatCapabilitiesCodecVersion) {
MGLOG_E("MG_Remote caps: format-capability blob is version %u, this build reads %u",
version, kFormatCapabilitiesCodecVersion);
MGLOG_E("MG_Remote caps: format-capability blob is version %u, this build reads %u", version,
kFormatCapabilitiesCodecVersion);
return false;
}
if (targets != MG_Backend::kFormatCapabilityTargetCount ||
@@ -253,8 +251,7 @@ namespace MobileGL::MG_Remote {
// Not a corrupt stream: a peer whose TextureTarget or TextureInternalFormat enum
// is a different size. Reading it anyway shears every cell onto a neighbouring
// format, which is exactly the failure the ABI fingerprint exists to make loud.
MGLOG_E("MG_Remote caps: format-capability blob is %ux%u, this build is %llux%llu",
targets, formats,
MGLOG_E("MG_Remote caps: format-capability blob is %ux%u, this build is %llux%llu", targets, formats,
static_cast<unsigned long long>(MG_Backend::kFormatCapabilityTargetCount),
static_cast<unsigned long long>(MG_Backend::kFormatCapabilityFormatCount));
return false;
@@ -278,8 +275,7 @@ namespace MobileGL::MG_Remote {
return false;
}
table[index / MG_Backend::kFormatCapabilityFormatCount]
[index % MG_Backend::kFormatCapabilityFormatCount] =
FormatCapabilityFlags(raw);
[index % MG_Backend::kFormatCapabilityFormatCount] = FormatCapabilityFlags(raw);
}
return true;
};
@@ -460,9 +456,8 @@ namespace MobileGL::MG_Remote {
inputs.ProgramArtifactsSchema = MG_State::GLState::ProgramArtifactsSchemaFingerprint();
inputs.OpCount = static_cast<Uint64>(MG_Pipe::MGPWireOp::kOpCount);
#if MOBILEGL_BUILD_DISAGGREGATED
inputs.ControlSchemaRevision =
(static_cast<Uint64>(MOBILEGL_PROTOCOL_CONTROL_REVISION) << 32) |
MG_Pipe::kMGPipeResourceRespecifyExtentCarrierRevision;
inputs.ControlSchemaRevision = (static_cast<Uint64>(MOBILEGL_PROTOCOL_CONTROL_REVISION) << 32) |
MG_Pipe::kMGPipeResourceRespecifyExtentCarrierRevision;
#else
// The pull build has no respecify extent carrier; preserve its pre-P7 wire fingerprint.
inputs.ControlSchemaRevision = MOBILEGL_PROTOCOL_CONTROL_REVISION;
@@ -479,8 +474,14 @@ namespace MobileGL::MG_Remote {
return fingerprint;
}
// Compatibility spelling for existing wire tests and helper peers.
Uint64 CapsAbiFingerprint() { return WireFingerprint(); }
const char* BuildFingerprint() { return MOBILEGL_BUILD_STAMP_VALUE; }
Bool BuildFingerprintPresent() { return MOBILEGL_BUILD_STAMP_PRESENT != 0; }
Uint64 CapsAbiFingerprint() {
return WireFingerprint();
}
const char* BuildFingerprint() {
return MOBILEGL_BUILD_STAMP_VALUE;
}
Bool BuildFingerprintPresent() {
return MOBILEGL_BUILD_STAMP_PRESENT != 0;
}
} // namespace MobileGL::MG_Remote
@@ -34,16 +34,16 @@ namespace MobileGL::MG_Remote::Client {
// `dl` (CONTRACT-P6 5.2): the family word every death here carries.
if (tier <= 1) {
SessionFail(MGFatalFamily::UnimplementedAdoptTier,
"MGPipe: Fatal{UnimplementedAdoptTier, \"T%u\"} - MOBILEGL_IPC_ADOPT_TIER=%u "
"reached a map_persistent that no handshake settled; T0 and T1 are not "
"implemented and only T2 (emulate) runs.",
static_cast<unsigned>(tier), static_cast<unsigned>(tier));
"MGPipe: Fatal{UnimplementedAdoptTier, \"T%u\"} - MOBILEGL_IPC_ADOPT_TIER=%u "
"reached a map_persistent that no handshake settled; T0 and T1 are not "
"implemented and only T2 (emulate) runs.",
static_cast<unsigned>(tier), static_cast<unsigned>(tier));
} else {
SessionFail(MGFatalFamily::UnimplementedAdoptTier,
"MGPipe: Fatal{UnimplementedAdoptTier, \"%u\"} - MOBILEGL_IPC_ADOPT_TIER=%u is "
"not an adoption tier; the only values are 0 and 1 (P11) and 2 (emulate, the "
"P5 default).",
static_cast<unsigned>(tier), static_cast<unsigned>(tier));
"MGPipe: Fatal{UnimplementedAdoptTier, \"%u\"} - MOBILEGL_IPC_ADOPT_TIER=%u is "
"not an adoption tier; the only values are 0 and 1 (P11) and 2 (emulate, the "
"P5 default).",
static_cast<unsigned>(tier), static_cast<unsigned>(tier));
}
}
} // namespace MobileGL::MG_Remote::Client
@@ -25,7 +25,7 @@
// library but never constructs one.
namespace MobileGL::MG_Backend {
extern UniquePtr<BackendObject>& pActiveBackendObject;
}
} // namespace MobileGL::MG_Backend
namespace MobileGL::MG_Remote::Client {
@@ -134,7 +134,9 @@ namespace MobileGL::MG_Remote::Client {
return true;
}
Bool BackendObject_Remote::InitPbufferSurface(EGLint, EGLint) { return true; }
Bool BackendObject_Remote::InitPbufferSurface(EGLint, EGLint) {
return true;
}
const RendererInfo& BackendObject_Remote::GetRendererInfo() const {
// TRAP 1: a reference, so the storage is the mirror's and not a temporary's.
@@ -192,8 +194,7 @@ namespace MobileGL::MG_Remote::Client {
return MG_Backend::BackendObject::InitializeEGLDisplay(dpy, major, minor);
}
Bool BackendObject_Remote::CreateEGLWindowSurface(EGLSurface surface,
const MG_Backend::WindowHandle& handle) {
Bool BackendObject_Remote::CreateEGLWindowSurface(EGLSurface surface, const MG_Backend::WindowHandle& handle) {
// The handle first: the server's backend has to know which window it is about to make
// a surface for, and ServerSetWindowHandle is the only way to tell it.
WaitForApplyBeforeEglForwarder("CreateEGLWindowSurface");
@@ -237,7 +238,8 @@ namespace MobileGL::MG_Remote::Client {
if (!reply.ok) {
MGLOG_E("MG_Remote client: the server could not resize the server-owned window surface to %ux%u "
"(channel rc=%d, refusal %s)",
width, height, static_cast<int>(reply.transport), Server::SurfaceRefusalCodeName(reply.refusal));
width, height, static_cast<int>(reply.transport),
Server::SurfaceRefusalCodeName(reply.refusal));
return false;
}
NoteServerOwnedRequest(surface, width, height);
@@ -384,8 +386,7 @@ namespace MobileGL::MG_Remote::Client {
return true;
}
Bool BackendObject_Remote::MakeEGLCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read,
EGLContext ctx) {
Bool BackendObject_Remote::MakeEGLCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx) {
// FORWARD FIRST, THEN RUN THE BASE. The server has to own the context before the base
// class latches "the surface is initialised" and calls InitCapabilities, because
// InitCapabilities' answer comes from a snapshot the server can only publish once its
@@ -492,7 +493,8 @@ namespace MobileGL::MG_Remote::Client {
const std::lock_guard<std::recursive_mutex> lock(m_eglStateMutex);
ResetEGLRuntimeState();
m_homedSurfaces.clear();
for (const auto& [id, references] : m_sessionSharedImages) m_lostSessionSharedImages.insert(id);
for (const auto& [id, references] : m_sessionSharedImages)
m_lostSessionSharedImages.insert(id);
m_sessionSharedImages.clear();
// The display the application initialized is initialized on the new server too: its
// surfaces and contexts are created against it from here on.
@@ -634,7 +636,8 @@ namespace MobileGL::MG_Remote::Client {
op.Damage[MG_Pipe::kMGPImportPlane1Pitch] = static_cast<Int32>(layout.Pitch[1]);
op.Damage[MG_Pipe::kMGPImportYuvColorSpace] = static_cast<Int32>(layout.ColorSpace);
op.Damage[MG_Pipe::kMGPImportYuvRange] = static_cast<Int32>(layout.Range);
op.Damage[MG_Pipe::kMGPImportYuvSiting] = static_cast<Int32>((layout.SitingX & 0xffffu) | (layout.SitingY << 16));
op.Damage[MG_Pipe::kMGPImportYuvSiting] =
static_cast<Int32>((layout.SitingX & 0xffffu) | (layout.SitingY << 16));
op.Damage[MG_Pipe::kMGPImportModifierLo] = static_cast<Int32>(static_cast<Uint32>(layout.Modifier));
op.Damage[MG_Pipe::kMGPImportModifierHi] = static_cast<Int32>(static_cast<Uint32>(layout.Modifier >> 32));
op.Damage[MG_Pipe::kMGPImportHasModifier] = layout.HasModifier ? 1 : 0;
+43 -26
View File
@@ -18,7 +18,7 @@
#include "../CapsCodec.h"
#include "../FatalFunnel.h"
#include <Config.h> // MG_Config::SplitTransportRequestedByConfig
#include <Config.h> // MG_Config::SplitTransportRequestedByConfig
#include <MG_Backend/MGPipe/PipeInputs.h> // MGPipeServerArm, D1c's role predicate
#include <MG_State/GLState/Core.h>
#include <MG_Util/Async/ShaderCompilePool.h>
@@ -69,8 +69,7 @@ namespace MobileGL::MG_Remote::Client {
// re-derived here from this process's switch, on every adoption.
void ReconcileClientCompileExtensions(RendererInfo& renderer) {
Vector<GLExtension>& extensions = renderer.RendererGLInfo.Extensions;
const auto found =
std::find(extensions.begin(), extensions.end(), E_GL_KHR_parallel_shader_compile);
const auto found = std::find(extensions.begin(), extensions.end(), E_GL_KHR_parallel_shader_compile);
const Bool advertised = found != extensions.end();
const Bool compilesAsync = MG_Util::Async::AsyncShaderCompileEnabled();
if (advertised == compilesAsync) return;
@@ -86,8 +85,7 @@ namespace MobileGL::MG_Remote::Client {
} // namespace
void CapsMirror::Adopt(const MG_Pipe::MGPCaps& caps, const MG_Backend::FormatCapabilityCache& formats,
const RendererInfo& renderer, const String& apiVersion,
BackendType backend) {
const RendererInfo& renderer, const String& apiVersion, BackendType backend) {
m_caps = caps;
// The two blobrefs inside MGPCaps name SEG_STAGE runs that belong to the SERVER's
// encoder and are meaningless on this side; the decoded objects beside them are the
@@ -123,10 +121,16 @@ namespace MobileGL::MG_Remote::Client {
if (g_capsAdoptedHook != nullptr) g_capsAdoptedHook();
}
void SetCapsAdoptedHook(CapsAdoptedHook hook) { g_capsAdoptedHook = hook; }
void SetCapsAdoptedHook(CapsAdoptedHook hook) {
g_capsAdoptedHook = hook;
}
Bool CapsMirror::Valid() const { return m_generation != 0; }
Uint64 CapsMirror::Generation() const { return m_generation; }
Bool CapsMirror::Valid() const {
return m_generation != 0;
}
Uint64 CapsMirror::Generation() const {
return m_generation;
}
const RendererInfo& CapsMirror::Renderer() const {
WarnPlaceholder("Renderer", m_generation);
@@ -153,7 +157,9 @@ namespace MobileGL::MG_Remote::Client {
return m_backend;
}
Uint64 CapsMirror::CallMask() const { return m_caps.CallMask; }
Uint64 CapsMirror::CallMask() const {
return m_caps.CallMask;
}
Bool CapsMirror::HasCap(MG_Pipe::MGPCapBit bit) const {
return (m_caps.CallMask & static_cast<Uint64>(bit)) != 0;
@@ -178,8 +184,7 @@ namespace MobileGL::MG_Remote::Client {
MGLOG_W("MG_Remote client: the server does not consume MGPipe subsystem 0x%llx - "
"this family emits NOTHING and the legacy pull path runs for it "
"(R-8). callMask=0x%llx, caps generation %llu",
static_cast<unsigned long long>(subsystemBit),
static_cast<unsigned long long>(m_caps.CallMask),
static_cast<unsigned long long>(subsystemBit), static_cast<unsigned long long>(m_caps.CallMask),
static_cast<unsigned long long>(m_generation));
}
}
@@ -209,8 +214,7 @@ namespace MobileGL::MG_Remote::Client {
// The wait is real only when somebody else is fetching the snapshot. ClientSession's
// hook answers false when no bring-up is in flight and when the CALLING thread is the
// one performing it - both of which mean nothing can land while this call blocks.
if (g_firstSnapshotWait != nullptr && g_firstSnapshotWait(kFirstSnapshotWaitMs) &&
m_generation != 0) {
if (g_firstSnapshotWait != nullptr && g_firstSnapshotWait(kFirstSnapshotWaitMs) && m_generation != 0) {
return;
}
// NOT MGLOG_W AND CARRY ON. The whole defect this closes is that carrying on was
@@ -226,11 +230,12 @@ namespace MobileGL::MG_Remote::Client {
"pull path on the client to take over. The session must be started - and its "
"first CapsSnapshot adopted - before any frontend object of this family is "
"born; MobileGL::Initialize does that before MG_State::Init()",
CapsSubsystemName(subsystemBit),
static_cast<unsigned long long>(subsystemBit));
CapsSubsystemName(subsystemBit), static_cast<unsigned long long>(subsystemBit));
}
void SetCapsFirstSnapshotWait(CapsFirstSnapshotWait wait) { g_firstSnapshotWait = wait; }
void SetCapsFirstSnapshotWait(CapsFirstSnapshotWait wait) {
g_firstSnapshotWait = wait;
}
Uint64 PublishedCapsGeneration() {
return g_publishedGeneration.load(::std::memory_order_acquire);
@@ -238,22 +243,34 @@ namespace MobileGL::MG_Remote::Client {
const char* CapsSubsystemName(Uint64 subsystemBit) {
switch (subsystemBit) {
case MG_Pipe::kMGPipeSubsystemResources: return "Resources";
case MG_Pipe::kMGPipeSubsystemVertexInput: return "VertexInput";
case MG_Pipe::kMGPipeSubsystemFramebuffer: return "Framebuffer";
case MG_Pipe::kMGPipeSubsystemTextureResources: return "TextureResources";
case MG_Pipe::kMGPipeSubsystemSamplers: return "Samplers";
case MG_Pipe::kMGPipeSubsystemPrograms: return "Programs";
case MG_Pipe::kMGPipeSubsystemBufferBindings: return "BufferBindings";
default: break;
case MG_Pipe::kMGPipeSubsystemResources:
return "Resources";
case MG_Pipe::kMGPipeSubsystemVertexInput:
return "VertexInput";
case MG_Pipe::kMGPipeSubsystemFramebuffer:
return "Framebuffer";
case MG_Pipe::kMGPipeSubsystemTextureResources:
return "TextureResources";
case MG_Pipe::kMGPipeSubsystemSamplers:
return "Samplers";
case MG_Pipe::kMGPipeSubsystemPrograms:
return "Programs";
case MG_Pipe::kMGPipeSubsystemBufferBindings:
return "BufferBindings";
default:
break;
}
// A bit with no word yet, or a mask of several. Named by its value rather than by a
// guess: G14 lets the list grow, never lets a wrong word into a Fatal.
return "unnamed-subsystem";
}
Uint64 ConsumerRefusals() { return g_consumerRefusals; }
Uint64 LastRefusedSubsystem() { return g_lastRefusedSubsystem; }
Uint64 ConsumerRefusals() {
return g_consumerRefusals;
}
Uint64 LastRefusedSubsystem() {
return g_lastRefusedSubsystem;
}
void ResetConsumerRefusalsForTest() {
g_consumerRefusals = 0;
g_lastRefusedSubsystem = 0;
+1 -2
View File
@@ -28,8 +28,7 @@ namespace MobileGL::MG_Remote::Client {
// Replaces the whole mirror and bumps Generation(). Called once per CapsSnapshot,
// including the re-sends that mean "invalidate" (R-12).
void Adopt(const MG_Pipe::MGPCaps& caps, const MG_Backend::FormatCapabilityCache& formats,
const RendererInfo& renderer, const String& apiVersion,
BackendType backend);
const RendererInfo& renderer, const String& apiVersion, BackendType backend);
// False until the first snapshot. The placeholder answers below are still safe to
// read - that is the point - but a caller that can wait should.
File diff suppressed because it is too large Load Diff
+11 -16
View File
@@ -20,7 +20,7 @@
#include "../Transport/ControlInbox.h"
#include <Includes.h>
#include <atomic> // the device-lost latch is read off the GL thread
#include <atomic> // the device-lost latch is read off the GL thread
#include <Config.h>
#include <MG_Pipe/MGPipe.h>
@@ -47,7 +47,7 @@
namespace MobileGL::MG_Remote::Transport {
class InProcessTransport;
}
} // namespace MobileGL::MG_Remote::Transport
namespace MobileGL::MG_Remote::Client {
@@ -165,8 +165,7 @@ namespace MobileGL::MG_Remote::Client {
static MobileGLResult RemoteControlSinkThunk(void* user, Server::SurfaceControlFrame& frame);
MobileGLResult RunRemoteSurfaceControlFrame(Server::SurfaceControlFrame& frame);
MobileGLResult FinishStartup(Transport::Doorbell* peerBell,
Transport::Doorbell* selfBell,
MobileGLResult FinishStartup(Transport::Doorbell* peerBell, Transport::Doorbell* selfBell,
bool startApplyThreadHere);
// `transport` is this process's end of a connection to an ALREADY
@@ -206,9 +205,9 @@ namespace MobileGL::MG_Remote::Client {
//
// Waiting is spin(MOBILEGL_IPC_SPIN_US) then park, through Doorbell::Wait, with
// producerParked set before blocking - the shape Doorbell::Wait already implements.
Uint64 EmitAndWait(MG_Pipe::MGPWireOp op, const void* payload, Uint64 payloadBytes,
const void* varTail, Uint64 varTailBytes, void* replyOut,
Uint64 replyBytes, Int32* statusOut, Uint64* replySizeOut = nullptr);
Uint64 EmitAndWait(MG_Pipe::MGPWireOp op, const void* payload, Uint64 payloadBytes, const void* varTail,
Uint64 varTailBytes, void* replyOut, Uint64 replyBytes, Int32* statusOut,
Uint64* replySizeOut = nullptr);
// The same call for a record that carries TWO tails (P5b d1: draw_vbo's user-index span
// or its MGPDrawIndirect block behind the range array). The one-tail form above is this
@@ -225,9 +224,8 @@ namespace MobileGL::MG_Remote::Client {
// skip an answer a caller uses: R-5 still forbids re-deriving one locally, and every
// other kReplySlot row passes true (the default) and waits.
Uint64 EmitAndWaitTails(MG_Pipe::MGPWireOp op, const void* payload, Uint64 payloadBytes,
const Wire::WireTail* tails, Uint32 tailCount, void* replyOut,
Uint64 replyBytes, Int32* statusOut,
Uint64* replySizeOut = nullptr, Bool wantReply = true,
const Wire::WireTail* tails, Uint32 tailCount, void* replyOut, Uint64 replyBytes,
Int32* statusOut, Uint64* replySizeOut = nullptr, Bool wantReply = true,
Bool willReadReply = true);
// ---- P11 B2: T0 (docs/archive/contracts/CONTRACT-P11.md B2) ------------------------------------
@@ -381,8 +379,7 @@ namespace MobileGL::MG_Remote::Client {
// barriered fill is always legal. Under lockstep it checks only with
// MOBILEGL_IPC_BATCH_WAITS=0 and an armed barrier, and fires when the applier is inside
// and this thread is not in a barrier wait. Never fires on the apply thread.
static void RefusePipeInputsTouchWhileApplierOwnsIt(const char* surface,
Bool isBarrieredFill = false);
static void RefusePipeInputsTouchWhileApplierOwnsIt(const char* surface, Bool isBarrieredFill = false);
// ---- c1's additions ---------------------------------------------------------------
@@ -455,8 +452,7 @@ namespace MobileGL::MG_Remote::Client {
// The reply slot for `seq`, addressed seq % slots with the seq stamped back into the
// header for self-check (R-3). `outStatus` is 0 OK / 1 DECLINED / 2 ERROR, and
// DECLINED IS A REAL ANSWER - MapPersistent's nullptr and the four Bool acceptances.
Bool ReadReply(Uint64 seq, void* outBytes, Uint64 outCapacity, Int32* outStatus,
Uint64* outSize);
Bool ReadReply(Uint64 seq, void* outBytes, Uint64 outCapacity, Int32* outStatus, Uint64* outSize);
// What one answer may carry. No single answer is ever bigger than this: a ReadPixels
// whose whole read is bigger is split by the emitter into row bands (or, for a row
// wider than a reply, single-row pieces) that each fit (g5-readback, EmitTables.h
@@ -473,8 +469,7 @@ namespace MobileGL::MG_Remote::Client {
// pixel fits a reply, once for that one pixel instead of emitting; the server's Post
// keeps its own refusal as the last line of defence, but that one fires on the apply
// thread with the record already on the wire, where all the client sees is a hang.
void RequireReadPixelsReplyFits(Uint32 width, Uint32 height, Uint32 format, Uint32 type,
Uint64 bytes) const;
void RequireReadPixelsReplyFits(Uint32 width, Uint32 height, Uint32 format, Uint32 type, Uint64 bytes) const;
// The reverse channel's reading end: OnBufferWriteback / OnGpuWritten /
// OnSurfaceChanged. Drained by the GL thread between verbs.
+57 -45
View File
@@ -97,10 +97,11 @@ namespace MobileGL::MG_Remote::Client {
if (session == nullptr) {
// @Ph-declined (ID-P7-1): returns ClientSession& and runs in the CLIENT - no
// session to hand back, no peer bytes, and the latch is a server-session idea.
SessionFail(MGFatalFamily::NoClientSession, "MGPipe: Fatal{NoClientSession, \"%s\"} - the remote emit table is "
"installed but no ClientSession is active. A slot may not fall through "
"to a driver this role does not have",
slot);
SessionFail(MGFatalFamily::NoClientSession,
"MGPipe: Fatal{NoClientSession, \"%s\"} - the remote emit table is "
"installed but no ClientSession is active. A slot may not fall through "
"to a driver this role does not have",
slot);
}
return *session;
}
@@ -186,8 +187,7 @@ namespace MobileGL::MG_Remote::Client {
g_publishedMaxRecordBytes = encoder.MaxRecordBytesSeen();
MGLOG_I("MGPipe: wire ledger: new maximum record - maxrec=%llu "
"maxrecop=%s cap=%llu (R-10's proof obligation; blobs are cut, a record is not)",
static_cast<unsigned long long>(g_publishedMaxRecordBytes),
encoder.MaxRecordOpName(),
static_cast<unsigned long long>(g_publishedMaxRecordBytes), encoder.MaxRecordOpName(),
static_cast<unsigned long long>(encoder.MaxRecordBytesCap()));
}
}
@@ -208,8 +208,7 @@ namespace MobileGL::MG_Remote::Client {
// travels exactly as it did - the present row's own barrier is the pacing there.
record.FrameSerial = session.AcquirePresentCredit();
record.DamageCount = session.TakePendingPresentDamage(record.Damage);
session.EmitAndWait(MG_Pipe::MGPWireOp::Present, &record, sizeof(record), nullptr, 0,
nullptr, 0, nullptr);
session.EmitAndWait(MG_Pipe::MGPWireOp::Present, &record, sizeof(record), nullptr, 0, nullptr, 0, nullptr);
// R-12's invalidation edge, drained at the one boundary every target crosses. A
// second CapsSnapshot IS the invalidation; nothing else on the client can see that
@@ -278,12 +277,13 @@ namespace MobileGL::MG_Remote::Client {
RemoteFenceProxy& FenceProxy(MG_Backend::BackendSyncHandle proxy) {
const auto it = g_fenceProxies.find(proxy);
if (it == g_fenceProxies.end())
Wire::WireProtocolFatal("Fence.proxy", "unknown client fence proxy");
if (it == g_fenceProxies.end()) Wire::WireProtocolFatal("Fence.proxy", "unknown client fence proxy");
return *it->second;
}
MG_Pipe::MGPipeHandle FenceHandle(MG_Backend::BackendSyncHandle proxy) { return FenceProxy(proxy).Handle; }
MG_Pipe::MGPipeHandle FenceHandle(MG_Backend::BackendSyncHandle proxy) {
return FenceProxy(proxy).Handle;
}
// ---- P10 (CONTRACT-P10.md §1): WHAT THE SERVER HAS SAID IS SIGNALED -----------------
//
@@ -332,7 +332,11 @@ namespace MobileGL::MG_Remote::Client {
// eventually true, and the escalation is the bound that holds even if one is late. A call
// that may wait (a non-zero timeout) is never answered "not yet" here - it crosses and
// waits on the server, as the GL call asks.
enum class FencePollAnswer { Signaled, NotYet, Cross };
enum class FencePollAnswer {
Signaled,
NotYet,
Cross
};
FencePollAnswer AnswerFencePollLocally(ClientSession& session, RemoteFenceProxy& fence, Bool mayWait) {
if (!FencePollsAnswerLocally()) return FencePollAnswer::Cross;
session.PollEntry();
@@ -358,20 +362,17 @@ namespace MobileGL::MG_Remote::Client {
auto proxy = MakeUnique<RemoteFenceProxy>();
proxy->Handle = MG_Pipe::MGPipeSlots().Allocate(MG_Pipe::MGPipeKind::Fence);
const MG_Pipe::MGPHandleOnly desc{proxy->Handle, static_cast<Uint32>(MG_Pipe::MGPipeKind::Fence), 0};
session.EmitAndWait(MG_Pipe::MGPWireOp::FenceCreate, &desc, sizeof(desc),
nullptr, 0, nullptr, 0, nullptr);
session.EmitAndWait(MG_Pipe::MGPWireOp::FenceCreate, &desc, sizeof(desc), nullptr, 0, nullptr, 0, nullptr);
auto* local = proxy.get();
g_fenceProxies.emplace(local, std::move(proxy));
return local;
}
Uint32 ReadFenceReply(ClientSession& session, MG_Pipe::MGPWireOp op,
const void* payload, Uint64 bytes) {
Uint32 ReadFenceReply(ClientSession& session, MG_Pipe::MGPWireOp op, const void* payload, Uint64 bytes) {
Uint32 result = 0;
Int32 status = Wire::ReplySink::kStatusError;
Uint64 replyBytes = 0;
session.EmitAndWait(op, payload, bytes, nullptr, 0, &result, sizeof(result),
&status, &replyBytes);
session.EmitAndWait(op, payload, bytes, nullptr, 0, &result, sizeof(result), &status, &replyBytes);
// A declined reply is an answer, not a corruption: the server declines these rows when
// it cannot answer them, and the client reports a verb that did not happen (the
// device-lost latch, a doorbell that died during teardown) as declined. It gets the
@@ -382,8 +383,7 @@ namespace MobileGL::MG_Remote::Client {
"happen (a lost device, or a teardown); answering the no-op the "
"frontend uses for a fence it cannot wait on",
Wire::WireOpName(op));
return op == MG_Pipe::MGPWireOp::FenceWait ? static_cast<Uint32>(GL_ALREADY_SIGNALED)
: 1u;
return op == MG_Pipe::MGPWireOp::FenceWait ? static_cast<Uint32>(GL_ALREADY_SIGNALED) : 1u;
}
// ERROR AND A SHORT OK ARE STILL THE PROTOCOL CORRUPTIONS THEY ALWAYS WERE: a
// transport fault is not the server saying no, and an OK answer that arrived empty is
@@ -399,9 +399,12 @@ namespace MobileGL::MG_Remote::Client {
RemoteFenceProxy& fence = FenceProxy(proxy);
if (fence.FromEndedSession()) return GL_ALREADY_SIGNALED;
switch (AnswerFencePollLocally(session, fence, /*mayWait=*/timeout != 0)) {
case FencePollAnswer::Signaled: return GL_ALREADY_SIGNALED;
case FencePollAnswer::NotYet: return GL_TIMEOUT_EXPIRED;
case FencePollAnswer::Cross: break;
case FencePollAnswer::Signaled:
return GL_ALREADY_SIGNALED;
case FencePollAnswer::NotYet:
return GL_TIMEOUT_EXPIRED;
case FencePollAnswer::Cross:
break;
}
g_fenceRoundTrips.fetch_add(1, std::memory_order_relaxed);
const MG_Pipe::MGPFenceWait request{fence.Handle, timeout, flags, 0};
@@ -416,9 +419,12 @@ namespace MobileGL::MG_Remote::Client {
RemoteFenceProxy& fence = FenceProxy(proxy);
if (fence.FromEndedSession()) return true;
switch (AnswerFencePollLocally(session, fence, /*mayWait=*/false)) {
case FencePollAnswer::Signaled: return true;
case FencePollAnswer::NotYet: return false;
case FencePollAnswer::Cross: break;
case FencePollAnswer::Signaled:
return true;
case FencePollAnswer::NotYet:
return false;
case FencePollAnswer::Cross:
break;
}
g_fenceRoundTrips.fetch_add(1, std::memory_order_relaxed);
const MG_Pipe::MGPHandleOnly desc{fence.Handle, static_cast<Uint32>(MG_Pipe::MGPipeKind::Fence), 0};
@@ -433,8 +439,8 @@ namespace MobileGL::MG_Remote::Client {
const std::lock_guard<std::mutex> lock(g_fenceMutex);
if (FenceProxy(proxy).FromEndedSession()) return;
const MG_Pipe::MGPFenceWait request{FenceHandle(proxy), timeout, flags, 0};
session.EmitAndWait(MG_Pipe::MGPWireOp::FenceWaitServer, &request, sizeof(request),
nullptr, 0, nullptr, 0, nullptr);
session.EmitAndWait(MG_Pipe::MGPWireOp::FenceWaitServer, &request, sizeof(request), nullptr, 0, nullptr, 0,
nullptr);
}
void EmitDeleteSync(MG_Backend::BackendSyncHandle proxy) {
@@ -446,8 +452,8 @@ namespace MobileGL::MG_Remote::Client {
if (auto* session = ClientSession::Active();
session != nullptr && session->Started() && !FenceProxy(proxy).FromEndedSession()) {
const MG_Pipe::MGPHandleOnly desc{handle, static_cast<Uint32>(MG_Pipe::MGPipeKind::Fence), 0};
session->EmitAndWait(MG_Pipe::MGPWireOp::FenceDestroy, &desc, sizeof(desc),
nullptr, 0, nullptr, 0, nullptr);
session->EmitAndWait(MG_Pipe::MGPWireOp::FenceDestroy, &desc, sizeof(desc), nullptr, 0, nullptr, 0,
nullptr);
}
MG_Pipe::MGPipeSlots().Free(MG_Pipe::MGPipeKind::Fence, handle);
ForgetFence(handle);
@@ -505,16 +511,18 @@ namespace MobileGL::MG_Remote::Client {
// The union, for the places that want every class-C row at once (the definitions and
// the assignments). A package never edits THIS; it edits its own list above.
#define MGR_UNMIGRATED_GL_SLOTS(X) \
MGR_UNMIGRATED_D1_SLOTS(X) \
MGR_UNMIGRATED_I1_SLOTS(X) \
MGR_UNMIGRATED_T2_SLOTS(X) \
MGR_UNMIGRATED_F1_SLOTS(X) \
#define MGR_UNMIGRATED_GL_SLOTS(X) \
MGR_UNMIGRATED_D1_SLOTS(X) \
MGR_UNMIGRATED_I1_SLOTS(X) \
MGR_UNMIGRATED_T2_SLOTS(X) \
MGR_UNMIGRATED_F1_SLOTS(X) \
MGR_UNMIGRATED_TAIL_SLOTS(X)
#define MGR_UNMIGRATED_GL_VALUE_SLOTS(X) MGR_UNMIGRATED_TAIL_VALUE_SLOTS(X)
#define MGR_DEFINE_UNMIGRATED(Name, Ret, Sig) \
Ret Name##_Unmigrated Sig { UnmigratedVerbFatal(#Name); }
#define MGR_DEFINE_UNMIGRATED(Name, Ret, Sig) \
Ret Name##_Unmigrated Sig { \
UnmigratedVerbFatal(#Name); \
}
MGR_UNMIGRATED_GL_SLOTS(MGR_DEFINE_UNMIGRATED)
MGR_UNMIGRATED_GL_VALUE_SLOTS(MGR_DEFINE_UNMIGRATED)
@@ -556,9 +564,8 @@ namespace MobileGL::MG_Remote::Client {
constexpr Uint32 kEmittedSlotsTail = 4;
constexpr Uint32 kEmittedSlotsSync = 5;
constexpr Uint32 kEmittedSlotsQueries = 11;
constexpr Uint32 kEmittedSlots =
kEmittedSlotsP5 + kEmittedSlotsD1 + kEmittedSlotsI1 + kEmittedSlotsT2 + kEmittedSlotsF1 +
kEmittedSlotsTail + kEmittedSlotsSync + kEmittedSlotsQueries;
constexpr Uint32 kEmittedSlots = kEmittedSlotsP5 + kEmittedSlotsD1 + kEmittedSlotsI1 + kEmittedSlotsT2 +
kEmittedSlotsF1 + kEmittedSlotsTail + kEmittedSlotsSync + kEmittedSlotsQueries;
constexpr Uint32 kLocallyAnsweredSlots = 2; // GetIntegeri_v, IsTimerQuerySupported
// EACH PACKAGE'S OWNERSHIP, PINNED. A package that flips a slot removes one row and
@@ -613,8 +620,7 @@ namespace MobileGL::MG_Remote::Client {
// reader is GL_Query.cpp:221. It does NOT ride inside MGPCaps::Dynamic - it is a
// member of GLFunctionsTable, which is exactly the thing a split client never
// receives - so it is answered from kCapCpuXfbPrimitiveAccounting.
table.GL.PrefersCpuXfbPrimitiveAccounting =
CapsMirrorInstance().PrefersCpuXfbPrimitiveAccounting();
table.GL.PrefersCpuXfbPrimitiveAccounting = CapsMirrorInstance().PrefersCpuXfbPrimitiveAccounting();
// ---- class B. P13 W5: every slot a VerbChannel emitter serves (draws, clears,
// blits, readbacks, compute, copies, image binds, XFB, mipmaps) is MG_Record's
@@ -681,8 +687,14 @@ namespace MobileGL::MG_Remote::Client {
return table;
}
Uint32 ImplementedVerbCount() { return kEmittedSlots; }
Uint32 LocallyAnsweredSlotCount() { return kLocallyAnsweredSlots; }
Uint32 UnmigratedSlotCount() { return kUnmigratedSlots; }
Uint32 ImplementedVerbCount() {
return kEmittedSlots;
}
Uint32 LocallyAnsweredSlotCount() {
return kLocallyAnsweredSlots;
}
Uint32 UnmigratedSlotCount() {
return kUnmigratedSlots;
}
} // namespace MobileGL::MG_Remote::Client
+13 -13
View File
@@ -47,27 +47,27 @@ namespace MobileGL::MG_Remote::Client {
// planners are MG_Record's (MG_Impl/Pipe/Verb/VerbPort.h) - the record arm runs them in a
// library without a transport. Re-exported here under the names the remote table's users
// and its unit cases already spell.
using MG_Record::UnmigratedVerbFatal;
using MG_Record::DroppedClearEmissions;
using MG_Record::DroppedDrawEmissions;
using MG_Record::ForEachReadbackBand;
using MG_Record::InstallMonolithVerbPort;
using MG_Record::MonolithVerbPortInstalled;
using MG_Record::SetDropClearEmissionForNegativeControl;
using MG_Record::DroppedClearEmissions;
using MG_Record::SetDropDrawEmissionForNegativeControl;
using MG_Record::DroppedDrawEmissions;
using MG_Record::TightReadbackByteCount;
using MG_Record::ScatterTightReadbackIntoPackState;
using MG_Record::PlanDrawIndirect;
using MG_Record::PlanDrawInfo;
using MG_Record::PlanDrawRange;
using MG_Record::PlanReadbackBands;
using MG_Record::ReadbackBand;
using MG_Record::ReadbackBandPlan;
using MG_Record::PlanReadbackBands;
using MG_Record::ForEachReadbackBand;
using MG_Record::ScatterReadbackBandIntoPackState;
using MG_Record::ReadbackPackStateIsTightForTest;
using MG_Record::ReadbackReplyIsComplete;
using MG_Record::RemoteDrawBindings;
using MG_Record::RemoteIndexSizeFor;
using MG_Record::PlanDrawInfo;
using MG_Record::PlanDrawRange;
using MG_Record::PlanDrawIndirect;
using MG_Record::ScatterReadbackBandIntoPackState;
using MG_Record::ScatterTightReadbackIntoPackState;
using MG_Record::SetDropClearEmissionForNegativeControl;
using MG_Record::SetDropDrawEmissionForNegativeControl;
using MG_Record::TightReadbackByteCount;
using MG_Record::UnmigratedVerbFatal;
// How many of the 71 slots have a real emitter. Reported at bring-up and asserted by the
// gate: a table that silently loses an emitter should not be able to look the same as one
+12 -10
View File
@@ -104,17 +104,19 @@ namespace MobileGL::MG_Remote::Client {
// one outcome the reverse channel exists to prevent: stale bytes that look like a
// successful read. It is the reply form's ReadbackDeclined, arriving one call later.
if (ClientSession::DeviceLost()) {
SessionFail(MGFatalFamily::ReadbackDeclined, "MGPipe: Fatal{ReadbackDeclined, \"buffer-writeback\"} - buffer %u "
"still owes a GPU write (a pack-buffer read or a shader store) and the device is "
"lost, so its readback was declined; its shadow holds the bytes from before the "
"write and is not returned as the GPU's",
buffer.GetExternalIndex());
SessionFail(MGFatalFamily::ReadbackDeclined,
"MGPipe: Fatal{ReadbackDeclined, \"buffer-writeback\"} - buffer %u "
"still owes a GPU write (a pack-buffer read or a shader store) and the device is "
"lost, so its readback was declined; its shadow holds the bytes from before the "
"write and is not returned as the GPU's",
buffer.GetExternalIndex());
}
SessionFail(MGFatalFamily::UnimplementedWritebackWait, "MGPipe: Fatal{UnimplementedWritebackWait} - a ClientSession is active and buffer %u "
"still has an outstanding GPU write after its readback was emitted. The wait is "
"ClientSession::EmitAndWait's (R-3: the reply slot id IS the record seq); P5 package "
"b1 landed the third state and s1/c1 own the wait itself.",
buffer.GetExternalIndex());
SessionFail(MGFatalFamily::UnimplementedWritebackWait,
"MGPipe: Fatal{UnimplementedWritebackWait} - a ClientSession is active and buffer %u "
"still has an outstanding GPU write after its readback was emitted. The wait is "
"ClientSession::EmitAndWait's (R-3: the reply slot id IS the record seq); P5 package "
"b1 landed the third state and s1/c1 own the wait itself.",
buffer.GetExternalIndex());
}
} // namespace MobileGL::MG_Remote::Client
@@ -24,7 +24,6 @@ namespace MobileGL::MG_State::GLState {
namespace MobileGL::MG_Remote::Client {
// ---- SyncGpuWrites' third state (CONTRACT-P5.md section 3) -----------------------
// Blocks until this buffer's OnBufferWriteback has landed. With no session - a build-split
+3 -2
View File
@@ -88,8 +88,9 @@ namespace MobileGL::MG_Remote::Client {
if (!g_declineLogged.exchange(true)) {
ServerProbeTarget target;
(void)ConfiguredProbeTarget(&target);
const auto tookUs = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - started).count();
const auto tookUs =
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - started)
.count();
MGLOG_I("MG_Remote client: no MobileGL server answers at \"%s\" (probed in %lld us); declining, so "
"the system's next GL implementation serves this process",
target.endpoint.c_str(), static_cast<long long>(tookUs));
+2 -2
View File
@@ -30,8 +30,8 @@ namespace MobileGL::MG_Remote::Client {
};
struct ServerProbeTarget {
std::string endpoint; // SocketTransport form: a path, "@name", or "tcp://host:port"
Uint32 timeoutMs = 0; // 0: probing disabled
std::string endpoint; // SocketTransport form: a path, "@name", or "tcp://host:port"
Uint32 timeoutMs = 0; // 0: probing disabled
};
// What the configuration says to probe; false when it names nothing probeable (NotApplicable).
+17 -21
View File
@@ -25,13 +25,13 @@
#include <MG_Pipe/MGPipeTypes.h>
#include <MG_Remote/Client/CapsMirror.h>
#define MGL_BACKEND_SLOT_CAP(Slot, CapBit) \
(::MobileGL::MG_Config::Transport != ::MobileGL::MG_Config::TransportMode::Monolith \
? ::MobileGL::MG_Remote::Client::CapsMirrorInstance().HasCap(CapBit) \
#define MGL_BACKEND_SLOT_CAP(Slot, CapBit) \
(::MobileGL::MG_Config::Transport != ::MobileGL::MG_Config::TransportMode::Monolith \
? ::MobileGL::MG_Remote::Client::CapsMirrorInstance().HasCap(CapBit) \
: ::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot != nullptr)
#define MGL_BACKEND_SLOT_LOCAL(Slot) \
(::MobileGL::MG_Config::Transport == ::MobileGL::MG_Config::TransportMode::Monolith && \
#define MGL_BACKEND_SLOT_LOCAL(Slot) \
(::MobileGL::MG_Config::Transport == ::MobileGL::MG_Config::TransportMode::Monolith && \
::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot != nullptr)
// The POINTER-valued forms, for the several sites shaped `if (const auto f = TABLE.GL.Slot)`.
@@ -41,27 +41,23 @@
// GL_Getter.cpp's GetInteger64v by -150 bytes and GetIntegerv by +2, which is two "resized"
// symbols and a red G1. In a pull build these two expand to the slot expression ITSELF, so the
// init-statement survives verbatim and the pull build's code generation cannot move.
#define MGL_BACKEND_SLOT_PTR_CAP(Slot, CapBit) \
(::MobileGL::MG_Config::Transport != ::MobileGL::MG_Config::TransportMode::Monolith \
? (::MobileGL::MG_Remote::Client::CapsMirrorInstance().HasCap(CapBit) \
? ::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot \
: nullptr) \
#define MGL_BACKEND_SLOT_PTR_CAP(Slot, CapBit) \
(::MobileGL::MG_Config::Transport != ::MobileGL::MG_Config::TransportMode::Monolith \
? (::MobileGL::MG_Remote::Client::CapsMirrorInstance().HasCap(CapBit) \
? ::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot \
: nullptr) \
: ::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot)
#define MGL_BACKEND_SLOT_PTR_LOCAL(Slot) \
(::MobileGL::MG_Config::Transport != ::MobileGL::MG_Config::TransportMode::Monolith \
? nullptr \
#define MGL_BACKEND_SLOT_PTR_LOCAL(Slot) \
(::MobileGL::MG_Config::Transport != ::MobileGL::MG_Config::TransportMode::Monolith \
? nullptr \
: ::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot)
#else
#define MGL_BACKEND_SLOT_CAP(Slot, CapBit) \
(::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot != nullptr)
#define MGL_BACKEND_SLOT_LOCAL(Slot) \
(::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot != nullptr)
#define MGL_BACKEND_SLOT_PTR_CAP(Slot, CapBit) \
(::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot)
#define MGL_BACKEND_SLOT_PTR_LOCAL(Slot) \
(::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot)
#define MGL_BACKEND_SLOT_CAP(Slot, CapBit) (::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot != nullptr)
#define MGL_BACKEND_SLOT_LOCAL(Slot) (::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot != nullptr)
#define MGL_BACKEND_SLOT_PTR_CAP(Slot, CapBit) (::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot)
#define MGL_BACKEND_SLOT_PTR_LOCAL(Slot) (::MobileGL::MG_Backend::gBackendFunctionsTable.GL.Slot)
#endif
+210 -196
View File
@@ -61,7 +61,9 @@ namespace MobileGL::MG_Remote::Client {
// thread an emitter runs the server's code directly, as the decoder does. Under spawn the
// answer is constant per process. Declared in WireTables.h because PipeFill.cpp and
// EGLImpl.cpp need it too; kept out of line so that header includes no server header.
Bool RunsAsTheServerRole() { return Server::ServerLoop::OnApplyThread(); }
Bool RunsAsTheServerRole() {
return Server::ServerLoop::OnApplyThread();
}
namespace {
@@ -92,10 +94,11 @@ namespace MobileGL::MG_Remote::Client {
if (session == nullptr) {
// @Ph-declined (ID-P7-1): returns ClientSession& and runs in the CLIENT - no
// session to hand back, no peer bytes, and the latch is a server-session idea.
SessionFail(MGFatalFamily::NoClientSession, "MGPipe: Fatal{NoClientSession, \"%s\"} - the client wire tables are "
"installed but no ClientSession is active. A row may not fall through to "
"a driver this role does not have",
row);
SessionFail(MGFatalFamily::NoClientSession,
"MGPipe: Fatal{NoClientSession, \"%s\"} - the client wire tables are "
"installed but no ClientSession is active. A row may not fall through to "
"a driver this role does not have",
row);
}
return *session;
}
@@ -104,14 +107,14 @@ namespace MobileGL::MG_Remote::Client {
// record declared no blob" and "the record declared an empty blob" must not be spelled
// the same way on a wire), so a row whose decoder calls RequireDeclaredBlob or
// ResolveOrFatal is checked HERE, on the producing side, where the row has a name.
MG_Pipe::MGPBlobRef StageRequired(ClientSession& session, const char* row,
const void* bytes, Uint64 count) {
MG_Pipe::MGPBlobRef StageRequired(ClientSession& session, const char* row, const void* bytes, Uint64 count) {
if (bytes == nullptr || count == 0) {
SessionFail(MGFatalFamily::BlobMissing, "MGPipe: Fatal{BlobMissing, \"%s\"} - the row's decoder requires a "
"declared blob and the call site handed over %llu bytes at %p. Under "
"monolith the companion pointer carries them; under split they have to "
"be staged, and there is nothing to stage",
row, static_cast<unsigned long long>(count), bytes);
SessionFail(MGFatalFamily::BlobMissing,
"MGPipe: Fatal{BlobMissing, \"%s\"} - the row's decoder requires a "
"declared blob and the call site handed over %llu bytes at %p. Under "
"monolith the companion pointer carries them; under split they have to "
"be staged, and there is nothing to stage",
row, static_cast<unsigned long long>(count), bytes);
}
return session.Encoder().StageBytes(bytes, count);
}
@@ -128,41 +131,40 @@ namespace MobileGL::MG_Remote::Client {
// The three regular shapes
// ---------------------------------------------------------------------------------
#define MGP_WIRE_PLAIN(Name, Payload, Table) \
void Wire_##Name(const MG_Pipe::Payload* payload) { \
if (RunsAsTheServerRole()) { MG_Pipe::MGPipeMonolith##Table().Name(payload); return; } \
ClientSession& session = RequireSession(#Name); \
session.EmitAndWait(MGPWireOp::Name, payload, sizeof(*payload), nullptr, 0, nullptr, 0, \
nullptr); \
++g_emitted; \
#define MGP_WIRE_PLAIN(Name, Payload, Table) \
void Wire_##Name(const MG_Pipe::Payload* payload) { \
if (RunsAsTheServerRole()) { \
MG_Pipe::MGPipeMonolith##Table().Name(payload); \
return; \
} \
ClientSession& session = RequireSession(#Name); \
session.EmitAndWait(MGPWireOp::Name, payload, sizeof(*payload), nullptr, 0, nullptr, 0, nullptr); \
++g_emitted; \
}
#define MGP_WIRE_BLOB(Name, Payload, BlobMember) \
void Wire_##Name(const MG_Pipe::Payload* payload, const void* blobBytes, \
Uint64 blobByteCount) { \
if (RunsAsTheServerRole()) { \
MG_Pipe::MGPipeMonolithContext().Name(payload, blobBytes, blobByteCount); \
return; \
} \
ClientSession& session = RequireSession(#Name); \
MG_Pipe::Payload record = *payload; \
record.BlobMember = StageRequired(session, #Name, blobBytes, blobByteCount); \
session.EmitAndWait(MGPWireOp::Name, &record, sizeof(record), nullptr, 0, nullptr, 0, \
nullptr); \
++g_emitted; \
#define MGP_WIRE_BLOB(Name, Payload, BlobMember) \
void Wire_##Name(const MG_Pipe::Payload* payload, const void* blobBytes, Uint64 blobByteCount) { \
if (RunsAsTheServerRole()) { \
MG_Pipe::MGPipeMonolithContext().Name(payload, blobBytes, blobByteCount); \
return; \
} \
ClientSession& session = RequireSession(#Name); \
MG_Pipe::Payload record = *payload; \
record.BlobMember = StageRequired(session, #Name, blobBytes, blobByteCount); \
session.EmitAndWait(MGPWireOp::Name, &record, sizeof(record), nullptr, 0, nullptr, 0, nullptr); \
++g_emitted; \
}
#define MGP_WIRE_TAIL(Name, Payload, TailType) \
void Wire_##Name(const MG_Pipe::Payload* payload, const void* varTail, Uint32 varTailCount) { \
if (RunsAsTheServerRole()) { \
MG_Pipe::MGPipeMonolithContext().Name(payload, varTail, varTailCount); \
return; \
} \
ClientSession& session = RequireSession(#Name); \
session.EmitAndWait(MGPWireOp::Name, payload, sizeof(*payload), varTail, \
static_cast<Uint64>(varTailCount) * sizeof(MG_Pipe::TailType), \
nullptr, 0, nullptr); \
++g_emitted; \
#define MGP_WIRE_TAIL(Name, Payload, TailType) \
void Wire_##Name(const MG_Pipe::Payload* payload, const void* varTail, Uint32 varTailCount) { \
if (RunsAsTheServerRole()) { \
MG_Pipe::MGPipeMonolithContext().Name(payload, varTail, varTailCount); \
return; \
} \
ClientSession& session = RequireSession(#Name); \
session.EmitAndWait(MGPWireOp::Name, payload, sizeof(*payload), varTail, \
static_cast<Uint64>(varTailCount) * sizeof(MG_Pipe::TailType), nullptr, 0, nullptr); \
++g_emitted; \
}
// -- screen -------------------------------------------------------------------
@@ -170,10 +172,12 @@ namespace MobileGL::MG_Remote::Client {
// store is released by this client after the record is out (the server's import holds its
// own reference until its fence for the store's last use; CONTRACT-P11.md B2).
void Wire_ResourceDestroy(const MG_Pipe::MGPHandleOnly* payload) {
if (RunsAsTheServerRole()) { MG_Pipe::MGPipeMonolithScreen().ResourceDestroy(payload); return; }
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithScreen().ResourceDestroy(payload);
return;
}
ClientSession& session = RequireSession("ResourceDestroy");
session.EmitAndWait(MGPWireOp::ResourceDestroy, payload, sizeof(*payload), nullptr, 0, nullptr, 0,
nullptr);
session.EmitAndWait(MGPWireOp::ResourceDestroy, payload, sizeof(*payload), nullptr, 0, nullptr, 0, nullptr);
++g_emitted;
if (static_cast<MG_Pipe::MGPipeKind>(payload->Kind) == MG_Pipe::MGPipeKind::Buffer)
session.ReleaseT0Store(payload->Handle);
@@ -248,8 +252,7 @@ namespace MobileGL::MG_Remote::Client {
ClientSession& session = RequireSession("SetDynamicState");
MG_Pipe::MGPDynamicState record = *payload;
record.Blob = StageOptional(session, blobBytes, blobByteCount);
session.EmitAndWait(MGPWireOp::SetDynamicState, &record, sizeof(record), nullptr, 0,
nullptr, 0, nullptr);
session.EmitAndWait(MGPWireOp::SetDynamicState, &record, sizeof(record), nullptr, 0, nullptr, 0, nullptr);
++g_emitted;
}
@@ -258,7 +261,9 @@ namespace MobileGL::MG_Remote::Client {
void Wire_SetResidualValueState(const MG_Pipe::MGPResidualValueState*, const void*, Uint64) {
SessionFail(MGFatalFamily::ResidualBlockSize,
"MGPipe: Fatal{ResidualBlockSize} - set_residual_value_state reached the wire table, "
"but %s", "the residual value block was deleted in P13 W3c (op 46 is a retired row; the verify comparator owns the capability cross-check)");
"but %s",
"the residual value block was deleted in P13 W3c (op 46 is a retired row; the verify "
"comparator owns the capability cross-check)");
}
// resource_readback. kReplySlot, and the answer is COMPLETION only: the bytes travel
@@ -267,12 +272,14 @@ namespace MobileGL::MG_Remote::Client {
// row 22). So the reply buffer is deliberately {nullptr, 0} and the wait is what makes
// the writeback already drained by the time this returns.
void Wire_ResourceReadback(const MG_Pipe::MGPReadback* payload, MG_Pipe::MGPReplySlot* reply) {
if (RunsAsTheServerRole()) { MG_Pipe::MGPipeMonolithContext().ResourceReadback(payload, reply); return; }
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithContext().ResourceReadback(payload, reply);
return;
}
ClientSession& session = RequireSession("ResourceReadback");
Int32 status = 0;
const Uint64 seq = session.EmitAndWait(MGPWireOp::ResourceReadback, payload,
sizeof(*payload), nullptr, 0, nullptr, 0,
&status);
const Uint64 seq = session.EmitAndWait(MGPWireOp::ResourceReadback, payload, sizeof(*payload), nullptr, 0,
nullptr, 0, &status);
// Cancellation has no wire ordinal; retain the caller's nonzero local ticket.
if (seq != Wire::kInvalidSeq) reply->Id = seq;
MG_Pipe::MGPipePostReply(*reply, status, 0);
@@ -313,14 +320,12 @@ namespace MobileGL::MG_Remote::Client {
// constant having drifted apart. 1 (and anything below it) is the pre-window shape.
Uint32 CreateWindowEffective() {
if (MG_Config::Ipc.CreateWindow <= 1) return 1;
return MG_Config::Ipc.CreateWindow < kCreateWindowMax ? MG_Config::Ipc.CreateWindow
: kCreateWindowMax;
return MG_Config::Ipc.CreateWindow < kCreateWindowMax ? MG_Config::Ipc.CreateWindow : kCreateWindowMax;
}
struct PendingCreate {
Uint64 Seq = 0;
MG_Pipe::MGPipeKind Kind = MG_Pipe::MGPipeKind::None;
MG_Pipe::MGPipeHandle Handle{};
};
PendingCreate g_createWindow[kCreateWindowMax];
Uint32 g_createWindowHead = 0;
@@ -342,8 +347,8 @@ namespace MobileGL::MG_Remote::Client {
// out to be LATENCY-bound, not throughput-bound (the server applies all 103,097 records in
// 1.95 s of a 9.64 s frame, and the client's 1,161 blocking takes at ~4.9 ms are 5.7 s of
// it) - so the way to make it faster is fewer blocks, not a faster apply.
Uint64 g_blockNoWindow = 0, g_blockSuspectsFull = 0, g_blockSuspectObject = 0,
g_blockWindowStillFull = 0, g_blockTotal = 0;
Uint64 g_blockNoWindow = 0, g_blockSuspectsFull = 0, g_blockSuspectObject = 0, g_blockWindowStillFull = 0,
g_blockTotal = 0;
void NoteCreateBlocked(Uint64& reason) {
++reason;
++g_blockTotal;
@@ -352,8 +357,7 @@ namespace MobileGL::MG_Remote::Client {
if (g_blockTotal % 256 == 0) {
MGLOG_I("P12 create blocks: total=%llu no-window=%llu suspects-full=%llu "
"suspect-object=%llu window-still-full=%llu",
static_cast<unsigned long long>(g_blockTotal),
static_cast<unsigned long long>(g_blockNoWindow),
static_cast<unsigned long long>(g_blockTotal), static_cast<unsigned long long>(g_blockNoWindow),
static_cast<unsigned long long>(g_blockSuspectsFull),
static_cast<unsigned long long>(g_blockSuspectObject),
static_cast<unsigned long long>(g_blockWindowStillFull));
@@ -386,8 +390,7 @@ namespace MobileGL::MG_Remote::Client {
}
void ClearCreateSuspect(MG_Pipe::MGPipeHandle handle) {
for (SizeT i = 0; i < g_createSuspects.size(); ++i) {
if (g_createSuspects[i].Slot != handle.Slot || g_createSuspects[i].Gen != handle.Gen)
continue;
if (g_createSuspects[i].Slot != handle.Slot || g_createSuspects[i].Gen != handle.Gen) continue;
for (SizeT j = i + 1; j < g_createSuspects.size(); ++j)
g_createSuspects[j - 1] = g_createSuspects[j];
g_createSuspects.pop_back();
@@ -399,9 +402,12 @@ namespace MobileGL::MG_Remote::Client {
// rather than defaulted, so a target added later cannot silently borrow the texture latch.
MG_Pipe::MGPipeKind CreateRecordKind(const MG_Pipe::MGPResourceDesc& desc) {
switch (static_cast<MG_Pipe::MGPipeResourceTarget>(desc.Target)) {
case MG_Pipe::MGPipeResourceTarget::Buffer: return MG_Pipe::MGPipeKind::Buffer;
case MG_Pipe::MGPipeResourceTarget::Renderbuffer: return MG_Pipe::MGPipeKind::Renderbuffer;
default: return MG_Pipe::MGPipeKind::Texture;
case MG_Pipe::MGPipeResourceTarget::Buffer:
return MG_Pipe::MGPipeKind::Buffer;
case MG_Pipe::MGPipeResourceTarget::Renderbuffer:
return MG_Pipe::MGPipeKind::Renderbuffer;
default:
return MG_Pipe::MGPipeKind::Texture;
}
}
@@ -420,11 +426,10 @@ namespace MobileGL::MG_Remote::Client {
// blocking take only: the poll is not a round trip, and it is the blocking take
// that is the thing MOBILEGL_IPC_CREATE_WINDOW divides (4,536 -> creates/window).
const Uint64 metricStart =
blocking ? Transport::LinkMetricsBeginReply(
true, static_cast<std::uint32_t>(MGPWireOp::ResourceCreate))
: 0;
const Transport::SessionWait wait =
session.WaitForApplied(head.Seq, blocking ? 120000u : 0u);
blocking
? Transport::LinkMetricsBeginReply(true, static_cast<std::uint32_t>(MGPWireOp::ResourceCreate))
: 0;
const Transport::SessionWait wait = session.WaitForApplied(head.Seq, blocking ? 120000u : 0u);
if (blocking) Transport::LinkMetricsReplyApplied(metricStart);
if (wait != Transport::SessionWait::Reached) return;
Int32 status = Wire::ReplySink::kStatusError;
@@ -451,9 +456,10 @@ namespace MobileGL::MG_Remote::Client {
// ERROR IS NOT A DECLINE (M4 / R-5), and a deferred answer does not change that:
// the same named Fatal the blocking take raises.
if (status == Wire::ReplySink::kStatusError) {
SessionFail(MGFatalFamily::ReplyError, "MGPipe: Fatal{ReplyError, \"resource_create\"} - the row answered "
"ERROR, which is not an acceptance answer; folding it into accepted or "
"refused would make a transport fault look like a resource decision");
SessionFail(MGFatalFamily::ReplyError,
"MGPipe: Fatal{ReplyError, \"resource_create\"} - the row answered "
"ERROR, which is not an acceptance answer; folding it into accepted or "
"refused would make a transport fault look like a resource decision");
}
if (status == Wire::ReplySink::kStatusDeclined) {
MG_Pipe::MGPipeNoteHandleUnpublished(head.Kind, head.Handle);
@@ -483,9 +489,8 @@ namespace MobileGL::MG_Remote::Client {
void EmitCreateBlocking(ClientSession& session, const MG_Pipe::MGPResourceDesc* payload,
MG_Pipe::MGPReplySlot* reply) {
Int32 status = Wire::ReplySink::kStatusError;
const Uint64 seq = session.EmitAndWait(MGPWireOp::ResourceCreate, payload,
sizeof(*payload), nullptr, 0, nullptr, 0,
&status);
const Uint64 seq = session.EmitAndWait(MGPWireOp::ResourceCreate, payload, sizeof(*payload), nullptr, 0,
nullptr, 0, &status);
// Cancellation has no wire ordinal; retain the caller's nonzero local ticket.
if (seq != Wire::kInvalidSeq) reply->Id = seq;
MG_Pipe::MGPipePostReply(*reply, status, status == 0 ? 1u : 0u);
@@ -498,7 +503,10 @@ namespace MobileGL::MG_Remote::Client {
// emissions, so a budget counted here fires too late. The window defers only where the
// link retains answers, and the store bounds it (docs/Performance/ipc-create-window.md).
void Wire_ResourceCreate(const MG_Pipe::MGPResourceDesc* payload, MG_Pipe::MGPReplySlot* reply) {
if (RunsAsTheServerRole()) { MG_Pipe::MGPipeMonolithScreen().ResourceCreate(payload, reply); return; }
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithScreen().ResourceCreate(payload, reply);
return;
}
ClientSession& session = RequireSession("ResourceCreate");
// THE WINDOW MAY ONLY DEFER WHERE THE LINK RETAINS ANSWERS (P12). On one that does
// not - the shared pool, whose slot is seq % slotCount and is reused - the answer the
@@ -519,14 +527,13 @@ namespace MobileGL::MG_Remote::Client {
// defers the answer and reads it later; this is the arm without one (the shared pool).
if (window <= 1 && session.RunAheadArmed() && !CreateIsSuspect(payload->Resource)) {
Int32 status = Wire::ReplySink::kStatusError;
const Uint64 seq = session.EmitAndWaitTails(
MGPWireOp::ResourceCreate, payload, sizeof(*payload), nullptr, 0, nullptr, 0,
&status, nullptr, /*wantReply=*/false, /*willReadReply=*/false);
const Uint64 seq =
session.EmitAndWaitTails(MGPWireOp::ResourceCreate, payload, sizeof(*payload), nullptr, 0, nullptr,
0, &status, nullptr, /*wantReply=*/false, /*willReadReply=*/false);
if (seq != Wire::kInvalidSeq) reply->Id = seq;
MG_Pipe::MGPipePostReply(*reply,
seq == Wire::kInvalidSeq ? Wire::ReplySink::kStatusDeclined
: Wire::ReplySink::kStatusOk,
seq == Wire::kInvalidSeq ? 0u : 1u);
MG_Pipe::MGPipePostReply(
*reply, seq == Wire::kInvalidSeq ? Wire::ReplySink::kStatusDeclined : Wire::ReplySink::kStatusOk,
seq == Wire::kInvalidSeq ? 0u : 1u);
++g_emitted;
if (seq == Wire::kInvalidSeq) ++g_declined;
return;
@@ -543,8 +550,7 @@ namespace MobileGL::MG_Remote::Client {
} else if (CreateIsSuspect(payload->Resource)) {
NoteCreateBlocked(g_blockSuspectObject);
}
if (window <= 1 || g_createSuspects.size() >= kCreateSuspectMax ||
CreateIsSuspect(payload->Resource)) {
if (window <= 1 || g_createSuspects.size() >= kCreateSuspectMax || CreateIsSuspect(payload->Resource)) {
// THE WINDOW RETIRES HERE, AND IT MAY BE HOLDING ANSWERS (P12). This branch is
// taken when the window is off, when the suspect list is full, or for a refused
// object - and in the last two cases entries already emitted are still in flight.
@@ -579,9 +585,9 @@ namespace MobileGL::MG_Remote::Client {
// not wait for its answer, and the drain above reads it later. A P12 no-reply bit
// keyed on wantReply would tell the server to skip the very answer this row comes
// back for (docs/.../p12/CREATE-WINDOW-MEASURED.md section 7).
const Uint64 seq = session.EmitAndWaitTails(
MGPWireOp::ResourceCreate, payload, sizeof(*payload), nullptr, 0, nullptr, 0,
&status, nullptr, /*wantReply=*/false, /*willReadReply=*/true);
const Uint64 seq =
session.EmitAndWaitTails(MGPWireOp::ResourceCreate, payload, sizeof(*payload), nullptr, 0, nullptr, 0,
&status, nullptr, /*wantReply=*/false, /*willReadReply=*/true);
if (seq == Wire::kInvalidSeq) {
// Cancelled: there is no ordinal to read an answer from, and the caller must NOT
// latch an object the record never described.
@@ -605,21 +611,22 @@ namespace MobileGL::MG_Remote::Client {
// waits, so a refusal reaches the caller as a refusal. EmitTextureParams pays the
// confirming create at the texture's first use (HealUnpublishedRecord).
void Wire_SetTextureParams(const MG_Pipe::MGPTextureParams* payload, MG_Pipe::MGPReplySlot* reply) {
if (RunsAsTheServerRole()) { MG_Pipe::MGPipeMonolithContext().SetTextureParams(payload, reply); return; }
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithContext().SetTextureParams(payload, reply);
return;
}
ClientSession& session = RequireSession("SetTextureParams");
const Bool confirmed = MG_Pipe::MGPipeHandleIsPublished(MG_Pipe::MGPipeKind::Texture,
payload->Res);
const Bool confirmed = MG_Pipe::MGPipeHandleIsPublished(MG_Pipe::MGPipeKind::Texture, payload->Res);
Int32 status = Wire::ReplySink::kStatusError;
Uint64 seq = Wire::kInvalidSeq;
if (confirmed) {
seq = session.EmitAndWaitTails(MGPWireOp::SetTextureParams, payload, sizeof(*payload),
nullptr, 0, nullptr, 0, &status, nullptr,
seq = session.EmitAndWaitTails(MGPWireOp::SetTextureParams, payload, sizeof(*payload), nullptr, 0,
nullptr, 0, &status, nullptr,
/*wantReply=*/false, /*willReadReply=*/false);
status = seq == Wire::kInvalidSeq ? Wire::ReplySink::kStatusDeclined
: Wire::ReplySink::kStatusOk;
status = seq == Wire::kInvalidSeq ? Wire::ReplySink::kStatusDeclined : Wire::ReplySink::kStatusOk;
} else {
seq = session.EmitAndWait(MGPWireOp::SetTextureParams, payload, sizeof(*payload),
nullptr, 0, nullptr, 0, &status);
seq = session.EmitAndWait(MGPWireOp::SetTextureParams, payload, sizeof(*payload), nullptr, 0, nullptr,
0, &status);
}
// Cancellation has no wire ordinal; retain the caller's nonzero local ticket.
if (seq != Wire::kInvalidSeq) reply->Id = seq;
@@ -628,12 +635,11 @@ namespace MobileGL::MG_Remote::Client {
if (status == 1) ++g_declined;
}
void Wire_ResourceSubData(const MG_Pipe::MGPSubData* payload, const void* blobBytes,
Uint64 blobByteCount, const void* varTail, Uint32 varTailCount,
MG_Pipe::MGPReplySlot* reply) {
void Wire_ResourceSubData(const MG_Pipe::MGPSubData* payload, const void* blobBytes, Uint64 blobByteCount,
const void* varTail, Uint32 varTailCount, MG_Pipe::MGPReplySlot* reply) {
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithContext().ResourceSubData(payload, blobBytes, blobByteCount,
varTail, varTailCount, reply);
MG_Pipe::MGPipeMonolithContext().ResourceSubData(payload, blobBytes, blobByteCount, varTail,
varTailCount, reply);
return;
}
ClientSession& session = RequireSession("ResourceSubData");
@@ -655,12 +661,10 @@ namespace MobileGL::MG_Remote::Client {
// may never take this path - R-5 has not moved.
const Bool wantReply = MG_Pipe::MGPipeSubDataWantsItsReply(record);
Int32 status = 0;
const Wire::WireTail tail{varTail, static_cast<Uint64>(varTailCount) *
sizeof(MG_Pipe::MGPSubRegion)};
const Wire::WireTail tail{varTail, static_cast<Uint64>(varTailCount) * sizeof(MG_Pipe::MGPSubRegion)};
const Uint64 seq = session.EmitAndWaitTails(
MGPWireOp::ResourceSubData, &record, sizeof(record),
varTail != nullptr ? &tail : nullptr, varTail != nullptr ? 1u : 0u, nullptr, 0,
&status, nullptr, wantReply,
MGPWireOp::ResourceSubData, &record, sizeof(record), varTail != nullptr ? &tail : nullptr,
varTail != nullptr ? 1u : 0u, nullptr, 0, &status, nullptr, wantReply,
// ONE ANSWER TO BOTH QUESTIONS, and on this row that is not a shortcut: the
// wire arm's MGPipeSubDataWantsItsReply is false for the whole texture half
// (PipeRoute.h), the caller discards the Bool either way, and the record's own
@@ -671,7 +675,8 @@ namespace MobileGL::MG_Remote::Client {
if (seq != Wire::kInvalidSeq) reply->Id = seq;
const Int32 postedStatus = (seq == Wire::kInvalidSeq || status == Wire::ReplySink::kStatusDeclined)
? Wire::ReplySink::kStatusDeclined
: wantReply ? status : Wire::ReplySink::kStatusOk;
: wantReply ? status
: Wire::ReplySink::kStatusOk;
MG_Pipe::MGPipePostReply(*reply, postedStatus, postedStatus == Wire::ReplySink::kStatusOk ? 1u : 0u);
if (seq != Wire::kInvalidSeq) ++g_emitted;
if (postedStatus == Wire::ReplySink::kStatusDeclined) ++g_declined;
@@ -680,15 +685,14 @@ namespace MobileGL::MG_Remote::Client {
void Wire_BufferSubDataResident(const MG_Pipe::MGPSubData* payload, const void* blobBytes,
Uint64 blobByteCount) {
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithContext().BufferSubDataResident(payload, blobBytes,
blobByteCount);
MG_Pipe::MGPipeMonolithContext().BufferSubDataResident(payload, blobBytes, blobByteCount);
return;
}
ClientSession& session = RequireSession("BufferSubDataResident");
MG_Pipe::MGPSubData record = *payload;
record.Blob = StageOptional(session, blobBytes, blobByteCount);
session.EmitAndWait(MGPWireOp::BufferSubDataResident, &record, sizeof(record), nullptr,
0, nullptr, 0, nullptr);
session.EmitAndWait(MGPWireOp::BufferSubDataResident, &record, sizeof(record), nullptr, 0, nullptr, 0,
nullptr);
++g_emitted;
}
@@ -701,19 +705,19 @@ namespace MobileGL::MG_Remote::Client {
// (MGPipeForEachSubDataRecordRange) lives there. What this emitter owes is the REFUSAL:
// a non-null pointer arriving here means the call site was not converted, and silently
// ignoring it would lose exactly the bytes R-13.3 promised would follow.
Bool Wire_Escape_ResourceRespecify(const MG_Pipe::MGPResourceDesc* desc,
const void* initialBytes,
Bool Wire_Escape_ResourceRespecify(const MG_Pipe::MGPResourceDesc* desc, const void* initialBytes,
const MG_Pipe::MGPRespecifiedLevel* level) {
if (RunsAsTheServerRole()) {
return MG_Pipe::MGPipeMonolithEscapes().ResourceRespecify(desc, initialBytes, level);
}
ClientSession& session = RequireSession("ResourceRespecify");
if (initialBytes != nullptr) {
SessionFail(MGFatalFamily::UncarriedInitialBytes, "MGPipe: Fatal{UncarriedInitialBytes, \"resource_respecify\"} - a call "
"site handed initial content to a split respecify. R-13.3 rules that "
"initialBytes never crosses and that the content follows as "
"resource_subdata; a caller that still passes it has bytes nothing will "
"carry");
SessionFail(MGFatalFamily::UncarriedInitialBytes,
"MGPipe: Fatal{UncarriedInitialBytes, \"resource_respecify\"} - a call "
"site handed initial content to a split respecify. R-13.3 rules that "
"initialBytes never crosses and that the content follows as "
"resource_subdata; a caller that still passes it has bytes nothing will "
"carry");
}
// Scope and exact mutable mip extent travel together through the descriptor helper.
// It temporarily reuses BufOffset/BufSize on non-buffer image targets; the server
@@ -721,8 +725,8 @@ namespace MobileGL::MG_Remote::Client {
MG_Pipe::MGPResourceDesc record = *desc;
if (level != nullptr) {
#if MOBILEGL_BUILD_DISAGGREGATED
MG_Pipe::MGPipeSetRespecifiedLevel(record, level->UploadTarget, level->Level,
level->Width, level->Height, level->Depth);
MG_Pipe::MGPipeSetRespecifiedLevel(record, level->UploadTarget, level->Level, level->Width,
level->Height, level->Depth);
#else
MG_Pipe::MGPipeSetRespecifiedLevel(record, level->UploadTarget, level->Level);
#endif
@@ -738,14 +742,13 @@ namespace MobileGL::MG_Remote::Client {
const Bool wantsReply = MG_Pipe::MGPipeResourceRespecifyWantsItsReply(record);
Int32 status = Wire::ReplySink::kStatusError;
if (!wantsReply) {
const Uint64 seq = session.EmitAndWaitTails(
MGPWireOp::ResourceRespecify, &record, sizeof(record), nullptr, 0, nullptr, 0,
&status, nullptr, /*wantReply=*/false, /*willReadReply=*/false);
status = seq == Wire::kInvalidSeq ? Wire::ReplySink::kStatusDeclined
: Wire::ReplySink::kStatusOk;
const Uint64 seq =
session.EmitAndWaitTails(MGPWireOp::ResourceRespecify, &record, sizeof(record), nullptr, 0, nullptr,
0, &status, nullptr, /*wantReply=*/false, /*willReadReply=*/false);
status = seq == Wire::kInvalidSeq ? Wire::ReplySink::kStatusDeclined : Wire::ReplySink::kStatusOk;
} else {
(void)session.EmitAndWait(MGPWireOp::ResourceRespecify, &record, sizeof(record),
nullptr, 0, nullptr, 0, &status);
(void)session.EmitAndWait(MGPWireOp::ResourceRespecify, &record, sizeof(record), nullptr, 0, nullptr, 0,
&status);
}
++g_emitted;
// P11 B2: A RESPECIFIED BUFFER'S T0 STORE IS DONE FOR THIS CLIENT. The frontend has
@@ -761,9 +764,10 @@ namespace MobileGL::MG_Remote::Client {
// - the generated acceptance rows through MGPipeTakeReplyBool, and now these two
// escapes - answers ERROR with the same named Fatal.
if (status == 2) {
SessionFail(MGFatalFamily::ReplyError, "MGPipe: Fatal{ReplyError, \"resource_respecify\"} - the row answered "
"ERROR, which is not an acceptance answer; folding it into accepted or "
"refused would make a transport fault look like a resource decision");
SessionFail(MGFatalFamily::ReplyError,
"MGPipe: Fatal{ReplyError, \"resource_respecify\"} - the row answered "
"ERROR, which is not an acceptance answer; folding it into accepted or "
"refused would make a transport fault look like a resource decision");
}
if (status == 1) ++g_declined;
return status == 0;
@@ -773,16 +777,15 @@ namespace MobileGL::MG_Remote::Client {
// {range, AccessFlags} control record and the bytes of exactly that range arrive ahead
// of it as resource_subdata. Same refusal as above, for the same reason: a blobref here
// would be "a second, forgeable way to say the same thing".
void Wire_Escape_ResourceFlushRange(const MG_Pipe::MGPFlushRange* record,
const void* bytes) {
void Wire_Escape_ResourceFlushRange(const MG_Pipe::MGPFlushRange* record, const void* bytes) {
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithEscapes().ResourceFlushRange(record, bytes);
return;
}
ClientSession& session = RequireSession("ResourceFlushRange");
(void)bytes; // ruled uncarried; the emitter in PipeFill.cpp sends the range first
session.EmitAndWait(MGPWireOp::ResourceFlushRange, record, sizeof(*record), nullptr, 0,
nullptr, 0, nullptr);
session.EmitAndWait(MGPWireOp::ResourceFlushRange, record, sizeof(*record), nullptr, 0, nullptr, 0,
nullptr);
++g_emitted;
}
@@ -791,8 +794,7 @@ namespace MobileGL::MG_Remote::Client {
// MapPersistentRoundtrips counter is defined as "one per storage definition in both
// modes" and a client that answered locally would zero it. `size` and `seedBytes` have
// no carrier (MGPHandleOnly is {Handle, Kind}) and need none: nothing is minted.
void* Wire_Escape_MapPersistent(const MG_Pipe::MGPHandleOnly* handle, Uint64 size,
const void* seedBytes) {
void* Wire_Escape_MapPersistent(const MG_Pipe::MGPHandleOnly* handle, Uint64 size, const void* seedBytes) {
if (RunsAsTheServerRole()) {
return MG_Pipe::MGPipeMonolithEscapes().MapPersistent(handle, size, seedBytes);
}
@@ -810,27 +812,28 @@ namespace MobileGL::MG_Remote::Client {
(void)size;
(void)seedBytes;
Int32 status = 0;
session.EmitAndWait(MGPWireOp::MapPersistent, handle, sizeof(*handle), nullptr, 0,
nullptr, 0, &status);
session.EmitAndWait(MGPWireOp::MapPersistent, handle, sizeof(*handle), nullptr, 0, nullptr, 0, &status);
++g_emitted;
// ERROR IS NOT A DECLINE (M4 / codex 5 / R-5), the same rule the respecify escape and
// the generated acceptance rows obey: status 2 is a transport fault, and returning
// nullptr for it would make it indistinguishable from R-6's legitimate decline.
if (status == 2) {
SessionFail(MGFatalFamily::ReplyError, "MGPipe: Fatal{ReplyError, \"map_persistent\"} - the row answered ERROR, "
"which is not an acceptance answer; a transport fault is not a resource "
"decision and may not be folded into the decline R-6 predicts");
SessionFail(MGFatalFamily::ReplyError,
"MGPipe: Fatal{ReplyError, \"map_persistent\"} - the row answered ERROR, "
"which is not an acceptance answer; a transport fault is not a resource "
"decision and may not be folded into the decline R-6 predicts");
}
if (status == 1) ++g_declined;
// NOT "always nullptr": the answer is READ. R-6 says the server declines, and the
// day it stops declining this returns what it actually said rather than what the
// ruling predicted.
if (status == 0) {
SessionFail(MGFatalFamily::UnexpectedMapAccept, "MGPipe: Fatal{UnexpectedMapAccept, \"map_persistent\"} - the server "
"accepted a persistent map under split. R-6 makes the split answer a "
"constant decline because there is no way to hand a host pointer across "
"a process boundary in P5; a pointer arriving here is one this client "
"cannot dereference");
SessionFail(MGFatalFamily::UnexpectedMapAccept,
"MGPipe: Fatal{UnexpectedMapAccept, \"map_persistent\"} - the server "
"accepted a persistent map under split. R-6 makes the split answer a "
"constant decline because there is no way to hand a host pointer across "
"a process boundary in P5; a pointer arriving here is one this client "
"cannot dereference");
}
return nullptr;
}
@@ -843,20 +846,20 @@ namespace MobileGL::MG_Remote::Client {
// Spirv[i] is Fatal on the far side rather than ignored.
void Wire_Escape_CreateShaderState(const MG_Pipe::MGPProgramDesc* desc,
const MG_State::GLState::LinkArtifacts* link,
const MG_State::GLState::SpirvArtifacts* spirv,
const Uint32* linkedStages, Uint32 linkedStageCount) {
const MG_State::GLState::SpirvArtifacts* spirv, const Uint32* linkedStages,
Uint32 linkedStageCount) {
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithEscapes().CreateShaderState(desc, link, spirv, linkedStages,
linkedStageCount);
MG_Pipe::MGPipeMonolithEscapes().CreateShaderState(desc, link, spirv, linkedStages, linkedStageCount);
return;
}
ClientSession& session = RequireSession("CreateShaderState");
if (link == nullptr || spirv == nullptr) {
SessionFail(MGFatalFamily::ArtefactsMissing, "MGPipe: Fatal{ArtefactsMissing, \"create_shader_state\"} - the record's "
"two typed companions are null. Under monolith the applier reads the "
"modules out of spirv->generatedSpirv; under split there is nothing to "
"serialise, and emitting the record anyway would create a CSO with no "
"code");
SessionFail(MGFatalFamily::ArtefactsMissing,
"MGPipe: Fatal{ArtefactsMissing, \"create_shader_state\"} - the record's "
"two typed companions are null. Under monolith the applier reads the "
"modules out of spirv->generatedSpirv; under split there is nothing to "
"serialise, and emitting the record anyway would create a CSO with no "
"code");
}
// EncodeProgramArchive APPENDS and never fails - everything it walks is owned
// plain data - so an empty archive means the two structs themselves were empty,
@@ -876,14 +879,15 @@ namespace MobileGL::MG_Remote::Client {
static_cast<Uint64>(archive.size()));
}
if (archive.empty()) {
SessionFail(MGFatalFamily::ArchiveEmpty, "MGPipe: Fatal{ArchiveEmpty, \"create_shader_state\"} - the program's "
"artefacts serialised to nothing");
SessionFail(MGFatalFamily::ArchiveEmpty,
"MGPipe: Fatal{ArchiveEmpty, \"create_shader_state\"} - the program's "
"artefacts serialised to nothing");
}
MG_Pipe::MGPProgramDesc record = *desc;
for (Uint32 i = 0; i < 6; ++i) record.Spirv[i] = MG_Pipe::MGPBlobRef{};
for (Uint32 i = 0; i < 6; ++i)
record.Spirv[i] = MG_Pipe::MGPBlobRef{};
record.Reflection = session.Encoder().StageBytes(archive.data(), archive.size());
session.EmitAndWait(MGPWireOp::CreateShaderState, &record, sizeof(record), nullptr, 0,
nullptr, 0, nullptr);
session.EmitAndWait(MGPWireOp::CreateShaderState, &record, sizeof(record), nullptr, 0, nullptr, 0, nullptr);
++g_emitted;
}
@@ -896,14 +900,12 @@ namespace MobileGL::MG_Remote::Client {
// already is. Staged rather than packed into one run with offsets, because the honesty
// pass the decoder runs is per span and a packed run would have to be re-split there
// against arithmetic nothing on the wire declares.
void Wire_Escape_SetProgramBindings(const MG_Pipe::MGPProgramBindings* hdr,
const Int32* blockBindings,
void Wire_Escape_SetProgramBindings(const MG_Pipe::MGPProgramBindings* hdr, const Int32* blockBindings,
const MG_Pipe::MGPProgramSamplerUnit* samplerUnits,
const MG_Pipe::MGPProgramStorageOverride* storageOverrides,
const char* const* storageOverrideNames) {
if (RunsAsTheServerRole()) {
MG_Pipe::MGPipeMonolithEscapes().SetProgramBindings(hdr, blockBindings, samplerUnits,
storageOverrides,
MG_Pipe::MGPipeMonolithEscapes().SetProgramBindings(hdr, blockBindings, samplerUnits, storageOverrides,
storageOverrideNames);
return;
}
@@ -938,17 +940,15 @@ namespace MobileGL::MG_Remote::Client {
const Wire::WireTail tails[3] = {
{blockBindings, static_cast<Uint64>(record.BlockBindingCount) * sizeof(Int32)},
{samplerUnits, static_cast<Uint64>(record.SamplerUnitCount) *
sizeof(MG_Pipe::MGPProgramSamplerUnit)},
{samplerUnits, static_cast<Uint64>(record.SamplerUnitCount) * sizeof(MG_Pipe::MGPProgramSamplerUnit)},
{overrides.empty() ? nullptr : overrides.data(),
static_cast<Uint64>(record.StorageOverrideCount) *
sizeof(MG_Pipe::MGPProgramStorageOverride)},
static_cast<Uint64>(record.StorageOverrideCount) * sizeof(MG_Pipe::MGPProgramStorageOverride)},
};
// ALWAYS THREE, even when a count is 0 - the decoder derives the third tail's
// offset from the first two, so declaring fewer would put the same bytes somewhere
// else (PipeWireCodec.cpp's layout arm says this from the other side).
session.EmitAndWaitTails(MGPWireOp::SetProgramBindings, &record, sizeof(record), tails, 3,
nullptr, 0, nullptr);
session.EmitAndWaitTails(MGPWireOp::SetProgramBindings, &record, sizeof(record), tails, 3, nullptr, 0,
nullptr);
++g_emitted;
}
@@ -990,13 +990,12 @@ namespace MobileGL::MG_Remote::Client {
ContextFrameEmit SendCreateContextFrame(Uint64 clientContextToken, Uint64 shareGroupToken, Uint32 flags) {
if (!ContextFramesCanCross()) return ContextFrameEmit::NoWire;
return Server::ServerCreateEGLContext(clientContextToken, shareGroupToken, flags) ? ContextFrameEmit::Sent
: ContextFrameEmit::Refused;
: ContextFrameEmit::Refused;
}
ContextFrameEmit SendDestroyContextFrame(Uint64 clientContextToken) {
if (!ContextFramesCanCross()) return ContextFrameEmit::NoWire;
return Server::ServerDestroyEGLContext(clientContextToken) ? ContextFrameEmit::Sent
: ContextFrameEmit::Refused;
return Server::ServerDestroyEGLContext(clientContextToken) ? ContextFrameEmit::Sent : ContextFrameEmit::Refused;
}
// P14 S1: bind_context. The same four windows as EmitApplierResetRecord below answer false,
@@ -1010,8 +1009,8 @@ namespace MobileGL::MG_Remote::Client {
if (g_clientTablesUninstalled.load(std::memory_order_acquire)) return false;
MG_Pipe::MGPBindContext record{};
record.ClientContextToken = clientContextToken;
const Uint64 seq = session->EmitAndWait(MGPWireOp::BindContext, &record, sizeof(record), nullptr, 0,
nullptr, 0, nullptr);
const Uint64 seq =
session->EmitAndWait(MGPWireOp::BindContext, &record, sizeof(record), nullptr, 0, nullptr, 0, nullptr);
++g_emitted;
// A DECLINED binding moved nothing on the server (a lost session, or a thread whose
// context is from an ended one), so the caller must not believe the session is bound to
@@ -1031,8 +1030,8 @@ namespace MobileGL::MG_Remote::Client {
if (g_clientTablesUninstalled.load(std::memory_order_acquire)) return false;
MG_Pipe::MGPApplierReset record{};
record.ContextSerial = g_applierResetContextSerial;
const Uint64 seq = session->EmitAndWait(MGPWireOp::ApplierReset, &record, sizeof(record), nullptr, 0,
nullptr, 0, nullptr);
const Uint64 seq =
session->EmitAndWait(MGPWireOp::ApplierReset, &record, sizeof(record), nullptr, 0, nullptr, 0, nullptr);
// THE SERIAL COUNTS RESETS THE SERVER WAS SENT, not resets this client primed: a declined
// record (a lost session, a thread on an ended session's context) never reached the
// server's count, and moving ours past it would latch the next session that hears one.
@@ -1059,8 +1058,7 @@ namespace MobileGL::MG_Remote::Client {
// can never disagree about who supplies the eight fields.
Bool ContextValuesWireLive() {
ClientSession* session = ClientSession::Active();
return session != nullptr && session->Started() &&
!g_clientTablesUninstalled.load(std::memory_order_acquire);
return session != nullptr && session->Started() && !g_clientTablesUninstalled.load(std::memory_order_acquire);
}
ObjectDeathEmit EmitObjectDeathRecord(MG_Pipe::MGPipeKind kind, Uint64 lifetimeId) {
@@ -1098,29 +1096,29 @@ namespace MobileGL::MG_Remote::Client {
// The WAIT is the only property the blocking mailbox hop provided and the only one P5c
// keeps (§5.2 step 3): it orders the death against in-flight verbs that name the
// handle, so the server's twin cannot be released underneath a verb that is using it.
session->EmitAndWait(MGPWireOp::ObjectDeath, &record, sizeof(record), nullptr, 0, nullptr,
0, nullptr);
session->EmitAndWait(MGPWireOp::ObjectDeath, &record, sizeof(record), nullptr, 0, nullptr, 0, nullptr);
++g_emitted;
return ObjectDeathEmit::Emitted;
}
void RequireClientTablesInstalled(const char* row) {
if (g_clientTablesUninstalled.load(std::memory_order_acquire)) {
SessionFail(MGFatalFamily::ClientTablesUninstalled, "MGPipe: Fatal{ClientTablesUninstalled, \"%s\"} - the client tables are "
"being torn down; refusing before session or ring access", row);
SessionFail(MGFatalFamily::ClientTablesUninstalled,
"MGPipe: Fatal{ClientTablesUninstalled, \"%s\"} - the client tables are "
"being torn down; refusing before session or ring access",
row);
}
}
namespace {
void WireStateObjectDestroyed(MG_Pipe::MGPipeKind kind, Uint64 lifetimeId,
MG_Pipe::MGPipeHandle /*handle*/) {
void WireStateObjectDestroyed(MG_Pipe::MGPipeKind kind, Uint64 lifetimeId, MG_Pipe::MGPipeHandle /*handle*/) {
(void)EmitObjectDeathRecord(kind, lifetimeId);
}
const MG_State::GLState::StateObjectDeathOps kClientStateObjectDeathOps = {
.OnDestroyed = &WireStateObjectDestroyed,
};
}
} // namespace
void InstallClientWireTables() {
using namespace MG_Pipe;
@@ -1218,8 +1216,12 @@ namespace MobileGL::MG_Remote::Client {
g_clientTablesUninstalled.store(false, std::memory_order_release);
}
Uint64 ClientWireRecordsEmitted() { return g_emitted; }
Uint64 ClientWireRecordsDeclined() { return g_declined; }
Uint64 ClientWireRecordsEmitted() {
return g_emitted;
}
Uint64 ClientWireRecordsDeclined() {
return g_declined;
}
// ---- P12: the CREATE WINDOW, as numbers a test can read ---------------------------------
//
@@ -1230,21 +1232,33 @@ namespace MobileGL::MG_Remote::Client {
// ceiling - it would REPLACE the oldest confirmation and leave the count where it was. Read it
// against the effective ceiling and the array size, which are the two numbers the guard itself
// is written in terms of, and not against a literal repeated in the test.
Uint32 ClientCreateWindowPending() { return g_createWindowCount; }
Uint32 ClientCreateWindowPending() {
return g_createWindowCount;
}
// Taken + Pending is every create the window ever accepted: the answers it has read back and
// the ones still outstanding. A create the window accepted and then LOST would break that
// sum, which is the failure a provisional accept makes possible and a count cannot show.
Uint64 ClientCreateWindowTaken() { return g_createWindowTaken; }
Uint64 ClientCreateWindowTaken() {
return g_createWindowTaken;
}
// P12: RetainedOutstanding / SkippedUnwanted / UnansweredWanted live on the LINK, where the
// store is (StreamLink::RetainedOutstanding and friends). They are what says whether the
// declared-answer store is bounded by the CLIENT (SkippedUnwanted grows, outstanding does
// not) rather than by the server's answer rate - which is the property the capacity rests on.
Uint32 ClientCreateWindowEffective() { return CreateWindowEffective(); }
Uint32 ClientCreateWindowArraySize() { return kCreateWindowMax; }
Uint32 ClientCreateWindowEffective() {
return CreateWindowEffective();
}
Uint32 ClientCreateWindowArraySize() {
return kCreateWindowMax;
}
// Objects whose create came back DECLINED: out of the window, next create blocking, and back
// in the moment a blocking create lands.
Uint32 ClientCreateWindowSuspects() { return static_cast<Uint32>(g_createSuspects.size()); }
Uint64 ClientCreateWindowRefusals() { return g_createRefusals; }
Uint32 ClientCreateWindowSuspects() {
return static_cast<Uint32>(g_createSuspects.size());
}
Uint64 ClientCreateWindowRefusals() {
return g_createRefusals;
}
} // namespace MobileGL::MG_Remote::Client
+10 -2
View File
@@ -121,7 +121,11 @@ namespace MobileGL::MG_Remote::Client {
// (before ClientSession::Start, or on the server role's own thread) means keep the context on
// the client's books alone. Refused is a server answer: eglCreateContext then returns
// EGL_NO_CONTEXT, because the server would refuse that context's first bind_context.
enum class ContextFrameEmit : Uint8 { Sent, NoWire, Refused };
enum class ContextFrameEmit : Uint8 {
Sent,
NoWire,
Refused
};
ContextFrameEmit SendCreateContextFrame(Uint64 clientContextToken, Uint64 shareGroupToken, Uint32 flags);
// A destroy has the same three answers; the client's EGL book is dropped either way (that is
// the caller's own state machine), and what a refusal changes is only what the server holds.
@@ -153,7 +157,11 @@ namespace MobileGL::MG_Remote::Client {
// third is NOT folded into the second: a server role with no wire at all - a ServerLoop
// fixture's shape, never a real split's - still has a server loop the caller delivers to,
// where a handle that never existed has nothing to deliver.
enum class ObjectDeathEmit : Uint8 { Emitted, NoHandle, NoSession };
enum class ObjectDeathEmit : Uint8 {
Emitted,
NoHandle,
NoSession
};
ObjectDeathEmit EmitObjectDeathRecord(MG_Pipe::MGPipeKind kind, Uint64 lifetimeId);
} // namespace MobileGL::MG_Remote::Client
+3 -2
View File
@@ -31,8 +31,9 @@ namespace MobileGL::MG_Remote {
// that exists has a row, and a family that does not exist is not a value of the enum.
inline ::MobileGL::Wire::FatalCode FatalCodeForFamily(MGFatalFamily family) {
switch (family) {
#define X(Family, WireCode, Why) \
case MGFatalFamily::Family: return ::MobileGL::Wire::FatalCode::WireCode;
#define X(Family, WireCode, Why) \
case MGFatalFamily::Family: \
return ::MobileGL::Wire::FatalCode::WireCode;
MGL_FATAL_FAMILY_LIST(X)
#undef X
}
+55 -48
View File
@@ -8,7 +8,7 @@
#include <MG_Remote/FatalFunnel.h>
#include "../Includes.h" // MGLOG_F and the umbrella; a .cpp may pull it, a Transport/ header may not
#include "../Includes.h" // MGLOG_F and the umbrella; a .cpp may pull it, a Transport/ header may not
#include <MG_Pipe/PipeSessionFail.h> // the seam MG_Backend's renderer dies through
#include "Client/ClientSession.h"
#include "Protocol/generated/protocol_generated.h"
@@ -67,8 +67,7 @@ namespace MobileGL::MG_Remote {
return MGFatalFamily::ProtocolCorruption;
}
[[noreturn]] void PipeSessionFailAdapter(MG_Pipe::MGPipeFatalFamily family,
const char* line) {
[[noreturn]] void PipeSessionFailAdapter(MG_Pipe::MGPipeFatalFamily family, const char* line) {
SessionFail(MapPipeFamily(family), "%s", line);
}
@@ -94,8 +93,7 @@ namespace MobileGL::MG_Remote {
void FormatFaultLine(char (&line)[512], const char* fmt, va_list args) {
const int written = std::vsnprintf(line, sizeof(line), fmt, args);
if (written < 0) {
std::snprintf(line, sizeof(line),
"MG_Remote: unformattable Fatal diagnostic (format=%s)", fmt);
std::snprintf(line, sizeof(line), "MG_Remote: unformattable Fatal diagnostic (format=%s)", fmt);
}
}
@@ -138,7 +136,9 @@ namespace MobileGL::MG_Remote {
std::unordered_map<int, LatchState*> g_domains;
int g_nextDomain = 1;
LatchState& Latch() { return *t_latch; }
LatchState& Latch() {
return *t_latch;
}
} // namespace
int FatalLatchDomainOpen() {
@@ -177,7 +177,9 @@ namespace MobileGL::MG_Remote {
return previous->id;
}
void ArmSessionLatch() { Latch().armed.store(true, std::memory_order_release); }
void ArmSessionLatch() {
Latch().armed.store(true, std::memory_order_release);
}
void ResetSessionLatch() {
LatchState& latch = Latch();
@@ -189,9 +191,13 @@ namespace MobileGL::MG_Remote {
latch.latched.store(false, std::memory_order_release);
}
bool SessionLatchArmed() { return Latch().armed.load(std::memory_order_acquire); }
bool SessionLatchArmed() {
return Latch().armed.load(std::memory_order_acquire);
}
bool SessionLatched() { return Latch().latched.load(std::memory_order_acquire); }
bool SessionLatched() {
return Latch().latched.load(std::memory_order_acquire);
}
MGFatalFamily SessionLatchedFamily() {
LatchState& latch = Latch();
@@ -199,9 +205,13 @@ namespace MobileGL::MG_Remote {
return latch.latchedFamily;
}
const char* SessionLatchedLine() { return Latch().latchedLine; }
const char* SessionLatchedLine() {
return Latch().latchedLine;
}
std::uint64_t SessionLatchCount() { return Latch().count.load(std::memory_order_relaxed); }
std::uint64_t SessionLatchCount() {
return Latch().count.load(std::memory_order_relaxed);
}
bool SessionLatch(MGFatalFamily family, const char* fmt, ...) {
char line[512];
@@ -249,8 +259,7 @@ namespace MobileGL::MG_Remote {
FatalFamilyName(family), kSessionLatchedExitCode);
// Before the peer hears of it: what the observer records must already hold when the client
// reacts (a lost compositor re-creates its window surface on a fresh session at once).
if (const SessionLatchObserver observer = g_sessionLatchObserver.load(std::memory_order_acquire))
observer();
if (const SessionLatchObserver observer = g_sessionLatchObserver.load(std::memory_order_acquire)) observer();
PublishSessionFault(family, line);
return false;
}
@@ -270,41 +279,40 @@ namespace MobileGL::MG_Remote {
}
namespace {
void PublishSessionFault(MGFatalFamily family, const char* line) {
// A SessionFault frame to the peer, BEFORE the abort, so the other side's log names the
// family instead of reading a bare EOF (5.2). Best-effort by nature: this thread is about
// to die, so a send that blocks or fails changes nothing it could have changed anyway.
//
// WHICHEVER ROLE THIS PROCESS IS. Under spawn only one session is active per process -
// the client has ClientSession, the server has ServerSession - so publishing on whatever
// control plane exists reaches the far side. Under inproc both exist and the frame goes
// to an in-process transport the aborting process will never read; harmless, and not
// worth a special case that would then be the untested one.
//
// `code` is the coarse seven-value projection; `family` carries the full word; `message`
// is the same line just logged. seq/op are not threaded through the 90 call sites and the
// message already names the op where it matters, so the frame carries what it can name
// honestly rather than a field that is always zero.
Transport::ITransport* control = nullptr;
if (Client::ClientSession* client = Client::ClientSession::Active()) {
control = client->Control_Plane();
}
if (control == nullptr) {
if (Server::ServerSession* server = Server::ServerSession::Active()) {
control = server->Control_Plane();
void PublishSessionFault(MGFatalFamily family, const char* line) {
// A SessionFault frame to the peer, BEFORE the abort, so the other side's log names the
// family instead of reading a bare EOF (5.2). Best-effort by nature: this thread is about
// to die, so a send that blocks or fails changes nothing it could have changed anyway.
//
// WHICHEVER ROLE THIS PROCESS IS. Under spawn only one session is active per process -
// the client has ClientSession, the server has ServerSession - so publishing on whatever
// control plane exists reaches the far side. Under inproc both exist and the frame goes
// to an in-process transport the aborting process will never read; harmless, and not
// worth a special case that would then be the untested one.
//
// `code` is the coarse seven-value projection; `family` carries the full word; `message`
// is the same line just logged. seq/op are not threaded through the 90 call sites and the
// message already names the op where it matters, so the frame carries what it can name
// honestly rather than a field that is always zero.
Transport::ITransport* control = nullptr;
if (Client::ClientSession* client = Client::ClientSession::Active()) {
control = client->Control_Plane();
}
if (control == nullptr) {
if (Server::ServerSession* server = Server::ServerSession::Active()) {
control = server->Control_Plane();
}
}
if (control != nullptr) {
::flatbuffers::FlatBufferBuilder builder(256);
const auto fatal = ::MobileGL::Wire::CreateFatalDirect(builder, FatalCodeForFamily(family), line,
FatalFamilyName(family));
const auto root =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::Fatal, fatal.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, root);
(void)control->SendFrame(MobileGLByteSpan{builder.GetBufferPointer(), builder.GetSize()});
}
}
if (control != nullptr) {
::flatbuffers::FlatBufferBuilder builder(256);
const auto fatal = ::MobileGL::Wire::CreateFatalDirect(
builder, FatalCodeForFamily(family), line, FatalFamilyName(family));
const auto root = ::MobileGL::Wire::CreateCtrlEnvelope(
builder, ::MobileGL::Wire::CtrlMsg::Fatal, fatal.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, root);
(void)control->SendFrame(
MobileGLByteSpan{builder.GetBufferPointer(), builder.GetSize()});
}
}
} // namespace
// P13 W5: THE RECORD ARM'S DEATHS GO THROUGH THIS FUNNEL WHEREVER MG_Remote IS LINKED.
@@ -320,8 +328,7 @@ namespace MobileGL::MG_Remote {
return SessionLatch(family, "%s", line);
}
[[maybe_unused]] const bool g_recordFailHooksRegistered = [] {
MG_Pipe::MGPipeInstallRecordFailHooks(&RecordFailThroughFunnel, &RecordLatchThroughFunnel,
&SessionLatched);
MG_Pipe::MGPipeInstallRecordFailHooks(&RecordFailThroughFunnel, &RecordLatchThroughFunnel, &SessionLatched);
return true;
}();
} // namespace
+22 -26
View File
@@ -20,18 +20,16 @@
#include <vector>
namespace MobileGL::MG_Remote {
inline MobileGLResult RefuseHandshake(Transport::ITransport& transport,
::MobileGL::Wire::RefuseCode code,
const char* detail, Uint64 expected = 0,
Uint64 actual = 0, const char* peerValue = nullptr) {
MGLOG_E("MGPipe: Refuse{%s} %s expected=%llu actual=%llu peer=%s",
::MobileGL::Wire::EnumNameRefuseCode(code), detail,
static_cast<unsigned long long>(expected), static_cast<unsigned long long>(actual),
inline MobileGLResult RefuseHandshake(Transport::ITransport& transport, ::MobileGL::Wire::RefuseCode code,
const char* detail, Uint64 expected = 0, Uint64 actual = 0,
const char* peerValue = nullptr) {
MGLOG_E("MGPipe: Refuse{%s} %s expected=%llu actual=%llu peer=%s", ::MobileGL::Wire::EnumNameRefuseCode(code),
detail, static_cast<unsigned long long>(expected), static_cast<unsigned long long>(actual),
peerValue == nullptr ? "" : peerValue);
::flatbuffers::FlatBufferBuilder builder(256);
auto refusal = ::MobileGL::Wire::CreateRefuseDirect(builder, code, detail, expected, actual, peerValue);
auto envelope = ::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::Refuse,
refusal.Union());
auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::Refuse, refusal.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
transport.SendFrame({builder.GetBufferPointer(), builder.GetSize()});
return MOBILEGL_ERR_PROTOCOL_MISMATCH;
@@ -65,32 +63,31 @@ namespace MobileGL::MG_Remote {
const char* bytes = presented == nullptr ? "" : presented->c_str();
const std::size_t size = presented == nullptr ? 0u : presented->size();
if (Transport::ConstantTimeTokenMatch(expected, bytes, size)) return MOBILEGL_OK;
return RefuseHandshake(transport, ::MobileGL::Wire::RefuseCode::Authentication,
"token mismatch");
return RefuseHandshake(transport, ::MobileGL::Wire::RefuseCode::Authentication, "token mismatch");
}
inline MobileGLResult ValidatePeerHandshake(Transport::ITransport& transport,
Uint32 major, Uint32 minor, Uint64 fingerprint,
const char* stamp, ::MobileGL::Wire::DialMode dial) {
inline MobileGLResult ValidatePeerHandshake(Transport::ITransport& transport, Uint32 major, Uint32 minor,
Uint64 fingerprint, const char* stamp,
::MobileGL::Wire::DialMode dial) {
using ::MobileGL::Wire::RefuseCode;
if (major != MOBILEGL_PROTOCOL_ABI_MAJOR || minor != MOBILEGL_PROTOCOL_ABI_MINOR)
return RefuseHandshake(transport, RefuseCode::ProtocolVersion, "protocol version",
MOBILEGL_ABI_VERSION(MOBILEGL_PROTOCOL_ABI_MAJOR, MOBILEGL_PROTOCOL_ABI_MINOR),
MOBILEGL_ABI_VERSION(major, minor));
MOBILEGL_ABI_VERSION(MOBILEGL_PROTOCOL_ABI_MAJOR, MOBILEGL_PROTOCOL_ABI_MINOR),
MOBILEGL_ABI_VERSION(major, minor));
if (fingerprint != WireFingerprint())
return RefuseHandshake(transport, RefuseCode::WireFingerprint, "wire layout",
WireFingerprint(), fingerprint);
return RefuseHandshake(transport, RefuseCode::WireFingerprint, "wire layout", WireFingerprint(),
fingerprint);
const bool sameBuild = BuildFingerprintPresent() && stamp != nullptr && stamp[0] != '\0' &&
std::strcmp(stamp, BuildFingerprint()) == 0;
const char* required = std::getenv("MOBILEGL_IPC_REQUIRE_SAME_BUILD");
const bool requireSame = dial == ::MobileGL::Wire::DialMode::Fork ||
(required != nullptr && std::strcmp(required, "1") == 0);
const bool requireSame =
dial == ::MobileGL::Wire::DialMode::Fork || (required != nullptr && std::strcmp(required, "1") == 0);
if (!sameBuild && requireSame)
return RefuseHandshake(transport, RefuseCode::BuildFingerprint, "build identity", 0, 0,
stamp == nullptr ? "<missing>" : stamp);
if (!sameBuild && dial == ::MobileGL::Wire::DialMode::Connect)
MGLOG_W("MGPipe: compatible wire with different build: local=%s peer=%s",
BuildFingerprint(), stamp == nullptr ? "<missing>" : stamp);
MGLOG_W("MGPipe: compatible wire with different build: local=%s peer=%s", BuildFingerprint(),
stamp == nullptr ? "<missing>" : stamp);
return MOBILEGL_OK;
}
@@ -103,11 +100,10 @@ namespace MobileGL::MG_Remote {
inline std::vector<std::uint8_t> EncodeDataBind(const std::uint8_t* nonce, std::size_t size) {
::flatbuffers::FlatBufferBuilder builder(64);
auto bind = ::MobileGL::Wire::CreateDataBind(builder, builder.CreateVector(nonce, size));
auto envelope = ::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::DataBind,
bind.Union());
auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::DataBind, bind.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
return std::vector<std::uint8_t>(builder.GetBufferPointer(),
builder.GetBufferPointer() + builder.GetSize());
return std::vector<std::uint8_t>(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
}
// True when `frame` is a verifiable CtrlEnvelope carrying a DataBind whose nonce is exactly
+97 -66
View File
@@ -28,37 +28,37 @@ namespace MobileGL::MG_Remote {
// renumbers either side fails HERE, at compile time, instead of silently mistranslating
// op identities between two processes that both still announce the same ABI major.
static_assert(static_cast<Uint8>(SurfaceControlOp::InitializeDisplay) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::InitializeDisplay));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::InitializeDisplay));
static_assert(static_cast<Uint8>(SurfaceControlOp::CreateWindowSurface) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::CreateWindowSurface));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::CreateWindowSurface));
static_assert(static_cast<Uint8>(SurfaceControlOp::CreatePbufferSurface) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::CreatePbufferSurface));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::CreatePbufferSurface));
static_assert(static_cast<Uint8>(SurfaceControlOp::ResizeWindowSurface) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ResizeWindowSurface));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ResizeWindowSurface));
static_assert(static_cast<Uint8>(SurfaceControlOp::ReleaseSurface) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ReleaseSurface));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ReleaseSurface));
static_assert(static_cast<Uint8>(SurfaceControlOp::MakeCurrent) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::MakeCurrent));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::MakeCurrent));
static_assert(static_cast<Uint8>(SurfaceControlOp::ReleaseCurrent) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ReleaseCurrent));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ReleaseCurrent));
// fc's three schema additions, append-only (protocol.fbs's own rule).
static_assert(static_cast<Uint8>(SurfaceControlOp::SetSwapInterval) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::SetSwapInterval));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::SetSwapInterval));
static_assert(static_cast<Uint8>(SurfaceControlOp::ReleaseResources) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ReleaseResources));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ReleaseResources));
static_assert(static_cast<Uint8>(SurfaceControlOp::SetWindowHandle) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::SetWindowHandle));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::SetWindowHandle));
// cp's one schema addition, on the same append-only terms.
static_assert(static_cast<Uint8>(SurfaceControlOp::InitCapabilities) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::InitCapabilities));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::InitCapabilities));
// P14 S1's two appends, on the same append-only terms. The VALUES are pinned because they
// are wire ABI, and pinned SEPARATELY from the identity cast beside them: the cast would
// stay green if both enums moved together to 20/21, and a peer built before the move
// would then read the two ops as two others.
static_assert(static_cast<Uint8>(SurfaceControlOp::CreateContext) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::CreateContext));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::CreateContext));
static_assert(static_cast<Uint8>(SurfaceControlOp::DestroyContext) ==
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::DestroyContext));
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::DestroyContext));
static_assert(static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::SetSwapInterval) == 8 &&
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::ReleaseResources) == 9 &&
static_cast<Uint8>(::MobileGL::Wire::SurfaceOpKind::SetWindowHandle) == 10 &&
@@ -95,15 +95,24 @@ namespace MobileGL::MG_Remote {
const char* SurfaceWireErrorName(SurfaceWireError error) {
switch (error) {
case SurfaceWireError::None: return "None";
case SurfaceWireError::UnknownOpKind: return "UnknownOpKind";
case SurfaceWireError::InprocOnlyOpOnTheWire: return "InprocOnlyOpOnTheWire";
case SurfaceWireError::WindowKindNamesNoBackend: return "WindowKindNamesNoBackend";
case SurfaceWireError::UnknownWindowKind: return "UnknownWindowKind";
case SurfaceWireError::MetalLayerArrived: return "MetalLayerArrived";
case SurfaceWireError::AndroidNativeWindowArrived: return "AndroidNativeWindowArrived";
case SurfaceWireError::ServerOwnedTokenNotZero: return "ServerOwnedTokenNotZero";
case SurfaceWireError::ServerOwnedOnSetWindowHandle: return "ServerOwnedOnSetWindowHandle";
case SurfaceWireError::None:
return "None";
case SurfaceWireError::UnknownOpKind:
return "UnknownOpKind";
case SurfaceWireError::InprocOnlyOpOnTheWire:
return "InprocOnlyOpOnTheWire";
case SurfaceWireError::WindowKindNamesNoBackend:
return "WindowKindNamesNoBackend";
case SurfaceWireError::UnknownWindowKind:
return "UnknownWindowKind";
case SurfaceWireError::MetalLayerArrived:
return "MetalLayerArrived";
case SurfaceWireError::AndroidNativeWindowArrived:
return "AndroidNativeWindowArrived";
case SurfaceWireError::ServerOwnedTokenNotZero:
return "ServerOwnedTokenNotZero";
case SurfaceWireError::ServerOwnedOnSetWindowHandle:
return "ServerOwnedOnSetWindowHandle";
}
return "<unknown SurfaceWireError>";
}
@@ -133,29 +142,50 @@ namespace MobileGL::MG_Remote {
bool WireWindowKindForWindowBackend(MG_Backend::WindowBackend backend, ::MobileGL::Wire::WindowKind* out) {
switch (backend) {
case MG_Backend::WindowBackend::Android: *out = ::MobileGL::Wire::WindowKind::AndroidNativeWindow; return true;
case MG_Backend::WindowBackend::X11: *out = ::MobileGL::Wire::WindowKind::X11; return true;
case MG_Backend::WindowBackend::MetalLayer: *out = ::MobileGL::Wire::WindowKind::MetalLayer; return true;
case MG_Backend::WindowBackend::Win32: *out = ::MobileGL::Wire::WindowKind::Win32Hwnd; return true;
case MG_Backend::WindowBackend::Unknown: *out = ::MobileGL::Wire::WindowKind::None; return true;
default: return false;
case MG_Backend::WindowBackend::Android:
*out = ::MobileGL::Wire::WindowKind::AndroidNativeWindow;
return true;
case MG_Backend::WindowBackend::X11:
*out = ::MobileGL::Wire::WindowKind::X11;
return true;
case MG_Backend::WindowBackend::MetalLayer:
*out = ::MobileGL::Wire::WindowKind::MetalLayer;
return true;
case MG_Backend::WindowBackend::Win32:
*out = ::MobileGL::Wire::WindowKind::Win32Hwnd;
return true;
case MG_Backend::WindowBackend::Unknown:
*out = ::MobileGL::Wire::WindowKind::None;
return true;
default:
return false;
}
}
bool WindowBackendForWireWindowKind(::MobileGL::Wire::WindowKind kind, MG_Backend::WindowBackend* out) {
switch (kind) {
case ::MobileGL::Wire::WindowKind::AndroidNativeWindow: *out = MG_Backend::WindowBackend::Android; return true;
case ::MobileGL::Wire::WindowKind::X11: *out = MG_Backend::WindowBackend::X11; return true;
case ::MobileGL::Wire::WindowKind::MetalLayer: *out = MG_Backend::WindowBackend::MetalLayer; return true;
case ::MobileGL::Wire::WindowKind::Win32Hwnd: *out = MG_Backend::WindowBackend::Win32; return true;
case ::MobileGL::Wire::WindowKind::None: *out = MG_Backend::WindowBackend::Unknown; return true;
case ::MobileGL::Wire::WindowKind::AndroidNativeWindow:
*out = MG_Backend::WindowBackend::Android;
return true;
case ::MobileGL::Wire::WindowKind::X11:
*out = MG_Backend::WindowBackend::X11;
return true;
case ::MobileGL::Wire::WindowKind::MetalLayer:
*out = MG_Backend::WindowBackend::MetalLayer;
return true;
case ::MobileGL::Wire::WindowKind::Win32Hwnd:
*out = MG_Backend::WindowBackend::Win32;
return true;
case ::MobileGL::Wire::WindowKind::None:
*out = MG_Backend::WindowBackend::Unknown;
return true;
// Surfaceless and Pbuffer are surface SHAPES, not window backends: no answer.
default: return false;
default:
return false;
}
}
SurfaceWireError EncodeSurfaceOpFrame(const SurfaceControlFrame& frame,
flatbuffers::FlatBufferBuilder* builder) {
SurfaceWireError EncodeSurfaceOpFrame(const SurfaceControlFrame& frame, flatbuffers::FlatBufferBuilder* builder) {
::MobileGL::Wire::SurfaceOpKind kind;
if (!WireKindForSurfaceControlOp(frame.kind, &kind)) {
return frame.kind == SurfaceControlOp::None ? SurfaceWireError::UnknownOpKind
@@ -177,16 +207,16 @@ namespace MobileGL::MG_Remote {
}
windowKind = ::MobileGL::Wire::WindowKind::ServerOwned;
nativeToken = 0;
} else if (!WireWindowKindForWindowBackend(
static_cast<MG_Backend::WindowBackend>(frame.windowBackend), &windowKind)) {
} else if (!WireWindowKindForWindowBackend(static_cast<MG_Backend::WindowBackend>(frame.windowBackend),
&windowKind)) {
return SurfaceWireError::UnknownWindowKind;
}
}
const auto op = ::MobileGL::Wire::CreateSurfaceOp(
*builder, frame.seq, kind, frame.display, frame.surface, windowKind, nativeToken,
frame.width, frame.height, frame.swapInterval, frame.readSurface, frame.context,
frame.shareGroupToken, frame.contextFlags);
const auto envelope = ::MobileGL::Wire::CreateCtrlEnvelope(*builder, ::MobileGL::Wire::CtrlMsg::SurfaceOp, op.Union());
*builder, frame.seq, kind, frame.display, frame.surface, windowKind, nativeToken, frame.width, frame.height,
frame.swapInterval, frame.readSurface, frame.context, frame.shareGroupToken, frame.contextFlags);
const auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(*builder, ::MobileGL::Wire::CtrlMsg::SurfaceOp, op.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(*builder, envelope);
return SurfaceWireError::None;
}
@@ -253,8 +283,7 @@ namespace MobileGL::MG_Remote {
return SurfaceWireError::None;
}
void EncodeSurfaceReplyFrame(const SurfaceControlFrame& frame,
flatbuffers::FlatBufferBuilder* builder) {
void EncodeSurfaceReplyFrame(const SurfaceControlFrame& frame, flatbuffers::FlatBufferBuilder* builder) {
// P12: the reply's surface geometry and refusal (SurfaceReply.width/height/refusal).
const auto reply = ::MobileGL::Wire::CreateSurfaceReply(
*builder, frame.seq, frame.ok, frame.eglMajor, frame.eglMinor, /*defaultFb=*/0, frame.eventHead,
@@ -308,33 +337,35 @@ namespace MobileGL::MG_Remote {
// definition. A non-zero one is a value from the peer's window system trying to
// cross (Rule G) - the peer's corrupt bytes, latched like the rows beside it.
(void)SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"SurfaceOp.nativeToken\"} - a wire %s named "
"WindowKind::ServerOwned with native token 0x%llx. The server-owned window is the "
"server's own and its token is always 0: no value from the client's window system "
"crosses (Rule G)",
::MobileGL::Wire::EnumNameSurfaceOpKind(op.kind()),
static_cast<unsigned long long>(op.nativeToken()));
"MGPipe: Fatal{ProtocolCorruption, \"SurfaceOp.nativeToken\"} - a wire %s named "
"WindowKind::ServerOwned with native token 0x%llx. The server-owned window is the "
"server's own and its token is always 0: no value from the client's window system "
"crosses (Rule G)",
::MobileGL::Wire::EnumNameSurfaceOpKind(op.kind()),
static_cast<unsigned long long>(op.nativeToken()));
} else if (error == SurfaceWireError::AndroidNativeWindowArrived) {
(void)SessionLatch(MGFatalFamily::UnmigratedSurface,
"MGPipe: Fatal{UnmigratedSurface, \"AndroidNativeWindow:use-MOBILEGL_IPC_SURFACE=server\"} - a wire "
"SurfaceOp (%s) named an ANativeWindow*, which is a pointer into the "
"CLIENT's process and means nothing here. A client-process window "
"handle can never cross processes: a windowed out-of-process client must "
"use MOBILEGL_IPC_SURFACE=server (the server owns the window); "
"otherwise it is pbuffer/surfaceless only",
::MobileGL::Wire::EnumNameSurfaceOpKind(op.kind()));
"MGPipe: Fatal{UnmigratedSurface, "
"\"AndroidNativeWindow:use-MOBILEGL_IPC_SURFACE=server\"} - a wire "
"SurfaceOp (%s) named an ANativeWindow*, which is a pointer into the "
"CLIENT's process and means nothing here. A client-process window "
"handle can never cross processes: a windowed out-of-process client must "
"use MOBILEGL_IPC_SURFACE=server (the server owns the window); "
"otherwise it is pbuffer/surfaceless only",
::MobileGL::Wire::EnumNameSurfaceOpKind(op.kind()));
} else if (error == SurfaceWireError::MetalLayerArrived) {
(void)SessionLatch(MGFatalFamily::UnmigratedSurface,
"MGPipe: Fatal{UnmigratedSurface, \"MetalLayer:use-MOBILEGL_IPC_SURFACE=server\"} - a wire "
"SurfaceOp named a CAMetalLayer* in the CLIENT's process. "
"A client-process window handle can never cross processes: use "
"MOBILEGL_IPC_SURFACE=server (the server owns the window)");
(void)SessionLatch(
MGFatalFamily::UnmigratedSurface,
"MGPipe: Fatal{UnmigratedSurface, \"MetalLayer:use-MOBILEGL_IPC_SURFACE=server\"} - a wire "
"SurfaceOp named a CAMetalLayer* in the CLIENT's process. "
"A client-process window handle can never cross processes: use "
"MOBILEGL_IPC_SURFACE=server (the server owns the window)");
} else {
(void)SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"SurfaceOp\"} - a wire surface op "
"failed validation: %s (wire kind %u, window kind %u)",
SurfaceWireErrorName(error), static_cast<unsigned>(op.kind()),
static_cast<unsigned>(op.windowKind()));
"MGPipe: Fatal{ProtocolCorruption, \"SurfaceOp\"} - a wire surface op "
"failed validation: %s (wire kind %u, window kind %u)",
SurfaceWireErrorName(error), static_cast<unsigned>(op.kind()),
static_cast<unsigned>(op.windowKind()));
}
if (replyOut != nullptr) {
*replyOut = SurfaceControlFrame{};
+8 -8
View File
@@ -34,12 +34,13 @@ namespace MobileGL::MG_Remote {
enum class SurfaceWireError {
None = 0,
UnknownOpKind, // no SurfaceControlOp for the wire kind (or None on the wire)
InprocOnlyOpOnTheWire, // a kind that may ride the inproc channel but never the wire
WindowKindNamesNoBackend,// Surfaceless/Pbuffer where a window backend is required
UnknownWindowKind, // a WindowKind the mapping table does not know
MetalLayerArrived, // -> Fatal{UnmigratedSurface, "MetalLayer:use-MOBILEGL_IPC_SURFACE=server"}
AndroidNativeWindowArrived, // -> Fatal{UnmigratedSurface, "AndroidNativeWindow:use-MOBILEGL_IPC_SURFACE=server"}
UnknownOpKind, // no SurfaceControlOp for the wire kind (or None on the wire)
InprocOnlyOpOnTheWire, // a kind that may ride the inproc channel but never the wire
WindowKindNamesNoBackend, // Surfaceless/Pbuffer where a window backend is required
UnknownWindowKind, // a WindowKind the mapping table does not know
MetalLayerArrived, // -> Fatal{UnmigratedSurface, "MetalLayer:use-MOBILEGL_IPC_SURFACE=server"}
AndroidNativeWindowArrived, // -> Fatal{UnmigratedSurface,
// "AndroidNativeWindow:use-MOBILEGL_IPC_SURFACE=server"}
// P12 (on-screen server window), D2. WindowKind::ServerOwned is legal on
// CreateWindowSurface only, and only with nativeToken 0:
ServerOwnedTokenNotZero, // -> Fatal{ProtocolCorruption, "SurfaceOp.nativeToken"}, latched
@@ -70,8 +71,7 @@ namespace MobileGL::MG_Remote {
SurfaceWireError DecodeWireSurfaceOp(const ::MobileGL::Wire::SurfaceOp& op, Server::SurfaceControlFrame* frame);
// The reply direction, same shape (CtrlEnvelope{SurfaceReply}).
void EncodeSurfaceReplyFrame(const Server::SurfaceControlFrame& frame,
flatbuffers::FlatBufferBuilder* builder);
void EncodeSurfaceReplyFrame(const Server::SurfaceControlFrame& frame, flatbuffers::FlatBufferBuilder* builder);
void DecodeWireSurfaceReply(const ::MobileGL::Wire::SurfaceReply& reply, Server::SurfaceControlFrame* frame);
// The server-side wire entry: decode, then post the frame through ServerLoop's channel and
+55 -54
View File
@@ -23,12 +23,13 @@
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
// ---------------------------------------------------------------------------
// ABI versions
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ABI versions
// ---------------------------------------------------------------------------
#define MOBILEGL_PROTOCOL_ABI_MAJOR 1
#define MOBILEGL_PROTOCOL_ABI_MINOR 0
@@ -46,63 +47,63 @@ extern "C" {
#define MOBILEGL_ABI_MAJOR_OF(version) ((uint32_t)(version) >> 16)
#define MOBILEGL_ABI_MINOR_OF(version) ((uint32_t)(version) & 0xFFFFu)
// ---------------------------------------------------------------------------
// Ids
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Ids
// ---------------------------------------------------------------------------
typedef uint64_t MobileGLSessionId; // one client GL context flow
typedef uint64_t MobileGLRequestSeq; // matches a request to its reply
typedef uint32_t MobileGLSegmentId; // shm segment id within a connection
typedef uint64_t MobileGLSessionId; // one client GL context flow
typedef uint64_t MobileGLRequestSeq; // matches a request to its reply
typedef uint32_t MobileGLSegmentId; // shm segment id within a connection
// ---------------------------------------------------------------------------
// Spans / regions
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Spans / regions
// ---------------------------------------------------------------------------
// Borrowed, read-only byte span. The pointee is owned by the producing side
// and is only valid for the duration documented at the consuming call site.
typedef struct MobileGLByteSpan {
const void* data;
uint64_t size;
} MobileGLByteSpan;
// Borrowed, read-only byte span. The pointee is owned by the producing side
// and is only valid for the duration documented at the consuming call site.
typedef struct MobileGLByteSpan {
const void* data;
uint64_t size;
} MobileGLByteSpan;
typedef struct MobileGLMutableByteSpan {
void* data;
uint64_t size;
} MobileGLMutableByteSpan;
typedef struct MobileGLMutableByteSpan {
void* data;
uint64_t size;
} MobileGLMutableByteSpan;
// A byte range inside an already-established shm segment. Segments are
// announced out of band (the SegmentRef table on the control channel, with the
// fd itself passed by SCM_RIGHTS) and stay stable for their declared lifetime;
// offsets are segment-relative.
typedef struct MobileGLShmRegion {
MobileGLSegmentId segmentId;
uint32_t reserved;
uint64_t offset;
uint64_t size;
} MobileGLShmRegion;
// A byte range inside an already-established shm segment. Segments are
// announced out of band (the SegmentRef table on the control channel, with the
// fd itself passed by SCM_RIGHTS) and stay stable for their declared lifetime;
// offsets are segment-relative.
typedef struct MobileGLShmRegion {
MobileGLSegmentId segmentId;
uint32_t reserved;
uint64_t offset;
uint64_t size;
} MobileGLShmRegion;
// ---------------------------------------------------------------------------
// Result codes (structured errors across every contract boundary)
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Result codes (structured errors across every contract boundary)
// ---------------------------------------------------------------------------
typedef enum MobileGLResult {
MOBILEGL_OK = 0,
MOBILEGL_ERR_NOT_INITIALIZED = 1,
MOBILEGL_ERR_INVALID_ARGUMENT = 2,
MOBILEGL_ERR_UNSUPPORTED = 3,
MOBILEGL_ERR_OUT_OF_MEMORY = 4,
MOBILEGL_ERR_PROTOCOL_MISMATCH = 5, // ABI/wire major mismatch, bad framing
MOBILEGL_ERR_TRANSPORT_CLOSED = 6, // peer gone / EOF
MOBILEGL_ERR_TIMEOUT = 7, // nothing arrived within the deadline
MOBILEGL_ERR_SHM_EXHAUSTED = 8,
MOBILEGL_ERR_SESSION_UNKNOWN = 9,
MOBILEGL_ERR_HANDLE_UNKNOWN = 10,
// The caller's buffer is smaller than the pending message. The message is
// NOT consumed and the required size is reported back; see
// ITransport::ReceiveFrame.
MOBILEGL_ERR_BUFFER_TOO_SMALL = 11,
MOBILEGL_ERR_FORCE_U32 = 0x7FFFFFFF
} MobileGLResult;
typedef enum MobileGLResult {
MOBILEGL_OK = 0,
MOBILEGL_ERR_NOT_INITIALIZED = 1,
MOBILEGL_ERR_INVALID_ARGUMENT = 2,
MOBILEGL_ERR_UNSUPPORTED = 3,
MOBILEGL_ERR_OUT_OF_MEMORY = 4,
MOBILEGL_ERR_PROTOCOL_MISMATCH = 5, // ABI/wire major mismatch, bad framing
MOBILEGL_ERR_TRANSPORT_CLOSED = 6, // peer gone / EOF
MOBILEGL_ERR_TIMEOUT = 7, // nothing arrived within the deadline
MOBILEGL_ERR_SHM_EXHAUSTED = 8,
MOBILEGL_ERR_SESSION_UNKNOWN = 9,
MOBILEGL_ERR_HANDLE_UNKNOWN = 10,
// The caller's buffer is smaller than the pending message. The message is
// NOT consumed and the required size is reported back; see
// ITransport::ReceiveFrame.
MOBILEGL_ERR_BUFFER_TOO_SMALL = 11,
MOBILEGL_ERR_FORCE_U32 = 0x7FFFFFFF
} MobileGLResult;
#ifdef __cplusplus
} // extern "C"
+13 -8
View File
@@ -32,15 +32,18 @@ namespace MobileGL::MG_Remote::Server {
}
} // namespace
AdoptInbox::~AdoptInbox() { Clear(); }
AdoptInbox::~AdoptInbox() {
Clear();
}
void AdoptInbox::Clear() {
for (auto& [seq, kept] : m_kept) CloseFd(kept.fd);
for (auto& [seq, kept] : m_kept)
CloseFd(kept.fd);
m_kept.clear();
}
AdoptInbox::Outcome AdoptInbox::Take(Uint64 seq, Uint32 timeoutMs, int* outFd,
Transport::AdoptT0::Offer* outOffer, std::string& why) {
AdoptInbox::Outcome AdoptInbox::Take(Uint64 seq, Uint32 timeoutMs, int* outFd, Transport::AdoptT0::Offer* outOffer,
std::string& why) {
return TakeKind(Kind::T0, seq, timeoutMs, outFd, outOffer, why);
}
@@ -71,13 +74,15 @@ namespace MobileGL::MG_Remote::Server {
const auto deadline = Clock::now() + std::chrono::milliseconds(timeoutMs);
for (;;) {
const auto now = Clock::now();
const Uint32 left = now >= deadline ? 0u
: static_cast<Uint32>(std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now).count());
const Uint32 left =
now >= deadline ? 0u
: static_cast<Uint32>(
std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now).count());
int fd = -1;
Uint8 sideband[Transport::FdPassing::kMaxSidebandBytes] = {};
std::uint64_t sidebandSize = 0;
const MobileGLResult received = m_transport->ReceiveFd(
&fd, MobileGLMutableByteSpan{sideband, sizeof(sideband)}, &sidebandSize, left);
const MobileGLResult received =
m_transport->ReceiveFd(&fd, MobileGLMutableByteSpan{sideband, sizeof(sideband)}, &sidebandSize, left);
if (received == MOBILEGL_ERR_TIMEOUT) {
why = std::string(kind == Kind::T0 ? "no store" : "no shared-image descriptor") +
" arrived on the aux socket within " + std::to_string(timeoutMs) + " ms of its record";
+10 -4
View File
@@ -29,7 +29,11 @@ namespace MobileGL::MG_Remote::Server {
class AdoptInbox {
public:
enum class Outcome : Uint8 { Taken, TimedOut, Closed };
enum class Outcome : Uint8 {
Taken,
TimedOut,
Closed
};
AdoptInbox() = default;
~AdoptInbox();
@@ -40,8 +44,7 @@ namespace MobileGL::MG_Remote::Server {
// The Offer for record `seq` and its hop descriptor (owned by the caller on Taken). Waits
// at most `timeoutMs` for it; `why` names what went wrong otherwise.
Outcome Take(Uint64 seq, Uint32 timeoutMs, int* outFd, Transport::AdoptT0::Offer* outOffer,
std::string& why);
Outcome Take(Uint64 seq, Uint32 timeoutMs, int* outFd, Transport::AdoptT0::Offer* outOffer, std::string& why);
// A shared image's descriptor for record `seq` (an Import; sideband MGPSharedImageFdOffer).
// Same matching and the same bound as Take; the two kinds share the aux socket and are kept
@@ -56,7 +59,10 @@ namespace MobileGL::MG_Remote::Server {
SizeT Kept() const { return m_kept.size(); }
private:
enum class Kind : Uint8 { T0, SharedImage };
enum class Kind : Uint8 {
T0,
SharedImage
};
struct KeptOffer {
int fd = -1;
Transport::AdoptT0::Offer offer{};
+13 -7
View File
@@ -41,26 +41,32 @@ namespace MobileGL::MG_Remote::Server {
struct ApplyThreadPolicy {
Uint64 requestedMask = 0; // 0 = make no affinity call
Uint32 spinUs = 0;
Bool recognised = true; // false: the affinity text was not `auto`, `off` or a number
const char* rule = ""; // what the log line names
Bool recognised = true; // false: the affinity text was not `auto`, `off` or a number
const char* rule = ""; // what the log line names
};
inline const char* ApplyPeerName(ApplyPeer peer) {
switch (peer) {
case ApplyPeer::InProcess: return "in-process";
case ApplyPeer::Forked: return "forked";
case ApplyPeer::Connected: return "connected";
case ApplyPeer::InProcess:
return "in-process";
case ApplyPeer::Forked:
return "forked";
case ApplyPeer::Connected:
return "connected";
}
return "unknown";
}
inline Uint32 PopCount64(Uint64 mask) {
Uint32 count = 0;
for (; mask != 0; mask &= mask - 1) ++count;
for (; mask != 0; mask &= mask - 1)
++count;
return count;
}
inline Uint64 LowestBit64(Uint64 mask) { return mask & (~mask + 1); }
inline Uint64 LowestBit64(Uint64 mask) {
return mask & (~mask + 1);
}
// Why ReservedApplyCore answered what it did - the log line names it.
enum class ReservedCoreReason : Uint8 {
@@ -95,7 +95,7 @@ namespace {
} // namespace
extern "C" JNIEXPORT void JNICALL Java_top_mobilegl_plugin_MobileGLDisplayActivity_nativeInstallDisplay(JNIEnv* env,
jclass clazz) {
jclass clazz) {
JavaVM* vm = nullptr;
if (env->GetJavaVM(&vm) != JNI_OK) vm = nullptr;
jmethodID onGeometryRequest = env->GetStaticMethodID(clazz, "onGeometryRequest", "(II)V");
@@ -117,7 +117,7 @@ extern "C" JNIEXPORT void JNICALL Java_top_mobilegl_plugin_MobileGLDisplayActivi
}
extern "C" JNIEXPORT jint JNICALL Java_top_mobilegl_plugin_MobileGLDisplayActivity_nativeServe(JNIEnv* env, jclass,
jstring listen) {
jstring listen) {
if (listen == nullptr) return mobilegl_server_serve_inprocess(nullptr);
const char* endpoint = env->GetStringUTFChars(listen, nullptr);
if (endpoint == nullptr) return 71; // OutOfMemoryError pending
@@ -147,13 +147,13 @@ extern "C" JNIEXPORT jboolean JNICALL Java_top_mobilegl_plugin_MobileGLDisplayAc
}
extern "C" JNIEXPORT jstring JNICALL Java_top_mobilegl_plugin_MobileGLDisplayActivity_nativeDetachWindow(JNIEnv* env,
jclass) {
jclass) {
const ServerWindowDetach detached = ServerDisplayInstance().Detach();
return env->NewStringUTF(ServerWindowDetachName(detached));
}
extern "C" JNIEXPORT void JNICALL Java_top_mobilegl_plugin_MobileGLDisplayActivity_nativeUninstallDisplay(JNIEnv* env,
jclass) {
jclass) {
ServerDisplayInstance().Uninstall();
const std::lock_guard<std::mutex> lock(g_upcallMutex);
if (g_activityClass != nullptr) env->DeleteGlobalRef(g_activityClass);
+11 -7
View File
@@ -18,14 +18,18 @@ namespace {
int DetachResult(ServerWindowDetach result) {
switch (result) {
case ServerWindowDetach::NoWindow: return MOBILEGL_SERVER_WINDOW_NO_WINDOW;
case ServerWindowDetach::Released: return MOBILEGL_SERVER_WINDOW_RELEASED;
case ServerWindowDetach::ReleasedBySession: return MOBILEGL_SERVER_WINDOW_RELEASED_BY_SESSION;
case ServerWindowDetach::TimedOut: return MOBILEGL_SERVER_WINDOW_TIMED_OUT;
case ServerWindowDetach::NoWindow:
return MOBILEGL_SERVER_WINDOW_NO_WINDOW;
case ServerWindowDetach::Released:
return MOBILEGL_SERVER_WINDOW_RELEASED;
case ServerWindowDetach::ReleasedBySession:
return MOBILEGL_SERVER_WINDOW_RELEASED_BY_SESSION;
case ServerWindowDetach::TimedOut:
return MOBILEGL_SERVER_WINDOW_TIMED_OUT;
}
return MOBILEGL_SERVER_WINDOW_TIMED_OUT;
}
}
} // namespace
extern "C" int mobilegl_server_display_install(const struct mobilegl_server_display_hooks* hooks) {
if (hooks == nullptr || hooks->acquire == nullptr || hooks->release == nullptr)
@@ -45,8 +49,8 @@ extern "C" int mobilegl_server_display_install(const struct mobilegl_server_disp
extern "C" int mobilegl_server_display_install_android(mobilegl_server_geometry_callback request_geometry, void* user) {
#if defined(__ANDROID__)
const auto androidHooks = AndroidNativeWindowHooks(request_geometry, user);
const mobilegl_server_display_hooks hooks{androidHooks.acquire, androidHooks.release,
androidHooks.requestGeometry, androidHooks.user};
const mobilegl_server_display_hooks hooks{androidHooks.acquire, androidHooks.release, androidHooks.requestGeometry,
androidHooks.user};
return mobilegl_server_display_install(&hooks);
#else
(void)request_geometry;
+53 -52
View File
@@ -18,68 +18,69 @@
#endif
#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif
struct ANativeWindow;
struct ANativeWindow;
typedef void (*mobilegl_server_geometry_callback)(void* user, uint32_t width, uint32_t height);
typedef void (*mobilegl_server_geometry_callback)(void* user, uint32_t width, uint32_t height);
// Opaque-window hooks also let non-Android hosts embed the display without depending on
// MobileGL's C++ headers. Both reference callbacks are required. Geometry may be null.
struct mobilegl_server_display_hooks {
void (*acquire)(void* user, void* window);
void (*release)(void* user, void* window);
mobilegl_server_geometry_callback request_geometry;
void* user;
};
// Opaque-window hooks also let non-Android hosts embed the display without depending on
// MobileGL's C++ headers. Both reference callbacks are required. Geometry may be null.
struct mobilegl_server_display_hooks {
void (*acquire)(void* user, void* window);
void (*release)(void* user, void* window);
mobilegl_server_geometry_callback request_geometry;
void* user;
};
enum mobilegl_server_display_result {
MOBILEGL_SERVER_DISPLAY_OK = 0,
MOBILEGL_SERVER_DISPLAY_INVALID_ARGUMENT = -1,
MOBILEGL_SERVER_DISPLAY_ALREADY_INSTALLED = -2,
MOBILEGL_SERVER_DISPLAY_NOT_INSTALLED = -3,
MOBILEGL_SERVER_DISPLAY_UNSUPPORTED_PLATFORM = -4
};
enum mobilegl_server_display_result {
MOBILEGL_SERVER_DISPLAY_OK = 0,
MOBILEGL_SERVER_DISPLAY_INVALID_ARGUMENT = -1,
MOBILEGL_SERVER_DISPLAY_ALREADY_INSTALLED = -2,
MOBILEGL_SERVER_DISPLAY_NOT_INSTALLED = -3,
MOBILEGL_SERVER_DISPLAY_UNSUPPORTED_PLATFORM = -4
};
enum mobilegl_server_window_detach {
MOBILEGL_SERVER_WINDOW_NO_WINDOW = 0,
MOBILEGL_SERVER_WINDOW_RELEASED = 1,
MOBILEGL_SERVER_WINDOW_RELEASED_BY_SESSION = 2,
MOBILEGL_SERVER_WINDOW_TIMED_OUT = 3
};
enum mobilegl_server_window_detach {
MOBILEGL_SERVER_WINDOW_NO_WINDOW = 0,
MOBILEGL_SERVER_WINDOW_RELEASED = 1,
MOBILEGL_SERVER_WINDOW_RELEASED_BY_SESSION = 2,
MOBILEGL_SERVER_WINDOW_TIMED_OUT = 3
};
// Install once before serving; owner lifecycle calls must be serialized. A live display or
// an old lease awaiting release refuses a replacement, so its window always uses its original
// reference hooks. The geometry callback runs on the apply thread and must return promptly.
// A positive size requests fixed buffer dimensions; 0/0 requests the host's layout dimensions.
// Report the resulting dimensions with attach, for example from SurfaceHolder.surfaceChanged.
// Callback user data must live until uninstall has returned and any timed-out lease has ended.
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_install(const struct mobilegl_server_display_hooks* hooks);
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_install_android(mobilegl_server_geometry_callback request_geometry,
void* user);
// Install once before serving; owner lifecycle calls must be serialized. A live display or
// an old lease awaiting release refuses a replacement, so its window always uses its original
// reference hooks. The geometry callback runs on the apply thread and must return promptly.
// A positive size requests fixed buffer dimensions; 0/0 requests the host's layout dimensions.
// Report the resulting dimensions with attach, for example from SurfaceHolder.surfaceChanged.
// Callback user data must live until uninstall has returned and any timed-out lease has ended.
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_install(const struct mobilegl_server_display_hooks* hooks);
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_install_android(
mobilegl_server_geometry_callback request_geometry, void* user);
// Attach retains its own reference. Attaching the same window updates its dimensions without
// another retain. A different window detaches the previous one first (up to 3 seconds).
// Android attach uses ANativeWindow_getWidth/Height for zero dimensions; the caller may release
// its ANativeWindow_fromSurface reference immediately after this call.
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_attach(void* window, uint32_t width, uint32_t height);
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_attach_android(struct ANativeWindow* window, uint32_t width,
uint32_t height);
// Attach retains its own reference. Attaching the same window updates its dimensions without
// another retain. A different window detaches the previous one first (up to 3 seconds).
// Android attach uses ANativeWindow_getWidth/Height for zero dimensions; the caller may release
// its ANativeWindow_fromSurface reference immediately after this call.
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_attach(void* window, uint32_t width, uint32_t height);
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_attach_android(struct ANativeWindow* window, uint32_t width,
uint32_t height);
// Returns mobilegl_server_window_detach. A timeout never drops a reference while the backend
// is rendering: release happens when the apply thread ends its lease. Surface loss currently
// ends the live split session; a fresh client session can use a newly attached window.
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_detach(uint32_t timeout_ms);
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_uninstall(uint32_t timeout_ms);
// Returns mobilegl_server_window_detach. A timeout never drops a reference while the backend
// is rendering: release happens when the apply thread ends its lease. Surface loss currently
// ends the live split session; a fresh client session can use a newly attached window.
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_detach(uint32_t timeout_ms);
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_uninstall(uint32_t timeout_ms);
// Blocking serve on the caller's thread; stop is non-blocking and safe on the UI thread.
// Endpoint: filesystem path, @abstract AF_UNIX name (SCM_RIGHTS + shared-memory rings), or
// tcp://host:port. One client session per process; independent workers need distinct endpoints.
// Before loading libMobileGL set MOBILEGL_IPC_ROLE=server and MOBILEGL_IPC_DIAL=no, and unset
// MOBILEGL_TRANSPORT, MOBILEGL_IPC_SERVER_PATH, MOBILEGL_IPC_RING_MB, MOBILEGL_IPC_STAGE_MB.
MOBILEGL_SERVER_EXPORT int mobilegl_server_serve_inprocess(const char* endpoint);
MOBILEGL_SERVER_EXPORT void mobilegl_server_stop_inprocess(void);
// Blocking serve on the caller's thread; stop is non-blocking and safe on the UI thread.
// Endpoint: filesystem path, @abstract AF_UNIX name (SCM_RIGHTS + shared-memory rings), or
// tcp://host:port. One client session per process; independent workers need distinct endpoints.
// Before loading libMobileGL set MOBILEGL_IPC_ROLE=server and MOBILEGL_IPC_DIAL=no, and unset
// MOBILEGL_TRANSPORT, MOBILEGL_IPC_SERVER_PATH, MOBILEGL_IPC_RING_MB, MOBILEGL_IPC_STAGE_MB.
MOBILEGL_SERVER_EXPORT int mobilegl_server_serve_inprocess(const char* endpoint);
MOBILEGL_SERVER_EXPORT void mobilegl_server_stop_inprocess(void);
#ifdef __cplusplus
}
+15 -14
View File
@@ -17,21 +17,22 @@
#pragma once
extern "C" {
extern "C"
{
// Serves `endpoint` on the CALLING thread until mobilegl_server_stop_inprocess is called and the
// live session (if any) has ended, then returns 0. The endpoint is tcp://host:port, or - for a
// same-device client - a unix @abstract name or filesystem path, whose control+data pair and
// SCM_RIGHTS shm segments make it the fastest on-device shape. Other returns, all logged by
// name: 64 / 65 (the environment preconditions above), 71 (empty endpoint), 72 (could not
// listen - EADDRINUSE is retried for up to ~5 s first, the previous server may still be dying),
// 73 (the listener failed), 74 (a server is already serving in this process). Re-callable after
// it returned.
__attribute__((visibility("default"))) int mobilegl_server_serve_inprocess(const char* endpoint);
// Serves `endpoint` on the CALLING thread until mobilegl_server_stop_inprocess is called and the
// live session (if any) has ended, then returns 0. The endpoint is tcp://host:port, or - for a
// same-device client - a unix @abstract name or filesystem path, whose control+data pair and
// SCM_RIGHTS shm segments make it the fastest on-device shape. Other returns, all logged by
// name: 64 / 65 (the environment preconditions above), 71 (empty endpoint), 72 (could not
// listen - EADDRINUSE is retried for up to ~5 s first, the previous server may still be dying),
// 73 (the listener failed), 74 (a server is already serving in this process). Re-callable after
// it returned.
__attribute__((visibility("default"))) int mobilegl_server_serve_inprocess(const char* endpoint);
// Asks the in-process server to stop and returns at once (it does not wait: call it from a UI
// thread). The live session ends within one control slice (~100 ms) - or when the op it is running
// returns (a ServerOwned wait for a window is interrupted too) - and then serve returns 0.
__attribute__((visibility("default"))) void mobilegl_server_stop_inprocess(void);
// Asks the in-process server to stop and returns at once (it does not wait: call it from a UI
// thread). The live session ends within one control slice (~100 ms) - or when the op it is running
// returns (a ServerOwned wait for a window is interrupted too) - and then serve returns 0.
__attribute__((visibility("default"))) void mobilegl_server_stop_inprocess(void);
} // extern "C"
+24 -23
View File
@@ -70,7 +70,8 @@ namespace MobileGL::MG_Remote::Server {
PairAcceptor::PairAcceptor(int listenFd, std::uint32_t budgetMs) : m_listener(listenFd), m_budgetMs(budgetMs) {}
PairAcceptor::~PairAcceptor() {
for (auto& waiting : m_waiting) Drop(waiting, true, kPairStopped);
for (auto& waiting : m_waiting)
Drop(waiting, true, kPairStopped);
for (const auto& pair : m_ready) {
RefuseAndClose(pair.first, Protocol::RefuseCode::Busy, kPairStopped);
::close(pair.second);
@@ -176,25 +177,25 @@ namespace MobileGL::MG_Remote::Server {
void PairAcceptor::Read(Waiting& waiting) {
switch (waiting.frame.Pump(waiting.fd)) {
case Step::NeedMore:
return;
case Step::Complete:
Judge(waiting);
return;
case Step::EndedEmpty:
// A readiness probe - a connect() and a close() - costs an accept and nothing else.
MGLOG_D("MG_Remote server: a connection closed before presenting a PairBind (a probe); dropped");
::close(waiting.fd);
waiting.fd = -1;
return;
case Step::NotAFrame:
RefuseAndClose(waiting.fd, Protocol::RefuseCode::MalformedHello, kFirstFrameNotAFrame);
waiting.fd = -1;
return;
case Step::EndedPartial:
RefuseAndClose(waiting.fd, Protocol::RefuseCode::MalformedHello, kFirstFrameTruncated);
waiting.fd = -1;
return;
case Step::NeedMore:
return;
case Step::Complete:
Judge(waiting);
return;
case Step::EndedEmpty:
// A readiness probe - a connect() and a close() - costs an accept and nothing else.
MGLOG_D("MG_Remote server: a connection closed before presenting a PairBind (a probe); dropped");
::close(waiting.fd);
waiting.fd = -1;
return;
case Step::NotAFrame:
RefuseAndClose(waiting.fd, Protocol::RefuseCode::MalformedHello, kFirstFrameNotAFrame);
waiting.fd = -1;
return;
case Step::EndedPartial:
RefuseAndClose(waiting.fd, Protocol::RefuseCode::MalformedHello, kFirstFrameTruncated);
waiting.fd = -1;
return;
}
}
@@ -281,9 +282,9 @@ namespace MobileGL::MG_Remote::Server {
}
void PairAcceptor::Compact() {
m_waiting.erase(std::remove_if(m_waiting.begin(), m_waiting.end(),
[](const Waiting& waiting) { return waiting.fd < 0; }),
m_waiting.end());
m_waiting.erase(
std::remove_if(m_waiting.begin(), m_waiting.end(), [](const Waiting& waiting) { return waiting.fd < 0; }),
m_waiting.end());
}
} // namespace MobileGL::MG_Remote::Server
+2 -2
View File
@@ -96,8 +96,8 @@ namespace MobileGL::MG_Remote::Server {
int m_listener;
std::uint32_t m_budgetMs;
std::vector<Waiting> m_waiting; // accept order, oldest first
std::deque<std::pair<int, int>> m_ready; // {control, aux}, not yet handed out
std::vector<Waiting> m_waiting; // accept order, oldest first
std::deque<std::pair<int, int>> m_ready; // {control, aux}, not yet handed out
Clock::time_point m_acceptResumeAt{};
};
+70 -50
View File
@@ -96,7 +96,9 @@ namespace MobileGL::MG_Remote::Server {
++m_posted;
}
Uint32 ReplyPool::SlotBytes() const { return m_slotBytes; }
Uint32 ReplyPool::SlotBytes() const {
return m_slotBytes;
}
// -----------------------------------------------------------------------------------
// ServerVerbSink - the five class-B verbs
@@ -109,8 +111,7 @@ namespace MobileGL::MG_Remote::Server {
ServerVerbSink::FenceEntry& ServerVerbSink::FindFence(MG_Pipe::MGPipeHandle handle) {
const auto it = m_fences.find(handle.Slot);
if (handle.Slot == 0 || it == m_fences.end() ||
!it->second.Live || it->second.Gen != handle.Gen) {
if (handle.Slot == 0 || it == m_fences.end() || !it->second.Live || it->second.Gen != handle.Gen) {
Wire::WireProtocolFatal("Fence.handle", "missing, destroyed or stale fence handle");
}
return it->second;
@@ -163,8 +164,7 @@ namespace MobileGL::MG_Remote::Server {
auto& entry = FindFence(desc.Handle);
const auto* table = Table("FenceStatus");
if (table == nullptr) return false;
result = entry.Native == nullptr || table->GL.GetSyncStatus == nullptr ||
table->GL.GetSyncStatus(entry.Native);
result = entry.Native == nullptr || table->GL.GetSyncStatus == nullptr || table->GL.GetSyncStatus(entry.Native);
return true;
}
@@ -296,10 +296,11 @@ namespace MobileGL::MG_Remote::Server {
if (present.FrameSerial == 0 && MGPipeServerPublishesRunAhead()) {
// PH-1 (3): latches in an armed session child - no credit is returned for it, the
// session closes and the peer's credit wait ends on the hang-up.
return SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"Present.FrameSerial\"} - a run-ahead "
"server was handed present serial 0. The client mints this 1-based and "
"waits on it for its credit (CONTRACT-P5E §2.4); returning a credit for "
"serial 0 would release a wait that is asking about frame N");
return SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"Present.FrameSerial\"} - a run-ahead "
"server was handed present serial 0. The client mints this 1-based and "
"waits on it for its credit (CONTRACT-P5E §2.4); returning a credit for "
"serial 0 would release a wait that is asking about frame N");
}
m_lastPresentSerial = present.FrameSerial != 0 ? present.FrameSerial : m_presents;
// §2.4's other half, and the reason ServerSession::ReturnPresentCredit has had no
@@ -335,12 +336,13 @@ namespace MobileGL::MG_Remote::Server {
// about how many make-current edges have crossed, which no backend answer can fix.
// PH-1 (3): latches in an armed session child (the peer wrote the serial); dies unarmed.
if (reset.ContextSerial != m_applierResetSerial) {
return SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"ApplierReset.ContextSerial\"} - the "
"record carries %llu and this session has accepted %llu reset(s); the "
"serial is asserted against the session's own count, not dispatched on "
"(one context per session in P5c)",
static_cast<unsigned long long>(reset.ContextSerial),
static_cast<unsigned long long>(m_applierResetSerial));
return SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"ApplierReset.ContextSerial\"} - the "
"record carries %llu and this session has accepted %llu reset(s); the "
"serial is asserted against the session's own count, not dispatched on "
"(one context per session in P5c)",
static_cast<unsigned long long>(reset.ContextSerial),
static_cast<unsigned long long>(m_applierResetSerial));
}
++m_applierResetSerial;
// THE WHOLE POINT OF THE RECORD: the reset runs HERE, on the apply thread, against
@@ -357,7 +359,9 @@ namespace MobileGL::MG_Remote::Server {
return *this;
}
ServerVerbSink::OwnedFd::~OwnedFd() { Reset(-1); }
ServerVerbSink::OwnedFd::~OwnedFd() {
Reset(-1);
}
void ServerVerbSink::OwnedFd::Reset(int next) {
#if !defined(_WIN32)
@@ -521,7 +525,8 @@ namespace MobileGL::MG_Remote::Server {
case MG_Pipe::kMGPSharedImageQueryBufferAge: {
MG_Backend::BackendObject* backend = ServerLoopInstance().Backend();
if (backend == nullptr) return false;
reply.BufferAge = backend->QueryCurrentBufferAge((op.Format & MG_Pipe::kMGPBufferAgeDamageRegionFollows) != 0);
reply.BufferAge =
backend->QueryCurrentBufferAge((op.Format & MG_Pipe::kMGPBufferAgeDamageRegionFollows) != 0);
return true;
}
case MG_Pipe::kMGPSharedImageSetDamageRegion: {
@@ -596,11 +601,12 @@ namespace MobileGL::MG_Remote::Server {
// PH-1 (3): the refusal latches in an armed session child (the peer wrote the token);
// unarmed it dies, like every other contract fact about a peer.
if (!session->BindContext(bind.ClientContextToken)) {
return SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"BindContext.ClientContextToken\"} - the "
"record binds token %llu, which this session never created; a bind_context "
"names a context CreateContext put in the session's table, and 0 (the "
"release) is the only token that needs no entry",
static_cast<unsigned long long>(bind.ClientContextToken));
return SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"BindContext.ClientContextToken\"} - the "
"record binds token %llu, which this session never created; a bind_context "
"names a context CreateContext put in the session's table, and 0 (the "
"release) is the only token that needs no entry",
static_cast<unsigned long long>(bind.ClientContextToken));
}
++m_bindContexts;
#if MOBILEGL_BUILD_DISAGGREGATED
@@ -634,22 +640,24 @@ namespace MobileGL::MG_Remote::Server {
// not a no-op.
// PH-1 (3): both refusals latch in an armed session child; unarmed they die as before.
if (MG_Pipe::MGPipeHandleIsNull(death.Handle)) {
return SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"ObjectDeath.Handle\"} - a null "
"handle never crosses: the client emits nothing for an object its own "
"allocator cannot resolve (CONTRACT-P5C.md §5.2)");
return SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"ObjectDeath.Handle\"} - a null "
"handle never crosses: the client emits nothing for an object its own "
"allocator cannot resolve (CONTRACT-P5C.md §5.2)");
}
if (death.Kind >= static_cast<Uint32>(MG_Pipe::MGPipeKind::KindCount)) {
return SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"ObjectDeath.Kind\"} - %u is not an "
"MGPipeKind",
static_cast<unsigned>(death.Kind));
return SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"ObjectDeath.Kind\"} - %u is not an "
"MGPipeKind",
static_cast<unsigned>(death.Kind));
}
// The per-kind release, keyed by the handle the record carried. A false answer is
// NOT a decline: the kind's own delete opcode may already have released the twin
// (the idempotent second path every notice arm documents), and a kind this backend
// does not twin (Buffer, whose death crosses as resource_destroy) legally resolves
// to nothing.
MG_Backend::DirectGLES::ReleaseTwinsForWireObjectDeath(
death.Handle, static_cast<MG_Pipe::MGPipeKind>(death.Kind));
MG_Backend::DirectGLES::ReleaseTwinsForWireObjectDeath(death.Handle,
static_cast<MG_Pipe::MGPipeKind>(death.Kind));
++m_objectDeaths;
return true;
}
@@ -663,19 +671,22 @@ namespace MobileGL::MG_Remote::Server {
void PipeApplier::Attach(Transport::ILink* link, MG_Backend::BackendObject* backend) {
if (!link || !link->Attached() || !m_segments)
SessionFail(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, PipeApplier::Attach missing link}");
SessionFail(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, PipeApplier::Attach missing link}");
m_verbs.SetBackend(backend);
m_verbs.SetMaxReplyBytes(link->Capabilities().MaxReplyBytes);
m_decoder = Wire::PipeWireDecoder(link, m_segments, m_replies);
m_decoder.SetVerbSink(&m_verbs);
MG_Pipe::MGPipeServerBlockNoteIdentity(); m_attached = true;
MG_Pipe::MGPipeServerBlockNoteIdentity();
m_attached = true;
}
void PipeApplier::Attach(Transport::RingControl* control, MG_Backend::BackendObject* backend) {
if (control == nullptr || m_segments == nullptr) {
SessionFail(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"PipeApplier::Attach\"} - no control "
"page or no segment table; ServerSession::Accept builds both before the "
"apply thread starts");
SessionFail(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"PipeApplier::Attach\"} - no control "
"page or no segment table; ServerSession::Accept builds both before the "
"apply thread starts");
}
m_verbs.SetBackend(backend);
m_decoder = Wire::PipeWireDecoder(control, m_segments, m_replies);
@@ -695,13 +706,16 @@ namespace MobileGL::MG_Remote::Server {
m_attached = false;
}
Bool PipeApplier::Attached() const { return m_attached; }
Bool PipeApplier::Attached() const {
return m_attached;
}
Bool PipeApplier::ApplyOne(const Transport::RingRecordView& record) {
if (!m_attached) {
SessionFail(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"PipeApplier::ApplyOne before Attach\"} "
"- a record reached the applier with no decoder; the apply thread calls "
"Attach once before its first pop");
SessionFail(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"PipeApplier::ApplyOne before Attach\"} "
"- a record reached the applier with no decoder; the apply thread calls "
"Attach once before its first pop");
}
// R-1's INVARIANT, THE SERVER'S HALF (table 3's gPipeInputs row, c1-v1 8.1). The flag
// is raised for the WHOLE of this function and not only around DecodeAndApply: the
@@ -720,11 +734,10 @@ namespace MobileGL::MG_Remote::Server {
// per-opcode tables, so in a session that latches faults a short or unknown record is
// declined before anything is stamped.
if (!m_decoder.AdmitOrDecline(record)) return false;
const Bool wireSaysBarriered = MG_Pipe::MGPipeBarriered(
static_cast<MG_Pipe::MGPWireOp>(record.kind), record.payload, MG_Pipe::MGPipeApplier());
MG_Pipe::MGPipeApplierSetCurrentRecordBarriered(
MGPipeApplierStampsBarriered(MG_Pipe::kMGPipeP5eClientWaitRuleLanded,
MGPipeServerPublishesRunAhead(), wireSaysBarriered));
const Bool wireSaysBarriered = MG_Pipe::MGPipeBarriered(static_cast<MG_Pipe::MGPWireOp>(record.kind),
record.payload, MG_Pipe::MGPipeApplier());
MG_Pipe::MGPipeApplierSetCurrentRecordBarriered(MGPipeApplierStampsBarriered(
MG_Pipe::kMGPipeP5eClientWaitRuleLanded, MGPipeServerPublishesRunAhead(), wireSaysBarriered));
// P5e (gl), ID-128: AND WHETHER IT WAS BARRIERED BY ESCALATION RATHER THAN BY ITS CLASS.
// The predicate above is the static wait class plus two payload-derived escalations
// (ID-83: an open transform-feedback span, a draw carrying client vertex arrays), so the
@@ -734,8 +747,7 @@ namespace MobileGL::MG_Remote::Server {
// client arrays (ID-82) owes, neither of which is this one.
MG_Pipe::MGPipeApplierSetCurrentRecordBarrieredByEscalation(
wireSaysBarriered &&
MG_Pipe::MGPipeWaitClassFor(static_cast<MG_Pipe::MGPWireOp>(record.kind)) ==
MG_Pipe::kWaitNone);
MG_Pipe::MGPipeWaitClassFor(static_cast<MG_Pipe::MGPWireOp>(record.kind)) == MG_Pipe::kWaitNone);
// ORDER IS THE CONTRACT'S: stamp, then apply. The stamp is what makes any server-side
// read of gPipeInputs legal at all (PipeApplier.h's block 1), so a record applied
// before it aborts on the FIRST field inside SyncRenderState.
@@ -764,9 +776,13 @@ namespace MobileGL::MG_Remote::Server {
MG_Pipe::MGPipeServerStampVerbBoundary(verb);
}
void PipeApplier::LeaveApplier() { MG_Pipe::MGPipeServerClearVerbBoundary(); }
void PipeApplier::LeaveApplier() {
MG_Pipe::MGPipeServerClearVerbBoundary();
}
Uint64 PipeApplier::ResidualPullCount() const { return MG_Pipe::MGPipeResidualPullCount(); }
Uint64 PipeApplier::ResidualPullCount() const {
return MG_Pipe::MGPipeResidualPullCount();
}
// The decoder poisons EXACTLY the runs it resolved, from inside DecodeAndApply, once the
// applier has returned - so this entry point is the manual one, for a caller that knows a
@@ -783,8 +799,12 @@ namespace MobileGL::MG_Remote::Server {
std::memset(const_cast<void*>(run), 0xDD, static_cast<SizeT>(size));
}
Uint64 PipeApplier::PoisonedStageBytes() const { return m_decoder.PoisonedStageBytes(); }
Uint64 PipeApplier::PoisonedStageBytes() const {
return m_decoder.PoisonedStageBytes();
}
Uint64 PipeApplier::DecoderAppliedSeq() const { return m_decoder.AppliedSeq(); }
Uint64 PipeApplier::DecoderAppliedSeq() const {
return m_decoder.AppliedSeq();
}
} // namespace MobileGL::MG_Remote::Server
+1 -2
View File
@@ -47,8 +47,7 @@ namespace MobileGL::MG_Remote::Server {
// record is barriered. A server without that bit has a lockstep client, and stamping its
// kWaitNone draws unbarriered while that client is parked would make CountBarrierPull abort.
// Pure so a unit test can pin "no run-ahead bit, never runs ahead" for any input.
constexpr Bool MGPipeApplierStampsBarriered(Bool clientWaitRuleLanded,
Bool serverPublishesRunAhead,
constexpr Bool MGPipeApplierStampsBarriered(Bool clientWaitRuleLanded, Bool serverPublishesRunAhead,
Bool wireSaysBarriered) {
return (clientWaitRuleLanded && serverPublishesRunAhead) ? wireSaysBarriered : true;
}
+28 -12
View File
@@ -70,7 +70,8 @@ namespace MobileGL::MG_Remote::Server {
inline constexpr const char* kFirstFrameBadDataBind = "first frame is a DataBind without a 16-byte nonce";
inline constexpr const char* kPreAuthDeadline = "no authenticated first frame within the pre-auth deadline";
inline constexpr const char* kPreAuthFull = "too many connections are awaiting authentication";
inline constexpr const char* kPreAuthDisplaced = "displaced from the pre-auth queue by a connection from another address";
inline constexpr const char* kPreAuthDisplaced =
"displaced from the pre-auth queue by a connection from another address";
inline constexpr const char* kAuthBackoffRefusal = "too many failed authentications from this address; retry later";
struct PreAuthKnobs {
@@ -118,7 +119,14 @@ namespace MobileGL::MG_Remote::Server {
// verify", and a peer speaking some other schema deserves the specific answer. msg_as_Hello()
// and msg_as_DataBind() are part of the guard, not consequences of it: flatbuffers verifies a
// NULL union member as valid, so a 24-byte envelope whose tag says Hello carries no Hello.
enum class FirstFrameShape { NoIdentifier, Unverifiable, Hello, DataBind, BadDataBind, NotAHello };
enum class FirstFrameShape {
NoIdentifier,
Unverifiable,
Hello,
DataBind,
BadDataBind,
NotAHello
};
inline FirstFrameShape ClassifyFirstFrame(const std::uint8_t* payload, std::size_t size) {
if (payload == nullptr || size < 8 || !::MobileGL::Wire::CtrlEnvelopeBufferHasIdentifier(payload))
@@ -128,8 +136,7 @@ namespace MobileGL::MG_Remote::Server {
const auto* envelope = ::MobileGL::Wire::GetCtrlEnvelope(payload);
if (envelope->msg_type() == ::MobileGL::Wire::CtrlMsg::DataBind) {
const auto* bind = envelope->msg_as_DataBind();
return bind != nullptr && bind->nonce() != nullptr &&
bind->nonce()->size() == Transport::kDataNonceBytes
return bind != nullptr && bind->nonce() != nullptr && bind->nonce()->size() == Transport::kDataNonceBytes
? FirstFrameShape::DataBind
: FirstFrameShape::BadDataBind;
}
@@ -142,12 +149,17 @@ namespace MobileGL::MG_Remote::Server {
// shapes that are not malformed.
inline const char* FirstFrameRefusalDetail(FirstFrameShape shape) {
switch (shape) {
case FirstFrameShape::NoIdentifier: return kFirstFrameNoIdentifier;
case FirstFrameShape::Unverifiable: return kFirstFrameUnverifiable;
case FirstFrameShape::BadDataBind: return kFirstFrameBadDataBind;
case FirstFrameShape::NotAHello: return kFirstFrameNotAHello;
case FirstFrameShape::Hello:
case FirstFrameShape::DataBind: return nullptr;
case FirstFrameShape::NoIdentifier:
return kFirstFrameNoIdentifier;
case FirstFrameShape::Unverifiable:
return kFirstFrameUnverifiable;
case FirstFrameShape::BadDataBind:
return kFirstFrameBadDataBind;
case FirstFrameShape::NotAHello:
return kFirstFrameNotAHello;
case FirstFrameShape::Hello:
case FirstFrameShape::DataBind:
return nullptr;
}
return kFirstFrameNotAHello;
}
@@ -166,7 +178,8 @@ namespace MobileGL::MG_Remote::Server {
inline std::size_t ChoosePendingToDisplace(const std::vector<std::string>& pending, const std::string& newcomer) {
std::unordered_map<std::string, std::size_t> held;
std::size_t most = 0;
for (const auto& address : pending) most = std::max(most, ++held[address]);
for (const auto& address : pending)
most = std::max(most, ++held[address]);
const auto own = held.find(newcomer);
if (own != held.end() && own->second >= most) return pending.size();
for (std::size_t index = 0; index < pending.size(); ++index)
@@ -238,7 +251,10 @@ namespace MobileGL::MG_Remote::Server {
auto victim = m_peers.end();
for (auto it = m_peers.begin(); it != m_peers.end(); ++it) {
const bool expired = it->second.until <= now;
if (victim == m_peers.end()) { victim = it; continue; }
if (victim == m_peers.end()) {
victim = it;
continue;
}
const bool victimExpired = victim->second.until <= now;
if (expired != victimExpired) {
if (expired) victim = it;
+27 -19
View File
@@ -32,22 +32,32 @@ namespace MobileGL::MG_Remote::Server {
const char* ServerWindowAcquireName(ServerWindowAcquire result) {
switch (result) {
case ServerWindowAcquire::Acquired: return "Acquired";
case ServerWindowAcquire::NoDisplay: return "NoDisplay";
case ServerWindowAcquire::NoWindow: return "NoWindow";
case ServerWindowAcquire::Cancelled: return "Cancelled";
case ServerWindowAcquire::Interrupted: return "Interrupted";
case ServerWindowAcquire::LeasedElsewhere: return "LeasedElsewhere";
case ServerWindowAcquire::Acquired:
return "Acquired";
case ServerWindowAcquire::NoDisplay:
return "NoDisplay";
case ServerWindowAcquire::NoWindow:
return "NoWindow";
case ServerWindowAcquire::Cancelled:
return "Cancelled";
case ServerWindowAcquire::Interrupted:
return "Interrupted";
case ServerWindowAcquire::LeasedElsewhere:
return "LeasedElsewhere";
}
return "<unknown ServerWindowAcquire>";
}
const char* ServerWindowDetachName(ServerWindowDetach result) {
switch (result) {
case ServerWindowDetach::NoWindow: return "NoWindow";
case ServerWindowDetach::Released: return "Released";
case ServerWindowDetach::ReleasedBySession: return "ReleasedBySession";
case ServerWindowDetach::TimedOut: return "TimedOut";
case ServerWindowDetach::NoWindow:
return "NoWindow";
case ServerWindowDetach::Released:
return "Released";
case ServerWindowDetach::ReleasedBySession:
return "ReleasedBySession";
case ServerWindowDetach::TimedOut:
return "TimedOut";
}
return "<unknown ServerWindowDetach>";
}
@@ -164,8 +174,8 @@ namespace MobileGL::MG_Remote::Server {
m_lostRequested = true;
if (m_onLost != nullptr) m_onLost(m_leaseHolder);
}
const Bool released = m_cv.wait_for(lock, std::chrono::milliseconds(timeoutMs),
[this] { return m_leaseHolder == nullptr; });
const Bool released =
m_cv.wait_for(lock, std::chrono::milliseconds(timeoutMs), [this] { return m_leaseHolder == nullptr; });
if (!released) {
// NOT RELEASED UNDER IT. The holder may still reference the window (Magma keeps the
// raw pointer until the swapchain is built), so the reference stays until it ends
@@ -265,11 +275,10 @@ namespace MobileGL::MG_Remote::Server {
m_lostRequested = false;
m_leaseEnding = false;
// A suspended holder that leased a window again waits for no other.
m_suspended.erase(std::remove_if(m_suspended.begin(), m_suspended.end(),
[holder](const SuspendedHolder& entry) {
return entry.holder == holder;
}),
m_suspended.end());
m_suspended.erase(
std::remove_if(m_suspended.begin(), m_suspended.end(),
[holder](const SuspendedHolder& entry) { return entry.holder == holder; }),
m_suspended.end());
if (out != nullptr) {
out->window = m_window;
out->width = m_width;
@@ -322,8 +331,7 @@ namespace MobileGL::MG_Remote::Server {
void ServerDisplay::ReleaseAfterLease(const ServerDisplayHooks& hooks, const std::vector<void*>& releases) const {
for (void* window : releases) {
ReleaseOutsideLock(hooks, window);
MGLOG_I("MG_Remote server: server window %p released after its session let go (a late Detach)",
window);
MGLOG_I("MG_Remote server: server window %p released after its session let go (a late Detach)", window);
}
}
+7 -8
View File
@@ -48,7 +48,7 @@ namespace MobileGL::MG_Remote::Server {
struct ServerWindowLease {
void* window = nullptr;
Uint32 width = 0; // the window's extent when the lease was taken
Uint32 width = 0; // the window's extent when the lease was taken
Uint32 height = 0;
Uint64 generation = 0;
// False when a size was requested and the window had not reached it within the geometry
@@ -58,10 +58,10 @@ namespace MobileGL::MG_Remote::Server {
enum class ServerWindowAcquire : Uint8 {
Acquired,
NoDisplay, // this process owns no display (HasDisplay() is false)
NoWindow, // a display, but no window was attached within the wait
Cancelled, // the caller's cancel predicate said so
Interrupted, // Interrupt() (the in-process server is stopping)
NoDisplay, // this process owns no display (HasDisplay() is false)
NoWindow, // a display, but no window was attached within the wait
Cancelled, // the caller's cancel predicate said so
Interrupted, // Interrupt() (the in-process server is stopping)
// P14 S2: NOT A BUG ANY MORE. Several sessions share one process and the display's window
// is one window, so a second ON-SCREEN session asking for it is refused by name here
// (ServerLoop logs the holder); offscreen sessions take no lease and are unaffected. A holder
@@ -115,9 +115,8 @@ namespace MobileGL::MG_Remote::Server {
// may lease again while it holds the lease (a surface re-created at a new size). `onResized`, when
// given, is called whenever the leased window changes size while the lease lasts.
ServerWindowAcquire AcquireFor(Uint32 width, Uint32 height, Uint32 timeoutMs, void* holder,
ServerWindowLostHook onLost, ServerWindowWaitCancel cancel,
void* cancelUser, ServerWindowLease* out,
ServerWindowResizedHook onResized = nullptr);
ServerWindowLostHook onLost, ServerWindowWaitCancel cancel, void* cancelUser,
ServerWindowLease* out, ServerWindowResizedHook onResized = nullptr);
// THE WINDOW'S EXTENT AS THE PLATFORM LAST REPORTED IT (surfaceCreated / surfaceChanged): the one
// source of truth for the size a session publishes to its client. False unless `holder` holds
// the lease on an attached window.
+3 -1
View File
@@ -16,4 +16,6 @@
extern "C" int mobilegl_server_main(int argc, char** argv);
int main(int argc, char** argv) { return mobilegl_server_main(argc, argv); }
int main(int argc, char** argv) {
return mobilegl_server_main(argc, argv);
}
+140 -121
View File
@@ -148,7 +148,9 @@ namespace MobileGL::MG_Remote::Server {
// resolves ServerLoopInstance to that session's loop): a session that latched is over, and
// its lease on the server window, if it holds one, ends once its apply thread has torn the
// backend down - which a fresh session of the same client waits for (MarkLeaseEnding).
void MarkLatchedSessionsLeaseEnding() { ServerDisplayInstance().MarkLeaseEnding(&ServerLoopInstance()); }
void MarkLatchedSessionsLeaseEnding() {
ServerDisplayInstance().MarkLeaseEnding(&ServerLoopInstance());
}
// The bounded join. InProcessTransportTest.cpp:344 uses five seconds for the same
// reason: a lost wakeup must be a RED TEST and not a hung CI job.
@@ -174,8 +176,7 @@ namespace MobileGL::MG_Remote::Server {
Uint64 ReadCpuMaxFrequencyKHz(const Uint cpu) {
char path[128];
std::snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", cpu);
std::snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/cpufreq/cpuinfo_max_freq", cpu);
std::FILE* file = std::fopen(path, "r");
if (file == nullptr) return 0;
unsigned long long value = 0;
@@ -199,7 +200,8 @@ namespace MobileGL::MG_Remote::Server {
if (frequencies[cpu] == 0) return;
}
Uint64 peak = 0;
for (Uint cpu = 0; cpu < cpuCount; ++cpu) peak = std::max(peak, frequencies[cpu]);
for (Uint cpu = 0; cpu < cpuCount; ++cpu)
peak = std::max(peak, frequencies[cpu]);
if (peak == 0) return;
const Uint64 threshold = peak * kBigCoreFrequencyPercent / 100;
for (Uint cpu = 0; cpu < cpuCount; ++cpu) {
@@ -387,7 +389,9 @@ namespace MobileGL::MG_Remote::Server {
return MOBILEGL_OK;
}
MG_Backend::BackendObject* ServerLoop::Backend() { return m_backend.get(); }
MG_Backend::BackendObject* ServerLoop::Backend() {
return m_backend.get();
}
MobileGLResult ServerLoop::InstallBackendForTesting(UniquePtr<MG_Backend::BackendObject> backend) {
if (m_backend != nullptr || backend == nullptr || m_running.load(std::memory_order_acquire)) {
@@ -463,20 +467,30 @@ namespace MobileGL::MG_Remote::Server {
return MOBILEGL_OK;
}
Bool ServerLoop::Running() const { return m_running.load(std::memory_order_acquire); }
Bool ServerLoop::Running() const {
return m_running.load(std::memory_order_acquire);
}
// OnApplyThread() is inline in the header now (P5d round 3, package D). Its whole body is
// one relaxed load of Detail::g_applyThreadKey and one comparison against the caller's own
// thread pointer; the two writes to that key are in ApplyThreadMain, below.
Uint64 ServerLoop::ResolvedAffinityMask() const { return m_affinityMask; }
Uint64 ServerLoop::DrainedRecords() const { return m_drained.load(std::memory_order_acquire); }
Uint64 ServerLoop::ParkCount() const { return m_parks.load(std::memory_order_acquire); }
Uint64 ServerLoop::ResolvedAffinityMask() const {
return m_affinityMask;
}
Uint64 ServerLoop::DrainedRecords() const {
return m_drained.load(std::memory_order_acquire);
}
Uint64 ServerLoop::ParkCount() const {
return m_parks.load(std::memory_order_acquire);
}
Uint64 ServerLoop::ParkBlockCount() const {
return m_parkBlocks.load(std::memory_order_relaxed);
}
Uint64 ServerLoop::NativeBindCount() const { return m_nativeBinds.load(std::memory_order_acquire); }
Uint64 ServerLoop::NativeBindCount() const {
return m_nativeBinds.load(std::memory_order_acquire);
}
Uint64 ServerLoop::ClientReleaseCount() const {
return m_clientReleases.load(std::memory_order_acquire);
}
@@ -491,9 +505,9 @@ namespace MobileGL::MG_Remote::Server {
// test in BackendObject_DirectGLES.cpp) is a ClientRelease the server records but does not
// forward. Otherwise an identical (dpy, draw, read, ctx) already held is a RepeatNoOp, and
// anything else is a real NativeBind. Pure: a unit case drives it with no EGL context.
EglBindAction ClassifyEglMakeCurrent(Bool haveCurrent, EGLDisplay curDpy, EGLSurface curDraw,
EGLSurface curRead, EGLContext curCtx, EGLDisplay dpy,
EGLSurface draw, EGLSurface read, EGLContext ctx) {
EglBindAction ClassifyEglMakeCurrent(Bool haveCurrent, EGLDisplay curDpy, EGLSurface curDraw, EGLSurface curRead,
EGLContext curCtx, EGLDisplay dpy, EGLSurface draw, EGLSurface read,
EGLContext ctx) {
if (draw == EGL_NO_SURFACE && read == EGL_NO_SURFACE && ctx == EGL_NO_CONTEXT) {
return EglBindAction::ClientRelease;
}
@@ -503,9 +517,8 @@ namespace MobileGL::MG_Remote::Server {
return EglBindAction::NativeBind;
}
ServerLoop::MakeCurrentOutcome ServerLoop::ApplyMakeCurrent(MG_Backend::BackendObject* backend,
EGLDisplay dpy, EGLSurface draw,
EGLSurface read, EGLContext ctx) {
ServerLoop::MakeCurrentOutcome ServerLoop::ApplyMakeCurrent(MG_Backend::BackendObject* backend, EGLDisplay dpy,
EGLSurface draw, EGLSurface read, EGLContext ctx) {
// Apply thread only (RunSurfaceControlFrame's dispatch put us here), so
// m_haveCurrentTuple/m_cur* need no
// lock. The counters are atomic for the reader on the test thread.
@@ -515,8 +528,8 @@ namespace MobileGL::MG_Remote::Server {
// this thread). What S4 changed is one layer down: the tuple now names the (session, context)
// registry's native context instead of the process's one.
MakeCurrentOutcome outcome;
const EglBindAction action = ClassifyEglMakeCurrent(m_haveCurrentTuple, m_curDpy, m_curDraw,
m_curRead, m_curCtx, dpy, draw, read, ctx);
const EglBindAction action =
ClassifyEglMakeCurrent(m_haveCurrentTuple, m_curDpy, m_curDraw, m_curRead, m_curCtx, dpy, draw, read, ctx);
switch (action) {
case EglBindAction::ClientRelease:
// Recorded, NOT forwarded (ID-54): the context stays current on this thread until
@@ -694,9 +707,7 @@ namespace MobileGL::MG_Remote::Server {
// would park this thread inside the post, and the ring test below already says "not now".
ServerVerbSink& verbs = session.Applier().Verbs();
constexpr Uint32 kFenceIdlePollMs = 1;
const auto eventRingHasRoom = [signals] {
return signals.EventRingFull->load(std::memory_order_acquire) == 0;
};
const auto eventRingHasRoom = [signals] { return signals.EventRingFull->load(std::memory_order_acquire) == 0; };
for (;;) {
std::optional<ScopedBackendTurn> turn(std::in_place);
@@ -779,8 +790,7 @@ namespace MobileGL::MG_Remote::Server {
const Bool fencePending = verbs.HasUnreportedFences();
m_applyParked.store(true, std::memory_order_release);
const bool woke = bell.Wait(*signals.ConsumerParked, ready, spinUs,
fencePending ? kFenceIdlePollMs : Transport::kWaitForever,
&m_parkBlocks);
fencePending ? kFenceIdlePollMs : Transport::kWaitForever, &m_parkBlocks);
m_applyParked.store(false, std::memory_order_release);
if (!woke) {
// Wait returns false only on a dead bell or an expired deadline. A dead bell IS the
@@ -994,14 +1004,15 @@ namespace MobileGL::MG_Remote::Server {
// frame's reply half; the reply is then published back into the slot under the lock so
// the poster's copy-out after m_controlDone sees it.
const auto dispatchStart = std::chrono::steady_clock::now();
const MobileGLResult result = ApplySurfaceControlFrame(
frame, m_controlProbeHook.load(std::memory_order_acquire),
m_controlProbeUser.load(std::memory_order_acquire));
const MobileGLResult result =
ApplySurfaceControlFrame(frame, m_controlProbeHook.load(std::memory_order_acquire),
m_controlProbeUser.load(std::memory_order_acquire));
// p7/spawnhang: A SLOW DISPATCH NAMES ITSELF. The first surface creation / MakeCurrent is
// where a lazy native bring-up runs, and on a cold CI runner it outran the client's whole
// reply budget with nothing in this log to say so - the investigation had to infer it.
const auto dispatchMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - dispatchStart).count();
const auto dispatchMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - dispatchStart)
.count();
if (dispatchMs >= static_cast<long long>(kSlowControlDispatchMs)) {
MGLOG_W("MG_Remote server: %s seq %llu ran %lld ms on mgl-srv-apply (a lazy native "
"bring-up runs inside the first surface creation or MakeCurrent)",
@@ -1108,8 +1119,8 @@ namespace MobileGL::MG_Remote::Server {
// report - used to be popped and applied unpolled, and a GUILTY context ran one more record
// (ServerLoopEglLatchTest under CPU load). `polledHead` is the command ring's head as loaded
// just BEFORE the last poll (the apply loop's own, handed in): every record below it was
// visible when that poll ran. A record at or past it re-loads the head and polls first. While the client stays ahead this is one
// poll per batch the drain catches up to, never more than one per record.
// visible when that poll ran. A record at or past it re-loads the head and polls first. While the client stays
// ahead this is one poll per batch the drain catches up to, never more than one per record.
Transport::LinkSignals signals{};
if (auto* link = session.DataLink()) signals = link->Signals();
for (;;) {
@@ -1141,10 +1152,10 @@ namespace MobileGL::MG_Remote::Server {
if (timed) {
const auto applyStarted = std::chrono::steady_clock::now();
applier.ApplyOne(record);
sFrameApplyNs += static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - applyStarted)
.count());
sFrameApplyNs +=
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - applyStarted)
.count());
} else {
applier.ApplyOne(record);
}
@@ -1154,8 +1165,7 @@ namespace MobileGL::MG_Remote::Server {
if (record.kind == static_cast<std::uint16_t>(MG_Pipe::MGPWireOp::Present)) {
const auto now = std::chrono::steady_clock::now();
const auto wallNs = static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(now - sFrameWallStart)
.count());
std::chrono::duration_cast<std::chrono::nanoseconds>(now - sFrameWallStart).count());
const double applyMs = static_cast<double>(sFrameApplyNs) / 1e6;
const double wallMs = static_cast<double>(wallNs) / 1e6;
// AND THE STAGE NOTHING ELSE COVERS: the io thread's socket reads, which
@@ -1170,16 +1180,14 @@ namespace MobileGL::MG_Remote::Server {
"outside_ms=%.1f | read: %llu B in %llu recv(%llu reads) "
"recv_ms=%.1f total_ms=%.1f (%.0f%% blocked, %.1f MB/s)",
static_cast<unsigned long long>(++sFrameIndex),
static_cast<unsigned long long>(sFrameRecords), applyMs, wallMs,
wallMs - applyMs,
static_cast<unsigned long long>(sFrameRecords), applyMs, wallMs, wallMs - applyMs,
static_cast<unsigned long long>(read.bytes),
static_cast<unsigned long long>(read.calls),
static_cast<unsigned long long>(read.reads),
static_cast<double>(read.nsInRecv) / 1e6,
static_cast<double>(read.nsTotal) / 1e6,
read.nsTotal ? 100.0 * static_cast<double>(read.nsInRecv) /
static_cast<double>(read.nsTotal)
: 0.0,
static_cast<double>(read.nsInRecv) / 1e6, static_cast<double>(read.nsTotal) / 1e6,
read.nsTotal
? 100.0 * static_cast<double>(read.nsInRecv) / static_cast<double>(read.nsTotal)
: 0.0,
read.nsTotal ? static_cast<double>(read.bytes) /
(static_cast<double>(read.nsTotal) / 1e9) / 1e6
: 0.0);
@@ -1210,9 +1218,10 @@ namespace MobileGL::MG_Remote::Server {
// for ever; skipping desynchronises seq, and seq IS the reply-slot id. PH-1 (3):
// the header is the peer's bytes, so an armed session child latches here and
// stops reading the ring - neither a retry nor a skip.
(void)SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"SEG_CMD record header\"} - the "
"consumer refused a record header at applied seq %llu",
static_cast<unsigned long long>(consumer.AppliedSeq()));
(void)SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"SEG_CMD record header\"} - the "
"consumer refused a record header at applied seq %llu",
static_cast<unsigned long long>(consumer.AppliedSeq()));
break;
}
if (!popped) break;
@@ -1242,12 +1251,13 @@ namespace MobileGL::MG_Remote::Server {
if (applier.DecoderAppliedSeq() != consumer.AppliedSeq()) {
// PH-1 (3): latched, and nothing is retired against a watermark this side no
// longer believes.
(void)SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"appliedSeq batched\"} - the "
"session's watermark is %llu and the decoder applied %llu records. P5 "
"forbids batching appliedSeq (R-9): the verb barrier's waiter reads it, "
"and a watermark ahead of the decoder promises work that has not run",
static_cast<unsigned long long>(consumer.AppliedSeq()),
static_cast<unsigned long long>(applier.DecoderAppliedSeq()));
(void)SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"appliedSeq batched\"} - the "
"session's watermark is %llu and the decoder applied %llu records. P5 "
"forbids batching appliedSeq (R-9): the verb barrier's waiter reads it, "
"and a watermark ahead of the decoder promises work that has not run",
static_cast<unsigned long long>(consumer.AppliedSeq()),
static_cast<unsigned long long>(applier.DecoderAppliedSeq()));
return applied;
}
// RETIRE. MANDATORY, not optional: RingProducer::FreeBytes() reclaims against
@@ -1311,12 +1321,13 @@ namespace MobileGL::MG_Remote::Server {
// render correctly with the context on the wrong thread - the one outcome R-1
// exists to make impossible. Fatal by name, never a fallback; compare
// CreateBackend's default: arm, which also refuses rather than substitutes.
SessionFail(MGFatalFamily::ApplyThreadNotRunning, "MGPipe: Fatal{ApplyThreadNotRunning, \"EGL on the app thread\"} - a "
"control frame reached RunSurfaceControlFrame with the server's backend "
"built but no mgl-srv-apply thread running (ClientSession::Start failed "
"after ServerLoop::CreateBackend). Dispatching it inline would make the "
"context current on the APP thread - the split lane's whole premise. "
"Refusing by name rather than falling back to monolith");
SessionFail(MGFatalFamily::ApplyThreadNotRunning,
"MGPipe: Fatal{ApplyThreadNotRunning, \"EGL on the app thread\"} - a "
"control frame reached RunSurfaceControlFrame with the server's backend "
"built but no mgl-srv-apply thread running (ClientSession::Start failed "
"after ServerLoop::CreateBackend). Dispatching it inline would make the "
"context current on the APP thread - the split lane's whole premise. "
"Refusing by name rather than falling back to monolith");
}
// Backend already gone (post-Stop teardown, or the pre-Init window): the dispatch's
// null-backend arm would answer NOT_INITIALIZED anyway, so that is the honest result
@@ -1361,11 +1372,11 @@ namespace MobileGL::MG_Remote::Server {
const ControlProgressSink sink = m_progressSink;
void* const user = m_progressUser;
if (!running || sink == nullptr) continue;
const auto elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - postedAt).count();
const auto elapsedMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - postedAt)
.count();
lock.unlock();
sink(user, frame.kind, frame.seq,
static_cast<Uint32>(std::min<long long>(elapsedMs, 0xFFFFFFFFll)));
sink(user, frame.kind, frame.seq, static_cast<Uint32>(std::min<long long>(elapsedMs, 0xFFFFFFFFll)));
lock.lock();
}
frame = m_controlFrame; // the reply half, written by the dispatch
@@ -1405,7 +1416,9 @@ namespace MobileGL::MG_Remote::Server {
m_exitCv.notify_all();
}
void ServerLoop::AbandonQueuedRecords() { m_abandonQueue.store(true, std::memory_order_release); }
void ServerLoop::AbandonQueuedRecords() {
m_abandonQueue.store(true, std::memory_order_release);
}
void ServerLoop::Stop() {
if (!m_thread.joinable()) {
@@ -1445,8 +1458,7 @@ namespace MobileGL::MG_Remote::Server {
Bool exited = false;
{
std::unique_lock<std::mutex> lock(m_exitMutex);
exited = m_exitCv.wait_for(lock, std::chrono::milliseconds(kJoinTimeoutMs),
[this] { return m_exited; });
exited = m_exitCv.wait_for(lock, std::chrono::milliseconds(kJoinTimeoutMs), [this] { return m_exited; });
}
if (!exited && !m_applyParked.load(std::memory_order_acquire)) {
// BUSY, NOT LOST (kBusyJoinTimeoutMs): the thread is applying, most likely blocked on
@@ -1466,15 +1478,16 @@ namespace MobileGL::MG_Remote::Server {
// run on into the client's teardown, reading rings the client is about to unmap -
// a use-after-free whose only symptom is an intermittent crash somewhere else.
// Aborting here is red, immediate, and names the cause.
SessionFail(MGFatalFamily::ApplyThreadJoinTimeout, "MGPipe: Fatal{ApplyThreadJoinTimeout} - mgl-srv-apply did not exit within "
"%u ms of Stop(). Stop() published m_stopRequested (in the park predicate) "
"BEFORE it rang, so a plain Notify should already have un-parked the thread; "
"Doorbell::Kill() (Doorbell.h, CondVarDoorbell::Kill; table 3 step 2) is "
"the belt to that "
"Notify's braces. If the thread is still parked after both, the wakeup was "
"lost, not slow. Aborting rather than detaching: a detached apply thread still "
"owns the context and would read rings the client is about to unmap",
kJoinTimeoutMs);
SessionFail(MGFatalFamily::ApplyThreadJoinTimeout,
"MGPipe: Fatal{ApplyThreadJoinTimeout} - mgl-srv-apply did not exit within "
"%u ms of Stop(). Stop() published m_stopRequested (in the park predicate) "
"BEFORE it rang, so a plain Notify should already have un-parked the thread; "
"Doorbell::Kill() (Doorbell.h, CondVarDoorbell::Kill; table 3 step 2) is "
"the belt to that "
"Notify's braces. If the thread is still parked after both, the wakeup was "
"lost, not slow. Aborting rather than detaching: a detached apply thread still "
"owns the context and would read rings the client is about to unmap",
kJoinTimeoutMs);
}
m_thread.join();
// The identity is NOT cleared here any more: the thread clears it itself, under
@@ -1563,9 +1576,11 @@ namespace MobileGL::MG_Remote::Server {
Bool WindowBackendFromFrameValue(Int value, MG_Backend::WindowBackend* out) {
if (value < static_cast<Int>(MG_Backend::WindowBackend::Unknown) ||
value >= static_cast<Int>(MG_Backend::WindowBackend::WindowBackendCount)) {
return SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"SurfaceOp.windowBackend\"} - a "
"control frame carried window backend tag %d, which names no "
"WindowBackend", value);
return SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"SurfaceOp.windowBackend\"} - a "
"control frame carried window backend tag %d, which names no "
"WindowBackend",
value);
}
*out = static_cast<MG_Backend::WindowBackend>(value);
return true;
@@ -1618,8 +1633,8 @@ namespace MobileGL::MG_Remote::Server {
} // namespace
MobileGLResult ServerLoop::ApplySurfaceControlFrame(SurfaceControlFrame& frame,
ControlProbeHook probeHook, void* probeUser) {
MobileGLResult ServerLoop::ApplySurfaceControlFrame(SurfaceControlFrame& frame, ControlProbeHook probeHook,
void* probeUser) {
// The frame channel's own tally, bumped for EVERY dispatch including probes: a forwarder
// that stopped posting frames (the fc red-once revert shape) leaves this unmoved, which
// is what ServerLoopTest's AVoidForwarderCrossesAsOneDispatchedFrame reads.
@@ -1641,8 +1656,7 @@ namespace MobileGL::MG_Remote::Server {
// process boundary, so the version numbers travel back in the frame's value.
EGLint major = 0;
EGLint minor = 0;
frame.ok = backend->InitializeEGLDisplay(HandleFromToken<EGLDisplay>(frame.display),
&major, &minor);
frame.ok = backend->InitializeEGLDisplay(HandleFromToken<EGLDisplay>(frame.display), &major, &minor);
frame.eglMajor = major;
frame.eglMinor = minor;
return MOBILEGL_OK;
@@ -1656,8 +1670,7 @@ namespace MobileGL::MG_Remote::Server {
frame.ok = false;
return MOBILEGL_ERR_PROTOCOL_MISMATCH; // PH-1 (3): latched
}
frame.ok = backend->CreateEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface),
window);
frame.ok = backend->CreateEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface), window);
// N-3: BackendObject_DirectGLES destroys and recreates the native context to create
// a DIFFERENT surface, so whatever tuple was bound names a dead context.
if (frame.ok) ForgetCurrentTuple();
@@ -1667,9 +1680,9 @@ namespace MobileGL::MG_Remote::Server {
// P12 review fix: a server-owned surface is resized by resizing the SERVER's window.
if (IsServerOwnedSurface(HandleFromToken<EGLSurface>(frame.surface)))
return ApplyServerOwnedWindowResize(backend, frame);
frame.ok = backend->ResizeEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface),
static_cast<Uint32>(frame.width),
static_cast<Uint32>(frame.height));
frame.ok =
backend->ResizeEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface),
static_cast<Uint32>(frame.width), static_cast<Uint32>(frame.height));
return MOBILEGL_OK;
case SurfaceControlOp::CreatePbufferSurface: {
// A surfaceless stand-in (kPbufferFlagSurfacelessStandIn) is no rendering path: it is
@@ -1690,8 +1703,8 @@ namespace MobileGL::MG_Remote::Server {
SessionSurfaceModeName(m_surfaceMode));
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
frame.ok = backend->CreateEGLPbufferSurface(HandleFromToken<EGLSurface>(frame.surface),
frame.width, frame.height);
frame.ok =
backend->CreateEGLPbufferSurface(HandleFromToken<EGLSurface>(frame.surface), frame.width, frame.height);
// N-3: as for the window surface - a (re)creation may have destroyed the context the
// held tuple named. The surface's own creation binds natively, so the client's
// make-current that follows is forwarded and deduped one layer down (ID-54).
@@ -1724,11 +1737,9 @@ namespace MobileGL::MG_Remote::Server {
// current with ITS context and not attached to a neighbour's. A frame from a client
// that sends a handle names no token and leaves the binding alone.
if (m_session != nullptr && frame.context != 0) (void)m_session->BindContext(frame.context);
const MakeCurrentOutcome outcome =
ApplyMakeCurrent(backend, HandleFromToken<EGLDisplay>(frame.display),
HandleFromToken<EGLSurface>(frame.surface),
HandleFromToken<EGLSurface>(frame.readSurface),
HandleFromToken<EGLContext>(frame.context));
const MakeCurrentOutcome outcome = ApplyMakeCurrent(
backend, HandleFromToken<EGLDisplay>(frame.display), HandleFromToken<EGLSurface>(frame.surface),
HandleFromToken<EGLSurface>(frame.readSurface), HandleFromToken<EGLContext>(frame.context));
frame.ok = outcome.ok;
if (!outcome.ok || !outcome.boundNatively) return MOBILEGL_OK;
// R-12, arm (a): the caps snapshot is REPUBLISHED because InitCapabilities has now
@@ -1765,9 +1776,8 @@ namespace MobileGL::MG_Remote::Server {
// current on this thread until ~BackendObject_DirectGLES or context loss, so
// forwarding would unbind and reinstate the off-thread degradation the phase
// removes). A client release never republishes: nothing ran that could move caps.
const MakeCurrentOutcome outcome =
ApplyMakeCurrent(backend, HandleFromToken<EGLDisplay>(frame.display),
EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
const MakeCurrentOutcome outcome = ApplyMakeCurrent(backend, HandleFromToken<EGLDisplay>(frame.display),
EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
frame.ok = outcome.ok;
return MOBILEGL_OK;
}
@@ -1788,8 +1798,7 @@ namespace MobileGL::MG_Remote::Server {
return MOBILEGL_ERR_NOT_INITIALIZED;
}
frame.ok = session->CreateContext(frame.context, frame.shareGroupToken, frame.contextFlags);
if (frame.ok && backend != nullptr &&
backend->GetBackendType() == BackendType::DirectGLES) {
if (frame.ok && backend != nullptr && backend->GetBackendType() == BackendType::DirectGLES) {
if (!MG_Backend::DirectGLES::CreateNativeContextFor(frame.context, frame.shareGroupToken)) {
(void)session->DestroyContext(frame.context);
frame.ok = false;
@@ -1807,8 +1816,7 @@ namespace MobileGL::MG_Remote::Server {
// P14 S4: the native context goes with the entry, and ONLY that one - the session's
// other contexts, and every other session's, are untouched. The display is terminated
// only when the last native context in the process has gone.
if (frame.ok && backend != nullptr &&
backend->GetBackendType() == BackendType::DirectGLES) {
if (frame.ok && backend != nullptr && backend->GetBackendType() == BackendType::DirectGLES) {
MG_Backend::DirectGLES::DestroyNativeContextFor(frame.context);
}
return MOBILEGL_OK;
@@ -1894,8 +1902,10 @@ namespace MobileGL::MG_Remote::Server {
// PH-1 (3): latched in an armed session child (under spawn SurfaceOpCodec has already
// refused an unknown wire kind, so this is defence in depth); unarmed it dies.
frame.ok = false;
(void)SessionLatch(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"SurfaceOp.kind\"} - the control slot held "
"kind %d, which names no dispatchable op", static_cast<int>(frame.kind));
(void)SessionLatch(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"SurfaceOp.kind\"} - the control slot held "
"kind %d, which names no dispatchable op",
static_cast<int>(frame.kind));
return MOBILEGL_ERR_PROTOCOL_MISMATCH;
}
@@ -1905,18 +1915,24 @@ namespace MobileGL::MG_Remote::Server {
const char* SessionSurfaceModeName(SessionSurfaceMode mode) {
switch (mode) {
case SessionSurfaceMode::None: return "undecided";
case SessionSurfaceMode::OnScreen: return "on-screen";
case SessionSurfaceMode::Offscreen: return "offscreen";
case SessionSurfaceMode::None:
return "undecided";
case SessionSurfaceMode::OnScreen:
return "on-screen";
case SessionSurfaceMode::Offscreen:
return "offscreen";
}
return "<unknown SessionSurfaceMode>";
}
Bool SessionSurfaceModeAdmits(SessionSurfaceMode current, Bool serverOwnedWindow) {
switch (current) {
case SessionSurfaceMode::None: return true;
case SessionSurfaceMode::OnScreen: return serverOwnedWindow;
case SessionSurfaceMode::Offscreen: return !serverOwnedWindow;
case SessionSurfaceMode::None:
return true;
case SessionSurfaceMode::OnScreen:
return serverOwnedWindow;
case SessionSurfaceMode::Offscreen:
return !serverOwnedWindow;
}
return false;
}
@@ -2008,7 +2024,7 @@ namespace MobileGL::MG_Remote::Server {
}
MobileGLResult ServerLoop::ApplyServerOwnedWindowSurface(MG_Backend::BackendObject* backend,
SurfaceControlFrame& frame) {
SurfaceControlFrame& frame) {
frame.ok = false;
frame.refusal = static_cast<Uint8>(SurfaceRefusalCode::None);
const Uint32 wantWidth = frame.width > 0 ? static_cast<Uint32>(frame.width) : 0u;
@@ -2027,7 +2043,8 @@ namespace MobileGL::MG_Remote::Server {
}
ServerWindowLease lease;
SurfaceRefusalCode refusal = SurfaceRefusalCode::None;
const MobileGLResult acquired = AcquireServerWindow(wantWidth, wantHeight, kServerWindowWaitMs, &lease, &refusal);
const MobileGLResult acquired =
AcquireServerWindow(wantWidth, wantHeight, kServerWindowWaitMs, &lease, &refusal);
if (acquired != MOBILEGL_OK) {
frame.refusal = static_cast<Uint8>(refusal);
return acquired;
@@ -2072,10 +2089,11 @@ namespace MobileGL::MG_Remote::Server {
frame.width = static_cast<Int>(lease.width);
frame.height = static_cast<Int>(lease.height);
// THE ARM PROOF (ID-124), once per surface: which of the two paths ran, and at what size.
MGLOG_I("MG_Remote server: surface=window %ux%u owner=server (ServerOwned seq %llu, window %p generation %llu%s)",
lease.width, lease.height, static_cast<unsigned long long>(frame.seq), lease.window,
static_cast<unsigned long long>(lease.generation),
lease.sizeAsRequested ? "" : ", NOT the size the client asked for");
MGLOG_I(
"MG_Remote server: surface=window %ux%u owner=server (ServerOwned seq %llu, window %p generation %llu%s)",
lease.width, lease.height, static_cast<unsigned long long>(frame.seq), lease.window,
static_cast<unsigned long long>(lease.generation),
lease.sizeAsRequested ? "" : ", NOT the size the client asked for");
return MOBILEGL_OK;
}
@@ -2096,7 +2114,7 @@ namespace MobileGL::MG_Remote::Server {
// for (the same lease, re-taken by its holder), the backend surface follows the window's REAL
// extent, and that extent is the reply's width/height - which the client adopts.
MobileGLResult ServerLoop::ApplyServerOwnedWindowResize(MG_Backend::BackendObject* backend,
SurfaceControlFrame& frame) {
SurfaceControlFrame& frame) {
frame.ok = false;
frame.refusal = static_cast<Uint8>(SurfaceRefusalCode::None);
const Uint32 wantWidth = frame.width > 0 ? static_cast<Uint32>(frame.width) : 0u;
@@ -2116,7 +2134,8 @@ namespace MobileGL::MG_Remote::Server {
}
ServerWindowLease lease;
SurfaceRefusalCode refusal = SurfaceRefusalCode::None;
const MobileGLResult acquired = AcquireServerWindow(wantWidth, wantHeight, kServerWindowWaitMs, &lease, &refusal);
const MobileGLResult acquired =
AcquireServerWindow(wantWidth, wantHeight, kServerWindowWaitMs, &lease, &refusal);
if (acquired != MOBILEGL_OK) {
frame.refusal = static_cast<Uint8>(refusal);
return acquired;
@@ -2124,7 +2143,8 @@ namespace MobileGL::MG_Remote::Server {
m_serverWindowWantWidth = wantWidth;
m_serverWindowWantHeight = wantHeight;
NoteServerWindowExtent(lease.window, lease.width, lease.height);
frame.ok = backend->ResizeEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface), lease.width, lease.height);
frame.ok =
backend->ResizeEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface), lease.width, lease.height);
frame.width = static_cast<Int>(lease.width);
frame.height = static_cast<Int>(lease.height);
MGLOG_I("MG_Remote server: surface=window %ux%u owner=server resized (ServerOwned seq %llu, %ux%u requested%s)",
@@ -2238,10 +2258,9 @@ namespace MobileGL::MG_Remote::Server {
ServerWindowAcquire acquired = ServerWindowAcquire::NoDisplay;
{
const ScopedBackendTurnYield yield;
acquired = ServerDisplayInstance().AcquireFor(m_serverWindowWantWidth, m_serverWindowWantHeight,
ServerDisplay::kGeometryGraceMs, this, &ServerWindowLostThunk,
&ServerWindowWaitCancelled, this, &lease,
&ServerWindowResizedThunk);
acquired = ServerDisplayInstance().AcquireFor(
m_serverWindowWantWidth, m_serverWindowWantHeight, ServerDisplay::kGeometryGraceMs, this,
&ServerWindowLostThunk, &ServerWindowWaitCancelled, this, &lease, &ServerWindowResizedThunk);
}
if (acquired != ServerWindowAcquire::Acquired) {
// Still registered: the next new window wakes this again.
+19 -11
View File
@@ -60,7 +60,11 @@ namespace MobileGL::MG_Remote::Server {
// nor are refused. The mode is a loop member reset by ServerLoop::Start, so each session
// latches on its own; it is not read from MOBILEGL_IPC_SURFACE, because one process's
// environment cannot speak for every session a unified server runs.
enum class SessionSurfaceMode : Uint8 { None, OnScreen, Offscreen };
enum class SessionSurfaceMode : Uint8 {
None,
OnScreen,
Offscreen
};
const char* SessionSurfaceModeName(SessionSurfaceMode mode);
// The decision, pure, so a unit case can drive all six pairs without a backend.
Bool SessionSurfaceModeAdmits(SessionSurfaceMode current, Bool serverOwnedWindow);
@@ -242,8 +246,8 @@ namespace MobileGL::MG_Remote::Server {
Bool ok = false; // the request was honoured
Bool boundNatively = false; // a native eglMakeCurrent ran (=> republish caps)
};
MakeCurrentOutcome ApplyMakeCurrent(MG_Backend::BackendObject* backend, EGLDisplay dpy,
EGLSurface draw, EGLSurface read, EGLContext ctx);
MakeCurrentOutcome ApplyMakeCurrent(MG_Backend::BackendObject* backend, EGLDisplay dpy, EGLSurface draw,
EGLSurface read, EGLContext ctx);
// N-3: forget the tuple ApplyMakeCurrent last bound. Apply thread only, like the tuple
// itself (the forwarders that call these run their Args::Run there). Called on every
@@ -281,8 +285,8 @@ namespace MobileGL::MG_Remote::Server {
// (NoServerWindow). The ServerOwned arm of the dispatch is its production caller; a test
// reaches it through RunProbeOnApplyThreadForTesting. The lease is held until the backend
// has let go of the window: ServerDisplay::Detach's lost hook (D6) or the session's end.
MobileGLResult AcquireServerWindow(Uint32 width, Uint32 height, Uint32 timeoutMs,
ServerWindowLease* out, SurfaceRefusalCode* refusal);
MobileGLResult AcquireServerWindow(Uint32 width, Uint32 height, Uint32 timeoutMs, ServerWindowLease* out,
SurfaceRefusalCode* refusal);
// APPLY THREAD ONLY: this session's surface mode (D4).
SessionSurfaceMode SurfaceMode() const { return m_surfaceMode; }
// How many times this loop released a lost server window and latched ServerWindowLost (D6) -
@@ -396,8 +400,8 @@ namespace MobileGL::MG_Remote::Server {
// The dispatch, on the apply thread (or inline for a re-entrant post): executes the
// frame's op against the private backend and fills the frame's reply half. The eleven
// wire+inproc op bodies from the old forwarder Args structs live here now.
MobileGLResult ApplySurfaceControlFrame(SurfaceControlFrame& frame,
ControlProbeHook probeHook = nullptr, void* probeUser = nullptr);
MobileGLResult ApplySurfaceControlFrame(SurfaceControlFrame& frame, ControlProbeHook probeHook = nullptr,
void* probeUser = nullptr);
// The caller already holds m_callerMutex; shared by normal and test-probe posting so
// the probe's metadata is protected for the same lifetime as its frame.
MobileGLResult PostSurfaceControlFrameWithCallerLock(SurfaceControlFrame& frame);
@@ -515,10 +519,14 @@ namespace MobileGL::MG_Remote::Server {
// (the native bind itself needs the joint lane). Given the tuple currently held on the apply
// thread and the request, is this a native bind, an identical no-op repeat, or a client
// release-current the server must record without forwarding?
enum class EglBindAction { NativeBind, RepeatNoOp, ClientRelease };
EglBindAction ClassifyEglMakeCurrent(Bool haveCurrent, EGLDisplay curDpy, EGLSurface curDraw,
EGLSurface curRead, EGLContext curCtx, EGLDisplay dpy,
EGLSurface draw, EGLSurface read, EGLContext ctx);
enum class EglBindAction {
NativeBind,
RepeatNoOp,
ClientRelease
};
EglBindAction ClassifyEglMakeCurrent(Bool haveCurrent, EGLDisplay curDpy, EGLSurface curDraw, EGLSurface curRead,
EGLContext curCtx, EGLDisplay dpy, EGLSurface draw, EGLSurface read,
EGLContext ctx);
// P11 B1 (CONTRACT-P11 B1): the cpu core this process's `auto` policy reserves for a dialled-in
// shared-segment client's apply thread (ApplyThreadPolicy.h ReservedApplyCoreForConfig over this
File diff suppressed because it is too large Load Diff
+192 -175
View File
@@ -109,8 +109,7 @@ namespace MobileGL::MG_Remote::Server {
// belong in shm, never here), so one stack-free vector sized from PeekFrameSize is
// the whole reader. The BUFFER_TOO_SMALL half of ReceiveFrame's contract is what
// makes the two-step safe: a short buffer leaves the message queued.
MobileGLResult ReceiveEnvelope(Transport::ITransport& transport, std::vector<Uint8>& out,
Uint32 timeoutMs) {
MobileGLResult ReceiveEnvelope(Transport::ITransport& transport, std::vector<Uint8>& out, Uint32 timeoutMs) {
Uint64 size = 0;
MobileGLMutableByteSpan empty{nullptr, 0};
const MobileGLResult probe = transport.ReceiveFrame(empty, &size, timeoutMs);
@@ -124,10 +123,8 @@ namespace MobileGL::MG_Remote::Server {
return transport.ReceiveFrame(span, &size, 0);
}
MobileGLResult SendEnvelope(Transport::ITransport& transport,
::flatbuffers::FlatBufferBuilder& builder) {
return transport.SendFrame(
MobileGLByteSpan{builder.GetBufferPointer(), builder.GetSize()});
MobileGLResult SendEnvelope(Transport::ITransport& transport, ::flatbuffers::FlatBufferBuilder& builder) {
return transport.SendFrame(MobileGLByteSpan{builder.GetBufferPointer(), builder.GetSize()});
}
// Every message from the peer is verified before a single field is read: the control
@@ -181,16 +178,16 @@ namespace MobileGL::MG_Remote::Server {
// SetCapabilityBits / SetConsumedSubsystems for why the one that used to be here was
// the phase's marquee defect committed from the server's side.
[[noreturn]] void FatalUnsetCallMask(Bool capBitsSet, Bool consumedSet) {
SessionFail(MGFatalFamily::UnsetCallMask, "MGPipe: Fatal{UnsetCallMask} - %s%s%s was never set on this ServerSession, "
"and there is no default: a guessed consumer mask makes the client's R-8 "
"liveness gates answer from a server-side fact the server never stated. The "
"client would stop emitting whole record families, clear its dirty flags on "
"acceptance anyway, and the lane would go green with the uploads lost "
"(ID-39, reflected). Call SetConsumedSubsystems() and SetCapabilityBits() "
"before Accept(); SetCapabilityBits(0) is a legitimate explicit answer",
capBitsSet ? "" : "SetCapabilityBits",
(!capBitsSet && !consumedSet) ? " and " : "",
consumedSet ? "" : "SetConsumedSubsystems");
SessionFail(MGFatalFamily::UnsetCallMask,
"MGPipe: Fatal{UnsetCallMask} - %s%s%s was never set on this ServerSession, "
"and there is no default: a guessed consumer mask makes the client's R-8 "
"liveness gates answer from a server-side fact the server never stated. The "
"client would stop emitting whole record families, clear its dirty flags on "
"acceptance anyway, and the lane would go green with the uploads lost "
"(ID-39, reflected). Call SetConsumedSubsystems() and SetCapabilityBits() "
"before Accept(); SetCapabilityBits(0) is a legitimate explicit answer",
capBitsSet ? "" : "SetCapabilityBits", (!capBitsSet && !consumedSet) ? " and " : "",
consumedSet ? "" : "SetConsumedSubsystems");
}
// ---- P5c ev: the reverse channel's PRODUCER callbacks (CONTRACT-P5C §4.1) --------
@@ -208,11 +205,12 @@ namespace MobileGL::MG_Remote::Server {
// a Reserve that returns nullptr is Fatal{EventRingOverflow}. CountDrop and the
// eventRingFull latch stay built and stay unused-by-policy; P9 owns the policy.
[[noreturn]] void FatalEventRingOverflow(const char* eventName, Uint64 payloadBytes) {
SessionFail(MGFatalFamily::EventRingOverflow, "MGPipe: Fatal{EventRingOverflow} - the producer of %s could not reserve "
"%llu bytes on SEG_EVENT. P5c's events are lossless and a full ring under "
"lockstep is a producer burst no measured workload has, so this is a "
"defect, not a drop (CONTRACT-P5C §4.4; the drop policy is P9's)",
eventName, static_cast<unsigned long long>(payloadBytes));
SessionFail(MGFatalFamily::EventRingOverflow,
"MGPipe: Fatal{EventRingOverflow} - the producer of %s could not reserve "
"%llu bytes on SEG_EVENT. P5c's events are lossless and a full ring under "
"lockstep is a producer burst no measured workload has, so this is a "
"defect, not a drop (CONTRACT-P5C §4.4; the drop policy is P9's)",
eventName, static_cast<unsigned long long>(payloadBytes));
}
// With run-ahead the client drains SEG_EVENT only at its waits, so a full event ring is an
@@ -240,13 +238,13 @@ namespace MobileGL::MG_Remote::Server {
}
Uint32 MillisecondsSince(std::chrono::steady_clock::time_point start) {
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start).count();
const auto elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
return elapsed <= 0 ? 0u : static_cast<Uint32>(elapsed);
}
void* ReserveEventOrBlock(ServerSession& session, Transport::EventKind kind,
const char* eventName, Uint64 payloadBytes) {
void* ReserveEventOrBlock(ServerSession& session, Transport::EventKind kind, const char* eventName,
Uint64 payloadBytes) {
// FORFEITED: every event after the first drop is a drop too, at once - no Reserve and
// no second wait. The session is on its way down, and the peer has already shown it
// does not read this ring.
@@ -260,28 +258,25 @@ namespace MobileGL::MG_Remote::Server {
// be a hang with a comment on it.
const Uint64 cap = session.Events().Ring().MaxRecordBytes();
if (payloadBytes + sizeof(Transport::RingRecordHeader) > cap) {
SessionFail(MGFatalFamily::EventRingOverflow, "MGPipe: Fatal{EventRingOverflow} - %s needs %llu bytes and SEG_EVENT "
"caps ONE record at %llu (half its capacity). Flow control cannot help: "
"no amount of draining makes a record fit a ring that is too small for "
"it. SEG_EVENT's size is fixed (SessionSegmentSizes::EventRingBytes; the "
"MOBILEGL_IPC_EVENT_KB knob P5e proposed was never built), so slice the "
"event at its producer, as the writeback path already does",
eventName, static_cast<unsigned long long>(payloadBytes),
static_cast<unsigned long long>(cap));
SessionFail(MGFatalFamily::EventRingOverflow,
"MGPipe: Fatal{EventRingOverflow} - %s needs %llu bytes and SEG_EVENT "
"caps ONE record at %llu (half its capacity). Flow control cannot help: "
"no amount of draining makes a record fit a ring that is too small for "
"it. SEG_EVENT's size is fixed (SessionSegmentSizes::EventRingBytes; the "
"MOBILEGL_IPC_EVENT_KB knob P5e proposed was never built), so slice the "
"event at its producer, as the writeback path already does",
eventName, static_cast<unsigned long long>(payloadBytes),
static_cast<unsigned long long>(cap));
}
const auto signals = session.DataLink()->Signals();
Transport::Doorbell& bell = session.DataLink()->ConsumerBell();
const auto drained = [signals] {
return signals.EventRingFull->load(std::memory_order_acquire) == 0;
};
const auto drained = [signals] { return signals.EventRingFull->load(std::memory_order_acquire) == 0; };
// THE WAIT ALSO ENDS ON A STOP (PH-6 fix round). ServerLoop::Stop() raises the session's
// request and rings this same bell; a predicate that asked only "drained?" swallowed
// that ring and parked again for the rest of the knob, so any knob over Stop()'s 5000 ms
// join turned an ordinary end of the control stream into Fatal{ApplyThreadJoinTimeout}.
const auto drainedOrStopping = [&drained, &session] {
return drained() || session.ApplyStopRequested();
};
const auto drainedOrStopping = [&drained, &session] { return drained() || session.ApplyStopRequested(); };
// ONE DEADLINE FOR THE WHOLE RESERVATION, NOT ONE PER PARK. The loop goes round as
// often as the client clears the latch - that is how a client that drains in pieces
// eventually makes room - but every round spends the same budget, so a peer that
@@ -299,13 +294,13 @@ namespace MobileGL::MG_Remote::Server {
const auto outOfPatience = [&]() {
return shortDrains == 0
? session.ForfeitReverseChannel("NotDraining", "the client did not drain SEG_EVENT",
eventName, payloadBytes, MillisecondsSince(start),
budgetMs, shortDrains)
eventName, payloadBytes, MillisecondsSince(start), budgetMs,
shortDrains)
: session.ForfeitReverseChannel("TooSlow",
"the client drained SEG_EVENT but never made room for "
"this record",
eventName, payloadBytes, MillisecondsSince(start),
budgetMs, shortDrains);
eventName, payloadBytes, MillisecondsSince(start), budgetMs,
shortDrains);
};
// Asked AFTER Dead() everywhere below: under spawn a stop usually FOLLOWS the peer's
// hangup (control EOF), and the hangup is the more specific answer. The same fact can
@@ -341,7 +336,8 @@ namespace MobileGL::MG_Remote::Server {
if (!drained()) {
if (session.ApplyStopRequested()) return stopped();
const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(
deadline - std::chrono::steady_clock::now()).count();
deadline - std::chrono::steady_clock::now())
.count();
if (left <= 0) return outOfPatience();
if (!bell.Wait(*signals.ConsumerParked, drainedOrStopping, /*spinUs=*/0,
static_cast<Uint32>(left))) {
@@ -386,8 +382,10 @@ namespace MobileGL::MG_Remote::Server {
// @Ph-declined (ID-P7-1): returns ServerSession& - there is no session to hand
// back and no honest "declined" reference, and the input is the backend's own
// reverse call, not a peer's bytes. Stays Fatal.
SessionFail(MGFatalFamily::RoleViolation, "MGPipe: Fatal{RoleViolation, \"%s.session-missing\"} - "
"a reverse callback has no accepted session owner", callback);
SessionFail(MGFatalFamily::RoleViolation,
"MGPipe: Fatal{RoleViolation, \"%s.session-missing\"} - "
"a reverse callback has no accepted session owner",
callback);
}
return *session;
}
@@ -407,8 +405,7 @@ namespace MobileGL::MG_Remote::Server {
return session->Events().Ring().Capacity();
}
void ServerOnBufferWriteback(MG_Pipe::MGPipeHandle res, Uint64 offset,
MG_Pipe::MGPBlobRef bytes) {
void ServerOnBufferWriteback(MG_Pipe::MGPipeHandle res, Uint64 offset, MG_Pipe::MGPBlobRef bytes) {
ServerSession& session = ReverseCallbackOwner("OnBufferWriteback");
if (bytes.Seg != MG_Pipe::kMGHostSpanSegNone) {
// The backend handed over a segment-tagged blobref. The ONLY legal shape at
@@ -417,15 +414,16 @@ namespace MobileGL::MG_Remote::Server {
// @Ph-declined (ID-P7-1): the blobref is the BACKEND's (a reverse callback with
// a void signature and no caller that could act on a decline), and it rides the
// ServerSession& above; not a peer's bytes. Stays Fatal.
SessionFail(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"OnBufferWriteback.Seg\"} - the "
"writeback producer was handed Seg %u; at the backend boundary the "
"blobref names the backend's own mapped bytes (Seg = "
"kMGHostSpanSegNone) and the copy into SEG_EVENT is the producer's",
bytes.Seg);
SessionFail(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"OnBufferWriteback.Seg\"} - the "
"writeback producer was handed Seg %u; at the backend boundary the "
"blobref names the backend's own mapped bytes (Seg = "
"kMGHostSpanSegNone) and the copy into SEG_EVENT is the producer's",
bytes.Seg);
}
const Uint64 payloadBytes = sizeof(Transport::EventBufferWritebackHead) + bytes.Size;
void* slot = ReserveEventOrBlock(session, Transport::kEventBufferWriteback,
"kEventBufferWriteback", payloadBytes);
void* slot =
ReserveEventOrBlock(session, Transport::kEventBufferWriteback, "kEventBufferWriteback", payloadBytes);
Transport::EventBufferWritebackHead head{};
head.Resource = Transport::EventHandle{res.Slot, res.Gen};
head.Offset = offset;
@@ -440,13 +438,11 @@ namespace MobileGL::MG_Remote::Server {
session.PublishEvents();
}
void ServerOnGpuWritten(MG_Pipe::MGPipeHandle res, Uint rangeCount,
const MG_Pipe::MGPRange* ranges) {
void ServerOnGpuWritten(MG_Pipe::MGPipeHandle res, Uint rangeCount, const MG_Pipe::MGPRange* ranges) {
ServerSession& session = ReverseCallbackOwner("OnGpuWritten");
const Uint64 tailBytes = static_cast<Uint64>(rangeCount) * sizeof(Transport::EventRange);
const Uint64 payloadBytes = sizeof(Transport::EventGpuWrittenHead) + tailBytes;
void* slot = ReserveEventOrBlock(session, Transport::kEventGpuWritten,
"kEventGpuWritten", payloadBytes);
void* slot = ReserveEventOrBlock(session, Transport::kEventGpuWritten, "kEventGpuWritten", payloadBytes);
Transport::EventGpuWrittenHead head{};
head.Resource = Transport::EventHandle{res.Slot, res.Gen};
head.RangeCount = static_cast<std::uint32_t>(rangeCount);
@@ -454,8 +450,7 @@ namespace MobileGL::MG_Remote::Server {
if (tailBytes != 0) {
// EventRange and MGPRange are the same two Uint64s (EventRing.h asserts it at
// EventRange's declaration), so the tail is a plain copy rather than a per-element conversion.
std::memcpy(static_cast<Uint8*>(slot) + sizeof(head), ranges,
static_cast<SizeT>(tailBytes));
std::memcpy(static_cast<Uint8*>(slot) + sizeof(head), ranges, static_cast<SizeT>(tailBytes));
}
session.PublishEvents();
}
@@ -463,8 +458,8 @@ namespace MobileGL::MG_Remote::Server {
void ServerOnSurfaceChanged(const MG_Pipe::MGPSurfaceInfo* info) {
ServerSession& session = ReverseCallbackOwner("OnSurfaceChanged");
constexpr Uint64 payloadBytes = sizeof(Transport::EventSurfaceChangedHead);
void* slot = ReserveEventOrBlock(session, Transport::kEventSurfaceChanged,
"kEventSurfaceChanged", payloadBytes);
void* slot =
ReserveEventOrBlock(session, Transport::kEventSurfaceChanged, "kEventSurfaceChanged", payloadBytes);
Transport::EventSurfaceChangedHead head{};
if (info != nullptr) {
head.Width = info->Width;
@@ -492,10 +487,11 @@ namespace MobileGL::MG_Remote::Server {
template <typename Fn>
void InstallReverseCallback(Fn& entry, Fn producer) {
if (entry != nullptr && entry != producer) {
SessionFail(MGFatalFamily::RoleViolation, "MGPipe: Fatal{RoleViolation, \"callback-double-install\"} - a reverse "
"MGPipeCallbacks entry is already claimed by a different function. With "
"an active transport the four reverse entries are the server "
"session's producers; the client installs them under monolith only");
SessionFail(MGFatalFamily::RoleViolation,
"MGPipe: Fatal{RoleViolation, \"callback-double-install\"} - a reverse "
"MGPipeCallbacks entry is already claimed by a different function. With "
"an active transport the four reverse entries are the server "
"session's producers; the client installs them under monolith only");
}
entry = producer;
}
@@ -518,7 +514,9 @@ namespace MobileGL::MG_Remote::Server {
} // namespace
Uint32 ServerSessionCount() { return g_liveSessions.load(std::memory_order_acquire); }
Uint32 ServerSessionCount() {
return g_liveSessions.load(std::memory_order_acquire);
}
ServerSession& ServerSessionInstance() {
if (ServerSession* const own = Detail::CurrentSession()) return *own;
@@ -536,10 +534,13 @@ namespace MobileGL::MG_Remote::Server {
ServerSession::ServerSession() {
m_link = Transport::CreateSharedLink(Transport::TransportRoleTag::ServerConsumer);
m_commands = &m_link->CommandsIn(); m_events = &m_link->EventsOut();
m_commands = &m_link->CommandsIn();
m_events = &m_link->EventsOut();
}
ServerSession::~ServerSession() { Close(); }
ServerSession::~ServerSession() {
Close();
}
void ServerSession::SetSegmentSizes(const Transport::SessionSegmentSizes& sizes) {
if (m_accepted) {
@@ -551,7 +552,9 @@ namespace MobileGL::MG_Remote::Server {
m_sizesSet = true;
}
void ServerSession::SetBackend(MG_Backend::BackendObject* backend) { m_backend = backend; }
void ServerSession::SetBackend(MG_Backend::BackendObject* backend) {
m_backend = backend;
}
void ServerSession::SetCapabilityBits(Uint64 capBits) {
m_capBits = capBits;
@@ -563,7 +566,9 @@ namespace MobileGL::MG_Remote::Server {
m_consumedSet = true;
}
Bool ServerSession::CallMaskIsSet() const { return m_capBitsSet && m_consumedSet; }
Bool ServerSession::CallMaskIsSet() const {
return m_capBitsSet && m_consumedSet;
}
Uint64 ServerSession::CallMask() const {
if (!CallMaskIsSet()) {
@@ -572,10 +577,11 @@ namespace MobileGL::MG_Remote::Server {
return m_capBits | MGCapsConsumerBits(m_consumedSubsystems);
}
Bool ServerSession::Accepted() const { return m_accepted; }
Bool ServerSession::Accepted() const {
return m_accepted;
}
MobileGLResult ServerSession::Accept(Transport::ITransport& transport,
const std::vector<Uint8>* firstFrame) {
MobileGLResult ServerSession::Accept(Transport::ITransport& transport, const std::vector<Uint8>* firstFrame) {
// P14 S2: A SERVED SESSION DOES NOT CLAIM THE PROCESS TABLE, because the table is SHARED.
// Its entries are the same five functions for every session and they route by the calling
// thread (ServerSession::Active()), so a second session installing them is the idempotent
@@ -586,19 +592,20 @@ namespace MobileGL::MG_Remote::Server {
// Fatal{callback-double-install} the single-session ruling asks for.
const Bool sharedTable = Detail::CurrentSession() == this;
ServerSession* expectedOwner = nullptr;
if (!sharedTable && !g_sessionOwner.compare_exchange_strong(expectedOwner, this,
std::memory_order_acq_rel, std::memory_order_acquire)) {
if (!sharedTable && !g_sessionOwner.compare_exchange_strong(expectedOwner, this, std::memory_order_acq_rel,
std::memory_order_acquire)) {
if (expectedOwner == this) return MOBILEGL_ERR_INVALID_ARGUMENT;
SessionFail(MGFatalFamily::RoleViolation, "MGPipe: Fatal{RoleViolation, \"callback-double-install\"} - another "
"ServerSession already owns or is accepting the process reverse channel");
SessionFail(MGFatalFamily::RoleViolation,
"MGPipe: Fatal{RoleViolation, \"callback-double-install\"} - another "
"ServerSession already owns or is accepting the process reverse channel");
}
struct ReleaseFailedClaim {
ServerSession* self;
~ReleaseFailedClaim() {
if (!self->Accepted()) {
ServerSession* expected = self;
g_sessionOwner.compare_exchange_strong(expected, nullptr,
std::memory_order_release, std::memory_order_relaxed);
g_sessionOwner.compare_exchange_strong(expected, nullptr, std::memory_order_release,
std::memory_order_relaxed);
}
}
} releaseOnFailure{this};
@@ -617,11 +624,10 @@ namespace MobileGL::MG_Remote::Server {
// shared memory exists. Its whole purpose is to refuse to interpret the peer's bytes.
std::vector<Uint8> frame;
if (firstFrame != nullptr) frame = *firstFrame;
const MobileGLResult received = firstFrame != nullptr ? MOBILEGL_OK
: ReceiveEnvelope(transport, frame, kHandshakeTimeoutMs);
const MobileGLResult received =
firstFrame != nullptr ? MOBILEGL_OK : ReceiveEnvelope(transport, frame, kHandshakeTimeoutMs);
if (received != MOBILEGL_OK) {
MGLOG_E("MG_Remote server: no Hello within %u ms (rc=%d)", kHandshakeTimeoutMs,
static_cast<int>(received));
MGLOG_E("MG_Remote server: no Hello within %u ms (rc=%d)", kHandshakeTimeoutMs, static_cast<int>(received));
return received;
}
const ::MobileGL::Wire::CtrlEnvelope* envelope = ParseEnvelope(frame);
@@ -637,8 +643,7 @@ namespace MobileGL::MG_Remote::Server {
"first control frame is not a verifiable Hello");
}
const ::MobileGL::Wire::Hello* hello = envelope->msg_as_Hello();
const char* theirStamp =
hello->buildFingerprint() == nullptr ? nullptr : hello->buildFingerprint()->c_str();
const char* theirStamp = hello->buildFingerprint() == nullptr ? nullptr : hello->buildFingerprint()->c_str();
// PH-7 (1). One policy, one constant-time comparison, shared with ServerMain's supervisor
// and its session child (Handshake.h AuthenticatePeerToken). This site used std::strcmp,
@@ -656,15 +661,14 @@ namespace MobileGL::MG_Remote::Server {
if (hello->dialMode() != ::MobileGL::Wire::DialMode::No &&
hello->dialMode() != ::MobileGL::Wire::DialMode::Fork &&
hello->dialMode() != ::MobileGL::Wire::DialMode::Connect)
return RefuseHandshake(transport, ::MobileGL::Wire::RefuseCode::MalformedHello,
"unknown dial mode");
return RefuseHandshake(transport, ::MobileGL::Wire::RefuseCode::MalformedHello, "unknown dial mode");
// P11 B1: remembered for the apply thread's `auto` policy (ApplyThreadPolicy.h).
m_peer = hello->dialMode() == ::MobileGL::Wire::DialMode::Connect ? ApplyPeer::Connected
: hello->dialMode() == ::MobileGL::Wire::DialMode::Fork ? ApplyPeer::Forked
: ApplyPeer::InProcess;
m_clientPid = hello->pid();
const MobileGLResult compatible = ValidatePeerHandshake(transport, hello->abiMajor(),
hello->abiMinor(), hello->wireFingerprint(), theirStamp, hello->dialMode());
const MobileGLResult compatible = ValidatePeerHandshake(
transport, hello->abiMajor(), hello->abiMinor(), hello->wireFingerprint(), theirStamp, hello->dialMode());
if (compatible != MOBILEGL_OK) return compatible;
const Uint64 ourFingerprint = WireFingerprint();
const auto* terms = hello->linkTerms();
@@ -680,9 +684,9 @@ namespace MobileGL::MG_Remote::Server {
"stream requires a data connection");
if (hello->backendType() >= static_cast<Uint32>(BackendType::BackendTypeCount) ||
(m_backend != nullptr && hello->backendType() != static_cast<Uint32>(m_backend->GetBackendType())))
return RefuseHandshake(transport, ::MobileGL::Wire::RefuseCode::Backend,
"backend request differs from server", m_backend == nullptr ? 0 :
static_cast<Uint32>(m_backend->GetBackendType()), hello->backendType());
return RefuseHandshake(
transport, ::MobileGL::Wire::RefuseCode::Backend, "backend request differs from server",
m_backend == nullptr ? 0 : static_cast<Uint32>(m_backend->GetBackendType()), hello->backendType());
// ---- P11 A1: this side's adopt tier, from its own knob and the plane the Hello asked
// for, before a byte of Welcome. A stream refuses T0/T1 by name and serves T2; over
@@ -767,7 +771,6 @@ namespace MobileGL::MG_Remote::Server {
m_replies = ReplyPool();
m_replies.SetLink(m_link.get());
m_consumer.SetLink(m_link->Capabilities().PublishIsDelivery ? nullptr : m_link.get());
m_replies.SetLink(m_link.get());
m_applier = PipeApplier(&m_segments, &m_replies);
@@ -777,8 +780,7 @@ namespace MobileGL::MG_Remote::Server {
::flatbuffers::FlatBufferBuilder builder(1024);
using Slot = Transport::SessionSegmentSlot;
const auto segmentRef = [&](Uint32 id, ::MobileGL::Wire::SegmentKind kind, Slot slot) {
return ::MobileGL::Wire::CreateSegmentRefDirect(builder, id, kind,
m_link->Memory().AnnouncedSize(slot),
return ::MobileGL::Wire::CreateSegmentRefDirect(builder, id, kind, m_link->Memory().AnnouncedSize(slot),
m_link->Memory().AnnouncedName(slot));
};
auto cmd = segmentRef(1, ::MobileGL::Wire::SegmentKind::Cmd, Slot::Cmd);
@@ -786,9 +788,8 @@ namespace MobileGL::MG_Remote::Server {
auto reply = segmentRef(3, ::MobileGL::Wire::SegmentKind::Reply, Slot::Reply);
auto event = segmentRef(4, ::MobileGL::Wire::SegmentKind::Event, Slot::Event);
auto stamp = builder.CreateString(BuildFingerprint());
const auto nonce = pendingStream != nullptr
? builder.CreateVector(dataNonce, sizeof(dataNonce))
: ::flatbuffers::Offset<::flatbuffers::Vector<Uint8>>();
const auto nonce = pendingStream != nullptr ? builder.CreateVector(dataNonce, sizeof(dataNonce))
: ::flatbuffers::Offset<::flatbuffers::Vector<Uint8>>();
const Uint64 maxReply = m_sizes.ReplyBytes / m_sizes.ReplySlotCount - sizeof(Transport::ReplySlotHeader);
const Uint64 cmdWindow = m_link->Memory().CmdRingCapacity();
const Uint64 stageWindow = m_link->Memory().StageBytes();
@@ -808,14 +809,12 @@ namespace MobileGL::MG_Remote::Server {
static_cast<unsigned long long>(terms->stageWindowBytes()),
static_cast<unsigned long long>(terms->eventWindowBytes()),
static_cast<unsigned long long>(terms->maxReplyBytes()),
static_cast<unsigned long long>(cmdWindow),
static_cast<unsigned long long>(stageWindow),
static_cast<unsigned long long>(eventWindow),
static_cast<unsigned long long>(maxReply));
static_cast<unsigned long long>(cmdWindow), static_cast<unsigned long long>(stageWindow),
static_cast<unsigned long long>(eventWindow), static_cast<unsigned long long>(maxReply));
}
auto negotiated = ::MobileGL::Wire::CreateLinkTerms(builder, terms->dataPlane(),
::MobileGL::Wire::WireForm::StructImage, maxReply, cmdWindow, stageWindow, eventWindow,
static_cast<Uint8>(m_adoptT0Asked ? m_adoptAsk : 2));
auto negotiated = ::MobileGL::Wire::CreateLinkTerms(
builder, terms->dataPlane(), ::MobileGL::Wire::WireForm::StructImage, maxReply, cmdWindow, stageWindow,
eventWindow, static_cast<Uint8>(m_adoptT0Asked ? m_adoptAsk : 2));
auto welcome = ::MobileGL::Wire::CreateWelcome(
builder, MOBILEGL_PROTOCOL_ABI_MAJOR, MOBILEGL_PROTOCOL_ABI_MINOR,
// CONTRACT-P6 4.3: THE SERVER'S OWN PID, not an echo of the client's.
@@ -825,12 +824,11 @@ namespace MobileGL::MG_Remote::Server {
// this is its natural carrier: under spawn it is the one value in the handshake
// that a same-process session cannot produce, because there the two pids are
// equal by construction.
static_cast<Uint32>(SelfProcessId()),
cmd, stage, reply, event, stamp, ourFingerprint, ourFingerprint, negotiated,
m_backend == nullptr ? hello->backendType() : static_cast<Uint32>(m_backend->GetBackendType()),
nonce);
auto root = ::MobileGL::Wire::CreateCtrlEnvelope(
builder, ::MobileGL::Wire::CtrlMsg::Welcome, welcome.Union());
static_cast<Uint32>(SelfProcessId()), cmd, stage, reply, event, stamp, ourFingerprint, ourFingerprint,
negotiated,
m_backend == nullptr ? hello->backendType() : static_cast<Uint32>(m_backend->GetBackendType()), nonce);
auto root =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::Welcome, welcome.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, root);
const MobileGLResult sent = SendEnvelope(transport, builder);
if (sent != MOBILEGL_OK) {
@@ -873,11 +871,9 @@ namespace MobileGL::MG_Remote::Server {
// by definition not monolith, so finding one of them claimed here is the double
// installation the check exists to name.
InstallReverseCallback(MG_Pipe::gMGPipeCallbacks.OnGlError, &ServerOnGlError);
InstallReverseCallback(MG_Pipe::gMGPipeCallbacks.OnBufferWriteback,
&ServerOnBufferWriteback);
InstallReverseCallback(MG_Pipe::gMGPipeCallbacks.OnBufferWriteback, &ServerOnBufferWriteback);
InstallReverseCallback(MG_Pipe::gMGPipeCallbacks.OnGpuWritten, &ServerOnGpuWritten);
InstallReverseCallback(MG_Pipe::gMGPipeCallbacks.OnSurfaceChanged,
&ServerOnSurfaceChanged);
InstallReverseCallback(MG_Pipe::gMGPipeCallbacks.OnSurfaceChanged, &ServerOnSurfaceChanged);
// ... and, beside them, the one NUMBER a writeback producer has to know: how much of
// SEG_EVENT one record may be. Same ownership rule as the four entries above - installed
// here, released at Close only if it is still ours - because a producer left holding a
@@ -948,8 +944,7 @@ namespace MobileGL::MG_Remote::Server {
callMask &= ~static_cast<Uint64>(MG_Pipe::kCapTimerQuery);
::flatbuffers::FlatBufferBuilder builder(4096);
auto dynamicVector =
builder.CreateVector(dynamicBytes, static_cast<::flatbuffers::uoffset_t>(sizeof(dynamic)));
auto dynamicVector = builder.CreateVector(dynamicBytes, static_cast<::flatbuffers::uoffset_t>(sizeof(dynamic)));
auto rendererVector = builder.CreateVector(renderer.data(), renderer.size());
auto formatsVector = builder.CreateVector(formats.data(), formats.size());
// The client reads this back through BackendObject_Remote::GetBackendAPIVersionString()
@@ -960,11 +955,11 @@ namespace MobileGL::MG_Remote::Server {
// answered "Espryt (MobileGL Core) (<real GLES renderer>, OpenGL ES <major>.<minor>)",
// i.e. it dropped the only place the real device was named.
auto apiVersion = builder.CreateString(m_backend->GetBackendAPIVersionString());
auto snapshot = ::MobileGL::Wire::CreateCapsSnapshot(
builder, dynamicVector, rendererVector, formatsVector, /*extensions=*/0, apiVersion,
callMask, static_cast<Uint32>(m_backend->GetBackendType()));
auto root = ::MobileGL::Wire::CreateCtrlEnvelope(
builder, ::MobileGL::Wire::CtrlMsg::CapsSnapshot, snapshot.Union());
auto snapshot = ::MobileGL::Wire::CreateCapsSnapshot(builder, dynamicVector, rendererVector, formatsVector,
/*extensions=*/0, apiVersion, callMask,
static_cast<Uint32>(m_backend->GetBackendType()));
auto root =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::CapsSnapshot, snapshot.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, root);
return SendEnvelope(*m_transport, builder);
}
@@ -978,16 +973,18 @@ namespace MobileGL::MG_Remote::Server {
// cannot end the session it failed to join. What ends the wait is the deadline or the
// control peer going away; the deadline is refused on the control connection, where the
// client is listening.
MobileGLResult ServerSession::BindDataConnection(Transport::ITransport& control,
Transport::ILink& link, const Uint8* nonce) {
MobileGLResult ServerSession::BindDataConnection(Transport::ITransport& control, Transport::ILink& link,
const Uint8* nonce) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(kHandshakeTimeoutMs);
Uint32 refused = 0;
for (;;) {
const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(
deadline - std::chrono::steady_clock::now()).count();
const auto left =
std::chrono::duration_cast<std::chrono::milliseconds>(deadline - std::chrono::steady_clock::now())
.count();
if (left <= 0) {
MGLOG_E("MG_Remote server: no data connection presented this session's nonce within "
"%u ms (%u refused)", kHandshakeTimeoutMs, refused);
"%u ms (%u refused)",
kHandshakeTimeoutMs, refused);
return RefuseHandshake(control, ::MobileGL::Wire::RefuseCode::Authentication,
"no data connection presented the session nonce");
}
@@ -1017,7 +1014,8 @@ namespace MobileGL::MG_Remote::Server {
void ServerSession::AttachDataLink(std::unique_ptr<Transport::ILink> link) {
m_link = std::move(link);
m_commands = &m_link->CommandsIn(); m_events = &m_link->EventsOut();
m_commands = &m_link->CommandsIn();
m_events = &m_link->EventsOut();
m_consumer.SetLink(m_link->Capabilities().PublishIsDelivery ? nullptr : m_link.get());
m_replies.SetLink(m_link.get());
if (m_link) SetExternalDoorbells(&m_link->ConsumerBell(), &m_link->ProducerBell());
@@ -1033,8 +1031,7 @@ namespace MobileGL::MG_Remote::Server {
if (m_closed) return;
m_closed = true;
ServerSession* expectedActive = this;
g_active.compare_exchange_strong(expectedActive, nullptr,
std::memory_order_acq_rel, std::memory_order_acquire);
g_active.compare_exchange_strong(expectedActive, nullptr, std::memory_order_acq_rel, std::memory_order_acquire);
if (m_accepted) {
// P14 S2: DROP THIS SESSION FIRST, THEN ASK WHETHER ANY IS LEFT. The two process-wide
// hooks below (the segment resolver and the reverse-channel table) are shared by every
@@ -1071,14 +1068,13 @@ namespace MobileGL::MG_Remote::Server {
// A default ask that ended on T2 says its totals at MGLOG_D, like its refusal.
const bool quiet = m_adoptT0Quiet && !m_adoptT0;
const char* ran = m_adoptT0 ? "ran T0" : m_adoptT0Settled ? "not granted (T2)" : "no native bind (T2)";
#define MGL_T0_SERVER_TOTALS(LOG) \
LOG("MG_Remote server: T0 session totals - %s%s; stores imported %llu, bytes %llu, declined %llu, " \
"refused %llu, stale offers %llu, malformed %llu, unclaimed %zu", \
ran, m_adoptT0Quiet ? " (default ask)" : "", static_cast<unsigned long long>(m_t0Stores), \
static_cast<unsigned long long>(m_t0Bytes), static_cast<unsigned long long>(m_t0Declined), \
static_cast<unsigned long long>(m_t0Refusals), \
static_cast<unsigned long long>(m_adoptInbox.StaleOffers()), \
static_cast<unsigned long long>(m_adoptInbox.MalformedOffers()), m_adoptInbox.Kept())
#define MGL_T0_SERVER_TOTALS(LOG) \
LOG("MG_Remote server: T0 session totals - %s%s; stores imported %llu, bytes %llu, declined %llu, " \
"refused %llu, stale offers %llu, malformed %llu, unclaimed %zu", \
ran, m_adoptT0Quiet ? " (default ask)" : "", static_cast<unsigned long long>(m_t0Stores), \
static_cast<unsigned long long>(m_t0Bytes), static_cast<unsigned long long>(m_t0Declined), \
static_cast<unsigned long long>(m_t0Refusals), static_cast<unsigned long long>(m_adoptInbox.StaleOffers()), \
static_cast<unsigned long long>(m_adoptInbox.MalformedOffers()), m_adoptInbox.Kept())
if (quiet) {
MGL_T0_SERVER_TOTALS(MGLOG_D);
} else {
@@ -1097,14 +1093,16 @@ namespace MobileGL::MG_Remote::Server {
*m_events = Transport::EventRingProducer();
m_replies = ReplyPool();
m_link = Transport::CreateSharedLink(Transport::TransportRoleTag::ServerConsumer);
m_commands = &m_link->CommandsIn(); m_events = &m_link->EventsOut();
m_externalConsumerBell = nullptr; m_externalProducerBell = nullptr;
m_commands = &m_link->CommandsIn();
m_events = &m_link->EventsOut();
m_externalConsumerBell = nullptr;
m_externalProducerBell = nullptr;
m_link->Memory().Close();
m_transport = nullptr;
m_accepted = false;
ServerSession* expectedOwner = this;
g_sessionOwner.compare_exchange_strong(expectedOwner, nullptr,
std::memory_order_release, std::memory_order_relaxed);
g_sessionOwner.compare_exchange_strong(expectedOwner, nullptr, std::memory_order_release,
std::memory_order_relaxed);
}
// ---- P11 B2: T0 (docs/archive/contracts/CONTRACT-P11.md B2) ------------------------------------------
@@ -1127,8 +1125,9 @@ namespace MobileGL::MG_Remote::Server {
void ServerSession::SettleAdoptT0AtBind() {
if (!m_accepted || !m_adoptT0Asked || m_adoptT0Settled) return;
m_adoptT0Settled = true;
const char* backendName = m_backend == nullptr ? "server"
: m_backend->GetBackendType() == BackendType::DirectVulkan ? "Magma" : "Espryt";
const char* backendName = m_backend == nullptr ? "server"
: m_backend->GetBackendType() == BackendType::DirectVulkan ? "Magma"
: "Espryt";
std::string why;
// A CLIENT IN THIS PROCESS SHARES THE SERVER'S MEMORY ALREADY: its persistent maps are the
// stores' own mappings, donated as monolith donates them, and no AHardwareBuffer, import
@@ -1177,15 +1176,15 @@ namespace MobileGL::MG_Remote::Server {
}
if (!why.empty()) {
++m_t0Refusals;
const char* word = MG_Config::Ipc.AllowAdoptT0 == 0 ? "disallowed"
const char* word = MG_Config::Ipc.AllowAdoptT0 == 0 ? "disallowed"
: inProcess ? "in process"
: !Transport::AdoptT0::PlatformHasAhb() ? "no AHardwareBuffer"
: "POST";
if (m_adoptT0Quiet) {
// THE DEFAULT ASK (MOBILEGL_IPC_ADOPT_TIER unset) FALLS BACK QUIETLY: every host
// session would otherwise carry this line. Counted (T0Refusals, the totals line).
MGLOG_D("MG_Remote server: T0 by default not granted (%s) - %s; the session runs T2",
word, why.c_str());
MGLOG_D("MG_Remote server: T0 by default not granted (%s) - %s; the session runs T2", word,
why.c_str());
return;
}
MGLOG_W("MG_Remote server: Refuse{AdoptT0Unavailable, \"%s\"} - the client asked T0 "
@@ -1226,8 +1225,7 @@ namespace MobileGL::MG_Remote::Server {
int hop = -1;
Transport::AdoptT0::Offer offer{};
std::string why;
const AdoptInbox::Outcome outcome =
m_adoptInbox.Take(seq, Transport::AdoptT0::kOfferWaitMs, &hop, &offer, why);
const AdoptInbox::Outcome outcome = m_adoptInbox.Take(seq, Transport::AdoptT0::kOfferWaitMs, &hop, &offer, why);
const auto closeHop = [&] {
#if !defined(_WIN32)
if (hop >= 0) ::close(hop);
@@ -1336,27 +1334,35 @@ namespace MobileGL::MG_Remote::Server {
return true;
}
Transport::RingConsumer& ServerSession::CommandRing() { return *m_commands; }
Transport::RingConsumer& ServerSession::CommandRing() {
return *m_commands;
}
Transport::RingControl& ServerSession::Control() {
Transport::RingControl* control = m_link->Memory().CmdControl();
if (control == nullptr) {
// @Ph-declined (ID-P7-1): returns RingControl& - no honest reference exists before
// Accept() mapped SEG_CMD, and this is a call-order invariant, not a peer's bytes.
SessionFail(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"ServerSession::Control\"} - the control "
"page does not exist until Accept() has mapped SEG_CMD");
SessionFail(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"ServerSession::Control\"} - the control "
"page does not exist until Accept() has mapped SEG_CMD");
}
return *control;
}
Wire::SegmentTable& ServerSession::Segments() { return m_segments; }
PipeApplier& ServerSession::Applier() { return m_applier; }
ReplyPool& ServerSession::Replies() { return m_replies; }
Wire::SegmentTable& ServerSession::Segments() {
return m_segments;
}
PipeApplier& ServerSession::Applier() {
return m_applier;
}
ReplyPool& ServerSession::Replies() {
return m_replies;
}
// The bell the apply thread parks on. On the server endpoint of an InProcessTransport that
// is SelfDoorbell(); the client reaches the same bell through its own PeerDoorbell().
void ServerSession::SetExternalDoorbells(Transport::Doorbell* consumer,
Transport::Doorbell* producer) {
void ServerSession::SetExternalDoorbells(Transport::Doorbell* consumer, Transport::Doorbell* producer) {
m_externalConsumerBell = consumer;
m_externalProducerBell = producer;
}
@@ -1370,8 +1376,9 @@ namespace MobileGL::MG_Remote::Server {
if (m_transport == nullptr) {
// @Ph-declined (ID-P7-1): returns Doorbell& - no bell exists to hand back before
// Accept(); a call-order invariant, not a peer's bytes.
SessionFail(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"ServerSession::ConsumerDoorbell\"} - no "
"transport; Accept() has not run");
SessionFail(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"ServerSession::ConsumerDoorbell\"} - no "
"transport; Accept() has not run");
}
if (m_transport->Role() == Transport::TransportRole::InProcess) {
return static_cast<Transport::InProcessTransport*>(m_transport)->SelfDoorbell();
@@ -1381,9 +1388,10 @@ namespace MobileGL::MG_Remote::Server {
// every transport has to invent a home for.
// @Ph-declined (ID-P7-1): returns Doorbell& for a transport role this build wires no
// pair for - a build-shape fact, not a peer's bytes, and no reference to decline with.
SessionFail(MGFatalFamily::UnmigratedVerb, "MGPipe: Fatal{UnmigratedVerb, \"ServerSession::ConsumerDoorbell\"} - transport "
"role %u has no doorbell pair yet; that is P6's SocketTransport",
static_cast<unsigned>(m_transport->Role()));
SessionFail(MGFatalFamily::UnmigratedVerb,
"MGPipe: Fatal{UnmigratedVerb, \"ServerSession::ConsumerDoorbell\"} - transport "
"role %u has no doorbell pair yet; that is P6's SocketTransport",
static_cast<unsigned>(m_transport->Role()));
}
Transport::Doorbell& ServerSession::ProducerDoorbell() {
@@ -1392,22 +1400,32 @@ namespace MobileGL::MG_Remote::Server {
}
if (m_transport == nullptr) {
// @Ph-declined (ID-P7-1): returns Doorbell& - as ConsumerDoorbell above.
SessionFail(MGFatalFamily::ProtocolCorruption, "MGPipe: Fatal{ProtocolCorruption, \"ServerSession::ProducerDoorbell\"} - no "
"transport; Accept() has not run");
SessionFail(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, \"ServerSession::ProducerDoorbell\"} - no "
"transport; Accept() has not run");
}
if (m_transport->Role() == Transport::TransportRole::InProcess) {
return static_cast<Transport::InProcessTransport*>(m_transport)->PeerDoorbell();
}
// @Ph-declined (ID-P7-1): returns Doorbell& - as ConsumerDoorbell above.
SessionFail(MGFatalFamily::UnmigratedVerb, "MGPipe: Fatal{UnmigratedVerb, \"ServerSession::ProducerDoorbell\"} - transport "
"role %u has no doorbell pair yet; that is P6's SocketTransport",
static_cast<unsigned>(m_transport->Role()));
SessionFail(MGFatalFamily::UnmigratedVerb,
"MGPipe: Fatal{UnmigratedVerb, \"ServerSession::ProducerDoorbell\"} - transport "
"role %u has no doorbell pair yet; that is P6's SocketTransport",
static_cast<unsigned>(m_transport->Role()));
}
Transport::SessionSegments& ServerSession::Shm() { return m_link->Memory(); }
Transport::SessionConsumer& ServerSession::Consumer() { return m_consumer; }
Transport::EventRingProducer& ServerSession::Events() { return *m_events; }
Transport::ITransport* ServerSession::Control_Plane() { return m_transport; }
Transport::SessionSegments& ServerSession::Shm() {
return m_link->Memory();
}
Transport::SessionConsumer& ServerSession::Consumer() {
return m_consumer;
}
Transport::EventRingProducer& ServerSession::Events() {
return *m_events;
}
Transport::ITransport* ServerSession::Control_Plane() {
return m_transport;
}
void ServerSession::PublishEvents() {
if (!m_accepted) {
@@ -1438,8 +1456,7 @@ namespace MobileGL::MG_Remote::Server {
}
const Uint32 messageBytes = static_cast<Uint32>(length) + 1;
const Uint64 payloadBytes = sizeof(Transport::EventGlErrorHead) + messageBytes;
void* slot = ReserveEventOrBlock(*this, Transport::kEventGlError, "kEventGlError",
payloadBytes);
void* slot = ReserveEventOrBlock(*this, Transport::kEventGlError, "kEventGlError", payloadBytes);
Transport::EventGlErrorHead head{};
head.Code = code;
head.MessageBytes = messageBytes;
+6 -12
View File
@@ -58,8 +58,7 @@ namespace MobileGL::MG_Remote::Server {
// CapsSnapshot. The ABI assertion (CapsCodec.h) happens HERE, before a single record is
// decoded: sizeof(DynamicBackendParameters), sizeof(MGPCaps), sizeof(GLFunctionsTable)
// and the build fingerprint must match, and a mismatch is Fatal{AbiMismatch}.
MobileGLResult Accept(Transport::ITransport& transport,
const std::vector<Uint8>* firstFrame = nullptr);
MobileGLResult Accept(Transport::ITransport& transport, const std::vector<Uint8>* firstFrame = nullptr);
// PH-7 (4), ID-P7-3. WHERE A TCP SESSION'S DATA CONNECTION COMES FROM.
//
@@ -203,18 +202,15 @@ namespace MobileGL::MG_Remote::Server {
// into scratch; the apply loop sees the latch and stops, and ServerMain ends the session
// with status 0, not as a fault. The latch is written only on the apply thread (reset by
// Accept) and read with acquire elsewhere. See ARCHITECTURE.md §11.7.
Bool ReverseChannelForfeited() const {
return m_reverseChannelForfeit.load(std::memory_order_acquire);
}
Bool ReverseChannelForfeited() const { return m_reverseChannelForfeit.load(std::memory_order_acquire); }
// Drops since the latch went up, counted HERE because the shared page's eventDropped is
// on a page the peer can write.
Uint64 ForfeitDrops() const { return m_forfeitDrops.load(std::memory_order_acquire); }
// `cause` is one word (the braces' content); `why` the sentence; `shortDrains` how many
// times the client cleared the latch without making room. Returns the scratch the caller
// writes the dropped event into.
void* ForfeitReverseChannel(const char* cause, const char* why, const char* eventName,
Uint64 payloadBytes, Uint32 waitedMs, Uint32 budgetMs,
Uint32 shortDrains);
void* ForfeitReverseChannel(const char* cause, const char* why, const char* eventName, Uint64 payloadBytes,
Uint32 waitedMs, Uint32 budgetMs, Uint32 shortDrains);
void* DropEventAfterForfeit(Uint64 payloadBytes);
// PH-6 fix round. SERVERLOOP::STOP'S REQUEST, SEEN BY THE ONE APPLY-THREAD WAIT THAT IS NOT
@@ -271,8 +267,7 @@ namespace MobileGL::MG_Remote::Server {
// DECLINED with a named line).
// A client in this process (inproc) takes no AHardwareBuffer: the backend donates the
// store's own mapping and `inProcessBase` carries its address in the OK answer.
Bool AdoptStoreT0(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status,
Uint64& inProcessBase);
Bool AdoptStoreT0(const MG_Pipe::MGPHandleOnly& handle, Uint64 seq, Int32& status, Uint64& inProcessBase);
// T0 granted for a client in this process: map_persistent donates (kCapAdoptInProcess).
Bool AdoptT0InProcess() const { return m_adoptT0InProcess; }
// The arm's proof for this session (B2): stores imported and their bytes, declined.
@@ -335,8 +330,7 @@ namespace MobileGL::MG_Remote::Server {
Uint64 ContextBinds() const { return m_contextBinds; }
private:
MobileGLResult BindDataConnection(Transport::ITransport& control, Transport::ILink& link,
const Uint8* nonce);
MobileGLResult BindDataConnection(Transport::ITransport& control, Transport::ILink& link, const Uint8* nonce);
DataConnectionSource m_dataSource;
std::unique_ptr<Transport::ILink> m_link;
+33 -34
View File
@@ -18,17 +18,17 @@
#include <vector>
#if defined(__unix__) || defined(__APPLE__) || defined(__ANDROID__)
# define MOBILEGL_SERVER_SPAWN_POSIX 1
# include <dirent.h>
# include <fcntl.h>
# include <dlfcn.h>
# include <sys/socket.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <unistd.h>
#define MOBILEGL_SERVER_SPAWN_POSIX 1
#include <dirent.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#else
# define MOBILEGL_SERVER_SPAWN_POSIX 0
#define MOBILEGL_SERVER_SPAWN_POSIX 0
#endif
namespace MobileGL::MG_Remote::Server {
@@ -46,7 +46,7 @@ namespace MobileGL::MG_Remote::Server {
if (path.empty()) {
return false;
}
struct stat st {};
struct stat st{};
if (::stat(path.c_str(), &st) != 0) {
return false;
}
@@ -66,8 +66,7 @@ namespace MobileGL::MG_Remote::Server {
return configured;
}
Dl_info info{};
if (::dladdr(reinterpret_cast<void*>(&ResolveImage), &info) != 0 &&
info.dli_fname != nullptr) {
if (::dladdr(reinterpret_cast<void*>(&ResolveImage), &info) != 0 && info.dli_fname != nullptr) {
const std::string self(info.dli_fname);
const std::size_t slash = self.find_last_of('/');
if (slash != std::string::npos) {
@@ -86,8 +85,9 @@ namespace MobileGL::MG_Remote::Server {
// MOBILEGL_IPC_ADOPT_TIER is scrubbed: the client's ask travels in its Hello.
bool ShouldScrub(const char* entry) {
static constexpr const char* kServerOwned[] = {
"MOBILEGL_IPC_WIRE_DEFERRED_MB=", "MOBILEGL_IPC_SPIN_US=", "MOBILEGL_IPC_SERVER_AFFINITY=",
"MOBILEGL_IPC_AUDIT=", "MOBILEGL_IPC_EVENT_WAIT_MS=", "MOBILEGL_IPC_ALLOW_ADOPT_T0=",
"MOBILEGL_IPC_WIRE_DEFERRED_MB=", "MOBILEGL_IPC_SPIN_US=",
"MOBILEGL_IPC_SERVER_AFFINITY=", "MOBILEGL_IPC_AUDIT=",
"MOBILEGL_IPC_EVENT_WAIT_MS=", "MOBILEGL_IPC_ALLOW_ADOPT_T0=",
};
for (const char* kept : kServerOwned) {
if (std::strncmp(entry, kept, std::strlen(kept)) == 0) {
@@ -184,14 +184,12 @@ namespace MobileGL::MG_Remote::Server {
return children;
}
MobileGLResult LaunchServer(const std::string& imagePath, const std::string& endpoint,
LaunchedServer* out) {
MobileGLResult LaunchServer(const std::string& imagePath, const std::string& endpoint, LaunchedServer* out) {
return LaunchServerWithArgs(imagePath, endpoint, {}, out);
}
MobileGLResult LaunchServerWithArgs(const std::string& imagePath, const std::string& endpoint,
const std::vector<std::string>& extraArgs,
LaunchedServer* out) {
const std::vector<std::string>& extraArgs, LaunchedServer* out) {
if (out == nullptr || endpoint.empty()) {
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
@@ -202,27 +200,27 @@ namespace MobileGL::MG_Remote::Server {
// NAMED, never a fallback. ConfigLoader.cpp's own comment names the
// accident this prevents: a lane that asked for spawn, silently got
// monolith, and went green on the wrong arm.
Transport::WireLogError(
"MG_Remote spawn: the server image is not executable: \"%s\" - refusing. Set "
"MOBILEGL_IPC_SERVER_PATH, or place libMobileGLServer.so beside libMobileGL.so. "
"There is NO monolith fallback from here.",
image.empty() ? "<unresolved>" : image.c_str());
Transport::WireLogError("MG_Remote spawn: the server image is not executable: \"%s\" - refusing. Set "
"MOBILEGL_IPC_SERVER_PATH, or place libMobileGLServer.so beside libMobileGL.so. "
"There is NO monolith fallback from here.",
image.empty() ? "<unresolved>" : image.c_str());
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
// The image's directory, which is where its libMobileGL.so is.
const std::string::size_type slash = image.find_last_of('/');
ScrubbedEnv env =
BuildChildEnv(slash == std::string::npos ? std::string() : image.substr(0, slash));
ScrubbedEnv env = BuildChildEnv(slash == std::string::npos ? std::string() : image.substr(0, slash));
// Built before the fork: the child may only make async-signal-safe calls until execve.
std::vector<std::string> args;
args.reserve(2 + extraArgs.size());
args.push_back(image);
args.push_back(endpoint);
for (const std::string& arg : extraArgs) args.push_back(arg);
for (const std::string& arg : extraArgs)
args.push_back(arg);
std::vector<char*> argvStorage;
argvStorage.reserve(args.size() + 1);
for (std::string& arg : args) argvStorage.push_back(arg.data());
for (std::string& arg : args)
argvStorage.push_back(arg.data());
argvStorage.push_back(nullptr);
char** argv = argvStorage.data();
@@ -266,8 +264,8 @@ namespace MobileGL::MG_Remote::Server {
const ssize_t got = ::read(report[0], &childErrno, sizeof(childErrno));
::close(report[0]);
if (got == static_cast<ssize_t>(sizeof(childErrno))) {
Transport::WireLogError("MG_Remote spawn: execve(\"%s\") failed in the child: %s",
image.c_str(), std::strerror(childErrno));
Transport::WireLogError("MG_Remote spawn: execve(\"%s\") failed in the child: %s", image.c_str(),
std::strerror(childErrno));
int status = 0;
::waitpid(pid, &status, 0);
return MOBILEGL_ERR_INVALID_ARGUMENT;
@@ -281,8 +279,7 @@ namespace MobileGL::MG_Remote::Server {
return MOBILEGL_OK;
}
MobileGLResult ReapServer(LaunchedServer& server, std::uint32_t timeoutMs,
int* outExitCode) {
MobileGLResult ReapServer(LaunchedServer& server, std::uint32_t timeoutMs, int* outExitCode) {
if (server.pid < 0) {
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
@@ -318,7 +315,7 @@ namespace MobileGL::MG_Remote::Server {
MobileGLResult LaunchServer(const std::string&, const std::string&, LaunchedServer*) {
Transport::WireLogError("MG_Remote spawn: unsupported on this platform - P6 lands POSIX only "
"(CONTRACT-P6 §2.6); there is no fork() to build a second process from");
"(CONTRACT-P6 §2.6); there is no fork() to build a second process from");
return MOBILEGL_ERR_UNSUPPORTED;
}
MobileGLResult LaunchServerWithArgs(const std::string& imagePath, const std::string& endpoint,
@@ -328,7 +325,9 @@ namespace MobileGL::MG_Remote::Server {
MobileGLResult ReapServer(LaunchedServer&, std::uint32_t, int*) {
return MOBILEGL_ERR_UNSUPPORTED;
}
int CountOwnChildren() { return -1; }
int CountOwnChildren() {
return -1;
}
#endif
+2 -4
View File
@@ -51,16 +51,14 @@ namespace MobileGL::MG_Remote::Server {
// unresolvable path is a NAMED REFUSAL and never a monolith fallback -
// ConfigLoader.cpp names that accident and this is the code that must not
// repeat it.
MobileGLResult LaunchServer(const std::string& imagePath, const std::string& endpoint,
LaunchedServer* out);
MobileGLResult LaunchServer(const std::string& imagePath, const std::string& endpoint, LaunchedServer* out);
// The same launch with arguments after the endpoint - `--serve` for the fork-per-session
// supervisor a TCP deployment runs (ServerMain.cpp). PH-1 (4)'s control needs exactly that
// shape on a unit rig: a session that latches must leave a supervisor that serves the next
// connection, and a single-session server exits with its only session.
MobileGLResult LaunchServerWithArgs(const std::string& imagePath, const std::string& endpoint,
const std::vector<std::string>& extraArgs,
LaunchedServer* out);
const std::vector<std::string>& extraArgs, LaunchedServer* out);
// Waits for the child, up to `timeoutMs`. Returns MOBILEGL_ERR_TIMEOUT if it
// is still running, in which case the caller decides whether to escalate -
+12 -7
View File
@@ -77,8 +77,8 @@ namespace MobileGL::MG_Remote::Server {
SessionRuntime* runtime = t_runtime;
if (runtime == nullptr) return false;
outKey->SessionKey = reinterpret_cast<Uint64>(&runtime->Session());
outKey->ShareGroupKey = MG_Pipe::MGPipeApplierShareGroupKeyFor(
outKey->SessionKey, runtime->Session().CurrentContextToken());
outKey->ShareGroupKey =
MG_Pipe::MGPipeApplierShareGroupKeyFor(outKey->SessionKey, runtime->Session().CurrentContextToken());
return true;
}
@@ -106,7 +106,9 @@ namespace MobileGL::MG_Remote::Server {
MG_Record::SetStagedBucketResolver(&ResolveThreadStagedBucket);
}
SessionRuntime* Detail::CurrentRuntime() { return t_runtime; }
SessionRuntime* Detail::CurrentRuntime() {
return t_runtime;
}
ServerSession* Detail::CurrentSession() {
SessionRuntime* runtime = t_runtime;
@@ -118,7 +120,9 @@ namespace MobileGL::MG_Remote::Server {
return runtime != nullptr ? &runtime->Loop() : nullptr;
}
MG_Pipe::PipeInputs* Detail::CurrentInputs() { return ResolveThreadInputs(); }
MG_Pipe::PipeInputs* Detail::CurrentInputs() {
return ResolveThreadInputs();
}
Detail::ThreadSessionScope::ThreadSessionScope(SessionRuntime& runtime) {
InstallSessionRuntimeHooks();
@@ -141,8 +145,7 @@ namespace MobileGL::MG_Remote::Server {
t_runtime = m_previous;
}
SessionRuntime::SessionRuntime()
: m_latchDomain(FatalLatchDomainOpen()) {}
SessionRuntime::SessionRuntime() : m_latchDomain(FatalLatchDomainOpen()) {}
SessionRuntime::~SessionRuntime() {
// P14 S5: this runtime's session is the key every applier it registered was filed
@@ -156,6 +159,8 @@ namespace MobileGL::MG_Remote::Server {
m_latchDomain = 0;
}
Uint32 LiveServerSessionCount() { return ServerSessionCount(); }
Uint32 LiveServerSessionCount() {
return ServerSessionCount();
}
} // namespace MobileGL::MG_Remote::Server
@@ -64,7 +64,7 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
#if !defined(_WIN32)
Bool IdentityOf(int fd, Uint64* dev, Uint64* ino) {
struct stat st {};
struct stat st{};
if (::fstat(fd, &st) != 0) return false;
*dev = static_cast<Uint64>(st.st_dev);
*ino = static_cast<Uint64>(st.st_ino);
@@ -105,7 +105,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_RIGHTS) continue;
const SizeT n = (c->cmsg_len - CMSG_LEN(0)) / sizeof(int);
const int* in = reinterpret_cast<const int*>(CMSG_DATA(c));
for (SizeT i = 0; i < n; ++i) fds.push_back(in[i]);
for (SizeT i = 0; i < n; ++i)
fds.push_back(in[i]);
}
}
::close(pair[1]);
@@ -144,8 +145,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
// CPU usage also keeps the layout one the platform can lock plane by plane, which is how
// every layout question about it is answered (AHardwareBuffer_lockPlanes) - never by
// assuming one.
constexpr uint64_t kYuvUsage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
constexpr uint64_t kYuvUsage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
uint32_t AhbFormatOf(Uint32 fourcc) {
if (fourcc == kFourccNv12) return AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
@@ -218,7 +219,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
}
image.Native = ahb;
image.Fd = fds[0];
for (SizeT i = 1; i < fds.size(); ++i) ::close(fds[i]);
for (SizeT i = 1; i < fds.size(); ++i)
::close(fds[i]);
image.Offset = 0;
if (!FourccIsYuv(image.Fourcc)) {
image.Stride = described.stride * 4;
@@ -255,7 +257,9 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
// A host has no AHardwareBuffer. A memfd stands in for the dma-buf so the registry, the
// descriptor round trip and identification can be exercised by the unit tests; no backend
// binds one.
Bool YuvAllocatableNative(Uint32) { return true; }
Bool YuvAllocatableNative(Uint32) {
return true;
}
Bool AllocateNative(Image& image, std::string& why) {
Uint64 bytes = 0;
@@ -288,7 +292,9 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
void ReleaseNative(void*) {}
#else
Bool YuvAllocatableNative(Uint32) { return false; }
Bool YuvAllocatableNative(Uint32) {
return false;
}
Bool AllocateNative(Image&, std::string& why) {
why = "shared images need POSIX descriptors";
@@ -353,8 +359,7 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
void DmaBufSync(int fd, Bool start) {
dma_buf_sync sync{};
sync.flags = DMA_BUF_SYNC_READ | (start ? DMA_BUF_SYNC_START : DMA_BUF_SYNC_END);
while (::ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync) != 0 && (errno == EINTR || errno == EAGAIN)) {
}
while (::ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync) != 0 && (errno == EINTR || errno == EAGAIN)) {}
}
#endif
} // namespace
@@ -384,12 +389,15 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
}
if (Fd >= 0) ::close(Fd);
if (WriteFence >= 0) ::close(WriteFence);
for (const int fence : ReadFences) ::close(fence);
for (const int fence : ReadFences)
::close(fence);
#endif
ReleaseNative(Native);
}
Bool FourccIsYuv(Uint32 fourcc) { return fourcc == kFourccNv12 || fourcc == kFourccP010; }
Bool FourccIsYuv(Uint32 fourcc) {
return fourcc == kFourccNv12 || fourcc == kFourccP010;
}
Uint32 FourccYuvSampleBytes(Uint32 fourcc) {
if (fourcc == kFourccNv12) return 1;
@@ -414,7 +422,9 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
fourcc == kFourccXrgb8888 || YuvFourccAllocatable(fourcc);
}
Bool FourccIgnoresAlpha(Uint32 fourcc) { return fourcc == kFourccXbgr8888 || fourcc == kFourccXrgb8888; }
Bool FourccIgnoresAlpha(Uint32 fourcc) {
return fourcc == kFourccXbgr8888 || fourcc == kFourccXrgb8888;
}
namespace {
// Allocate's body. `foreign`, when given, is on the image before anything can find it.
@@ -457,8 +467,7 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
}
reg.byIdentity[key] = image;
if (image->Foreign != nullptr) {
reg.foreignByIdentity[std::make_pair(image->Foreign->IdentityDev, image->Foreign->IdentityIno)] =
image;
reg.foreignByIdentity[std::make_pair(image->Foreign->IdentityDev, image->Foreign->IdentityIno)] = image;
}
return image;
}
@@ -517,8 +526,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
const Uint64 lumaEnd = static_cast<Uint64>(layout.Offset[0]) +
static_cast<Uint64>(layout.Pitch[0]) * (height - 1) +
static_cast<Uint64>(width) * sampleBytes;
const Uint64 chromaEnd =
static_cast<Uint64>(layout.Offset[1]) + static_cast<Uint64>(layout.Pitch[1]) * (chromaHeight - 1) + chromaRow;
const Uint64 chromaEnd = static_cast<Uint64>(layout.Offset[1]) +
static_cast<Uint64>(layout.Pitch[1]) * (chromaHeight - 1) + chromaRow;
if (layout.Pitch[0] < width * sampleBytes || layout.Pitch[1] < chromaRow || lumaEnd > size ||
chromaEnd > size) {
why = Format("its planes do not fit the %lld-byte buffer", static_cast<long long>(size));
@@ -633,8 +642,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
const auto it = reg.byIdentity.find(std::make_pair(dev, ino));
ImageRef image = it != reg.byIdentity.end() ? it->second.lock() : nullptr;
if (image == nullptr) {
why = Format("the descriptor (dev %llu ino %llu) was not exported by this server", static_cast<long long>(dev),
static_cast<long long>(ino));
why = Format("the descriptor (dev %llu ino %llu) was not exported by this server",
static_cast<long long>(dev), static_cast<long long>(ino));
}
return image;
#endif
@@ -651,14 +660,17 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
Registry& reg = Reg();
const std::lock_guard<std::mutex> lock(reg.mutex);
SizeT live = 0;
for (const auto& [id, weak] : reg.byId) live += weak.expired() ? 0 : 1;
for (const auto& [id, weak] : reg.byId)
live += weak.expired() ? 0 : 1;
return live;
}
// ---- synchronisation ------------------------------------------------------------------------
namespace SyncFile {
Bool Signaled(int fd) { return Wait(fd, 0); }
Bool Signaled(int fd) {
return Wait(fd, 0);
}
Bool Wait(int fd, Uint32 timeoutMs) {
#if defined(_WIN32)
@@ -767,7 +779,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
}
void ReadTracker::PublishFrame(int fence) {
for (const auto& [id, image] : m_images) PublishRead(*image, Dup(fence));
for (const auto& [id, image] : m_images)
PublishRead(*image, Dup(fence));
CloseFd(fence);
m_images.clear();
}
+4 -3
View File
@@ -91,7 +91,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages::Yuv {
-cScale * 2.0 * kr * (1.0 - kr) / kg,
0.0,
};
for (SizeT i = 0; i < 9; ++i) c.Matrix[i] = static_cast<float>(m[i]);
for (SizeT i = 0; i < 9; ++i)
c.Matrix[i] = static_cast<float>(m[i]);
c.Offset[0] = static_cast<float>(yOffset);
c.Offset[1] = static_cast<float>(cOffset);
c.Offset[2] = static_cast<float>(cOffset);
@@ -130,8 +131,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages::Yuv {
std::memcpy(to, from, static_cast<SizeT>(width) * sampleBytes);
} else {
for (Uint32 x = 0; x < width; ++x)
std::memcpy(to + static_cast<SizeT>(x) * dst.Y.PixelStride, from + static_cast<SizeT>(x) * sampleBytes,
sampleBytes);
std::memcpy(to + static_cast<SizeT>(x) * dst.Y.PixelStride,
from + static_cast<SizeT>(x) * sampleBytes, sampleBytes);
}
}
// Chroma.
@@ -12,23 +12,40 @@ namespace MobileGL::MG_Remote::Server {
const char* SurfaceControlOpName(SurfaceControlOp op) {
switch (op) {
case SurfaceControlOp::None: return "None";
case SurfaceControlOp::InitializeDisplay: return "InitializeDisplay";
case SurfaceControlOp::CreateWindowSurface: return "CreateWindowSurface";
case SurfaceControlOp::CreatePbufferSurface: return "CreatePbufferSurface";
case SurfaceControlOp::ResizeWindowSurface: return "ResizeWindowSurface";
case SurfaceControlOp::ReleaseSurface: return "ReleaseSurface";
case SurfaceControlOp::MakeCurrent: return "MakeCurrent";
case SurfaceControlOp::ReleaseCurrent: return "ReleaseCurrent";
case SurfaceControlOp::SetSwapInterval: return "SetSwapInterval";
case SurfaceControlOp::ReleaseResources: return "ReleaseResources";
case SurfaceControlOp::SetWindowHandle: return "SetWindowHandle";
case SurfaceControlOp::InitCapabilities: return "InitCapabilities";
case SurfaceControlOp::CreateContext: return "CreateContext";
case SurfaceControlOp::DestroyContext: return "DestroyContext";
case SurfaceControlOp::SwapBuffersInprocOnly: return "SwapBuffersInprocOnly";
case SurfaceControlOp::InitWindowSurfaceInprocOnly: return "InitWindowSurfaceInprocOnly";
case SurfaceControlOp::ProbeForTesting: return "ProbeForTesting";
case SurfaceControlOp::None:
return "None";
case SurfaceControlOp::InitializeDisplay:
return "InitializeDisplay";
case SurfaceControlOp::CreateWindowSurface:
return "CreateWindowSurface";
case SurfaceControlOp::CreatePbufferSurface:
return "CreatePbufferSurface";
case SurfaceControlOp::ResizeWindowSurface:
return "ResizeWindowSurface";
case SurfaceControlOp::ReleaseSurface:
return "ReleaseSurface";
case SurfaceControlOp::MakeCurrent:
return "MakeCurrent";
case SurfaceControlOp::ReleaseCurrent:
return "ReleaseCurrent";
case SurfaceControlOp::SetSwapInterval:
return "SetSwapInterval";
case SurfaceControlOp::ReleaseResources:
return "ReleaseResources";
case SurfaceControlOp::SetWindowHandle:
return "SetWindowHandle";
case SurfaceControlOp::InitCapabilities:
return "InitCapabilities";
case SurfaceControlOp::CreateContext:
return "CreateContext";
case SurfaceControlOp::DestroyContext:
return "DestroyContext";
case SurfaceControlOp::SwapBuffersInprocOnly:
return "SwapBuffersInprocOnly";
case SurfaceControlOp::InitWindowSurfaceInprocOnly:
return "InitWindowSurfaceInprocOnly";
case SurfaceControlOp::ProbeForTesting:
return "ProbeForTesting";
}
return "<unknown SurfaceControlOp>";
}
@@ -62,11 +79,16 @@ namespace MobileGL::MG_Remote::Server {
const char* SurfaceRefusalCodeName(SurfaceRefusalCode code) {
switch (code) {
case SurfaceRefusalCode::None: return "None";
case SurfaceRefusalCode::NoServerDisplay: return "NoServerDisplay";
case SurfaceRefusalCode::NoServerWindow: return "NoServerWindow";
case SurfaceRefusalCode::SurfaceModeMismatch: return "SurfaceModeMismatch";
case SurfaceRefusalCode::ServerOwnedOnSetWindowHandle: return "ServerOwnedOnSetWindowHandle";
case SurfaceRefusalCode::None:
return "None";
case SurfaceRefusalCode::NoServerDisplay:
return "NoServerDisplay";
case SurfaceRefusalCode::NoServerWindow:
return "NoServerWindow";
case SurfaceRefusalCode::SurfaceModeMismatch:
return "SurfaceModeMismatch";
case SurfaceRefusalCode::ServerOwnedOnSetWindowHandle:
return "ServerOwnedOnSetWindowHandle";
}
return "<unknown SurfaceRefusalCode>";
}
@@ -62,9 +62,9 @@ namespace MobileGL::MG_Remote::Server {
// is safe by construction: these three never cross a process boundary and are named
// symbolically everywhere they are used, so nothing outside this build could have pinned
// them - where the eleven above and the two below are wire ABI and may never move.
SwapBuffersInprocOnly = 14, // present travels as a record (the class-B Present emitter)
InitWindowSurfaceInprocOnly = 15,// a client-side no-op; kept for the inproc test lane
ProbeForTesting = 16, // MG_Test's arbitrary-work seam through the same channel
SwapBuffersInprocOnly = 14, // present travels as a record (the class-B Present emitter)
InitWindowSurfaceInprocOnly = 15, // a client-side no-op; kept for the inproc test lane
ProbeForTesting = 16, // MG_Test's arbitrary-work seam through the same channel
};
// P12 (on-screen server window), D2. THE SERVER-OWNED WINDOW, AS A FRAME-LOCAL TAG.
@@ -151,16 +151,15 @@ namespace MobileGL::MG_Remote::Server {
SurfaceControlFrame frame{};
// Exhaustive by the language's aggregate decomposition rule: adding ANY member
// without extending this binding is a compile error, so new fields cannot evade it.
const auto& [kind, seq, display, surface, readSurface, context, windowBackend,
nativeToken, width, height, swapInterval, shareGroupToken, contextFlags, ok,
eglMajor, eglMinor, eventHead, refusal] = frame;
const auto& [kind, seq, display, surface, readSurface, context, windowBackend, nativeToken, width, height,
swapInterval, shareGroupToken, contextFlags, ok, eglMajor, eglMinor, eventHead, refusal] =
frame;
return IsSurfaceControlValue<decltype(kind)> && IsSurfaceControlValue<decltype(seq)> &&
IsSurfaceControlValue<decltype(display)> && IsSurfaceControlValue<decltype(surface)> &&
IsSurfaceControlValue<decltype(readSurface)> && IsSurfaceControlValue<decltype(context)> &&
IsSurfaceControlValue<decltype(windowBackend)> && IsSurfaceControlValue<decltype(nativeToken)> &&
IsSurfaceControlValue<decltype(width)> && IsSurfaceControlValue<decltype(height)> &&
IsSurfaceControlValue<decltype(swapInterval)> &&
IsSurfaceControlValue<decltype(shareGroupToken)> &&
IsSurfaceControlValue<decltype(swapInterval)> && IsSurfaceControlValue<decltype(shareGroupToken)> &&
IsSurfaceControlValue<decltype(contextFlags)> && IsSurfaceControlValue<decltype(ok)> &&
IsSurfaceControlValue<decltype(eglMajor)> && IsSurfaceControlValue<decltype(eglMinor)> &&
IsSurfaceControlValue<decltype(eventHead)> && IsSurfaceControlValue<decltype(refusal)>;
@@ -169,8 +168,7 @@ namespace MobileGL::MG_Remote::Server {
static_assert(Detail::SurfaceControlFrameHasOnlyValues(),
"every surface control frame member must be an integer or enum, never a pointer");
static_assert(std::is_trivially_copyable_v<SurfaceControlFrame> &&
std::is_standard_layout_v<SurfaceControlFrame>,
static_assert(std::is_trivially_copyable_v<SurfaceControlFrame> && std::is_standard_layout_v<SurfaceControlFrame>,
"the surface control frame must remain a trivially copyable value aggregate");
const char* SurfaceControlOpName(SurfaceControlOp op);
+23 -11
View File
@@ -34,7 +34,9 @@ namespace MobileGL::MG_Remote::Transport::AdoptT0 {
#if defined(__ANDROID__)
bool RealHasAhb() { return true; }
bool RealHasAhb() {
return true;
}
bool RealAllocateHeld(std::uint64_t size, const void* seed, HeldStore& out, std::string& why) {
out = HeldStore{};
@@ -82,8 +84,8 @@ namespace MobileGL::MG_Remote::Transport::AdoptT0 {
return -1;
}
if (store != nullptr && store->ahb != nullptr) {
const int sent = AHardwareBuffer_sendHandleToUnixSocket(
static_cast<const AHardwareBuffer*>(store->ahb), pair[0]);
const int sent =
AHardwareBuffer_sendHandleToUnixSocket(static_cast<const AHardwareBuffer*>(store->ahb), pair[0]);
if (sent != 0) {
why = Format("AHardwareBuffer_sendHandleToUnixSocket rc=%lld errno=%lld", sent, errno);
::close(pair[0]);
@@ -137,7 +139,9 @@ namespace MobileGL::MG_Remote::Transport::AdoptT0 {
// No AHardwareBuffer: the server never grants T0 here, so a client never allocates. The
// empty hop still works, so a flags-0 Offer is real on every POSIX host.
bool RealHasAhb() { return false; }
bool RealHasAhb() {
return false;
}
bool RealAllocateHeld(std::uint64_t, const void*, HeldStore& out, std::string& why) {
out = HeldStore{};
@@ -165,7 +169,9 @@ namespace MobileGL::MG_Remote::Transport::AdoptT0 {
#endif
}
void RealReleaseHeld(HeldStore& store) { store = HeldStore{}; }
void RealReleaseHeld(HeldStore& store) {
store = HeldStore{};
}
bool RealReceiveFromHop(int, std::uint64_t, void** outAhb, std::string& why) {
*outAhb = nullptr;
@@ -196,8 +202,10 @@ namespace MobileGL::MG_Remote::Transport::AdoptT0 {
void ReleaseHeld(HeldStore& store) {
const PlatformForTest* test = g_testPlatform.load(std::memory_order_acquire);
if (test != nullptr) test->ReleaseHeld(store);
else RealReleaseHeld(store);
if (test != nullptr)
test->ReleaseHeld(store);
else
RealReleaseHeld(store);
}
bool ReceiveFromHop(int hopFd, std::uint64_t size, void** outAhb, std::string& why) {
@@ -208,14 +216,18 @@ namespace MobileGL::MG_Remote::Transport::AdoptT0 {
void AcquireImported(void* ahb) {
const PlatformForTest* test = g_testPlatform.load(std::memory_order_acquire);
if (test != nullptr) test->AcquireImported(ahb);
else RealAcquireImported(ahb);
if (test != nullptr)
test->AcquireImported(ahb);
else
RealAcquireImported(ahb);
}
void ReleaseImported(void* ahb) {
const PlatformForTest* test = g_testPlatform.load(std::memory_order_acquire);
if (test != nullptr) test->ReleaseImported(ahb);
else RealReleaseImported(ahb);
if (test != nullptr)
test->ReleaseImported(ahb);
else
RealReleaseImported(ahb);
}
void SetPlatformForTest(const PlatformForTest* platform) {
+15 -5
View File
@@ -19,12 +19,18 @@ namespace MobileGL::MG_Remote::Transport {
namespace {
std::atomic<bool> g_settled{false};
const char* SideName(AdoptTierSide side) { return side == AdoptTierSide::Client ? "client" : "server"; }
const char* SideName(AdoptTierSide side) {
return side == AdoptTierSide::Client ? "client" : "server";
}
} // namespace
bool AdoptTierIsDefault() { return MG_Config::Ipc.AdoptTier == MG_Config::kAdoptTierUnset; }
bool AdoptTierIsDefault() {
return MG_Config::Ipc.AdoptTier == MG_Config::kAdoptTierUnset;
}
bool AdoptTierWantsT0() { return MG_Config::Ipc.AdoptTier == 0 || AdoptTierIsDefault(); }
bool AdoptTierWantsT0() {
return MG_Config::Ipc.AdoptTier == 0 || AdoptTierIsDefault();
}
std::uint32_t AdoptTierAskFor(bool streamDataPlane) {
if (streamDataPlane || !AdoptTierWantsT0()) return 2u;
@@ -81,8 +87,12 @@ namespace MobileGL::MG_Remote::Transport {
return AdoptTierAskFor(false);
}
bool AdoptTierSettledAtHandshake() { return g_settled.load(std::memory_order_acquire); }
bool AdoptTierSettledAtHandshake() {
return g_settled.load(std::memory_order_acquire);
}
void ForgetAdoptTierSettlementForTest() { g_settled.store(false, std::memory_order_release); }
void ForgetAdoptTierSettlementForTest() {
g_settled.store(false, std::memory_order_release);
}
} // namespace MobileGL::MG_Remote::Transport
+7 -2
View File
@@ -20,14 +20,19 @@
namespace MobileGL::MG_Remote::Transport {
enum class AdoptTierSide : std::uint8_t { Client, Server };
enum class AdoptTierSide : std::uint8_t {
Client,
Server
};
// THE HELLO'S T0 ASK WHEN THE KNOB IS UNSET (LinkTerms.adoptTier, protocol.fbs): T0 as asked
// by 0, except that the server names a refusal only at MGLOG_D. The ask travels because only
// the server knows why it did not grant T0 (its allow switch, its platform, its POST).
inline constexpr std::uint32_t kAdoptAskT0Default = 0x80;
// An ask that is a T0 ask (0 by name, kAdoptAskT0Default by default).
inline bool AdoptAskIsT0(std::uint32_t ask) { return ask == 0 || ask == kAdoptAskT0Default; }
inline bool AdoptAskIsT0(std::uint32_t ask) {
return ask == 0 || ask == kAdoptAskT0Default;
}
// This process's knob asks for T0 (0, or unset).
bool AdoptTierWantsT0();
// This process's knob is unset: T0 fallbacks are quiet.
+3 -6
View File
@@ -58,17 +58,14 @@ namespace MobileGL::MG_Remote::Transport {
// are folded into the accumulator rather than branched on, and nothing leaves early. The
// presented length does not extend the loop: a peer's own bytes are the one thing it already
// knows, and letting it choose the trip count would let it choose our work.
inline bool ConstantTimeTokenMatch(const char* expected, const char* presented,
std::size_t presentedSize) {
inline bool ConstantTimeTokenMatch(const char* expected, const char* presented, std::size_t presentedSize) {
if (expected == nullptr) return false;
const std::size_t expectedSize = std::strlen(expected);
const std::size_t width = expectedSize > kAuthTokenCompareBytes ? expectedSize : kAuthTokenCompareBytes;
std::size_t difference = expectedSize ^ presentedSize;
for (std::size_t index = 0; index < width; ++index) {
const unsigned char ours =
index < expectedSize ? static_cast<unsigned char>(expected[index]) : 0u;
const unsigned char theirs =
index < presentedSize ? static_cast<unsigned char>(presented[index]) : 0u;
const unsigned char ours = index < expectedSize ? static_cast<unsigned char>(expected[index]) : 0u;
const unsigned char theirs = index < presentedSize ? static_cast<unsigned char>(presented[index]) : 0u;
difference |= static_cast<unsigned char>(ours ^ theirs);
}
return difference == 0;
+10 -5
View File
@@ -15,7 +15,9 @@
namespace MobileGL::MG_Remote::Transport {
ControlInbox::ControlInbox(ITransport& transport) : m_transport(transport), m_reader([this] { Read(); }) {}
ControlInbox::~ControlInbox() { Stop(); }
ControlInbox::~ControlInbox() {
Stop();
}
void ControlInbox::Stop() {
{
@@ -53,7 +55,8 @@ namespace MobileGL::MG_Remote::Transport {
}
if (result == MOBILEGL_OK) {
flatbuffers::Verifier verifier(frame.data(), frame.size());
if (!::MobileGL::Wire::VerifyCtrlEnvelopeBuffer(verifier)) result = MOBILEGL_ERR_PROTOCOL_MISMATCH;
if (!::MobileGL::Wire::VerifyCtrlEnvelopeBuffer(verifier))
result = MOBILEGL_ERR_PROTOCOL_MISMATCH;
else {
const auto* envelope = ::MobileGL::Wire::GetCtrlEnvelope(frame.data());
if (const auto* log = envelope->msg_as_LogLine()) {
@@ -81,8 +84,10 @@ namespace MobileGL::MG_Remote::Transport {
MobileGLResult ControlInbox::Receive(std::vector<std::uint8_t>& frame, std::uint32_t timeoutMs) {
std::unique_lock<std::mutex> lock(m_mutex);
const auto ready = [&] { return m_stop || !m_frames.empty() || m_result != MOBILEGL_OK; };
if (timeoutMs == kWaitForever) m_cv.wait(lock, ready);
else if (!m_cv.wait_for(lock, std::chrono::milliseconds(timeoutMs), ready)) return MOBILEGL_ERR_TIMEOUT;
if (timeoutMs == kWaitForever)
m_cv.wait(lock, ready);
else if (!m_cv.wait_for(lock, std::chrono::milliseconds(timeoutMs), ready))
return MOBILEGL_ERR_TIMEOUT;
if (m_frames.empty()) return m_result == MOBILEGL_OK ? MOBILEGL_ERR_TRANSPORT_CLOSED : m_result;
frame = std::move(m_frames.front());
m_frames.pop_front();
@@ -108,4 +113,4 @@ namespace MobileGL::MG_Remote::Transport {
if (m_cv.wait_until(lock, deadline) == std::cv_status::timeout) return m_result != MOBILEGL_OK;
}
}
}
} // namespace MobileGL::MG_Remote::Transport
+2 -1
View File
@@ -31,6 +31,7 @@ namespace MobileGL::MG_Remote::Transport {
// control connection, the peer the server's log forward (LogForward.h) must not be held by.
// Frames already queued stay queued; resuming picks up where the socket is.
void PauseReadingForTest(bool paused);
private:
void Read();
ITransport& m_transport;
@@ -43,4 +44,4 @@ namespace MobileGL::MG_Remote::Transport {
MobileGLResult m_result = MOBILEGL_OK;
std::thread m_reader;
};
}
} // namespace MobileGL::MG_Remote::Transport
+14 -16
View File
@@ -108,15 +108,15 @@ namespace MobileGL::MG_Remote::Transport {
}
CpuRelax();
}
const auto elapsedNs = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - start)
.count();
const auto elapsedNs =
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now() - start)
.count();
if (elapsedNs <= 0) {
continue; // clock too coarse to see this probe at all
}
sawUsableClock = true;
const std::uint64_t perUs = (static_cast<std::uint64_t>(kProbeIterations) * 1000ull) /
static_cast<std::uint64_t>(elapsedNs);
const std::uint64_t perUs =
(static_cast<std::uint64_t>(kProbeIterations) * 1000ull) / static_cast<std::uint64_t>(elapsedNs);
const std::uint32_t clamped =
perUs > kMaxSpinItersPerUs ? kMaxSpinItersPerUs : static_cast<std::uint32_t>(perUs);
if (clamped > best) {
@@ -164,7 +164,9 @@ namespace MobileGL::MG_Remote::Transport {
CondVarDoorbell::CondVarDoorbell() : m_impl(new Impl()) {}
CondVarDoorbell::~CondVarDoorbell() { delete m_impl; }
CondVarDoorbell::~CondVarDoorbell() {
delete m_impl;
}
void CondVarDoorbell::Notify() {
{
@@ -189,9 +191,7 @@ namespace MobileGL::MG_Remote::Transport {
if (timeoutMs == 0) {
return false;
}
const auto woken = [this] {
return m_impl->signals != 0 || m_dead.load(std::memory_order_relaxed);
};
const auto woken = [this] { return m_impl->signals != 0 || m_dead.load(std::memory_order_relaxed); };
if (timeoutMs == kWaitForever) {
m_impl->cv.wait(lock, woken);
} else if (!m_impl->cv.wait_for(lock, std::chrono::milliseconds(timeoutMs), woken)) {
@@ -312,9 +312,9 @@ namespace MobileGL::MG_Remote::Transport {
for (;;) {
int pollTimeout = -1;
if (timeoutMs != kWaitForever) {
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count();
const auto elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start)
.count();
const long long remaining = static_cast<long long>(timeoutMs) - elapsed;
pollTimeout = remaining <= 0 ? 0 : static_cast<int>(remaining);
}
@@ -366,8 +366,7 @@ namespace MobileGL::MG_Remote::Transport {
// Doorbell::Wait re-parks until its deadline and kWaitForever has
// none.
if ((pfd.revents & (POLLERR | POLLNVAL)) != 0) {
MGLOG_D("MG_Remote doorbell: fd %d unusable (revents=0x%X)", m_fd,
static_cast<unsigned>(pfd.revents));
MGLOG_D("MG_Remote doorbell: fd %d unusable (revents=0x%X)", m_fd, static_cast<unsigned>(pfd.revents));
m_dead.store(true, std::memory_order_release);
return false;
}
@@ -389,8 +388,7 @@ namespace MobileGL::MG_Remote::Transport {
// Readiness with no bit we requested or recognise: there is
// nothing to consume and no way to make progress, so refuse to
// poll this descriptor again.
MGLOG_D("MG_Remote doorbell: fd %d ready with revents=0x%X", m_fd,
static_cast<unsigned>(pfd.revents));
MGLOG_D("MG_Remote doorbell: fd %d ready with revents=0x%X", m_fd, static_cast<unsigned>(pfd.revents));
m_dead.store(true, std::memory_order_release);
return false;
}
+6 -9
View File
@@ -131,8 +131,8 @@ namespace MobileGL::MG_Remote::Transport {
// caller gets parks that are a subset of its OWN waits off a bell it
// shares with another subsystem; see ParkEntries() above.
template <class Ready>
bool Wait(std::atomic<std::uint32_t>& parked, Ready&& ready, std::uint32_t spinUs,
std::uint32_t timeoutMs, std::atomic<std::uint64_t>* parkTally = nullptr) {
bool Wait(std::atomic<std::uint32_t>& parked, Ready&& ready, std::uint32_t spinUs, std::uint32_t timeoutMs,
std::atomic<std::uint64_t>* parkTally = nullptr) {
if (ready()) {
return true;
}
@@ -141,9 +141,7 @@ namespace MobileGL::MG_Remote::Transport {
// and skips the calibration probe: PipeWireCodec's SEG_STAGE retirement wait passes 0
// because it wants the park, and MOBILEGL_IPC_SPIN_US=0 is a no-spin control.
const std::uint64_t budget =
spinUs == 0 ? 0ull
: static_cast<std::uint64_t>(SpinItersPerUs()) *
static_cast<std::uint64_t>(spinUs);
spinUs == 0 ? 0ull : static_cast<std::uint64_t>(SpinItersPerUs()) * static_cast<std::uint64_t>(spinUs);
for (std::uint64_t spun = 0; spun < budget; ++spun) {
if (ready()) {
return true;
@@ -166,8 +164,7 @@ namespace MobileGL::MG_Remote::Transport {
// safe direction for a deadline whose expiry is reported as a timeout.
const auto deadline = timeoutMs == kWaitForever
? std::chrono::steady_clock::time_point::max()
: std::chrono::steady_clock::now() +
std::chrono::milliseconds(timeoutMs);
: std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
// ONE PARK PER Wait is what the ledger counts, and this loop may Park
// many times for one Wait; see the counting site below.
@@ -340,8 +337,8 @@ namespace MobileGL::MG_Remote::Transport {
// afterwards.
std::uint64_t Drain();
int m_fd; // polled
int m_notifyFd; // written; equals m_fd in the single-fd (cross-process) form
int m_fd; // polled
int m_notifyFd; // written; equals m_fd in the single-fd (cross-process) form
int m_witnessFd = -1; // polled for hangup only; NOT owned, NEVER read
// Set ONLY by the witness branch of Park, never by Kill or by our own EOF. See
// Doorbell::PeerHungUp for why the distinction is the whole design.
+10 -20
View File
@@ -122,10 +122,8 @@ namespace MobileGL::MG_Remote::Transport {
// `eventControl` is SEG_EVENT's own control page; `cmdControl` is the
// SEG_CMD page, because Ring.h declares eventRingFull / eventDropped
// there and the client's waits already look at it.
EventRingProducer(RingControl* eventControl, RingControl* cmdControl, void* base,
std::uint64_t capacityBytes)
: m_cmdControl(cmdControl),
m_producer(eventControl, base, capacityBytes, RingCursorSet::Cmd) {}
EventRingProducer(RingControl* eventControl, RingControl* cmdControl, void* base, std::uint64_t capacityBytes)
: m_cmdControl(cmdControl), m_producer(eventControl, base, capacityBytes, RingCursorSet::Cmd) {}
bool Valid() const { return m_producer.Valid() && m_cmdControl != nullptr; }
@@ -170,17 +168,14 @@ namespace MobileGL::MG_Remote::Transport {
public:
EventRingConsumer() = default;
EventRingConsumer(RingControl* eventControl, RingControl* cmdControl, void* base,
std::uint64_t capacityBytes, const void* segmentBase)
: m_cmdControl(cmdControl),
m_consumer(eventControl, base, capacityBytes, RingCursorSet::Cmd),
EventRingConsumer(RingControl* eventControl, RingControl* cmdControl, void* base, std::uint64_t capacityBytes,
const void* segmentBase)
: m_cmdControl(cmdControl), m_consumer(eventControl, base, capacityBytes, RingCursorSet::Cmd),
m_segmentBase(static_cast<const std::uint8_t*>(segmentBase)) {}
bool Valid() const { return m_consumer.Valid() && m_cmdControl != nullptr; }
bool Pop(RingRecordView& out, bool* outCorrupt = nullptr) {
return m_consumer.Pop(out, outCorrupt);
}
bool Pop(RingRecordView& out, bool* outCorrupt = nullptr) { return m_consumer.Pop(out, outCorrupt); }
// Release the bytes and clear the full latch. Only after the caller has
// finished with every payload pointer it popped: a writeback's bytes live
@@ -197,8 +192,7 @@ namespace MobileGL::MG_Remote::Transport {
// the forward direction's publish-then-ring order exists to prevent. The
// exchange is what makes the ring one-shot: only the drain that actually found
// the flag up rings, so a steady stream of drains costs one relaxed RMW.
const bool wasFull =
m_cmdControl->eventRingFull.exchange(0, std::memory_order_acq_rel) != 0;
const bool wasFull = m_cmdControl->eventRingFull.exchange(0, std::memory_order_acq_rel) != 0;
if (wasFull && m_serverBell != nullptr) {
NotifyIfParked(*m_serverBell, m_cmdControl->consumerParked);
}
@@ -215,18 +209,14 @@ namespace MobileGL::MG_Remote::Transport {
// OnBufferWriteback MGPBlobRef must carry (Seg = kSegEvent, Offset =
// this, Size = the head's Size). Never a host address - R-2's rule B.
std::uint64_t OffsetInSegment(const void* payload) const {
return static_cast<std::uint64_t>(static_cast<const std::uint8_t*>(payload) -
m_segmentBase);
return static_cast<std::uint64_t>(static_cast<const std::uint8_t*>(payload) - m_segmentBase);
}
std::uint64_t DroppedEvents() const {
return m_cmdControl == nullptr
? 0
: m_cmdControl->eventDropped.load(std::memory_order_relaxed);
return m_cmdControl == nullptr ? 0 : m_cmdControl->eventDropped.load(std::memory_order_relaxed);
}
bool RingIsFull() const {
return m_cmdControl != nullptr &&
m_cmdControl->eventRingFull.load(std::memory_order_acquire) != 0;
return m_cmdControl != nullptr && m_cmdControl->eventRingFull.load(std::memory_order_acquire) != 0;
}
RingConsumer& Ring() { return m_consumer; }
+23 -19
View File
@@ -34,11 +34,17 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
#if defined(_WIN32)
bool Supported() { return false; }
bool Supported() {
return false;
}
MobileGLResult CreateSocketPair(int[2]) { return MOBILEGL_ERR_UNSUPPORTED; }
MobileGLResult CreateSocketPair(int[2]) {
return MOBILEGL_ERR_UNSUPPORTED;
}
MobileGLResult SendFd(int, int, MobileGLByteSpan, bool) { return MOBILEGL_ERR_UNSUPPORTED; }
MobileGLResult SendFd(int, int, MobileGLByteSpan, bool) {
return MOBILEGL_ERR_UNSUPPORTED;
}
MobileGLResult ReceiveFd(int, int*, MobileGLMutableByteSpan, std::uint64_t*, std::uint32_t) {
return MOBILEGL_ERR_UNSUPPORTED;
@@ -60,9 +66,9 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
for (;;) {
int pollTimeout = -1;
if (timeoutMs != 0xFFFFFFFFu) {
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count();
const auto elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start)
.count();
const long long remaining = static_cast<long long>(timeoutMs) - elapsed;
pollTimeout = remaining <= 0 ? 0 : static_cast<int>(remaining);
}
@@ -78,7 +84,9 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
}
} // namespace
bool Supported() { return true; }
bool Supported() {
return true;
}
MobileGLResult CreateSocketPair(int outFds[2]) {
if (outFds == nullptr) {
@@ -119,8 +127,7 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
header.sidebandSize = static_cast<std::uint32_t>(sideband.size);
std::memcpy(payload, &header, sizeof(header));
if (sideband.size != 0) {
std::memcpy(payload + sizeof(header), sideband.data,
static_cast<std::size_t>(sideband.size));
std::memcpy(payload + sizeof(header), sideband.data, static_cast<std::size_t>(sideband.size));
}
const std::size_t payloadSize = sizeof(header) + static_cast<std::size_t>(sideband.size);
@@ -154,8 +161,7 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
if (sent >= 0) {
if (static_cast<std::size_t>(sent) != payloadSize) {
// A datagram socket sends all or nothing.
MGLOG_E("MG_Remote fd passing: short datagram (%zd of %zu bytes)", sent,
payloadSize);
MGLOG_E("MG_Remote fd passing: short datagram (%zd of %zu bytes)", sent, payloadSize);
return MOBILEGL_ERR_TRANSPORT_CLOSED;
}
return MOBILEGL_OK;
@@ -180,8 +186,8 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
}
}
MobileGLResult ReceiveFd(int socket, int* outFd, MobileGLMutableByteSpan sideband,
std::uint64_t* outSidebandSize, std::uint32_t timeoutMs) {
MobileGLResult ReceiveFd(int socket, int* outFd, MobileGLMutableByteSpan sideband, std::uint64_t* outSidebandSize,
std::uint32_t timeoutMs) {
if (socket < 0 || outFd == nullptr) {
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
@@ -254,8 +260,7 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
// rather than leaked, whatever else is wrong with the message.
int received[4];
int receivedCount = 0;
for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr;
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg != nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
continue;
}
@@ -308,11 +313,10 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
SidebandHeader header{};
std::memcpy(&header, payload, sizeof(header));
if (header.magic != kSidebandMagic ||
header.sidebandSize > kMaxSidebandBytes ||
if (header.magic != kSidebandMagic || header.sidebandSize > kMaxSidebandBytes ||
sizeof(SidebandHeader) + header.sidebandSize != static_cast<std::size_t>(got)) {
MGLOG_E("MG_Remote fd passing: bad sideband header (magic=0x%08X size=%u datagram=%zd)",
header.magic, header.sidebandSize, got);
MGLOG_E("MG_Remote fd passing: bad sideband header (magic=0x%08X size=%u datagram=%zd)", header.magic,
header.sidebandSize, got);
closeAll(-1);
return MOBILEGL_ERR_PROTOCOL_MISMATCH;
}
+2 -2
View File
@@ -57,7 +57,7 @@ namespace MobileGL::MG_Remote::Transport::FdPassing {
// On success *outFd owns a descriptor this process must close.
// MOBILEGL_ERR_TIMEOUT when nothing arrived (timeoutMs 0 = poll),
// MOBILEGL_ERR_TRANSPORT_CLOSED on peer close.
MobileGLResult ReceiveFd(int socket, int* outFd, MobileGLMutableByteSpan sideband,
std::uint64_t* outSidebandSize, std::uint32_t timeoutMs);
MobileGLResult ReceiveFd(int socket, int* outFd, MobileGLMutableByteSpan sideband, std::uint64_t* outSidebandSize,
std::uint32_t timeoutMs);
} // namespace MobileGL::MG_Remote::Transport::FdPassing
+4 -9
View File
@@ -40,13 +40,11 @@ namespace MobileGL::MG_Remote::Transport {
inline constexpr std::uint64_t kFrameReaderCompactThreshold = 64ull * 1024;
// Appends one framed message to `out`.
inline MobileGLResult AppendFrame(std::vector<std::uint8_t>& out, const void* payload,
std::uint64_t size) {
inline MobileGLResult AppendFrame(std::vector<std::uint8_t>& out, const void* payload, std::uint64_t size) {
if (size > kMaxFramePayloadSize) {
WireLogError("MG_Remote framing: refusing to send a %llu byte payload (cap %llu); "
"bulk bytes belong in shm",
static_cast<unsigned long long>(size),
static_cast<unsigned long long>(kMaxFramePayloadSize));
static_cast<unsigned long long>(size), static_cast<unsigned long long>(kMaxFramePayloadSize));
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
if (size != 0 && payload == nullptr) {
@@ -87,9 +85,7 @@ namespace MobileGL::MG_Remote::Transport {
bool Failed() const { return m_failed; }
bool HasMessage() const {
return !m_failed && m_haveHeader && Available() >= kFrameHeaderSize + m_pendingSize;
}
bool HasMessage() const { return !m_failed && m_haveHeader && Available() >= kFrameHeaderSize + m_pendingSize; }
// Size of the next complete message, or 0 when none is complete yet.
std::uint64_t PendingMessageSize() const { return HasMessage() ? m_pendingSize : 0; }
@@ -179,8 +175,7 @@ namespace MobileGL::MG_Remote::Transport {
m_buffer.clear();
m_readPos = 0;
} else if (m_readPos >= kFrameReaderCompactThreshold) {
m_buffer.erase(m_buffer.begin(),
m_buffer.begin() + static_cast<std::ptrdiff_t>(m_readPos));
m_buffer.erase(m_buffer.begin(), m_buffer.begin() + static_cast<std::ptrdiff_t>(m_readPos));
m_readPos = 0;
}
// Header of the next message may already be buffered.
+15 -18
View File
@@ -51,18 +51,18 @@ namespace MobileGL::MG_Remote::Transport {
// §11.2). The event flags stay out: the client clears eventRingFull on every drain, an RMW
// that would share the cache line the client's spin predicate reads for appliedSeq.
struct LinkProgress {
std::atomic<std::uint64_t> appliedSeq; // records applied
std::atomic<std::uint64_t> retiredSeq; // GPU finished
std::atomic<std::uint64_t> completedFrameSerial; // releases *RetiredTail / SEG_ADOPT
std::atomic<std::uint64_t> presentAckSerial; // returns present credit
std::atomic<std::uint64_t> appliedSeq; // records applied
std::atomic<std::uint64_t> retiredSeq; // GPU finished
std::atomic<std::uint64_t> completedFrameSerial; // releases *RetiredTail / SEG_ADOPT
std::atomic<std::uint64_t> presentAckSerial; // returns present credit
};
// The reverse channel's two flags. Separated from LinkProgress for the
// false-sharing reason above, not for a taxonomic one: both roles read
// eventRingFull in a park predicate, and both roles write it.
struct LinkEventFlags {
std::atomic<std::uint32_t> eventRingFull; // server latches, client clears
std::atomic<std::uint32_t> eventDropped; // server-only counter
std::atomic<std::uint32_t> eventRingFull; // server latches, client clears
std::atomic<std::uint32_t> eventDropped; // server-only counter
};
// ---- the producer's handle ----------------------------------------------
@@ -112,7 +112,7 @@ namespace MobileGL::MG_Remote::Transport {
// second clause is that a transport DECLARES what it supports and may not
// invent a third answer.
struct LinkCapabilities {
bool SharedMapping = false; // peers see one set of bytes; no copy on read
bool SharedMapping = false; // peers see one set of bytes; no copy on read
bool DescriptorPassing = false;
bool PublishIsDelivery = false; // false => Flush() is mandatory before any wait
std::uint64_t MaxRecordBytes = 0;
@@ -168,7 +168,6 @@ namespace MobileGL::MG_Remote::Transport {
virtual EventRingConsumer& EventsIn() = 0;
virtual void BindDoorbells(Doorbell*, Doorbell*) {}
// The watermark block. Taken once; every wait predicate reads through
// the returned pointer with no further virtual dispatch. Never null on
// an attached link.
@@ -190,8 +189,7 @@ namespace MobileGL::MG_Remote::Transport {
// Reserve `bytes` of staging and return where the record should name
// it. MOBILEGL_ERR_RING_OVERRUN when the request cannot be satisfied at
// any occupancy; the caller's existing back-pressure path is unchanged.
virtual MobileGLResult StageBytes(std::uint64_t bytes, LinkSpan* outSpan,
void** outWritePtr) = 0;
virtual MobileGLResult StageBytes(std::uint64_t bytes, LinkSpan* outSpan, void** outWritePtr) = 0;
// Resolve a span the peer named. On a shared mapping this is pointer
// arithmetic over the peer's segment; on a stream it points into the
@@ -203,15 +201,14 @@ namespace MobileGL::MG_Remote::Transport {
// progress transmission, send window, flush - did not name it, and
// without it a stream link has no way to answer the eleven
// ResolveOrFatal call sites in the decoder.
virtual MobileGLResult ResolveSpan(LinkSegment segment, LinkSpan span,
const void** outPtr) = 0;
virtual MobileGLResult ResolveSpan(LinkSegment segment, LinkSpan span, const void** outPtr) = 0;
// ---- replies --------------------------------------------------------
virtual MobileGLResult PostReply(std::uint64_t seq, std::int32_t status,
const void* payload, std::uint64_t size) = 0;
virtual MobileGLResult ReadReply(std::uint64_t seq, std::int32_t* outStatus,
const void** outPayload, std::uint64_t* outSize) = 0;
virtual MobileGLResult PostReply(std::uint64_t seq, std::int32_t status, const void* payload,
std::uint64_t size) = 0;
virtual MobileGLResult ReadReply(std::uint64_t seq, std::int32_t* outStatus, const void** outPayload,
std::uint64_t* outSize) = 0;
// P12: DECLARE THAT THIS SEQ'S ANSWER WILL BE READ, so that a link which can retain it
// does. Called by the producer side of the wire, in seq order, BEFORE the record is
@@ -285,8 +282,8 @@ namespace MobileGL::MG_Remote::Transport {
// Setup factories keep concrete link selection out of codec/session code.
std::unique_ptr<ILink> CreateSharedLink(TransportRoleTag role);
MobileGLResult CreateStreamLink(int fd, const SessionSegmentSizes& sizes,
TransportRoleTag role, std::unique_ptr<ILink>& out);
MobileGLResult CreateStreamLink(int fd, const SessionSegmentSizes& sizes, TransportRoleTag role,
std::unique_ptr<ILink>& out);
// PH-7 (4), ID-P7-3. The server's nonce-bound stream link, in the two steps
// StreamLink::AttachOwnedDeferred / BindDataFd describe: created with its owned memory and
// no descriptor (Welcome announces those sizes before the data connection exists), then
+2 -2
View File
@@ -80,8 +80,8 @@ namespace MobileGL::MG_Remote::Transport {
// be half-consumed, so the capacity is checked BEFORE anything is read
// and a short buffer returns MOBILEGL_ERR_BUFFER_TOO_SMALL with the
// required size, having consumed nothing and dropped no descriptor.
virtual MobileGLResult ReceiveFd(int* outFd, MobileGLMutableByteSpan sideband,
std::uint64_t* outSidebandSize, std::uint32_t timeoutMs) = 0;
virtual MobileGLResult ReceiveFd(int* outFd, MobileGLMutableByteSpan sideband, std::uint64_t* outSidebandSize,
std::uint32_t timeoutMs) = 0;
// ---- lifecycle ------------------------------------------------------
@@ -117,8 +117,7 @@ namespace MobileGL::MG_Remote::Transport {
// illegal the day the delivery mode changes to `spawn`.
if (bytes.size > kMaxFramePayloadSize) {
MGLOG_E("MG_Remote inproc: refusing a %llu byte message (cap %llu)",
static_cast<unsigned long long>(bytes.size),
static_cast<unsigned long long>(kMaxFramePayloadSize));
static_cast<unsigned long long>(bytes.size), static_cast<unsigned long long>(kMaxFramePayloadSize));
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
@@ -135,8 +134,7 @@ namespace MobileGL::MG_Remote::Transport {
return MOBILEGL_OK;
}
MobileGLResult InProcessTransport::ReceiveFrame(MobileGLMutableByteSpan buffer,
std::uint64_t* outSize,
MobileGLResult InProcessTransport::ReceiveFrame(MobileGLMutableByteSpan buffer, std::uint64_t* outSize,
std::uint32_t timeoutMs) {
if (outSize != nullptr) {
*outSize = 0;
@@ -194,8 +192,7 @@ namespace MobileGL::MG_Remote::Transport {
if (fd < 0) {
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
if (sideband.size > FdPassing::kMaxSidebandBytes ||
(sideband.size != 0 && sideband.data == nullptr)) {
if (sideband.size > FdPassing::kMaxSidebandBytes || (sideband.size != 0 && sideband.data == nullptr)) {
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
// Same ownership rule as SCM_RIGHTS: the peer gets its own descriptor
@@ -228,8 +225,7 @@ namespace MobileGL::MG_Remote::Transport {
}
MobileGLResult InProcessTransport::ReceiveFd(int* outFd, MobileGLMutableByteSpan sideband,
std::uint64_t* outSidebandSize,
std::uint32_t timeoutMs) {
std::uint64_t* outSidebandSize, std::uint32_t timeoutMs) {
if (outFd == nullptr) {
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
@@ -285,11 +281,17 @@ namespace MobileGL::MG_Remote::Transport {
// directions are half-closed and both ring doorbells are KILLED, because a
// peer parked on a ring doorbell mid-frame would otherwise never come back
// (a mere ring is consumed once and the waiter parks again).
void InProcessTransport::Shutdown() { m_channel->Close(); }
void InProcessTransport::Shutdown() {
m_channel->Close();
}
Doorbell& InProcessTransport::PeerDoorbell() { return m_channel->Bell(1 - m_endpoint); }
Doorbell& InProcessTransport::PeerDoorbell() {
return m_channel->Bell(1 - m_endpoint);
}
Doorbell& InProcessTransport::SelfDoorbell() { return m_channel->Bell(m_endpoint); }
Doorbell& InProcessTransport::SelfDoorbell() {
return m_channel->Bell(m_endpoint);
}
namespace {
std::atomic<bool> g_peerInAnotherProcessForTest{false};
@@ -39,8 +39,8 @@ namespace MobileGL::MG_Remote::Transport {
std::uint32_t timeoutMs) override;
std::uint64_t PeekFrameSize() override;
MobileGLResult ShareFd(int fd, MobileGLByteSpan sideband) override;
MobileGLResult ReceiveFd(int* outFd, MobileGLMutableByteSpan sideband,
std::uint64_t* outSidebandSize, std::uint32_t timeoutMs) override;
MobileGLResult ReceiveFd(int* outFd, MobileGLMutableByteSpan sideband, std::uint64_t* outSidebandSize,
std::uint32_t timeoutMs) override;
void Shutdown() override;
TransportRole Role() const override { return TransportRole::InProcess; }
+211 -200
View File
@@ -18,211 +18,222 @@
#endif
namespace MobileGL::MG_Remote::Transport {
namespace {
struct Window {
std::uint64_t waitReplies = 0, samples = 0, replyNs = 0, stageBytes = 0;
std::uint64_t transportWaitNs = 0;
std::array<std::uint64_t, 32> histogram{};
// One slot per wire op, plus a last slot for anything past the table. See
// LinkMetricsMaxOps: the point is that the per-op numbers still SUM to waitReplies.
std::array<std::uint64_t, LinkMetricsMaxOps> waitRepliesByOp{};
std::array<std::uint64_t, LinkMetricsMaxOps> emittedByOp{};
};
// The client already has exactly one record producer. The server never writes these.
struct Metrics {
bool active = false;
std::uint64_t frame = 0, started = 0, lastFrame = 0, cpuStarted = 0, cpuLast = 0;
Window total{}, current{};
} metrics;
std::uint64_t ClockNs() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
std::uint64_t ThreadCpuNs() {
namespace {
struct Window {
std::uint64_t waitReplies = 0, samples = 0, replyNs = 0, stageBytes = 0;
std::uint64_t transportWaitNs = 0;
std::array<std::uint64_t, 32> histogram{};
// One slot per wire op, plus a last slot for anything past the table. See
// LinkMetricsMaxOps: the point is that the per-op numbers still SUM to waitReplies.
std::array<std::uint64_t, LinkMetricsMaxOps> waitRepliesByOp{};
std::array<std::uint64_t, LinkMetricsMaxOps> emittedByOp{};
};
// The client already has exactly one record producer. The server never writes these.
struct Metrics {
bool active = false;
std::uint64_t frame = 0, started = 0, lastFrame = 0, cpuStarted = 0, cpuLast = 0;
Window total{}, current{};
} metrics;
std::uint64_t ClockNs() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
std::uint64_t ThreadCpuNs() {
#if defined(CLOCK_THREAD_CPUTIME_ID)
timespec value{};
if (::clock_gettime(CLOCK_THREAD_CPUTIME_ID, &value) == 0)
return std::uint64_t(value.tv_sec) * 1000000000ull + value.tv_nsec;
timespec value{};
if (::clock_gettime(CLOCK_THREAD_CPUTIME_ID, &value) == 0)
return std::uint64_t(value.tv_sec) * 1000000000ull + value.tv_nsec;
#endif
return 0;
return 0;
}
std::uint64_t QuantileUpperUs(const Window& w, unsigned percentile) {
if (w.samples == 0) return 0;
const auto target = (w.samples * percentile + 99) / 100;
std::uint64_t count = 0;
for (unsigned i = 0; i < w.histogram.size(); ++i) {
count += w.histogram[i];
if (count >= target) return 1ull << i;
}
return 1ull << 31;
}
// The per-op split, formatted into a caller buffer. Raw ids: this file includes only
// <cstdint> and friends, not the op table, and the reader names rows with WireOpName
// (2 ResourceCreate, 3 ResourceRespecify, 47 SetTextureParams, 48 ResourceSubData).
void FormatOpSplit(const Window& w, char* out, std::size_t outSize) {
std::size_t at = 0;
out[0] = '\0';
for (std::uint32_t op = 1; op < LinkMetricsMaxOps && at + 24 < outSize; ++op) {
const std::uint64_t waits = w.waitRepliesByOp[op];
if (waits == 0) continue;
const int n = std::snprintf(out + at, outSize - at, "%s%u=%llu", at == 0 ? "" : ",",
static_cast<unsigned>(op), static_cast<unsigned long long>(waits));
if (n < 0 || static_cast<std::size_t>(n) >= outSize - at) break;
at += static_cast<std::size_t>(n);
}
if (at == 0) std::snprintf(out, outSize, "(none)");
}
// The same shape for the emission tally: one formatter, two arrays, so the two lines cannot
// drift apart in their id spelling.
void FormatCounts(const std::array<std::uint64_t, LinkMetricsMaxOps>& counts, char* out, std::size_t outSize) {
std::size_t at = 0;
out[0] = '\0';
for (std::uint32_t op = 1; op < LinkMetricsMaxOps && at + 24 < outSize; ++op) {
if (counts[op] == 0) continue;
const int n = std::snprintf(out + at, outSize - at, "%s%u=%llu", at == 0 ? "" : ",",
static_cast<unsigned>(op), static_cast<unsigned long long>(counts[op]));
if (n < 0 || static_cast<std::size_t>(n) >= outSize - at) break;
at += static_cast<std::size_t>(n);
}
if (at == 0) std::snprintf(out, outSize, "(none)");
}
void Emit(const char* kind, const Window& w, std::uint64_t wallNs, std::uint64_t cpuNs) {
char histogram[768]{};
std::size_t at = 0;
for (unsigned i = 0; i < w.histogram.size(); ++i) {
const int n = std::snprintf(histogram + at, sizeof(histogram) - at, "%s%llu", i == 0 ? "" : ",",
static_cast<unsigned long long>(w.histogram[i]));
if (n < 0 || static_cast<std::size_t>(n) >= sizeof(histogram) - at) break;
at += static_cast<std::size_t>(n);
}
MGLOG_I("P65LinkMetrics kind=%s frame=%llu wait_replies=%llu rtt_samples=%llu "
"rtt_mean_us=%.3f rtt_p50_upper_us=%llu rtt_p99_upper_us=%llu "
"stage_bytes=%llu wall_ns=%llu client_thread_cpu_ns=%llu "
"transport_wait_ns=%llu rtt_hist_us_pow2=%s",
kind, static_cast<unsigned long long>(metrics.frame),
static_cast<unsigned long long>(w.waitReplies), static_cast<unsigned long long>(w.samples),
w.samples == 0 ? 0.0 : double(w.replyNs) / double(w.samples) / 1000.0,
static_cast<unsigned long long>(QuantileUpperUs(w, 50)),
static_cast<unsigned long long>(QuantileUpperUs(w, 99)),
static_cast<unsigned long long>(w.stageBytes), static_cast<unsigned long long>(wallNs),
static_cast<unsigned long long>(cpuNs), static_cast<unsigned long long>(w.transportWaitNs),
histogram);
// AND THE PER-OP SPLIT WITH EVERY FRAME. A whole-session total cannot name the rows behind
// ONE frame, and one frame is the whole shape of this problem: the device run that
// motivated this printed 43,232 of its 43,629 waits in a single atlas frame, and the exit
// summary never appeared at all because a SIGTERM does not take the session's clean stop
// path. Skipped when the frame bought nothing, so a steady-state session stays quiet.
if (w.waitReplies != 0) {
char split[1024];
FormatOpSplit(w, split, sizeof(split));
MGLOG_I("P65LinkMetrics kind=frame-op frame=%llu wait_replies=%llu by_op=%s",
static_cast<unsigned long long>(metrics.frame), static_cast<unsigned long long>(w.waitReplies),
split);
}
// WHAT THE FRAME SENT, not only what it paid for. Printed for the same frames - a frame
// that bought no wait still SENT records, and the count is what says whether the waits are
// one per record (a real handshake) or fewer (a batched one).
std::uint64_t emitted = 0;
for (std::uint32_t op = 1; op < LinkMetricsMaxOps; ++op)
emitted += w.emittedByOp[op];
if (emitted != 0) {
char sent[1024];
FormatCounts(w.emittedByOp, sent, sizeof(sent));
MGLOG_I("P65LinkMetrics kind=frame-sent frame=%llu records=%llu by_op=%s",
static_cast<unsigned long long>(metrics.frame), static_cast<unsigned long long>(emitted), sent);
}
}
} // namespace
void LinkMetricsBegin() {
metrics = {};
const char* enabled = std::getenv("MOBILEGL_PIPE_STATS");
metrics.active = enabled != nullptr && std::strcmp(enabled, "1") == 0;
if (!metrics.active) return;
metrics.started = metrics.lastFrame = ClockNs();
metrics.cpuStarted = metrics.cpuLast = ThreadCpuNs();
}
std::uint64_t QuantileUpperUs(const Window& w, unsigned percentile) {
if (w.samples == 0) return 0;
const auto target = (w.samples * percentile + 99) / 100;
std::uint64_t count = 0;
for (unsigned i = 0; i < w.histogram.size(); ++i) {
count += w.histogram[i];
if (count >= target) return 1ull << i;
}
return 1ull << 31;
std::uint64_t LinkMetricsBeginReply(bool wantsReply, std::uint32_t op) {
if (!metrics.active || !wantsReply) return 0;
const std::uint32_t slot = op < LinkMetricsMaxOps ? op : LinkMetricsMaxOps - 1;
++metrics.current.waitReplies;
++metrics.total.waitReplies;
++metrics.current.waitRepliesByOp[slot];
++metrics.total.waitRepliesByOp[slot];
return ClockNs();
}
// The per-op split, formatted into a caller buffer. Raw ids: this file includes only
// <cstdint> and friends, not the op table, and the reader names rows with WireOpName
// (2 ResourceCreate, 3 ResourceRespecify, 47 SetTextureParams, 48 ResourceSubData).
void FormatOpSplit(const Window& w, char* out, std::size_t outSize) {
std::size_t at = 0;
out[0] = '\0';
for (std::uint32_t op = 1; op < LinkMetricsMaxOps && at + 24 < outSize; ++op) {
const std::uint64_t waits = w.waitRepliesByOp[op];
if (waits == 0) continue;
const int n = std::snprintf(out + at, outSize - at, "%s%u=%llu", at == 0 ? "" : ",",
static_cast<unsigned>(op),
static_cast<unsigned long long>(waits));
if (n < 0 || static_cast<std::size_t>(n) >= outSize - at) break;
at += static_cast<std::size_t>(n);
}
if (at == 0) std::snprintf(out, outSize, "(none)");
}
// The same shape for the emission tally: one formatter, two arrays, so the two lines cannot
// drift apart in their id spelling.
void FormatCounts(const std::array<std::uint64_t, LinkMetricsMaxOps>& counts, char* out,
std::size_t outSize) {
std::size_t at = 0;
out[0] = '\0';
for (std::uint32_t op = 1; op < LinkMetricsMaxOps && at + 24 < outSize; ++op) {
if (counts[op] == 0) continue;
const int n = std::snprintf(out + at, outSize - at, "%s%u=%llu", at == 0 ? "" : ",",
static_cast<unsigned>(op),
static_cast<unsigned long long>(counts[op]));
if (n < 0 || static_cast<std::size_t>(n) >= outSize - at) break;
at += static_cast<std::size_t>(n);
}
if (at == 0) std::snprintf(out, outSize, "(none)");
}
void Emit(const char* kind, const Window& w, std::uint64_t wallNs, std::uint64_t cpuNs) {
char histogram[768]{};
std::size_t at = 0;
for (unsigned i = 0; i < w.histogram.size(); ++i) {
const int n = std::snprintf(histogram + at, sizeof(histogram) - at, "%s%llu",
i == 0 ? "" : ",", static_cast<unsigned long long>(w.histogram[i]));
if (n < 0 || static_cast<std::size_t>(n) >= sizeof(histogram) - at) break;
at += static_cast<std::size_t>(n);
}
MGLOG_I("P65LinkMetrics kind=%s frame=%llu wait_replies=%llu rtt_samples=%llu "
"rtt_mean_us=%.3f rtt_p50_upper_us=%llu rtt_p99_upper_us=%llu "
"stage_bytes=%llu wall_ns=%llu client_thread_cpu_ns=%llu "
"transport_wait_ns=%llu rtt_hist_us_pow2=%s",
kind, static_cast<unsigned long long>(metrics.frame),
static_cast<unsigned long long>(w.waitReplies), static_cast<unsigned long long>(w.samples),
w.samples == 0 ? 0.0 : double(w.replyNs) / double(w.samples) / 1000.0,
static_cast<unsigned long long>(QuantileUpperUs(w, 50)),
static_cast<unsigned long long>(QuantileUpperUs(w, 99)),
static_cast<unsigned long long>(w.stageBytes), static_cast<unsigned long long>(wallNs),
static_cast<unsigned long long>(cpuNs),
static_cast<unsigned long long>(w.transportWaitNs), histogram);
// AND THE PER-OP SPLIT WITH EVERY FRAME. A whole-session total cannot name the rows behind
// ONE frame, and one frame is the whole shape of this problem: the device run that
// motivated this printed 43,232 of its 43,629 waits in a single atlas frame, and the exit
// summary never appeared at all because a SIGTERM does not take the session's clean stop
// path. Skipped when the frame bought nothing, so a steady-state session stays quiet.
if (w.waitReplies != 0) {
char split[1024];
FormatOpSplit(w, split, sizeof(split));
MGLOG_I("P65LinkMetrics kind=frame-op frame=%llu wait_replies=%llu by_op=%s",
static_cast<unsigned long long>(metrics.frame),
static_cast<unsigned long long>(w.waitReplies), split);
}
// WHAT THE FRAME SENT, not only what it paid for. Printed for the same frames - a frame
// that bought no wait still SENT records, and the count is what says whether the waits are
// one per record (a real handshake) or fewer (a batched one).
std::uint64_t emitted = 0;
for (std::uint32_t op = 1; op < LinkMetricsMaxOps; ++op) emitted += w.emittedByOp[op];
if (emitted != 0) {
char sent[1024];
FormatCounts(w.emittedByOp, sent, sizeof(sent));
MGLOG_I("P65LinkMetrics kind=frame-sent frame=%llu records=%llu by_op=%s",
static_cast<unsigned long long>(metrics.frame),
static_cast<unsigned long long>(emitted), sent);
void LinkMetricsReplyApplied(std::uint64_t startedNs) {
if (!metrics.active || startedNs == 0) return;
const auto ns = ClockNs() - startedNs;
const auto us = (ns + 999) / 1000;
unsigned bucket = 0;
while (bucket < 31 && us > (1ull << bucket))
++bucket;
for (Window* w : {&metrics.current, &metrics.total}) {
++w->samples;
w->replyNs += ns;
++w->histogram[bucket];
}
}
}
void LinkMetricsBegin() {
metrics = {};
const char* enabled = std::getenv("MOBILEGL_PIPE_STATS");
metrics.active = enabled != nullptr && std::strcmp(enabled, "1") == 0;
if (!metrics.active) return;
metrics.started = metrics.lastFrame = ClockNs();
metrics.cpuStarted = metrics.cpuLast = ThreadCpuNs();
}
std::uint64_t LinkMetricsBeginReply(bool wantsReply, std::uint32_t op) {
if (!metrics.active || !wantsReply) return 0;
const std::uint32_t slot = op < LinkMetricsMaxOps ? op : LinkMetricsMaxOps - 1;
++metrics.current.waitReplies; ++metrics.total.waitReplies;
++metrics.current.waitRepliesByOp[slot]; ++metrics.total.waitRepliesByOp[slot];
return ClockNs();
}
void LinkMetricsReplyApplied(std::uint64_t startedNs) {
if (!metrics.active || startedNs == 0) return;
const auto ns = ClockNs() - startedNs;
const auto us = (ns + 999) / 1000;
unsigned bucket = 0;
while (bucket < 31 && us > (1ull << bucket)) ++bucket;
for (Window* w : {&metrics.current, &metrics.total}) {
++w->samples; w->replyNs += ns; ++w->histogram[bucket];
void LinkMetricsNoteRecord(std::uint32_t op) {
if (!metrics.active) return;
const std::uint32_t slot = op < LinkMetricsMaxOps ? op : LinkMetricsMaxOps - 1;
++metrics.current.emittedByOp[slot];
++metrics.total.emittedByOp[slot];
}
std::uint64_t LinkMetricsRecordsFor(std::uint32_t op) {
return metrics.total.emittedByOp[op < LinkMetricsMaxOps ? op : LinkMetricsMaxOps - 1];
}
std::uint64_t LinkMetricsReplyWaits() {
return metrics.total.waitReplies;
}
std::uint64_t LinkMetricsReplyWaitsFor(std::uint32_t op) {
return metrics.total.waitRepliesByOp[op < LinkMetricsMaxOps ? op : LinkMetricsMaxOps - 1];
}
void LinkMetricsStageBytes(std::uint64_t bytes) {
if (!metrics.active) return;
metrics.current.stageBytes += bytes;
metrics.total.stageBytes += bytes;
}
std::uint64_t LinkMetricsBeginTransportWait() {
return metrics.active ? ClockNs() : 0;
}
void LinkMetricsEndTransportWait(std::uint64_t startedNs) {
if (!metrics.active || startedNs == 0) return;
const auto elapsed = ClockNs() - startedNs;
metrics.current.transportWaitNs += elapsed;
metrics.total.transportWaitNs += elapsed;
}
void LinkMetricsPresent() {
if (!metrics.active) return;
++metrics.frame;
const auto now = ClockNs(), cpu = ThreadCpuNs();
Emit("frame", metrics.current, now - metrics.lastFrame, cpu - metrics.cpuLast);
metrics.current = {};
metrics.lastFrame = now;
metrics.cpuLast = cpu;
}
void LinkMetricsEnd() {
if (!metrics.active) return;
Emit("summary", metrics.total, ClockNs() - metrics.started, ThreadCpuNs() - metrics.cpuStarted);
// AND THE PER-OP SPLIT AT THE SAME INSTANT, on its own line. The handoff wanted this number
// and could not get it: under spawn the client does not advance the frame window, so the
// per-frame lines never print, and a SIGKILL takes the exit-time dump with it. The summary is
// written from the one place a client that ends the ordinary way always reaches. `op` is raw -
// this file does not include the op table - and the reader names rows with WireOpName.
char split[1024];
FormatOpSplit(metrics.total, split, sizeof(split));
MGLOG_I("P65LinkMetrics kind=per-op wait_replies=%llu by_op=%s",
static_cast<unsigned long long>(metrics.total.waitReplies), split);
metrics.active = false;
}
void LinkMetricsServerPresent(std::uint64_t serial) {
// Independent of the client latch: spawn servers never call LinkMetricsBegin.
// The baseline belongs to the apply thread, and is discarded if that thread is replaced.
static thread_local const bool enabled = [] {
const char* value = std::getenv("MOBILEGL_PIPE_STATS");
return value != nullptr && std::strcmp(value, "1") == 0;
}();
if (!enabled) return;
static thread_local std::uint64_t previousSerial = 0, previousWall = 0, previousCpu = 0;
const auto wall = ClockNs(), cpu = ThreadCpuNs();
const bool valid = previousWall != 0 && serial > previousSerial && cpu >= previousCpu;
MGLOG_I("P65ServerMetrics frame=%llu valid=%u wall_ns=%llu apply_thread_cpu_ns=%llu",
static_cast<unsigned long long>(serial), valid ? 1u : 0u,
static_cast<unsigned long long>(valid ? wall - previousWall : 0),
static_cast<unsigned long long>(valid ? cpu - previousCpu : 0));
previousSerial = serial;
previousWall = wall;
previousCpu = cpu;
}
}
void LinkMetricsNoteRecord(std::uint32_t op) {
if (!metrics.active) return;
const std::uint32_t slot = op < LinkMetricsMaxOps ? op : LinkMetricsMaxOps - 1;
++metrics.current.emittedByOp[slot]; ++metrics.total.emittedByOp[slot];
}
std::uint64_t LinkMetricsRecordsFor(std::uint32_t op) {
return metrics.total.emittedByOp[op < LinkMetricsMaxOps ? op : LinkMetricsMaxOps - 1];
}
std::uint64_t LinkMetricsReplyWaits() { return metrics.total.waitReplies; }
std::uint64_t LinkMetricsReplyWaitsFor(std::uint32_t op) {
return metrics.total.waitRepliesByOp[op < LinkMetricsMaxOps ? op : LinkMetricsMaxOps - 1];
}
void LinkMetricsStageBytes(std::uint64_t bytes) {
if (!metrics.active) return;
metrics.current.stageBytes += bytes; metrics.total.stageBytes += bytes;
}
std::uint64_t LinkMetricsBeginTransportWait() {
return metrics.active ? ClockNs() : 0;
}
void LinkMetricsEndTransportWait(std::uint64_t startedNs) {
if (!metrics.active || startedNs == 0) return;
const auto elapsed = ClockNs() - startedNs;
metrics.current.transportWaitNs += elapsed;
metrics.total.transportWaitNs += elapsed;
}
void LinkMetricsPresent() {
if (!metrics.active) return;
++metrics.frame;
const auto now = ClockNs(), cpu = ThreadCpuNs();
Emit("frame", metrics.current, now - metrics.lastFrame, cpu - metrics.cpuLast);
metrics.current = {}; metrics.lastFrame = now; metrics.cpuLast = cpu;
}
void LinkMetricsEnd() {
if (!metrics.active) return;
Emit("summary", metrics.total, ClockNs() - metrics.started, ThreadCpuNs() - metrics.cpuStarted);
// AND THE PER-OP SPLIT AT THE SAME INSTANT, on its own line. The handoff wanted this number
// and could not get it: under spawn the client does not advance the frame window, so the
// per-frame lines never print, and a SIGKILL takes the exit-time dump with it. The summary is
// written from the one place a client that ends the ordinary way always reaches. `op` is raw -
// this file does not include the op table - and the reader names rows with WireOpName.
char split[1024];
FormatOpSplit(metrics.total, split, sizeof(split));
MGLOG_I("P65LinkMetrics kind=per-op wait_replies=%llu by_op=%s",
static_cast<unsigned long long>(metrics.total.waitReplies), split);
metrics.active = false;
}
void LinkMetricsServerPresent(std::uint64_t serial) {
// Independent of the client latch: spawn servers never call LinkMetricsBegin.
// The baseline belongs to the apply thread, and is discarded if that thread is replaced.
static thread_local const bool enabled = [] {
const char* value = std::getenv("MOBILEGL_PIPE_STATS");
return value != nullptr && std::strcmp(value, "1") == 0;
}();
if (!enabled) return;
static thread_local std::uint64_t previousSerial = 0, previousWall = 0, previousCpu = 0;
const auto wall = ClockNs(), cpu = ThreadCpuNs();
const bool valid = previousWall != 0 && serial > previousSerial && cpu >= previousCpu;
MGLOG_I("P65ServerMetrics frame=%llu valid=%u wall_ns=%llu apply_thread_cpu_ns=%llu",
static_cast<unsigned long long>(serial), valid ? 1u : 0u,
static_cast<unsigned long long>(valid ? wall - previousWall : 0),
static_cast<unsigned long long>(valid ? cpu - previousCpu : 0));
previousSerial = serial; previousWall = wall; previousCpu = cpu;
}
}
} // namespace MobileGL::MG_Remote::Transport
+6 -6
View File
@@ -64,15 +64,15 @@ namespace MobileGL::MG_Remote::Transport {
// ClientSession/ServerLoop. The counters themselves stay in StreamLink.cpp beside the recv /
// sendmsg loops that feed them.
struct LinkIoStats {
std::uint64_t bytes = 0; // payload bytes taken off the socket
std::uint64_t calls = 0; // recv() calls it took
std::uint64_t reads = 0; // Link::Read() calls (one per header, envelope or chunk)
std::uint64_t nsInRecv = 0; // wall time inside recv - blocking included
std::uint64_t nsTotal = 0; // wall time inside Read
std::uint64_t bytes = 0; // payload bytes taken off the socket
std::uint64_t calls = 0; // recv() calls it took
std::uint64_t reads = 0; // Link::Read() calls (one per header, envelope or chunk)
std::uint64_t nsInRecv = 0; // wall time inside recv - blocking included
std::uint64_t nsTotal = 0; // wall time inside Read
};
LinkIoStats LinkMetricsTakeReadStats();
// The client's half: bytes handed to the io thread's sendmsg, the calls it took, and the
// wall time inside sendmsg (blocking included). nsInRecv carries the send time; reads is
// unused on this side.
LinkIoStats LinkMetricsTakeSendStats();
}
} // namespace MobileGL::MG_Remote::Transport
+33 -18
View File
@@ -40,20 +40,29 @@ namespace MobileGL::MG_Remote::Transport {
Wire::LogLevel WireLogLevelFor(int level) {
if (level <= MOBILEGL_LOG_LEVEL_DEBUG) return Wire::LogLevel::Debug;
switch (level) {
case MOBILEGL_LOG_LEVEL_INFO: return Wire::LogLevel::Info;
case MOBILEGL_LOG_LEVEL_WARN: return Wire::LogLevel::Warn;
case MOBILEGL_LOG_LEVEL_ERROR: return Wire::LogLevel::Error;
default: return Wire::LogLevel::Fatal;
case MOBILEGL_LOG_LEVEL_INFO:
return Wire::LogLevel::Info;
case MOBILEGL_LOG_LEVEL_WARN:
return Wire::LogLevel::Warn;
case MOBILEGL_LOG_LEVEL_ERROR:
return Wire::LogLevel::Error;
default:
return Wire::LogLevel::Fatal;
}
}
int LogLevelFromWire(Wire::LogLevel level) {
switch (level) {
case Wire::LogLevel::Debug: return MOBILEGL_LOG_LEVEL_DEBUG;
case Wire::LogLevel::Info: return MOBILEGL_LOG_LEVEL_INFO;
case Wire::LogLevel::Warn: return MOBILEGL_LOG_LEVEL_WARN;
case Wire::LogLevel::Error: return MOBILEGL_LOG_LEVEL_ERROR;
case Wire::LogLevel::Fatal: return MOBILEGL_LOG_LEVEL_FATAL;
case Wire::LogLevel::Debug:
return MOBILEGL_LOG_LEVEL_DEBUG;
case Wire::LogLevel::Info:
return MOBILEGL_LOG_LEVEL_INFO;
case Wire::LogLevel::Warn:
return MOBILEGL_LOG_LEVEL_WARN;
case Wire::LogLevel::Error:
return MOBILEGL_LOG_LEVEL_ERROR;
case Wire::LogLevel::Fatal:
return MOBILEGL_LOG_LEVEL_FATAL;
}
// A value this build's schema does not name: a newer peer's severity. Never demoted into
// the lossy class - an unknown severity is filed as an error.
@@ -77,13 +86,17 @@ namespace MobileGL::MG_Remote::Transport {
m_sender = std::thread([this] { Run(); });
}
LogForwardChannel::~LogForwardChannel() { (void)Close(); }
LogForwardChannel::~LogForwardChannel() {
(void)Close();
}
void LogForwardChannel::ForwardThunk(void* user, int level, const char* line) {
static_cast<LogForwardChannel*>(user)->Offer(level, line);
}
bool LogForwardChannel::IsUrgentLocked() const { return m_urgent > 0 || m_closing; }
bool LogForwardChannel::IsUrgentLocked() const {
return m_urgent > 0 || m_closing;
}
void LogForwardChannel::QueueGapNoticesLocked(bool lossyGapToo) {
// ONE NOTICE PER GAP, however many lines it swallowed, so a notice never needs room of its
@@ -180,8 +193,10 @@ namespace MobileGL::MG_Remote::Transport {
item.text.assign(line, length);
const std::uint64_t seq = item.seq;
m_queue.push_back(std::move(item));
if (lossy) m_lossyBytes += length;
else m_errorBytes += length;
if (lossy)
m_lossyBytes += length;
else
m_errorBytes += length;
if (fatal) ++m_urgent;
m_cv.notify_all();
if (!fatal) {
@@ -286,8 +301,10 @@ namespace MobileGL::MG_Remote::Transport {
Item item = std::move(front);
m_queue.pop_front();
if (item.kind == Kind::Line) {
if (item.level <= MOBILEGL_LOG_LEVEL_WARN) m_lossyBytes -= item.text.size();
else m_errorBytes -= item.text.size();
if (item.level <= MOBILEGL_LOG_LEVEL_WARN)
m_lossyBytes -= item.text.size();
else
m_errorBytes -= item.text.size();
}
if (item.kind == Kind::Control || (item.kind == Kind::Line && item.level >= MOBILEGL_LOG_LEVEL_FATAL)) {
--m_urgent;
@@ -322,9 +339,7 @@ namespace MobileGL::MG_Remote::Transport {
// that the tail it has is not the whole tail.
QueueGapNoticesLocked(/*lossyGapToo=*/true);
m_cv.notify_all();
const auto drained = [this] {
return (m_queue.empty() && !m_sending) || m_counters.transportFailed;
};
const auto drained = [this] { return (m_queue.empty() && !m_sending) || m_counters.transportFailed; };
if (!m_cv.wait_for(lock, std::chrono::milliseconds(m_limits.closeDrainMs), drained)) {
// Not taken within the budget: the peer is not reading. The rest is abandoned,
// and a sender inside a write is blocked on that peer and needs unblocking.
+11 -7
View File
@@ -31,7 +31,7 @@
// flatbuffers) into every includer; the definition is protocol_generated.h's.
namespace MobileGL::Wire {
enum class LogLevel : std::uint8_t;
}
} // namespace MobileGL::Wire
namespace MobileGL::MG_Remote::Transport {
@@ -60,8 +60,8 @@ namespace MobileGL::MG_Remote::Transport {
};
struct LogForwardCounters {
std::uint64_t forwarded = 0; // lines written to the transport (notices excluded)
std::uint64_t lossyDropped = 0; // DEBUG/INFO/WARN lines dropped at the lossy cap
std::uint64_t forwarded = 0; // lines written to the transport (notices excluded)
std::uint64_t lossyDropped = 0; // DEBUG/INFO/WARN lines dropped at the lossy cap
std::uint64_t lossyDroppedBytes = 0;
std::uint64_t errorsCoalesced = 0; // ERROR/FATAL lines past the error cap
std::uint64_t errorsPaced = 0; // ERROR lines that waited for the pace (delayed, sent)
@@ -99,14 +99,18 @@ namespace MobileGL::MG_Remote::Transport {
static void ForwardThunk(void* user, int level, const char* line);
private:
enum class Kind : std::uint8_t { Line, Notice, Control };
enum class Kind : std::uint8_t {
Line,
Notice,
Control
};
struct Item {
Kind kind = Kind::Line;
int level = 0;
std::uint64_t seq = 0;
std::string text; // Line, Notice
std::string text; // Line, Notice
std::vector<std::uint8_t> frame; // Control
bool paced = false; // already counted as paced once
bool paced = false; // already counted as paced once
};
void Run();
@@ -129,7 +133,7 @@ namespace MobileGL::MG_Remote::Transport {
// m_writtenSeq when a FATAL wait last ran out; while it has not moved since, the next FATAL
// does not wait again (Offer). ~0 = no wait has run out.
std::uint64_t m_fatalGaveUpAtWritten = ~0ull;
std::uint64_t m_urgent = 0; // FATAL lines and control frames queued: do not pace
std::uint64_t m_urgent = 0; // FATAL lines and control frames queued: do not pace
// A gap not yet marked in the stream, per class.
std::uint64_t m_gapLossyLines = 0;
std::uint64_t m_gapLossyBytes = 0;
+2 -2
View File
@@ -31,8 +31,8 @@ namespace MobileGL::MG_Remote::Transport {
inline std::vector<std::uint8_t> EncodePairBind(const std::uint8_t (&nonce)[kPairNonceBytes], bool aux) {
::flatbuffers::FlatBufferBuilder builder(64);
auto bind = ::MobileGL::Wire::CreatePairBind(builder, builder.CreateVector(nonce, kPairNonceBytes), aux);
auto envelope = ::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::PairBind,
bind.Union());
auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::PairBind, bind.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
return std::vector<std::uint8_t>(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
}
+28 -35
View File
@@ -79,8 +79,7 @@ namespace MobileGL::MG_Remote::Transport {
if (slotBytes <= sizeof(ReplySlotHeader)) {
WireLogError("MG_Remote reply pool: rejected, %llu bytes over %u slots leaves no "
"room for a payload past the %llu byte slot header",
static_cast<unsigned long long>(sizeBytes),
static_cast<unsigned>(slotCount),
static_cast<unsigned long long>(sizeBytes), static_cast<unsigned>(slotCount),
static_cast<unsigned long long>(sizeof(ReplySlotHeader)));
return;
}
@@ -91,14 +90,13 @@ namespace MobileGL::MG_Remote::Transport {
// it is naturally aligned. A geometry whose slotBytes is not a
// multiple of 8 puts later slots on odd boundaries, so it is refused
// here rather than left to a future caller to discover.
if ((slotBytes % 8) != 0 ||
(reinterpret_cast<std::uintptr_t>(base) % alignof(ReplySlotHeader)) != 0) {
WireLogError("MG_Remote reply pool: rejected, a %llu byte slot at base alignment "
"%llu would put a slot header on an unaligned address, and the seq "
"stamp the wrong-slot check reads has to be untorn",
static_cast<unsigned long long>(slotBytes),
static_cast<unsigned long long>(
reinterpret_cast<std::uintptr_t>(base) % alignof(ReplySlotHeader)));
if ((slotBytes % 8) != 0 || (reinterpret_cast<std::uintptr_t>(base) % alignof(ReplySlotHeader)) != 0) {
WireLogError(
"MG_Remote reply pool: rejected, a %llu byte slot at base alignment "
"%llu would put a slot header on an unaligned address, and the seq "
"stamp the wrong-slot check reads has to be untorn",
static_cast<unsigned long long>(slotBytes),
static_cast<unsigned long long>(reinterpret_cast<std::uintptr_t>(base) % alignof(ReplySlotHeader)));
return;
}
m_base = static_cast<std::uint8_t*>(base);
@@ -107,8 +105,7 @@ namespace MobileGL::MG_Remote::Transport {
// Truncated to 32 bits deliberately: the header's Size field is
// 32-bit, so a slot no 32-bit count could describe would let a
// legal-looking Size name bytes past the slot.
m_slotBytes = slotBytes > 0xFFFFFFFFull ? 0xFFFFFFFFu
: static_cast<std::uint32_t>(slotBytes);
m_slotBytes = slotBytes > 0xFFFFFFFFull ? 0xFFFFFFFFu : static_cast<std::uint32_t>(slotBytes);
}
bool Valid() const { return m_base != nullptr; }
@@ -118,8 +115,7 @@ namespace MobileGL::MG_Remote::Transport {
// it emits a ReadPixels, which is the whole reason a fixed slot size is
// legitimate rather than a guess.
std::uint32_t MaxReplyBytes() const {
return m_slotBytes == 0 ? 0u
: m_slotBytes - static_cast<std::uint32_t>(sizeof(ReplySlotHeader));
return m_slotBytes == 0 ? 0u : m_slotBytes - static_cast<std::uint32_t>(sizeof(ReplySlotHeader));
}
// ID-47: THE CLIENT'S HALF of "a reply larger than one slot is fatal". True
@@ -142,8 +138,8 @@ namespace MobileGL::MG_Remote::Transport {
// what is refused is exactly what would have been posted. This is the one
// call c1's OnReadPixels emitter makes before EmitAndWait; the
// ClientSession forwards it verbatim (RequireReadPixelsReplyFits).
void RequireReadPixelsFits(std::uint32_t width, std::uint32_t height, std::uint32_t format,
std::uint32_t type, std::uint64_t bytes) const {
void RequireReadPixelsFits(std::uint32_t width, std::uint32_t height, std::uint32_t format, std::uint32_t type,
std::uint64_t bytes) const {
if (CanHold(bytes)) {
return;
}
@@ -153,9 +149,8 @@ namespace MobileGL::MG_Remote::Transport {
"client before emission (ID-47): a reply is neither truncated nor "
"chunked, and the client bands any read whose single pixel fits a slot, "
"so this answer is one that no banding could have sent",
static_cast<unsigned>(width), static_cast<unsigned>(height),
static_cast<unsigned>(format), static_cast<unsigned>(type),
static_cast<unsigned long long>(bytes),
static_cast<unsigned>(width), static_cast<unsigned>(height), static_cast<unsigned>(format),
static_cast<unsigned>(type), static_cast<unsigned long long>(bytes),
static_cast<unsigned>(MaxReplyBytes()), static_cast<unsigned>(m_slots),
static_cast<unsigned>(m_slotBytes), static_cast<unsigned>(MaxReplyBytes()));
}
@@ -168,8 +163,7 @@ namespace MobileGL::MG_Remote::Transport {
}
for (std::uint32_t slot = 0; slot < m_slots; ++slot) {
ReplySlotHeader header{};
std::memcpy(m_base + static_cast<std::uint64_t>(slot) * m_slotBytes, &header,
sizeof(header));
std::memcpy(m_base + static_cast<std::uint64_t>(slot) * m_slotBytes, &header, sizeof(header));
}
}
@@ -194,9 +188,8 @@ namespace MobileGL::MG_Remote::Transport {
"for seq %llu does not fit a %u byte slot (payload cap %u). P5 does "
"not chunk replies: the client knows an answer's size before it emits "
"the record, so this means the two sides disagree about the frame",
static_cast<unsigned long long>(size),
static_cast<unsigned long long>(seq), static_cast<unsigned>(m_slotBytes),
static_cast<unsigned>(MaxReplyBytes()));
static_cast<unsigned long long>(size), static_cast<unsigned long long>(seq),
static_cast<unsigned>(m_slotBytes), static_cast<unsigned>(MaxReplyBytes()));
}
std::uint8_t* slot = SlotAt(seq);
if (size != 0 && bytes != nullptr) {
@@ -213,15 +206,17 @@ namespace MobileGL::MG_Remote::Transport {
// The link can lend the immutable payload after appliedSeq without a
// second copy. The same seq and size honesty checks guard both readers.
bool ReadView(std::uint64_t seq, std::int32_t* status, const void** payload,
std::uint64_t* size) const {
bool ReadView(std::uint64_t seq, std::int32_t* status, const void** payload, std::uint64_t* size) const {
if (!m_base || !seq || !status || !payload || !size) return false;
const auto* slot = SlotAt(seq);
ReplySlotHeader header{}; std::memcpy(&header, slot, sizeof header);
ReplySlotHeader header{};
std::memcpy(&header, slot, sizeof header);
std::atomic_thread_fence(std::memory_order_acquire);
if (header.Seq != seq || header.Size > MaxReplyBytes()) return false;
*status = header.Status; *size = header.Size;
*payload = slot + sizeof header; return true;
*status = header.Status;
*size = header.Size;
*payload = slot + sizeof header;
return true;
}
// Client side. Returns false when the slot does not carry THIS seq - the
@@ -231,8 +226,8 @@ namespace MobileGL::MG_Remote::Transport {
// A payload larger than the caller's buffer is a caller bug rather than a
// wire fault (the caller sized it from the call it made), so it returns
// false with *outSize set to what was there, the ReceiveFrame shape.
bool Read(std::uint64_t seq, void* outBytes, std::uint64_t outCapacity,
std::int32_t* outStatus, std::uint64_t* outSize) const {
bool Read(std::uint64_t seq, void* outBytes, std::uint64_t outCapacity, std::int32_t* outStatus,
std::uint64_t* outSize) const {
if (outStatus != nullptr) {
*outStatus = kReplyStatusError;
}
@@ -254,16 +249,14 @@ namespace MobileGL::MG_Remote::Transport {
WireLogError("MG_Remote reply pool: slot %llu carries seq %llu, not %llu - the two "
"sides' sequence spaces have drifted (a counted kRecPad, R-9) or the "
"addressing disagrees",
static_cast<unsigned long long>(seq & m_mask),
static_cast<unsigned long long>(header.Seq),
static_cast<unsigned long long>(seq & m_mask), static_cast<unsigned long long>(header.Seq),
static_cast<unsigned long long>(seq));
return false;
}
if (header.Size > MaxReplyBytes()) {
WireLogError("MG_Remote reply pool: slot for seq %llu declares %u payload bytes in "
"a %u byte slot",
static_cast<unsigned long long>(seq), header.Size,
static_cast<unsigned>(m_slotBytes));
static_cast<unsigned long long>(seq), header.Size, static_cast<unsigned>(m_slotBytes));
return false;
}
if (outStatus != nullptr) {
+33 -48
View File
@@ -24,7 +24,9 @@ namespace MobileGL::MG_Remote::Transport {
return (value + (kRingRecordAlignment - 1)) & ~(kRingRecordAlignment - 1);
}
bool IsPowerOfTwo(std::uint64_t value) { return value != 0 && (value & (value - 1)) == 0; }
bool IsPowerOfTwo(std::uint64_t value) {
return value != 0 && (value & (value - 1)) == 0;
}
std::atomic<std::uint64_t>& Head(RingControl& c, RingCursorSet which) {
return which == RingCursorSet::Cmd ? c.cmdHead : c.stageHead;
@@ -54,8 +56,7 @@ namespace MobileGL::MG_Remote::Transport {
control.ringGeneration.store(1, std::memory_order_relaxed);
}
bool RingCursorsValid(const RingControl& control, RingCursorSet cursors,
std::uint64_t capacityBytes) {
bool RingCursorsValid(const RingControl& control, RingCursorSet cursors, std::uint64_t capacityBytes) {
const std::uint64_t head = Head(control, cursors).load(std::memory_order_acquire);
const std::uint64_t applied = AppliedTail(control, cursors).load(std::memory_order_acquire);
const std::uint64_t retired = RetiredTail(control, cursors).load(std::memory_order_acquire);
@@ -72,8 +73,7 @@ namespace MobileGL::MG_Remote::Transport {
if (head != applied || applied != retired) {
MGLOG_E("MG_Remote ring: hard drain refused, ring is not quiesced "
"(head=%llu applied=%llu retired=%llu)",
static_cast<unsigned long long>(head),
static_cast<unsigned long long>(applied),
static_cast<unsigned long long>(head), static_cast<unsigned long long>(applied),
static_cast<unsigned long long>(retired));
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
@@ -87,18 +87,16 @@ namespace MobileGL::MG_Remote::Transport {
// Producer
// -----------------------------------------------------------------------
RingProducer::RingProducer(RingControl* control, void* base, std::uint64_t capacityBytes,
RingCursorSet cursors)
RingProducer::RingProducer(RingControl* control, void* base, std::uint64_t capacityBytes, RingCursorSet cursors)
: m_control(control), m_base(static_cast<std::uint8_t*>(base)), m_capacity(capacityBytes),
m_mask(capacityBytes - 1), m_cursors(cursors) {
if (control == nullptr || base == nullptr || !IsPowerOfTwo(capacityBytes) ||
capacityBytes < kMinRingCapacity || capacityBytes > kMaxRingCapacity) {
if (control == nullptr || base == nullptr || !IsPowerOfTwo(capacityBytes) || capacityBytes < kMinRingCapacity ||
capacityBytes > kMaxRingCapacity) {
MGLOG_E("MG_Remote ring: producer rejected, capacity %llu must be a power of two "
"between %llu and %llu bytes over a non-null mapping (a record may be at most "
"half the ring, and the record header's size field is 32-bit, so a bigger ring "
"would truncate it)",
static_cast<unsigned long long>(capacityBytes),
static_cast<unsigned long long>(kMinRingCapacity),
static_cast<unsigned long long>(capacityBytes), static_cast<unsigned long long>(kMinRingCapacity),
static_cast<unsigned long long>(kMaxRingCapacity));
m_control = nullptr;
m_base = nullptr;
@@ -124,8 +122,7 @@ namespace MobileGL::MG_Remote::Transport {
return inFlight >= m_capacity ? 0 : m_capacity - inFlight;
}
void* RingProducer::Reserve(std::uint16_t kind, std::uint16_t flags,
std::uint64_t payloadBytes) {
void* RingProducer::Reserve(std::uint16_t kind, std::uint16_t flags, std::uint64_t payloadBytes) {
if (m_control == nullptr) {
return nullptr;
}
@@ -160,8 +157,7 @@ namespace MobileGL::MG_Remote::Transport {
const bool needsPad = spaceToEnd < total;
const std::uint64_t needed = needsPad ? spaceToEnd + total : total;
if (FreeBytes() < needed) {
MGLOG_D("MG_Remote ring: full, %llu bytes free, %llu needed",
static_cast<unsigned long long>(FreeBytes()),
MGLOG_D("MG_Remote ring: full, %llu bytes free, %llu needed", static_cast<unsigned long long>(FreeBytes()),
static_cast<unsigned long long>(needed));
return nullptr;
}
@@ -198,18 +194,16 @@ namespace MobileGL::MG_Remote::Transport {
// Consumer
// -----------------------------------------------------------------------
RingConsumer::RingConsumer(RingControl* control, void* base, std::uint64_t capacityBytes,
RingCursorSet cursors)
: m_control(control), m_base(static_cast<const std::uint8_t*>(base)),
m_capacity(capacityBytes), m_mask(capacityBytes - 1), m_cursors(cursors) {
if (control == nullptr || base == nullptr || !IsPowerOfTwo(capacityBytes) ||
capacityBytes < kMinRingCapacity || capacityBytes > kMaxRingCapacity) {
RingConsumer::RingConsumer(RingControl* control, void* base, std::uint64_t capacityBytes, RingCursorSet cursors)
: m_control(control), m_base(static_cast<const std::uint8_t*>(base)), m_capacity(capacityBytes),
m_mask(capacityBytes - 1), m_cursors(cursors) {
if (control == nullptr || base == nullptr || !IsPowerOfTwo(capacityBytes) || capacityBytes < kMinRingCapacity ||
capacityBytes > kMaxRingCapacity) {
MGLOG_E("MG_Remote ring: consumer rejected, capacity %llu must be a power of two "
"between %llu and %llu bytes over a non-null mapping (a record may be at most "
"half the ring, and the record header's size field is 32-bit, so a bigger ring "
"would truncate it)",
static_cast<unsigned long long>(capacityBytes),
static_cast<unsigned long long>(kMinRingCapacity),
static_cast<unsigned long long>(capacityBytes), static_cast<unsigned long long>(kMinRingCapacity),
static_cast<unsigned long long>(kMaxRingCapacity));
m_control = nullptr;
m_base = nullptr;
@@ -233,8 +227,7 @@ namespace MobileGL::MG_Remote::Transport {
if (available < sizeof(RingRecordHeader) || available > m_capacity) {
MGLOG_E("MG_Remote ring: %llu bytes between tail and head is impossible for a %llu "
"byte ring",
static_cast<unsigned long long>(available),
static_cast<unsigned long long>(m_capacity));
static_cast<unsigned long long>(available), static_cast<unsigned long long>(m_capacity));
if (outCorrupt != nullptr) {
*outCorrupt = true;
}
@@ -249,13 +242,12 @@ namespace MobileGL::MG_Remote::Transport {
// preceded by these bounds checks and a violation is fatal, never a
// retry (plan section 6.3, runtime bounds discipline).
const std::uint64_t size = header.size;
if (size < sizeof(RingRecordHeader) || (size % kRingRecordAlignment) != 0 ||
size > available || offset + size > m_capacity) {
if (size < sizeof(RingRecordHeader) || (size % kRingRecordAlignment) != 0 || size > available ||
offset + size > m_capacity) {
MGLOG_E("MG_Remote ring: corrupt record header at cursor %llu "
"(kind=%u flags=0x%04X size=%u available=%llu)",
static_cast<unsigned long long>(m_localTail),
static_cast<unsigned>(header.kind), static_cast<unsigned>(header.flags),
header.size, static_cast<unsigned long long>(available));
static_cast<unsigned long long>(m_localTail), static_cast<unsigned>(header.kind),
static_cast<unsigned>(header.flags), header.size, static_cast<unsigned long long>(available));
if (outCorrupt != nullptr) {
*outCorrupt = true;
}
@@ -362,8 +354,7 @@ namespace MobileGL::MG_Remote::Transport {
// already rules the same way for the cursor invariants on this page.
// "Early" can only be caught at the call site, which is why every
// advance below has exactly one caller and a named unit case.
void AdvanceMonotonic(std::atomic<std::uint64_t>& watermark, std::uint64_t to,
const char* name) {
void AdvanceMonotonic(std::atomic<std::uint64_t>& watermark, std::uint64_t to, const char* name) {
const std::uint64_t current = watermark.load(std::memory_order_relaxed);
if (to < current) {
// WireLogFatal, not MGLOG_F + abort: the line has to reach stderr
@@ -373,8 +364,7 @@ namespace MobileGL::MG_Remote::Transport {
"cannot be un-resumed, and every later advance of this watermark "
"would be a no-op, so the verb barrier and every reply wait would "
"block for ever",
name, static_cast<unsigned long long>(current),
static_cast<unsigned long long>(to));
name, static_cast<unsigned long long>(current), static_cast<unsigned long long>(to));
}
if (to == current) {
return;
@@ -423,8 +413,8 @@ namespace MobileGL::MG_Remote::Transport {
SetLink(link.Capabilities().PublishIsDelivery ? nullptr : &link);
}
void SessionProducer::Attach(RingControl* control, RingProducer* cmd, Doorbell* peerBell,
Doorbell* selfBell, std::uint32_t spinUs) {
void SessionProducer::Attach(RingControl* control, RingProducer* cmd, Doorbell* peerBell, Doorbell* selfBell,
std::uint32_t spinUs) {
m_control = control;
m_cmd = cmd;
m_peerBell = peerBell;
@@ -519,8 +509,7 @@ namespace MobileGL::MG_Remote::Transport {
}
RingControl* control = m_control;
const ScopedWakeTarget wake(control->producerWakeSeq, seq);
return Park([control, seq] { return Watermark::Reached(control->Progress.appliedSeq, seq); },
timeoutMs);
return Park([control, seq] { return Watermark::Reached(control->Progress.appliedSeq, seq); }, timeoutMs);
}
SessionWait SessionProducer::WaitForPresentAck(std::uint64_t serial, std::uint32_t timeoutMs) {
@@ -538,8 +527,7 @@ namespace MobileGL::MG_Remote::Transport {
return m_control != nullptr && m_control->eventRingFull.load(std::memory_order_acquire) != 0;
}
SessionWait SessionProducer::WaitForAppliedOrEventBacklog(std::uint64_t seq,
std::uint32_t timeoutMs) {
SessionWait SessionProducer::WaitForAppliedOrEventBacklog(std::uint64_t seq, std::uint32_t timeoutMs) {
if (!Valid()) {
return SessionWait::TimedOut;
}
@@ -553,8 +541,7 @@ namespace MobileGL::MG_Remote::Transport {
timeoutMs);
}
SessionWait SessionProducer::WaitForPresentAckOrEventBacklog(std::uint64_t serial,
std::uint32_t timeoutMs) {
SessionWait SessionProducer::WaitForPresentAckOrEventBacklog(std::uint64_t serial, std::uint32_t timeoutMs) {
if (!Valid()) {
return SessionWait::TimedOut;
}
@@ -588,8 +575,8 @@ namespace MobileGL::MG_Remote::Transport {
SetLink(link.Capabilities().PublishIsDelivery ? nullptr : &link);
}
void SessionConsumer::Attach(RingControl* control, RingConsumer* cmd, Doorbell* peerBell,
Doorbell* selfBell, std::uint32_t spinUs) {
void SessionConsumer::Attach(RingControl* control, RingConsumer* cmd, Doorbell* peerBell, Doorbell* selfBell,
std::uint32_t spinUs) {
m_control = control;
m_cmd = cmd;
m_peerBell = peerBell;
@@ -619,10 +606,8 @@ namespace MobileGL::MG_Remote::Transport {
if (m_link && m_link->FlushProgress() != MOBILEGL_OK) return SessionWait::ShutDown;
const bool woke = m_selfBell->Wait(
control->consumerParked,
[control, cmd] {
return control->cmdHead.load(std::memory_order_acquire) != cmd->LocalTail();
},
m_spinUs, timeoutMs);
[control, cmd] { return control->cmdHead.load(std::memory_order_acquire) != cmd->LocalTail(); }, m_spinUs,
timeoutMs);
if (woke) {
return SessionWait::Reached;
}
+17 -26
View File
@@ -65,12 +65,12 @@ namespace MobileGL::MG_Remote::Transport {
alignas(64) LinkProgress Progress;
// ---- doorbell / generation -------------------------------------------
alignas(64) std::atomic<std::uint32_t> serverEpoch; // ++ on context loss / server restart
std::atomic<std::uint32_t> ringGeneration; // ++ after a hard drain
std::atomic<std::uint32_t> consumerParked; // server asleep, producer must ring
std::atomic<std::uint32_t> producerParked; // client asleep, server must ring
std::atomic<std::uint32_t> eventRingFull; // SEG_EVENT full, server stopped applying
std::atomic<std::uint32_t> eventDropped; // dropped lossy events
alignas(64) std::atomic<std::uint32_t> serverEpoch; // ++ on context loss / server restart
std::atomic<std::uint32_t> ringGeneration; // ++ after a hard drain
std::atomic<std::uint32_t> consumerParked; // server asleep, producer must ring
std::atomic<std::uint32_t> producerParked; // client asleep, server must ring
std::atomic<std::uint32_t> eventRingFull; // SEG_EVENT full, server stopped applying
std::atomic<std::uint32_t> eventDropped; // dropped lossy events
// ---- the parked producer's wake target -------------------------------
//
@@ -120,8 +120,7 @@ namespace MobileGL::MG_Remote::Transport {
// there. Growth INSIDE LinkProgress is caught, by sizeof below; growth BESIDE it is
// not, and the only thing standing between that and a false-sharing regression is
// this comment and the group's own.
static_assert(offsetof(RingControl, Progress) % 64 == 0,
"LinkProgress starts a cache line of its own");
static_assert(offsetof(RingControl, Progress) % 64 == 0, "LinkProgress starts a cache line of its own");
static_assert(offsetof(RingControl, serverEpoch) == offsetof(RingControl, Progress) + 64,
"the Progress line holds Progress and nothing else");
@@ -130,12 +129,9 @@ namespace MobileGL::MG_Remote::Transport {
// roles map, and swapping two of them passes every other check in this file.
static_assert(offsetof(LinkProgress, appliedSeq) == 0, "LinkProgress field order: appliedSeq");
static_assert(offsetof(LinkProgress, retiredSeq) == 8, "LinkProgress field order: retiredSeq");
static_assert(offsetof(LinkProgress, completedFrameSerial) == 16,
"LinkProgress field order: completedFrameSerial");
static_assert(offsetof(LinkProgress, presentAckSerial) == 24,
"LinkProgress field order: presentAckSerial");
static_assert(sizeof(LinkProgress) == 32,
"four consumer-written watermarks; the event flags are NOT in here");
static_assert(offsetof(LinkProgress, completedFrameSerial) == 16, "LinkProgress field order: completedFrameSerial");
static_assert(offsetof(LinkProgress, presentAckSerial) == 24, "LinkProgress field order: presentAckSerial");
static_assert(sizeof(LinkProgress) == 32, "four consumer-written watermarks; the event flags are NOT in here");
// The two event flags stay in the doorbell group on purpose (see the group's comment).
// LinkEventFlags mirrors them, so the mirror's shape is pinned too.
@@ -143,8 +139,8 @@ namespace MobileGL::MG_Remote::Transport {
"the two event flags are adjacent, in that order");
static_assert(offsetof(RingControl, eventRingFull) / 64 == offsetof(RingControl, serverEpoch) / 64,
"the event flags share the doorbell group's line, NOT the Progress line");
static_assert(offsetof(LinkEventFlags, eventRingFull) == 0 &&
offsetof(LinkEventFlags, eventDropped) == 4 && sizeof(LinkEventFlags) == 8,
static_assert(offsetof(LinkEventFlags, eventRingFull) == 0 && offsetof(LinkEventFlags, eventDropped) == 4 &&
sizeof(LinkEventFlags) == 8,
"LinkEventFlags mirrors the pair in RingControl");
// The page's real content must fit the page it claims. sizeof(RingControl) == 4096
@@ -181,7 +177,7 @@ namespace MobileGL::MG_Remote::Transport {
kRecNone = 0,
kRecNeedsAck = 1u << 0,
kRecHasBlob = 1u << 1,
kRecPad = 1u << 2, // filler to the wrap boundary, no payload meaning
kRecPad = 1u << 2, // filler to the wrap boundary, no payload meaning
kRecBorrowSlot = 1u << 3, // slot is borrowed into the GPU timeline; retires late
kRecVarTail = 1u << 4,
// P12: THE PEER NEED NOT ANSWER THIS RECORD. Set by the producer for a record whose
@@ -229,8 +225,7 @@ namespace MobileGL::MG_Remote::Transport {
// head >= appliedTail >= retiredTail, and the ring never holds more than
// its capacity. False means the shared page is corrupt (or a peer is
// misbehaving), which is a Fatal{ProtocolCorruption}, never a retry.
bool RingCursorsValid(const RingControl& control, RingCursorSet cursors,
std::uint64_t capacityBytes);
bool RingCursorsValid(const RingControl& control, RingCursorSet cursors, std::uint64_t capacityBytes);
// Bumps ringGeneration, invalidating every offset either side has cached.
// Both sides must be quiesced and the ring fully drained
@@ -254,8 +249,7 @@ namespace MobileGL::MG_Remote::Transport {
// `base` is the ring's byte area (NOT the control page) and
// `capacityBytes` must be a power of two between kMinRingCapacity and
// kMaxRingCapacity. Anything else leaves Valid() false.
RingProducer(RingControl* control, void* base, std::uint64_t capacityBytes,
RingCursorSet cursors);
RingProducer(RingControl* control, void* base, std::uint64_t capacityBytes, RingCursorSet cursors);
bool Valid() const { return m_control != nullptr; }
@@ -287,9 +281,7 @@ namespace MobileGL::MG_Remote::Transport {
private:
std::uint64_t TailForReclaim() const;
std::uint8_t* SlotAt(std::uint64_t cursor) const {
return m_base + static_cast<std::size_t>(cursor & m_mask);
}
std::uint8_t* SlotAt(std::uint64_t cursor) const { return m_base + static_cast<std::size_t>(cursor & m_mask); }
RingControl* m_control = nullptr;
std::uint8_t* m_base = nullptr;
@@ -303,8 +295,7 @@ namespace MobileGL::MG_Remote::Transport {
class RingConsumer {
public:
RingConsumer() = default;
RingConsumer(RingControl* control, void* base, std::uint64_t capacityBytes,
RingCursorSet cursors);
RingConsumer(RingControl* control, void* base, std::uint64_t capacityBytes, RingCursorSet cursors);
bool Valid() const { return m_control != nullptr; }
+1 -2
View File
@@ -94,8 +94,7 @@ namespace MobileGL::MG_Remote::Transport {
// (R-16's control on this rule: revert PeakRssBytes to the kernel's peak and
// it does).
RoleMemorySample SampleRoleMemoryInto(std::atomic<std::uint64_t>& runningPeak, MemoryRole role,
std::uint64_t kernelPeakRssBytes,
std::uint64_t kernelCurrentRssBytes);
std::uint64_t kernelPeakRssBytes, std::uint64_t kernelCurrentRssBytes);
// Emits one line at INFO level, which is what every P5 lane builds at, so
// t1's harness can grep it out of a lane log without a new log sink. Not
+5 -1
View File
@@ -52,7 +52,11 @@ namespace MobileGL::MG_Remote::Transport {
LinkCapabilities ShmLink::Capabilities() const {
const ReplySlotPool replies(m_memory.ReplyBase(), m_memory.ReplyBytes(), m_memory.ReplySlotCount());
// RetainsReplies stays false: a slot is seq % slotCount and the next answer into it wins.
return {true, true, true, m_memory.CmdRingCapacity() / 2, replies.MaxReplyBytes(),
return {true,
true,
true,
m_memory.CmdRingCapacity() / 2,
replies.MaxReplyBytes(),
/*RetainsReplies=*/false};
}
LinkProgress* ShmLink::Progress() {
+54 -45
View File
@@ -32,9 +32,13 @@
namespace MobileGL::MG_Remote::Transport {
ShmSegment::~ShmSegment() { Close(); }
ShmSegment::~ShmSegment() {
Close();
}
ShmSegment::ShmSegment(ShmSegment&& other) noexcept { Steal(std::move(other)); }
ShmSegment::ShmSegment(ShmSegment&& other) noexcept {
Steal(std::move(other));
}
ShmSegment& ShmSegment::operator=(ShmSegment&& other) noexcept {
if (this != &other) {
@@ -60,7 +64,9 @@ namespace MobileGL::MG_Remote::Transport {
other.m_readOnly = false;
}
bool ShmSegment::Valid() const { return m_size != 0 && (m_fd >= 0 || m_nativeHandle != nullptr); }
bool ShmSegment::Valid() const {
return m_size != 0 && (m_fd >= 0 || m_nativeHandle != nullptr);
}
// =======================================================================
// P5: the role memory ledger and the VmHWM sample
@@ -172,17 +178,14 @@ namespace MobileGL::MG_Remote::Transport {
}
RoleMemorySample SampleRoleMemoryInto(std::atomic<std::uint64_t>& runningPeak, MemoryRole role,
std::uint64_t kernelPeakRssBytes,
std::uint64_t kernelCurrentRssBytes) {
std::uint64_t kernelPeakRssBytes, std::uint64_t kernelCurrentRssBytes) {
// max(running, kernel peak, kernel current), as a CAS loop: two threads
// sampling at once (the GL thread and the apply thread both log memory
// at phase boundaries) must not let a lower value overwrite a higher.
const std::uint64_t observed =
kernelPeakRssBytes > kernelCurrentRssBytes ? kernelPeakRssBytes : kernelCurrentRssBytes;
std::uint64_t peak = runningPeak.load(std::memory_order_relaxed);
while (observed > peak &&
!runningPeak.compare_exchange_weak(peak, observed, std::memory_order_relaxed)) {
}
while (observed > peak && !runningPeak.compare_exchange_weak(peak, observed, std::memory_order_relaxed)) {}
RoleMemorySample sample;
sample.Role = role;
// THE RUNNING MAXIMUM, never the kernel's peak verbatim - RoleMemory.h
@@ -222,8 +225,7 @@ namespace MobileGL::MG_Remote::Transport {
// =======================================================================
namespace {
constexpr std::size_t kSlotCount =
static_cast<std::size_t>(SessionSegmentSlot::kSessionSegmentCount);
constexpr std::size_t kSlotCount = static_cast<std::size_t>(SessionSegmentSlot::kSessionSegmentCount);
std::size_t SlotIndex(SessionSegmentSlot slot) {
const std::size_t index = static_cast<std::size_t>(slot);
@@ -231,7 +233,9 @@ namespace MobileGL::MG_Remote::Transport {
}
} // namespace
SessionSegments::~SessionSegments() { Close(); }
SessionSegments::~SessionSegments() {
Close();
}
MobileGLResult SessionSegments::Create(const SessionSegmentSizes& sizes, MemoryRole role) {
Close();
@@ -268,12 +272,10 @@ namespace MobileGL::MG_Remote::Transport {
}
for (std::size_t index = 0; index < kSlotCount; ++index) {
const MobileGLResult created =
ShmSegment::Create(specs[index].name, specs[index].bytes, m_owned[index]);
const MobileGLResult created = ShmSegment::Create(specs[index].name, specs[index].bytes, m_owned[index]);
if (created != MOBILEGL_OK) {
MGLOG_E("MG_Remote session: could not create segment %s of %llu bytes (rc=%d)",
specs[index].name, static_cast<unsigned long long>(specs[index].bytes),
static_cast<int>(created));
MGLOG_E("MG_Remote session: could not create segment %s of %llu bytes (rc=%d)", specs[index].name,
static_cast<unsigned long long>(specs[index].bytes), static_cast<int>(created));
Close();
return created;
}
@@ -311,15 +313,19 @@ namespace MobileGL::MG_Remote::Transport {
MobileGLResult SessionSegments::CreatePrivate(const SessionSegmentSizes& sizes, MemoryRole role) {
Close();
const std::uint64_t bytes[4] = {SegmentBytesForRing(sizes.CmdRingBytes), sizes.StageBytes,
sizes.ReplyBytes, SegmentBytesForRing(sizes.EventRingBytes)};
const std::uint64_t bytes[4] = {SegmentBytesForRing(sizes.CmdRingBytes), sizes.StageBytes, sizes.ReplyBytes,
SegmentBytesForRing(sizes.EventRingBytes)};
for (unsigned i = 0; i != 4; ++i) {
if (!bytes[i] || bytes[i] > static_cast<std::uint64_t>(SIZE_MAX)) {
Close(); return MOBILEGL_ERR_INVALID_ARGUMENT;
Close();
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
m_private[i] = ::operator new(static_cast<std::size_t>(bytes[i]), std::align_val_t{alignof(RingControl)},
std::nothrow);
if (!m_private[i]) {
Close();
return MOBILEGL_ERR_OUT_OF_MEMORY;
}
m_private[i] = ::operator new(static_cast<std::size_t>(bytes[i]),
std::align_val_t{alignof(RingControl)}, std::nothrow);
if (!m_private[i]) { Close(); return MOBILEGL_ERR_OUT_OF_MEMORY; }
m_privateSizes[i] = bytes[i];
std::memset(m_private[i], 0, static_cast<std::size_t>(bytes[i]));
m_mappedBytes += bytes[i];
@@ -327,15 +333,21 @@ namespace MobileGL::MG_Remote::Transport {
m_cmdControl = static_cast<RingControl*>(m_private[0]);
m_cmdRingBase = static_cast<std::uint8_t*>(m_private[0]) + sizeof(RingControl);
m_cmdRingCapacity = RingCapacityForSegment(bytes[0]);
m_stageBase = m_private[1]; m_stageBytes = bytes[1];
m_replyBase = m_private[2]; m_replyBytes = bytes[2]; m_replySlotCount = sizes.ReplySlotCount;
m_stageBase = m_private[1];
m_stageBytes = bytes[1];
m_replyBase = m_private[2];
m_replyBytes = bytes[2];
m_replySlotCount = sizes.ReplySlotCount;
m_eventControl = static_cast<RingControl*>(m_private[3]);
m_eventSegmentBase = m_private[3];
m_eventRingBase = static_cast<std::uint8_t*>(m_private[3]) + sizeof(RingControl);
m_eventRingCapacity = RingCapacityForSegment(bytes[3]);
InitRingControl(*m_cmdControl); InitRingControl(*m_eventControl);
m_role = role; m_valid = true;
LedgerAddSegment(role, m_mappedBytes); m_booked = true;
InitRingControl(*m_cmdControl);
InitRingControl(*m_eventControl);
m_role = role;
m_valid = true;
LedgerAddSegment(role, m_mappedBytes);
m_booked = true;
return MOBILEGL_OK;
}
@@ -357,14 +369,12 @@ namespace MobileGL::MG_Remote::Transport {
const ShmSegment* theirs = owner.m_segments[index];
const int duplicate = theirs == nullptr ? -1 : ::dup(theirs->Fd());
if (duplicate < 0) {
MGLOG_E("MG_Remote session: could not dup the owner's descriptor for segment %zu",
index);
MGLOG_E("MG_Remote session: could not dup the owner's descriptor for segment %zu", index);
Close();
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
// Adopt takes ownership of `duplicate` on success only.
const MobileGLResult adopted =
ShmSegment::Adopt(duplicate, theirs->Size(), m_owned[index]);
const MobileGLResult adopted = ShmSegment::Adopt(duplicate, theirs->Size(), m_owned[index]);
if (adopted != MOBILEGL_OK) {
::close(duplicate);
Close();
@@ -404,13 +414,14 @@ namespace MobileGL::MG_Remote::Transport {
return MOBILEGL_OK;
}
MobileGLResult SessionSegments::AdoptFromDescriptors(const int fds[4],
const std::uint64_t sizes[4],
std::uint32_t replySlotCount,
MemoryRole role) {
MobileGLResult SessionSegments::AdoptFromDescriptors(const int fds[4], const std::uint64_t sizes[4],
std::uint32_t replySlotCount, MemoryRole role) {
Close();
#if defined(_WIN32)
(void)fds; (void)sizes; (void)replySlotCount; (void)role;
(void)fds;
(void)sizes;
(void)replySlotCount;
(void)role;
// ShmSegment::Adopt is POSIX-only and a Windows peer resolves the section
// by the name in SegmentRef instead. Refused BY NAME rather than silently
// producing an unmapped session (CONTRACT-P6 §2.6).
@@ -432,11 +443,10 @@ namespace MobileGL::MG_Remote::Transport {
// against the announced size - which is the only bound there is on a
// peer's claim about how big a segment is (CONTRACT-P6 §4.4 notes it
// has no analogue on a link with no fd).
const MobileGLResult adopted = ShmSegment::Adopt(fds[index], sizes[index],
m_owned[index]);
const MobileGLResult adopted = ShmSegment::Adopt(fds[index], sizes[index], m_owned[index]);
if (adopted != MOBILEGL_OK) {
MGLOG_E("MG_Remote session: adopting slot %zu (fd=%d, %llu bytes) failed",
index, fds[index], static_cast<unsigned long long>(sizes[index]));
MGLOG_E("MG_Remote session: adopting slot %zu (fd=%d, %llu bytes) failed", index, fds[index],
static_cast<unsigned long long>(sizes[index]));
Close();
return adopted;
}
@@ -501,14 +511,12 @@ namespace MobileGL::MG_Remote::Transport {
m_mappedBytes += m_segments[index]->Size();
}
if (m_cmdRingCapacity == 0 || m_stageBytes == 0 || m_eventRingCapacity == 0 ||
m_replyBytes == 0) {
if (m_cmdRingCapacity == 0 || m_stageBytes == 0 || m_eventRingCapacity == 0 || m_replyBytes == 0) {
MGLOG_E("MG_Remote session: segment sizes leave no usable ring (cmd cap=%llu stage "
"cap=%llu event cap=%llu reply=%llu). A ring is the largest POWER OF TWO that "
"fits after the 4096 byte control page, so a segment must be strictly larger "
"than one page plus the smallest ring",
static_cast<unsigned long long>(m_cmdRingCapacity),
static_cast<unsigned long long>(m_stageBytes),
static_cast<unsigned long long>(m_cmdRingCapacity), static_cast<unsigned long long>(m_stageBytes),
static_cast<unsigned long long>(m_eventRingCapacity),
static_cast<unsigned long long>(m_replyBytes));
return;
@@ -529,7 +537,8 @@ namespace MobileGL::MG_Remote::Transport {
for (std::size_t index = 0; index < kSlotCount; ++index) {
m_segments[index] = nullptr;
if (m_private[index]) ::operator delete(m_private[index], std::align_val_t{alignof(RingControl)});
m_private[index] = nullptr; m_privateSizes[index] = 0;
m_private[index] = nullptr;
m_privateSizes[index] = 0;
}
m_cmdControl = nullptr;
m_cmdRingBase = nullptr;
@@ -70,8 +70,7 @@ namespace MobileGL::MG_Remote::Transport {
// on across the sysroots this builds against.
fd = static_cast<int>(::syscall(SYS_memfd_create, label, MFD_CLOEXEC));
if (fd >= 0 && ::ftruncate(fd, static_cast<off_t>(size)) != 0) {
MGLOG_E("MG_Remote shm: ftruncate(%llu) failed (errno=%d)",
static_cast<unsigned long long>(size), errno);
MGLOG_E("MG_Remote shm: ftruncate(%llu) failed (errno=%d)", static_cast<unsigned long long>(size), errno);
::close(fd);
fd = -1;
}
@@ -84,8 +83,7 @@ namespace MobileGL::MG_Remote::Transport {
// travels by SCM_RIGHTS.
char shmName[kShmNameMax];
std::snprintf(shmName, sizeof(shmName), "/mgl-%d-%u-%s", static_cast<int>(::getpid()),
g_shmCounter.fetch_add(1, std::memory_order_relaxed),
nameHint != nullptr ? nameHint : "seg");
g_shmCounter.fetch_add(1, std::memory_order_relaxed), nameHint != nullptr ? nameHint : "seg");
fd = ::shm_open(shmName, O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd < 0) {
MGLOG_E("MG_Remote shm: shm_open(%s) failed (errno=%d)", shmName, errno);
@@ -93,8 +91,8 @@ namespace MobileGL::MG_Remote::Transport {
}
::shm_unlink(shmName);
if (::ftruncate(fd, static_cast<off_t>(size)) != 0) {
MGLOG_E("MG_Remote shm: ftruncate(%llu) failed (errno=%d)",
static_cast<unsigned long long>(size), errno);
MGLOG_E("MG_Remote shm: ftruncate(%llu) failed (errno=%d)", static_cast<unsigned long long>(size),
errno);
::close(fd);
return MOBILEGL_ERR_SHM_EXHAUSTED;
}
@@ -125,8 +123,7 @@ namespace MobileGL::MG_Remote::Transport {
// the announcement claims would turn every later offset into an
// out-of-bounds map.
struct stat st{};
if (::fstat(fd, &st) == 0 && st.st_size > 0 &&
static_cast<std::uint64_t>(st.st_size) < size) {
if (::fstat(fd, &st) == 0 && st.st_size > 0 && static_cast<std::uint64_t>(st.st_size) < size) {
MGLOG_E("MG_Remote shm: peer announced %llu bytes but the descriptor is %lld",
static_cast<unsigned long long>(size), static_cast<long long>(st.st_size));
return MOBILEGL_ERR_INVALID_ARGUMENT;
@@ -159,8 +156,8 @@ namespace MobileGL::MG_Remote::Transport {
const int prot = readOnly ? PROT_READ : (PROT_READ | PROT_WRITE);
void* addr = ::mmap(nullptr, static_cast<size_t>(m_size), prot, MAP_SHARED, m_fd, 0);
if (addr == MAP_FAILED) {
MGLOG_E("MG_Remote shm: mmap of %llu bytes failed (errno=%d)",
static_cast<unsigned long long>(m_size), errno);
MGLOG_E("MG_Remote shm: mmap of %llu bytes failed (errno=%d)", static_cast<unsigned long long>(m_size),
errno);
return MOBILEGL_ERR_OUT_OF_MEMORY;
}
m_mapping = addr;
@@ -51,23 +51,20 @@ namespace MobileGL::MG_Remote::Transport {
out.Close();
char name[kShmNameMax];
std::snprintf(name, sizeof(name), "Local\\mgl-%lu-%u-%s",
static_cast<unsigned long>(::GetCurrentProcessId()),
g_sectionCounter.fetch_add(1, std::memory_order_relaxed),
nameHint != nullptr ? nameHint : "seg");
std::snprintf(name, sizeof(name), "Local\\mgl-%lu-%u-%s", static_cast<unsigned long>(::GetCurrentProcessId()),
g_sectionCounter.fetch_add(1, std::memory_order_relaxed), nameHint != nullptr ? nameHint : "seg");
wchar_t wide[kShmNameMax];
if (!ToWide(name, wide, static_cast<int>(kShmNameMax))) {
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
HANDLE section = ::CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE,
static_cast<DWORD>(size >> 32),
static_cast<DWORD>(size & 0xFFFFFFFFull), wide);
HANDLE section =
::CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, static_cast<DWORD>(size >> 32),
static_cast<DWORD>(size & 0xFFFFFFFFull), wide);
if (section == nullptr) {
MGLOG_E("MG_Remote shm: CreateFileMappingW(%s, %llu) failed (GetLastError=%lu)", name,
static_cast<unsigned long long>(size),
static_cast<unsigned long>(::GetLastError()));
static_cast<unsigned long long>(size), static_cast<unsigned long>(::GetLastError()));
return MOBILEGL_ERR_SHM_EXHAUSTED;
}
if (::GetLastError() == ERROR_ALREADY_EXISTS) {
@@ -126,12 +123,10 @@ namespace MobileGL::MG_Remote::Transport {
Unmap();
}
void* view = ::MapViewOfFile(static_cast<HANDLE>(m_nativeHandle),
readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0,
static_cast<SIZE_T>(m_size));
readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, static_cast<SIZE_T>(m_size));
if (view == nullptr) {
MGLOG_E("MG_Remote shm: MapViewOfFile of %llu bytes failed (GetLastError=%lu)",
static_cast<unsigned long long>(m_size),
static_cast<unsigned long>(::GetLastError()));
static_cast<unsigned long long>(m_size), static_cast<unsigned long>(::GetLastError()));
return MOBILEGL_ERR_OUT_OF_MEMORY;
}
m_mapping = view;
+100 -81
View File
@@ -24,22 +24,22 @@
#include <cstdio>
#if defined(__unix__) || defined(__APPLE__) || defined(__ANDROID__)
# define MOBILEGL_SOCKET_TRANSPORT_POSIX 1
# include <poll.h>
# include <sys/stat.h>
# include <sys/un.h>
# include <sys/socket.h>
# include <sys/types.h>
# include <unistd.h>
# include <fcntl.h>
# include <netdb.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# if defined(__linux__)
# include <sys/syscall.h>
# endif
#define MOBILEGL_SOCKET_TRANSPORT_POSIX 1
#include <poll.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#if defined(__linux__)
#include <sys/syscall.h>
#endif
#else
# define MOBILEGL_SOCKET_TRANSPORT_POSIX 0
#define MOBILEGL_SOCKET_TRANSPORT_POSIX 0
#endif
namespace MobileGL::MG_Remote::Transport {
@@ -49,7 +49,9 @@ namespace MobileGL::MG_Remote::Transport {
namespace {
constexpr std::uint64_t kPumpChunkBytes = 64ull * 1024;
bool TcpName(const std::string& name) { return name.compare(0, 6, "tcp://") == 0; }
bool TcpName(const std::string& name) {
return name.compare(0, 6, "tcp://") == 0;
}
bool TcpAddress(const std::string& name, addrinfo** result) {
*result = nullptr;
@@ -57,19 +59,16 @@ namespace MobileGL::MG_Remote::Transport {
std::string host, port;
if (!address.empty() && address[0] == '[') {
const auto end = address.find(']');
if (end == std::string::npos || end + 1 >= address.size() || address[end + 1] != ':')
return false;
if (end == std::string::npos || end + 1 >= address.size() || address[end + 1] != ':') return false;
host = address.substr(1, end - 1);
port = address.substr(end + 2);
} else {
const auto colon = address.find(':');
if (colon == std::string::npos || address.find(':', colon + 1) != std::string::npos)
return false;
if (colon == std::string::npos || address.find(':', colon + 1) != std::string::npos) return false;
host = address.substr(0, colon);
port = address.substr(colon + 1);
}
if (host.empty() || port.empty() || port.find_first_not_of("0123456789") != std::string::npos)
return false;
if (host.empty() || port.empty() || port.find_first_not_of("0123456789") != std::string::npos) return false;
char* end = nullptr;
const auto number = std::strtoul(port.c_str(), &end, 10);
if (*end || number == 0 || number > 65535) return false;
@@ -99,7 +98,8 @@ namespace MobileGL::MG_Remote::Transport {
bool ConfigureTcp(int fd) {
const int one = 1, idle = 2, interval = 1, count = 3, timeout = 5000;
if (::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0 ||
::setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)) != 0) return false;
::setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)) != 0)
return false;
#if defined(TCP_KEEPIDLE)
if (::setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)) != 0) return false;
#endif
@@ -118,8 +118,7 @@ namespace MobileGL::MG_Remote::Transport {
MobileGLResult ConnectTcp(const std::string& name, std::uint32_t timeoutMs, int* outFd) {
addrinfo* addresses = nullptr;
if (!TcpAddress(name, &addresses)) return MOBILEGL_ERR_INVALID_ARGUMENT;
const auto deadline = std::chrono::steady_clock::now() +
std::chrono::milliseconds(std::max(1u, timeoutMs));
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(std::max(1u, timeoutMs));
do {
for (auto* address = addresses; address; address = address->ai_next) {
const int fd = ::socket(address->ai_family, SOCK_STREAM, IPPROTO_TCP);
@@ -129,14 +128,17 @@ namespace MobileGL::MG_Remote::Transport {
bool connected = ::connect(fd, address->ai_addr, address->ai_addrlen) == 0;
if (!connected && errno == EINPROGRESS) {
const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(
deadline - std::chrono::steady_clock::now()).count();
deadline - std::chrono::steady_clock::now())
.count();
pollfd pfd{fd, POLLOUT, 0};
int rc;
do { rc = ::poll(&pfd, 1, static_cast<int>(std::max<std::int64_t>(0, remaining))); }
while (rc < 0 && errno == EINTR && std::chrono::steady_clock::now() < deadline);
do {
rc = ::poll(&pfd, 1, static_cast<int>(std::max<std::int64_t>(0, remaining)));
} while (rc < 0 && errno == EINTR && std::chrono::steady_clock::now() < deadline);
int error = 0;
socklen_t length = sizeof(error);
connected = rc > 0 && ::getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &length) == 0 && error == 0;
connected =
rc > 0 && ::getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &length) == 0 && error == 0;
}
if (connected && ConfigureTcp(fd)) {
::fcntl(fd, F_SETFL, flags);
@@ -218,11 +220,10 @@ namespace MobileGL::MG_Remote::Transport {
// SIGCHLD by construction).
int WaitReadable(int fd, std::uint32_t timeoutMs) {
for (;;) {
struct pollfd pfd {};
struct pollfd pfd{};
pfd.fd = fd;
pfd.events = POLLIN;
const int timeout =
timeoutMs == kWaitForever ? -1 : static_cast<int>(timeoutMs);
const int timeout = timeoutMs == kWaitForever ? -1 : static_cast<int>(timeoutMs);
const int rc = ::poll(&pfd, 1, timeout);
if (rc < 0 && errno == EINTR) {
continue;
@@ -281,8 +282,7 @@ namespace MobileGL::MG_Remote::Transport {
for (std::uint32_t waited = 0;; waited += 5) {
const int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
WireLogError("MG_Remote SocketTransport: socket() failed: %s",
std::strerror(errno));
WireLogError("MG_Remote SocketTransport: socket() failed: %s", std::strerror(errno));
return MOBILEGL_ERR_UNSUPPORTED;
}
if (::connect(fd, reinterpret_cast<sockaddr*>(&address), length) == 0) {
@@ -312,7 +312,10 @@ namespace MobileGL::MG_Remote::Transport {
std::size_t written = 0;
while (written < framed.size()) {
const ssize_t n = ::send(fd, framed.data() + written, framed.size() - written, MSG_NOSIGNAL);
if (n > 0) { written += static_cast<std::size_t>(n); continue; }
if (n > 0) {
written += static_cast<std::size_t>(n);
continue;
}
if (n < 0 && errno == EINTR) continue;
WireLogError("MG_Remote SocketTransport: the %s could not be sent (%zu of %zu bytes): %s", what,
written, framed.size(), std::strerror(errno));
@@ -349,8 +352,7 @@ namespace MobileGL::MG_Remote::Transport {
if (type != SOCK_STREAM) return "is not a stream socket";
sockaddr_storage local{};
socklen_t localSize = sizeof(local);
if (::getsockname(fd, reinterpret_cast<sockaddr*>(&local), &localSize) != 0 ||
local.ss_family != AF_UNIX)
if (::getsockname(fd, reinterpret_cast<sockaddr*>(&local), &localSize) != 0 || local.ss_family != AF_UNIX)
return "is not an AF_UNIX socket (a TCP pair has no descriptor channel; use tcp:// instead)";
sockaddr_storage peer{};
socklen_t peerSize = sizeof(peer);
@@ -387,8 +389,7 @@ namespace MobileGL::MG_Remote::Transport {
return MOBILEGL_ERR_UNSUPPORTED;
}
if (::bind(listenFd, reinterpret_cast<sockaddr*>(&address), length) != 0) {
WireLogError("MG_Remote SocketTransport: bind(\"%s\") failed: %s", path.c_str(),
std::strerror(errno));
WireLogError("MG_Remote SocketTransport: bind(\"%s\") failed: %s", path.c_str(), std::strerror(errno));
::close(listenFd);
return MOBILEGL_ERR_UNSUPPORTED;
}
@@ -400,8 +401,7 @@ namespace MobileGL::MG_Remote::Transport {
::chmod(path.c_str(), 0600);
}
if (::listen(listenFd, 4) != 0) {
WireLogError("MG_Remote SocketTransport: listen(\"%s\") failed: %s", path.c_str(),
std::strerror(errno));
WireLogError("MG_Remote SocketTransport: listen(\"%s\") failed: %s", path.c_str(), std::strerror(errno));
::close(listenFd);
if (!abstractEndpoint) {
::unlink(path.c_str());
@@ -423,9 +423,8 @@ namespace MobileGL::MG_Remote::Transport {
if (minted != MOBILEGL_OK) return minted;
const auto controlBind = EncodePairBind(nonce, false);
int control = -1;
const MobileGLResult first =
ConnectPresenting(path, timeoutMs, controlBind.data(), controlBind.size(), "control connection's PairBind",
&control);
const MobileGLResult first = ConnectPresenting(path, timeoutMs, controlBind.data(), controlBind.size(),
"control connection's PairBind", &control);
if (first != MOBILEGL_OK) {
return first;
}
@@ -452,7 +451,8 @@ namespace MobileGL::MG_Remote::Transport {
// instead of writing a PairBind into something that is not the server.
if (controlFd == auxFd) {
WireLogError("MG_Remote: Refuse{ProtocolMismatch} fd: endpoint names descriptor %d twice; the control "
"and aux connections are two sockets", controlFd);
"and aux connections are two sockets",
controlFd);
return MOBILEGL_ERR_INVALID_ARGUMENT;
}
const int fds[2] = {controlFd, auxFd};
@@ -482,8 +482,8 @@ namespace MobileGL::MG_Remote::Transport {
MobileGLResult result = MintNonce(nonce, sizeof(nonce));
if (result == MOBILEGL_OK) {
const auto controlBind = EncodePairBind(nonce, false);
result = SendFirstFrame(controlFd, controlBind.data(), controlBind.size(),
"fd: control connection's PairBind");
result =
SendFirstFrame(controlFd, controlBind.data(), controlBind.size(), "fd: control connection's PairBind");
}
if (result == MOBILEGL_OK) {
const auto auxBind = EncodePairBind(nonce, true);
@@ -514,7 +514,8 @@ namespace MobileGL::MG_Remote::Transport {
int rc;
do {
const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(
deadline - std::chrono::steady_clock::now()).count();
deadline - std::chrono::steady_clock::now())
.count();
rc = ::poll(&pfd, 1, static_cast<int>(std::max<std::int64_t>(0, remaining)));
} while (rc < 0 && errno == EINTR);
int error = 0;
@@ -603,8 +604,9 @@ namespace MobileGL::MG_Remote::Transport {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
const auto readExactly = [&](std::uint8_t* into, std::size_t size) -> MobileGLResult {
while (size != 0) {
const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(
deadline - std::chrono::steady_clock::now()).count();
const auto left =
std::chrono::duration_cast<std::chrono::milliseconds>(deadline - std::chrono::steady_clock::now())
.count();
if (left <= 0) return MOBILEGL_ERR_TIMEOUT;
const int ready = WaitReadable(fd, static_cast<std::uint32_t>(left));
if (ready == 0) return MOBILEGL_ERR_TIMEOUT;
@@ -636,7 +638,10 @@ namespace MobileGL::MG_Remote::Transport {
#if defined(__linux__) && defined(SYS_getrandom)
while (filled < size) {
const long n = ::syscall(SYS_getrandom, out + filled, size - filled, 0);
if (n > 0) { filled += static_cast<std::size_t>(n); continue; }
if (n > 0) {
filled += static_cast<std::size_t>(n);
continue;
}
if (n < 0 && errno == EINTR) continue;
break; // ENOSYS on an old kernel: the device file below is the same pool.
}
@@ -645,7 +650,10 @@ namespace MobileGL::MG_Remote::Transport {
const int source = ::open("/dev/urandom", O_RDONLY | O_CLOEXEC);
while (source >= 0 && filled < size) {
const ssize_t n = ::read(source, out + filled, size - filled);
if (n > 0) { filled += static_cast<std::size_t>(n); continue; }
if (n > 0) {
filled += static_cast<std::size_t>(n);
continue;
}
if (n < 0 && errno == EINTR) continue;
break;
}
@@ -653,7 +661,8 @@ namespace MobileGL::MG_Remote::Transport {
}
if (filled == size) return MOBILEGL_OK;
WireLogError("MG_Remote SocketTransport: the kernel CSPRNG gave %zu of %zu bytes; a data "
"nonce is not minted from anything weaker", filled, size);
"nonce is not minted from anything weaker",
filled, size);
return MOBILEGL_ERR_UNSUPPORTED;
}
@@ -670,8 +679,7 @@ namespace MobileGL::MG_Remote::Transport {
// Framing.h is what makes a stream safe: the message boundary is in the
// payload, not in the socket.
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, stream) != 0) {
WireLogError("MG_Remote SocketTransport: socketpair(SOCK_STREAM) failed: %s",
std::strerror(errno));
WireLogError("MG_Remote SocketTransport: socketpair(SOCK_STREAM) failed: %s", std::strerror(errno));
return MOBILEGL_ERR_UNSUPPORTED;
}
@@ -711,7 +719,9 @@ namespace MobileGL::MG_Remote::Transport {
return ::shutdown(m_streamFd, SHUT_WR) == 0 ? MOBILEGL_OK : MOBILEGL_ERR_TRANSPORT_CLOSED;
}
SocketTransport::~SocketTransport() { Shutdown(); }
SocketTransport::~SocketTransport() {
Shutdown();
}
MobileGLResult SocketTransport::SendFrame(MobileGLByteSpan bytes) {
if (bytes.size > kMaxFramePayloadSize) {
@@ -741,8 +751,7 @@ namespace MobileGL::MG_Remote::Transport {
std::uint64_t written = 0;
while (written < framed.size()) {
const ssize_t n = ::send(m_streamFd, framed.data() + written,
static_cast<std::size_t>(framed.size() - written),
MSG_NOSIGNAL);
static_cast<std::size_t>(framed.size() - written), MSG_NOSIGNAL);
if (n > 0) {
written += static_cast<std::uint64_t>(n);
continue;
@@ -759,8 +768,8 @@ namespace MobileGL::MG_Remote::Transport {
}
// SendFrame is also the log sink: do not recurse into the logger.
std::fprintf(stderr, "MG_Remote SocketTransport: send failed after %llu of %llu bytes: %s\n",
static_cast<unsigned long long>(written),
static_cast<unsigned long long>(framed.size()), std::strerror(errno));
static_cast<unsigned long long>(written), static_cast<unsigned long long>(framed.size()),
std::strerror(errno));
m_closed = true;
return MOBILEGL_ERR_TRANSPORT_CLOSED;
}
@@ -789,8 +798,7 @@ namespace MobileGL::MG_Remote::Transport {
for (;;) {
const ssize_t n = ::recv(m_streamFd, chunk, sizeof(chunk), 0);
if (n > 0) {
const MobileGLResult fed =
m_reader.Feed(chunk, static_cast<std::uint64_t>(n));
const MobileGLResult fed = m_reader.Feed(chunk, static_cast<std::uint64_t>(n));
if (fed != MOBILEGL_OK) {
// Bad magic or an over-long length. FrameReader latches
// itself failed and never recovers; mirror that here so a
@@ -824,8 +832,7 @@ namespace MobileGL::MG_Remote::Transport {
}
}
MobileGLResult SocketTransport::ReceiveFrame(MobileGLMutableByteSpan buffer,
std::uint64_t* outSize,
MobileGLResult SocketTransport::ReceiveFrame(MobileGLMutableByteSpan buffer, std::uint64_t* outSize,
std::uint32_t timeoutMs) {
if (outSize == nullptr) {
return MOBILEGL_ERR_INVALID_ARGUMENT;
@@ -893,8 +900,7 @@ namespace MobileGL::MG_Remote::Transport {
}
MobileGLResult SocketTransport::ReceiveFd(int* outFd, MobileGLMutableByteSpan sideband,
std::uint64_t* outSidebandSize,
std::uint32_t timeoutMs) {
std::uint64_t* outSidebandSize, std::uint32_t timeoutMs) {
if (m_tcp || m_auxFd < 0) {
return MOBILEGL_ERR_UNSUPPORTED;
}
@@ -926,8 +932,7 @@ namespace MobileGL::MG_Remote::Transport {
// POSIX only (CONTRACT-P6 §2.6). The refusal is NAMED at construction rather
// than at first use: a transport that accepts a connection and then fails
// every operation is the silent-fallback shape this project forbids.
MobileGLResult SocketTransport::CreatePair(std::unique_ptr<SocketTransport>&,
std::unique_ptr<SocketTransport>&) {
MobileGLResult SocketTransport::CreatePair(std::unique_ptr<SocketTransport>&, std::unique_ptr<SocketTransport>&) {
WireLogError("MG_Remote SocketTransport: unsupported on this platform - P6 lands POSIX "
"only (CONTRACT-P6 §2.6); `unix:` and `pipe:` stay named refusals");
return MOBILEGL_ERR_UNSUPPORTED;
@@ -939,30 +944,44 @@ namespace MobileGL::MG_Remote::Transport {
return MOBILEGL_ERR_UNSUPPORTED;
}
bool SocketTransport::Probe(const std::string&, std::uint32_t) { return false; }
bool SocketTransport::Probe(const std::string&, std::uint32_t) {
return false;
}
SocketTransport::SocketTransport(int, int, TransportRole role) : m_role(role) {}
SocketTransport::~SocketTransport() = default;
int SocketTransport::TakeDataFd() { return -1; }
int SocketTransport::TakeDataFd() {
return -1;
}
void SocketTransport::CloseLocalCopy() {}
MobileGLResult SocketTransport::ShutdownSend() { return MOBILEGL_ERR_UNSUPPORTED; }
MobileGLResult SocketTransport::SendFrame(MobileGLByteSpan) { return MOBILEGL_ERR_UNSUPPORTED; }
MobileGLResult SocketTransport::ReceiveFrame(MobileGLMutableByteSpan, std::uint64_t*,
std::uint32_t) {
MobileGLResult SocketTransport::ShutdownSend() {
return MOBILEGL_ERR_UNSUPPORTED;
}
std::uint64_t SocketTransport::PeekFrameSize() { return 0; }
std::uint64_t SocketTransport::BufferedBytes() { return 0; }
MobileGLResult SocketTransport::SendFrame(MobileGLByteSpan) {
return MOBILEGL_ERR_UNSUPPORTED;
}
MobileGLResult SocketTransport::ReceiveFrame(MobileGLMutableByteSpan, std::uint64_t*, std::uint32_t) {
return MOBILEGL_ERR_UNSUPPORTED;
}
std::uint64_t SocketTransport::PeekFrameSize() {
return 0;
}
std::uint64_t SocketTransport::BufferedBytes() {
return 0;
}
MobileGLResult SocketTransport::ShareFd(int, MobileGLByteSpan) {
return MOBILEGL_ERR_UNSUPPORTED;
}
MobileGLResult SocketTransport::ReceiveFd(int*, MobileGLMutableByteSpan, std::uint64_t*,
std::uint32_t) {
MobileGLResult SocketTransport::ReceiveFd(int*, MobileGLMutableByteSpan, std::uint64_t*, std::uint32_t) {
return MOBILEGL_ERR_UNSUPPORTED;
}
MobileGLResult SocketTransport::PumpOnce(std::uint32_t) { return MOBILEGL_ERR_UNSUPPORTED; }
void SocketTransport::Shutdown() { m_closed = true; }
MobileGLResult SocketTransport::PumpOnce(std::uint32_t) {
return MOBILEGL_ERR_UNSUPPORTED;
}
void SocketTransport::Shutdown() {
m_closed = true;
}
#endif
@@ -61,8 +61,7 @@ namespace MobileGL::MG_Remote::Transport {
// nothing is written; otherwise this presents one - a fresh nonce, control then aux - exactly
// as ConnectTo does, before the Hello. TRANSPORT_CLOSED when that send failed: the descriptors
// are then left open, because the server's refusal may be waiting on the control connection.
static MobileGLResult AdoptConnectedPair(int controlFd, int auxFd,
std::unique_ptr<SocketTransport>& outClient,
static MobileGLResult AdoptConnectedPair(int controlFd, int auxFd, std::unique_ptr<SocketTransport>& outClient,
bool pairedAtHandoff = false);
// ---- PH-7 (4), ID-P7-3: a TCP data connection is BOUND, not paired --------
@@ -127,8 +126,8 @@ namespace MobileGL::MG_Remote::Transport {
std::uint64_t PeekFrameSize() override;
MobileGLResult ShareFd(int fd, MobileGLByteSpan sideband) override;
MobileGLResult ReceiveFd(int* outFd, MobileGLMutableByteSpan sideband,
std::uint64_t* outSidebandSize, std::uint32_t timeoutMs) override;
MobileGLResult ReceiveFd(int* outFd, MobileGLMutableByteSpan sideband, std::uint64_t* outSidebandSize,
std::uint32_t timeoutMs) override;
void Shutdown() override;
TransportRole Role() const override { return m_role; }
+31 -21
View File
@@ -189,10 +189,10 @@ namespace MobileGL::MG_Remote::Transport {
while (size) {
const auto recvStarted = std::chrono::steady_clock::now();
const auto n = ::recv(fd, p, size, 0);
gReadNsInRecv.fetch_add(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - recvStarted).count()),
std::memory_order_relaxed);
gReadNsInRecv.fetch_add(static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - recvStarted)
.count()),
std::memory_order_relaxed);
++calls;
if (n < 0 && errno == EINTR) continue;
if (n <= 0) {
@@ -205,10 +205,10 @@ namespace MobileGL::MG_Remote::Transport {
gReadCalls.fetch_add(calls, std::memory_order_relaxed);
gReadReads.fetch_add(1, std::memory_order_relaxed);
gReadBytes.fetch_add(wanted, std::memory_order_relaxed);
gReadNsTotal.fetch_add(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - readStarted).count()),
std::memory_order_relaxed);
gReadNsTotal.fetch_add(static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - readStarted)
.count()),
std::memory_order_relaxed);
return true;
#else
(void)data;
@@ -237,9 +237,10 @@ namespace MobileGL::MG_Remote::Transport {
while (message.msg_iovlen) {
const auto syscallStarted = std::chrono::steady_clock::now();
const auto sent = ::sendmsg(fd, &message, MSG_NOSIGNAL);
gSendNsInSend.fetch_add(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - syscallStarted).count()),
gSendNsInSend.fetch_add(
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - syscallStarted)
.count()),
std::memory_order_relaxed);
++sendCalls;
if (sent < 0 && errno == EINTR) continue;
@@ -260,10 +261,10 @@ namespace MobileGL::MG_Remote::Transport {
}
gSendCalls.fetch_add(sendCalls, std::memory_order_relaxed);
gSendBytes.fetch_add(size, std::memory_order_relaxed);
gSendNsTotal.fetch_add(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - sendStarted).count()),
std::memory_order_relaxed);
gSendNsTotal.fetch_add(static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - sendStarted)
.count()),
std::memory_order_relaxed);
return true;
#else
(void)payload;
@@ -582,7 +583,11 @@ namespace MobileGL::MG_Remote::Transport {
}
LinkCapabilities StreamLink::Capabilities() const {
if (!m_impl->memory) return {};
return {false, false, false, m_impl->memory->CmdRingCapacity() / 2, m_impl->MaxReply(),
return {false,
false,
false,
m_impl->memory->CmdRingCapacity() / 2,
m_impl->MaxReply(),
/*RetainsReplies=*/true};
}
LinkProgress* StreamLink::Progress() {
@@ -769,8 +774,7 @@ namespace MobileGL::MG_Remote::Transport {
// record for the life of the session (4,536 a frame). The cursor's entries can never be
// needed again: a seq is answered once.
if (x.wantCursor >= 64) {
x.wanted.erase(x.wanted.begin(),
x.wanted.begin() + static_cast<std::ptrdiff_t>(x.wantCursor));
x.wanted.erase(x.wanted.begin(), x.wanted.begin() + static_cast<std::ptrdiff_t>(x.wantCursor));
x.wantCursor = 0;
}
}
@@ -780,9 +784,15 @@ namespace MobileGL::MG_Remote::Transport {
std::lock_guard<std::mutex> lock(x.replyMutex);
return static_cast<std::uint64_t>(x.retained.size());
}
std::uint64_t StreamLink::RetainedTotal() const { return m_impl->retainedTotal; }
std::uint64_t StreamLink::SkippedUnwanted() const { return m_impl->skippedUnwanted; }
std::uint64_t StreamLink::UnansweredWanted() const { return m_impl->unansweredWanted; }
std::uint64_t StreamLink::RetainedTotal() const {
return m_impl->retainedTotal;
}
std::uint64_t StreamLink::SkippedUnwanted() const {
return m_impl->skippedUnwanted;
}
std::uint64_t StreamLink::UnansweredWanted() const {
return m_impl->unansweredWanted;
}
void StreamLink::Detach() {
auto& x = *m_impl;
+1 -2
View File
@@ -38,8 +38,7 @@ namespace MobileGL::MG_Remote::Transport {
const int written = std::vsnprintf(line, sizeof(line), format, args);
va_end(args);
if (written < 0) {
std::snprintf(line, sizeof(line),
"MG_Remote wire: unformattable Fatal diagnostic (format=%s)", format);
std::snprintf(line, sizeof(line), "MG_Remote wire: unformattable Fatal diagnostic (format=%s)", format);
}
MGLOG_F("%s", line);
// The stderr echo is the half a death test can see (WireLog.h). Unbuffered
+2 -4
View File
@@ -21,8 +21,7 @@ namespace MobileGL::MG_Remote::Transport {
#if defined(__GNUC__) || defined(__clang__)
__attribute__((format(printf, 1, 2)))
#endif
void
WireLogError(const char* format, ...);
void WireLogError(const char* format, ...);
// Formats one line, emits it at FATAL level (MGLOG_F), ECHOES IT TO STDERR,
// and aborts. Every `Fatal{...}` this layer raises goes through here.
@@ -42,7 +41,6 @@ namespace MobileGL::MG_Remote::Transport {
#if defined(__GNUC__) || defined(__clang__)
__attribute__((format(printf, 1, 2)))
#endif
void
WireLogFatal(const char* format, ...);
void WireLogFatal(const char* format, ...);
} // namespace MobileGL::MG_Remote::Transport
File diff suppressed because it is too large Load Diff
+14 -19
View File
@@ -120,12 +120,10 @@ namespace MobileGL::MG_Remote::Wire {
// RequireDeclaredBlob, called by the decoder where the payload implies content.
// Returns true when honest; false when a fault was latched (armed session child only;
// elsewhere the check does not return). Decode-path callers return on false.
Bool CheckBlobIsHonest(MG_Pipe::MGPWireOp op, const MG_Pipe::MGPBlobRef& blob,
const SegmentTable& segments);
Bool CheckBlobIsHonest(MG_Pipe::MGPWireOp op, const MG_Pipe::MGPBlobRef& blob, const SegmentTable& segments);
// R-2.3 arm 2: the record's other fields say it carries content, so the blob must be
// declared. Fatal on an absent blob, then CheckBlobIsHonest on a present one.
Bool RequireDeclaredBlob(MG_Pipe::MGPWireOp op, const MG_Pipe::MGPBlobRef& blob,
const SegmentTable& segments);
Bool RequireDeclaredBlob(MG_Pipe::MGPWireOp op, const MG_Pipe::MGPBlobRef& blob, const SegmentTable& segments);
// R-2.3 arms 1 and 3 for MGHostSpan. Our client produces ZERO host spans
// (kCapNeedsHostIndexBytes / kCapNeedsHostUboBytes are RESERVED since P8-F, never set), so
// this firing at all is a finding, not just a corruption check.
@@ -145,8 +143,7 @@ namespace MobileGL::MG_Remote::Wire {
// A legal segment run must also contain every index the draw will consume. Shared by
// the encoder, decoder and sink so a direct sink call cannot bypass the extent gate.
// PH-1 (3): answers like the arms above (false = latched).
Bool CheckDrawUserIndices(const MG_Pipe::MGPDrawInfo& info,
const MG_Pipe::MGPDrawRange* ranges,
Bool CheckDrawUserIndices(const MG_Pipe::MGPDrawInfo& info, const MG_Pipe::MGPDrawRange* ranges,
const MG_Pipe::MGHostSpan& span);
// ---- one record's shape, computed once and read by both sides ----------------------
@@ -160,7 +157,7 @@ namespace MobileGL::MG_Remote::Wire {
inline constexpr Uint32 kMaxWireRecordTails = 3;
struct WireRecordLayout {
Uint64 PayloadBytes = 0; // sizeof the op's payload struct
Uint64 PayloadBytes = 0; // sizeof the op's payload struct
Uint64 TailOffset[kMaxWireRecordTails] = {0, 0, 0}; // from the START of the record,
// header included
Uint64 TailBytes[kMaxWireRecordTails] = {0, 0, 0};
@@ -196,8 +193,8 @@ namespace MobileGL::MG_Remote::Wire {
public:
PipeWireEncoder() = default;
PipeWireEncoder(Transport::ILink* link, SegmentTable* segments);
PipeWireEncoder(Transport::RingControl* control, Transport::RingProducer* cmd,
Transport::RingProducer* stage, SegmentTable* segments);
PipeWireEncoder(Transport::RingControl* control, Transport::RingProducer* cmd, Transport::RingProducer* stage,
SegmentTable* segments);
Bool Valid() const;
@@ -231,8 +228,8 @@ namespace MobileGL::MG_Remote::Wire {
// tail holds 8 bytes is Fatal HERE, on the producing side, rather than on a peer that
// can only report a corrupt stream. That is the same arithmetic the decoder runs, so
// the check is real rather than a restatement of the caller's own belief.
Uint64 EncodeRecord(MG_Pipe::MGPWireOp op, const void* payload, Uint64 payloadBytes,
const WireTail* tails, Uint32 tailCount, Bool noReply = false);
Uint64 EncodeRecord(MG_Pipe::MGPWireOp op, const void* payload, Uint64 payloadBytes, const WireTail* tails,
Uint32 tailCount, Bool noReply = false);
// `noReply` (P12) stamps Transport::kRecNoReply, which tells the server it may SKIP the
// answer for this record. IT IS NOT "the caller is not waiting" - that is `wantReply` on
@@ -302,7 +299,9 @@ namespace MobileGL::MG_Remote::Wire {
void SetStageRetirementDoorbell(Transport::Doorbell* bell) { m_stageRetirementBell = bell; }
using CancellationHook = Bool (*)(void* self);
void SetCancellationState(const std::atomic<bool>* lost, CancellationHook hook, void* self) {
m_sessionLost = lost; m_cancellationHook = hook; m_cancellationSelf = self;
m_sessionLost = lost;
m_cancellationHook = hook;
m_cancellationSelf = self;
}
// The fast check is used on encode; the doorbell probe only runs while
// staging or at an unsuccessful wait boundary.
@@ -312,7 +311,6 @@ namespace MobileGL::MG_Remote::Wire {
Bool CheckCancellation();
void SetStageWaitTimeoutMs(Uint32 timeoutMs) { m_stageWaitTimeoutMs = timeoutMs; }
// P5e (ra, CONTRACT-P5E §2.6): THE STAGE BELL IS A CLIENT WAIT, so it has to drain the
// reverse channel like every other one. The encoder cannot drain it itself - SEG_EVENT
// and its consumers belong to the session - so the session installs a hook and this
@@ -402,8 +400,7 @@ namespace MobileGL::MG_Remote::Wire {
public:
PipeWireDecoder() = default;
PipeWireDecoder(Transport::ILink* link, SegmentTable* segments, ReplySink* replies);
PipeWireDecoder(Transport::RingControl* control, SegmentTable* segments,
ReplySink* replies);
PipeWireDecoder(Transport::RingControl* control, SegmentTable* segments, ReplySink* replies);
Bool Valid() const;
@@ -518,8 +515,7 @@ namespace MobileGL::MG_Remote::Wire {
void PoisonResolvedRuns();
// The ONLY way this class answers a record. Fatals if `op` carries no kReplySlot -
// see LastAcceptanceKnown() for why that is a Fatal and not a log line.
void PostReply(MG_Pipe::MGPWireOp op, Uint64 seq, Int32 status, const void* bytes,
Uint64 size);
void PostReply(MG_Pipe::MGPWireOp op, Uint64 seq, Int32 status, const void* bytes, Uint64 size);
friend Bool MGPipeWireRecordApplyThunk(MG_Pipe::MGPWireOp, const void*, Uint64, Uint64);
@@ -545,7 +541,6 @@ namespace MobileGL::MG_Remote::Wire {
// The hook MGPipeApplyWireRecord dispatches to once its generated per-opcode bounds gate
// has passed. Inert unless a decoder is active on the calling thread.
Bool MGPipeWireRecordApplyThunk(MG_Pipe::MGPWireOp op, const void* record, Uint64 size,
Uint64 remaining);
Bool MGPipeWireRecordApplyThunk(MG_Pipe::MGPWireOp op, const void* record, Uint64 size, Uint64 remaining);
} // namespace MobileGL::MG_Remote::Wire