From 93f8679fa1b2580da0c54566dd4f3af3fa0c3b34 Mon Sep 17 00:00:00 2001 From: Mikhail Bezoyan Date: Tue, 8 Oct 2024 22:12:03 +0000 Subject: [PATCH] [finagle-netty4] Expose a knob to control io-ratio in EpollEventLoop Problem We can't control how much time netty spends processing i/o events compared to other work Solution Introduce a knob that allows us to do this Differential Revision: https://phabricator.twitter.biz/D1175405 --- .../finagle/netty4/WorkerEventLoop.scala | 18 +++++++++++----- .../com/twitter/finagle/netty4/ioRatio.scala | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 finagle-netty4/src/main/scala/com/twitter/finagle/netty4/ioRatio.scala diff --git a/finagle-netty4/src/main/scala/com/twitter/finagle/netty4/WorkerEventLoop.scala b/finagle-netty4/src/main/scala/com/twitter/finagle/netty4/WorkerEventLoop.scala index debeff3bba..c42c08cbf7 100644 --- a/finagle-netty4/src/main/scala/com/twitter/finagle/netty4/WorkerEventLoop.scala +++ b/finagle-netty4/src/main/scala/com/twitter/finagle/netty4/WorkerEventLoop.scala @@ -61,11 +61,16 @@ private object WorkerEventLoop { ) } - def make(executor: Executor, numWorkers: Int): EventLoopGroup = { + def make(executor: Executor, numWorkers: Int, ioRatio: Int = 50): EventLoopGroup = { workerPoolSize.addAndGet(numWorkers) val result = - if (useNativeEpoll() && Epoll.isAvailable) new EpollEventLoopGroup(numWorkers, executor) - else new NioEventLoopGroup(numWorkers, executor) + if (useNativeEpoll() && Epoll.isAvailable) { + val group = new EpollEventLoopGroup(numWorkers, executor) + group.setIoRatio(ioRatio) + group + } else { + new NioEventLoopGroup(numWorkers, executor) + } eventLoopGroups.add(result) @@ -73,7 +78,10 @@ private object WorkerEventLoop { } lazy val Global: EventLoopGroup = make( - Executors.newCachedThreadPool(new BlockingTimeTrackingThreadFactory(mkNettyThreadFactory())), - numWorkers() + executor = Executors.newCachedThreadPool( + new BlockingTimeTrackingThreadFactory(mkNettyThreadFactory()) + ), + numWorkers = numWorkers(), + ioRatio = ioRatio() ) } diff --git a/finagle-netty4/src/main/scala/com/twitter/finagle/netty4/ioRatio.scala b/finagle-netty4/src/main/scala/com/twitter/finagle/netty4/ioRatio.scala new file mode 100644 index 0000000000..d20580f396 --- /dev/null +++ b/finagle-netty4/src/main/scala/com/twitter/finagle/netty4/ioRatio.scala @@ -0,0 +1,21 @@ +package com.twitter.finagle.netty4 + +import com.twitter.app.GlobalFlag + +/** + * Flag for defining the percentage of the desired amount of time spent for I/O in the child event + * loops. The default value is 50, which means the event loop will try to spend the same amount of + * time for I/O as for non-I/O tasks. + * + * {{ + * -com.twitter.finagle.netty4.ioRatio=50 + * }} + * + */ +object ioRatio + extends GlobalFlag( + 50, + "Sets the percentage of the desired amount of time spent for I/O in the child event loops. " + + "The default value is 50, which means the event loop will try to spend the same amount of " + + "time for I/O as for non-I/O tasks." + )