From 92634a68fbddbdec08e1ca31b94bf57dc73d2cf4 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 22 Jan 2018 12:05:43 +0000 Subject: [PATCH] Add utility for detecting core count. --- Sources/CNIOLinux/include/c_nio_linux.h | 1 + Sources/NIO/Utilities.swift | 46 +++++++++++++++++++++++ Tests/LinuxMain.swift | 1 + Tests/NIOTests/UtilitiesTest+XCTest.swift | 33 ++++++++++++++++ Tests/NIOTests/UtilitiesTest.swift | 22 +++++++++++ 5 files changed, 103 insertions(+) create mode 100644 Sources/NIO/Utilities.swift create mode 100644 Tests/NIOTests/UtilitiesTest+XCTest.swift create mode 100644 Tests/NIOTests/UtilitiesTest.swift diff --git a/Sources/CNIOLinux/include/c_nio_linux.h b/Sources/CNIOLinux/include/c_nio_linux.h index d2a5ecf00..6c857eae9 100644 --- a/Sources/CNIOLinux/include/c_nio_linux.h +++ b/Sources/CNIOLinux/include/c_nio_linux.h @@ -18,6 +18,7 @@ #include #include #include +#include #include // Some explanation is required here. diff --git a/Sources/NIO/Utilities.swift b/Sources/NIO/Utilities.swift new file mode 100644 index 000000000..1c6c847cc --- /dev/null +++ b/Sources/NIO/Utilities.swift @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +#if os(Linux) +import func CNIOLinux.get_nprocs + +private func linuxCoreCount() -> Int { + return Int(get_nprocs()) +} +#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +private func darwinCoreCount() -> Int { + var cores: CInt = 0 + var coresLen: size_t = MemoryLayout.size(ofValue: cores) + let rc = sysctlbyname("hw.logicalcpu", &cores, &coresLen, nil, 0) + precondition(rc == 0) + return Int(cores) +} +#endif + +/// A utility function that returns an estimate of the number of *logical* cores +/// on the system. +/// +/// This value can be used to help provide an estimate of how many threads to use with +/// the `MultiThreadedEventLoopGroup`. The exact ratio between this number and the number +/// of threads to use is a matter for the programmer, and can be determined based on the +/// specific execution behaviour of the program. +/// +/// - returns: The logical core count on the system. +public func systemCoreCount() -> Int { + #if os(Linux) + return linuxCoreCount() + #elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS) + return darwinCoreCount() + #endif +} diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index f129a499a..a1b1567ed 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -69,5 +69,6 @@ import XCTest testCase(SystemTest.allTests), testCase(TLSConfigurationTest.allTests), testCase(TypeAssistedChannelHandlerTest.allTests), + testCase(UtilitiesTest.allTests), ]) #endif \ No newline at end of file diff --git a/Tests/NIOTests/UtilitiesTest+XCTest.swift b/Tests/NIOTests/UtilitiesTest+XCTest.swift new file mode 100644 index 000000000..f183428a4 --- /dev/null +++ b/Tests/NIOTests/UtilitiesTest+XCTest.swift @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// +/// +/// UtilitiesTest+XCTest.swift +/// +import XCTest + +/// +/// NOTE: This file was generated by generate_linux_tests.rb +/// +/// Do NOT edit this file directly as it will be regenerated automatically when needed. +/// + +extension UtilitiesTest { + + static var allTests : [(String, (UtilitiesTest) -> () throws -> Void)] { + return [ + ("testCoreCountWorks", testCoreCountWorks), + ] + } +} + diff --git a/Tests/NIOTests/UtilitiesTest.swift b/Tests/NIOTests/UtilitiesTest.swift new file mode 100644 index 000000000..404b46ef9 --- /dev/null +++ b/Tests/NIOTests/UtilitiesTest.swift @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import NIO +import XCTest + +class UtilitiesTest: XCTestCase { + func testCoreCountWorks() { + XCTAssertGreaterThan(systemCoreCount(), 0) + } +}