public inbox for cygwin-developers@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin-developers@cygwin.com
Subject: Re: 3.3.0: Possible regression in cygwin DLL (Win10); fixed in snapshot
Date: Thu, 11 Nov 2021 22:20:56 +0900	[thread overview]
Message-ID: <20211111222056.c0ed3ac2fec60b6ff0be8085@nifty.ne.jp> (raw)
In-Reply-To: <20211111210234.6a43a2ba237cef11ecc7016b@nifty.ne.jp>

[-- Attachment #1: Type: text/plain, Size: 1843 bytes --]

On Thu, 11 Nov 2021 21:02:34 +0900
Takashi Yano wrote:
> On Thu, 11 Nov 2021 12:33:21 +0100
> Corinna Vinschen wrote:
> > On Nov 11 20:12, Takashi Yano wrote:
> > > > [FileProcessIdsUsingFileInformation]
> > > 
> > > Thanks for advice. get_query_hdl_per_* is called in the write side,
> > > so only knows pipe handle of the write side. We would like to know
> > > ProcessId which have pipe handle of the read side. Can we use
> > > FileProcessIdsUsingFileInformation for this perpose?
> > 
> > I don't know.  I just stumbled over it yesterday and I thought it might
> > be something we could utilize.  Supposedly it returns PIDs for processes
> > having that file open, but how this works for pipe read/write sides
> > needs testing.  Of course, knowing how Windows functions usually only go
> > half the way, the function will either only return the current side of
> > the pipe, or even an error code :-/  Never mind, it was just an idea.
> 
> I have tested the behaviour of FileProcessIdsUsingFileInformation
> just now. We can use it! I will try to use it in get_query_hdl().

I have tried to utilize FileProcessIdsUsingFileInformation in
get_query_hdl_per_process() as the patch attached. It works as
expected.

I also measured the response of select() using these functions.
The first time and second time responses are measured. The second
time should be much faster than the first time because search
result has been cached.

First time, Second time
 4.620400 [msec], 0.102300 [msec] << get_query_hdl_per_process()
19.080400 [msec], 0.199000 [msec] << get_query_hdl_per_system()
14.364300 [msec], 0.156800 [msec] << FileProcessIdsUsingFileInformation

Unfortunately, FileProcessIdsUsingFileInformation is slower than
current get_query_hdl_per_process(). It takes about 3 times longer
time.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

[-- Attachment #2: fpiufi.patch --]
[-- Type: application/octet-stream, Size: 2977 bytes --]

diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 9392a28c1..83c09d7c6 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -1201,50 +1201,32 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
   NTSTATUS status;
   ULONG len;
   DWORD n_process = 256;
-  PSYSTEM_PROCESS_INFORMATION spi;
+  FILE_PROCESS_IDS_USING_FILE_INFORMATION *fpiufi;
   do
-    { /* Enumerate processes */
-      DWORD nbytes = n_process * sizeof (SYSTEM_PROCESS_INFORMATION);
-      spi = (PSYSTEM_PROCESS_INFORMATION) HeapAlloc (GetProcessHeap (),
-						     0, nbytes);
-      if (!spi)
+    { /* Enumerate processes having the pipe handle. */
+      IO_STATUS_BLOCK io;
+      DWORD nbytes = n_process * sizeof (ULONG)
+	+ sizeof (FILE_PROCESS_IDS_USING_FILE_INFORMATION);
+      fpiufi = (FILE_PROCESS_IDS_USING_FILE_INFORMATION *)
+	HeapAlloc (GetProcessHeap (), 0, nbytes);
+      if (!fpiufi)
 	return NULL;
-      status = NtQuerySystemInformation (SystemProcessInformation,
-					 spi, nbytes, &len);
+      status = NtQueryInformationFile (get_handle (), &io, fpiufi, nbytes,
+				       FileProcessIdsUsingFileInformation);
       if (NT_SUCCESS (status))
 	break;
-      HeapFree (GetProcessHeap (), 0, spi);
+      HeapFree (GetProcessHeap (), 0, fpiufi);
       n_process *= 2;
     }
   while (n_process < (1L<<20) && status == STATUS_INFO_LENGTH_MISMATCH);
   if (!NT_SUCCESS (status))
     return NULL;
 
-  /* In most cases, it is faster to check the processes in reverse order.
-     To do this, store PIDs into an array. */
-  DWORD *proc_pids = (DWORD *) HeapAlloc (GetProcessHeap (), 0,
-					  n_process * sizeof (DWORD));
-  if (!proc_pids)
-    {
-      HeapFree (GetProcessHeap (), 0, spi);
-      return NULL;
-    }
-  PSYSTEM_PROCESS_INFORMATION p = spi;
-  n_process = 0;
-  while (true)
-    {
-      proc_pids[n_process++] = (DWORD)(intptr_t) p->UniqueProcessId;
-      if (!p->NextEntryOffset)
-	break;
-      p = (PSYSTEM_PROCESS_INFORMATION) ((char *) p + p->NextEntryOffset);
-    }
-  HeapFree (GetProcessHeap (), 0, spi);
-
-  for (LONG i = (LONG) n_process - 1; i >= 0; i--)
+  for (ULONG i = 0; i < fpiufi->NumberOfProcessIdsInList; i++)
     {
       HANDLE proc = OpenProcess (PROCESS_DUP_HANDLE
 				 | PROCESS_QUERY_INFORMATION,
-				 0, proc_pids[i]);
+				 0, fpiufi->ProcessIdList[i]);
       if (!proc)
 	continue;
 
@@ -1298,7 +1280,7 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
 	      query_hdl_proc = proc;
 	      query_hdl_value = (HANDLE)(intptr_t) phi->Handles[j].HandleValue;
 	      HeapFree (GetProcessHeap (), 0, phi);
-	      HeapFree (GetProcessHeap (), 0, proc_pids);
+	      HeapFree (GetProcessHeap (), 0, fpiufi);
 	      return h;
 	    }
 close_handle:
@@ -1308,7 +1290,7 @@ close_handle:
 close_proc:
       CloseHandle (proc);
     }
-  HeapFree (GetProcessHeap (), 0, proc_pids);
+  HeapFree (GetProcessHeap (), 0, fpiufi);
   return NULL;
 }
 

  reply	other threads:[~2021-11-11 13:21 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAEv6GOB8PXHgHoz7hdJy6Bia2GWEmUDnTd252gTGinz2vuv=hA@mail.gmail.com>
     [not found] ` <20211105123950.b118a7f2ba38379764df4c12@nifty.ne.jp>
     [not found]   ` <CAEv6GOA-y58YrftXgEgFrjqtOTHmfdu2Vrq76Lwn0suZpZ=U9w@mail.gmail.com>
     [not found]     ` <20211105170542.96ce6dd4ca32880ddfddd660@nifty.ne.jp>
     [not found]       ` <CAEv6GODiM88Xfhk9R3AcEW6UTYSzACzYe4C0gPoTYm=u9ZTqRQ@mail.gmail.com>
     [not found]         ` <20211106044116.698b465a5d8ed6ce2cc75c99@nifty.ne.jp>
     [not found]           ` <2cfa5de7-3b95-9062-4572-f36d304bc916@cornell.edu>
2021-11-06  6:10             ` Takashi Yano
2021-11-06 11:42               ` Corinna Vinschen
2021-11-06 12:02                 ` Corinna Vinschen
2021-11-06 14:13                   ` Takashi Yano
2021-11-06 17:20                     ` Corinna Vinschen
2021-11-07  3:01                       ` Takashi Yano
2021-11-06 16:38                 ` Ken Brown
2021-11-06 17:20                   ` Corinna Vinschen
2021-11-07  3:46                   ` Takashi Yano
2021-11-07 22:20                     ` Ken Brown
2021-11-08  8:23                       ` Takashi Yano
2021-11-08  9:46                         ` Corinna Vinschen
2021-11-10  8:30                 ` Takashi Yano
2021-11-10 10:34                   ` Corinna Vinschen
2021-11-10 13:30                     ` Takashi Yano
2021-11-10 20:35                       ` Corinna Vinschen
2021-11-10 21:32                         ` Ken Brown
2021-11-11 16:11                           ` Ken Brown
2021-11-12  8:38                             ` Takashi Yano
2021-11-16 23:46                             ` Takashi Yano
2021-11-17  8:10                               ` Takashi Yano
2021-11-17 15:12                                 ` Ken Brown
2021-11-11  9:52                         ` Corinna Vinschen
2021-11-11 11:12                           ` Takashi Yano
2021-11-11 11:33                             ` Corinna Vinschen
2021-11-11 12:02                               ` Takashi Yano
2021-11-11 13:20                                 ` Takashi Yano [this message]
2021-11-11 16:07                                   ` Corinna Vinschen
2021-11-12  8:33                             ` Takashi Yano
2021-11-12 10:02                               ` Corinna Vinschen
2021-12-12 13:26                                 ` Takashi Yano
2021-12-12 13:36                                   ` Ken Brown
2021-12-13 11:15                                     ` Takashi Yano

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=20211111222056.c0ed3ac2fec60b6ff0be8085@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin-developers@cygwin.com \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).