Skip to content

Commit

Permalink
Initialize ELG with the number of affinitized cores
Browse files Browse the repository at this point in the history
The MultiThreadedEventLoopGroup that runs the HTTPServer is initialized with all the available cores. On Linux, it could be instead initialised to the number of affinitized cores. If the number of affinitized cores is lesser than the `System.coreCount`, initializing the MultiThreadedEventLoopGroup with the later comes with a performance penalty.
  • Loading branch information
Pushkar N Kulkarni committed Dec 17, 2018
1 parent 0722291 commit 6439ae1
Show file tree
Hide file tree
Showing 4 changed files with 42 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
25 changes: 25 additions & 0 deletions Sources/CLinuxHelpers/ProcessorAffinity.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifdef __linux__

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

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
3 changes: 3 additions & 0 deletions Sources/CLinuxHelpers/include/ProcessorAffinity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#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 6439ae1

Please sign in to comment.