Skip to content

Commit 9ab638e

Browse files
committed
Don’t oversubscribe the CPU with tasks
This dramatically increases multithreaded performance: Before: https://browser.geekbench.com/v6/cpu/3115104 After: https://browser.geekbench.com/v6/cpu/3115190
1 parent a268b19 commit 9ab638e

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

Sources/libhostmgr/Model/Configuration.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ public struct Configuration: Codable {
5252
public var allowAWSAcceleratedTransfer: Bool! = Defaults.defaultAWSAcceleratedTransferAllowed
5353
public var awsConfigurationMethod: AWSConfigurationType! = Defaults.defaultAWSConfigurationMethod
5454

55-
/// VM Memory Settings
56-
public static let hostReservedRAM: UInt64 = 1024 * 1024 * 4096 // Leave 4GB for the VM host
57-
public var useSharedMemoryCapacity: Bool = false
55+
// MARK: VM Resource Settings
56+
57+
/// Should this node run more than one concurrent VM?
58+
public let isSharedNode: Bool = false
59+
60+
/// How much RAM should be reserved for the host (and not allocated to VMs)
61+
public let hostReservedRAM: UInt64 = 1024 * 1024 * 2048 // Leave 2GB for the VM host
5862

5963
enum CodingKeys: String, CodingKey {
6064
case version

Sources/libhostmgr/VM/VMConfiguration.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,25 @@ struct VMConfiguration {
4141
return virtualMachineConfiguration
4242
}
4343

44+
func calculateCPUCount(shared: Bool) -> Int {
45+
if shared {
46+
return (ProcessInfo.processInfo.physicalProcessorCount - 1).quotientAndRemainder(dividingBy: 2).quotient
47+
}
48+
49+
return ProcessInfo.processInfo.physicalProcessorCount
50+
}
51+
4452
var cpuCount: Int {
45-
VZVirtualMachineConfiguration.maximumAllowedCPUCount - 1
53+
calculateCPUCount(shared: Configuration.shared.isSharedNode)
4654
}
4755

4856
func calculateMemorySize(
4957
min _min: UInt64 = VZVirtualMachineConfiguration.minimumAllowedMemorySize,
5058
max _max: UInt64 = VZVirtualMachineConfiguration.maximumAllowedMemorySize,
59+
hostReserved: UInt64 = Configuration.shared.hostReservedRAM,
5160
shared: Bool
5261
) -> UInt64 {
53-
let vmReservedSize = _max - Configuration.hostReservedRAM
62+
let vmReservedSize = _max - hostReserved
5463

5564
if shared {
5665
return min(max(_min, vmReservedSize.quotientAndRemainder(dividingBy: 2).quotient), _max)
@@ -60,7 +69,7 @@ struct VMConfiguration {
6069
}
6170

6271
var memorySize: UInt64 {
63-
calculateMemorySize(shared: Configuration.shared.useSharedMemoryCapacity)
72+
calculateMemorySize(shared: Configuration.shared.isSharedNode)
6473
}
6574

6675
var graphicsConfiguration: [VZMacGraphicsDeviceConfiguration] {

Tests/libhostmgrTests/VM/VMConfigurationTests.swift

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,96 @@ import XCTest
22
@testable import libhostmgr
33

44
final class VMConfigurationTests: XCTestCase {
5+
let min: UInt64 = 4_194_304
6+
let hostReservedRAM: UInt64 = 1024 * 1024 * 4096
57

68
func testThatMemorySizeCalculationsAreValidForUnshared8GBCapacity() throws {
7-
let min: UInt64 = 4_194_304
89
let max: UInt64 = 8_589_934_592
910
let configuration = try VMConfiguration(
1011
diskImagePath: URL(fileURLWithPath: "/dev/null"),
1112
macAddress: .randomLocallyAdministered()
1213
)
1314

14-
XCTAssertEqual(configuration.calculateMemorySize(min: min, max: max, shared: false), 4_294_967_296)
15+
XCTAssertEqual(configuration.calculateMemorySize(
16+
min: min,
17+
max: max,
18+
hostReserved: hostReservedRAM,
19+
shared: false
20+
), 4_294_967_296)
1521
}
1622

1723
func testThatMemorySizeCalculationsAreValidForShared8GBCapacity() throws {
18-
let min: UInt64 = 4_194_304
1924
let max: UInt64 = 8_589_934_592
2025
let configuration = try VMConfiguration(
2126
diskImagePath: URL(fileURLWithPath: "/dev/null"),
2227
macAddress: .randomLocallyAdministered()
2328
)
2429

25-
XCTAssertEqual(configuration.calculateMemorySize(min: min, max: max, shared: true), 2_147_483_648)
30+
XCTAssertEqual(configuration.calculateMemorySize(
31+
min: min,
32+
max: max,
33+
hostReserved: hostReservedRAM,
34+
shared: true
35+
), 2_147_483_648)
2636
}
2737

2838
func testThatMemorySizeCalculationsAreValidForUnshared16GBCapacity() throws {
29-
let min: UInt64 = 4_194_304
3039
let max: UInt64 = 17_179_869_184
3140
let configuration = try VMConfiguration(
3241
diskImagePath: URL(fileURLWithPath: "/dev/null"),
3342
macAddress: .randomLocallyAdministered()
3443
)
3544

36-
XCTAssertEqual(configuration.calculateMemorySize(min: min, max: max, shared: false), 12_884_901_888)
45+
XCTAssertEqual(configuration.calculateMemorySize(
46+
min: min,
47+
max: max,
48+
hostReserved: hostReservedRAM,
49+
shared: false
50+
), 12_884_901_888)
3751
}
3852

3953
func testThatMemorySizeCalculationsAreValidForShared16GBCapacity() throws {
40-
let min: UInt64 = 4_194_304
4154
let max: UInt64 = 17_179_869_184
4255
let configuration = try VMConfiguration(
4356
diskImagePath: URL(fileURLWithPath: "/dev/null"),
4457
macAddress: .randomLocallyAdministered()
4558
)
4659

47-
XCTAssertEqual(configuration.calculateMemorySize(min: min, max: max, shared: true), 6_442_450_944)
60+
XCTAssertEqual(configuration.calculateMemorySize(
61+
min: min,
62+
max: max,
63+
hostReserved: hostReservedRAM,
64+
shared: true
65+
), 6_442_450_944)
4866
}
4967

5068
func testThatMemorySizeCalculationsAreValidForUnshared32GBCapacity() throws {
51-
let min: UInt64 = 4_194_304
5269
let max: UInt64 = 34_359_738_368
5370
let configuration = try VMConfiguration(
5471
diskImagePath: URL(fileURLWithPath: "/dev/null"),
5572
macAddress: .randomLocallyAdministered()
5673
)
5774

58-
XCTAssertEqual(configuration.calculateMemorySize(min: min, max: max, shared: false), 30_064_771_072)
75+
XCTAssertEqual(configuration.calculateMemorySize(
76+
min: min,
77+
max: max,
78+
hostReserved: hostReservedRAM,
79+
shared: false
80+
), 30_064_771_072)
5981
}
6082

6183
func testThatMemorySizeCalculationsAreValidForShared32GBCapacity() throws {
62-
let min: UInt64 = 4_194_304
6384
let max: UInt64 = 34_359_738_368
6485
let configuration = try VMConfiguration(
6586
diskImagePath: URL(fileURLWithPath: "/dev/null"),
6687
macAddress: .randomLocallyAdministered()
6788
)
6889

69-
XCTAssertEqual(configuration.calculateMemorySize(min: min, max: max, shared: true), 15_032_385_536)
90+
XCTAssertEqual(configuration.calculateMemorySize(
91+
min: min,
92+
max: max,
93+
hostReserved: hostReservedRAM,
94+
shared: true
95+
), 15_032_385_536)
7096
}
7197
}

0 commit comments

Comments
 (0)