Log memory and when system calls App#onTrimMemory()

This commit is contained in:
Torsten Grote 2024-09-16 12:01:29 -03:00
parent f188230269
commit 463fc33230
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
5 changed files with 45 additions and 1 deletions

View file

@ -6,6 +6,8 @@
package com.stevesoltys.seedvault package com.stevesoltys.seedvault
import android.Manifest.permission.INTERACT_ACROSS_USERS_FULL import android.Manifest.permission.INTERACT_ACROSS_USERS_FULL
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo
import android.app.Application import android.app.Application
import android.app.backup.BackupManager import android.app.backup.BackupManager
import android.app.backup.BackupManager.PACKAGE_MANAGER_SENTINEL import android.app.backup.BackupManager.PACKAGE_MANAGER_SENTINEL
@ -17,15 +19,16 @@ import android.os.ServiceManager.getService
import android.os.StrictMode import android.os.StrictMode
import android.os.UserHandle import android.os.UserHandle
import android.os.UserManager import android.os.UserManager
import android.util.Log
import androidx.work.ExistingPeriodicWorkPolicy.UPDATE import androidx.work.ExistingPeriodicWorkPolicy.UPDATE
import androidx.work.WorkManager import androidx.work.WorkManager
import com.google.android.material.color.DynamicColors import com.google.android.material.color.DynamicColors
import com.stevesoltys.seedvault.MemoryLogger.getMemStr
import com.stevesoltys.seedvault.backend.BackendManager import com.stevesoltys.seedvault.backend.BackendManager
import com.stevesoltys.seedvault.backend.saf.storagePluginModuleSaf import com.stevesoltys.seedvault.backend.saf.storagePluginModuleSaf
import com.stevesoltys.seedvault.backend.webdav.storagePluginModuleWebDav import com.stevesoltys.seedvault.backend.webdav.storagePluginModuleWebDav
import com.stevesoltys.seedvault.crypto.cryptoModule import com.stevesoltys.seedvault.crypto.cryptoModule
import com.stevesoltys.seedvault.header.headerModule import com.stevesoltys.seedvault.header.headerModule
import com.stevesoltys.seedvault.metadata.MetadataManager
import com.stevesoltys.seedvault.metadata.metadataModule import com.stevesoltys.seedvault.metadata.metadataModule
import com.stevesoltys.seedvault.restore.install.installModule import com.stevesoltys.seedvault.restore.install.installModule
import com.stevesoltys.seedvault.restore.restoreUiModule import com.stevesoltys.seedvault.restore.restoreUiModule
@ -154,6 +157,15 @@ open class App : Application() {
private val backendManager: BackendManager by inject() private val backendManager: BackendManager by inject()
private val backupStateManager: BackupStateManager by inject() private val backupStateManager: BackupStateManager by inject()
override fun onTrimMemory(level: Int) {
Log.w("Seedvault", "onTrimMemory($level) ${getMemStr()}")
val processInfo = RunningAppProcessInfo()
ActivityManager.getMyMemoryState(processInfo)
Log.w("Seedvault", " lastTrimLevel: ${processInfo.lastTrimLevel}")
Log.w("Seedvault", " importance: ${processInfo.importance}")
super.onTrimMemory(level)
}
/** /**
* Disables the framework scheduling in favor of our own. * Disables the framework scheduling in favor of our own.
* Introduced in the first half of 2024 and can be removed after a suitable migration period. * Introduced in the first half of 2024 and can be removed after a suitable migration period.

View file

@ -0,0 +1,24 @@
/*
* SPDX-FileCopyrightText: 2024 The Calyx Institute
* SPDX-License-Identifier: Apache-2.0
*/
package com.stevesoltys.seedvault
import android.util.Log
object MemoryLogger {
fun log() {
Log.d("MemoryLogger", getMemStr())
}
fun getMemStr(): String {
val r = Runtime.getRuntime()
val total = r.totalMemory() / 1024 / 1024
val free = r.freeMemory() / 1024 / 1024
val max = r.maxMemory() / 1024 / 1024
val used = total - free
return "$free MiB free - $used of $total (max $max)"
}
}

View file

@ -6,6 +6,7 @@
package com.stevesoltys.seedvault.transport.backup package com.stevesoltys.seedvault.transport.backup
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import com.stevesoltys.seedvault.MemoryLogger
import com.stevesoltys.seedvault.backend.BackendManager import com.stevesoltys.seedvault.backend.BackendManager
import com.stevesoltys.seedvault.crypto.Crypto import com.stevesoltys.seedvault.crypto.Crypto
import com.stevesoltys.seedvault.settings.SettingsManager import com.stevesoltys.seedvault.settings.SettingsManager
@ -52,6 +53,7 @@ internal class AppBackupManager(
@WorkerThread @WorkerThread
suspend fun afterBackupFinished(success: Boolean): com.stevesoltys.seedvault.proto.Snapshot? { suspend fun afterBackupFinished(success: Boolean): com.stevesoltys.seedvault.proto.Snapshot? {
MemoryLogger.log()
log.info { "After backup finished. Success: $success" } log.info { "After backup finished. Success: $success" }
// free up memory by clearing blobs cache // free up memory by clearing blobs cache
blobCache.clear() blobCache.clear()
@ -72,6 +74,7 @@ internal class AppBackupManager(
null null
} finally { } finally {
snapshotCreator = null snapshotCreator = null
MemoryLogger.log()
} }
} }

View file

@ -7,6 +7,7 @@ package com.stevesoltys.seedvault.transport.backup
import android.content.Context import android.content.Context
import android.content.Context.MODE_APPEND import android.content.Context.MODE_APPEND
import com.stevesoltys.seedvault.MemoryLogger
import com.stevesoltys.seedvault.proto.Snapshot import com.stevesoltys.seedvault.proto.Snapshot
import com.stevesoltys.seedvault.proto.Snapshot.Blob import com.stevesoltys.seedvault.proto.Snapshot.Blob
import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.KotlinLogging
@ -41,6 +42,7 @@ class BlobCache(
fun populateCache(blobs: List<FileInfo>, snapshots: List<Snapshot>) { fun populateCache(blobs: List<FileInfo>, snapshots: List<Snapshot>) {
log.info { "Getting all blobs from backend..." } log.info { "Getting all blobs from backend..." }
blobMap.clear() blobMap.clear()
MemoryLogger.log()
// create map of blobId to size of blob on backend // create map of blobId to size of blob on backend
val blobIds = blobs.associate { val blobIds = blobs.associate {
Pair(it.fileHandle.name, it.size.toInt()) Pair(it.fileHandle.name, it.size.toInt())
@ -51,6 +53,7 @@ class BlobCache(
snapshots.forEach { snapshot -> snapshots.forEach { snapshot ->
onSnapshotLoaded(snapshot, blobIds) onSnapshotLoaded(snapshot, blobIds)
} }
MemoryLogger.log()
} }
/** /**

View file

@ -8,6 +8,7 @@ package com.stevesoltys.seedvault.transport.backup
import androidx.annotation.WorkerThread import androidx.annotation.WorkerThread
import com.github.luben.zstd.ZstdOutputStream import com.github.luben.zstd.ZstdOutputStream
import com.google.protobuf.ByteString import com.google.protobuf.ByteString
import com.stevesoltys.seedvault.MemoryLogger
import com.stevesoltys.seedvault.backend.BackendManager import com.stevesoltys.seedvault.backend.BackendManager
import com.stevesoltys.seedvault.crypto.Crypto import com.stevesoltys.seedvault.crypto.Crypto
import com.stevesoltys.seedvault.header.VERSION import com.stevesoltys.seedvault.header.VERSION
@ -61,6 +62,7 @@ internal class BlobCreator(
// we could just write 0s, but because of defense in depth, we use random bytes // we could just write 0s, but because of defense in depth, we use random bytes
cryptoStream.write(crypto.getRandomBytes(paddingSize)) cryptoStream.write(crypto.getRandomBytes(paddingSize))
} }
MemoryLogger.log()
payloadBuffer.clear() payloadBuffer.clear()
// compute hash and save blob // compute hash and save blob