Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatCore/Sources/Device.swift3// Description: Measured device profile (chip, memory, storage, thermal state, memory pressure) and the compatibility rules built on it.4import Foundation5#if canImport(Darwin)6import Darwin7#endif8#if canImport(os)9import os10#endif1112public struct DeviceProfile: Sendable, Hashable, Codable {13 public enum Platform: String, Sendable, Codable { case mac, iPhone, iPad, simulator }14 public enum Thermal: String, Sendable, Codable { case nominal, fair, serious, critical }1516 public var platform: Platform17 public var machine: String // e.g. "Mac16,5", "iPhone18,2"18 public var chip: String? // e.g. "Apple M5 Max" (macOS); nil when the OS does not expose it19 public var performanceCores: Int?20 public var efficiencyCores: Int?21 public var physicalMemoryBytes: Int6422 public var availableMemoryBytes: Int64? // what this process may still allocate (iOS) or free+inactive pages (macOS)23 public var storageFreeBytes: Int64?24 public var storageTotalBytes: Int64?25 public var thermal: Thermal26 public var lowPowerMode: Bool27 public var osVersion: String2829 public var isSimulator: Bool { platform == .simulator }30 public var memoryGB: Double { Double(physicalMemoryBytes) / 1_073_741_824 }3132 /// The share of RAM a model may take. macOS shares unified memory with every app; iOS kills a process well below physical RAM.33 public var usableModelMemoryBytes: Int64 {34 switch platform {35 case .mac: return Int64(Double(physicalMemoryBytes) * (physicalMemoryBytes >= 32 * 1_073_741_824 ? 0.75 : 0.65))36 case .iPhone, .iPad, .simulator:37 // iOS jetsam limits are roughly half of RAM for a foreground app (more with the increased-memory-limit entitlement)38 let budget = Int64(Double(physicalMemoryBytes) * 0.55)39 if let available = availableMemoryBytes, available > 0 { return min(budget, available) }40 return budget41 }42 }4344 public static func current() -> DeviceProfile {45 let info = ProcessInfo.processInfo46 #if targetEnvironment(simulator)47 let platform: Platform = .simulator48 #elseif os(macOS)49 let platform: Platform = .mac50 #else51 let platform: Platform = sysctlString("hw.machine")?.hasPrefix("iPad") == true ? .iPad : .iPhone52 #endif53 #if os(macOS)54 let machine = sysctlString("hw.model") ?? "Mac"55 #else56 let machine = sysctlString("hw.machine") ?? "Apple device"57 #endif58 var free: Int64?, total: Int64?59 if let values = try? URL(fileURLWithPath: NSHomeDirectory()).resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey, .volumeTotalCapacityKey]) {60 free = values.volumeAvailableCapacityForImportantUsage; total = values.volumeTotalCapacity.map(Int64.init)61 }62 let thermal: Thermal = switch info.thermalState { case .nominal: .nominal; case .fair: .fair; case .serious: .serious; case .critical: .critical; @unknown default: .nominal }63 let v = info.operatingSystemVersion64 return DeviceProfile(platform: platform, machine: machine, chip: sysctlString("machdep.cpu.brand_string"),65 performanceCores: sysctlInt("hw.perflevel0.physicalcpu"), efficiencyCores: sysctlInt("hw.perflevel1.physicalcpu"),66 physicalMemoryBytes: Int64(info.physicalMemory), availableMemoryBytes: availableMemory(),67 storageFreeBytes: free, storageTotalBytes: total, thermal: thermal, lowPowerMode: info.isLowPowerModeEnabled,68 osVersion: "\(v.majorVersion).\(v.minorVersion)\(v.patchVersion > 0 ? ".\(v.patchVersion)" : "")")69 }7071 static func sysctlString(_ name: String) -> String? {72 var size = 073 guard sysctlbyname(name, nil, &size, nil, 0) == 0, size > 0 else { return nil }74 var buffer = [CChar](repeating: 0, count: size)75 guard sysctlbyname(name, &buffer, &size, nil, 0) == 0 else { return nil }76 let s = String(decoding: buffer.prefix(while: { $0 != 0 }).map { UInt8(bitPattern: $0) }, as: UTF8.self)77 return s.isEmpty ? nil : s78 }79 public static func sysctlIntPublic(_ name: String) -> Int? { sysctlInt(name) }80 static func sysctlInt(_ name: String) -> Int? {81 var value: Int32 = 0; var size = MemoryLayout<Int32>.size82 return sysctlbyname(name, &value, &size, nil, 0) == 0 ? Int(value) : nil83 }8485 static func availableMemory() -> Int64? {86 #if os(iOS) && !targetEnvironment(simulator)87 return Int64(os_proc_available_memory())88 #else89 var stats = vm_statistics64(); var count = mach_msg_type_number_t(MemoryLayout<vm_statistics64>.size / MemoryLayout<integer_t>.size)90 let result = withUnsafeMutablePointer(to: &stats) { ptr in91 ptr.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { host_statistics64(mach_host_self(), HOST_VM_INFO64, $0, &count) }92 }93 guard result == KERN_SUCCESS else { return nil }94 var pageSize: vm_size_t = 0; host_page_size(mach_host_self(), &pageSize)95 return Int64(stats.free_count + stats.inactive_count + stats.purgeable_count) * Int64(pageSize)96 #endif97 }9899 /// Resident memory of this process (phys_footprint, the figure Activity Monitor and jetsam use).100 public static func processFootprintBytes() -> Int64? {101 var info = task_vm_info_data_t(); var count = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<natural_t>.size)102 let result = withUnsafeMutablePointer(to: &info) { ptr in103 ptr.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count) }104 }105 return result == KERN_SUCCESS ? Int64(info.phys_footprint) : nil106 }107108 public func compatibility(of model: ModelDescriptor, backendAvailable: (BackendKind) -> String?, context: Int = 4096) -> CompatibilityVerdict {109 if model.backend == .remote { return CompatibilityVerdict(fit: .comfortable, reasons: ["Runs on a remote node; nothing is loaded on this device."]) }110 if let reason = backendAvailable(model.backend) { return CompatibilityVerdict(fit: .unsupported, reasons: [reason]) }111 if model.backend == .appleFoundation { return CompatibilityVerdict(fit: .comfortable, reasons: ["System model managed by the OS; Local Chat loads nothing itself."]) }112 guard let need = model.estimatedMemoryBytes(context: context) else {113 return CompatibilityVerdict(fit: .unknown, reasons: ["The size of this model is unknown; memory needs cannot be estimated."])114 }115 let usable = usableModelMemoryBytes116 var reasons: [String] = []117 if let free = storageFreeBytes, !model.isLocal, let size = model.sizeBytes, size > free - 2 * 1_073_741_824 {118 reasons.append("Not enough free storage: needs \(ByteFormat.string(size)), \(ByteFormat.string(free)) free.")119 return CompatibilityVerdict(fit: .tooLarge, reasons: reasons, estimatedMemoryBytes: need)120 }121 if need > usable {122 reasons.append("Needs about \(ByteFormat.memory(need)); this device can give a model about \(ByteFormat.memory(usable)).")123 return CompatibilityVerdict(fit: .tooLarge, reasons: reasons, estimatedMemoryBytes: need)124 }125 if Double(need) > Double(usable) * 0.7 {126 reasons.append("Uses most of the memory available to models (\(ByteFormat.memory(need)) of \(ByteFormat.memory(usable))). Close other apps first.")127 return CompatibilityVerdict(fit: .tight, reasons: reasons, estimatedMemoryBytes: need)128 }129 if thermal == .serious || thermal == .critical { reasons.append("The device is hot; generation will be slower.") }130 if lowPowerMode { reasons.append("Low Power Mode is on; generation will be slower.") }131 return CompatibilityVerdict(fit: .comfortable, reasons: reasons, estimatedMemoryBytes: need)132 }133}134