Add utility for detecting core count.

This commit is contained in:
Cory Benfield committed 2018-01-22 12:05:43 +00:00
1 parent 34cc404142
commit 92634a68fb
5 files changed
+103

No files matched your search

+1
View File
@@ -18,6 +18,7 @@
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
#include <sys/sysinfo.h>
#include <sys/socket.h>
// Some explanation is required here.
+46
View File
@@ -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
}
+1
View File
@@ -69,5 +69,6 @@ import XCTest
testCase(SystemTest.allTests),
testCase(TLSConfigurationTest.allTests),
testCase(TypeAssistedChannelHandlerTest.allTests),
testCase(UtilitiesTest.allTests),
])
#endif
+33
View File
@@ -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),
]
}
}
+22
View File
@@ -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)
}
}