Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux: ProcessInfo.activeProcessorCount: Allow for bad data in CFS quota files #2991

Merged
merged 1 commit into from
May 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Sources/Foundation/ProcessInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,26 @@ open class ProcessInfo: NSObject {
}

// These are internal access for testing
static func countCoreIds(cores: Substring) -> Int {
static func countCoreIds(cores: Substring) -> Int? {
let ids = cores.split(separator: "-", maxSplits: 1)
guard let first = ids.first.flatMap({ Int($0, radix: 10) }),
let last = ids.last.flatMap({ Int($0, radix: 10) }),
last >= first
else { preconditionFailure("cpuset format is incorrect") }
else {
return nil
}
return 1 + last - first
}

static func coreCount(cpuset cpusetPath: String) -> Int? {
guard let cpuset = try? firstLineOfFile(path: cpusetPath).split(separator: ","),
!cpuset.isEmpty
else { return nil }

return cpuset.map(countCoreIds).reduce(0, +)
if let first = cpuset.first, let count = countCoreIds(cores: first) {
return count
} else {
return nil
}
}

static func coreCount(quota quotaPath: String, period periodPath: String) -> Int? {
Expand Down