add descriptions to EventLoop and NIOThread (#1317)

Motivation:

Certain issues are easier debugged if we can print the thread names.

Modifications:

- add the thread name to SelectableEventLoop's description
- make EventLoop and NIOThread CustomStringConvertible

Result:

easier debugging.
This commit is contained in:
Johannes Weiss authored and GitHub committed 2019-12-19 14:52:03 +00:00
1 parent 7314c59733
commit 6174c9802c
5 files changed
+90 -8

No files matched your search

+14
View File
@@ -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<SelectableEventLoop>()
private let myGroupID: Int
private let index = NIOAtomic<Int>.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
+1 -1
View File
@@ -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) }"
}
}
}
+44 -7
View File
@@ -36,6 +36,7 @@ private func sys_pthread_setname_np(_ p: pthread_t, _ pointer: UnsafePointer<Int
///
/// All methods exposed are thread-safe.
final class NIOThread {
private let desiredName: String?
/// The pthread_t used by this instance.
private let pthread: pthread_t
@@ -44,8 +45,9 @@ final class NIOThread {
///
/// - arguments:
/// - pthread: The `pthread_t` that is wrapped and used by the `NIOThread`.
private init(pthread: pthread_t) {
private init(pthread: pthread_t, desiredName: String?) {
self.pthread = pthread
self.desiredName = desiredName
}
/// Execute the given body with the `pthread_t` that is used by this `NIOThread` as argument.
@@ -60,14 +62,20 @@ final class NIOThread {
}
/// Get current name of the `NIOThread` or `nil` if not set.
var name: String? {
var currentName: String? {
get {
// 64 bytes should be good enough as on Linux the limit is usually 16 and it's very unlikely a user will ever set something longer anyway.
var chars: [CChar] = Array(repeating: 0, count: 64)
guard sys_pthread_getname_np(pthread, &chars, chars.count) == 0 else {
return nil
return chars.withUnsafeMutableBufferPointer { ptr in
guard sys_pthread_getname_np(self.pthread, ptr.baseAddress!, ptr.count) == 0 else {
return nil
}
return String(decoding: UnsafeRawBufferPointer(UnsafeBufferPointer<CChar>(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)"
}
}
}
@@ -65,6 +65,8 @@ extension EventLoopTest {
("testSchedulingTaskOnTheEventLoopWithinTheEventLoopsOnlyIOOperation", testSchedulingTaskOnTheEventLoopWithinTheEventLoopsOnlyIOOperation),
("testCancellingTheLastOutstandingTask", testCancellingTheLastOutstandingTask),
("testSchedulingTaskOnTheEventLoopWithinTheEventLoopsOnlyScheduledTask", testSchedulingTaskOnTheEventLoopWithinTheEventLoopsOnlyScheduledTask),
("testSelectableEventLoopDescription", testSelectableEventLoopDescription),
("testMultiThreadedEventLoopGroupDescription", testMultiThreadedEventLoopGroupDescription),
]
}
}
+29
View File
@@ -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)
}
}