From bdf004b44f77c56fca752cd1cf243c802f8469c9 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 23 Mar 2026 08:20:33 +0000 Subject: [PATCH] Enable thread-pools to be pinned to CPUs (#3557) Motivation: Similar to MultiThreadedEventLoopGroup, it is useful in some NUMA circumstances to be able to pin thread pools to specific CPUs. This can minimise the overheads of moving memory around across NUMA nodes. Modifications: Added a pinned CPUs initializer to NIOThreadPool Result: Users have more flexibility. --- Sources/NIOPosix/NIOThreadPool.swift | 39 ++++++++++++++-- Tests/NIOPosixTests/NIOThreadPoolTest.swift | 49 +++++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/Sources/NIOPosix/NIOThreadPool.swift b/Sources/NIOPosix/NIOThreadPool.swift index 34af904eb..78bcba1e3 100644 --- a/Sources/NIOPosix/NIOThreadPool.swift +++ b/Sources/NIOPosix/NIOThreadPool.swift @@ -115,6 +115,7 @@ public final class NIOThreadPool { public let numberOfThreads: Int private let canBeStopped: Bool + private let pinnedCPUIDs: [Int]? /// Gracefully shutdown this `NIOThreadPool`. All tasks will be run before shutdown will take place. /// @@ -213,20 +214,42 @@ public final class NIOThreadPool { /// - Parameters: /// - numberOfThreads: The number of threads to use for the thread pool. public convenience init(numberOfThreads: Int) { - self.init(numberOfThreads: numberOfThreads, canBeStopped: true) + self.init( + numberOfThreads: numberOfThreads, + pinnedCPUIDs: nil, + canBeStopped: true + ) } + #if !os(WASI) + #if os(Linux) || os(Android) + /// Initialize a `NIOThreadPool` thread pool with threads pinned to specific CPUs. + /// + /// - Parameters: + /// - pinnedCPUIDs: The IDs of the CPUs to use for the thread pool. One thread will be + /// created per entry in this array. + public convenience init(pinnedCPUIDs: [Int]) { + self.init( + numberOfThreads: pinnedCPUIDs.count, + pinnedCPUIDs: pinnedCPUIDs, + canBeStopped: true + ) + } + + #endif + #endif /// Create a ``NIOThreadPool`` that is already started, cannot be shut down and must not be `deinit`ed. /// /// This is only useful for global singletons. public static func _makePerpetualStartedPool(numberOfThreads: Int, threadNamePrefix: String) -> NIOThreadPool { - let pool = self.init(numberOfThreads: numberOfThreads, canBeStopped: false) + let pool = self.init(numberOfThreads: numberOfThreads, pinnedCPUIDs: nil, canBeStopped: false) pool._start(threadNamePrefix: threadNamePrefix) return pool } - private init(numberOfThreads: Int, canBeStopped: Bool) { + private init(numberOfThreads: Int, pinnedCPUIDs: [Int]?, canBeStopped: Bool) { self.numberOfThreads = numberOfThreads + self.pinnedCPUIDs = pinnedCPUIDs self.canBeStopped = canBeStopped self._workAvailable = NIOThreadPoolWorkAvailable() } @@ -331,8 +354,18 @@ public final class NIOThreadPool { // that would otherwise be emitted. let readyThreads = ConditionLock(value: 0) for id in 0..([]) + let threadNameCollectionSem = DispatchSemaphore(value: 0) + let threadBlockingSem = DispatchSemaphore(value: 0) + + // let's use up all the threads + for i in (0..