Skip to content

Commit

Permalink
Rework to use main queue and optimization of vm watcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Nov 28, 2020
1 parent 54e2750 commit 811ab8e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 48 deletions.
17 changes: 13 additions & 4 deletions virtual.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
2A2221922571779B00701E83 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2221912571779B00701E83 /* Constants.swift */; };
2A2221952571786A00701E83 /* VirtualCommandRun.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2221942571786A00701E83 /* VirtualCommandRun.swift */; };
2A2221992571792B00701E83 /* VirtualVersionOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A2221982571792B00701E83 /* VirtualVersionOptions.swift */; };
2ABF25AC24CF7B07001BEDC2 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ABF25AB24CF7B07001BEDC2 /* main.swift */; };
Expand All @@ -18,6 +17,16 @@
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
2A2221B22572CD1200701E83 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
2ABF25A624CF7B07001BEDC2 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
Expand All @@ -30,7 +39,6 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
2A2221912571779B00701E83 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
2A2221942571786A00701E83 /* VirtualCommandRun.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VirtualCommandRun.swift; sourceTree = "<group>"; };
2A2221982571792B00701E83 /* VirtualVersionOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VirtualVersionOptions.swift; sourceTree = "<group>"; };
2ABF25A824CF7B07001BEDC2 /* virtual */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = virtual; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -89,7 +97,6 @@
2AC0F3C124D099C700880496 /* VirtualSystem.swift */,
2A2221982571792B00701E83 /* VirtualVersionOptions.swift */,
2AC0F3C724D2526000880496 /* Utils.swift */,
2A2221912571779B00701E83 /* Constants.swift */,
);
path = virtual;
sourceTree = "<group>";
Expand All @@ -104,6 +111,7 @@
2ABF25A424CF7B07001BEDC2 /* Sources */,
2ABF25A524CF7B07001BEDC2 /* Frameworks */,
2ABF25A624CF7B07001BEDC2 /* CopyFiles */,
2A2221B22572CD1200701E83 /* Embed Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -159,7 +167,6 @@
files = (
2A2221952571786A00701E83 /* VirtualCommandRun.swift in Sources */,
2A2221992571792B00701E83 /* VirtualVersionOptions.swift in Sources */,
2A2221922571779B00701E83 /* Constants.swift in Sources */,
2ABF25AC24CF7B07001BEDC2 /* main.swift in Sources */,
2AC0F3C224D099C700880496 /* VirtualSystem.swift in Sources */,
2AC0F3C024D099A500880496 /* VirtualCommand.swift in Sources */,
Expand Down Expand Up @@ -302,6 +309,7 @@
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "$(SRCROOT)/virtual/Info.plist";
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 0.0.2;
PRODUCT_BUNDLE_IDENTIFIER = io.endfinger.virtual;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -322,6 +330,7 @@
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "$(SRCROOT)/virtual/Info.plist";
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 0.0.2;
PRODUCT_BUNDLE_IDENTIFIER = io.endfinger.virtual;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
10 changes: 0 additions & 10 deletions virtual/Constants.swift

This file was deleted.

8 changes: 4 additions & 4 deletions virtual/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleShortVersionString</key>
<string>1</string>
<key>CFBundleName</key>
<string>virtual</string>
<key>CFBundleIdentifier</key>
<string>io.endfinger.virtual</string>
<key>CFBundleName</key>
<string>virtual</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
</dict>
</plist>
25 changes: 13 additions & 12 deletions virtual/VirtualCommandRun.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,32 @@ struct VirtualCommandRun: ParsableCommand {
var version: Bool = false

mutating func run() throws {
if version {
print("virtual version \(virtualToolVersion)")
VirtualCommand.exit()
}

enableRawMode(fileHandle: FileHandle.standardInput)

let system = VirtualSystem(command: self)
do {
try system.start()

var lastState: VZVirtualMachine.State = .stopped
while system.machine != nil {
let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.main)
timer.schedule(deadline: .now(), repeating: .milliseconds(100))

var lastMachineState: VZVirtualMachine.State = .stopped
timer.setEventHandler {
let currentState = system.machine!.state
if currentState != lastState {
if currentState != lastMachineState {
NSLog("Virtual Machine State: \(system.stateToString())")
lastState = currentState
lastMachineState = currentState
}

if currentState == .error {
VirtualCommand.exit()
VirtualCommand.exit(withError: ExitCode.failure)
} else if currentState == .stopped {
VirtualCommand.exit(withError: ExitCode.success)
}

sleep(1)
}
timer.resume()

dispatchMain()
} catch {
NSLog("\(error)")
}
Expand Down
28 changes: 11 additions & 17 deletions virtual/VirtualSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ import Virtualization

class VirtualSystem: NSObject, VZVirtualMachineDelegate {
let command: VirtualCommandRun
let queue = DispatchQueue(label: "io.endfinger.virtual.vm")

var machine: VZVirtualMachine?

init(command: VirtualCommandRun) {
self.command = command
}

func guestDidStop(_: VZVirtualMachine) {
NSLog("Guest Stop.")
NSLog("Virtual Machine Stopped")
}

func virtualMachine(_: VZVirtualMachine, didStopWithError error: Error) {
NSLog("Guest Stopped with error: \(String(describing: error))")
NSLog("Virtual Machine Stopped: \(String(describing: error))")
}

func start() throws {
Expand Down Expand Up @@ -74,18 +72,16 @@ class VirtualSystem: NSObject, VZVirtualMachineDelegate {
config.networkDevices = network

try config.validate()
let vm = VZVirtualMachine(configuration: config, queue: queue)
let vm = VZVirtualMachine(configuration: config)
vm.delegate = self
machine = vm

queue.sync {
vm.start { result in
switch result {
case .success:
NSLog("Virtual Machine Started")
case let .failure(error):
NSLog("Virtual Machine Failure: \(error)")
}
vm.start { result in
switch result {
case .success:
NSLog("Virtual Machine Started")
case let .failure(error):
NSLog("Virtual Machine Failure: \(error)")
}
}
}
Expand Down Expand Up @@ -116,10 +112,8 @@ class VirtualSystem: NSObject, VZVirtualMachineDelegate {
}

func stop() throws {
if let machine = machine {
try queue.sync {
try machine.requestStop()
}
if let vm = machine {
try vm.requestStop()
}
}
}
9 changes: 8 additions & 1 deletion virtual/VirtualVersionOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
//

import ArgumentParser
import Cocoa

struct VirtualVersionOptions: ParsableArguments {
@Flag(name: .long, help: "Show the Tool Version")
var version: Bool = false

func validate() throws {
if version {
print(virtualToolVersion)
let bundleVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String?

if let versionAsString = bundleVersion {
print("\(versionAsString)")
} else {
print("unknown")
}
throw ExitCode.success
}
}
Expand Down

0 comments on commit 811ab8e

Please sign in to comment.