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

Code before:

    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:

    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