mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-17 23:13:22 +09:00
[Android] retain trace replay ownership across Activity recreation
This commit is contained in:
@@ -298,6 +298,17 @@ if(NOT CMAKE_CROSSCOMPILING)
|
||||
COMMAND mobilegl_trace_env_overrides_test
|
||||
COMMENT "Checking the retrace env passthrough parser")
|
||||
add_dependencies(mobilegl_trace_replay mobilegl_trace_env_overrides_test)
|
||||
|
||||
add_executable(mobilegl_trace_replay_lease_test
|
||||
"${MOBILEGL_TRACE_ROOT}/trace_replay_lease_test.cpp")
|
||||
target_compile_features(mobilegl_trace_replay_lease_test PRIVATE cxx_std_17)
|
||||
target_include_directories(mobilegl_trace_replay_lease_test PRIVATE
|
||||
"${MOBILEGL_TRACE_SHARED_CPP_DIR}")
|
||||
target_link_libraries(mobilegl_trace_replay_lease_test PRIVATE Threads::Threads)
|
||||
add_custom_command(TARGET mobilegl_trace_replay_lease_test POST_BUILD
|
||||
COMMAND mobilegl_trace_replay_lease_test
|
||||
COMMENT "Checking process ownership of native trace replay")
|
||||
add_dependencies(mobilegl_trace_replay mobilegl_trace_replay_lease_test)
|
||||
endif()
|
||||
|
||||
if(MOBILEGL_TRACE_REPLAY_MOBILEGL_LIBRARY)
|
||||
@@ -464,4 +475,4 @@ execute_process(
|
||||
--fixture-root "${MOBILEGL_TRACE_ROOT}/fixtures"
|
||||
OUTPUT_FILE "${MOBILEGL_TRACE_CASES_CMAKE}"
|
||||
COMMAND_ERROR_IS_FATAL ANY)
|
||||
include("${MOBILEGL_TRACE_CASES_CMAKE}")
|
||||
include("${MOBILEGL_TRACE_CASES_CMAKE}")
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
package top.mobilegl.plugin.trace;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** Deterministic Activity replacement while native work or its completion is pending. */
|
||||
public final class TraceReplaySessionTest {
|
||||
private static final class Queue implements Executor {
|
||||
final ArrayDeque<Runnable> commands = new ArrayDeque<>();
|
||||
|
||||
@Override
|
||||
public void execute(Runnable command) {
|
||||
commands.add(command);
|
||||
}
|
||||
|
||||
void drain() {
|
||||
while (!commands.isEmpty()) {
|
||||
commands.remove().run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void require(boolean condition, String message) {
|
||||
if (!condition) {
|
||||
throw new AssertionError(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Queue worker = new Queue();
|
||||
Queue main = new Queue();
|
||||
TraceReplaySession<String> session = new TraceReplaySession<>(worker, main);
|
||||
List<String> oldResults = new ArrayList<>();
|
||||
List<String> newResults = new ArrayList<>();
|
||||
TraceReplaySession.Listener<String> oldActivity = oldResults::add;
|
||||
TraceReplaySession.Listener<String> newActivity = newResults::add;
|
||||
int[] nativeCalls = {0};
|
||||
|
||||
// Recreation before a valid Surface: the replacement may start the first run.
|
||||
session.attach(oldActivity);
|
||||
session.detach(oldActivity);
|
||||
session.attach(newActivity);
|
||||
require(session.start(() -> { ++nativeCalls[0]; return "real native result"; }),
|
||||
"replacement must be able to start a pending request");
|
||||
require(session.hasStarted(), "ownership must be published before worker dispatch");
|
||||
|
||||
// surfaceCreated and surfaceChanged can both queue callbacks, and a recreated
|
||||
// Activity receives both again. None may dispatch another native invocation.
|
||||
session.detach(newActivity);
|
||||
session.attach(oldActivity);
|
||||
require(!session.start(() -> { ++nativeCalls[0]; return "duplicate"; }),
|
||||
"Activity recreation must retain the running invocation");
|
||||
worker.drain();
|
||||
require(nativeCalls[0] == 1, "only one worker may execute the native request");
|
||||
|
||||
// The native result can arrive between the old Activity's destruction and the
|
||||
// replacement attaching. It must be kept, without calling a destroyed owner.
|
||||
session.detach(oldActivity);
|
||||
main.drain();
|
||||
require(oldResults.isEmpty() && newResults.isEmpty(), "destroyed owners must not be notified");
|
||||
session.attach(newActivity);
|
||||
require(newResults.equals(List.of("real native result")), "replacement must receive original result");
|
||||
require(!session.start(() -> "replayed completed request"), "completed request must not replay");
|
||||
|
||||
// An obsolete owner must not detach the current owner, including while running.
|
||||
TraceReplaySession<String> next = new TraceReplaySession<>(worker, main);
|
||||
next.attach(oldActivity);
|
||||
next.attach(newActivity);
|
||||
next.detach(oldActivity);
|
||||
next.start(() -> "next native result");
|
||||
worker.drain();
|
||||
main.drain();
|
||||
require(oldResults.isEmpty(), "obsolete owner was notified");
|
||||
require(newResults.equals(List.of("real native result", "next native result")),
|
||||
"late detach lost the current owner");
|
||||
|
||||
// Replace the observer while the worker is actually inside the native-call
|
||||
// boundary, not just queued. Latches force the interleaving without sleeps.
|
||||
CountDownLatch entered = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
List<Thread> threads = new ArrayList<>();
|
||||
TraceReplaySession<String> running = new TraceReplaySession<>(command -> {
|
||||
Thread thread = new Thread(command);
|
||||
threads.add(thread);
|
||||
thread.start();
|
||||
}, main);
|
||||
running.attach(oldActivity);
|
||||
running.start(() -> {
|
||||
entered.countDown();
|
||||
try {
|
||||
if (!release.await(5, TimeUnit.SECONDS)) {
|
||||
throw new AssertionError("test did not release the native invocation");
|
||||
}
|
||||
} catch (InterruptedException exception) {
|
||||
throw new AssertionError(exception);
|
||||
}
|
||||
return "running native result";
|
||||
});
|
||||
require(entered.await(5, TimeUnit.SECONDS), "native worker did not enter");
|
||||
running.detach(oldActivity);
|
||||
running.attach(newActivity);
|
||||
boolean duplicate = running.start(() -> "duplicate while running");
|
||||
release.countDown();
|
||||
for (Thread thread : threads) {
|
||||
thread.join(5000);
|
||||
require(!thread.isAlive(), "native worker did not finish");
|
||||
}
|
||||
require(!duplicate, "recreation entered native code while original invocation was active");
|
||||
main.drain();
|
||||
require(oldResults.isEmpty(), "running invocation notified destroyed Activity");
|
||||
require(newResults.equals(List.of("real native result", "next native result", "running native result")),
|
||||
"running invocation lost its original result during Activity replacement");
|
||||
System.out.println("TraceReplaySession: pending, running and completed recreation passed");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run the Android replay ownership regression without an emulator or Gradle."""
|
||||
|
||||
import pathlib
|
||||
import subprocess
|
||||
import tempfile
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
|
||||
def main():
|
||||
root = pathlib.Path(__file__).resolve().parents[2]
|
||||
trace = root / "android-plugin/app/src/trace"
|
||||
android = "{http://schemas.android.com/apk/res/android}"
|
||||
manifest = ET.parse(trace / "AndroidManifest.xml")
|
||||
activity = next(a for a in manifest.findall("application/activity")
|
||||
if a.get(android + "name") == ".trace.TraceReplayActivity")
|
||||
config_value = activity.get(android + "configChanges", "0")
|
||||
if config_value == "@integer/trace_replay_config_changes":
|
||||
resources = ET.parse(trace / "res/values/config.xml")
|
||||
config_value = resources.find("integer[@name='trace_replay_config_changes']").text
|
||||
changes = int(config_value, 0)
|
||||
# The failing emulator added resource overlays after replay had already started.
|
||||
# orientation/screenSize alone does not handle CONFIG_ASSETS_PATHS.
|
||||
if changes & 0x80000000 == 0:
|
||||
raise RuntimeError("TraceReplayActivity must handle CONFIG_ASSETS_PATHS")
|
||||
if changes & 0xF80 != 0xF80:
|
||||
raise RuntimeError("TraceReplayActivity must retain the fixed-size render surface on display changes")
|
||||
with tempfile.TemporaryDirectory(prefix="mobilegl-replay-lifecycle-") as output:
|
||||
subprocess.run([
|
||||
"javac", "--release", "11", "-d", output,
|
||||
str(trace / "java/top/mobilegl/plugin/trace/TraceReplaySession.java"),
|
||||
str(root / "tools/trace_replay/TraceReplaySessionTest.java"),
|
||||
], check=True)
|
||||
subprocess.run(["java", "-cp", output, "top.mobilegl.plugin.trace.TraceReplaySessionTest"], check=True)
|
||||
print("Android replay manifest and lifecycle checks passed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "trace_replay_lease.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
using mobilegl_trace::TraceReplayLease;
|
||||
std::atomic<int> rejected{0};
|
||||
{
|
||||
TraceReplayLease original;
|
||||
if (!original) {
|
||||
std::cerr << "initial replay could not claim process state\n";
|
||||
return 1;
|
||||
}
|
||||
std::vector<std::thread> contenders;
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
contenders.emplace_back([&] {
|
||||
TraceReplayLease duplicate;
|
||||
if (!duplicate) {
|
||||
++rejected;
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& thread : contenders) {
|
||||
thread.join();
|
||||
}
|
||||
if (rejected != 16) {
|
||||
std::cerr << "a duplicate replay entered the live process state\n";
|
||||
return 1;
|
||||
}
|
||||
// A rejected caller's destructor must not release the real owner's claim.
|
||||
TraceReplayLease stillDuplicate;
|
||||
if (stillDuplicate) {
|
||||
std::cerr << "rejected caller released the original replay's claim\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
TraceReplayLease next;
|
||||
if (!next) {
|
||||
std::cerr << "completed replay did not release process state\n";
|
||||
return 1;
|
||||
}
|
||||
std::cout << "TraceReplayLease: concurrent invocations rejected; owner cleanup retained\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user