The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] pipe: wakeup writer only if pipe buffer is at least half empty
@ 2019-10-27 15:46 Konstantin Khlebnikov
  2019-10-27 16:12 ` Linus Torvalds
  2019-11-04  8:47 ` [pipe] 975832d6ec: hackbench.throughput 15.8% improvement kernel test robot
  0 siblings, 2 replies; 7+ messages in thread
From: Konstantin Khlebnikov @ 2019-10-27 15:46 UTC (permalink / raw)
  To: David Howells, Peter Zijlstra, Linus Torvalds, linux-kernel

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@yandex-team.ru>
---
 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().


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-11-04  8:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-10-27 15:46 [PATCH] pipe: wakeup writer only if pipe buffer is at least half empty Konstantin Khlebnikov
2019-10-27 16:12 ` Linus Torvalds
2019-10-28  9:09   ` Konstantin Khlebnikov
2019-10-28  9:52     ` [PATCH] pipe: add fcntl for changing free space desired for write Konstantin Khlebnikov
2019-10-28 12:22     ` [PATCH] pipe: wakeup writer only if pipe buffer is at least half empty Linus Torvalds
2019-10-28 12:30       ` Konstantin Khlebnikov
2019-11-04  8:47 ` [pipe] 975832d6ec: hackbench.throughput 15.8% improvement kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox