From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 50AFD382E442; Fri, 9 Dec 2022 12:01:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 50AFD382E442 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670587313; bh=1+moQfRG5mlqUC5v2fN+M8gP7Yo5iE3LjmBwsQwjeSU=; h=From:To:Subject:Date:From; b=q2qyHufH1yPALOCRB35zw5U2akEUmiKhk+KHFBUZ4DbIb18QPvooT1fAcghFzquGO 6yrYmNIre907Ti4G27uYtlxW4fnMeyge3h47k9CmnOywKPFhl2z4KmOHB/c9WYC569 d8pWmQtTnKWMEERf55XoISRB8lfw6xQ/dKB6tkmE= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: pipe: Fix performance degradation for non-cygwin pipe. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 3a910b624a1bcd3d2ee625a308ba9f108a152fb7 X-Git-Newrev: be635ebc2f5df7f07ebc3f1f9531f38e3c09c970 Message-Id: <20221209120153.50AFD382E442@sourceware.org> Date: Fri, 9 Dec 2022 12:01:53 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dbe635ebc2f5= df7f07ebc3f1f9531f38e3c09c970 commit be635ebc2f5df7f07ebc3f1f9531f38e3c09c970 Author: Takashi Yano Date: Thu Dec 8 22:02:43 2022 +0900 Cygwin: pipe: Fix performance degradation for non-cygwin pipe. =20 https://cygwin.com/pipermail/cygwin/2022-December/252628.html =20 After the commit 9e4d308cd592, the performance of read from non-cygwin pipe has been degraded. This is because select_sem mechanism does not work for non-cygwin pipe. This patch fixes the issue. =20 Fixes: 9e4d308cd592 ("Cygwin: pipe: Adopt FILE_SYNCHRONOUS_IO_NONALERT flag for read pipe.") Reported-by: tryandbuy Reviewed-by: Corinna Vinschen Signed-off-by: Takashi Yano Diff: --- winsup/cygwin/fhandler/pipe.cc | 16 +++++++++++++++- winsup/cygwin/release/3.4.1 | 3 +++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc index 94b85c349..e72ee65ca 100644 --- a/winsup/cygwin/fhandler/pipe.cc +++ b/winsup/cygwin/fhandler/pipe.cc @@ -281,6 +281,8 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) size_t nbytes =3D 0; NTSTATUS status =3D STATUS_SUCCESS; IO_STATUS_BLOCK io; + ULONGLONG t0 =3D GetTickCount64 (); /* Init timer */ + const ULONGLONG t0_threshold =3D 20; =20 if (!len) return; @@ -312,6 +314,7 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) { ULONG_PTR nbytes_now =3D 0; ULONG len1 =3D (ULONG) (len - nbytes); + DWORD select_sem_timeout =3D 0; =20 FILE_PIPE_LOCAL_INFORMATION fpli; status =3D NtQueryInformationFile (get_handle (), &io, @@ -358,7 +361,18 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) nbytes =3D (size_t) -1; break; } - waitret =3D cygwait (select_sem, 1); + /* If the pipe is a non-cygwin pipe, select_sem trick + does not work. As a result, the following cygwait() + will return only after timeout occurs. This causes + performance degradation. However, setting timeout + to zero causes high CPU load. So, set timeout to + non-zero only when select_sem is valid or pipe is + not ready to read for more than t0_threshold. + This prevents both the performance degradation and + the high CPU load. */ + if (select_sem || GetTickCount64 () - t0 > t0_threshold) + select_sem_timeout =3D 1; + waitret =3D cygwait (select_sem, select_sem_timeout); if (waitret =3D=3D WAIT_CANCELED) pthread::static_cancel_self (); else if (waitret =3D=3D WAIT_SIGNALED) diff --git a/winsup/cygwin/release/3.4.1 b/winsup/cygwin/release/3.4.1 index 226329d9c..9a831eda5 100644 --- a/winsup/cygwin/release/3.4.1 +++ b/winsup/cygwin/release/3.4.1 @@ -10,3 +10,6 @@ Bug Fixes compiled under Cygwin 3.4.0 having a public facing interface using FILE need to be recompiled. Addresses: https://savannah.gnu.org/bugs/index.php?63480 + +- Fix performance degradation of non-cygwin pipe. + Addresses: https://cygwin.com/pipermail/cygwin/2022-December/252628.html