Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5218b65c21 |
No files matched your search
@@ -1,4 +1,5 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <jni.h>
|
||||
#include <limits.h>
|
||||
#include <linux/futex.h>
|
||||
@@ -27,3 +28,16 @@ Java_com_droiddeck_launcher_session_ThreadSignals_tgkill(JNIEnv *env, jclass cla
|
||||
(void)clazz;
|
||||
return syscall(SYS_tgkill, tgid, tid, signal) == 0 ? 0 : errno;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_com_droiddeck_launcher_session_PageCacheNative_fadviseDontNeed(JNIEnv *env, jclass clazz, jstring path) {
|
||||
(void)clazz;
|
||||
const char *chars = (*env)->GetStringUTFChars(env, path, NULL);
|
||||
if (!chars) return ENOMEM;
|
||||
int fd = open(chars, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
|
||||
(*env)->ReleaseStringUTFChars(env, path, chars);
|
||||
if (fd < 0) return errno;
|
||||
int rc = posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
|
||||
close(fd);
|
||||
return rc;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import android.util.Log
|
||||
import java.io.File
|
||||
|
||||
internal object PageCacheTrim {
|
||||
private const val TAG = "PageCacheTrim"
|
||||
private const val MIN_FILE_BYTES = 8L shl 20
|
||||
private const val POLL_MS = 3000L
|
||||
private const val RESCAN_MS = 120_000L
|
||||
|
||||
data class Mem(val freeKb: Long, val fileKb: Long)
|
||||
|
||||
fun parse(meminfo: String): Mem? {
|
||||
fun field(name: String) = Regex("""(?m)^$name:\s+(\d+) kB""").find(meminfo)?.groupValues?.get(1)?.toLongOrNull()
|
||||
val free = field("MemFree") ?: return null
|
||||
val file = (field("Active\\(file\\)") ?: return null) + (field("Inactive\\(file\\)") ?: return null)
|
||||
return Mem(free, file)
|
||||
}
|
||||
|
||||
fun shouldTrim(mem: Mem, baselineFileKb: Long): Boolean {
|
||||
val built = mem.fileKb - baselineFileKb
|
||||
return (mem.freeKb < 2560 * 1024 && built >= 768 * 1024) ||
|
||||
(mem.freeKb < 1024 * 1024 && built >= 128 * 1024)
|
||||
}
|
||||
|
||||
fun start(roots: () -> List<File>, alive: () -> Boolean) {
|
||||
Thread({
|
||||
var baseline = 0L
|
||||
var files = emptyList<File>()
|
||||
var scanned = 0L
|
||||
var trims = 0
|
||||
var failureLogged = false
|
||||
while (alive()) {
|
||||
runCatching {
|
||||
val mem = parse(File("/proc/meminfo").readText())
|
||||
if (mem != null && shouldTrim(mem, baseline)) {
|
||||
val now = System.currentTimeMillis()
|
||||
if (now - scanned > RESCAN_MS) {
|
||||
files = roots().flatMap { root -> root.walkTopDown().onEnter { !java.nio.file.Files.isSymbolicLink(it.toPath()) }.filter { it.isFile && it.length() >= MIN_FILE_BYTES }.toList() }
|
||||
scanned = now
|
||||
}
|
||||
val began = System.nanoTime()
|
||||
files.forEach(::drop)
|
||||
val after = parse(File("/proc/meminfo").readText())
|
||||
baseline = after?.fileKb ?: mem.fileKb
|
||||
trims++
|
||||
Log.i(TAG, "trim #$trims: free ${mem.freeKb / 1024} -> ${after?.freeKb?.div(1024)} MB, file cache ${mem.fileKb / 1024} -> ${after?.fileKb?.div(1024)} MB, ${files.size} files, ${(System.nanoTime() - began) / 1_000_000} ms")
|
||||
}
|
||||
}.onFailure {
|
||||
if (!failureLogged) Log.w(TAG, "trim failed: $it")
|
||||
failureLogged = true
|
||||
}
|
||||
Thread.sleep(POLL_MS)
|
||||
}
|
||||
}, "page-cache-trim").apply { isDaemon = true }.start()
|
||||
}
|
||||
|
||||
private fun drop(file: File) {
|
||||
PageCacheNative.fadviseDontNeed(file.path)
|
||||
}
|
||||
}
|
||||
|
||||
internal object PageCacheNative {
|
||||
init {
|
||||
System.loadLibrary("sdnative")
|
||||
}
|
||||
|
||||
@JvmStatic external fun fadviseDontNeed(path: String): Int
|
||||
}
|
||||
@@ -426,6 +426,11 @@ class SessionService : Service() {
|
||||
++sessionGen
|
||||
}
|
||||
}
|
||||
val trimGen = gen
|
||||
PageCacheTrim.start(
|
||||
roots = { listOfNotNull(File(filesDir, "linuxfs"), GameStorage.effective(applicationContext)?.let { File(it.path) }).filter(File::isDirectory) },
|
||||
alive = { SessionState.running && sessionGen == trimGen },
|
||||
)
|
||||
if (SessionState.stopRequested) {
|
||||
stopSession(0)
|
||||
return START_NOT_STICKY
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class PageCacheTrimTest {
|
||||
private val meminfo = "MemFree: 1048576 kB\nActive(file): 2097152 kB\nInactive(file): 1048576 kB\n"
|
||||
|
||||
@Test fun parsesFreeAndFileLru() = assertEquals(PageCacheTrim.Mem(1048576, 3145728), PageCacheTrim.parse(meminfo))
|
||||
@Test fun rejectsMissingFields() = assertNull(PageCacheTrim.parse("MemFree: 1 kB\n"))
|
||||
@Test fun trimsWhenCacheBuiltUpAndFreeIsLow() = assertTrue(PageCacheTrim.shouldTrim(PageCacheTrim.Mem(2000 * 1024, 2000 * 1024), 1000 * 1024))
|
||||
@Test fun skipsSmallBuildUp() = assertFalse(PageCacheTrim.shouldTrim(PageCacheTrim.Mem(2000 * 1024, 1200 * 1024), 1000 * 1024))
|
||||
@Test fun skipsWhenPlentyFree() = assertFalse(PageCacheTrim.shouldTrim(PageCacheTrim.Mem(6000 * 1024, 5000 * 1024), 1000 * 1024))
|
||||
@Test fun lowFreeAcceptsSmallerBuildUp() = assertTrue(PageCacheTrim.shouldTrim(PageCacheTrim.Mem(500 * 1024, 1200 * 1024), 1000 * 1024))
|
||||
}
|
||||
Reference in new issue
Block a user