[PATCH] pipe: wakeup writer only if pipe buffer is at least half empty

From: Konstantin Khlebnikov
Date: Sun Oct 27 2019 - 11:46:39 EST


There is no reason to wakeup writer if pipe has only one empty page.
This means reader consumes data slower then writer produces it.

This patch waits until buffer is at least half empty before waking writer.
This lets him produce more data at once. In general, this change should
increase data batching and decrease rate of context switches.

perf stat bash -c 'seq 50000000 | wc' shows decreasing count of context
switches from 26k to 13k. Execution time stays the same.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@xxxxxxxxxxxxxx>
---
fs/pipe.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/fs/pipe.c b/fs/pipe.c
index 8a2ab2f974bd..14754c9095ce 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -324,7 +324,8 @@ pipe_read(struct kiocb *iocb, struct iov_iter *to)
curbuf = (curbuf + 1) & (pipe->buffers - 1);
pipe->curbuf = curbuf;
pipe->nrbufs = --bufs;
- do_wakeup = 1;
+ /* Wakeup writer if buffer is half empty */
+ do_wakeup = bufs <= pipe->buffers / 2;
}
total_len -= chars;
if (!total_len)
@@ -555,7 +556,9 @@ pipe_poll(struct file *filp, poll_table *wait)
}

if (filp->f_mode & FMODE_WRITE) {
- mask |= (nrbufs < pipe->buffers) ? EPOLLOUT | EPOLLWRNORM : 0;
+ /* Wakeup writer if buffer is half empty */
+ if (nrbufs <= pipe->buffers / 2)
+ mask |= EPOLLOUT | EPOLLWRNORM;
/*
* Most Unices do not set EPOLLERR for FIFOs but on Linux they
* behave exactly like pipes for poll().