Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize ELG with the number of affinitized cores #120

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ let package = Package(
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "CLinuxHelpers",
dependencies: []),
.target(
name: "KituraNet",
dependencies: ["NIO", "NIOFoundationCompat", "NIOHTTP1", "NIOOpenSSL", "SSLService", "LoggerAPI", "NIOWebSocket"]),
dependencies: ["NIO", "NIOFoundationCompat", "NIOHTTP1", "NIOOpenSSL", "SSLService", "LoggerAPI", "NIOWebSocket", "CLinuxHelpers"]),
.testTarget(
name: "KituraNetTests",
dependencies: ["KituraNet"]),
Expand Down
40 changes: 40 additions & 0 deletions Sources/CLinuxHelpers/ProcessorAffinity.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright IBM Corporation 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifdef __linux__

#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>

// Reference: https://stackoverflow.com/questions/10490756/how-to-use-sched-getaffinity-and-sched-setaffinity-in-linux-from-c
int linux_sched_getaffinity() {
cpu_set_t mask;
long nproc, i, count = 0;

if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
return -1;
} else {
nproc = sysconf(_SC_NPROCESSORS_ONLN);
for (i = 0; i < nproc; i++) {
if(CPU_ISSET(i, &mask))
count += 1;
}
return count;
}
}

#endif
19 changes: 19 additions & 0 deletions Sources/CLinuxHelpers/include/ProcessorAffinity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright IBM Corporation 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#if __linux__
int linux_sched_getaffinity();
#endif
12 changes: 10 additions & 2 deletions Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import NIOOpenSSL
import SSLService
import LoggerAPI
import NIOWebSocket
import CLinuxHelpers

/// An HTTP server that listens for connections on a socket.
public class HTTPServer : Server {
Expand Down Expand Up @@ -66,11 +67,18 @@ public class HTTPServer : Server {
private let maxPendingConnections = 100

/// The event loop group on which the HTTP handler runs
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
private let eventLoopGroup: MultiThreadedEventLoopGroup

private var ctx: ChannelHandlerContext?

public init() { }
public init() {
#if os(Linux)
let numberOfCores = Int(linux_sched_getaffinity())
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: numberOfCores > 0 ? numberOfCores : System.coreCount)
#else
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
#endif
}

/// SSL cert configs for handling client requests
public var sslConfig: SSLService.Configuration? {
Expand Down