The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
To: David Howells <dhowells@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] pipe: wakeup writer only if pipe buffer is at least half empty
Date: Sun, 27 Oct 2019 18:46:20 +0300	[thread overview]
Message-ID: <157219118016.7078.16223055699799396042.stgit@buzz> (raw)

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().


             reply	other threads:[~2019-10-27 15:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-27 15:46 Konstantin Khlebnikov [this message]
2019-10-27 16:12 ` [PATCH] pipe: wakeup writer only if pipe buffer is at least half empty 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=157219118016.7078.16223055699799396042.stgit@buzz \
    --to=khlebnikov@yandex-team.ru \
    --cc=dhowells@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox