Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][photos] Add ML run coordination primitives

laurensprieme38953dcaa43
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_process_lock.dart b/mobile/apps/photos/lib/services/machine_learning/ml_process_lock.dart
new file mode 100644
index 0000000000..7335fe1972
--- /dev/null
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_process_lock.dart
@@ -0,0 +1,115 @@
+import "dart:async";
+
+import "package:ente_photos_platform/ente_photos_platform.dart";
+import "package:logging/logging.dart";
+import "package:synchronized/synchronized.dart";
+
+enum MlOperation { fullRun, indexing, clustering, startupRemoteHydration }
+
+enum MlLockAttempt {
+ ran,
+ deniedFunnelBusy,
+ deniedByOtherEngine,
+ deniedChannelError,
+}
+
+enum MlRunDisposition { completed, denied, stopped, failed }
+
+/// Per-engine funnel plus the process-global `ml` lock.
+///
+/// All top-level ML operations must run through [tryRunExclusive]: the funnel
+/// serializes operations within this engine, and the native lock excludes the
+/// other engine. Single attempt, no waiting β€” callers defer to their normal
+/// triggers on denial.
+class MlProcessLock {
+ MlProcessLock._();
+ static final instance = MlProcessLock._();
+
+ static const _lockName = "ml";
+ final _logger = Logger("MlProcessLock");
+ final Lock _funnel = Lock();
+ MlOperation? _activeOperation;
+
+ /// Whether an operation currently holds the funnel. For asserts and
+ /// diagnostics only β€” never use this to skip [tryRunExclusive].
+ bool get isBusy => _activeOperation != null;
+
+ /// The operation currently holding the funnel, if any. For derived status
+ /// and diagnostics only.
+ MlOperation? get activeOperation => _activeOperation;
+
+ /// Runs [body] while holding the funnel and the native `ml` lock. The lock
+ /// is released only after [body] completes, so [body] must drain any work
+ /// it started before returning. Errors from [body] propagate after release.
+ /// [background] labels this engine's origin for diagnostics.
+ Future<MlLockAttempt> tryRunExclusive(
+ MlOperation operation,
+ Future<void> Function() body, {
+ required bool background,
+ }) async {
+ if (_funnel.locked) {
+ _logger.info(
+ "Funnel busy with ${_activeOperation?.name}, denying ${operation.name}",
+ );
+ return MlLockAttempt.deniedFunnelBusy;
+ }
+ return _funnel.synchronized(() async {
+ _activeOperation = operation;
+ try {
+ final origin = background ? "bg" : "fg";
+ final bool acquired;
+ try {
+ acquired = await ProcessLockClient.instance.tryAcquire(
+ name: _lockName,
+ origin: origin,
+ operation: operation.name,
+ );
+ } catch (e, s) {
+ // Fail closed: no protected ML without the lock.
+ _logger.severe(
+ "Process lock channel failure for ${operation.name}",
+ e,
+ s,
+ );
+ return MlLockAttempt.deniedChannelError;
+ }
+ if (!acquired) {
+ unawaited(_logOwner(operation));
+ return MlLockAttempt.deniedByOtherEngine;
+ }
+ _logger.info("Acquired ml lock ($origin/${operation.name})");
+ final heldSince = DateTime.now();
+ try {
+ await body();
+ return MlLockAttempt.ran;
+ } finally {
+ try {
+ await ProcessLockClient.instance.release(name: _lockName);
+ final heldFor = DateTime.now().difference(heldSince);
+ _logger.info(
+ "Released ml lock ($origin/${operation.name}) after "
+ "${heldFor.inSeconds}s",
+ );
+ } catch (e, s) {
+ _logger.severe(
+ "Failed to release ml lock after ${operation.name}",
+ e,
+ s,
+ );
+ }
+ }
+ } finally {
+ _activeOperation = null;
+ }
+ });
+ }
+
+ Future<void> _logOwner(MlOperation denied) async {
+ try {
+ final owner = await ProcessLockClient.instance.state(name: _lockName);
+ _logger.info("ml lock denied for ${denied.name}, current owner: $owner");
+ } catch (e) {
+ _logger.warning("Could not fetch ml lock state after denial: $e");
+ }
+ }
+}
diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart b/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart
new file mode 100644
index 0000000000..c82c70e2c6
--- /dev/null
+++ b/mobile/apps/photos/lib/services/machine_learning/ml_run_control.dart
@@ -0,0 +1,46 @@
+import "package:logging/logging.dart";
+
+enum MlStopReason {
+ foregroundActive,
+ backgroundDeadline,
+ controller,
+ manual,
+ corruptModel,
+}
+
+/// Durable per-run stop latch. Once stop is requested it holds for the rest
+/// of that run β€” nothing resumes; work continues only when a new run starts.
+class MlRunControl {
+ final _logger = Logger("MlRunControl");
+ bool _stopRequested = false;
+ MlStopReason? _stopReason;
+ void Function()? _onStop;
+
+ bool get stopRequested => _stopRequested;
+ MlStopReason? get stopReason => _stopReason;
+
+ /// Idempotent; the first reason wins.
+ void requestStop(MlStopReason reason) {
+ if (_stopRequested) return;
+ _stopRequested = true;
+ _stopReason = reason;
+ _logger.info("ML run stop requested (${reason.name})");
+ final onStop = _onStop;
+ _onStop = null;
+ onStop?.call();
+ }
+
+ /// One-shot notification for the active run. Fires immediately when stop
+ /// was already requested.
+ void attachOnStop(void Function() onStop) {
+ if (_stopRequested) {
+ onStop();
+ return;
+ }
+ _onStop = onStop;
+ }
+
+ void detachOnStop() {
+ _onStop = null;
+ }
+}
diff --git a/mobile/apps/photos/lib/services/process_activity.dart b/mobile/apps/photos/lib/services/process_activity.dart
new file mode 100644
index 0000000000..2fb28bb62b
--- /dev/null
+++ b/mobile/apps/photos/lib/services/process_activity.dart
@@ -0,0 +1,18 @@
+import "package:shared_preferences/shared_preferences.dart";
+
+const kLastBGTaskHeartBeatTime = "bg_task_hb_time";
+const kLastFGTaskHeartBeatTime = "fg_task_hb_time";
+const kFGTaskDeathTimeoutInMicroseconds = 5000000;
+
+/// Whether the foreground engine has written its heartbeat within the last
+/// five seconds. Reloads prefs so writes from the other engine are seen.
+///
+/// Heartbeats influence priority and yield latency only β€” never correctness.
+Future<bool> isForegroundEngineActive() async {
+ final prefs = await SharedPreferences.getInstance();
+ await prefs.reload();
+ final currentTime = DateTime.now().microsecondsSinceEpoch;
+ final lastFGHeartBeatTime = prefs.getInt(kLastFGTaskHeartBeatTime) ?? 0;
+ return lastFGHeartBeatTime >
+ (currentTime - kFGTaskDeathTimeoutInMicroseconds);
+}