Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Never open the disk of a running machine to measure it

Jackye31942a91e18
diff --git a/Research/Host/machine_templates.md b/Research/Host/machine_templates.md
index 06ca1f42..b45a9052 100644
--- a/Research/Host/machine_templates.md
+++ b/Research/Host/machine_templates.md
@@ -174,12 +174,23 @@ every 30 s, with a 15 s mapping budget after which the rest is mapped on a
later pass (those folders show no exclusive size meanwhile).
Mapping opens the file (`O_EVTONLY`), and while it is open `lsof` lists
-Launchpad for the image. `cfw install` refuses a disk someone holds, so
-Launchpad never opens a folder it is working on (a creation, a template build,
-an export, an install), keeps a stopped machine's last ranges until its files
-change, and leaves its own process out when it asks `lsof` which machines run.
-A `vphone-cli` run outside Launchpad in the ~0.1 s a changed image is being
-mapped can still see it held.
+Launchpad for the image. `cfw install` refuses a disk someone holds, and
+until the change below `vm stop` signalled every holder (SIGINT, and SIGKILL
+to whoever was left at its timeout). The retest of PR #633 (N2) found
+Launchpad mapping running machines' images about every 30 s, so a forced
+`vm stop` could kill Launchpad. Launchpad therefore opens a machine's files only when
+`VPhoneLaunchpadDiskAccess` allows it: no `vm launch` of its own still runs
+for the machine (starting, running, stopping), the last `lsof` names no other
+process for its disk, and Launchpad has no operation on it (a creation, a
+template build, an export, an install or update, a shutdown or stop) or on a
+whole library (an import, a template deletion). It asks again right before
+each file is mapped, so a machine started during a pass is not opened. Any
+other machine keeps the ranges last mapped, and a stopped one is opened only
+when a file's size, mtime or ctime changed since then: once after each run.
+Launchpad leaves its own process out when it asks `lsof` which machines run.
+A `vphone-cli` started outside Launchpad, or a root process `lsof` does not
+show to the user, in the ~0.1 s a changed stopped image is being mapped can
+still see it held.
What a holder is decides what the CLI does with it
(`VPhoneProcessHolder`, by `proc_pidpath`). Only `vphone-vm` (any copy of the
diff --git a/VPhoneLaunchpad/Tests/TemplatesTests.swift b/VPhoneLaunchpad/Tests/TemplatesTests.swift
index 4fab1a06..426246a5 100644
--- a/VPhoneLaunchpad/Tests/TemplatesTests.swift
+++ b/VPhoneLaunchpad/Tests/TemplatesTests.swift
@@ -463,10 +463,50 @@ struct TemplatesTests {
partial.summary(locale: english))
precondition(VPhoneLaunchpadDiskUsage(allocated: 1_000_000_000, exclusive: nil).summary(locale: english) == "1 GB", "Without an exclusive size")
extentArithmetic()
+ diskAccess()
try await sharedExtents()
print("Disk use tests passed")
}
+ /// When the meter may open a machine's files (retest N2: `vm stop`
+ /// signals every process `lsof` lists for a disk, Launchpad included).
+ static func diskAccess() {
+ typealias Access = VPhoneLaunchpadDiskAccess
+ precondition(Access().mayOpen, "A stopped machine nothing holds or works on")
+ precondition(!Access(isLaunched: true).mayOpen, "Started, running or stopping by Launchpad")
+ precondition(!Access(isHeld: true).mayOpen, "Started elsewhere, or held by anyone else")
+ precondition(!Access(isBusy: true).mayOpen, "Created, exported, installed into, stopped")
+ precondition(!Access(isLibraryBusy: true).mayOpen, "An import or template deletion")
+ precondition(!Access(isLaunched: true, isHeld: true).mayOpen, "Running")
+
+ typealias Meter = VPhoneLaunchpadDiskMeter
+ // An unchanged file is never opened, whatever else holds.
+ for mayOpen in [true, false] {
+ for hasTime in [true, false] {
+ precondition(Meter.use(isCurrent: true, hasLast: true, mayOpen: mayOpen, hasTime: hasTime) == .current,
+ "Current: \(mayOpen) \(hasTime)")
+ }
+ }
+ // A changed file is opened only when its folder may be opened and
+ // the pass has time left.
+ precondition(Meter.use(isCurrent: false, hasLast: true, mayOpen: true, hasTime: true) == .map, "Changed")
+ precondition(Meter.use(isCurrent: false, hasLast: false, mayOpen: true, hasTime: true) == .map, "Never mapped")
+ precondition(Meter.use(isCurrent: false, hasLast: true, mayOpen: false, hasTime: true) == .last, "Running: last extents")
+ precondition(Meter.use(isCurrent: false, hasLast: false, mayOpen: false, hasTime: true) == .unknown, "Running, never mapped")
+ precondition(Meter.use(isCurrent: false, hasLast: true, mayOpen: true, hasTime: false) == .unknown, "Out of time")
+ precondition(Meter.use(isCurrent: false, hasLast: false, mayOpen: false, hasTime: false) == .unknown, "Nothing")
+ }
+
+ /// The folders `mayOpenNow` was asked about.
+ actor AskedFolders {
+ private(set) var folders: [String] = []
+
+ func ask(_ folder: String, answer: Bool) -> Bool {
+ folders.append(folder)
+ return answer
+ }
+ }
+
/// The sweep on made-up extents.
static func extentArithmetic() {
typealias Extents = VPhoneLaunchpadDiskExtents
@@ -569,6 +609,23 @@ struct TemplatesTests {
precondition(open[third.path]?.exclusive == 0, "Third: \(String(describing: open[third.path]))")
precondition(open[template.path]?.exclusive == 0, "Template with a clone that wrote nothing: \(String(describing: open[template.path]))")
+ // Asked right before mapping: only a changed file's folder is asked,
+ // and a no keeps the extents last mapped. Unchanged files ask
+ // nothing.
+ let everything: [VPhoneLaunchpadDiskMeter.Folder] = [.init(path: template.path), .init(path: first.path), .init(path: second.path), .init(path: third.path)]
+ let asked = AskedFolders()
+ let unchanged = await meter.measure(everything) { folder in await asked.ask(folder, answer: true) }
+ let askedUnchanged = await asked.folders
+ precondition(askedUnchanged.isEmpty, "Nothing changed, nothing asked: \(askedUnchanged)")
+ precondition(unchanged[first.path]?.exclusive == 7 << 20, "Unchanged: \(String(describing: unchanged[first.path]))")
+ try write(first.appendingPathComponent("Disk.img"), at: 224 << 20, count: 1 << 20, byte: 0x77)
+ let refused = await meter.measure(everything) { folder in await asked.ask(folder, answer: false) }
+ let askedChanged = await asked.folders
+ precondition(askedChanged == [first.path], "Only the changed folder is asked: \(askedChanged)")
+ precondition(refused[first.path]?.exclusive == 7 << 20, "Refused at the last moment: \(String(describing: refused[first.path]))")
+ let allowed = await meter.measure(everything) { folder in await asked.ask(folder, answer: true) }
+ precondition(allowed[first.path]?.exclusive == 8 << 20, "Mapped once allowed: \(String(describing: allowed[first.path]))")
+
// Links are not followed.
try fm.createSymbolicLink(at: second.appendingPathComponent("link.img"), withDestinationURL: disk)
let linked = await meter.measure([.init(path: second.path)])
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadDiskUsage.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadDiskUsage.swift
index c683cdfd..81c9e4bf 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadDiskUsage.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadDiskUsage.swift
@@ -37,6 +37,37 @@ nonisolated struct VPhoneLaunchpadDiskUsage: Hashable, Sendable {
}
}
+// MARK: - Opening
+
+/// Whether the meter may open a machine's files, from what Launchpad knows
+/// of the machine.
+///
+/// Mapping opens the file, and while it is open `lsof` lists Launchpad for
+/// it. `vm stop` signals every process `lsof` lists for a disk (SIGINT, then
+/// SIGKILL to whoever is left at its timeout), and `cfw install` and
+/// `update-environment` refuse a disk anyone else holds. So the meter opens
+/// only the files of a machine that nothing runs, nothing holds and nothing
+/// works on; any other machine keeps the extents last mapped.
+nonisolated struct VPhoneLaunchpadDiskAccess: Hashable, Sendable {
+ /// Launchpad's `vm launch` for the machine has not exited: starting,
+ /// running, stopping, or panicked and not yet ended.
+ var isLaunched = false
+ /// `lsof` listed a process other than Launchpad for its disk image: a
+ /// machine started elsewhere, or anything else holding it.
+ var isHeld = false
+ /// Launchpad has an operation on the machine: a creation or the template
+ /// build in it, an export (queued or running), a CFW install, an
+ /// environment or kernel update, a shutdown, a stop, a rename.
+ var isBusy = false
+ /// Launchpad has an operation on a whole library (an import, a template
+ /// deletion), which can write any folder in it.
+ var isLibraryBusy = false
+
+ var mayOpen: Bool {
+ !isLaunched && !isHeld && !isBusy && !isLibraryBusy
+ }
+}
+
// MARK: - Measuring
/// Measures machine and template folders against each other, keeping each
@@ -46,17 +77,45 @@ nonisolated struct VPhoneLaunchpadDiskUsage: Hashable, Sendable {
/// is different, so a stopped machine's 20 GB image is opened once after it
/// changes, not on every pass. Mapping opens the file: `lsof` then lists
/// Launchpad for it, which `vm stop` and `cfw install` read as the machine
-/// running. Folders Launchpad is working on are therefore never opened
-/// (`Folder.mayOpen`), and Launchpad leaves its own process out when it asks
-/// `lsof` which machines run.
+/// running. A folder is opened only while `VPhoneLaunchpadDiskAccess` allows
+/// it (`Folder.mayOpen`, asked again right before each file is mapped), and
+/// Launchpad leaves its own process out when it asks `lsof` which machines
+/// run.
actor VPhoneLaunchpadDiskMeter {
nonisolated struct Folder: Hashable, Sendable {
var path: String
- /// False while something works on the folder: its files' last
- /// extents are used, and a file never mapped leaves it unknown.
+ /// False while something runs, holds or works on the folder: its
+ /// files' last extents are used, and a file never mapped leaves it
+ /// unknown.
var mayOpen = true
}
+ /// What a pass does with one file.
+ nonisolated enum FileUse: Equatable, Sendable {
+ /// The extents mapped last are current: nothing is opened.
+ case current
+ /// The file changed (or was never mapped) and may be opened.
+ case map
+ /// The file may not be opened: the extents mapped last stand in.
+ case last
+ /// Nothing to go on: the folder's exclusive size is unknown.
+ case unknown
+ }
+
+ /// The decision for one file. Only a changed file is ever opened, and
+ /// only when its folder may be opened and the pass has time left.
+ nonisolated static func use(isCurrent: Bool, hasLast: Bool, mayOpen: Bool, hasTime: Bool) -> FileUse {
+ if isCurrent {
+ return .current
+ }
+ guard mayOpen else {
+ return hasLast ? .last : .unknown
+ }
+ // A changed file the budget leaves out is unknown, not stale, so a
+ // folder that is still being written does not show a wrong size.
+ return hasTime ? .map : .unknown
+ }
+
private nonisolated struct FileKey: Hashable {
var device: Int64
var inode: UInt64
@@ -85,13 +144,23 @@ actor VPhoneLaunchpadDiskMeter {
/// Each folder's usage, by `Folder.path`. Files are not followed through
/// links.
- func measure(_ folders: [Folder]) -> [String: VPhoneLaunchpadDiskUsage] {
+ ///
+ /// `mayOpenNow` is asked with the folder's path right before one of its
+ /// files is mapped, so a machine started, held or worked on since the
+ /// pass began is not opened. The meter has one caller at a time; a
+ /// second pass running while this one waits on `mayOpenNow` would only
+ /// leave the cache less complete.
+ func measure(
+ _ folders: [Folder],
+ mayOpenNow: (@Sendable (String) async -> Bool)? = nil,
+ ) async -> [String: VPhoneLaunchpadDiskUsage] {
let deadline = ContinuousClock.now + budget
var seen: Set<String> = []
var owners: [[VPhoneLaunchpadDiskExtents]] = []
var usages: [VPhoneLaunchpadDiskUsage] = []
var unmappedBytes: [Int64] = []
for folder in folders {
+ var mayOpen = folder.mayOpen
var usage = VPhoneLaunchpadDiskUsage(allocated: 0, exclusive: 0)
var extents: [VPhoneLaunchpadDiskExtents] = []
var unmapped: Int64 = 0
@@ -111,23 +180,37 @@ actor VPhoneLaunchpadDiskMeter {
modified: status.st_mtimespec.tv_sec, modifiedNanoseconds: status.st_mtimespec.tv_nsec,
changed: status.st_ctimespec.tv_sec, changedNanoseconds: status.st_ctimespec.tv_nsec,
)
- var mapped = cache[file].flatMap { $0.key == key ? $0 : nil }
- if mapped == nil, folder.mayOpen, ContinuousClock.now < deadline {
+ let last = cache[file]
+ func decide() -> FileUse {
+ Self.use(isCurrent: last?.key == key, hasLast: last != nil, mayOpen: mayOpen, hasTime: ContinuousClock.now < deadline)
+ }
+ var decision = decide()
+ if decision == .map, let mayOpenNow, await !mayOpenNow(folder.path) {
+ mayOpen = false
+ decision = decide()
+ }
+ var found: Mapped?
+ switch decision {
+ case .current, .last:
+ // A folder that may not be opened keeps what was last
+ // mapped, which is close: its blocks rarely move.
+ found = last
+ case .map:
switch VPhoneLaunchpadDiskExtents.map(file, deadline: deadline) {
case let .success(result):
- mapped = Mapped(key: key, extents: result)
+ found = Mapped(key: key, extents: result)
case .failure(.unsupported):
- mapped = Mapped(key: key, extents: nil)
+ found = Mapped(key: key, extents: nil)
case .failure(.tooLarge):
break
}
- if let mapped {
- cache[file] = mapped
+ if let found {
+ cache[file] = found
}
+ case .unknown:
+ break
}
- // A folder that may not be opened keeps what was last
- // mapped, which is close: its blocks rarely move.
- guard let found = mapped ?? (folder.mayOpen ? nil : cache[file]) else {
+ guard let found else {
usage.exclusive = nil
continue
}
diff --git a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachineLibrary.swift b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachineLibrary.swift
index 2cf708eb..654d8975 100644
--- a/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachineLibrary.swift
+++ b/VPhoneLaunchpad/VPhoneLaunchpad/Machines/VPhoneLaunchpadMachineLibrary.swift
@@ -769,16 +769,24 @@ final class VPhoneLaunchpadMachineLibrary {
return
}
isMeasuringDiskUsage = true
- // A machine being created, exported or installed into is not opened:
- // `lsof` would show Launchpad holding its disk to `cfw install`.
let machineFolders = machines.map { machine in
- let isBusy = if case .busy = state(of: machine.path) { true } else { false }
- return VPhoneLaunchpadDiskMeter.Folder(path: machine.path.url.path, mayOpen: !isBusy)
+ VPhoneLaunchpadDiskMeter.Folder(path: machine.path.url.path, mayOpen: diskAccess(of: machine.path).mayOpen)
}
let libraryRoots = roots.filter { $0 == libraryRoot || VPhoneLaunchpadMachineLocations.isAvailable($0) }
let meter = diskMeter
+ let templatesMayOpen = globalActivity == nil
+ let mayOpenNow: @Sendable (String) async -> Bool = { [weak self] folder in
+ await self?.mayOpenDisk(inFolder: folder) ?? false
+ }
Task {
- let measured = await Self.measure(machineFolders, paths: paths, libraryRoots: libraryRoots, meter: meter)
+ let measured = await Self.measure(
+ machineFolders,
+ paths: paths,
+ libraryRoots: libraryRoots,
+ templatesMayOpen: templatesMayOpen,
+ meter: meter,
+ mayOpenNow: mayOpenNow,
+ )
diskUsage = measured.usage
templateSources = measured.sources
templateUsage = measured.templates
@@ -791,12 +799,40 @@ final class VPhoneLaunchpadMachineLibrary {
}
}
+ /// Whether the disk meter may open this machine's files now: only when
+ /// no `vm launch` of Launchpad's for it is still running (a start, a
+ /// run, a stop), the last `lsof` named no other process for its disk,
+ /// and Launchpad has no operation on it or on a whole library.
+ private func diskAccess(of machine: Path) -> VPhoneLaunchpadDiskAccess {
+ let isBusy = if case .busy = state(of: machine) { true } else { false }
+ return VPhoneLaunchpadDiskAccess(
+ isLaunched: launched[machine] != nil,
+ isHeld: externallyRunning.contains(machine),
+ isBusy: isBusy || creation(for: machine)?.isRunning == true || exports[machine] != nil,
+ isLibraryBusy: globalActivity != nil,
+ )
+ }
+
+ /// `diskAccess` for a measured folder, asked right before the meter
+ /// opens one of its files. A template folder may be opened unless a
+ /// library-wide operation (a template deletion) runs; a folder that is
+ /// neither a listed machine nor a template is not opened.
+ private func mayOpenDisk(inFolder folder: String) -> Bool {
+ if let machine = machines.first(where: { $0.path.url.path == folder }) {
+ return diskAccess(of: machine.path).mayOpen
+ }
+ let isTemplate = URL(fileURLWithPath: folder).deletingLastPathComponent().lastPathComponent == ".templates"
+ return isTemplate && globalActivity == nil
+ }
+
@concurrent
private nonisolated static func measure(
_ machineFolders: [VPhoneLaunchpadDiskMeter.Folder],
paths: [Path],
libraryRoots: [String],
+ templatesMayOpen: Bool,
meter: VPhoneLaunchpadDiskMeter,
+ mayOpenNow: @escaping @Sendable (String) async -> Bool,
) async -> (usage: [Path: VPhoneLaunchpadDiskUsage], sources: [Path: String], templates: [String: VPhoneLaunchpadDiskUsage]) {
// Templates are keyed as `usage(of:)` looks them up.
var templateKeys: [String: String] = [:]
@@ -805,8 +841,8 @@ final class VPhoneLaunchpadMachineLibrary {
templateKeys[folder] = URL(fileURLWithPath: folder).lastPathComponent + "@" + root
}
}
- let folders = machineFolders + templateKeys.keys.sorted().map { VPhoneLaunchpadDiskMeter.Folder(path: $0) }
- let measured = await meter.measure(folders)
+ let folders = machineFolders + templateKeys.keys.sorted().map { VPhoneLaunchpadDiskMeter.Folder(path: $0, mayOpen: templatesMayOpen) }
+ let measured = await meter.measure(folders, mayOpenNow: mayOpenNow)
var usage: [Path: VPhoneLaunchpadDiskUsage] = [:]
var sources: [Path: String] = [:]
for path in paths {