From 40be05e4d494ca9c3673222f3c96849cc966b7fd Mon Sep 17 00:00:00 2001 From: Amos Ng Date: Fri, 12 Jun 2020 03:10:00 +0800 Subject: [PATCH] Made threads-per-cpu configurable for moar speed --- dev/settings.json | 3 ++- src/main/java/mdnet/base/ClientSettings.java | 11 +++++++++-- src/main/kotlin/mdnet/base/Netty.kt | 8 ++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/dev/settings.json b/dev/settings.json index 7bdb768..515d8e4 100644 --- a/dev/settings.json +++ b/dev/settings.json @@ -3,5 +3,6 @@ "max_cache_size_mib": 2048, "client_port": 8080, "max_burst_rate_kib_per_second": 100, - "max_bandwidth_mib_per_hour": 1 + "max_bandwidth_mib_per_hour": 1, + "threads_per_cpu": 32 } diff --git a/src/main/java/mdnet/base/ClientSettings.java b/src/main/java/mdnet/base/ClientSettings.java index d84687a..de17707 100644 --- a/src/main/java/mdnet/base/ClientSettings.java +++ b/src/main/java/mdnet/base/ClientSettings.java @@ -16,14 +16,17 @@ public final class ClientSettings { private final int clientPort; @SerializedName("client_secret") private final String clientSecret; + @SerializedName("threads_per_cpu") + private final int threadsPerCPU; public ClientSettings(long maxCacheSizeMib, long maxBandwidthMibPerHour, long maxBurstRateKibPerSecond, - int clientPort, String clientSecret) { + int clientPort, String clientSecret, int threadsPerCPU) { this.maxCacheSizeMib = maxCacheSizeMib; this.maxBandwidthMibPerHour = maxBandwidthMibPerHour; this.maxBurstRateKibPerSecond = maxBurstRateKibPerSecond; this.clientPort = clientPort; this.clientSecret = Objects.requireNonNull(clientSecret); + this.threadsPerCPU = threadsPerCPU; } public long getMaxCacheSizeMib() { @@ -46,11 +49,15 @@ public final class ClientSettings { return clientSecret; } + public int getThreadsPerCPU() { + return threadsPerCPU; + } + @Override public String toString() { return "ClientSettings{" + "maxCacheSizeMib=" + maxCacheSizeMib + ", maxBandwidthMibPerHour=" + maxBandwidthMibPerHour + ", maxBurstRateKibPerSecond=" + maxBurstRateKibPerSecond + ", clientPort=" - + clientPort + ", clientSecret='" + "" + '\'' + '}'; + + clientPort + ", clientSecret='" + "" + '\'' + ", threadsPerCPU=" + threadsPerCPU + "}"; } public static boolean isSecretValid(String clientSecret) { diff --git a/src/main/kotlin/mdnet/base/Netty.kt b/src/main/kotlin/mdnet/base/Netty.kt index 070cdb2..36dace2 100644 --- a/src/main/kotlin/mdnet/base/Netty.kt +++ b/src/main/kotlin/mdnet/base/Netty.kt @@ -35,12 +35,12 @@ import java.util.concurrent.atomic.AtomicReference import javax.net.ssl.SSLException private val LOGGER = LoggerFactory.getLogger("Application") -private val THREADS_TO_ALLOCATE = Runtime.getRuntime().availableProcessors() * 32 / 2 +private val THREADS_TO_ALLOCATE = Runtime.getRuntime().availableProcessors() class Netty(private val tls: ServerSettings.TlsCert, private val clientSettings: ClientSettings, private val stats: AtomicReference) : ServerConfig { override fun toServer(httpHandler: HttpHandler): Http4kServer = object : Http4kServer { - private val masterGroup = NioEventLoopGroup(THREADS_TO_ALLOCATE) - private val workerGroup = NioEventLoopGroup(THREADS_TO_ALLOCATE) + private val masterGroup = NioEventLoopGroup(THREADS_TO_ALLOCATE * clientSettings.getThreadsPerCPU()) + private val workerGroup = NioEventLoopGroup(THREADS_TO_ALLOCATE * clientSettings.getThreadsPerCPU()) private lateinit var closeFuture: ChannelFuture private lateinit var address: InetSocketAddress @@ -54,7 +54,7 @@ class Netty(private val tls: ServerSettings.TlsCert, private val clientSettings: override fun start(): Http4kServer = apply { if (LOGGER.isInfoEnabled) { - LOGGER.info("Starting webserver with {} threads", THREADS_TO_ALLOCATE) + LOGGER.info("Starting webserver with {} threads", THREADS_TO_ALLOCATE * clientSettings.getThreadsPerCPU()) } val (mainCert, chainCert) = getX509Certs(tls.certificate)