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

Code before:
        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, +)
    }

    // cgroup1
    static func coreCount(quota quotaPath: String,  period periodPath: String) -> 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
    }

    // cgroup2
    static func coreCount(maxPath: String) -> Int? {
        guard let maxDetails = try? firstLineOfFile(path: maxPath),
              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):
Can I propose we rename these functions to be substantially more explicative of what they do? The little comments doesn't help much.

Code after:
        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