Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Application
import android.app.ActivityManager
import android.os.Build
import android.os.Process
import com.shifthackz.aisdv1.app.inference.AndroidCurrentProcessPerformanceReader
import com.shifthackz.aisdv1.app.inference.InferenceServiceDependencies
import android.database.CursorWindow
import android.os.StrictMode
Expand Down Expand Up @@ -56,6 +57,7 @@ class AiStableDiffusionClientApp : Application() {
Timber.plant(Timber.DebugTree())
}
inferenceDependencies = InferenceServiceDependencies(
performanceReader = AndroidCurrentProcessPerformanceReader(),
engines = createNativeInferenceEngineFactory(this, BuildConfig.BUILD_TYPE == "dev"),
files = FileGenerationFileStore(this, CompletePngImageValidator()),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.shifthackz.aisdv1.app.inference

import android.os.Process
import android.os.Debug
import com.shifthackz.aisdv1.domain.entity.ProcessPerformanceReading

/**
* Reads the current process's proportional resident memory through the public Android API.
* Shared pages are apportioned so summing host and worker readings avoids counting them twice.
* @author Dmitriy Moroz
*/
internal class AndroidCurrentProcessPerformanceReader : CurrentProcessPerformanceReader {
override fun read(): ProcessPerformanceReading {
val memory = Debug.MemoryInfo()
Debug.getMemoryInfo(memory)
val residentKilobytes = memory.dalvikPss.toLong() + memory.nativePss + memory.otherPss
check(residentKilobytes > 0)
return ProcessPerformanceReading(
identity = "${Process.myPid()}:${Process.getStartElapsedRealtime()}",
cpuTimeNanos = Process.getElapsedCpuTime() * 1_000_000L,
residentBytes = residentKilobytes * 1024L,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.os.Message
import android.os.Messenger
import android.os.Process
import android.os.SystemClock
import com.shifthackz.aisdv1.domain.entity.ProcessPerformanceReading
import com.shifthackz.aisdv1.domain.entity.LocalDiffusionStatus
import com.shifthackz.aisdv1.domain.entity.NativeGenerationEngine
import com.shifthackz.aisdv1.domain.entity.NativeGenerationStage
Expand All @@ -28,6 +29,8 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withTimeoutOrNull

/**
Expand All @@ -42,11 +45,16 @@ internal class AndroidInferenceConnection(private val context: Context) : Infere
private val ready = CompletableDeferred<Unit>()
private val identityReady = CompletableDeferred<Unit>()
private val result = CompletableDeferred<Unit>()
private val performanceMutex = Mutex()
private var resourceRequestId = 0L
private var pendingPerformance: CompletableDeferred<ProcessPerformanceReading>? = null
private val dead = CompletableDeferred<Unit>()
private val reply = Messenger(Handler(Looper.getMainLooper(), ::onMessage))
private var remote: Messenger? = null
private var binder: IBinder? = null
private var incarnation = ""

@Volatile
private var pid = -1
private var sequence = -1L
private var job = ""
Expand Down Expand Up @@ -156,6 +164,27 @@ internal class AndroidInferenceConnection(private val context: Context) : Infere
result.await()
}

/**
* Serializes on-demand resource requests on the control dispatcher. A short read deadline affects
* only the overlay; it never interrupts native generation or its heartbeat.
* @author Dmitriy Moroz
*/
override suspend fun readPerformance(): ProcessPerformanceReading? = performanceMutex.withLock {
withContext(Dispatchers.Main.immediate) {
if (!hasSubmitted || result.isCompleted) return@withContext null
val response = CompletableDeferred<ProcessPerformanceReading>()
pendingPerformance = response
resourceRequestId++
try {
send(InferenceProtocol.READ_PERFORMANCE)
withTimeoutOrNull(2_000) { response.await() }
?: error("Inference resource reading timed out")
} finally {
pendingPerformance = null
}
}
}

private fun onMessage(message: Message): Boolean {
val data = message.data
if (message.sendingUid != Process.myUid() || data.getInt(InferenceProtocol.VERSION_KEY) != InferenceProtocol.VERSION) return true
Expand All @@ -178,6 +207,23 @@ internal class AndroidInferenceConnection(private val context: Context) : Infere
}
if (data.getString(InferenceProtocol.JOB) != job || data.getString(InferenceProtocol.ENGINE_KEY) != expectedEngine?.name) return true
when (message.what) {
InferenceProtocol.PERFORMANCE -> {
if (data.getLong(InferenceProtocol.RESOURCE_REQUEST_ID) != resourceRequestId) return true
val cpu = data.getLong(InferenceProtocol.CPU_NANOS, -1)
val rss = data.getLong(InferenceProtocol.RESIDENT_BYTES, -1)
if (cpu >= 0 && rss >= 0) {
pendingPerformance?.complete(
ProcessPerformanceReading(
"$pid:$incarnation",
cpu,
rss
)
)
} else {
pendingPerformance?.completeExceptionally(IllegalStateException("Inference resource reading failed"))
}
}

InferenceProtocol.STAGE -> if (!result.isCompleted) {
runCatching {
NativeGenerationStage.valueOf(
Expand Down Expand Up @@ -208,6 +254,10 @@ internal class AndroidInferenceConnection(private val context: Context) : Infere
private fun send(what: Int) {
try {
remote?.send(InferenceProtocol.message(what, incarnation, job, replyTo = reply).apply {
if (what == InferenceProtocol.READ_PERFORMANCE) data.putLong(
InferenceProtocol.RESOURCE_REQUEST_ID,
resourceRequestId
)
if (what == InferenceProtocol.SUBMIT) data.putString(
InferenceProtocol.ENGINE_KEY,
expectedEngine?.name
Expand All @@ -219,6 +269,7 @@ internal class AndroidInferenceConnection(private val context: Context) : Infere
}

private fun processDied() {
pendingPerformance?.completeExceptionally(IllegalStateException("Inference process exited"))
dead.complete(Unit)
fail(NativeGenerationFailure.Category.PROCESS_DIED)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.shifthackz.aisdv1.app.inference

import com.shifthackz.aisdv1.domain.entity.ProcessPerformanceReading

/** Reads native-worker counters from inside that worker's sandbox. @author Dmitriy Moroz */
internal interface CurrentProcessPerformanceReader {
/**
* Reads CPU and resident memory without retaining a sampler; invoke off the control thread.
* File and platform failures propagate to the requesting host as an unavailable reading.
* @author Dmitriy Moroz
*/
fun read(): ProcessPerformanceReading
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.shifthackz.aisdv1.app.inference

import com.shifthackz.aisdv1.domain.entity.ProcessPerformanceReading
import com.shifthackz.aisdv1.domain.entity.LocalDiffusionStatus
import com.shifthackz.aisdv1.domain.entity.NativeGenerationEngine
import com.shifthackz.aisdv1.domain.entity.NativeGenerationStage
Expand All @@ -17,6 +18,13 @@ internal interface InferenceConnection {
*/
val processId: Int?

/**
* Reads child-owned counters on demand. Returns null before submission or after completion;
* timeout, child death and malformed responses fail this read without cancelling inference.
* @author Dmitriy Moroz
*/
suspend fun readPerformance(): ProcessPerformanceReading?

/**
* Binds and validates one child incarnation, persists its identity, then awaits this job completion.
* Delivers only matching job events; binding, protocol or child-exit failures propagate to the host.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import android.os.Messenger
* @author Dmitriy Moroz
*/
internal object InferenceProtocol {
const val VERSION = 1
const val VERSION = 2
const val HELLO = 1
const val READY = 2
const val SUBMIT = 3
Expand All @@ -21,6 +21,11 @@ internal object InferenceProtocol {
const val PING = 8
const val PONG = 9
const val EXIT = 10
const val RESOURCE_REQUEST_ID = "resource_request_id"
const val READ_PERFORMANCE = 12
const val PERFORMANCE = 13
const val CPU_NANOS = "cpu_nanos"
const val RESIDENT_BYTES = "resident_bytes"
const val STAGE = 11
const val STAGE_KEY = "stage"
const val ENGINE_KEY = "engine"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import com.shifthackz.aisdv1.presentation.app.bootstrap.NativeInferenceEngineFac
/**
* Framework-service composition supplied by the application root without host services.
*
* @param performanceReader On-demand resource reader restricted to the worker itself.
* @param engines Child-only runtime factory.
* @param files Private request/result exchange.
* @author Dmitriy Moroz
*/
internal class InferenceServiceDependencies(
val engines: NativeInferenceEngineFactory,
val files: GenerationFileStore
val files: GenerationFileStore,
val performanceReader: CurrentProcessPerformanceReader
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.shifthackz.aisdv1.app.inference

import android.util.Base64
import com.shifthackz.aisdv1.app.diagnostics.GenerationAttemptJournal
import com.shifthackz.aisdv1.domain.entity.ProcessPerformanceReading
import com.shifthackz.aisdv1.domain.entity.LocalDiffusionStatus
import com.shifthackz.aisdv1.domain.entity.NativeGenerationEngine
import com.shifthackz.aisdv1.domain.entity.NativeGenerationStage
Expand Down Expand Up @@ -45,6 +46,14 @@ internal class NativeGenerationGatewayImpl(
) : NativeGenerationGateway {
private val admission = Mutex()
private val active = AtomicReference<OwnedInferenceConnection?>()

/**
* Requests counters through the owned connection without starting or binding a service.
* @author Dmitriy Moroz
*/
override suspend fun readProcessPerformance(): ProcessPerformanceReading? =
active.get()?.connection?.readPerformance()

private val initialized = CompletableDeferred<Unit>()
private val progress = NativeGenerationEngine.entries.associateWith {
MutableStateFlow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.os.Messenger
import android.os.Process
import com.shifthackz.aisdv1.app.AiStableDiffusionClientApp
import com.shifthackz.aisdv1.domain.feature.diffusion.NativeInferenceEngine
import com.shifthackz.aisdv1.domain.entity.ProcessPerformanceReading
import com.shifthackz.aisdv1.domain.entity.NativeGenerationStage
import com.shifthackz.aisdv1.domain.entity.NativeGenerationEngine
import java.util.UUID
Expand Down Expand Up @@ -151,6 +152,21 @@ internal class NativeGenerationService : Service() {
}
}

InferenceProtocol.READ_PERFORMANCE -> if (job == activeJob) {
val resourceRequestId = message.data.getLong(InferenceProtocol.RESOURCE_REQUEST_ID)
scope.launch {
val reading = withContext(Dispatchers.IO) {
runCatching { dependencies.performanceReader.read() }
}
send(
InferenceProtocol.PERFORMANCE,
job,
performance = reading.getOrNull(),
resourceRequestId = resourceRequestId
)
}
}

InferenceProtocol.PING -> send(InferenceProtocol.PONG)
InferenceProtocol.EXIT -> Process.killProcess(Process.myPid())
}
Expand All @@ -162,10 +178,12 @@ internal class NativeGenerationService : Service() {
job: String = "",
current: Int = 0,
total: Int = 0,
stage: NativeGenerationStage? = null
stage: NativeGenerationStage? = null,
performance: ProcessPerformanceReading? = null,
resourceRequestId: Long = 0L,
) {
if (Looper.myLooper() != Looper.getMainLooper()) {
control.post { send(what, job, current, total, stage) }
control.post { send(what, job, current, total, stage, performance, resourceRequestId) }
return
}
if (what == InferenceProtocol.STAGE) {
Expand All @@ -174,6 +192,11 @@ internal class NativeGenerationService : Service() {
}
val reply = host ?: return
val message = InferenceProtocol.message(what, incarnation, job, sequence.incrementAndGet())
message.data.putLong(InferenceProtocol.RESOURCE_REQUEST_ID, resourceRequestId)
performance?.let {
message.data.putLong(InferenceProtocol.CPU_NANOS, it.cpuTimeNanos)
message.data.putLong(InferenceProtocol.RESIDENT_BYTES, it.residentBytes)
}
message.data.putInt(InferenceProtocol.PID, Process.myPid())
message.data.putString(InferenceProtocol.ENGINE_KEY, engineKind?.name)
message.data.putString(InferenceProtocol.STAGE_KEY, stage?.name)
Expand Down
Loading
Loading