[MG_Remote, MG_Pipe, MG_Test, CI, tools, docs] (P6.5): the first wave completed and reviewed - archive v2, publication boundary, half-close, AcceptPair/role fixes, device re-validation

This commit is contained in:
2026-09-22 10:56:19 -04:00
parent 018fab0f5b
commit fe28bdb5f3
75 changed files with 4887 additions and 254 deletions
@@ -120,7 +120,12 @@ MOBILEGL_GL_API void glFinish() {
MobileGL::MG_Pipe::MGPipeClientFinish();
#endif
}
MOBILEGL_GL_API void glFlush() { MGLOG_D("Implementing function: %s(...)", __FUNCTION__); }
MOBILEGL_GL_API void glFlush() {
MGLOG_D("Implementing function: %s(...)", __FUNCTION__);
#if MOBILEGL_BUILD_DISAGGREGATED
MobileGL::MG_Pipe::MGPipeClientFlush();
#endif
}
DECLARE_GL_FUNCTION_HEAD(void, FramebufferRenderbuffer, GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer) DECLARE_GL_FUNCTION_END_NO_RETURN(void, FramebufferRenderbuffer, target, attachment, renderbuffertarget, renderbuffer)
DECLARE_GL_FUNCTION_HEAD(void, FramebufferTexture2D, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level) DECLARE_GL_FUNCTION_END_NO_RETURN(void, FramebufferTexture2D, target, attachment, textarget, texture, level)
DECLARE_GL_FUNCTION_HEAD(void, FrontFace, GLenum mode) DECLARE_GL_FUNCTION_END_NO_RETURN(void, FrontFace, mode)
+8
View File
@@ -1251,6 +1251,14 @@ namespace MobileGL::MG_Pipe {
// P5e (ra), CONTRACT-P5E §2.5. Declared in PipeMutation.h; see there for why it is not a
// ClientSession call at the GL entry point.
void MGPipeClientFlush() {
if (MG_Config::Transport == MG_Config::TransportMode::Monolith) return;
if (MG_Remote::Client::RunsAsTheServerRole()) return;
if (MG_Remote::Client::ClientSession* session = MG_Remote::Client::ClientSession::Active()) {
session->Flush();
}
}
void MGPipeClientFinish() {
if (MG_Config::Transport == MG_Config::TransportMode::Monolith) return;
if (MG_Remote::Client::RunsAsTheServerRole()) return;
@@ -11,6 +11,8 @@
#if !defined(__ANDROID__)
#include <MG_State/GLState/BufferState/BufferObject.h>
#include <MG_State/GLState/Core.h>
#include <MG_Util/Metrics/PipeStats.h>
#include <Config.h>
#define MGITEST_PERSISTENT_MAP_PEEK_LIVE 1
#endif
@@ -27,6 +29,19 @@
namespace MGITest {
bool PeekSeparateClientMapStats(unsigned long long* acquisitions, unsigned long long* pushedBytes) {
#if defined(MGITEST_PERSISTENT_MAP_PEEK_LIVE) && MOBILEGL_BUILD_DISAGGREGATED && MOBILEGL_PIPE_PUSH
if (MobileGL::MG_Config::Transport != MobileGL::MG_Config::TransportMode::Spawn) return false;
namespace Stats = MobileGL::MG_Util::PipeStats;
*acquisitions = Stats::TotalCalls(Stats::CallClass::MapPersistentRoundtrips);
*pushedBytes = Stats::TotalBytes(Stats::ByteClass::PersistentMapPush);
return true;
#else
(void)acquisitions; (void)pushedBytes;
return false;
#endif
}
bool PersistentMapPeekAvailable() {
#if defined(MGITEST_PERSISTENT_MAP_PEEK_LIVE)
return true;
@@ -58,4 +58,8 @@ namespace MGITest {
// Same contract as above: false means "could not look".
bool PeekBufferIsLivePersistentMap(unsigned int bufferName, bool* outLive);
// Separate-process clients own these counters; the remote Present window
// belongs to the server and cannot report client map acquisitions or bytes.
bool PeekSeparateClientMapStats(unsigned long long* acquisitions, unsigned long long* pushedBytes);
} // namespace MGITest
@@ -174,7 +174,7 @@ namespace MGITest::PipeStatsWindow {
// "the stats channel never reached the process". Concatenated, Last() takes whichever role
// emitted the final summary.
inline Window LastFromLaneLog() {
return Last(ReadWholeFile(LibraryLogPath()) + ReadWholeFile(ServerLibraryLogPath()));
return Last(ReadLaneLog());
}
// One counter out of that line, by its short name ("mpr", "draws", "csom"), or -1 when the
@@ -18,6 +18,15 @@ foreach(listName IN ITEMS @MGL_SPLIT_ARM_TEST_LISTS@)
foreach(entry IN LISTS ${listName})
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT
"MOBILEGL_LOG_FILE_PATH=@CMAKE_CURRENT_BINARY_DIR@/split-logs/${entry}.log")
if(entry MATCHES "TriangleScenario.TheServerStampedAVerbBoundaryOnThisDrawingFrame$")
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT
"MGITEST_STRICT_ARMING_LANE=1;MOBILEGL_PIPE_STATS=1;MOBILEGL_PIPE_STATS_PERIOD=1")
elseif(entry MATCHES "PersistentCoherentMapScenario.TheMapLandsInTheArmItsLaneDeclares$")
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT
"MGITEST_PMAP_LANE=1;MGITEST_PERSISTENT_MAP_ARM=emulated;MOBILEGL_PIPE_STATS=1;MOBILEGL_PIPE_STATS_PERIOD=1")
elseif(entry MATCHES "CtWireScenario\\.")
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT "MOBILEGL_TEST_SERVER_COUNTERS=1")
endif()
endforeach()
endforeach()
foreach(entry IN LISTS MGL_SPLIT_PA_STRICT_DRAW_TESTS MGL_SPLIT_PA_STRICT_DISPATCH_TESTS)
@@ -28,6 +37,13 @@ foreach(scenario @MGL_SPLIT_SMALL_RING_SCENARIOS@)
foreach(entry IN LISTS MGL_SPLIT_SMALL_${scenario}_TESTS)
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT
"MOBILEGL_LOG_FILE_PATH=@CMAKE_CURRENT_BINARY_DIR@/split-logs/${entry}.log")
if(entry MATCHES "TriangleScenario.TheServerStampedAVerbBoundaryOnThisDrawingFrame$")
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT
"MGITEST_STRICT_ARMING_LANE=1;MOBILEGL_PIPE_STATS=1;MOBILEGL_PIPE_STATS_PERIOD=1")
elseif(entry MATCHES "PersistentCoherentMapScenario.TheMapLandsInTheArmItsLaneDeclares$")
set_tests_properties("${entry}" PROPERTIES ENVIRONMENT
"MGITEST_PMAP_LANE=1;MGITEST_PERSISTENT_MAP_ARM=emulated;MOBILEGL_PIPE_STATS=1;MOBILEGL_PIPE_STATS_PERIOD=1")
endif()
endforeach()
endforeach()
# d1's entries are covered by MGL_SPLIT_ARM_TESTS above (P6 `t6`).
@@ -55,24 +55,30 @@ namespace {
return MobileGL::MG_Remote::Server::ServerSessionInstance().Applier().Verbs();
}
// P6 `t6`: THE COUNTERS BELOW LIVE IN THE SERVER'S PROCESS, and under spawn that is not
// this one.
//
// ServerVerbs() reads PipeApplier's in-process tallies. Under inproc the applier is a thread
// here and the numbers are this process's own; under spawn it is a thread in a DIFFERENT
// process and this process's copy stays 0 forever - so these cases would fail asserting a
// fact about somebody else's memory, which is a harness limit and not a product defect.
//
// SKIPPED BY NAME RATHER THAN EXCLUDED FROM THE LANE. Exit gate 9.2 asks that
// integration-spawn's case-name SET equal integration-split's, and a case dropped from the
// registration would make the two sets differ silently - the exact drift the shared
// registration macro exists to prevent. A skip keeps the name, states the reason, and is
// counted as a skip rather than as coverage.
//
// WHAT WOULD RETIRE IT: cross-process telemetry, which is also what `t6`'s S8 needs for
// SessionFaultCount() over a whole run. Until that exists this is the honest answer.
bool ServerCountersAreObservableHere() {
return MobileGL::MG_Config::Transport != MobileGL::MG_Config::TransportMode::Spawn;
// Inproc reads its local sink. A separate-process session snapshots that
// same sink on LogFlush, after the client fences its complete command prefix.
// TCP forwards the server log; unix+shm reads the server's local role file.
struct CounterSnapshot {
bool valid = false;
MobileGL::Uint64 resets = 0, serial = 0, deaths = 0;
};
CounterSnapshot ServerCounters() {
if (MobileGL::MG_Config::Transport != MobileGL::MG_Config::TransportMode::Spawn) {
return {true, ServerVerbs().ApplierResets(), ServerVerbs().ExpectedApplierResetSerial(),
ServerVerbs().ObjectDeaths()};
}
MGPipeSyncPeerLog();
const std::string log = PipeStatsWindow::ReadWholeFile(PipeStatsWindow::ServerLibraryLogPath());
const auto at = log.rfind("MGPipe server counters:");
if (at == std::string::npos) return {};
const PipeStatsWindow::Window window{true, log.substr(at, log.find('\n', at) - at)};
const auto resets = PipeStatsWindow::CounterOrAbsent(window, "resets");
const auto serial = PipeStatsWindow::CounterOrAbsent(window, "reset-serial");
const auto deaths = PipeStatsWindow::CounterOrAbsent(window, "deaths");
if (resets < 0 || serial < 0 || deaths < 0) return {};
return {true, static_cast<MobileGL::Uint64>(resets), static_cast<MobileGL::Uint64>(serial),
static_cast<MobileGL::Uint64>(deaths)};
}
class CtWireScenario : public ScenarioTest {
@@ -116,24 +122,20 @@ namespace {
TEST_F(CtWireScenario, ApplierResetCrossesAtThePrimedEdgeInSerialOrder) {
if (!Ready()) return;
if (!ServerCountersAreObservableHere()) {
GTEST_SKIP() << "transport=spawn: the applier's counters are in the SERVER's process "
"and this one's copy is 0 by construction. The record still crosses - "
"the retrace lane is what proves that - but this case asserts on a "
"tally only an in-process applier can publish.";
}
// The first verb of the process primes the tracker; the edge is the producer. The
// clear also gives the case its pixels, so a lane that emitted nothing is red twice
// (here and in the fixture's emit-ordinal rule).
glClearColor(0.2f, 0.4f, 0.6f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_GE(ServerVerbs().ApplierResets(), 1u)
const auto counters = ServerCounters();
ASSERT_TRUE(counters.valid) << "server control-record telemetry missing";
EXPECT_GE(counters.resets, 1u)
<< "the FreshlyPrimed edge emitted no applier_reset; the server's g_applier was "
"never reset for this session";
// The serial sequence is the session's own count (§1: asserted, never dispatched
// on): every record the sink accepted carried the serial it expected, or the counter
// and the accepted tally would disagree.
EXPECT_EQ(ServerVerbs().ExpectedApplierResetSerial(), ServerVerbs().ApplierResets())
EXPECT_EQ(counters.serial, counters.resets)
<< "an applier_reset record was dropped or replayed on the wire";
GLubyte pixel[4]{};
@@ -169,12 +171,9 @@ namespace {
// The object dies unbound so the destructor - and the death record - fire at the
// delete, and the tally is read AFTER the EmitAndWait the delete blocked on.
glBindTexture(GL_TEXTURE_2D, 0);
if (!ServerCountersAreObservableHere()) {
GTEST_SKIP() << "transport=spawn: ObjectDeaths is the SERVER process's tally and this "
"process's copy never moves. The death record itself crosses; what "
"cannot cross today is the counter that would prove it here.";
}
const MobileGL::Uint64 deathsBefore = ServerVerbs().ObjectDeaths();
const auto beforeCounters = ServerCounters();
ASSERT_TRUE(beforeCounters.valid) << "server object-death telemetry missing";
const MobileGL::Uint64 deathsBefore = beforeCounters.deaths;
glDeleteTextures(1, &texture);
// THE FENCE, the framebuffer case's exactly (MOBILEGL_IPC_BATCH_WAITS, default on):
// object_death is a kCtxObject value-class record, published WITHOUT waiting for its
@@ -183,7 +182,9 @@ namespace {
// clock-free spin changed that timing, then failed 1 in 5. A clear is kCtxVerb and
// still waits, and its wait covers the death.
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_GT(ServerVerbs().ObjectDeaths(), deathsBefore)
const auto afterCounters = ServerCounters();
ASSERT_TRUE(afterCounters.valid) << "server object-death telemetry missing";
EXPECT_GT(afterCounters.deaths, deathsBefore)
<< "the texture's death produced no object_death record; the server's twin was "
"never told to let go";
@@ -230,12 +231,9 @@ namespace {
// Die unbound, framebuffer first so the attachment's own death is a separate record.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
if (!ServerCountersAreObservableHere()) {
GTEST_SKIP() << "transport=spawn: ObjectDeaths is the SERVER process's tally and this "
"process's copy never moves. The death record itself crosses; what "
"cannot cross today is the counter that would prove it here.";
}
const MobileGL::Uint64 deathsBefore = ServerVerbs().ObjectDeaths();
const auto beforeCounters = ServerCounters();
ASSERT_TRUE(beforeCounters.valid) << "server object-death telemetry missing";
const MobileGL::Uint64 deathsBefore = beforeCounters.deaths;
glDeleteFramebuffers(1, &fbo);
// THE FENCE (MOBILEGL_IPC_BATCH_WAITS, default on): object_death is a kCtxObject
// value-class record - published WITHOUT waiting for its own apply (production-safe:
@@ -243,7 +241,9 @@ namespace {
// server-side counter this assertion reads only moves at apply time, so it needs a
// wait boundary: a clear is kCtxVerb and still waits, and its wait covers the death.
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_GT(ServerVerbs().ObjectDeaths(), deathsBefore)
const auto afterCounters = ServerCounters();
ASSERT_TRUE(afterCounters.valid) << "server object-death telemetry missing";
EXPECT_GT(afterCounters.deaths, deathsBefore)
<< "the framebuffer's death produced no object_death record - and no other "
"opcode can carry it";
glDeleteRenderbuffers(1, &renderbuffer);
@@ -466,6 +466,8 @@ void main() { oColor = vec4(vColor, 1.0); }
}
Gl().EndFrame(); // close the setup window; SetUp's own store and map go in it
unsigned long long acquisitionsBefore = 0, pushedBefore = 0;
const bool separateClientStats = PeekSeparateClientMapStats(&acquisitionsBefore, &pushedBefore);
// One acquisition inside the counted window: one fresh immutable store, mapped once.
const CoherentStore store = MakeCoherentlyMappedQuadStore();
@@ -500,7 +502,16 @@ void main() { oColor = vec4(vColor, 1.0); }
"not reach the process, or nothing reached PipeStats::OnPresent.";
RecordProperty("stats_line", window.line.c_str());
const long long roundtrips = PipeStatsWindow::CounterOrAbsent(window, "mpr");
unsigned long long acquisitionsAfter = 0, pushedAfter = 0;
if (separateClientStats) {
ASSERT_TRUE(PeekSeparateClientMapStats(&acquisitionsAfter, &pushedAfter));
ASSERT_GE(acquisitionsAfter, acquisitionsBefore);
ASSERT_GE(pushedAfter, pushedBefore);
RecordProperty("stats_counter_owner", "client process totals over the counted workload");
}
const long long roundtrips = separateClientStats
? static_cast<long long>(acquisitionsAfter - acquisitionsBefore)
: PipeStatsWindow::CounterOrAbsent(window, "mpr");
ASSERT_GE(roundtrips, 0) << "the summary line carries no mpr= field: " << window.line;
EXPECT_EQ(roundtrips, kExpectedRoundtrips)
<< "one PERSISTENT|WRITE|COHERENT map of one freshly defined store is ONE acquisition "
@@ -523,7 +534,9 @@ void main() { oColor = vec4(vColor, 1.0); }
// cross-check. Deriving the arm from pmap would have made "the push was never wired" and
// "the store was adopted" the same reading, and they are different defects with different
// owners.
const double pushedBytes = PipeStatsWindow::CounterAsDoubleOrAbsent(window, "pmap");
const double pushedBytes = separateClientStats
? static_cast<double>(pushedAfter - pushedBefore)
: PipeStatsWindow::CounterAsDoubleOrAbsent(window, "pmap");
ASSERT_GE(pushedBytes, 0.0) << "the summary line carries no pmap= field: " << window.line;
RecordProperty("persistent_map_push_bytes_per_frame", std::to_string(pushedBytes).c_str());
RecordProperty("map_persistent_roundtrips", std::to_string(roundtrips).c_str());
+2
View File
@@ -263,6 +263,8 @@ namespace MobileGL::MG_Pipe {
// monolith transport, and on a lockstep one (there the client waited out every command it
// issued before it could reach the call).
void MGPipeClientFinish();
// Submit a stream link's published prefix; never wait for appliedSeq.
void MGPipeClientFlush();
#endif
void MGPipeEmitResourceCreate(MG_State::GLState::BufferObject& buffer);
+4
View File
@@ -321,6 +321,10 @@ namespace MobileGL::MG_Pipe {
MGPReplySlot reply = MGPipeMintReplySlot();
MGP_ResourceSubData(&record, bytes, byteCount, regions, record.RegionCount, &reply);
const Bool accepted = MGPipeTakeReplyBool(reply, "resource_subdata");
#if MOBILEGL_BUILD_DISAGGREGATED
// A cancelled wire upload is declined even when no server reply was requested.
if (MGPipeInstalledArm() == MGPipeRouteArm::kClientWire) return accepted;
#endif
return MGPipeSubDataWantsItsReply(record) ? accepted : true;
}
inline void MGPipeRouteBufferSubDataResident(const MGPSubData& record, const void* bytes,
+60
View File
@@ -0,0 +1,60 @@
# P6.5 第一波:TCP 控制与 stream 数据
2026-09-22。依据 `docs/Disaggregated/ROADMAP.md` 的「立即的下一项」。本文件记录实现所需的协议细节;验收结果单独记入阶段报告,不能用本文件代替运行证据。
## 范围与配置
本波实现 POSIX client/server 的全 TCP 连接;主验收形态是 WSL x86_64 client 与 Redmi aarch64 server。保留 inproc 与 fork/UNIX 的共享段路径。Windows client、TCP 控制加共享段、压缩、内容去重、异步 reply、真实窗口均不在本波。
`MOBILEGL_TRANSPORT=spawn` 表示两进程拓扑。`MOBILEGL_IPC_CONTROL` 选择 `fork`(默认)、`unix:<path>` 或 `tcp://host:port`,`MOBILEGL_IPC_DATA=auto|shm|stream` 选择数据面。本波 TCP 只接受 stream,fork/UNIX 只接受 shm;不支持的显式组合具名拒绝。Connect 不启动本地子进程,实际 server pid 来自 Welcome。
TCP 两条连接依次为控制和数据。每条均设置 NODELAY、KEEPALIVE,Linux/Android 另设置 KEEPIDLE=2、KEEPINTVL=1、KEEPCNT=3、USER_TIMEOUT=5000。网络错误和 EOF 才触发 peer-hung-up;apply 超时不是 device loss。无 `MOBILEGL_IPC_TOKEN` 的 listener 仅能绑定 loopback;错令牌返回 Refuse,不打印令牌值。
## 握手与布局
Hello/Welcome 追加 `wireFingerprint`、`LinkTerms`,Hello 带 dial 与 token,Welcome 带实际 backend。原 union 标签不重排;Refuse 与 LogFlush 追加。Refuse 是正常的拒绝结果,不能改成 ABI mismatch abort。
wire 指纹覆盖 PipeFields 逐成员名称/offset/size、按序 opcode/payload/flags/WaitClass、记录头、句柄、render-state 字段表、blob codec 版本、协议版本、字节序、指针宽度与 caps/dynamic-parameters 布局。build stamp 单独比较:Fork 必须相同,Connect 不同只警告,`MOBILEGL_IPC_REQUIRE_SAME_BUILD=1` 则拒绝。窗口尺寸由 server 陈述,不进入布局指纹。DynamicBackendParameters 使用定宽整数;没有借机改 GLFunctionsTable ABI。
启动必须收到并采用真实的首个 CapsSnapshot,之后才能锁存 run-ahead 并宣布 session started。
后台控制 reader 尚未把 caps 放入 inbox,不能解释成 server 不支持能力;也不能先用 placeholder
永久锁成 lockstep,再期待后来的快照自动提升。后续快照仍只允许 demotion,缺少首 caps 是有界启动失败。
program archive 在 split 构建使用 v2,头中校验真实序列化 schema 摘要,且版本和摘要进入握手指纹。v1 的宿主对象大小 echo 在 Linux 是 1056,在 Android 是 0,不能用于跨端兼容判定;libstdc++ 与 libc++ 的字符串/容器布局不相同,即使两端都是 LP64。payload 按字段序列化,schema 递归覆盖字段名、次序和类型,不包含容器宿主 sizeof/offsetof。宿主大小编译绊线保留为本地维护检查;非 split 路径保留 v1,G1 不因此让步。
## 数据面与生命周期
ILink 拥有数据面存储与端点。ShmLink 保留映射、reply slot pool 和现有 ring 实现;StreamLink 拥有私有镜像。session 只缓存端点/进度指针;reserve/pop 仍使用原有非虚 ring 操作。测试兼容的原始 ring 构造入口和启动时的 descriptor 交付不作为生产链路选择入口。
数据帧含 MGLD magic、长度、kind、两个 64-bit 字段及 payload。Stage 在引用它的 Cmd 之前发送;Cmd/Event 按逻辑 cursor 严格顺序接收并检查容量。接收线程先写数据,再 release 发布 head/水位。每侧数据连接有一个独立 reader;发送在调用线程串行进行,不引入无界发送队列。
进度保留单调、`>=`、只能晚不能早的规则。Progress 必须包含 **cmdRetiredTail**:FreeBytes 依赖退役尾,只有 applied tail 不能回收 borrowed record,也不能保证环回绕。event credit 以 ClientProgress 返回。越窗、非法方向、倒退水位是 ProtocolCorruption。
server 在 park 前、Reply 之后、present/completion 变化时冲刷进度,并按 64 条记录/1 ms 合并普通进度。client 每个等待点先 Flush。Stream reply 使用 seq 标识的消息邮箱;已经观察到 applied 却没有相应 reply 必须拒绝,不能永久等待。ShmLink 保留旧 slot 语义。
Present 与 `glFlush` 也是 client 的发送边界:将已经发布的批次交给对端,保持 run-ahead,不新增 apply 等待。单靠下一次 credit wait 冲刷,会让最后一帧在 client 随后空闲时永久留在本地,credit=3 时还会把前三帧积在发送端。
已经丢失连接或在 stage/cmd 空间等待中观察到真实挂断时,编码器取消本次及后续发射:不复制新 blob、不增加 wire seq,返回 DECLINED,并保留本地 reply ticket 的合法性。只因超时不能闩 device loss;stage 空间等待使用与 barrier 一致的 120 秒上限,让传输层有时间报告半开连接的失败。
两条 TCP 连接之间没有到达顺序保证。因此 surface RPC 前先等待此前提交的命令;SurfaceReply 另携带 **eventHead**。server 冲刷事件后发回复,client 等数据 reader 收到该 head 后才完成 RPC。仅按两个 send 的先后推断 SurfaceChanged 已到达是错误的。
## 控制、日志和进程
client 的专用控制 reader 直接落盘 LogLine,其他控制消息进入有界 inbox,避免 GL 线程等数据时 server 因日志 socket 写满而停住。server 同时写本地日志并前送;client 写自己的 `<base>.server.log`。`MGPipeSyncPeerLog()` 先同步命令,再以 LogFlush/ack 对齐日志观察点。正常的无远端情形是 no-op,损坏的 ack 不能静默成功。
supervisor 接受连接后 fork,每会话独立进程,父进程不创建 backend/EGL;同时只允许一会话,第二个返回 Busy。父进程关闭自己的 socket 副本时 **不调用 shutdown**,否则会关闭 child 仍使用的连接。后端由 Hello 请求,显式 server 环境钉住的后端不匹配则拒绝。server 子进程保留 P6 的 PipeStats Init/Shutdown。
正常 client 结束请求时只先关闭控制 socket 的写半边,等 server 完成 backend 销毁、最终日志和进程退出后的 EOF,再完成本地关闭。否则连续的独立 client 会抢到前一会话仍在清理的时间窗。Busy 拒绝在读取 Hello 后发送,避免带未读请求直接 close 引发 TCP RST,丢掉已经排队的 Refuse。
trace flavour 的前台 MobileGLServerService 从 nativeLibraryDir 启动可执行 server,设置服务端角色与反递归标志,持 wake lock。`am force-stop` 终止该包的服务进程。
## 带宽和测量口径
第一波不启用压缩或内容去重。P6 gate 8 的 rd12 stage 中位/均值是 816,248 / 1,534,555 B/帧,60 fps 对应约 49 / 92 MB/s;OpenRA 短样本均值明显受加载尖峰影响。该数据已经说明 Wi-Fi 可能成为瓶颈,但没有压缩率、额外 CPU 或纹理重复率证据,不能据此选定压缩算法,也不能宣称已满足 60 fps。
`MOBILEGL_PIPE_STATS=1` 下,P65LinkMetrics 记录逐帧 reply 次数、RTT 固定桶、stage bytes、wall time 与 client thread CPU。p50/p99 为桶上界;回复的测量包含必要的发送、排队与 apply,不能冒充空载网络 ping。Stage 字节/帧除以 wall time 是负载流量;链路吞吐另用真实 TCP 64 MiB 突发测量。与 PipeStats 的同源字节计数是独立观测,不能相加。
## 验收中的两处澄清
- 车道 parity 比较 DirectGLES 的可比用例集合,而不是不同标签下的总条数;当前基线是 102 条。TCP 每条还需真实 `control=tcp data=stream server=... pid=N` 证明与本地零子进程断言。fixture 不算用例通过数,禁止用 EGL 启动失败的 SKIP 代替通过。
- lf 文档列出的四个 arming 场景中,UnlocatedIoBlock、PrimitivesGeneratedNoXfb、PointSizeDemotion 的 marker 来自 server;关闭日志前送应让对应断言变红。PipeVerifyArming 的 marker 来自 **client PipeFill**,关闭 server 前送本来就不应使它变红;它应继续通过,作为角色分离对照。不得伪造一个 server marker 来满足错误的负控预期。
+3
View File
@@ -36,6 +36,7 @@
#include "Protocol/mg_protocol_base.h"
#include <MG_Pipe/PipeWireLayout.h>
#include <MG_State/GLState/ProgramState/ProgramArtifactsCodec.h>
#include <MGGitHash.h>
#include <MG_Util/Debug/Log.h>
@@ -472,6 +473,8 @@ namespace MobileGL::MG_Remote {
inputs.FormatCapabilityFormats = MG_Backend::kFormatCapabilityFormatCount;
inputs.FormatCapabilitiesCodecVersion = kFormatCapabilitiesCodecVersion;
inputs.RendererInfoCodecVersion = kRendererInfoCodecVersion;
inputs.ProgramArtifactsCodecVersion = MG_State::GLState::kProgramArtifactsCodecVersion;
inputs.ProgramArtifactsSchema = MG_State::GLState::ProgramArtifactsSchemaFingerprint();
inputs.OpCount = static_cast<Uint64>(MG_Pipe::MGPWireOp::kOpCount);
inputs.AbiVersion = MOBILEGL_ABI_VERSION(MOBILEGL_PROTOCOL_ABI_MAJOR, MOBILEGL_PROTOCOL_ABI_MINOR);
inputs.PointerBits = sizeof(void*) * 8;
+109 -24
View File
@@ -196,9 +196,9 @@ namespace MobileGL::MG_Remote::Client {
// the queue's work is not the caller's budget being spent.
const auto wait = producer.WaitForAppliedOrEventBacklog(seq, chunkMs);
if (wait == Transport::SessionWait::Reached) {
Transport::RingControl* control = producer.Control();
if (control == nullptr ||
Transport::Watermark::Reached(control->Progress.appliedSeq, seq)) {
const auto* progress = producer.Progress();
if (progress == nullptr ||
Transport::Watermark::Reached(progress->appliedSeq, seq)) {
return wait;
}
DrainEventRing(events);
@@ -1025,8 +1025,20 @@ namespace MobileGL::MG_Remote::Client {
}
void ClientSession::SyncPeerLog() {
if (!m_started || !m_controlInbox || !m_transport || DeviceLost()) return;
WaitForApplyToCatchUp("SyncPeerLog");
if (!m_started || !m_socketTransport || !m_transport || DeviceLost()) return;
const auto submitted = m_producer.LastPublishedSeq();
m_producer.PublishAndNotify(submitted);
const auto wait = WaitForAppliedBudget(m_producer, *m_events, submitted, kRemoteControlTimeoutMs);
if (wait == Transport::SessionWait::ShutDown) {
if (const auto* bell = m_producer.SelfDoorbell(); bell && bell->PeerHungUp())
LatchDeviceLost("log synchronization woke on a peer that had hung up");
return;
}
if (wait != Transport::SessionWait::Reached) {
SessionFail(MGFatalFamily::BarrierTimeout,
"MGPipe: Fatal{BarrierTimeout, LogFlush} apply prefix was not completed");
}
DrainEventRing(*m_events);
const std::lock_guard<std::mutex> lock(m_remoteControlMutex);
const auto seq = ++m_logFlushSeq;
flatbuffers::FlatBufferBuilder builder(64);
@@ -1254,21 +1266,48 @@ namespace MobileGL::MG_Remote::Client {
m_encoder = Wire::PipeWireEncoder(m_link.get(), &m_segments);
m_encoder.SetLink(m_link->Capabilities().PublishIsDelivery ? nullptr : m_link.get());
m_encoder.SetStageRetirementDoorbell(m_producer.SelfDoorbell());
m_encoder.SetStageWaitTimeoutMs(kBarrierTimeoutMs);
m_encoder.SetCancellationState(&m_deviceLost, +[](void* self) {
auto& session = *static_cast<ClientSession*>(self);
auto* bell = session.m_producer.SelfDoorbell();
if (bell && bell->PeerHungUp()) {
session.LatchDeviceLost("staging observed a peer that had hung up");
}
return session.m_deviceLost.load(std::memory_order_acquire) || (bell && bell->Dead());
}, this);
// P5e (ra, §2.6): the stage bell is a client wait and every client wait drains. The
// encoder owns the park; the session owns SEG_EVENT, so the session lends it a drain.
m_encoder.SetStageWaitHook(
+[](void* self) { DrainEventRing(static_cast<ClientSession*>(self)->Events()); }, this);
// ---- 7. the first CapsSnapshot, if the server had a backend to publish one from.
// ONE DRAIN, ONE ADOPTER (c1): PumpControlPlane below is the only thing in the client
// that turns a CapsSnapshot into a CapsMirror generation, so R-12's "a second arrival
// IS the invalidation" lives in exactly one place. s1's step here used to stop at
// "the snapshot arrived and is verifiable"; it now goes all the way, through the same
// function every later arrival goes through.
if (PumpControlPlane() == 0) {
MGLOG_W("MG_Remote client: the handshake carried no CapsSnapshot - the caps mirror is "
"a PLACEHOLDER until one arrives. Every caps read until then answers a "
"default and says so");
// ---- 7. A real first snapshot belongs to startup, not to a later GL call.
// Inproc already queued it; an independent control reader may still be
// receiving it. Do not latch run-ahead against a placeholder: promotion
// after this first decision is deliberately forbidden.
{
std::vector<Uint8> initialCaps;
const auto received = ReceiveControlFrame(initialCaps, kHandshakeTimeoutMs);
if (received != MOBILEGL_OK) {
MGLOG_E("MG_Remote client: Refuse{InitialCapsSnapshot} no initial capabilities "
"within %u ms (rc=%d); the session was not started",
kHandshakeTimeoutMs, static_cast<int>(received));
Stop();
return received;
}
const auto* envelope = ParseEnvelope(initialCaps);
if (envelope == nullptr || envelope->msg_type() != ::MobileGL::Wire::CtrlMsg::CapsSnapshot ||
envelope->msg_as_CapsSnapshot() == nullptr ||
!AdoptCapsSnapshot(envelope->msg_as_CapsSnapshot())) {
MGLOG_E("MG_Remote client: Refuse{InitialCapsSnapshot} first post-Welcome control "
"frame is not a valid capabilities snapshot; the session was not started");
Stop();
return MOBILEGL_ERR_PROTOCOL_MISMATCH;
}
LatchRunAheadFromCaps();
// A newer snapshot already queued may demote the initial decision.
// LogLine frames on TCP were filtered by ControlInbox before this read.
PumpControlPlane();
}
// BOTH, AND m_started FIRST. `Active()` is what the integration lane's skip reads and
@@ -1363,8 +1402,7 @@ namespace MobileGL::MG_Remote::Client {
// gets right, and only then do the transports go.
m_producer.Detach();
m_link = Transport::CreateSharedLink(Transport::TransportRoleTag::ClientProducer);
m_cmd = &m_link->CommandsOut(); m_events = &m_link->EventsIn();
m_link->Memory().Close();
m_cmd = &m_link->CommandsOut(); m_events = &m_link->EventsIn();
Server::ServerSessionInstance().Close();
m_clientTransport.reset();
m_serverTransport.reset();
@@ -1379,8 +1417,7 @@ namespace MobileGL::MG_Remote::Client {
//
// 1. publish and let the server drain. Bounded: a lost record must be a red lane, not
// a hung exit.
Transport::RingControl* control = m_link->Memory().CmdControl();
if (control != nullptr) {
if (m_link->Attached()) {
// The PRODUCER's own last-published seq, not RingControl::submittedSeq. Ring.h:72-77
// permits submittedSeq to be published lazily and Ring.h:243 encourages batching
// the publish, so the shared watermark is allowed to lag the emitter - and a drain
@@ -1403,6 +1440,26 @@ namespace MobileGL::MG_Remote::Client {
// kWaitForever (CondVarDoorbell::Kill): a Notify is consumed by one Park, after which
// Doorbell::Wait re-tests a condition nothing published, finds the bell alive and
// parks again, forever. InProcessChannel::Close kills both bells.
// Request EOF starts remote backend destruction. Keep receiving until
// peer EOF, so the next client cannot overtake that session's cleanup.
if (m_socketTransport && !m_deviceLost.load(std::memory_order_acquire) &&
m_socketTransport->ShutdownSend() == MOBILEGL_OK) {
bool closed = false;
if (m_controlInbox) closed = m_controlInbox->WaitClosed(kDrainTimeoutMs);
else {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(kDrainTimeoutMs);
for (;;) {
const auto left = std::chrono::duration_cast<std::chrono::milliseconds>(
deadline - std::chrono::steady_clock::now()).count();
if (left <= 0) break;
std::vector<Uint8> finalFrame;
const auto result = ReceiveControlFrame(finalFrame, static_cast<Uint32>(left));
if (result == MOBILEGL_ERR_TRANSPORT_CLOSED) { closed = true; break; }
if (result != MOBILEGL_OK) break;
}
}
if (!closed) MGLOG_W("MG_Remote client: remote teardown did not reach EOF within %u ms", kDrainTimeoutMs);
}
if (m_transport != nullptr) {
m_transport->Shutdown();
}
@@ -1530,12 +1587,17 @@ namespace MobileGL::MG_Remote::Client {
Wire::WireOpName(op));
}
const auto declineCancelled = [&] {
if (statusOut) *statusOut = Wire::ReplySink::kStatusDeclined;
return Wire::kInvalidSeq;
};
if (DeviceLost() || m_encoder.Cancelled()) return declineCancelled();
const Bool rowCarriesReplySlot =
(MG_Pipe::MGPipeCallFlagsFor(op) & static_cast<Uint32>(MG_Pipe::kReplySlot)) != 0;
const Bool ownsReplySlot = rowCarriesReplySlot && wantReply;
const auto replyMetricStart = Transport::LinkMetricsBeginReply(ownsReplySlot);
Uint64 seq = m_encoder.EncodeRecord(op, payload, payloadBytes, tails, tailCount);
if (seq == Wire::kInvalidSeq) {
if (DeviceLost() || m_encoder.Cancelled()) return declineCancelled();
// EncodeRecord already refused individually oversized records. This one fits,
// but appliedSeq may be ahead of retiredTail: wait for actual reclaimable space,
// including the wrap pad. Prior EmitAndWait calls have published every record.
@@ -1545,6 +1607,10 @@ namespace MobileGL::MG_Remote::Client {
const Uint64 needed = layout.TotalBytes + (toEnd < layout.TotalBytes ? toEnd : 0);
const BarrierWaitScope waiting;
const auto wait = m_producer.WaitForCmdSpace(needed, kBarrierTimeoutMs);
if (wait == Transport::SessionWait::ShutDown) {
(void)m_encoder.CheckCancellation();
return declineCancelled();
}
if (wait != Transport::SessionWait::Reached) {
SessionFail(MGFatalFamily::RetirementWaitFailed, "MGPipe: Fatal{RetirementWaitFailed, \"SEG_CMD\"} - %s needs %llu "
"reclaimable bytes; the retirement wait ended on %s",
@@ -1559,6 +1625,7 @@ namespace MobileGL::MG_Remote::Client {
DrainEventRing(*m_events);
seq = m_encoder.EncodeRecord(op, payload, payloadBytes, tails, tailCount);
if (seq == Wire::kInvalidSeq) {
if (DeviceLost() || m_encoder.Cancelled()) return declineCancelled();
SessionFail(MGFatalFamily::RingOverrun, "MGPipe: Fatal{RingOverrun, \"SEG_CMD\"} - %s refused after sufficient "
"space retired", Wire::WireOpName(op));
}
@@ -1567,8 +1634,14 @@ namespace MobileGL::MG_Remote::Client {
// Publish the head, record submittedSeq, THEN ring - in that order, which is
// SessionProducer's one job and RingTest.cpp:446's pin. Notify-then-publish loses the
// wakeup.
const auto replyMetricStart = Transport::LinkMetricsBeginReply(ownsReplySlot);
m_producer.PublishAndNotify(seq);
if (op == MG_Pipe::MGPWireOp::Present) Transport::LinkMetricsPresent();
if (op == MG_Pipe::MGPWireOp::Present) {
// A credit is permission to run ahead, not permission to retain the
// last frame locally until another frame or a wait happens to arrive.
Flush();
Transport::LinkMetricsPresent();
}
// Does this row own a reply slot? kMGPipeCallFlags IS THE SINGLE SOURCE OF TRUTH
// (R-16 / ID-31) - fourteen rows now, because the four Bool acceptance entry points
@@ -1871,6 +1944,18 @@ namespace MobileGL::MG_Remote::Client {
#endif
}
void ClientSession::Flush() {
if (!m_started || !m_link) return;
const auto result = m_link->Flush();
if (result == MOBILEGL_ERR_TRANSPORT_CLOSED) {
if (m_link->PeerHungUp()) LatchDeviceLost("published commands could not reach the disconnected peer");
} else if (result != MOBILEGL_OK) {
SessionFail(MGFatalFamily::ProtocolCorruption,
"MGPipe: Fatal{ProtocolCorruption, link flush} published commands were not delivered (rc=%d)",
static_cast<int>(result));
}
}
void ClientSession::Finish() {
// glFinish. Nothing goes on the wire - Flush and Finish have no record (ARCHITECTURE
// §11) - and under lockstep there was nothing to do, because the client had already
@@ -1909,9 +1994,9 @@ namespace MobileGL::MG_Remote::Client {
// swapping, and this waiter holds the only drain.
wait = m_producer.WaitForPresentAckOrEventBacklog(awaited, kBarrierTimeoutMs);
if (wait != Transport::SessionWait::Reached) break;
Transport::RingControl* control = m_producer.Control();
if (control == nullptr ||
Transport::Watermark::Reached(control->Progress.presentAckSerial, awaited)) {
const auto* progress = m_producer.Progress();
if (progress == nullptr ||
Transport::Watermark::Reached(progress->presentAckSerial, awaited)) {
break;
}
DrainEventRing(*m_events);
@@ -245,6 +245,9 @@ namespace MobileGL::MG_Remote::Client {
// true while a run-ahead queue still holds them, so under run-ahead glFinish becomes
// exactly WaitForApplyToCatchUp + the drain that comes with it.
void Finish();
// Submit published commands without waiting for application. This is
// glFlush's contract and the frame-submit boundary for Present.
void Flush();
// The last seq this session published, whether or not it waited for it. The forced
// waits above are defined against it.
+39 -8
View File
@@ -44,6 +44,7 @@
// is legal under rule E (CONTRACT-P5C.md §5.2).
#include <MG_Impl/Pipe/SlotAllocator.h>
#include <MG_State/GLState/ProgramState/ProgramArtifactsCodec.h>
#include <MG_State/GLState/StateObjectDeathNotice.h>
#include <MG_Util/Debug/Log.h>
// P5e (pg): ID-87 asks this package for bytes-per-link measured rather than argued, and the
// archive's byte count exists exactly once - here, where it is serialised.
@@ -297,7 +298,8 @@ namespace MobileGL::MG_Remote::Client {
const Uint64 seq = session.EmitAndWait(MGPWireOp::ResourceReadback, payload,
sizeof(*payload), nullptr, 0, nullptr, 0,
&status);
reply->Id = seq;
// 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);
++g_emitted;
}
@@ -317,7 +319,8 @@ namespace MobileGL::MG_Remote::Client {
const Uint64 seq = session.EmitAndWait(MGPWireOp::ResourceCreate, payload,
sizeof(*payload), nullptr, 0, nullptr, 0,
&status);
reply->Id = seq;
// 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);
++g_emitted;
if (status == 1) ++g_declined;
@@ -330,7 +333,8 @@ namespace MobileGL::MG_Remote::Client {
const Uint64 seq = session.EmitAndWait(MGPWireOp::SetTextureParams, payload,
sizeof(*payload), nullptr, 0, nullptr, 0,
&status);
reply->Id = seq;
// 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);
++g_emitted;
if (status == 1) ++g_declined;
@@ -369,11 +373,14 @@ namespace MobileGL::MG_Remote::Client {
MGPWireOp::ResourceSubData, &record, sizeof(record),
varTail != nullptr ? &tail : nullptr, varTail != nullptr ? 1u : 0u, nullptr, 0,
&status, nullptr, wantReply);
reply->Id = seq;
MG_Pipe::MGPipePostReply(*reply, wantReply ? status : 0,
(wantReply ? status == 0 : true) ? 1u : 0u);
++g_emitted;
if (wantReply && status == 1) ++g_declined;
// Cancellation has no wire ordinal; retain the caller's nonzero local ticket.
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;
MG_Pipe::MGPipePostReply(*reply, postedStatus, postedStatus == Wire::ReplySink::kStatusOk ? 1u : 0u);
if (seq != Wire::kInvalidSeq) ++g_emitted;
if (postedStatus == Wire::ReplySink::kStatusDeclined) ++g_declined;
}
void Wire_BufferSubDataResident(const MG_Pipe::MGPSubData* payload, const void* blobBytes,
@@ -712,9 +719,30 @@ namespace MobileGL::MG_Remote::Client {
}
}
namespace {
void WireStateObjectDestroyed(MG_Pipe::MGPipeKind kind, Uint64 lifetimeId) {
(void)EmitObjectDeathRecord(kind, lifetimeId);
}
const MG_State::GLState::StateObjectDeathOps kClientStateObjectDeathOps = {
.OnDestroyed = &WireStateObjectDestroyed,
};
}
void InstallClientWireTables() {
using namespace MG_Pipe;
// DirectGLES normally installs this notice while creating its handle
// backend. An independent client never creates that backend: without a
// client emitter, NotifyAndFree silently drops object_death before
// freeing the slot. Keep inproc's existing backend dispatcher, and give
// the remote client the same wire delivery without a local twin table.
if (MG_Config::Transport == MG_Config::TransportMode::Spawn &&
MG_Config::ActiveBackendType == BackendType::DirectGLES &&
MG_State::GLState::GetStateObjectDeathOps() == nullptr) {
MG_State::GLState::SetStateObjectDeathOps(&kClientStateObjectDeathOps);
}
// A fresh install means the routed tables are live again: a Start after a previous
// session's Stop clears the teardown-refusal flag so its own routed calls are not
// refused. (Stop already puts the monolith adapters back and clears the flag; this is
@@ -778,6 +806,9 @@ namespace MobileGL::MG_Remote::Client {
// is complete, by ReinstallMonolithAfterTeardown, for the at-exit deletes that reach a
// process with no session at all. Idempotent: safe to call when nothing was installed.
g_clientTablesUninstalled.store(true, std::memory_order_release);
if (MG_State::GLState::GetStateObjectDeathOps() == &kClientStateObjectDeathOps) {
MG_State::GLState::SetStateObjectDeathOps(nullptr);
}
}
void ReinstallMonolithAfterTeardown() {
+54 -10
View File
@@ -4,6 +4,7 @@
#include "../Transport/SocketTransport.h"
#include "../Transport/WireLog.h"
#include "../Protocol/SurfaceOpCodec.h"
#include "../Handshake.h"
#include "ServerLoop.h"
#include "ServerSession.h"
#include "SurfaceControlFrame.h"
@@ -19,6 +20,7 @@
#include <memory>
#include <string>
#include <vector>
#include <chrono>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -46,6 +48,17 @@ void ForwardLog(void* user, const char* text) {
(void)transport.SendFrame({builder.GetBufferPointer(), builder.GetSize()});
}
void RefuseBusy(SocketTransport& transport, const char* detail) {
// Unread Hello bytes can make close send RST and discard our Refuse.
std::uint64_t bytes = 0;
if (transport.ReceiveFrame({nullptr, 0}, &bytes, 2000) == MOBILEGL_ERR_BUFFER_TOO_SMALL &&
bytes <= 1024 * 1024) {
std::vector<std::uint8_t> hello(static_cast<std::size_t>(bytes));
(void)transport.ReceiveFrame({hello.data(), hello.size()}, &bytes, 0);
}
Refuse(transport, Protocol::RefuseCode::Busy, detail);
}
struct FlushAck { ITransport* transport; std::uint64_t seq; };
void SendLogAck(void* pointer) {
auto& ack = *static_cast<FlushAck*>(pointer);
@@ -70,6 +83,10 @@ void SendLogAck(void* pointer) {
if (!Protocol::VerifyCtrlEnvelopeBuffer(verifier)) ::_exit(67);
const auto* hello = Protocol::GetCtrlEnvelope(helloFrame.data())->msg_as_Hello();
if (!hello) ::_exit(67);
// Reject incompatible wire before backend bring-up can obscure the cause.
if (ValidatePeerHandshake(*control, hello->abiMajor(), hello->abiMinor(),
hello->wireFingerprint(), hello->buildFingerprint() ? hello->buildFingerprint()->c_str() : nullptr,
hello->dialMode()) != MOBILEGL_OK) ::_exit(0);
if (tcp) {
const char* expected = std::getenv("MOBILEGL_IPC_TOKEN");
const std::string token = hello->token() ? hello->token()->str() : std::string();
@@ -178,6 +195,19 @@ void SendLogAck(void* pointer) {
const auto* envelope = Protocol::GetCtrlEnvelope(buffer.data());
if (const auto* flush = envelope->msg_as_LogFlush()) {
if (flush->ack()) break;
if (const char* inspect = std::getenv("MOBILEGL_TEST_SERVER_COUNTERS");
inspect && std::strcmp(inspect, "1") == 0) {
// SyncPeerLog has fenced the client's complete command prefix.
// Acquire the applier's publication before reading its counters;
// this serial client sends no more records until this ack.
const auto applied = session.DataLink()->Progress()->appliedSeq.load(std::memory_order_acquire);
const auto& verbs = session.Applier().Verbs();
MGLOG_I("MGPipe server counters: applied=%llu resets=%llu reset-serial=%llu deaths=%llu",
static_cast<unsigned long long>(applied),
static_cast<unsigned long long>(verbs.ApplierResets()),
static_cast<unsigned long long>(verbs.ExpectedApplierResetSerial()),
static_cast<unsigned long long>(verbs.ObjectDeaths()));
}
FlushAck ack{control.get(), flush->seq()};
MobileGL::MG_Util::Debug::WithLogBarrier(&SendLogAck, &ack);
continue;
@@ -197,13 +227,23 @@ void SendLogAck(void* pointer) {
MobileGL::MG_Util::PipeStats::Shutdown();
session.Close();
MobileGL::MG_Util::Debug::SetLogForwarder(nullptr, nullptr);
control->Shutdown();
// _exit closes control after cleanup/flush; peer EOF is the client's fence.
std::fflush(nullptr);
::_exit(0);
}
}
extern "C" __attribute__((visibility("default"))) int mobilegl_server_main(int argc, char** argv) {
// The supervisor and every forked session child log under the server role.
// Set it here rather than trusting the launcher to have done so: log role is
// resolved from this env at first use, and a launcher that omits it would
// silently misfile this process's main-thread server lines as the client
// role and never forward them. Done before any logging so the value is cached
// as server and inherited by the fork children. DIAL/scrub stay hard checks.
if (const char* role = std::getenv("MOBILEGL_IPC_ROLE");
!role || std::strcmp(role, "server") != 0) {
::setenv("MOBILEGL_IPC_ROLE", "server", 1);
}
const char* dial = std::getenv("MOBILEGL_IPC_DIAL");
if (!dial || std::strcmp(dial, "no") != 0) {
std::fprintf(stderr, "MG_Remote server: MOBILEGL_IPC_DIAL=no anti-recursion catch (a) missing\n");
@@ -217,15 +257,15 @@ extern "C" __attribute__((visibility("default"))) int mobilegl_server_main(int a
}
std::string endpoint;
bool serve = false;
unsigned maxSessions = 0;
for (int i = 1; i < argc; ++i) {
const std::string arg(argv[i]);
if (arg == "--serve") serve = true;
else if (arg == "--max-sessions" && i + 1 < argc) {
char* end = nullptr;
const auto count = std::strtoul(argv[++i], &end, 10);
if (*end || count == 0 || count > 1000000) return 71;
maxSessions = static_cast<unsigned>(count);
// This is a concurrency limit, not a cumulative session budget.
// P6.5 deliberately supports one active rendering session.
if (*end || count != 1) return 71;
} else if (endpoint.empty() && arg.compare(0, 2, "--") != 0) endpoint = arg;
else return 71;
}
@@ -235,10 +275,8 @@ extern "C" __attribute__((visibility("default"))) int mobilegl_server_main(int a
if (SocketTransport::Listen(endpoint, &listener) != MOBILEGL_OK) return 72;
WireLogError("MG_Remote server: pid=%d listening on %s", static_cast<int>(::getpid()), endpoint.c_str());
pid_t active = -1;
unsigned sessions = 0;
for (;;) {
if (active > 0 && ::waitpid(active, nullptr, WNOHANG) == active) active = -1;
if (maxSessions && sessions >= maxSessions && active < 0) break;
std::unique_ptr<SocketTransport> control;
const auto accepted = SocketTransport::AcceptPair(listener, serve ? 250 : 30000, control);
if (accepted == MOBILEGL_ERR_TIMEOUT && serve) continue;
@@ -249,16 +287,22 @@ extern "C" __attribute__((visibility("default"))) int mobilegl_server_main(int a
RunSession(std::move(control));
}
if (active > 0 && ::waitpid(active, nullptr, WNOHANG) == active) active = -1;
if (active > 0 || (maxSessions && sessions >= maxSessions)) {
Refuse(*control, Protocol::RefuseCode::Busy, "one session is already active");
// exit_group closes files just before waitpid can observe the exit.
// Allow that small scheduling window; a live peer remains Busy.
const auto reapDeadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(50);
while (active > 0 && std::chrono::steady_clock::now() < reapDeadline) {
if (::waitpid(active, nullptr, WNOHANG) == active) { active = -1; break; }
::usleep(1000);
}
if (active > 0) {
RefuseBusy(*control, "one session is already active");
continue;
}
std::fflush(nullptr);
const pid_t child = ::fork();
if (child == 0) { ::close(listener); RunSession(std::move(control)); }
if (child < 0) { Refuse(*control, Protocol::RefuseCode::Busy, "could not create session child"); continue; }
if (child < 0) { RefuseBusy(*control, "could not create session child"); continue; }
active = child;
++sessions;
control->CloseLocalCopy();
}
::close(listener);
@@ -14,6 +14,7 @@
#include "../CapsCodec.h"
#include "../Handshake.h"
#include "../Transport/LinkMetrics.h"
#include "../Protocol/generated/protocol_generated.h"
#include "../Transport/InProcessTransport.h"
@@ -853,6 +854,7 @@ namespace MobileGL::MG_Remote::Server {
if (!m_accepted) {
return;
}
Transport::LinkMetricsServerPresent(serial);
m_consumer.ReturnPresentCredit(serial);
FlushDataProgress();
}
@@ -74,4 +74,17 @@ namespace MobileGL::MG_Remote::Transport {
std::lock_guard<std::mutex> lock(m_mutex);
return m_frames.empty() ? 0 : m_frames.front().size();
}
bool ControlInbox::WaitClosed(std::uint32_t timeoutMs) {
std::unique_lock<std::mutex> lock(m_mutex);
// Teardown has no outstanding control caller; discard queued snapshots
// so the reader can always reach EOF through its bounded inbox.
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
for (;;) {
m_frames.clear();
m_bytes = 0;
m_cv.notify_all();
if (m_result != MOBILEGL_OK || m_stop) return true;
if (m_cv.wait_until(lock, deadline) == std::cv_status::timeout) return m_result != MOBILEGL_OK;
}
}
}
@@ -17,6 +17,7 @@ namespace MobileGL::MG_Remote::Transport {
void Stop();
MobileGLResult Receive(std::vector<std::uint8_t>& frame, std::uint32_t timeoutMs);
std::uint64_t Peek();
bool WaitClosed(std::uint32_t timeoutMs);
private:
void Read();
ITransport& m_transport;
+5 -2
View File
@@ -79,6 +79,8 @@ namespace MobileGL::MG_Remote::Transport {
// Incremental frame extractor over a raw byte stream.
class FrameReader {
public:
explicit FrameReader(std::uint32_t expectedMagic = kFrameMagic) : m_expectedMagic(expectedMagic) {}
// Feeds raw stream bytes. Validates the frame header the moment enough
// bytes for one exist - a bad magic or an oversized length is reported
// here, not swallowed.
@@ -163,11 +165,11 @@ namespace MobileGL::MG_Remote::Transport {
std::uint32_t length = 0;
std::memcpy(&magic, m_buffer.data() + m_readPos, sizeof(magic));
std::memcpy(&length, m_buffer.data() + m_readPos + 4, sizeof(length));
if (magic != kFrameMagic) {
if (magic != m_expectedMagic) {
m_failed = true;
WireLogError("MG_Remote framing: bad frame magic 0x%08X (expected 0x%08X); the "
"control stream is desynchronized and this transport is now dead",
magic, kFrameMagic);
magic, m_expectedMagic);
return MOBILEGL_ERR_PROTOCOL_MISMATCH;
}
if (length > kMaxFramePayloadSize) {
@@ -203,6 +205,7 @@ namespace MobileGL::MG_Remote::Transport {
std::uint64_t m_pendingSize = 0;
bool m_haveHeader = false;
bool m_failed = false;
std::uint32_t m_expectedMagic = kFrameMagic;
};
} // namespace MobileGL::MG_Remote::Transport
@@ -104,4 +104,22 @@ void LinkMetricsEnd() {
Emit("summary", metrics.total, ClockNs() - metrics.started, ThreadCpuNs() - metrics.cpuStarted);
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;
}
}
@@ -10,4 +10,5 @@ namespace MobileGL::MG_Remote::Transport {
void LinkMetricsReplyApplied(std::uint64_t startedNs);
void LinkMetricsStageBytes(std::uint64_t bytes);
void LinkMetricsPresent();
void LinkMetricsServerPresent(std::uint64_t serial);
}
+2
View File
@@ -693,6 +693,8 @@ namespace MobileGL::MG_Remote::Transport {
mix(inputs.FormatCapabilityFormats);
mix(inputs.FormatCapabilitiesCodecVersion);
mix(inputs.RendererInfoCodecVersion);
mix(inputs.ProgramArtifactsCodecVersion);
mix(inputs.ProgramArtifactsSchema);
mix(inputs.OpCount);
mix(inputs.AbiVersion);
mix(inputs.PointerBits);
@@ -570,6 +570,8 @@ namespace MobileGL::MG_Remote::Transport {
std::uint64_t FormatCapabilityFormats = 0;
std::uint64_t FormatCapabilitiesCodecVersion = 0;
std::uint64_t RendererInfoCodecVersion = 0;
std::uint64_t ProgramArtifactsCodecVersion = 0;
std::uint64_t ProgramArtifactsSchema = 0;
std::uint64_t OpCount = 0;
std::uint32_t AbiVersion = 0;
std::uint32_t PointerBits = 0;
+1 -1
View File
@@ -319,7 +319,7 @@ namespace MobileGL::MG_Remote::Transport {
}
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_INVALID_ARGUMENT; }
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];
@@ -354,20 +354,28 @@ namespace MobileGL::MG_Remote::Transport {
}
int accepted[2] = {-1, -1};
for (int index = 0; index < 2; ++index) {
const int ready = WaitReadable(listenFd, index == 0 ? timeoutMs : 2000);
if (ready <= 0) {
for (int fd : accepted) {
if (fd >= 0) ::close(fd);
const std::uint32_t budget = index == 0 ? timeoutMs : 2000;
for (;;) {
const int ready = WaitReadable(listenFd, budget);
if (ready <= 0) {
for (int fd : accepted) {
if (fd >= 0) ::close(fd);
}
// Half a client is not a client. Both connections or neither, so
// a peer that died between the two cannot leave a session that
// looks up but has no way to receive a descriptor.
if (index != 0 || ready < 0) WireLogError("MG_Remote SocketTransport: only %d of 2 connections arrived within "
"%u ms", index, timeoutMs);
return ready == 0 ? MOBILEGL_ERR_TIMEOUT : MOBILEGL_ERR_TRANSPORT_CLOSED;
}
// Half a client is not a client. Both connections or neither, so
// a peer that died between the two cannot leave a session that
// looks up but has no way to receive a descriptor.
if (index != 0 || ready < 0) WireLogError("MG_Remote SocketTransport: only %d of 2 connections arrived within "
"%u ms", index, timeoutMs);
return ready == 0 ? MOBILEGL_ERR_TIMEOUT : MOBILEGL_ERR_TRANSPORT_CLOSED;
}
accepted[index] = ::accept(listenFd, nullptr, nullptr);
if (accepted[index] < 0) {
accepted[index] = ::accept(listenFd, nullptr, nullptr);
if (accepted[index] >= 0) break;
// EINTR (this process reaps SIGCHLD by construction) and ECONNABORTED
// (the peer reset between poll() and accept()) are not transport
// failures: the listener is still good. Re-poll within the budget
// rather than returning TRANSPORT_CLOSED, which the --serve
// supervisor turns into a full server exit (return 73).
if (errno == EINTR || errno == ECONNABORTED) continue;
for (int fd : accepted) {
if (fd >= 0) ::close(fd);
}
@@ -456,6 +464,12 @@ namespace MobileGL::MG_Remote::Transport {
m_closed = true;
}
MobileGLResult SocketTransport::ShutdownSend() {
std::lock_guard<std::mutex> lock(m_sendMutex);
if (m_streamFd < 0 || m_closed) return MOBILEGL_ERR_TRANSPORT_CLOSED;
return ::shutdown(m_streamFd, SHUT_WR) == 0 ? MOBILEGL_OK : MOBILEGL_ERR_TRANSPORT_CLOSED;
}
SocketTransport::~SocketTransport() { Shutdown(); }
MobileGLResult SocketTransport::SendFrame(MobileGLByteSpan bytes) {
@@ -548,9 +562,9 @@ namespace MobileGL::MG_Remote::Transport {
return MOBILEGL_OK;
}
if (n == 0) {
// Clean EOF. The peer is gone - but anything already reassembled
// stays readable, which ITransport promises in as many words.
m_closed = true;
// Request EOF can be a half-close: drain queued frames and keep
// the reverse direction usable for teardown diagnostics.
m_readClosed = true;
return MOBILEGL_ERR_TRANSPORT_CLOSED;
}
if (errno == EINTR) {
@@ -589,7 +603,7 @@ namespace MobileGL::MG_Remote::Transport {
if (m_reader.HasMessage()) {
return m_reader.TakeMessage(buffer, outSize);
}
if (m_closed) {
if (m_closed || m_readClosed) {
return MOBILEGL_ERR_TRANSPORT_CLOSED;
}
@@ -609,7 +623,7 @@ namespace MobileGL::MG_Remote::Transport {
if (m_failed) {
return 0;
}
if (!m_reader.HasMessage() && !m_closed) {
if (!m_reader.HasMessage() && !m_closed && !m_readClosed) {
// A non-blocking pump, so a caller that has not received yet can
// still size its buffer. Errors are not reported here: PeekFrameSize
// answers "how big is the next message", and 0 means "none buffered",
@@ -677,6 +691,7 @@ namespace MobileGL::MG_Remote::Transport {
SocketTransport::~SocketTransport() = default;
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*,
@@ -132,6 +132,8 @@ namespace MobileGL::MG_Remote::Transport {
// Parent of a session child closes its duplicate without shutdown(2),
// which would also disconnect the child's inherited socket.
void CloseLocalCopy();
// End requests while retaining the receive side for final diagnostics/EOF.
MobileGLResult ShutdownSend();
private:
// Pulls whatever the socket has into the reassembler. Returns
@@ -153,6 +155,7 @@ namespace MobileGL::MG_Remote::Transport {
FrameReader m_reader;
std::atomic<bool> m_closed{false};
std::atomic<bool> m_readClosed{false};
bool m_failed = false; // framing violated: latched, never recovers
};
+96 -47
View File
@@ -15,6 +15,7 @@
#if !defined(_WIN32)
#include <cerrno>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#if defined(__linux__)
#include <pthread.h>
@@ -23,6 +24,8 @@
namespace MobileGL::MG_Remote::Transport {
namespace {
constexpr std::uint64_t kDataEnvelopeBytes = 20;
constexpr std::uint64_t kMaxDataPayload = kMaxFramePayloadSize - kDataEnvelopeBytes;
constexpr std::uint32_t kDataMagic = 0x444c474d; // MGLD, little endian
enum Kind : unsigned {
Cmd = 1,
@@ -102,32 +105,17 @@ namespace MobileGL::MG_Remote::Transport {
return memory->ReplyBytes() / memory->ReplySlotCount() - sizeof(ReplySlotHeader);
}
void Die(bool peer) {
closed.store(true, std::memory_order_release);
{
// Pair every reply-wait predicate transition with its mutex:
// an EOF between the predicate and cv.wait must not lose a wake.
std::lock_guard<std::mutex> lock(replyMutex);
closed.store(true, std::memory_order_release);
}
consumer.Kill(peer);
producer.Kill(peer);
replyReady.notify_all();
#if !defined(_WIN32)
if (fd >= 0) ::shutdown(fd, SHUT_RDWR);
#endif
}
bool Write(const void* data, std::size_t size) {
#if !defined(_WIN32)
auto* p = static_cast<const std::uint8_t*>(data);
while (size) {
const auto n = ::send(fd, p, size, MSG_NOSIGNAL);
if (n < 0 && errno == EINTR) continue;
if (n <= 0) {
Die(!stopping.load(std::memory_order_acquire));
return false;
}
p += n;
size -= static_cast<std::size_t>(n);
}
return true;
#else
(void)data;
(void)size;
return false;
#endif
}
bool Read(void* data, std::size_t size) {
@@ -151,20 +139,50 @@ namespace MobileGL::MG_Remote::Transport {
#endif
}
bool Send(unsigned kind, std::uint64_t a, std::uint64_t b, const void* payload, std::uint64_t size) {
if (size > kMaxFramePayloadSize) Corrupt("outgoing frame exceeds limit");
if (size > kMaxDataPayload) Corrupt("outgoing frame exceeds limit");
std::uint8_t h[28]{};
Put32(h, kDataMagic);
Put32(h + 4, static_cast<std::uint32_t>(size));
Put32(h + 4, static_cast<std::uint32_t>(kDataEnvelopeBytes + size));
h[8] = static_cast<std::uint8_t>(kind);
Put64(h + 12, a);
Put64(h + 20, b);
return Write(h, sizeof h) && (!size || Write(payload, static_cast<std::size_t>(size)));
#if !defined(_WIN32)
// One gather write keeps a small command and its envelope in one
// TCP send (TCP_NODELAY is enabled). Partial writes retain the same
// frame lock and advance through both vectors without copying.
iovec vectors[2] = {{h, sizeof h}, {const_cast<void*>(payload), static_cast<std::size_t>(size)}};
msghdr message{};
message.msg_iov = vectors;
message.msg_iovlen = size ? 2 : 1;
while (message.msg_iovlen) {
const auto sent = ::sendmsg(fd, &message, MSG_NOSIGNAL);
if (sent < 0 && errno == EINTR) continue;
if (sent <= 0) {
Die(!stopping.load(std::memory_order_acquire));
return false;
}
auto consumed = static_cast<std::size_t>(sent);
while (message.msg_iovlen && consumed >= message.msg_iov->iov_len) {
consumed -= message.msg_iov->iov_len;
++message.msg_iov;
--message.msg_iovlen;
}
if (message.msg_iovlen && consumed) {
message.msg_iov->iov_base = static_cast<std::uint8_t*>(message.msg_iov->iov_base) + consumed;
message.msg_iov->iov_len -= consumed;
}
}
return true;
#else
(void)payload;
return false;
#endif
}
bool SendRing(unsigned kind, void* base, std::uint64_t capacity, std::uint64_t head, std::uint64_t& sent) {
if (head < sent || head - sent > capacity) Corrupt("outgoing ring window invalid");
while (sent < head) {
const auto at = sent % capacity;
const auto count = std::min({head - sent, capacity - at, kMaxFramePayloadSize});
const auto count = std::min({head - sent, capacity - at, kMaxDataPayload});
if (!Send(kind, sent, 0, static_cast<std::uint8_t*>(base) + at, count)) return false;
sent += count;
}
@@ -193,23 +211,31 @@ namespace MobileGL::MG_Remote::Transport {
return Send(ProgressFrame, 0, 0, p, sizeof p);
}
void ReceiveRing(void* base, std::uint64_t capacity, std::uint64_t tail, std::uint64_t a,
const std::vector<std::uint8_t>& p, std::uint64_t& received) {
if (a != received || a < tail || a - tail > capacity || p.size() > capacity - (a - tail) ||
p.size() > capacity - a % capacity || p.empty())
const std::uint8_t* payload, std::uint64_t size, std::uint64_t& received) {
if (a != received || a < tail || a - tail > capacity || size > capacity - (a - tail) ||
size > capacity - a % capacity || size == 0)
Corrupt("ring frame outside receive window");
std::memcpy(static_cast<std::uint8_t*>(base) + a % capacity, p.data(), p.size());
received += p.size();
std::memcpy(static_cast<std::uint8_t*>(base) + a % capacity, payload, static_cast<std::size_t>(size));
received += size;
}
void Loop() {
#if defined(__linux__)
pthread_setname_np(pthread_self(), Client() ? "mgl-cli-io" : "mgl-srv-io");
#endif
FrameReader frames(kDataMagic);
for (;;) {
std::uint8_t h[28];
if (!Read(h, sizeof h)) return;
const auto size = Get32(h + 4), kind = unsigned(h[8]);
if (!Read(h, kFrameHeaderSize)) return;
if (frames.Feed(h, kFrameHeaderSize) != MOBILEGL_OK) Corrupt("invalid data frame header");
const auto framedSize = Get32(h + 4);
if (framedSize < kDataEnvelopeBytes) Corrupt("truncated data envelope");
if (!Read(h + kFrameHeaderSize, kDataEnvelopeBytes)) return;
if (frames.Feed(h + kFrameHeaderSize, kDataEnvelopeBytes) != MOBILEGL_OK)
Corrupt("invalid data frame envelope");
const auto size = framedSize - static_cast<std::uint32_t>(kDataEnvelopeBytes);
const auto kind = unsigned(h[8]);
const auto a = Get64(h + 12), b = Get64(h + 20);
if (Get32(h) != kDataMagic || h[9] || h[10] || h[11] || size > kMaxFramePayloadSize)
if (Get32(h) != kDataMagic || h[9] || h[10] || h[11] || size > kMaxDataPayload)
Corrupt("invalid data frame header");
// Reject the size/window before allocating or reading a hostile payload.
if ((kind == Stage && (Client() || a > memory->StageBytes() || size > memory->StageBytes() - a)) ||
@@ -219,16 +245,28 @@ namespace MobileGL::MG_Remote::Transport {
(kind == ProgressFrame && (!Client() || size != 64)) ||
(kind == ClientProgress && (Client() || size != 8)) || kind < Cmd || kind > Event)
Corrupt("frame kind or size outside negotiated window");
std::vector<std::uint8_t> p(size);
if (size && !Read(p.data(), size)) return;
if (((kind == Cmd || kind == Stage || kind == Event) && b != 0) ||
(kind == ProgressFrame && (a != 0 || b != 0)) || (kind == Reply && b > UINT32_MAX))
Corrupt("nonzero reserved data envelope field");
std::uint8_t chunk[64 * 1024];
std::uint64_t remaining = size;
while (remaining) {
const auto count = std::min<std::uint64_t>(remaining, sizeof chunk);
if (!Read(chunk, static_cast<std::size_t>(count))) return;
if (frames.Feed(chunk, count) != MOBILEGL_OK) Corrupt("invalid data frame payload");
remaining -= count;
}
std::vector<std::uint8_t> p;
if (frames.TakeMessage(p) != MOBILEGL_OK) Corrupt("incomplete data frame");
const auto* payload = p.data() + kDataEnvelopeBytes;
if (kind == Stage) {
std::memcpy(static_cast<std::uint8_t*>(memory->StageBase()) + a, p.data(), size);
std::memcpy(static_cast<std::uint8_t*>(memory->StageBase()) + a, payload, size);
} else if (kind == Cmd) {
ReceiveRing(memory->CmdRingBase(), memory->CmdRingCapacity(),
C().cmdRetiredTail.load(std::memory_order_acquire), a, p, receivedCmd);
C().cmdRetiredTail.load(std::memory_order_acquire), a, payload, size, receivedCmd);
} else if (kind == ClientProgress) {
if (a != receivedCmd) Corrupt("cmdHead does not match delivered bytes");
const auto tail = Get64(p.data());
const auto tail = Get64(payload);
if (tail > E().cmdHead.load(std::memory_order_acquire)) Corrupt("event tail ahead of head");
const auto oldTail = E().cmdRetiredTail.load(std::memory_order_acquire);
Advance(E().cmdAppliedTail, tail);
@@ -241,11 +279,11 @@ namespace MobileGL::MG_Remote::Transport {
consumer.Notify();
} else if (kind == Event) {
ReceiveRing(memory->EventRingBase(), memory->EventRingCapacity(),
E().cmdRetiredTail.load(std::memory_order_acquire), a, p, receivedEvent);
E().cmdRetiredTail.load(std::memory_order_acquire), a, payload, size, receivedEvent);
Advance(E().cmdHead, receivedEvent);
producer.Notify();
} else if (kind == ProgressFrame) {
const auto* v = p.data();
const auto* v = payload;
if (Get64(v + 8) > Get64(v) || Get64(v) > C().submittedSeq.load(std::memory_order_acquire) ||
Get64(v + 40) > Get64(v + 32) || Get64(v + 32) > C().cmdHead.load(std::memory_order_acquire) ||
Get64(v + 48) > 1)
@@ -254,10 +292,17 @@ namespace MobileGL::MG_Remote::Transport {
Advance(C().cmdRetiredTail, Get64(v + 40));
C().eventRingFull.store(static_cast<std::uint32_t>(Get64(v + 48)), std::memory_order_release);
C().eventDropped.store(static_cast<std::uint32_t>(Get64(v + 56)), std::memory_order_release);
Watermark::AdvanceRetired(C(), Get64(v + 8));
Watermark::AdvanceCompletedFrame(C(), Get64(v + 16));
Watermark::AdvancePresentAck(C(), Get64(v + 24));
Watermark::AdvanceApplied(C(), Get64(v));
{
std::lock_guard<std::mutex> lock(replyMutex);
Watermark::AdvanceApplied(C(), Get64(v));
// AdvanceRetired clamps to local appliedSeq. Apply this
// received snapshot in dependency order or the final
// retirement is silently clamped to the previous batch.
Watermark::AdvanceRetired(C(), Get64(v + 8));
}
replyReady.notify_all();
producer.Notify();
} else if (kind == Reply) {
{
@@ -265,7 +310,7 @@ namespace MobileGL::MG_Remote::Transport {
if (a <= replySeq) Corrupt("reply sequence moved backwards");
replySeq = a;
replyStatus = static_cast<std::int32_t>(static_cast<std::uint32_t>(b));
reply = std::move(p);
reply.assign(payload, payload + size);
}
replyReady.notify_all();
producer.Notify();
@@ -327,7 +372,7 @@ namespace MobileGL::MG_Remote::Transport {
m_impl->owned->InitializeEndpoints();
}
SessionSegments& StreamLink::Memory() {
return m_impl->owned->Memory();
return m_impl->memory ? *m_impl->memory : m_impl->owned->Memory();
}
RingProducer& StreamLink::CommandsOut() {
return m_impl->owned->CommandsOut();
@@ -416,7 +461,7 @@ namespace MobileGL::MG_Remote::Transport {
for (const auto span : x.stage) {
std::uint64_t sent = 0;
while (sent < span.Size) {
const auto count = std::min(span.Size - sent, kMaxFramePayloadSize);
const auto count = std::min(span.Size - sent, kMaxDataPayload);
if (!x.Send(Stage, span.Offset + sent, 0,
static_cast<std::uint8_t*>(x.memory->StageBase()) + span.Offset + sent, count))
return MOBILEGL_ERR_TRANSPORT_CLOSED;
@@ -459,11 +504,15 @@ namespace MobileGL::MG_Remote::Transport {
}
MobileGLResult StreamLink::ReadReply(std::uint64_t seq, std::int32_t* status, const void** p, std::uint64_t* size) {
auto& x = *m_impl;
if (!status || !p || !size || !x.Client()) return MOBILEGL_ERR_INVALID_ARGUMENT;
if (!status || !p || !size || !seq || !x.Client()) return MOBILEGL_ERR_INVALID_ARGUMENT;
if (!x.memory) return MOBILEGL_ERR_NOT_INITIALIZED;
std::unique_lock<std::mutex> lock(x.replyMutex);
// The applied watermark is sent after the reply, so normal callers never
// wait here. A standalone reply reader may wait without polling.
x.replyReady.wait(lock, [&] { return x.replySeq >= seq || x.closed.load(std::memory_order_acquire); });
x.replyReady.wait(lock, [&] {
return x.closed.load(std::memory_order_acquire) || x.replySeq >= seq ||
x.C().Progress.appliedSeq.load(std::memory_order_acquire) >= seq;
});
if (x.replySeq != seq) return x.closed.load() ? MOBILEGL_ERR_TRANSPORT_CLOSED : MOBILEGL_ERR_PROTOCOL_MISMATCH;
x.readReply = x.reply;
*status = x.replyStatus;
+22 -5
View File
@@ -858,7 +858,16 @@ namespace MobileGL::MG_Remote::Wire {
Bool PipeWireEncoder::Valid() const { return m_progress != nullptr && m_cmd != nullptr; }
Bool PipeWireEncoder::CheckCancellation() {
if (Cancelled() || (m_cancellationHook && m_cancellationHook(m_cancellationSelf)) ||
(m_stageRetirementBell && m_stageRetirementBell->Dead())) {
m_cancelled = true;
}
return m_cancelled;
}
Uint8* PipeWireEncoder::StageAllocate(Uint64 size) {
if (CheckCancellation()) return nullptr;
if (m_stageBase == nullptr) {
const SegmentView view = m_segments != nullptr ? m_segments->Get(kSegStage)
: SegmentView{};
@@ -912,6 +921,7 @@ namespace MobileGL::MG_Remote::Wire {
bool reclaimed = false;
bool waited = false;
for (;;) {
if (CheckCancellation()) return nullptr;
// THE WRAP SKIP MAY ONLY BE CHARGED AGAINST BYTES THAT ARE STILL IN FLIGHT. When
// there are none the allocator starts over at offset zero, so a blob the segment
// can hold whole is never refused (see RebaseEmptyStage). On retry this runs
@@ -952,9 +962,13 @@ namespace MobileGL::MG_Remote::Wire {
// therefore no reverse channel to drain.
if (m_stageWaitHook != nullptr) m_stageWaitHook(m_stageWaitSelf);
if (m_link) m_link->Flush();
if (!ready() && !m_stageRetirementBell->Wait(*m_signals.ProducerParked, ready, 0, 5000)) {
SessionFail(MGFatalFamily::RetirementWaitFailed, "MGPipe: Fatal{RetirementWaitFailed, \"SEG_STAGE\"} producer wait "
"ended before the pending allocation retired (shutdown or timeout)");
if (CheckCancellation()) return nullptr;
if (!ready() && !m_stageRetirementBell->Wait(*m_signals.ProducerParked, ready, 0,
m_stageWaitTimeoutMs)) {
if (CheckCancellation()) return nullptr;
SessionFail(MGFatalFamily::RetirementWaitFailed,
"MGPipe: Fatal{RetirementWaitFailed, \"SEG_STAGE\"} live peer did not retire "
"the pending allocation within %u ms", m_stageWaitTimeoutMs);
}
if (m_stageWaitHook != nullptr) m_stageWaitHook(m_stageWaitSelf);
}
@@ -972,6 +986,7 @@ namespace MobileGL::MG_Remote::Wire {
}
MGPBlobRef PipeWireEncoder::StageBytes(const void* bytes, Uint64 size) {
if (CheckCancellation()) return {};
if (!Valid() || m_segments == nullptr) {
WireProtocolFatal("PipeWireEncoder::StageBytes",
"no command producer or segment table installed");
@@ -988,6 +1003,7 @@ namespace MobileGL::MG_Remote::Wire {
}
Uint8* slot = StageAllocate(size);
if (slot == nullptr || CheckCancellation()) return {};
std::memcpy(slot, bytes, static_cast<SizeT>(size));
const Uint64 offset = static_cast<Uint64>(slot - m_stageBase);
if (m_link) m_link->NoteStage({offset, size});
@@ -1017,6 +1033,7 @@ namespace MobileGL::MG_Remote::Wire {
Uint64 PipeWireEncoder::EncodeRecord(MGPWireOp op, const void* payload, Uint64 payloadBytes,
const WireTail* tails, Uint32 tailCount) {
if (Cancelled()) return kInvalidSeq;
if (!Valid()) {
WireProtocolFatal("PipeWireEncoder::EncodeRecord", "no SEG_CMD producer installed");
}
@@ -1358,14 +1375,14 @@ namespace MobileGL::MG_Remote::Wire {
// ---------------------------------------------------------------------------------
PipeWireDecoder::PipeWireDecoder(Transport::ILink* link, SegmentTable* segments, ReplySink* replies)
: m_segments(segments), m_replies(replies), m_valid(link && link->Attached()) {
: m_valid(link && link->Attached()), m_segments(segments), m_replies(replies) {
m_auditPoison = MG_Config::Ipc.Audit;
InstallApplyHook();
}
PipeWireDecoder::PipeWireDecoder(Transport::RingControl* control, SegmentTable* segments,
ReplySink* replies)
: m_segments(segments), m_replies(replies), m_valid(control != nullptr) {
: m_valid(control != nullptr), m_segments(segments), m_replies(replies) {
m_auditPoison = MG_Config::Ipc.Audit;
// Once, at construction, beside the resolver it is modelled on - not per record from
// the apply thread. The thunk is inert without a decoder on the calling thread, so an
+18
View File
@@ -343,6 +343,18 @@ namespace MobileGL::MG_Remote::Wire {
// without a consumer cannot wait for retirement and retain the named refusal.
void SetLink(Transport::ILink* link) { m_link = link; }
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;
}
// The fast check is used on encode; the doorbell probe only runs while
// staging or at an unsuccessful wait boundary.
Bool Cancelled() const {
return m_cancelled || (m_sessionLost && m_sessionLost->load(std::memory_order_acquire));
}
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
@@ -403,6 +415,12 @@ namespace MobileGL::MG_Remote::Wire {
Uint64 m_cmdWrapPads = 0;
Uint64 m_stageReclaimWaits = 0;
Transport::Doorbell* m_stageRetirementBell = nullptr;
const std::atomic<bool>* m_sessionLost = nullptr;
CancellationHook m_cancellationHook = nullptr;
void* m_cancellationSelf = nullptr;
Bool m_cancelled = false;
Uint32 m_stageWaitTimeoutMs = 120000;
// P5e (ra): the session's event drain, called around the stage-bell park (§2.6).
StageWaitHook m_stageWaitHook = nullptr;
void* m_stageWaitSelf = nullptr;
@@ -413,9 +413,9 @@ namespace MobileGL::MG_State::GLState {
// {h,cpp}, beside this header rather than inside it so the check_include_closure.py
// "artifacts-header" probe stays untouched. It is two visitors over the tables below - a
// writer that appends to a Vector<Uint8> and a reader that consumes one - length-prefixed,
// little-endian, with a format-version word first and a MGL_LINKARTIFACTS_SIZE echo
// second, so a struct that gained a field and a codec that did not is a mismatch at READ
// time rather than a silent truncation. Adding a member to any struct above therefore
// little-endian, with a format-version word and (for disaggregated v2) a recursive
// wire-schema digest of these tables. Native container sizes are local maintenance
// assertions below, never cross-standard-library wire facts. Adding a member to any struct above therefore
// means: add its VisitFields row here, update the sizeof number below, and bump
// kProgramArtifactsCodecVersion. `LinkArtifacts::program` stays the one deliberate
// omission, and the codec has no arm for it.
@@ -13,6 +13,7 @@
#include <bit>
#include <cstring>
#include <set>
#include <limits>
#include <string>
#include <type_traits>
#include <vector>
@@ -70,6 +71,88 @@ namespace MobileGL::MG_State::GLState {
template <class T>
concept ArchiveUniformInitializer = std::same_as<T, UniformInitializer>;
// glslang supplies no VisitFields table for this aggregate. Keep its one explicit
// table shared by the payload writer, reader and wire-schema walk.
template <class Self, class Visitor>
void VisitUniformInitializer(Self& value, Visitor&& visit) {
visit("name", value.name);
visit("basicType", value.basicType);
visit("vectorSize", value.vectorSize);
visit("matrixCols", value.matrixCols);
visit("matrixRows", value.matrixRows);
visit("arraySize", value.arraySize);
visit("intValues", value.intValues);
visit("floatValues", value.floatValues);
}
#if MOBILEGL_BUILD_DISAGGREGATED
void SchemaWord(Uint64& hash, Uint64 word) {
for (unsigned i = 0; i < 8; ++i) {
hash ^= (word >> (i * 8)) & 255u;
hash *= 1099511628211ull;
}
}
void SchemaName(Uint64& hash, const char* text) {
do {
hash ^= static_cast<unsigned char>(*text);
hash *= 1099511628211ull;
} while (*text++ != '\0');
}
template <class Value>
void DescribeWireType(Uint64& hash) {
using T = std::remove_cv_t<Value>;
if constexpr (ArchiveScalar<T>) {
// These are the bytes PutRaw/TakeRaw actually carry, not the surrounding
// C++ object's layout. Equal-width signed/unsigned or float/int differ.
if constexpr (std::is_enum_v<T>) {
SchemaName(hash, "enum");
DescribeWireType<std::underlying_type_t<T>>(hash);
} else {
SchemaName(hash, std::is_same_v<T, bool> ? "bool" :
std::is_floating_point_v<T> ? "ieee-float" :
std::is_signed_v<T> ? "signed" : "unsigned");
SchemaWord(hash, sizeof(T));
if constexpr (std::is_floating_point_v<T>) {
static_assert(std::numeric_limits<T>::is_iec559);
SchemaWord(hash, std::numeric_limits<T>::digits);
SchemaWord(hash, std::numeric_limits<T>::max_exponent);
}
}
} else if constexpr (ArchiveString<T>) {
SchemaName(hash, "string-u64-count-byte-content");
} else if constexpr (ArchiveMap<T>) {
SchemaName(hash, "map-u64-count");
DescribeWireType<typename T::key_type>(hash);
DescribeWireType<typename T::mapped_type>(hash);
} else if constexpr (ArchiveSet<T>) {
SchemaName(hash, "set-u64-count");
DescribeWireType<typename T::value_type>(hash);
} else if constexpr (ArchiveFixedArray<T>) {
SchemaName(hash, "fixed-array");
SchemaWord(hash, std::tuple_size<T>::value);
DescribeWireType<typename T::value_type>(hash);
} else if constexpr (ArchiveVector<T>) {
SchemaName(hash, "vector-u64-count");
DescribeWireType<typename T::value_type>(hash);
} else {
SchemaName(hash, ArchiveUniformInitializer<T> ? "uniform-initializer" : "record");
// Visit only types/names from default records. Container contents are never
// traversed, and no sizeof(string/vector/map), offset or allocator enters it.
T value{};
Uint64 fields = 0;
const auto field = [&](const char* name, const auto& member) {
++fields;
SchemaName(hash, name);
DescribeWireType<std::remove_cvref_t<decltype(member)>>(hash);
};
if constexpr (ArchiveUniformInitializer<T>) VisitUniformInitializer(value, field);
else VisitFields(value, field);
SchemaWord(hash, fields);
SchemaName(hash, "end-record");
}
}
#endif
// ---- the writer ----
template <class T>
@@ -117,6 +200,11 @@ namespace MobileGL::MG_State::GLState {
} else if constexpr (ArchiveVector<T>) {
WriteSequence(out, value);
} else if constexpr (ArchiveUniformInitializer<T>) {
#if MOBILEGL_BUILD_DISAGGREGATED
VisitUniformInitializer(value, [&out](const char*, const auto& member) {
WriteValue(out, member);
});
#else
WriteValue(out, value.name);
WriteValue(out, value.basicType);
WriteValue(out, value.vectorSize);
@@ -125,6 +213,7 @@ namespace MobileGL::MG_State::GLState {
WriteValue(out, value.arraySize);
WriteValue(out, value.intValues);
WriteValue(out, value.floatValues);
#endif
} else {
// The archive's own structs: TypeFacts, ResourceReflection, XfbVarying. ONE
// table serves both directions, so a member added to any of them is carried by
@@ -223,6 +312,11 @@ namespace MobileGL::MG_State::GLState {
if (!in.Ok) return;
}
} else if constexpr (ArchiveUniformInitializer<T>) {
#if MOBILEGL_BUILD_DISAGGREGATED
VisitUniformInitializer(value, [&in](const char*, auto& member) {
if (in.Ok) ReadValue(in, member);
});
#else
ReadValue(in, value.name);
ReadValue(in, value.basicType);
ReadValue(in, value.vectorSize);
@@ -231,6 +325,7 @@ namespace MobileGL::MG_State::GLState {
ReadValue(in, value.arraySize);
ReadValue(in, value.intValues);
ReadValue(in, value.floatValues);
#endif
} else {
VisitFields(value, [&in](const char*, auto& field) {
if (in.Ok) ReadValue(in, field);
@@ -238,29 +333,41 @@ namespace MobileGL::MG_State::GLState {
}
}
// The struct-size echo. Under a toolchain whose sizes are not pinned yet
// (ProgramArtifacts.h's libc++ branch until the integrator fills it in) this is 0,
// which still round-trips within one build - the echo compares what THIS build wrote
// against what THIS build expects - and stops mattering the moment the pin lands.
//
// P5e (pg) READ THIS LINE AS THE CONTRACT ASKED (CONTRACT-P5E §5.5) and its verdict is
// in ProgramArtifactsCodec.h's header comment: the caveat is about the ECHO WORD, not
// about a member the codec skips - the one member VisitFields omits is
// `LinkArtifacts::program`, and no backend twin reads it. A 0 echo is exact for an
// in-process split, where the writer and the reader are one build, and becomes a real
// cross-toolchain check when P6 puts a client and a server in different processes
// built by different compilers.
// v1 is retained for local monolith verification. Its native size echo is not a
// wire compatibility fact: libstdc++ writes 1056 while unpinned libc++ writes zero.
#if !MOBILEGL_BUILD_DISAGGREGATED
#ifdef MGL_LINKARTIFACTS_SIZE
inline constexpr Uint64 kLinkArtifactsSizeEcho = MGL_LINKARTIFACTS_SIZE;
#else
inline constexpr Uint64 kLinkArtifactsSizeEcho = 0;
#endif
#endif
} // namespace
#if MOBILEGL_BUILD_DISAGGREGATED
Uint64 ProgramArtifactsSchemaFingerprint() {
static const Uint64 fingerprint = [] {
Uint64 hash = 1469598103934665603ull;
SchemaName(hash, "MobileGL.ProgramArchive.v2.little-endian");
// The outer stage framing is part of the same wire contract.
SchemaName(hash, "stage-list-u32-count-u32-elements");
SchemaWord(hash, kProgramArchiveMaxStages);
DescribeWireType<LinkArtifacts>(hash);
DescribeWireType<SpirvArtifacts>(hash);
return hash == 0 ? Uint64{1} : hash;
}();
return fingerprint;
}
#endif
void EncodeProgramArtifacts(const LinkArtifacts& link, const SpirvArtifacts& spirv,
Vector<Uint8>& out) {
PutRaw(out, kProgramArtifactsCodecVersion);
#if MOBILEGL_BUILD_DISAGGREGATED
PutRaw(out, ProgramArtifactsSchemaFingerprint());
#else
PutRaw(out, kLinkArtifactsSizeEcho);
#endif
// `link` is walked through its own VisitFields table, which omits the live
// SharedPtr<glslang::TProgram>: 57 of the 58 members. There is no arm here for it and
// there must not be one - it points into a glslang arena that no archived instance
@@ -280,14 +387,16 @@ namespace MobileGL::MG_State::GLState {
ReadCursor in{bytes, size, 0, true};
Uint32 version = 0;
Uint64 sizeEcho = 0;
if (!TakeRaw(in, version) || !TakeRaw(in, sizeEcho)) return false;
// REFUSED, NOT GUESSED. A different version word or a struct that changed width means
// the bytes describe a layout this build does not have; deserialising them anyway
// writes garbage into the tail of a reflection table, which is exactly the failure the
// two words exist to turn into a clean false.
Uint64 schema = 0;
if (!TakeRaw(in, version) || !TakeRaw(in, schema)) return false;
// Refuse the declared wire shape before reading any field. In v2 the second word
// follows serialization types/order; C++ container object sizes are irrelevant.
if (version != kProgramArtifactsCodecVersion) return false;
if (sizeEcho != kLinkArtifactsSizeEcho) return false;
#if MOBILEGL_BUILD_DISAGGREGATED
if (schema != ProgramArtifactsSchemaFingerprint()) return false;
#else
if (schema != kLinkArtifactsSizeEcho) return false;
#endif
ReadValue(in, link);
ReadValue(in, spirv);
@@ -32,9 +32,9 @@
// path.
//
// THE FORMAT, and every part of it is a refusal rather than a guess:
// * a VERSION word first, and a MGL_LINKARTIFACTS_SIZE echo second, so a struct that gained
// a field and a codec that did not is a MISMATCH AT READ TIME rather than a silent
// truncation that deserialises garbage into the tail of a reflection table;
// * a VERSION word first, and a wire-schema fingerprint second (v2), so member order,
// type or count-layout drift is a MISMATCH before decoding. The local-only v1 form
// retains its legacy native-size echo;
// * length-prefixed everything - strings, vectors, maps, sets - with the count checked
// against the bytes that remain before a single element is allocated, so a corrupt count
// cannot turn into a four-billion-element resize;
@@ -54,19 +54,20 @@
// member it does not carry is one no twin has ever asked for. It could not be carried in any
// case: it points into a glslang arena that no archived instance owns.
//
// AND THE OTHER HALF OF THAT CONTRACT LINE - `ProgramArtifactsCodec.cpp`'s libc++ caveat - is
// about the STRUCT-SIZE ECHO, not about a member: under a toolchain whose `MGL_LINKARTIFACTS_SIZE`
// is not pinned yet (the NDK's libc++ branch of `ProgramArtifacts.h`, inert until the integrator
// fills it in) the echo word is 0. That is SAFE FOR P5e AND NOT A GAP: the echo compares what
// this build wrote against what this build expects, so a round trip within one build is exact,
// and P5e is an in-process split where the writer and the reader ARE one build. It becomes a
// real check the day a client and a server built by different toolchains share a wire, which is
// P6's boundary and P6's problem - stated here so it cannot be discovered instead.
// P6.5: native STL object sizes never describe this field-wise byte stream. Disaggregated
// peers use v2's recursive wire-schema fingerprint; monolith verification retains v1.
namespace MobileGL::MG_State::GLState {
// Bumped whenever the byte format changes in a way a previous reader would misread. A
// reader that sees a different word REFUSES; it never tries to guess a layout.
#if MOBILEGL_BUILD_DISAGGREGATED
inline constexpr Uint32 kProgramArtifactsCodecVersion = 2;
// Derived from the actual VisitFields order/names, container element schemas and scalar
// representations. No native container size/offset is included. Also checked in Hello.
Uint64 ProgramArtifactsSchemaFingerprint();
#else
inline constexpr Uint32 kProgramArtifactsCodecVersion = 1;
#endif
// Appends the archive to `out` (which is not cleared, so a caller may frame it). Never
// fails: everything it walks is owned plain data.
@@ -75,7 +76,7 @@ namespace MobileGL::MG_State::GLState {
// Replaces `link` and `spirv` with what `bytes` describes. Returns false - with both
// outputs left in a defined, default state - for a truncated stream, a version mismatch, a
// struct-size mismatch, or trailing bytes the format does not account for. `link.program`
// wire-schema mismatch (native-size echo for local v1), or trailing bytes. `link.program`
// is always null on return.
Bool DecodeProgramArtifacts(const Uint8* bytes, SizeT size, LinkArtifacts& link,
SpirvArtifacts& spirv);
+11
View File
@@ -213,6 +213,17 @@ endif()
include(GoogleTest)
gtest_discover_tests(PipeCatalogueTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit)
# This source scanner must inspect the configured checkout, including in out-of-tree builds.
# Apply the override after GoogleTest's deferred discovery, and only to this one test.
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/PipeCatalogueSourceRoot.cmake" CONTENT
"if(\"PipeCatalogue.FrontendNeverTakesAnApplierAddress\" IN_LIST PipeCatalogueTest_TESTS)
set_tests_properties(PipeCatalogue.FrontendNeverTakesAnApplierAddress PROPERTIES
WORKING_DIRECTORY [==[${MGL_ROOT}]==])
endif()
")
set_property(DIRECTORY APPEND PROPERTY TEST_INCLUDE_FILES
"${CMAKE_CURRENT_BINARY_DIR}/PipeCatalogueSourceRoot.cmake")
gtest_discover_tests(FieldOwnershipTest DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit)
foreach(pipeTest ResourceEmitTest VertexInputEmitTest
FramebufferEmitTest TextureEmitTest SamplerEmitTest ImageEmitTest
@@ -18,8 +18,8 @@
// and every one of TypeFacts' twenty members - a codec that dropped one would be invisible
// until a backend read a reflection answer that had quietly become zero;
// * a TRUNCATED stream is refused rather than guessed at;
// * a VERSION or struct-size mismatch is refused rather than deserialised into a layout this
// build does not have.
// * a VERSION or wire-schema mismatch is refused before decoding; native STL object
// sizes do not describe a cross-platform serialized archive.
//
// AND ONE PROPERTY THAT IS NOT ABOUT BYTES AT ALL: LinkArtifacts has 58 members and its
// VisitFields table visits 57. The 58th is the live SharedPtr<glslang::TProgram>, which is
@@ -32,6 +32,7 @@
// stays name-for-name identical between the pull and the push trees.
#include <gtest/gtest.h>
#include <cstring>
#include "Includes.h"
#if MOBILEGL_PIPE_PUSH
@@ -325,9 +326,8 @@ TEST(ProgramArtifactsCodec, ATruncatedStreamIsRefusedNotGuessed) {
#endif
}
// Negative control 2. The version word and the struct-size echo are the two things a compiler
// cannot check: a struct that gained a field and a codec that did not would otherwise
// deserialise garbage into the tail of a reflection table. Both have to REFUSE.
// Negative control 2: both the version and schema word must refuse mismatches.
// Monolith v1 retains the old native-size echo in that second word.
TEST(ProgramArtifactsCodec, AVersionMismatchIsRefused) {
#if MOBILEGL_PIPE_PUSH
Vector<Uint8> bytes;
@@ -344,9 +344,7 @@ TEST(ProgramArtifactsCodec, AVersionMismatchIsRefused) {
++wrongVersion[0];
EXPECT_FALSE(DecodeProgramArtifacts(wrongVersion.data(), wrongVersion.size(), link, spirv));
// Then the MGL_LINKARTIFACTS_SIZE echo, which is the half that catches a struct that grew
// under a codec that did not - the failure the four sizeof trip wires in
// ProgramArtifacts.h send an author here to fix.
// Then the wire schema (v2) or local native-size echo (v1). Neither may be ignored.
Vector<Uint8> wrongSize = bytes;
++wrongSize[4];
EXPECT_FALSE(DecodeProgramArtifacts(wrongSize.data(), wrongSize.size(), link, spirv));
@@ -373,3 +371,39 @@ TEST(ProgramArtifactsCodec, TheTablesVisitEveryMemberExceptTheLiveProgram) {
GTEST_SKIP() << "MOBILEGL_PIPE_PUSH is off: the archive codec is push-only";
#endif
}
TEST(ProgramArtifactsCodec, PortableHeaderUsesWireSchemaRatherThanNativeContainerSize) {
#if MOBILEGL_PIPE_PUSH && MOBILEGL_BUILD_DISAGGREGATED
Vector<Uint8> bytes;
EncodeProgramArtifacts(MakeLinkArtifacts(), MakeSpirvArtifacts(), bytes);
ASSERT_GT(bytes.size(), 12u);
Uint32 version = 0;
Uint64 schema = 0;
std::memcpy(&version, bytes.data(), sizeof(version));
std::memcpy(&schema, bytes.data() + sizeof(version), sizeof(schema));
EXPECT_EQ(version, 2u);
EXPECT_EQ(schema, ProgramArtifactsSchemaFingerprint());
EXPECT_NE(schema, 0u);
EXPECT_NE(schema, sizeof(LinkArtifacts));
RecordProperty("program_archive_wire_schema", std::to_string(schema));
// The old Linux/native echo (1056) and unpinned libc++ echo (0) both fail.
// This control distinguishes a portable schema from simply deleting the check.
for (const Uint64 legacyEcho : {Uint64{1056}, Uint64{0}}) {
auto legacy = bytes;
std::memcpy(legacy.data() + sizeof(version), &legacyEcho, sizeof(legacyEcho));
LinkArtifacts link;
SpirvArtifacts spirv;
EXPECT_FALSE(DecodeProgramArtifacts(legacy.data(), legacy.size(), link, spirv));
EXPECT_TRUE(link.uniformReflection.empty());
EXPECT_TRUE(spirv.generatedSpirv.empty());
}
auto v1 = bytes;
v1[0] = 1;
LinkArtifacts link;
SpirvArtifacts spirv;
EXPECT_FALSE(DecodeProgramArtifacts(v1.data(), v1.size(), link, spirv));
#else
GTEST_SKIP() << "portable archive headers apply to the disaggregated wire";
#endif
}
+13 -1
View File
@@ -22,7 +22,7 @@ if (NOT WIN32)
# P6 so: the SAME nine ITransport-contract cases InProcessTransportTest runs, over a real
# socketpair with real threads, plus the three a stream has and a message queue does not.
# POSIX for the same reason as FdPassingTest - and because CONTRACT-P6 §2.6 lands POSIX only.
list(APPEND MOBILEGL_WIRE_TESTS SocketTransportTest StreamLinkTest)
list(APPEND MOBILEGL_WIRE_TESTS SocketTransportTest StreamLinkTest StreamClientPublicationTest InitialCapsStartupTest)
# P6 sm: two MobileGL processes. Needs the real server image beside it, so
# the dependency is declared after the loop rather than assumed.
list(APPEND MOBILEGL_WIRE_TESTS ServerSpawnTest)
@@ -141,3 +141,15 @@ foreach (wiretest IN ITEMS RemoteClientTest ServerLoopTest SessionHandshakeTest
gtest_discover_tests(${wiretest} DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit)
endforeach ()
# P6.5 manual fault-injection probe: requires an explicit live peer and is never
# registered with ctest, so a device-less unit run cannot silently skip this gate.
if (NOT WIN32)
add_executable(MobileGLPeerLossProbe PeerLossProbe.cpp)
target_include_directories(MobileGLPeerLossProbe PRIVATE
${MGL_ROOT}/include
${MGL_ROOT}/MobileGL
${MGL_ROOT}/3rdparty/flatbuffers/include
)
target_link_libraries(MobileGLPeerLossProbe PRIVATE ${LINK_LIBRARIES})
endif ()
+18
View File
@@ -195,3 +195,21 @@ TEST(FramingTest, ManyMessagesCompactTheBufferInsteadOfGrowing) {
}
EXPECT_EQ(reader.BufferedBytes(), 0u);
}
TEST(Framing, ADataMagicReaderKeepsTheSameLengthAndReassemblyRules) {
using namespace MobileGL::MG_Remote::Transport;
constexpr std::uint32_t dataMagic = 0x444c474d;
std::vector<std::uint8_t> payload(20 + 9, 0x5a), framed;
ASSERT_EQ(AppendFrame(framed, payload.data(), payload.size()), MOBILEGL_OK);
std::memcpy(framed.data(), &dataMagic, sizeof dataMagic);
FrameReader reader(dataMagic);
ASSERT_EQ(reader.Feed(framed.data(), 7), MOBILEGL_OK);
EXPECT_FALSE(reader.HasMessage());
ASSERT_EQ(reader.Feed(framed.data() + 7, framed.size() - 7), MOBILEGL_OK);
EXPECT_EQ(reader.PendingMessageSize(), payload.size());
std::vector<std::uint8_t> received;
ASSERT_EQ(reader.TakeMessage(received), MOBILEGL_OK);
EXPECT_EQ(received, payload);
FrameReader controlReader;
EXPECT_EQ(controlReader.Feed(framed.data(), framed.size()), MOBILEGL_ERR_PROTOCOL_MISMATCH);
}
@@ -0,0 +1,183 @@
// SPDX-License-Identifier: LGPL-3.0-only
// Starts at the post-Welcome boundary with real TCP endpoints and ControlInbox.
// The peer publishes real CapsSnapshot bytes; no private run-ahead flag is set.
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/CapsCodec.h>
#include <MG_Remote/Transport/ControlInbox.h>
#include <MG_Remote/Transport/SocketTransport.h>
#include <MG_Remote/Protocol/generated/protocol_generated.h>
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <thread>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
using namespace MobileGL;
using namespace MobileGL::MG_Remote;
namespace {
template <class Tag, typename Tag::Type member>
struct StartupPeerAccess {
friend typename Tag::Type StartupPeerMember(Tag) { return member; }
};
struct StartupControlTag {
using Type = Transport::ITransport* Client::ClientSession::*;
friend Type StartupPeerMember(StartupControlTag);
};
template struct StartupPeerAccess<StartupControlTag, &Client::ClientSession::m_transport>;
struct StartupInboxTag {
using Type = std::unique_ptr<Transport::ControlInbox> Client::ClientSession::*;
friend Type StartupPeerMember(StartupInboxTag);
};
template struct StartupPeerAccess<StartupInboxTag, &Client::ClientSession::m_controlInbox>;
class InitialCapsStartup : public testing::Test {
protected:
Client::ClientSession client;
std::unique_ptr<Transport::SocketTransport> controlClient, controlPeer;
std::unique_ptr<Transport::ILink> dataPeer;
const decltype(MG_Config::Ipc) savedIpc = MG_Config::Ipc;
const MG_Config::TransportMode savedTransport = MG_Config::Transport;
void SetUp() override {
::alarm(10);
MG_Config::Transport = MG_Config::TransportMode::Spawn;
MG_Config::Ipc.RunAhead = 1;
MG_Config::Ipc.VerbBarrier = 1;
int reserve = ::socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(reserve, 0);
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ASSERT_EQ(::bind(reserve, reinterpret_cast<sockaddr*>(&address), sizeof address), 0);
socklen_t length = sizeof address;
ASSERT_EQ(::getsockname(reserve, reinterpret_cast<sockaddr*>(&address), &length), 0);
const std::string endpoint = "tcp://127.0.0.1:" + std::to_string(ntohs(address.sin_port));
::close(reserve);
int listener = -1;
ASSERT_EQ(Transport::SocketTransport::Listen(endpoint, &listener), MOBILEGL_OK);
MobileGLResult accepted = MOBILEGL_ERR_TIMEOUT;
std::thread accepting(
[&] { accepted = Transport::SocketTransport::AcceptPair(listener, 2000, controlPeer); });
const auto connected = Transport::SocketTransport::ConnectTo(endpoint, 2000, controlClient);
accepting.join();
::close(listener);
ASSERT_EQ(connected, MOBILEGL_OK);
ASSERT_EQ(accepted, MOBILEGL_OK);
ASSERT_TRUE(controlClient->IsTcp());
ASSERT_TRUE(controlPeer->IsTcp());
Transport::SessionSegmentSizes sizes;
sizes.CmdRingBytes = 4096;
sizes.StageBytes = 4096;
sizes.EventRingBytes = 4096;
sizes.ReplyBytes = 4096;
ASSERT_EQ(client.AttachStreamLink(controlClient->TakeDataFd(), sizes), MOBILEGL_OK);
ASSERT_EQ(Transport::CreateStreamLink(controlPeer->TakeDataFd(), sizes,
Transport::TransportRoleTag::ServerConsumer, dataPeer),
MOBILEGL_OK);
dataPeer->InitializeEndpoints();
client.*StartupPeerMember(StartupControlTag{}) = controlClient.get();
client.*StartupPeerMember(StartupInboxTag{}) = std::make_unique<Transport::ControlInbox>(*controlClient);
}
void TearDown() override {
if (client.DataLink()) client.DataLink()->Detach();
if (dataPeer) dataPeer->Detach();
client.Stop();
MG_Config::Ipc = savedIpc;
MG_Config::Transport = savedTransport;
::alarm(0);
}
MobileGLResult Start() {
return client.FinishStartup(&client.DataLink()->ConsumerBell(), &client.DataLink()->ProducerBell(), false);
}
MobileGLResult SendCaps(Uint64 bits) {
MG_Backend::DynamicBackendParameters dynamic{};
MG_Backend::FormatCapabilityCache formats{};
RendererInfo renderer{};
Vector<Uint8> encodedFormats, encodedRenderer;
if (!EncodeFormatCapabilities(formats, encodedFormats) || !EncodeRendererInfo(renderer, encodedRenderer))
return MOBILEGL_ERR_PROTOCOL_MISMATCH;
flatbuffers::FlatBufferBuilder builder;
const auto parameters = builder.CreateVector(reinterpret_cast<const Uint8*>(&dynamic), sizeof dynamic);
const auto rendererBytes = builder.CreateVector(encodedRenderer.data(), encodedRenderer.size());
const auto formatBytes = builder.CreateVector(encodedFormats.data(), encodedFormats.size());
const auto version = builder.CreateString("4.6");
const auto caps =
::MobileGL::Wire::CreateCapsSnapshot(builder, parameters, rendererBytes, formatBytes, 0, version, bits,
static_cast<Uint32>(BackendType::DirectGLES));
const auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::CapsSnapshot, caps.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
return controlPeer->SendFrame({builder.GetBufferPointer(), builder.GetSize()});
}
MobileGLResult SendLog() {
flatbuffers::FlatBufferBuilder builder;
const auto line = ::MobileGL::Wire::CreateLogLine(
builder, ::MobileGL::Wire::LogLevel::Info,
builder.CreateString("initial caps intentionally delayed by test peer"));
const auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::LogLine, line.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
return controlPeer->SendFrame({builder.GetBufferPointer(), builder.GetSize()});
}
bool PumpSnapshot() {
const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(1);
do {
if (client.PumpControlPlane()) return true;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (std::chrono::steady_clock::now() < end);
return false;
}
};
TEST_F(InitialCapsStartup, DelayedTcpSnapshotArmsRunAheadAndLaterSnapshotsCanOnlyDemote) {
std::atomic<bool> startupReturned{false};
bool returnedBeforeCaps = false;
MobileGLResult logResult = MOBILEGL_ERR_TIMEOUT, capsResult = MOBILEGL_ERR_TIMEOUT;
std::thread sender([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
returnedBeforeCaps = startupReturned.load(std::memory_order_acquire);
logResult = SendLog();
capsResult = SendCaps(MG_Pipe::kCapRunAheadApply);
});
const auto result = Start();
startupReturned.store(true, std::memory_order_release);
sender.join();
ASSERT_EQ(logResult, MOBILEGL_OK);
ASSERT_EQ(capsResult, MOBILEGL_OK);
EXPECT_FALSE(returnedBeforeCaps) << "startup returned without a real initial snapshot";
ASSERT_EQ(result, MOBILEGL_OK);
EXPECT_TRUE(client.Started());
EXPECT_TRUE(client.RunAheadArmed());
ASSERT_EQ(SendCaps(0), MOBILEGL_OK);
ASSERT_TRUE(PumpSnapshot());
EXPECT_FALSE(client.RunAheadArmed());
ASSERT_EQ(SendCaps(MG_Pipe::kCapRunAheadApply), MOBILEGL_OK);
ASSERT_TRUE(PumpSnapshot());
EXPECT_FALSE(client.RunAheadArmed()) << "a later cap must not re-promote this session";
}
TEST_F(InitialCapsStartup, AHealthyPeerWithoutInitialCapsFailsStartupWithoutDeviceLoss) {
const auto before = std::chrono::steady_clock::now();
EXPECT_EQ(Start(), MOBILEGL_ERR_TIMEOUT);
const auto elapsed = std::chrono::steady_clock::now() - before;
EXPECT_GE(elapsed, std::chrono::seconds(4));
EXPECT_LT(elapsed, std::chrono::seconds(8));
EXPECT_FALSE(client.Started());
EXPECT_FALSE(client.RunAheadArmed());
EXPECT_FALSE(Client::ClientSession::DeviceLost());
}
TEST_F(InitialCapsStartup, ANullInitialSnapshotFailsInsteadOfStartingWithAPlaceholder) {
flatbuffers::FlatBufferBuilder builder;
const auto envelope = ::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::CapsSnapshot,
flatbuffers::Offset<void>{});
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
ASSERT_EQ(controlPeer->SendFrame({builder.GetBufferPointer(), builder.GetSize()}), MOBILEGL_OK);
EXPECT_EQ(Start(), MOBILEGL_ERR_PROTOCOL_MISMATCH);
EXPECT_FALSE(client.Started());
EXPECT_FALSE(Client::ClientSession::DeviceLost());
}
} // namespace
+270
View File
@@ -0,0 +1,270 @@
// SPDX-License-Identifier: LGPL-3.0-only
// Manual P6.5 S2/half-open probe. Never registered as a device-less unit test.
#include <Init.h>
#include <MG_Impl/GLImpl/Getter/GL_Getter.h>
#include <MG_Impl/Pipe/SlotAllocator.h>
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/Transport/Doorbell.h>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <csignal>
#include <sys/wait.h>
#include <unistd.h>
using namespace MobileGL;
using namespace MobileGL::MG_Remote;
namespace {
struct Options {
bool kill = false;
bool external = false;
unsigned deadlineMs = 10000;
unsigned armTimeoutMs = 120000;
std::string readyFile;
std::string startFile;
};
void Usage() {
std::fprintf(stderr, "MobileGLPeerLossProbe (--kill | --external) [--deadline-ms 10000]\n"
" [--ready-file PATH] [--start-file PATH] [--arm-timeout-ms 120000]\n"
"MOBILEGL_IPC_CONTROL=tcp://host:port is required; token/build policy use normal env.\n"
"--kill executes MGITEST_PEER_KILL_CMD with {pid} replaced by Welcome.serverPid.\n"
"--external sends no fault command. After READY, create --start-file immediately\n"
"before the parent's Wi-Fi cut; without a start file, type GO on stdin to arm.\n"
"Only descriptor hangup passes. A deadline never sets the device-lost latch.\n");
}
bool Number(const char* text, unsigned& result) {
char* end = nullptr;
const auto value = std::strtoul(text, &end, 10);
if (!text[0] || !end || *end || !value || value > 600000) return false;
result = static_cast<unsigned>(value);
return true;
}
bool Parse(int argc, char** argv, Options& options) {
for (int i = 1; i < argc; ++i) {
const std::string argument = argv[i];
if (argument == "--kill")
options.kill = true;
else if (argument == "--external")
options.external = true;
else if (i + 1 < argc && argument == "--deadline-ms") {
if (!Number(argv[++i], options.deadlineMs)) return false;
} else if (i + 1 < argc && argument == "--arm-timeout-ms") {
if (!Number(argv[++i], options.armTimeoutMs)) return false;
} else if (i + 1 < argc && argument == "--ready-file")
options.readyFile = argv[++i];
else if (i + 1 < argc && argument == "--start-file")
options.startFile = argv[++i];
else
return false;
}
return options.kill != options.external && (!options.kill || options.startFile.empty());
}
bool Exists(const std::string& path) {
std::error_code error;
return std::filesystem::exists(path, error);
}
struct SessionCleanup {
Client::ClientSession& session;
~SessionCleanup() { session.Stop(); }
};
// The fault helper gets its own process group. It only execs the caller's
// explicit template; killing/reaping this helper never touches a supervisor.
class FaultCommand {
public:
bool Start(const std::string& command) {
m_pid = ::fork();
if (m_pid == 0) {
if (::setpgid(0, 0) != 0) ::_exit(126);
::execl("/bin/sh", "sh", "-c", command.c_str(), static_cast<char*>(nullptr));
::_exit(127);
}
if (m_pid < 0) return false;
(void)::setpgid(m_pid, m_pid);
return true;
}
bool Finished() {
if (m_pid <= 0) return true;
const auto result = ::waitpid(m_pid, &m_status, WNOHANG);
if (result == m_pid) {
m_pid = -1;
m_finished = true;
}
return m_finished;
}
bool Succeeded() const { return m_finished && WIFEXITED(m_status) && WEXITSTATUS(m_status) == 0; }
~FaultCommand() {
if (m_pid <= 0 || Finished()) return;
(void)::kill(-m_pid, SIGTERM);
for (int i = 0; i != 20 && !Finished(); ++i)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (m_pid > 0) {
(void)::kill(-m_pid, SIGKILL);
(void)::waitpid(m_pid, &m_status, 0);
}
}
private:
pid_t m_pid = -1;
int m_status = -1;
bool m_finished = false;
};
bool ArmExternal(const Options& options) {
if (options.startFile.empty()) {
std::string line;
return std::getline(std::cin, line) && line == "GO";
}
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(options.armTimeoutMs);
while (!Exists(options.startFile)) {
if (std::chrono::steady_clock::now() >= deadline) return false;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
return true;
}
} // namespace
int main(int argc, char** argv) {
if (argc == 2 && std::strcmp(argv[1], "--help") == 0) {
Usage();
return 0;
}
Options options;
if (!Parse(argc, argv, options)) {
Usage();
return 64;
}
const char* endpoint = std::getenv("MOBILEGL_IPC_CONTROL");
if (!endpoint || std::strncmp(endpoint, "tcp://", 6) != 0) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: MOBILEGL_IPC_CONTROL must name an existing TCP server\n");
return 64;
}
if ((!options.readyFile.empty() && Exists(options.readyFile)) ||
(!options.startFile.empty() && Exists(options.startFile))) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: readiness/start files must be fresh for this run\n");
return 64;
}
const char* killTemplate = std::getenv("MGITEST_PEER_KILL_CMD");
if (options.kill && (!killTemplate || !std::strstr(killTemplate, "{pid}"))) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: --kill requires MGITEST_PEER_KILL_CMD containing {pid}\n");
return 64;
}
// No server is forked by the client. StartSpawned resolves Connect from the
// ordinary configuration, exactly as the integration TCP lane does.
::setenv("MOBILEGL_TRANSPORT", "spawn", 1);
::setenv("MOBILEGL_IPC_DATA", "stream", 1);
::setenv("MOBILEGL_IPC_ROLE", "client", 1);
::setenv("MOBILEGL_BACKEND_TYPE", "DirectGLES", 0);
MG_ConfigLoader::Init();
auto& session = Client::ClientSessionInstance();
SessionCleanup cleanup{session};
const auto started = session.StartSpawned();
if (started != MOBILEGL_OK || !session.ConnectDial() || !session.DataLink() ||
session.DataLink()->Capabilities().SharedMapping) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: TCP+stream bringup rc=%d\n", started);
return 2;
}
const auto peerPid = session.PeerServerPid();
auto* bell = session.SelfDoorbellForTest();
if (peerPid <= 1 || !bell || bell->PeerHungUp() || Client::ClientSession::DeviceLost() ||
MG_Impl::GLImpl::GetGraphicsResetStatus() != GL_NO_ERROR) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: no healthy Welcome peer before arming\n");
return 2;
}
// A real control round-trip finishes before the idle measurement. No GL
// command or application heartbeat is sent during the detection interval.
session.SyncPeerLog();
if (bell->PeerHungUp() || Client::ClientSession::DeviceLost()) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: peer disappeared before readiness\n");
return 2;
}
if (!options.readyFile.empty()) {
std::ofstream ready(options.readyFile, std::ios::out | std::ios::trunc);
ready << peerPid << '\n';
ready.flush();
if (!ready) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: cannot publish ready file\n");
return 2;
}
}
std::printf("P65_PEER_LOSS_READY pid=%u mode=%s control=%s data=stream deadline_ms=%u\n", peerPid,
options.kill ? "kill" : "external", endpoint, options.deadlineMs);
std::fflush(stdout);
if (options.external && !ArmExternal(options)) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: external trigger was not armed\n");
return 3;
}
if (bell->PeerHungUp()) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: peer disappeared before timer arm; timing is invalid\n");
return 3;
}
std::string command;
if (options.kill) {
command = killTemplate;
const auto pidText = std::to_string(peerPid);
for (std::size_t at = 0; (at = command.find("{pid}", at)) != std::string::npos; at += pidText.size())
command.replace(at, 5, pidText);
}
const auto start = std::chrono::steady_clock::now();
const auto deadline = start + std::chrono::milliseconds(options.deadlineMs);
FaultCommand fault;
if (options.kill && !fault.Start(command)) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: could not start the caller's kill command\n");
return 4;
}
std::printf("P65_PEER_LOSS_ARMED pid=%u mode=%s\n", peerPid, options.kill ? "kill" : "external");
std::fflush(stdout);
while (!bell->PeerHungUp() && std::chrono::steady_clock::now() < deadline) {
if (options.kill && fault.Finished() && !fault.Succeeded()) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: caller's kill command failed\n");
return 4;
}
(void)bell->Park(10);
}
const auto hangupAt = std::chrono::steady_clock::now();
const auto hangupMs = std::chrono::duration_cast<std::chrono::milliseconds>(hangupAt - start).count();
if (!bell->PeerHungUp() || !bell->Dead() || hangupAt >= deadline) {
std::fprintf(stderr,
"P65_PEER_LOSS_FAIL: deadline_ms=%u elapsed_ms=%lld peer_hung_up=%d dead=%d device_lost=%d\n",
options.deadlineMs, static_cast<long long>(hangupMs), bell->PeerHungUp(), bell->Dead(),
Client::ClientSession::DeviceLost());
return 3;
}
if (options.kill) {
while (!fault.Finished() && std::chrono::steady_clock::now() < deadline)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (!fault.Succeeded()) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: kill command did not complete successfully within budget\n");
return 4;
}
}
// Exercise the production barrier's ShutDown branch, not a test-written
// latch. The peer is already proven gone, so this valid reply-owning record
// cannot reach a server or inspect an invented fence handle there.
const MG_Pipe::MGPHandleOnly fence{{1, 1}, static_cast<Uint32>(MG_Pipe::MGPipeKind::Fence), 0};
Uint32 reply = 0;
Int32 status = Wire::ReplySink::kStatusError;
session.EmitAndWait(MG_Pipe::MGPWireOp::FenceStatus, &fence, sizeof fence, nullptr, 0, &reply, sizeof reply,
&status);
const auto lostMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
const bool lost = Client::ClientSession::DeviceLost();
const auto reset = MG_Impl::GLImpl::GetGraphicsResetStatus();
if (!lost || reset != GL_UNKNOWN_CONTEXT_RESET || status != Wire::ReplySink::kStatusDeclined ||
lostMs >= options.deadlineMs) {
std::fprintf(stderr, "P65_PEER_LOSS_FAIL: production latch=%d gl_reset=0x%x reply_status=%d elapsed_ms=%lld\n",
lost, reset, status, static_cast<long long>(lostMs));
return 5;
}
std::printf("P65_PEER_LOSS_PASS pid=%u mode=%s peer_hung_up_ms=%lld device_lost_ms=%lld "
"deadline_ms=%u device_lost=1 gl_reset=0x%x source=production-barrier\n",
peerPid, options.kill ? "kill" : "external", static_cast<long long>(hangupMs),
static_cast<long long>(lostMs), options.deadlineMs, reset);
std::fflush(stdout);
return 0;
}
+14 -7
View File
@@ -74,9 +74,12 @@ struct BackendTag {
};
template struct PeerAccess<BackendTag, &Srv::ServerLoop::m_backend>;
void StartControlSession() {
MG_Backend::BackendObject* ControlCapsPeer();
void StartControlSession(MG_Backend::BackendObject* capsSource = nullptr) {
::alarm(15);
MG_Config::Transport = MG_Config::TransportMode::InProcess;
Srv::ServerSessionInstance().SetBackend(capsSource ? capsSource : ControlCapsPeer());
Srv::ServerSessionInstance().SetCapabilityBits(0);
Srv::ServerSessionInstance().SetConsumedSubsystems(kMGPipeSubsystemsMigratedAtP4a);
if (ClientSessionInstance().Start(MG_Config::TransportMode::InProcess, {}) != MOBILEGL_OK)
@@ -135,6 +138,13 @@ struct CapsPeer : MG_Backend::BackendObject {
BackendType GetBackendType() const override { return BackendType::DirectGLES; }
};
MG_Backend::BackendObject* ControlCapsPeer() {
// A real encoded initial snapshot is required even when the apply fixture
// substitutes every verb and has no GPU backend of its own.
static CapsPeer backend;
return &backend;
}
void ExpectNamedAbort(const ChildResult& child, const char* diagnostic) {
EXPECT_TRUE(DiedOfAbort(child)) << DescribeStatus(child) << '\n' << child.Log;
EXPECT_NE(child.Log.find(diagnostic), std::string::npos) << child.Log;
@@ -387,8 +397,7 @@ TEST(RemoteClientControls, BounceReadPixelsShortReplyRefusesBeforeScatter) {
TEST(RemoteClientControls, DeleteTexturesAdvancesTheWireOrdinal) {
const auto child = RunInChild([] {
CapsPeer backend;
Srv::ServerSessionInstance().SetBackend(&backend);
StartControlSession();
StartControlSession(&backend);
ErrorPeer replies;
replies.status = 0; // The peer accepts the resource; the client must publish and delete it.
replies.Install();
@@ -442,8 +451,7 @@ TEST(RemoteClientControls, ServerRoleRespecifyDoesNotEmitClientInitialBytes) {
// SEG_EVENT producers are what replaces the mechanism this case used to drive.
const auto child = RunInChild([] {
CapsPeer backend;
Srv::ServerSessionInstance().SetBackend(&backend);
StartControlSession();
StartControlSession(&backend);
MG_Config::Features.PipePush |= kMGPipeSubsystemResources;
Srv::ServerLoopInstance().RunProbeOnApplyThreadForTesting([](void*) {
MG_State::GLState::BufferObject buffer(701);
@@ -475,8 +483,7 @@ TEST(RemoteClientControls, ServerRoleFlushDoesNotRunTheClientSubDataFollowup) {
// (Fatal{RoleViolation, "MGPipeSlots"}; ev's events replace the mechanism).
const auto child = RunInChild([] {
CapsPeer backend;
Srv::ServerSessionInstance().SetBackend(&backend);
StartControlSession();
StartControlSession(&backend);
MG_Config::Features.PipePush |= kMGPipeSubsystemResources;
Srv::ServerLoopInstance().RunProbeOnApplyThreadForTesting([](void*) {
MGPipeResourceOps ops{};
@@ -40,7 +40,7 @@ void CheckE1Boundary(Bool serverCap, MGPWireOp op, Bool expectWait, const char*
const Uint64 caps = serverCap ? static_cast<Uint64>(kCapRunAheadApply) : 0;
Srv::ServerSessionInstance().SetCapabilityBits(caps);
Srv::ServerSessionInstance().SetConsumedSubsystems(kMGPipeSubsystemsMigratedAtP4a);
AdoptSnapshot(MakeSnapshot(kMGPipeSubsystemsMigratedAtP4a, caps));
Srv::ServerSessionInstance().SetBackend(ControlCapsPeer());
auto& session = ClientSessionInstance();
if (session.Start(MG_Config::TransportMode::InProcess, {}) != MOBILEGL_OK) ::_exit(81);
auto& producer = session.Producer();
+3 -18
View File
@@ -2338,19 +2338,8 @@ void StartRunAheadSession() {
Srv::ServerSessionInstance().SetCapabilityBits(
static_cast<Uint64>(MG_Pipe::kCapRunAheadApply));
Srv::ServerSessionInstance().SetConsumedSubsystems(kMGPipeSubsystemsMigratedAtP4a);
// THE MIRROR IS ADOPTED BEFORE Start, AND THAT IS THE LATCH'S RULE MADE VISIBLE. This
// process has no server backend, so PublishCapsSnapshot is deferred and the handshake
// carries no snapshot at all - and §1 says a later snapshot may only turn run-ahead OFF,
// never on. So the arm has to be true at the FIRST adoption, which for a fixture means
// before the session takes its latch. A test that adopted afterwards and found the client
// still lockstep would be reading the contract's own rule as a failure.
{
MG_Pipe::MGPCaps caps{};
caps.CallMask = static_cast<Uint64>(MG_Pipe::kCapRunAheadApply) |
MG_Remote::MGCapsConsumerBits(kMGPipeSubsystemsMigratedAtP4a);
CapsMirrorInstance().Adopt(caps, MG_Backend::FormatCapabilityCache{}, RendererInfo{},
String{"4.6"}, BackendType::DirectGLES);
}
// The test peer publishes the first snapshot through the real handshake.
Srv::ServerSessionInstance().SetBackend(ControlCapsPeer());
if (ClientSessionInstance().Start(MG_Config::TransportMode::InProcess, {}) != MOBILEGL_OK) {
::_exit(81);
}
@@ -2411,11 +2400,7 @@ TEST(RemoteRunAhead, ClearingTheVerbBarrierDisarmsRunAheadEvenWithTheCapBit) {
Srv::ServerSessionInstance().SetCapabilityBits(
static_cast<Uint64>(MG_Pipe::kCapRunAheadApply));
Srv::ServerSessionInstance().SetConsumedSubsystems(kMGPipeSubsystemsMigratedAtP4a);
MG_Pipe::MGPCaps caps{};
caps.CallMask = static_cast<Uint64>(MG_Pipe::kCapRunAheadApply) |
MG_Remote::MGCapsConsumerBits(kMGPipeSubsystemsMigratedAtP4a);
CapsMirrorInstance().Adopt(caps, MG_Backend::FormatCapabilityCache{}, RendererInfo{},
String{"4.6"}, BackendType::DirectGLES);
Srv::ServerSessionInstance().SetBackend(ControlCapsPeer());
if (ClientSessionInstance().Start(MG_Config::TransportMode::InProcess, {}) != MOBILEGL_OK) {
::_exit(81);
}
@@ -41,6 +41,7 @@
#include <MG_Remote/CapsCodec.h>
#include <MG_Remote/Handshake.h>
#include <MG_Pipe/PipeWireLayout.h>
#include <MG_State/GLState/ProgramState/ProgramArtifactsCodec.h>
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/Protocol/generated/protocol_generated.h>
#include <MG_Remote/Server/ServerSession.h>
@@ -126,6 +127,8 @@ TEST(SessionHandshakeTest, TheAbiFingerprintChangesWhenAnyOfItsInputsDoes) {
EXPECT_EQ(inputs.MemberLayout, MG_Pipe::kMGPipeWireMemberLayoutDigest);
EXPECT_EQ(inputs.CatalogueLayout, MG_Pipe::kMGPipeWireCatalogueDigest);
EXPECT_EQ(inputs.RenderStateLayout, MG_Pipe::WireRenderStateDigest());
EXPECT_EQ(inputs.ProgramArtifactsCodecVersion, MG_State::GLState::kProgramArtifactsCodecVersion);
EXPECT_EQ(inputs.ProgramArtifactsSchema, MG_State::GLState::ProgramArtifactsSchemaFingerprint());
EXPECT_EQ(inputs.OpCount, static_cast<Uint64>(MG_Pipe::MGPWireOp::kOpCount));
EXPECT_EQ(inputs.PointerBits, sizeof(void*) * 8);
const auto perturbed = [&](auto mutate) {
@@ -142,6 +145,8 @@ TEST(SessionHandshakeTest, TheAbiFingerprintChangesWhenAnyOfItsInputsDoes) {
EXPECT_NE(production, perturbed([](auto& i) { ++i.FormatCapabilityFormats; })) << "FormatCapabilityFormats";
EXPECT_NE(production, perturbed([](auto& i) { ++i.FormatCapabilitiesCodecVersion; })) << "FormatCapabilitiesCodecVersion";
EXPECT_NE(production, perturbed([](auto& i) { ++i.RendererInfoCodecVersion; })) << "RendererInfoCodecVersion";
EXPECT_NE(production, perturbed([](auto& i) { ++i.ProgramArtifactsCodecVersion; })) << "ProgramArtifactsCodecVersion";
EXPECT_NE(production, perturbed([](auto& i) { ++i.ProgramArtifactsSchema; })) << "ProgramArtifactsSchema";
EXPECT_NE(production, perturbed([](auto& i) { ++i.OpCount; })) << "OpCount";
EXPECT_NE(production, perturbed([](auto& i) { ++i.AbiVersion; })) << "AbiVersion";
EXPECT_NE(production, perturbed([](auto& i) { ++i.PointerBits; })) << "PointerBits";
@@ -174,6 +174,21 @@ TEST(SocketTransportTest, CarriesFramesInBothDirections) {
EXPECT_EQ(Receive(*pair.client), welcome);
}
TEST(SocketTransportTest, RequestHalfCloseStillDeliversTheFinalReply) {
std::unique_ptr<SocketTransport> client, server;
ASSERT_EQ(SocketTransport::CreatePair(client, server), MOBILEGL_OK);
ASSERT_EQ(client->SendFrame(Span("last request")), MOBILEGL_OK);
EXPECT_EQ(Receive(*server), "last request");
ASSERT_EQ(client->ShutdownSend(), MOBILEGL_OK);
std::uint64_t bytes = 0;
char buffer[32]{};
EXPECT_EQ(server->ReceiveFrame({buffer, sizeof buffer}, &bytes, 1000), MOBILEGL_ERR_TRANSPORT_CLOSED);
ASSERT_EQ(server->SendFrame(Span("cleanup complete")), MOBILEGL_OK);
EXPECT_EQ(Receive(*client), "cleanup complete");
server->Shutdown();
EXPECT_EQ(client->ReceiveFrame({buffer, sizeof buffer}, &bytes, 1000), MOBILEGL_ERR_TRANSPORT_CLOSED);
}
TEST(SocketTransportTest, PreservesOrder) {
Pair pair = MakePair();
@@ -0,0 +1,297 @@
// SPDX-License-Identifier: LGPL-3.0-only
// Production session submission with a real stream reader and a deliberately
// non-applying peer. A passing test cannot be supplied by a later client wait.
#include <MG_Remote/Client/ClientSession.h>
#include <MG_Remote/CapsCodec.h>
#include <MG_Remote/Transport/InProcessTransport.h>
#include <MG_Remote/Transport/StreamLink.h>
#include <MG_Remote/Protocol/generated/protocol_generated.h>
#include <gtest/gtest.h>
#include <sys/socket.h>
#include <unistd.h>
#include <atomic>
#include <MG_Pipe/PipeRoute.h>
#include <MG_Impl/GLImpl/Getter/GL_Getter.h>
#include <chrono>
#include <thread>
#include <csignal>
#include <sys/wait.h>
#include <vector>
#include <cstring>
using namespace MobileGL;
using namespace MobileGL::MG_Remote;
namespace {
// Only the test's control endpoint is injected. A queued real CapsSnapshot
// arms the production latch without a backend, EGL context, or private toggle.
template <class Tag, typename Tag::Type member>
struct PublicationPeerAccess {
friend typename Tag::Type PublicationPeerMember(Tag) { return member; }
};
struct ControlEndpointTag {
using Type = Transport::ITransport* Client::ClientSession::*;
friend Type PublicationPeerMember(ControlEndpointTag);
};
template struct PublicationPeerAccess<ControlEndpointTag, &Client::ClientSession::m_transport>;
class StreamClientPublication : public testing::Test {
protected:
Client::ClientSession client;
std::unique_ptr<Transport::ILink> peer;
std::unique_ptr<Transport::InProcessTransport> controlClient, controlPeer;
const decltype(MG_Config::Ipc) savedIpc = MG_Config::Ipc;
const MG_Config::TransportMode savedTransport = MG_Config::Transport;
void SetUp() override {
// A regression that turns submission into an applied wait fails
// promptly instead of consuming the production 120-second budget.
::alarm(5);
MG_Config::Transport = MG_Config::TransportMode::Spawn;
MG_Config::Ipc.RunAhead = 1;
MG_Config::Ipc.VerbBarrier = 1;
MG_Config::Ipc.BatchWaits = 1;
MG_Config::Ipc.PresentCredit = 3;
Transport::SessionSegmentSizes sizes;
sizes.CmdRingBytes = 4096;
sizes.StageBytes = 4096;
sizes.EventRingBytes = 4096;
sizes.ReplyBytes = 4096;
int sockets[2];
ASSERT_EQ(::socketpair(AF_UNIX, SOCK_STREAM, 0, sockets), 0);
ASSERT_EQ(AttachData(sockets, sizes), MOBILEGL_OK);
Transport::InProcessTransport::CreatePair(controlClient, controlPeer);
client.*PublicationPeerMember(ControlEndpointTag{}) = controlClient.get();
MG_Backend::DynamicBackendParameters dynamic{};
MG_Backend::FormatCapabilityCache formats{};
RendererInfo renderer{};
Vector<Uint8> encodedFormats, encodedRenderer;
ASSERT_TRUE(EncodeFormatCapabilities(formats, encodedFormats));
ASSERT_TRUE(EncodeRendererInfo(renderer, encodedRenderer));
flatbuffers::FlatBufferBuilder builder;
const auto dynamicBytes = builder.CreateVector(reinterpret_cast<const Uint8*>(&dynamic), sizeof dynamic);
const auto rendererBytes = builder.CreateVector(encodedRenderer.data(), encodedRenderer.size());
const auto formatBytes = builder.CreateVector(encodedFormats.data(), encodedFormats.size());
const auto version = builder.CreateString("4.6");
const auto caps = ::MobileGL::Wire::CreateCapsSnapshot(builder, dynamicBytes, rendererBytes, formatBytes, 0,
version, MG_Pipe::kCapRunAheadApply,
static_cast<Uint32>(BackendType::DirectGLES));
const auto envelope =
::MobileGL::Wire::CreateCtrlEnvelope(builder, ::MobileGL::Wire::CtrlMsg::CapsSnapshot, caps.Union());
::MobileGL::Wire::FinishCtrlEnvelopeBuffer(builder, envelope);
ASSERT_EQ(controlPeer->SendFrame({builder.GetBufferPointer(), builder.GetSize()}), MOBILEGL_OK);
ASSERT_EQ(
client.FinishStartup(&client.DataLink()->ConsumerBell(), &client.DataLink()->ProducerBell(), false),
MOBILEGL_OK);
ASSERT_TRUE(client.RunAheadArmed());
}
void TearDown() override {
// The peer intentionally never applies. Closing delivery before
// teardown prevents its drain from supplying the assertion.
if (client.DataLink()) client.DataLink()->Detach();
if (peer) peer->Detach();
client.Stop();
MG_Config::Ipc = savedIpc;
MG_Config::Transport = savedTransport;
::alarm(0);
}
virtual MobileGLResult AttachData(int sockets[2], const Transport::SessionSegmentSizes& sizes) {
const auto result = client.AttachStreamLink(sockets[0], sizes);
if (result != MOBILEGL_OK) {
::close(sockets[1]);
return result;
}
const auto attached =
Transport::CreateStreamLink(sockets[1], sizes, Transport::TransportRoleTag::ServerConsumer, peer);
if (attached == MOBILEGL_OK) peer->InitializeEndpoints();
return attached;
}
bool Upload(std::size_t count, unsigned char fill) {
std::vector<Uint8> bytes(count, fill);
MG_Pipe::MGPSubData record{};
record.Res = {1, 1};
record.Target = MG_Pipe::kMGPipeResourceTargetBuffer;
record.UnionBox.W = static_cast<Uint32>(count);
record.UnionBox.H = record.UnionBox.D = 1;
return MG_Pipe::MGPipeRouteResourceSubData(record, bytes.data(), bytes.size());
}
bool PeerGone() {
auto& bell = client.DataLink()->ProducerBell();
const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(1);
while (!bell.PeerHungUp() && std::chrono::steady_clock::now() < end)
(void)bell.Park(1);
return bell.PeerHungUp();
}
bool StageIs(unsigned char fill) {
const auto& memory = client.DataLink()->Memory();
const auto* bytes = static_cast<const Uint8*>(memory.StageBase());
for (Uint64 i = 0; i < memory.StageBytes(); ++i)
if (bytes[i] != fill) return false;
return true;
}
bool ReceivedPublishedPrefix() {
const auto head = client.DataLink()->Signals().CmdHead->load(std::memory_order_acquire);
const auto signals = peer->Signals();
return peer->ConsumerBell().Wait(
*signals.ConsumerParked, [&] { return signals.CmdHead->load(std::memory_order_acquire) >= head; }, 0,
1000);
}
};
TEST_F(StreamClientPublication, AllThreePresentCreditsReachAnIdlePeerWithoutAnyClientWait) {
for (Uint64 serial = 1; serial <= 3; ++serial) {
MG_Pipe::MGPPresent present{};
present.FrameSerial = client.AcquirePresentCredit();
ASSERT_EQ(present.FrameSerial, serial);
client.EmitAndWait(MG_Pipe::MGPWireOp::Present, &present, sizeof present, nullptr, 0, nullptr, 0, nullptr);
ASSERT_TRUE(ReceivedPublishedPrefix()) << "present " << serial << " remained in the local batch";
}
EXPECT_EQ(client.Producer().Waits(), 0u);
EXPECT_EQ(client.PresentCreditWaits(), 0u);
EXPECT_EQ(client.DataLink()->Progress()->appliedSeq.load(), 0u);
for (int record = 0; record != 3; ++record) {
Transport::RingRecordView view{};
ASSERT_TRUE(peer->CommandsIn().Pop(view));
EXPECT_EQ(view.kind, static_cast<std::uint16_t>(MG_Pipe::MGPWireOp::Present));
}
}
TEST_F(StreamClientPublication, ExportedGlFlushDeliversPublishedCommandsWithoutWaitingForApplied) {
MG_Pipe::MGPMemoryBarrier barrier{};
client.EmitAndWait(MG_Pipe::MGPWireOp::MemoryBarrier, &barrier, sizeof barrier, nullptr, 0, nullptr, 0,
nullptr);
ASSERT_EQ(client.LastPublishedSeq(), 1u);
EXPECT_EQ(peer->Signals().CmdHead->load(), 0u);
::glFlush();
ASSERT_TRUE(ReceivedPublishedPrefix());
EXPECT_EQ(client.Producer().Waits(), 0u);
EXPECT_EQ(client.DataLink()->Progress()->appliedSeq.load(), 0u);
Transport::RingRecordView view{};
ASSERT_TRUE(peer->CommandsIn().Pop(view));
EXPECT_EQ(view.kind, static_cast<std::uint16_t>(MG_Pipe::MGPWireOp::MemoryBarrier));
}
TEST_F(StreamClientPublication, ADisconnectedPeerDeclinesALargeUploadWithoutCopyOrWireOrdinal) {
std::memset(client.DataLink()->Memory().StageBase(), 0x33, 4096);
peer->Detach();
ASSERT_TRUE(PeerGone());
// Larger than the whole window: cancellation must precede allocation,
// capacity checks and memcpy. The live path would refuse this request.
EXPECT_FALSE(Upload(32768, 0x99));
EXPECT_TRUE(Client::ClientSession::DeviceLost());
EXPECT_EQ(MG_Impl::GLImpl::GetGraphicsResetStatus(), GL_UNKNOWN_CONTEXT_RESET);
EXPECT_TRUE(client.Encoder().Cancelled());
EXPECT_EQ(client.Encoder().EmitSeq(), 0u);
EXPECT_EQ(client.LastPublishedSeq(), 0u);
EXPECT_TRUE(StageIs(0x33));
// A second local decline still consumes a nonzero MintReplySlot ticket.
EXPECT_FALSE(Upload(16, 0x88));
EXPECT_EQ(client.Encoder().EmitSeq(), 0u);
MG_Pipe::MGPMemoryBarrier record{};
EXPECT_EQ(client.Encoder().EncodeRecord(MG_Pipe::MGPWireOp::MemoryBarrier, &record, sizeof record,
static_cast<const void*>(nullptr), Uint64{0}),
MG_Remote::Wire::kInvalidSeq);
EXPECT_TRUE(StageIs(0x33));
}
class StreamClientStagingProcess : public StreamClientPublication {
protected:
pid_t peerPid = -1;
MobileGLResult AttachData(int sockets[2], const Transport::SessionSegmentSizes& sizes) override {
int ready[2];
if (::pipe(ready) != 0) {
::close(sockets[0]);
::close(sockets[1]);
return MOBILEGL_ERR_NOT_INITIALIZED;
}
// Fork before attaching either stream, so the child starts its own
// real reader and inherits no running thread or live link mutex.
peerPid = ::fork();
if (peerPid == 0) {
::close(ready[0]);
::close(sockets[0]);
std::unique_ptr<Transport::ILink> link;
const auto result =
Transport::CreateStreamLink(sockets[1], sizes, Transport::TransportRoleTag::ServerConsumer, link);
if (result == MOBILEGL_OK) link->InitializeEndpoints();
const unsigned char success = result == MOBILEGL_OK;
(void)::write(ready[1], &success, 1);
::close(ready[1]);
if (!success) ::_exit(81);
for (;;)
::pause(); // Reader receives; the peer deliberately never applies.
}
::close(ready[1]);
::close(sockets[1]);
unsigned char success = 0;
const auto received = peerPid > 0 ? ::read(ready[0], &success, 1) : -1;
::close(ready[0]);
if (received != 1 || !success) {
::close(sockets[0]);
return MOBILEGL_ERR_NOT_INITIALIZED;
}
return client.AttachStreamLink(sockets[0], sizes);
}
void TearDown() override {
if (peerPid > 0) {
(void)::kill(peerPid, SIGKILL);
(void)::waitpid(peerPid, nullptr, 0);
peerPid = -1;
}
StreamClientPublication::TearDown();
}
};
TEST_F(StreamClientStagingProcess, APeerKilledDuringAFullStageWaitDeclinesWithoutCopyingTheNextBlob) {
ASSERT_TRUE(Upload(4096, 0x5a));
ASSERT_EQ(client.Encoder().EmitSeq(), 1u);
client.Flush();
auto* parked = client.DataLink()->Signals().ProducerParked;
std::atomic<bool> killedWhileParked{false};
std::thread killer([&] {
const auto end = std::chrono::steady_clock::now() + std::chrono::seconds(2);
while (!parked->load(std::memory_order_acquire) && std::chrono::steady_clock::now() < end)
std::this_thread::yield();
killedWhileParked.store(parked->load(std::memory_order_acquire) != 0, std::memory_order_release);
(void)::kill(peerPid, SIGKILL);
});
const bool accepted = Upload(64, 0xa7);
killer.join();
EXPECT_TRUE(killedWhileParked.load());
EXPECT_FALSE(accepted);
EXPECT_TRUE(Client::ClientSession::DeviceLost());
EXPECT_EQ(MG_Impl::GLImpl::GetGraphicsResetStatus(), GL_UNKNOWN_CONTEXT_RESET);
EXPECT_GT(client.Encoder().StageReclaimWaits(), 0u);
EXPECT_EQ(client.Encoder().EmitSeq(), 1u);
EXPECT_EQ(client.LastPublishedSeq(), 1u);
EXPECT_TRUE(StageIs(0x5a));
}
class HealthyStageTimeoutHarness : public StreamClientPublication {
public:
void TestBody() override {}
void Run() {
SetUp();
client.Encoder().SetStageWaitTimeoutMs(5);
if (!Upload(4096, 0x5a)) ::_exit(82);
(void)Upload(64, 0xa7);
::_exit(83);
}
};
TEST(StreamClientCancellationDeath, AHealthyPeerStageTimeoutDoesNotInventDeviceLoss) {
EXPECT_EXIT(
{
::signal(
SIGABRT, +[](int) {
const bool lost = Client::ClientSession::DeviceLost();
const char* text = lost ? "stage-timeout-device-lost=1\n" : "stage-timeout-device-lost=0\n";
(void)::write(STDERR_FILENO, text, sizeof("stage-timeout-device-lost=0\n") - 1);
::_exit(lost ? 91 : 72);
});
HealthyStageTimeoutHarness harness;
harness.Run();
},
testing::ExitedWithCode(72), "RetirementWaitFailed(.|\n)*stage-timeout-device-lost=0");
}
} // namespace
+121 -5
View File
@@ -8,6 +8,9 @@
#include <thread>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <cstdio>
using namespace MobileGL::MG_Remote::Transport;
namespace {
@@ -64,18 +67,28 @@ namespace {
Queue(1, 24);
ASSERT_EQ(client.Flush(), MOBILEGL_OK);
ASSERT_EQ(apply.WaitForWork(2000), SessionWait::Reached);
ASSERT_TRUE(apply.ApplyOne([&](const RingRecordView& v) {
RingRecordView view;
ASSERT_TRUE(consumer.Pop(view));
[&](const RingRecordView& v) {
std::uint64_t offset;
std::memcpy(&offset, v.payload, sizeof offset);
const void* p = nullptr;
ASSERT_EQ(server.ResolveSpan(LinkSegment::Stage, {offset, bytes.size()}, &p), MOBILEGL_OK);
EXPECT_EQ(std::memcmp(p, bytes.data(), bytes.size()), 0);
}));
apply.RetireThrough(1);
}(view);
// Advance locally without a periodic-progress callback: this isolates
// flush-on-idle so removing it cannot be masked by the one-ms timer.
Watermark::AdvanceApplied(*serverMemory.CmdControl(), 1);
Watermark::AdvanceRetired(*serverMemory.CmdControl(), 1);
consumer.PublishRetired();
// This is the production idle discipline, not a polling sender thread.
EXPECT_EQ(apply.WaitForWork(1), SessionWait::TimedOut);
EXPECT_EQ(publish.WaitForApplied(1, 2000), SessionWait::Reached);
EXPECT_EQ(client.Progress()->retiredSeq.load(), 1u);
// applied and retired are independent watermarks. The reader publishes
// them in dependency order, and an applied waiter can run between stores.
EXPECT_TRUE(client.ProducerBell().Wait(
clientMemory.CmdControl()->producerParked,
[&] { return client.Progress()->retiredSeq.load(std::memory_order_acquire) >= 1; }, 0, 2000));
}
TEST_F(StreamPair, RetiredByteCursorCrossesManyWrapsWithoutStageRingCursors) {
@@ -152,6 +165,109 @@ namespace {
EXPECT_EQ(publish.WaitForApplied(10, 2000), SessionWait::ShutDown);
}
TEST_F(StreamPair, PeerEofReleasesAnAlreadyWaitingReplyReader) {
std::atomic<bool> finished{false};
MobileGLResult result = MOBILEGL_OK;
std::thread waiting([&] {
std::int32_t status;
const void* payload;
std::uint64_t size;
result = client.ReadReply(1, &status, &payload, &size);
finished.store(true, std::memory_order_release);
});
server.Detach();
EXPECT_TRUE(Eventually([&] { return finished.load(std::memory_order_acquire); }));
waiting.join();
EXPECT_EQ(result, MOBILEGL_ERR_TRANSPORT_CLOSED);
}
TEST_F(StreamPair, AppliedWithoutItsReplyIsRefusedWithoutAnUnboundedWait) {
Queue(1, 1);
ASSERT_EQ(client.Flush(), MOBILEGL_OK);
ASSERT_EQ(apply.WaitForWork(2000), SessionWait::Reached);
ASSERT_TRUE(apply.ApplyOne([](const RingRecordView&) {}));
apply.RetireThrough(1);
ASSERT_EQ(server.FlushProgress(), MOBILEGL_OK);
ASSERT_EQ(publish.WaitForApplied(1, 2000), SessionWait::Reached);
std::int32_t status;
const void* p;
std::uint64_t size;
EXPECT_EQ(client.ReadReply(1, &status, &p, &size), MOBILEGL_ERR_PROTOCOL_MISMATCH);
}
TEST_F(StreamPair, ControlFenceWaitsForEventBytesOnTheIndependentDataConnection) {
EventRingProducer events(serverMemory.EventControl(), serverMemory.CmdControl(), serverMemory.EventRingBase(),
1024);
auto* payload = events.Reserve(kEventGlError, 8);
ASSERT_NE(payload, nullptr);
std::memset(payload, 0, 8);
events.Ring().Publish();
const auto fence = server.EventPublishedHead();
EXPECT_EQ(client.WaitForEventDelivery(fence, 1), MOBILEGL_ERR_TIMEOUT);
ASSERT_EQ(server.Flush(), MOBILEGL_OK);
EXPECT_EQ(client.WaitForEventDelivery(fence, 2000), MOBILEGL_OK);
EXPECT_GE(client.EventPublishedHead(), fence);
}
TEST(StreamLinkTcp, RecordsSixtyFourMiBStageBurstThroughputOnRealLoopbackTcp) {
int listener = ::socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(listener, 0);
sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ASSERT_EQ(::bind(listener, reinterpret_cast<sockaddr*>(&address), sizeof address), 0);
ASSERT_EQ(::listen(listener, 1), 0);
socklen_t addressBytes = sizeof address;
ASSERT_EQ(::getsockname(listener, reinterpret_cast<sockaddr*>(&address), &addressBytes), 0);
int clientFd = ::socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(clientFd, 0);
ASSERT_EQ(::connect(clientFd, reinterpret_cast<sockaddr*>(&address), sizeof address), 0);
int serverFd = ::accept(listener, nullptr, nullptr);
ASSERT_GE(serverFd, 0);
::close(listener);
int enabled = 1;
ASSERT_EQ(::setsockopt(clientFd, IPPROTO_TCP, TCP_NODELAY, &enabled, sizeof enabled), 0);
ASSERT_EQ(::setsockopt(serverFd, IPPROTO_TCP, TCP_NODELAY, &enabled, sizeof enabled), 0);
SessionSegmentSizes sizes;
sizes.CmdRingBytes = 1024;
sizes.EventRingBytes = 1024;
sizes.StageBytes = 64ull * 1024 * 1024;
sizes.ReplyBytes = 4096;
std::unique_ptr<ILink> client, server;
ASSERT_EQ(CreateStreamLink(clientFd, sizes, TransportRoleTag::ClientProducer, client), MOBILEGL_OK);
ASSERT_EQ(CreateStreamLink(serverFd, sizes, TransportRoleTag::ServerConsumer, server), MOBILEGL_OK);
client->InitializeEndpoints();
server->InitializeEndpoints();
std::memset(client->Memory().StageBase(), 0x5a, static_cast<std::size_t>(sizes.StageBytes));
client->NoteStage({0, sizes.StageBytes});
SessionProducer producer;
producer.Attach(*client, 0);
SessionConsumer consumer;
consumer.Attach(*server, 0);
auto* record = client->CommandsOut().Reserve(1, 0, sizeof(std::uint64_t));
ASSERT_NE(record, nullptr);
std::memcpy(record, &sizes.StageBytes, sizeof(std::uint64_t));
producer.PublishAndNotify(1);
const auto start = std::chrono::steady_clock::now();
ASSERT_EQ(client->Flush(), MOBILEGL_OK);
ASSERT_EQ(consumer.WaitForWork(10000), SessionWait::Reached);
ASSERT_TRUE(consumer.ApplyOne([&](const RingRecordView&) {
const auto* bytes = static_cast<const unsigned char*>(server->Memory().StageBase());
for (std::uint64_t offset = 0; offset < sizes.StageBytes; offset += 4096)
EXPECT_EQ(bytes[offset], 0x5a);
EXPECT_EQ(bytes[sizes.StageBytes - 1], 0x5a);
}));
consumer.RetireThrough(1);
ASSERT_EQ(server->FlushProgress(), MOBILEGL_OK);
ASSERT_EQ(producer.WaitForApplied(1, 10000), SessionWait::Reached);
const double elapsed = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
const double mibPerSecond = 64.0 / elapsed;
RecordProperty("tcp_stage_bytes", std::to_string(sizes.StageBytes));
RecordProperty("tcp_stage_mib_per_second", std::to_string(mibPerSecond));
std::printf("P6.5 Stage burst control=none data=tcp-loopback bytes=%llu seconds=%.6f MiB/s=%.3f\n",
static_cast<unsigned long long>(sizes.StageBytes), elapsed, mibPerSecond);
}
void WriteFrame(int fd, unsigned kind, std::uint64_t a, const void* payload, std::uint32_t size) {
unsigned char h[28]{};
auto put = [&](unsigned at, std::uint64_t value, unsigned width) {
@@ -159,7 +275,7 @@ namespace {
h[at + i] = value >> (8 * i);
};
put(0, 0x444c474d, 4);
put(4, size, 4);
put(4, size + 20, 4);
h[8] = kind;
put(12, a, 8);
ASSERT_EQ(::send(fd, h, sizeof h, MSG_NOSIGNAL), sizeof h);