mirror of
https://github.com/apple/swift-nio
synced 2026-11-27 05:04:22 +00:00
do not waste time when metrics delegate is not set (#3441)
### Motivation: Unfortunately, even if no metics delegate has been configured, significant work is being done _each EL tick_. Concretely: - 2 `NIODealine.now()` calls which are expensive particularly on some hypervisors (may cause a VMEXIT!) - Some unnecessary calculations ### Modifications: - Only perform the metrics delegate work if a delegate has actually been set ### Result: If no delegate is set, each EL tick will now be faster. Co-authored-by: George Barnett <gbarnett@apple.com>
This commit is contained in:
1 parent
888c16a7d3
commit
3eea09220e
2 files changed
+39
-21
No files matched your search
@@ -107,9 +107,9 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup {
|
||||
canBeShutdownIndividually: canEventLoopBeShutdownIndividually,
|
||||
metricsDelegate: metricsDelegate
|
||||
)
|
||||
threadSpecificEventLoop.currentValue = loop
|
||||
Self.threadSpecificEventLoop.currentValue = loop
|
||||
defer {
|
||||
threadSpecificEventLoop.currentValue = nil
|
||||
Self.threadSpecificEventLoop.currentValue = nil
|
||||
}
|
||||
callback(loop)
|
||||
try loop.run()
|
||||
|
||||
@@ -187,9 +187,7 @@ internal final class SelectableEventLoop: EventLoop, @unchecked Sendable {
|
||||
private let promiseCreationStoreLock = NIOLock()
|
||||
private var _promiseCreationStore: [_NIOEventLoopFutureIdentifier: (file: StaticString, line: UInt)] = [:]
|
||||
|
||||
private let metricsDelegate: (any NIOEventLoopMetricsDelegate)?
|
||||
|
||||
private var lastTickEndTime: NIODeadline
|
||||
private var metricsDelegateState: MetricsDelegateState?
|
||||
|
||||
@usableFromInline
|
||||
internal func _promiseCreated(futureIdentifier: _NIOEventLoopFutureIdentifier, file: StaticString, line: UInt) {
|
||||
@@ -261,7 +259,9 @@ internal final class SelectableEventLoop: EventLoop, @unchecked Sendable {
|
||||
canBeShutdownIndividually: Bool,
|
||||
metricsDelegate: NIOEventLoopMetricsDelegate?
|
||||
) {
|
||||
self.metricsDelegate = metricsDelegate
|
||||
self.metricsDelegateState = metricsDelegate.map { delegate in
|
||||
MetricsDelegateState(metricsDelegate: delegate, lastTickEndime: .now())
|
||||
}
|
||||
self._uniqueID = uniqueID
|
||||
self._parentGroup = parentGroup
|
||||
self._selector = selector
|
||||
@@ -270,7 +270,6 @@ internal final class SelectableEventLoop: EventLoop, @unchecked Sendable {
|
||||
self.msgBufferPool = Pool<PooledMsgBuffer>(maxSize: 16)
|
||||
self.tasksCopy.reserveCapacity(Self.tasksCopyBatchSize)
|
||||
self.canBeShutdownIndividually = canBeShutdownIndividually
|
||||
self.lastTickEndTime = .now()
|
||||
// note: We are creating a reference cycle here that we'll break when shutting the SelectableEventLoop down.
|
||||
// note: We have to create the promise and complete it because otherwise we'll hit a loop in `makeSucceededFuture`. This is
|
||||
// fairly dumb, but it's the only option we have.
|
||||
@@ -747,21 +746,35 @@ internal final class SelectableEventLoop: EventLoop, @unchecked Sendable {
|
||||
return nextDeadline
|
||||
}
|
||||
|
||||
private func runLoop(selfIdentifier: ObjectIdentifier) -> NIODeadline? {
|
||||
let tickStartTime: NIODeadline = .now()
|
||||
let sleepTime: TimeAmount = tickStartTime - self.lastTickEndTime
|
||||
private func runOneLoopTick(selfIdentifier: ObjectIdentifier) -> NIODeadline? {
|
||||
let tickStartInfo:
|
||||
(
|
||||
metricsDelegate: any NIOEventLoopMetricsDelegate,
|
||||
tickStartTime: NIODeadline,
|
||||
sleepTime: TimeAmount
|
||||
)? = self.metricsDelegateState.map { metricsDelegateState in
|
||||
let tickStartTime = NIODeadline.now() // Potentially expensive, only if delegate set.
|
||||
return (
|
||||
metricsDelegate: metricsDelegateState.metricsDelegate,
|
||||
tickStartTime: tickStartTime,
|
||||
sleepTime: tickStartTime - metricsDelegateState.lastTickEndime
|
||||
)
|
||||
}
|
||||
|
||||
var tasksProcessedInTick = 0
|
||||
defer {
|
||||
let tickEndTime: NIODeadline = .now()
|
||||
let tickInfo = NIOEventLoopTickInfo(
|
||||
eventLoopID: selfIdentifier,
|
||||
numberOfTasks: tasksProcessedInTick,
|
||||
sleepTime: sleepTime,
|
||||
startTime: tickStartTime,
|
||||
endTime: tickEndTime
|
||||
)
|
||||
self.metricsDelegate?.processedTick(info: tickInfo)
|
||||
self.lastTickEndTime = tickEndTime
|
||||
if let tickStartInfo = tickStartInfo {
|
||||
let tickEndTime = NIODeadline.now() // Potentially expensive, only if delegate set.
|
||||
let tickInfo = NIOEventLoopTickInfo(
|
||||
eventLoopID: selfIdentifier,
|
||||
numberOfTasks: tasksProcessedInTick,
|
||||
sleepTime: tickStartInfo.sleepTime,
|
||||
startTime: tickStartInfo.tickStartTime,
|
||||
endTime: tickEndTime
|
||||
)
|
||||
tickStartInfo.metricsDelegate.processedTick(info: tickInfo)
|
||||
self.metricsDelegateState?.lastTickEndime = tickEndTime
|
||||
}
|
||||
}
|
||||
while true {
|
||||
let nextReadyDeadline = self._tasksLock.withLock { () -> NIODeadline? in
|
||||
@@ -905,7 +918,7 @@ internal final class SelectableEventLoop: EventLoop, @unchecked Sendable {
|
||||
}
|
||||
}
|
||||
}
|
||||
nextReadyDeadline = runLoop(selfIdentifier: selfIdentifier)
|
||||
nextReadyDeadline = self.runOneLoopTick(selfIdentifier: selfIdentifier)
|
||||
}
|
||||
|
||||
// This EventLoop was closed so also close the underlying selector.
|
||||
@@ -1036,6 +1049,11 @@ internal final class SelectableEventLoop: EventLoop, @unchecked Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
struct MetricsDelegateState {
|
||||
var metricsDelegate: any NIOEventLoopMetricsDelegate
|
||||
var lastTickEndime: NIODeadline
|
||||
}
|
||||
|
||||
extension SelectableEventLoop: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
@usableFromInline
|
||||
var description: String {
|
||||
|
||||
Reference in new issue
Block a user