Repository: apple/swift-nio
File: Sources/NIOCore/Linux.swift (Swift)

Code before:
                preconditionFailure("read returned EWOULDBLOCK despite a blocking fd")
            }
        }
        return String(buffer: buf).prefix(while: { $0 != "\n" })
    }

    private 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") }
        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, +)
    }

    /// Get the available core count according to cgroup1 restrctions.
    /// Round up the the next whole number.
    static func coreCountCgroup1Restriction(quota quotaPath: String = Linux.cfsQuotaPath,
                                            period periodPath: String = Linux.cfsPeriodPath) -> Int? {
        guard
            let quota = try? Int(firstLineOfFile(path: quotaPath)),
            quota > 0
        else { return nil }
        guard
            let period = try? Int(firstLineOfFile(path: periodPath)),
            period > 0
        else { return nil }
        return (quota - 1 + period) / period // always round up if fractional CPU quota requested
    }

    /// Get the available core count according to cgroup2 restrctions.
    /// Round up the the next whole number.
    static func coreCountCgroup2Restriction(cpuMaxPath: String = Linux.cfsCpuMaxPath) -> Int? {
        guard let maxDetails = try? firstLineOfFile(path: cpuMaxPath),
              let spaceIndex = maxDetails.firstIndex(of: " "),
              let quota = Int(maxDetails[maxDetails.startIndex ..< spaceIndex]),
              let period = Int(maxDetails[maxDetails.index(after: spaceIndex) ..< maxDetails.endIndex])
        else { return nil }
        return (quota - 1 + period) / period // always round up if fractional CPU quota requested
    }
}
#endif

Review comment (suggestion):
Following in the wake of @MaxDesiatov, typo nit
```suggestion
    /// Round up to the next whole number.
```

Code after:
                preconditionFailure("read returned EWOULDBLOCK despite a blocking fd")
            }
        }
        return String(buffer: buf).prefix(while: { $0 != "\n" })
    }

    private 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") }
        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, +)
    }

    /// Get the available core count according to cgroup1 restrictions.
    /// Round up to the next whole number.
    static func coreCountCgroup1Restriction(quota quotaPath: String = Linux.cfsQuotaPath,
                                            period periodPath: String = Linux.cfsPeriodPath) -> Int? {
        guard
            let quota = try? Int(firstLineOfFile(path: quotaPath)),
            quota > 0
        else { return nil }
        guard
            let period = try? Int(firstLineOfFile(path: periodPath)),
            period > 0
        else { return nil }
        return (quota - 1 + period) / period // always round up if fractional CPU quota requested
    }

    /// Get the available core count according to cgroup2 restrictions.
    /// Round up to the next whole number.
    static func coreCountCgroup2Restriction(cpuMaxPath: String = Linux.cfsCpuMaxPath) -> Int? {
        guard let maxDetails = try? firstLineOfFile(path: cpuMaxPath),
              let spaceIndex = maxDetails.firstIndex(of: " "),
              let quota = Int(maxDetails[maxDetails.startIndex ..< spaceIndex]),
              let period = Int(maxDetails[maxDetails.index(after: spaceIndex) ..< maxDetails.endIndex])
        else { return nil }
        return (quota - 1 + period) / period // always round up if fractional CPU quota requested
    }
}
#endif