|
| 1 | +/* |
| 2 | + Infomaniak kDrive - iOS App |
| 3 | + Copyright (C) 2025 Infomaniak Network SA |
| 4 | + |
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | + |
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | + |
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +import Foundation |
| 20 | +import InfomaniakDI |
| 21 | + |
| 22 | +public final class UploadParallelismOrchestrator { |
| 23 | + @LazyInjectService(customTypeIdentifier: UploadQueueID.global) private var globalUploadQueue: UploadQueueable |
| 24 | + @LazyInjectService(customTypeIdentifier: UploadQueueID.photo) private var photoUploadQueue: UploadQueueable |
| 25 | + @LazyInjectService private var appContextService: AppContextServiceable |
| 26 | + |
| 27 | + private let serialEventQueue = DispatchQueue( |
| 28 | + label: "com.infomaniak.drive.upload-parallelism-orchestrator.event", |
| 29 | + qos: .default |
| 30 | + ) |
| 31 | + |
| 32 | + private var uploadParallelismHeuristic: WorkloadParallelismHeuristic? |
| 33 | + private var memoryPressureObserver: DispatchSourceMemoryPressure? |
| 34 | + |
| 35 | + private var availableParallelism: Int { |
| 36 | + guard let uploadParallelismHeuristic else { |
| 37 | + return ParallelismDefaults.reducedParallelism |
| 38 | + } |
| 39 | + return uploadParallelismHeuristic.currentParallelism |
| 40 | + } |
| 41 | + |
| 42 | + private lazy var allQueues = [globalUploadQueue, photoUploadQueue] |
| 43 | + |
| 44 | + public init() { |
| 45 | + setupObservation() |
| 46 | + } |
| 47 | + |
| 48 | + private func setupObservation() { |
| 49 | + serialEventQueue.async { |
| 50 | + self.observeMemoryWarnings() |
| 51 | + self.uploadParallelismHeuristic = WorkloadParallelismHeuristic(delegate: self) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + func observeMemoryWarnings() { |
| 56 | + guard appContextService.context == .fileProviderExtension else { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + let source = DispatchSource.makeMemoryPressureSource(eventMask: .all, queue: .main) |
| 61 | + memoryPressureObserver = source |
| 62 | + source.setEventHandler { [weak self] in |
| 63 | + guard let self else { return } |
| 64 | + let event: DispatchSource.MemoryPressureEvent = source.data |
| 65 | + switch event { |
| 66 | + case DispatchSource.MemoryPressureEvent.normal: |
| 67 | + Log.uploadQueue("MemoryPressureEvent normal", level: .info) |
| 68 | + case DispatchSource.MemoryPressureEvent.warning: |
| 69 | + Log.uploadQueue("MemoryPressureEvent warning", level: .info) |
| 70 | + case DispatchSource.MemoryPressureEvent.critical: |
| 71 | + Log.uploadQueue("MemoryPressureEvent critical", level: .error) |
| 72 | + serialEventQueue.async { |
| 73 | + @InjectService var uploadService: UploadServiceable |
| 74 | + uploadService.rescheduleRunningOperations() |
| 75 | + } |
| 76 | + default: |
| 77 | + break |
| 78 | + } |
| 79 | + } |
| 80 | + source.resume() |
| 81 | + } |
| 82 | + |
| 83 | + private func computeUploadParallelismPerQueueAndApply() { |
| 84 | + serialEventQueue.async { |
| 85 | + let currentAvailableParallelism = self.availableParallelism |
| 86 | + Log.uploadQueue("Current total available upload parallelism :\(currentAvailableParallelism)") |
| 87 | + |
| 88 | + let activeQueues = self.allQueues.filter(\.isActive) |
| 89 | + let inactiveQueues = self.allQueues.filter { !$0.isActive } |
| 90 | + |
| 91 | + assert(activeQueues.count + inactiveQueues.count == self.allQueues.count, "queue count should match") |
| 92 | + |
| 93 | + inactiveQueues.forEach { $0.parallelismShouldChange(value: ParallelismDefaults.serial) } |
| 94 | + |
| 95 | + Log.uploadQueue("Inactive queues:\(inactiveQueues.count) set to serial") |
| 96 | + guard !activeQueues.isEmpty else { |
| 97 | + Log.uploadQueue("No active queues") |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + let parallelismPerActiveQueue = max(ParallelismDefaults.serial, currentAvailableParallelism / activeQueues.count) |
| 102 | + Log.uploadQueue("Active queues \(activeQueues.count) new parallelism:\(parallelismPerActiveQueue)") |
| 103 | + activeQueues.forEach { $0.parallelismShouldChange(value: parallelismPerActiveQueue) } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +extension UploadParallelismOrchestrator: UploadQueueDelegate { |
| 109 | + public func operationQueueBecameEmpty(_ queue: UploadQueue) { |
| 110 | + computeUploadParallelismPerQueueAndApply() |
| 111 | + } |
| 112 | + |
| 113 | + public func operationQueueNoLongerEmpty(_ queue: UploadQueue) { |
| 114 | + computeUploadParallelismPerQueueAndApply() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +extension UploadParallelismOrchestrator: ParallelismHeuristicDelegate { |
| 119 | + public func parallelismShouldChange(value: Int) { |
| 120 | + computeUploadParallelismPerQueueAndApply() |
| 121 | + } |
| 122 | +} |
0 commit comments