Skip to content

Commit

Permalink
Initialize ELG with the number of affinitized cores (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushkar N Kulkarni authored and djones6 committed Dec 19, 2018
1 parent 710d459 commit fad87da
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
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

0 comments on commit fad87da

Please sign in to comment.