Coverage Report

Created: 2025-09-08 06:42

/src/swift-nio/Sources/NIOCore/Linux.swift
Line
Count
Source (jump to first uncovered line)
1
//===----------------------------------------------------------------------===//
2
//
3
// This source file is part of the SwiftNIO open source project
4
//
5
// Copyright (c) 2017-2023 Apple Inc. and the SwiftNIO project authors
6
// Licensed under Apache License v2.0
7
//
8
// See LICENSE.txt for license information
9
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10
//
11
// SPDX-License-Identifier: Apache-2.0
12
//
13
//===----------------------------------------------------------------------===//
14
15
// This is a companion to System.swift that provides only Linux specials: either things that exist
16
// only on Linux, or things that have Linux-specific extensions.
17
18
#if os(Linux) || os(Android)
19
import CNIOLinux
20
enum Linux {
21
    static let cfsQuotaPath = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
22
    static let cfsPeriodPath = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
23
    static let cpuSetPath = "/sys/fs/cgroup/cpuset/cpuset.cpus"
24
    static let cfsCpuMaxPath = "/sys/fs/cgroup/cpu.max"
25
26
0
    private static func firstLineOfFile(path: String) throws -> Substring {
27
0
        let fh = try NIOFileHandle(_deprecatedPath: path)
28
0
        defer { try! fh.close() }
29
0
        // linux doesn't properly report /sys/fs/cgroup/* files lengths so we use a reasonable limit
30
0
        var buf = ByteBufferAllocator().buffer(capacity: 1024)
31
0
        try buf.writeWithUnsafeMutableBytes(minimumWritableBytes: buf.capacity) { ptr in
32
0
            let res = try fh.withUnsafeFileDescriptor { fd -> CoreIOResult<ssize_t> in
33
0
                try SystemCalls.read(descriptor: fd, pointer: ptr.baseAddress!, size: ptr.count)
34
0
            }
35
0
            switch res {
36
0
            case .processed(let n):
37
0
                return n
38
0
            case .wouldBlock:
39
0
                preconditionFailure("read returned EWOULDBLOCK despite a blocking fd")
40
0
            }
41
0
        }
42
0
        return String(buffer: buf).prefix(while: { $0 != "\n" })
43
0
    }
44
45
0
    private static func countCoreIds(cores: Substring) -> Int {
46
0
        let ids = cores.split(separator: "-", maxSplits: 1)
47
0
        guard
48
0
            let first = ids.first.flatMap({ Int($0, radix: 10) }),
49
0
            let last = ids.last.flatMap({ Int($0, radix: 10) }),
50
0
            last >= first
51
0
        else { preconditionFailure("cpuset format is incorrect") }
52
0
        return 1 + last - first
53
0
    }
54
55
0
    static func coreCount(cpuset cpusetPath: String) -> Int? {
56
0
        guard
57
0
            let cpuset = try? firstLineOfFile(path: cpusetPath).split(separator: ","),
58
0
            !cpuset.isEmpty
59
0
        else { return nil }
60
0
        return cpuset.map(countCoreIds).reduce(0, +)
61
0
    }
62
63
    /// Get the available core count according to cgroup1 restrictions.
64
    /// Round up to the next whole number.
65
    static func coreCountCgroup1Restriction(
66
        quota quotaPath: String = Linux.cfsQuotaPath,
67
        period periodPath: String = Linux.cfsPeriodPath
68
0
    ) -> Int? {
69
0
        guard
70
0
            let quota = try? Int(firstLineOfFile(path: quotaPath)),
71
0
            quota > 0
72
0
        else { return nil }
73
0
        guard
74
0
            let period = try? Int(firstLineOfFile(path: periodPath)),
75
0
            period > 0
76
0
        else { return nil }
77
0
        return (quota - 1 + period) / period  // always round up if fractional CPU quota requested
78
0
    }
79
80
    /// Get the available core count according to cgroup2 restrictions.
81
    /// Round up to the next whole number.
82
0
    static func coreCountCgroup2Restriction(cpuMaxPath: String = Linux.cfsCpuMaxPath) -> Int? {
83
0
        guard let maxDetails = try? firstLineOfFile(path: cpuMaxPath),
84
0
            let spaceIndex = maxDetails.firstIndex(of: " "),
85
0
            let quota = Int(maxDetails[maxDetails.startIndex..<spaceIndex]),
86
0
            let period = Int(maxDetails[maxDetails.index(after: spaceIndex)..<maxDetails.endIndex])
87
0
        else { return nil }
88
0
        return (quota - 1 + period) / period  // always round up if fractional CPU quota requested
89
0
    }
90
}
91
#endif