unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: storm@cua.dk (Kim F. Storm)
Cc: Emacs Devel <emacs-devel@gnu.org>
Subject: Re: 100% CPU on TCP servers (on Windoze).
Date: Fri, 14 Jul 2006 15:24:23 +0200	[thread overview]
Message-ID: <m3irm01fqw.fsf@kfs-l.imdomain.dk> (raw)
In-Reply-To: <f7ccd24b0607140527o7bd34c99y67ec85ff93c783be@mail.gmail.com> (Juanma Barranquero's message of "Fri, 14 Jul 2006 14:27:27 +0200")

"Juanma Barranquero" <lekktu@gmail.com> writes:

> On 7/14/06, Kim F. Storm <storm@cua.dk> wrote:
>
>> Any idea how to fix that?
>
> You can make it work with the attached patch. Apparently,
> WSAEventSelect is exported from ws2_32.dll but not from wsock32.dll. I
> don't know whether all targets (I mean, all supported Windows
> environments) have or use ws2_32.dll, though.
>
> Unfortunately, with your patch and the attached one, I still see a 50%
> CPU use on the reported example.

Thanks.  Here is something which actually seems to work -- provided
we can use ws2_32.dll:

Index: w32.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/w32.c,v
retrieving revision 1.103
diff -c -r1.103 w32.c
*** w32.c	14 Jul 2006 09:29:32 -0000	1.103
--- w32.c	14 Jul 2006 13:25:07 -0000
***************
*** 2701,2706 ****
--- 2701,2708 ----
  void (PASCAL *pfn_WSASetLastError) (int iError);
  int (PASCAL *pfn_WSAGetLastError) (void);
  int (PASCAL *pfn_WSAEventSelect) (SOCKET s, HANDLE hEventObject, long lNetworkEvents);
+ HANDLE (PASCAL *pfn_WSACreateEvent) (void);
+ int (PASCAL *pfn_WSACloseEvent) (HANDLE hEvent);
  int (PASCAL *pfn_socket) (int af, int type, int protocol);
  int (PASCAL *pfn_bind) (SOCKET s, const struct sockaddr *addr, int namelen);
  int (PASCAL *pfn_connect) (SOCKET s, const struct sockaddr *addr, int namelen);
***************
*** 2770,2776 ****
      = (void *) GetProcAddress (GetModuleHandle ("kernel32.dll"),
  			       "SetHandleInformation");
  
!   winsock_lib = LoadLibrary ("wsock32.dll");
  
    if (winsock_lib != NULL)
      {
--- 2772,2778 ----
      = (void *) GetProcAddress (GetModuleHandle ("kernel32.dll"),
  			       "SetHandleInformation");
  
!   winsock_lib = LoadLibrary ("Ws2_32.dll");
  
    if (winsock_lib != NULL)
      {
***************
*** 2784,2789 ****
--- 2786,2793 ----
        LOAD_PROC( WSASetLastError );
        LOAD_PROC( WSAGetLastError );
        LOAD_PROC( WSAEventSelect );
+       LOAD_PROC( WSACreateEvent );
+       LOAD_PROC( WSACloseEvent );
        LOAD_PROC( socket );
        LOAD_PROC( bind );
        LOAD_PROC( connect );
***************
*** 3298,3307 ****
        if (rc == SOCKET_ERROR)
  	set_errno ();
        else
! 	{
! 	  fd_info[s].flags |= FILE_LISTEN;
! 	  pfn_WSAEventSelect (SOCK_HANDLE (s), fd_info[s].cp->char_avail, FD_ACCEPT);
! 	}
        return rc;
      }
    h_errno = ENOTSOCK;
--- 3302,3308 ----
        if (rc == SOCKET_ERROR)
  	set_errno ();
        else
! 	fd_info[s].flags |= FILE_LISTEN;
        return rc;
      }
    h_errno = ENOTSOCK;
***************
*** 3342,3357 ****
    if (fd_info[s].flags & FILE_LISTEN)
      {
        SOCKET t = pfn_accept (SOCK_HANDLE (s), addr, addrlen);
!       if (t != INVALID_SOCKET)
! 	{
! 	  int fd = socket_to_fd (t);
! 	  if (fd >= 0)
! 	    pfn_WSAEventSelect (SOCK_HANDLE (fd), fd_info[fd].cp->char_avail, FD_READ | FD_CLOSE);
! 	  return fd;
! 	}
  
!       set_errno ();
!       return -1;
      }
    h_errno = ENOTSOCK;
    return -1;
--- 3343,3357 ----
    if (fd_info[s].flags & FILE_LISTEN)
      {
        SOCKET t = pfn_accept (SOCK_HANDLE (s), addr, addrlen);
!       int fd = -1;
!       if (t == INVALID_SOCKET)
! 	set_errno ();
!       else
! 	fd = socket_to_fd (t);
  
!       fd_info[s].cp->status = STATUS_READ_ACKNOWLEDGED;
!       ResetEvent (fd_info[s].cp->char_avail);
!       return fd;
      }
    h_errno = ENOTSOCK;
    return -1;
***************
*** 3653,3658 ****
--- 3653,3688 ----
    return cp->status;
  }
  
+ int _sys_wait_accept (int fd)
+ {
+   HANDLE hEv;
+   child_process * cp;
+   int rc;
+ 
+   if (fd < 0 || fd >= MAXDESC)
+     return STATUS_READ_ERROR;
+ 
+   cp = fd_info[fd].cp;
+ 
+   if (cp == NULL || cp->fd != fd || cp->status != STATUS_READ_READY)
+     return STATUS_READ_ERROR;
+ 
+   cp->status = STATUS_READ_FAILED;
+ 
+   hEv = pfn_WSACreateEvent ();
+   rc = pfn_WSAEventSelect (SOCK_HANDLE (fd), hEv, FD_ACCEPT);
+   if (rc != SOCKET_ERROR)
+     {
+       rc = WaitForSingleObject (hEv, INFINITE);
+       pfn_WSAEventSelect (SOCK_HANDLE (fd), NULL, 0);
+       pfn_WSACloseEvent (hEv);
+       if (rc == WAIT_OBJECT_0)
+ 	cp->status = STATUS_READ_SUCCEEDED;
+     }
+ 
+   return cp->status;
+ }
+ 
  int
  sys_read (int fd, char * buffer, unsigned int count)
  {
Index: w32.h
===================================================================
RCS file: /cvsroot/emacs/emacs/src/w32.h,v
retrieving revision 1.20
diff -c -r1.20 w32.h
*** w32.h	14 Jul 2006 09:29:22 -0000	1.20
--- w32.h	14 Jul 2006 13:24:48 -0000
***************
*** 137,142 ****
--- 137,145 ----
  extern void globals_of_w32menu (void);
  extern void syms_of_fontset (void);
  
+ extern int _sys_read_ahead (int fd);
+ extern int _sys_wait_accept (int fd);
+ 
  #endif /* EMACS_W32_H */
  
  /* arch-tag: 02c36b00-312b-4c4d-a1d9-f905c5e968f0
Index: w32proc.c
===================================================================
RCS file: /cvsroot/emacs/emacs/src/w32proc.c,v
retrieving revision 1.66
diff -c -r1.66 w32proc.c
*** w32proc.c	6 Feb 2006 15:23:22 -0000	1.66
--- w32proc.c	14 Jul 2006 13:23:28 -0000
***************
*** 1,6 ****
  /* Process support for GNU Emacs on the Microsoft W32 API.
     Copyright (C) 1992, 1995, 1999, 2000, 2001, 2002, 2003, 2004,
!                  2005, 2006 Free Software Foundation, Inc.
  
  This file is part of GNU Emacs.
  
--- 1,6 ----
  /* Process support for GNU Emacs on the Microsoft W32 API.
     Copyright (C) 1992, 1995, 1999, 2000, 2001, 2002, 2003, 2004,
! 		 2005, 2006 Free Software Foundation, Inc.
  
  This file is part of GNU Emacs.
  
***************
*** 280,286 ****
      {
        int rc;
  
!       rc = _sys_read_ahead (cp->fd);
  
        /* The name char_avail is a misnomer - it really just means the
  	 read-ahead has completed, whether successfully or not. */
--- 280,289 ----
      {
        int rc;
  
!       if (fd_info[cp->fd].flags & FILE_LISTEN)
! 	rc = _sys_wait_accept (cp->fd);
!       else
! 	rc = _sys_read_ahead (cp->fd);
  
        /* The name char_avail is a misnomer - it really just means the
  	 read-ahead has completed, whether successfully or not. */

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

  parent reply	other threads:[~2006-07-14 13:24 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-06 18:38 100% CPU on TCP servers Juanma Barranquero
2005-08-18 15:51 ` Juanma Barranquero
2005-09-09 12:53   ` Kim F. Storm
2005-09-09 15:25     ` Juanma Barranquero
2005-09-10  7:47   ` Jason Rumney
2005-09-10 23:01     ` Kim F. Storm
2005-10-12 15:07     ` Kim F. Storm
2005-10-12 22:42       ` Juanma Barranquero
2006-07-13 22:35   ` 100% CPU on TCP servers (on Windoze) Kim F. Storm
2006-07-14  8:15     ` Jason Rumney
2006-07-14  9:54       ` Kim F. Storm
2006-07-14 10:54         ` Juanma Barranquero
2006-07-14  9:51     ` Jason Rumney
2006-07-14 10:23       ` Kim F. Storm
2006-07-14 10:43         ` Jason Rumney
2006-07-14 11:14           ` Eli Zaretskii
2006-07-14 10:50         ` Juanma Barranquero
2006-07-14 11:16           ` Kim F. Storm
2006-07-14 11:42             ` Kim F. Storm
2006-07-14 12:27               ` Juanma Barranquero
2006-07-14 13:08                 ` Eli Zaretskii
2006-07-14 13:59                   ` Jason Rumney
2006-07-14 14:22                     ` Kim F. Storm
2006-07-14 14:41                       ` Jason Rumney
2006-07-14 17:29                       ` Eli Zaretskii
2006-07-14 18:12                         ` Stefan Monnier
2006-07-14 15:43                     ` Stuart D. Herring
2006-07-14 13:24                 ` Kim F. Storm [this message]
2006-07-14 13:40                   ` Eli Zaretskii
2006-07-14 14:04                     ` Kim F. Storm
2006-07-14 15:28                   ` Juanma Barranquero
2006-07-14 22:07                     ` Kim F. Storm
2006-07-14 23:23                       ` Juanma Barranquero
2006-07-15  0:50                         ` Kim F. Storm
2006-07-15  2:09                           ` Juanma Barranquero
2006-07-15  9:05                           ` Eli Zaretskii
2006-07-15 13:54                             ` Juanma Barranquero
2006-07-15 14:59                               ` Stefan Monnier
2006-07-15 15:30                                 ` Juanma Barranquero
2006-07-15 20:38                                   ` Eli Zaretskii
2006-07-15 22:07                                     ` Stefan Monnier
2006-07-15 17:16                             ` Richard Stallman
2006-07-15  2:04                         ` Stefan Monnier
2006-07-15  2:11                           ` Juanma Barranquero
2006-07-15 11:52                             ` Stefan Monnier
2006-07-15 13:50                               ` Juanma Barranquero
2006-07-15 14:58                                 ` Stefan Monnier
2006-07-15 15:24                                   ` Juanma Barranquero
2006-07-15 22:02                                     ` Stefan Monnier

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=m3irm01fqw.fsf@kfs-l.imdomain.dk \
    --to=storm@cua.dk \
    --cc=emacs-devel@gnu.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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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