Skip to content

Commit c1e2f09

Browse files
authored
Move MulticastChannel and NIONetworkDevice to NIOCore (#1931)
Motivation: MultcastChannel is a general abstraction for expressing multicast capabilities on a given Channel. This abstraction doesn't have any particularly tight tie to the POSIX layer, so it belongs in NIOCore. This is also expressed in terms of NIONetworkDevice, so we need to move that over. That also encourages us to bring over System.enumerateDevices, and given that System.coreCount is also fairly general-purpose we may as well bring it along too. Modifications: - Move MulticastChannel to NIOCore - Move NIONetworkDevice to NIOCore - Move System to NIOCore Result: More general-purpose abstractions in NIOCore.
1 parent a7e064d commit c1e2f09

File tree

11 files changed

+602
-622
lines changed

11 files changed

+602
-622
lines changed

Sources/NIO/Linux.swift

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftNIO open source project
44
//
5-
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
5+
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -118,9 +118,6 @@ internal enum Epoll {
118118
}
119119

120120
internal enum Linux {
121-
static let cfsQuotaPath = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
122-
static let cfsPeriodPath = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
123-
static let cpuSetPath = "/sys/fs/cgroup/cpuset/cpuset.cpus"
124121
#if os(Android)
125122
static let SOCK_CLOEXEC = Glibc.SOCK_CLOEXEC
126123
static let SOCK_NONBLOCK = Glibc.SOCK_NONBLOCK
@@ -140,54 +137,5 @@ internal enum Linux {
140137
}
141138
return fd
142139
}
143-
144-
private static func firstLineOfFile(path: String) throws -> Substring {
145-
let fh = try NIOFileHandle(path: path)
146-
defer { try! fh.close() }
147-
// linux doesn't properly report /sys/fs/cgroup/* files lengths so we use a reasonable limit
148-
var buf = ByteBufferAllocator().buffer(capacity: 1024)
149-
try buf.writeWithUnsafeMutableBytes(minimumWritableBytes: buf.capacity) { ptr in
150-
let res = try fh.withUnsafeFileDescriptor { fd -> IOResult<ssize_t> in
151-
return try Posix.read(descriptor: fd, pointer: ptr.baseAddress!, size: ptr.count)
152-
}
153-
switch res {
154-
case .processed(let n):
155-
return n
156-
case .wouldBlock:
157-
preconditionFailure("read returned EWOULDBLOCK despite a blocking fd")
158-
}
159-
}
160-
return String(buffer: buf).prefix(while: { $0 != "\n" })
161-
}
162-
163-
private static func countCoreIds(cores: Substring) -> Int {
164-
let ids = cores.split(separator: "-", maxSplits: 1)
165-
guard
166-
let first = ids.first.flatMap({ Int($0, radix: 10) }),
167-
let last = ids.last.flatMap({ Int($0, radix: 10) }),
168-
last >= first
169-
else { preconditionFailure("cpuset format is incorrect") }
170-
return 1 + last - first
171-
}
172-
173-
static func coreCount(cpuset cpusetPath: String) -> Int? {
174-
guard
175-
let cpuset = try? firstLineOfFile(path: cpusetPath).split(separator: ","),
176-
!cpuset.isEmpty
177-
else { return nil }
178-
return cpuset.map(countCoreIds).reduce(0, +)
179-
}
180-
181-
static func coreCount(quota quotaPath: String, period periodPath: String) -> Int? {
182-
guard
183-
let quota = try? Int(firstLineOfFile(path: quotaPath)),
184-
quota > 0
185-
else { return nil }
186-
guard
187-
let period = try? Int(firstLineOfFile(path: periodPath)),
188-
period > 0
189-
else { return nil }
190-
return (quota - 1 + period) / period // always round up if fractional CPU quota requested
191-
}
192140
}
193141
#endif

0 commit comments

Comments
 (0)