mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 13:05:47 +00:00
Keep unanswered spills and unfinished restores tracked until a resume can release them
This commit is contained in:
1 parent
f17406effc
commit
7ad52166a2
3 files changed
+68
-21
No files matched your search
@@ -56,6 +56,8 @@ internal class SessionSuspendController(
|
||||
private var frozenSince: Long? = null
|
||||
private var pagedOutTargets = emptyList<ProcessRecord>()
|
||||
private var spilledTargets = emptyList<ProcessRecord>()
|
||||
// Asked to spill and not yet released by a resume count they saw: acknowledged or not, each may park its process.
|
||||
private var spillAsked = emptyList<ProcessRecord>()
|
||||
|
||||
fun freeze(pageOut: Boolean = false, spill: Boolean = false, completion: (Boolean) -> Unit) {
|
||||
cancelled = false
|
||||
@@ -131,7 +133,7 @@ internal class SessionSuspendController(
|
||||
if (resumed) frozen = emptyList()
|
||||
// a spilled process runs again only once its helper has read its memory back
|
||||
completion(resumed && awaitRestored())
|
||||
if (resumed) readBack()
|
||||
if (resumed) readBack(waitForRestore = false)
|
||||
}, { completion(false) })
|
||||
}
|
||||
|
||||
@@ -321,6 +323,7 @@ internal class SessionSuspendController(
|
||||
if (targets.isEmpty()) return
|
||||
targets.forEach { ackFile(control, "ack", it).delete(); ackFile(control, "restored", it).delete() }
|
||||
val start = nowMillis()
|
||||
spillAsked = (spillAsked + targets).distinct()
|
||||
for (record in targets) send(record, SIGURG)
|
||||
val waiting = targets.toMutableList()
|
||||
while (!closing && !cancelled && nowMillis() - start < SPILL_TIMEOUT_MS) {
|
||||
@@ -349,24 +352,30 @@ internal class SessionSuspendController(
|
||||
0L
|
||||
}
|
||||
|
||||
// False when a spilled process's helper may not have seen the new count: it would stay stopped.
|
||||
// False when a process asked to spill may not have seen the new count: it would stay stopped. The count is
|
||||
// put in place whole, so a failed write leaves the one before.
|
||||
private fun bumpResume(): Boolean {
|
||||
val control = zramControl?.takeIf { it.isFile } ?: return spilledTargets.isEmpty()
|
||||
val control = zramControl?.takeIf { it.isFile } ?: return spillAsked.isEmpty()
|
||||
val counter = resumeFile(control)
|
||||
return try {
|
||||
val ok = try {
|
||||
val next = resumeCount(control) + 1
|
||||
counter.writeText(next.toString())
|
||||
val fresh = File(counter.parentFile, "${counter.name}.new")
|
||||
fresh.writeText(next.toString())
|
||||
java.nio.file.Files.move(fresh.toPath(), counter.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING,
|
||||
java.nio.file.StandardCopyOption.ATOMIC_MOVE)
|
||||
resumeCount(control) == next
|
||||
} catch (_: IOException) {
|
||||
spilledTargets.isEmpty()
|
||||
false
|
||||
}
|
||||
if (ok) spillAsked = emptyList()
|
||||
return ok || spillAsked.isEmpty()
|
||||
}
|
||||
|
||||
// True when every spilled process has its memory back (or is gone).
|
||||
// True when every spilled process has its memory back (or is gone). Ones still reading stay tracked, so a
|
||||
// later resume waits for them again.
|
||||
private fun awaitRestored(): Boolean {
|
||||
val targets = spilledTargets
|
||||
spilledTargets = emptyList()
|
||||
val control = zramControl ?: return true
|
||||
val targets = spilledTargets
|
||||
if (targets.isEmpty()) return true
|
||||
val start = nowMillis()
|
||||
val waiting = targets.toMutableList()
|
||||
@@ -379,7 +388,8 @@ internal class SessionSuspendController(
|
||||
} catch (_: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
}
|
||||
targets.forEach { ackFile(control, "restored", it).delete() }
|
||||
targets.filterNot { it in waiting }.forEach { ackFile(control, "restored", it).delete() }
|
||||
spilledTargets = waiting.toList()
|
||||
restored(targets.size, waiting.size, memoryKb(targets).first, nowMillis() - start)
|
||||
return waiting.isEmpty()
|
||||
}
|
||||
@@ -413,8 +423,8 @@ internal class SessionSuspendController(
|
||||
false
|
||||
}
|
||||
|
||||
private fun readBack() {
|
||||
awaitRestored()
|
||||
private fun readBack(waitForRestore: Boolean = true) {
|
||||
if (waitForRestore) awaitRestored()
|
||||
val targets = pagedOutTargets.filterNot { isGone(it) }
|
||||
pagedOutTargets = emptyList()
|
||||
val control = zramControl?.takeIf { it.isFile } ?: return
|
||||
|
||||
@@ -162,6 +162,35 @@ class SessionSuspendControllerTest {
|
||||
assertEquals(2, fixture.restores.size)
|
||||
}
|
||||
|
||||
@Test fun aRestoreThatOutlastsTheWaitIsWaitedForAgainByTheNextResume() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.controller.freeze(pageOut = true, spill = true) { assertTrue(it) }
|
||||
fixture.restoreDelayMs = 60_000L
|
||||
fixture.controller.resume { assertFalse(it) }
|
||||
assertEquals(listOf(1 to 1), fixture.restores)
|
||||
assertTrue(fixture.game.threads.all { it.state == 't' })
|
||||
fixture.restoreDelayMs = 0L
|
||||
var called = false
|
||||
fixture.controller.resume {
|
||||
assertTrue(it)
|
||||
assertTrue(fixture.game.threads.none { thread -> thread.state == 't' })
|
||||
called = true
|
||||
}
|
||||
assertTrue(called)
|
||||
assertEquals(listOf(1 to 1, 1 to 0), fixture.restores)
|
||||
}
|
||||
|
||||
@Test fun aResumeCountThatCannotBeWrittenFailsWhileASpillHasNotAnswered() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
fixture.spillSilent = true
|
||||
fixture.controller.freeze(pageOut = true, spill = true) { }
|
||||
File(fixture.control.parentFile, ".dd-zram.resume.new").mkdirs()
|
||||
fixture.controller.resume { assertFalse(it) }
|
||||
assertFalse(File(fixture.control.parentFile, ".dd-zram.resume").isFile)
|
||||
}
|
||||
|
||||
@Test fun aGuestThatCouldNotSpillIsReadBackFromZram() {
|
||||
val fixture = fixture(17)
|
||||
fixture.control.writeText("0")
|
||||
@@ -583,6 +612,8 @@ class SessionSuspendControllerTest {
|
||||
var handlerStalls = false
|
||||
var handlerReplaced = false
|
||||
var spillFallsBack = false
|
||||
var spillSilent = false
|
||||
var restoreDelayMs = RESTORE_DELAY_MS
|
||||
val spills = mutableListOf<Pair<Int, Long>>()
|
||||
val restores = mutableListOf<Pair<Int, Int>>()
|
||||
val resumeAtContinue = mutableListOf<String?>()
|
||||
@@ -651,7 +682,7 @@ class SessionSuspendControllerTest {
|
||||
|
||||
private fun sleep(millis: Long) {
|
||||
now += millis
|
||||
if (spilling) {
|
||||
if (spilling && !spillSilent) {
|
||||
spilling = false
|
||||
val ack = File(control.parentFile, "${control.name}.ack.$GAME")
|
||||
if (spillFallsBack) {
|
||||
@@ -665,7 +696,7 @@ class SessionSuspendControllerTest {
|
||||
}
|
||||
}
|
||||
val count = resume.takeIf { it.isFile }?.readText()?.trim() ?: "0"
|
||||
if (restoring && continuing && count != spilledBase && now - continuedAt >= RESTORE_DELAY_MS) {
|
||||
if (restoring && continuing && count != spilledBase && now - continuedAt >= restoreDelayMs) {
|
||||
restoring = false
|
||||
gameResidentKb = 4_000_000L
|
||||
game.threads.forEach { if (it.state != 'Z') it.state = 'S' }
|
||||
|
||||
@@ -31,6 +31,7 @@ struct shared {
|
||||
volatile long progress;
|
||||
volatile long errors;
|
||||
volatile int stop;
|
||||
volatile int quiet; // readers leave the blocks alone (a page-out they would undo is being measured)
|
||||
};
|
||||
|
||||
static const char *ctl;
|
||||
@@ -58,6 +59,7 @@ static void *work(void *arg) {
|
||||
}
|
||||
unsigned seed = (unsigned)id * 77u + 1u;
|
||||
while (!shared->stop) {
|
||||
if (shared->quiet) continue;
|
||||
if (!same(blocks[id], size, seed)) __atomic_add_fetch(&shared->errors, 1, __ATOMIC_RELAXED);
|
||||
__atomic_add_fetch(&shared->progress, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
@@ -130,21 +132,23 @@ int main(int argc, char **argv) {
|
||||
ctl = argv[1];
|
||||
size = (size_t)atol(argv[2]) << 20;
|
||||
block_urg = argc > 3 && !strcmp(argv[3], "block");
|
||||
if (argc > 3 && !strcmp(argv[3], "undumpable")) prctl(PR_SET_DUMPABLE, 0);
|
||||
int undumpable = argc > 3 && !strcmp(argv[3], "undumpable");
|
||||
if (undumpable) prctl(PR_SET_DUMPABLE, 0);
|
||||
shared = mmap(NULL, sizeof(*shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
if (shared == MAP_FAILED) return 2;
|
||||
char resume[512];
|
||||
snprintf(resume, sizeof(resume), "%s.resume", ctl);
|
||||
unlink(resume);
|
||||
put(".hold", "");
|
||||
for (int i = 0; i < THREADS; i++) {
|
||||
blocks[i] = i % 2 ? malloc(size) : mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (!blocks[i] || blocks[i] == MAP_FAILED) return 3;
|
||||
fill(blocks[i], size, (unsigned)i * 77u + 1u);
|
||||
}
|
||||
if (mprotect(blocks[2], size, PROT_READ) != 0) return 4;
|
||||
pid_t child = fork();
|
||||
if (child == 0) {
|
||||
// the blocks are the child's alone: a page-out skips pages another process still maps
|
||||
for (int i = 0; i < THREADS; i++) {
|
||||
blocks[i] = i % 2 ? malloc(size) : mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
if (!blocks[i] || blocks[i] == MAP_FAILED) _exit(3);
|
||||
fill(blocks[i], size, (unsigned)i * 77u + 1u);
|
||||
}
|
||||
if (mprotect(blocks[2], size, PROT_READ) != 0) _exit(4);
|
||||
pthread_t t[THREADS];
|
||||
for (long i = 0; i < THREADS; i++) pthread_create(&t[i], NULL, work, (void *)i);
|
||||
for (int i = 0; i < THREADS; i++) pthread_join(t[i], NULL);
|
||||
@@ -152,10 +156,12 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
while (shared->progress < 50) nap(10);
|
||||
long before = rss_kb(child);
|
||||
if (undumpable) shared->quiet = 1;
|
||||
put(".hold", "4 0");
|
||||
kill(child, SIGURG);
|
||||
long spilled = wait_for(".ack.", child, 20000);
|
||||
long after = rss_kb(child);
|
||||
shared->quiet = 0;
|
||||
long frozen_at = shared->progress;
|
||||
nap(500);
|
||||
long frozen_now = shared->progress;
|
||||
|
||||
Reference in new issue
Block a user