Skip to content

Commit

Permalink
Log memory and when system calls App#onTrimMemory()
Browse files Browse the repository at this point in the history
  • Loading branch information
grote committed Sep 16, 2024
1 parent 0005d05 commit 48215d9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/src/main/java/com/stevesoltys/seedvault/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/stevesoltys/seedvault/MemoryLogger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 used = total - free
return "$free MiB free - $used of $total"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -72,6 +74,7 @@ internal class AppBackupManager(
null
} finally {
snapshotCreator = null
MemoryLogger.log()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +41,7 @@ class BlobCache(
fun populateCache(blobs: List<FileInfo>, snapshots: List<Snapshot>) {
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())
Expand All @@ -50,6 +52,7 @@ class BlobCache(
snapshots.forEach { snapshot ->
onSnapshotLoaded(snapshot, blobIds)
}
MemoryLogger.log()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 48215d9

Please sign in to comment.