diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index f02a4ffb4..6b0e0e05c 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -249,6 +249,12 @@ public protocol EventLoop: EventLoopGroup { func preconditionInEventLoop(file: StaticString, line: UInt) } +extension EventLoopGroup { + public var description: String { + return String(describing: self) + } +} + /// Represents a time _interval_. /// /// - note: `TimeAmount` should not be used to represent a point in time. @@ -754,6 +760,7 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { private static let threadSpecificEventLoop = ThreadSpecificVariable() + private let myGroupID: Int private let index = NIOAtomic.makeAtomic(value: 0) private let eventLoops: [SelectableEventLoop] private let shutdownLock: Lock = Lock() @@ -812,6 +819,7 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { /// - threadInitializers: The `ThreadInitializer`s to use. internal init(threadInitializers: [ThreadInitializer]) { let myGroupID = nextEventLoopGroupID.add(1) + self.myGroupID = myGroupID var idx = 0 self.eventLoops = threadInitializers.map { initializer in // Maximum name length on linux is 16 by default. @@ -945,6 +953,12 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup { } } +extension MultiThreadedEventLoopGroup: CustomStringConvertible { + public var description: String { + return "MultiThreadedEventLoopGroup { threadPattern = NIO-ELT-\(self.myGroupID)-#* }" + } +} + @usableFromInline internal final class ScheduledTask { let task: () -> Void diff --git a/Sources/NIO/SelectableEventLoop.swift b/Sources/NIO/SelectableEventLoop.swift index c981bd419..9d104cc9d 100644 --- a/Sources/NIO/SelectableEventLoop.swift +++ b/Sources/NIO/SelectableEventLoop.swift @@ -478,7 +478,7 @@ extension SelectableEventLoop: CustomStringConvertible { @usableFromInline var description: String { return self._tasksLock.withLock { - return "SelectableEventLoop { selector = \(self._selector), scheduledTasks = \(self._scheduledTasks.description) }" + return "SelectableEventLoop { selector = \(self._selector), thread = \(self.thread), scheduledTasks = \(self._scheduledTasks.description) }" } } } diff --git a/Sources/NIO/Thread.swift b/Sources/NIO/Thread.swift index a01e200fa..37272c3b9 100644 --- a/Sources/NIO/Thread.swift +++ b/Sources/NIO/Thread.swift @@ -36,6 +36,7 @@ private func sys_pthread_setname_np(_ p: pthread_t, _ pointer: UnsafePointer(rebasing: ptr.prefix { char in + char != 0 + })), + as: Unicode.UTF8.self) } - return String(cString: chars) } } @@ -107,7 +115,7 @@ final class NIOThread { // this is non-critical so we ignore the result here, we've seen EPERM in containers. } - body(NIOThread(pthread: pt)) + body(NIOThread(pthread: pt, desiredName: name)) return nil }, Unmanaged.passRetained(box).toOpaque()) @@ -126,7 +134,36 @@ final class NIOThread { /// Returns the current running `NIOThread`. static var current: NIOThread { - return NIOThread(pthread: pthread_self()) + return NIOThread(pthread: pthread_self(), desiredName: nil) + } +} + +extension NIOThread: CustomStringConvertible { + var description: String { + let desiredName = self.desiredName + let actualName = self.currentName + + switch (desiredName, actualName) { + case (.some(let desiredName), .some(desiredName)): + // We know the current, actual name and the desired name and they match. This is hopefully the most common + // situation. + return "NIOThread(name = \(desiredName))" + case (.some(let desiredName), .some(let actualName)): + // We know both names but they're not equal. That's odd but not impossible, some misbehaved library might + // have changed the name. + return "NIOThread(desiredName = \(desiredName), actualName = \(actualName))" + case (.some(let desiredName), .none): + // We only know the desired name and can't get the actual thread name. The OS might not be able to provide + // the name to us. + return "NIOThread(desiredName = \(desiredName))" + case (.none, .some(let actualName)): + // We only know the actual name. This can happen when we don't have a reference to the actually spawned + // thread but rather ask for the current thread and then print it. + return "NIOThread(actualName = \(actualName))" + case (.none, .none): + // We know nothing, sorry. + return "NIOThread(n/a)" + } } } diff --git a/Tests/NIOTests/EventLoopTest+XCTest.swift b/Tests/NIOTests/EventLoopTest+XCTest.swift index d43440510..af3945ad8 100644 --- a/Tests/NIOTests/EventLoopTest+XCTest.swift +++ b/Tests/NIOTests/EventLoopTest+XCTest.swift @@ -65,6 +65,8 @@ extension EventLoopTest { ("testSchedulingTaskOnTheEventLoopWithinTheEventLoopsOnlyIOOperation", testSchedulingTaskOnTheEventLoopWithinTheEventLoopsOnlyIOOperation), ("testCancellingTheLastOutstandingTask", testCancellingTheLastOutstandingTask), ("testSchedulingTaskOnTheEventLoopWithinTheEventLoopsOnlyScheduledTask", testSchedulingTaskOnTheEventLoopWithinTheEventLoopsOnlyScheduledTask), + ("testSelectableEventLoopDescription", testSelectableEventLoopDescription), + ("testMultiThreadedEventLoopGroupDescription", testMultiThreadedEventLoopGroupDescription), ] } } diff --git a/Tests/NIOTests/EventLoopTest.swift b/Tests/NIOTests/EventLoopTest.swift index 8e2633945..287138b08 100644 --- a/Tests/NIOTests/EventLoopTest.swift +++ b/Tests/NIOTests/EventLoopTest.swift @@ -1047,4 +1047,33 @@ public final class EventLoopTest : XCTestCase { } g.wait() } + + func testSelectableEventLoopDescription() { + let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1) + defer { + XCTAssertNoThrow(try elg.syncShutdownGracefully()) + } + + let el: EventLoop = elg.next() + let expectedPrefix = "SelectableEventLoop { selector = Selector { descriptor =" + let expectedContains = "thread = NIOThread(name = NIO-ELT-" + let expectedSuffix = ", scheduledTasks = PriorityQueue(count: 0): [] }" + let desc = el.description + XCTAssert(el.description.starts(with: expectedPrefix), desc) + XCTAssert(el.description.reversed().starts(with: expectedSuffix.reversed()), desc) + // let's check if any substring contains the `expectedContains` + XCTAssert(desc.indices.contains { startIndex in + desc[startIndex...].starts(with: expectedContains) + }, desc) + } + + func testMultiThreadedEventLoopGroupDescription() { + let elg: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) + defer { + XCTAssertNoThrow(try elg.syncShutdownGracefully()) + } + + XCTAssert(elg.description.starts(with: "MultiThreadedEventLoopGroup { threadPattern = NIO-ELT-"), + elg.description) + } }