From 463fc33230cdb375b15f5e0638049e9be0fbf45c Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 16 Sep 2024 12:01:29 -0300 Subject: [PATCH] Log memory and when system calls App#onTrimMemory() --- .../java/com/stevesoltys/seedvault/App.kt | 14 ++++++++++- .../com/stevesoltys/seedvault/MemoryLogger.kt | 24 +++++++++++++++++++ .../transport/backup/AppBackupManager.kt | 3 +++ .../seedvault/transport/backup/BlobCache.kt | 3 +++ .../seedvault/transport/backup/BlobCreator.kt | 2 ++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/stevesoltys/seedvault/MemoryLogger.kt diff --git a/app/src/main/java/com/stevesoltys/seedvault/App.kt b/app/src/main/java/com/stevesoltys/seedvault/App.kt index 90e6b3ad..57ac6b34 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/App.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/App.kt @@ -6,6 +6,8 @@ package com.stevesoltys.seedvault import android.Manifest.permission.INTERACT_ACROSS_USERS_FULL +import android.app.ActivityManager +import android.app.ActivityManager.RunningAppProcessInfo import android.app.Application import android.app.backup.BackupManager import android.app.backup.BackupManager.PACKAGE_MANAGER_SENTINEL @@ -17,15 +19,16 @@ import android.os.ServiceManager.getService import android.os.StrictMode import android.os.UserHandle import android.os.UserManager +import android.util.Log import androidx.work.ExistingPeriodicWorkPolicy.UPDATE import androidx.work.WorkManager import com.google.android.material.color.DynamicColors +import com.stevesoltys.seedvault.MemoryLogger.getMemStr import com.stevesoltys.seedvault.backend.BackendManager import com.stevesoltys.seedvault.backend.saf.storagePluginModuleSaf import com.stevesoltys.seedvault.backend.webdav.storagePluginModuleWebDav import com.stevesoltys.seedvault.crypto.cryptoModule import com.stevesoltys.seedvault.header.headerModule -import com.stevesoltys.seedvault.metadata.MetadataManager import com.stevesoltys.seedvault.metadata.metadataModule import com.stevesoltys.seedvault.restore.install.installModule import com.stevesoltys.seedvault.restore.restoreUiModule @@ -154,6 +157,15 @@ open class App : Application() { private val backendManager: BackendManager 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. * Introduced in the first half of 2024 and can be removed after a suitable migration period. diff --git a/app/src/main/java/com/stevesoltys/seedvault/MemoryLogger.kt b/app/src/main/java/com/stevesoltys/seedvault/MemoryLogger.kt new file mode 100644 index 00000000..7adfd676 --- /dev/null +++ b/app/src/main/java/com/stevesoltys/seedvault/MemoryLogger.kt @@ -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)" + } +} diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/AppBackupManager.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/AppBackupManager.kt index d3eb96eb..0cebe6e8 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/AppBackupManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/AppBackupManager.kt @@ -6,6 +6,7 @@ package com.stevesoltys.seedvault.transport.backup import androidx.annotation.WorkerThread +import com.stevesoltys.seedvault.MemoryLogger import com.stevesoltys.seedvault.backend.BackendManager import com.stevesoltys.seedvault.crypto.Crypto import com.stevesoltys.seedvault.settings.SettingsManager @@ -52,6 +53,7 @@ internal class AppBackupManager( @WorkerThread suspend fun afterBackupFinished(success: Boolean): com.stevesoltys.seedvault.proto.Snapshot? { + MemoryLogger.log() log.info { "After backup finished. Success: $success" } // free up memory by clearing blobs cache blobCache.clear() @@ -72,6 +74,7 @@ internal class AppBackupManager( null } finally { snapshotCreator = null + MemoryLogger.log() } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BlobCache.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BlobCache.kt index 5ee83a9c..28e5c8e9 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BlobCache.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BlobCache.kt @@ -7,6 +7,7 @@ package com.stevesoltys.seedvault.transport.backup import android.content.Context import android.content.Context.MODE_APPEND +import com.stevesoltys.seedvault.MemoryLogger import com.stevesoltys.seedvault.proto.Snapshot import com.stevesoltys.seedvault.proto.Snapshot.Blob import io.github.oshai.kotlinlogging.KotlinLogging @@ -41,6 +42,7 @@ class BlobCache( fun populateCache(blobs: List, snapshots: List) { log.info { "Getting all blobs from backend..." } blobMap.clear() + MemoryLogger.log() // create map of blobId to size of blob on backend val blobIds = blobs.associate { Pair(it.fileHandle.name, it.size.toInt()) @@ -51,6 +53,7 @@ class BlobCache( snapshots.forEach { snapshot -> onSnapshotLoaded(snapshot, blobIds) } + MemoryLogger.log() } /** diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BlobCreator.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BlobCreator.kt index 0bc0a431..c75249cb 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BlobCreator.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/BlobCreator.kt @@ -8,6 +8,7 @@ package com.stevesoltys.seedvault.transport.backup import androidx.annotation.WorkerThread import com.github.luben.zstd.ZstdOutputStream import com.google.protobuf.ByteString +import com.stevesoltys.seedvault.MemoryLogger import com.stevesoltys.seedvault.backend.BackendManager import com.stevesoltys.seedvault.crypto.Crypto 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 cryptoStream.write(crypto.getRandomBytes(paddingSize)) } + MemoryLogger.log() payloadBuffer.clear() // compute hash and save blob