httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chia-liang Kao <cl...@CirX.ORG>
Subject [PATCH] economic ap_poll and related
Date Wed, 26 Apr 2000 07:09:47 GMT
Hi,

I noticed that whenever ap_poll is invoked, the pollset is always 
re-allocated and filled.  So in a typical server that polls again 
and again in the main loop, you'll have to do ap_setup_poll and add 
every socket in it in every loop. I also noticed that 
ap_remove_poll_socket only clears the event mask but not entirely remove 
the socket from pollfd..

The attached patch makes use of the pollfd to store required info (this
also reduce memory usage overhead). And also rename the original 
ap_remove_poll_socket to ap_mask_poll_socket(if this is still useful), 
and add the real ap_remove_poll_socket function that just remove the
specified fd from the pollfd. Also, if adding too many sockets in the
pollfd, ap_add_poll_socket would return ENOMEM;

I also found that since ap_socket_t and ap_file_t are different, ap_poll
only supports sockets. Is there any plan to add ap_file_t support?
This should be fairly easy because the ap_poll* functions only make use
of socketdes field of ap_socket. 

Cheers,
CLK

Index: include/apr_network_io.h
===================================================================
RCS file: /home/apache/REPOS/apache-2.0/src/lib/apr/include/apr_network_io.h,v
retrieving revision 1.31
diff -u -r1.31 apr_network_io.h
--- include/apr_network_io.h	2000/04/16 16:59:37	1.31
+++ include/apr_network_io.h	2000/04/26 06:51:39
@@ -618,20 +618,33 @@
 
 /*
 
-=head1 ap_status_t ap_remove_poll_socket(ap_pollfd_t *aprset, ap_socket_t *sock, ap_int16_t
events)
+=head1 ap_status_t ap_mask_poll_socket(ap_pollfd_t *aprset, ap_socket_t *sock, ap_int16_t
events)
 
-B<Add a socket to the poll structure.> 
+B<Modfify a socket in the poll structure with mask.> 
 
     arg 1) The poll structure we will be using. 
-    arg 2) The socket to remove from the current poll structure. 
+    arg 2) The socket to modify in poll structure. 
     arg 3) The events to stop looking for during the poll.  One of:
               APR_POLLIN    -- signal if read will not block
               APR_POLLPRI   -- signal if prioirty data is availble to be read
               APR_POLLOUT   -- signal if write will not block
 
 =cut
+ */
+ap_status_t ap_mask_poll_socket(ap_pollfd_t *aprset, ap_socket_t *sock,
+                                  ap_int16_t events);
+/*
+
+=head1 ap_status_t ap_remove_poll_socket(ap_pollfd_t *aprset, ap_socket_t *sock)
+
+B<Remove a socket from the poll structure.> 
+
+    arg 1) The poll structure we will be using. 
+    arg 2) The socket to remove from the current poll structure. 
+
+=cut
  */
-ap_status_t ap_remove_poll_socket(ap_pollfd_t *aprset, ap_socket_t *sock,
+ap_status_t ap_mask_poll_socket(ap_pollfd_t *aprset, ap_socket_t *sock,
                                   ap_int16_t events);
 
 /*
Index: network_io/unix/networkio.h
===================================================================
RCS file: /home/apache/REPOS/apache-2.0/src/lib/apr/network_io/unix/networkio.h,v
retrieving revision 1.24
diff -u -r1.24 networkio.h
--- network_io/unix/networkio.h	2000/04/22 06:16:43	1.24
+++ network_io/unix/networkio.h	2000/04/24 05:27:57
@@ -121,7 +121,8 @@
 struct ap_pollfd_t {
     ap_pool_t *cntxt;
 #ifdef HAVE_POLL
-    int *sock;
+    struct pollfd *pollset;
+    int num;
     int curpos;
 #else
     fd_set *read;
Index: network_io/unix/poll.c
===================================================================
RCS file: /home/apache/REPOS/apache-2.0/src/lib/apr/network_io/unix/poll.c,v
retrieving revision 1.26
diff -u -r1.26 poll.c
--- network_io/unix/poll.c	2000/04/19 22:32:07	1.26
+++ network_io/unix/poll.c	2000/04/26 06:50:43
@@ -59,10 +59,10 @@
 ap_status_t ap_setup_poll(ap_pollfd_t **new, ap_int32_t num, ap_pool_t *cont)
 {
     (*new) = (ap_pollfd_t *)ap_palloc(cont, sizeof(ap_pollfd_t));
-    (*new)->sock = ap_palloc(cont, sizeof(ap_socket_t) * num);
-    (*new)->events = ap_palloc(cont, sizeof(ap_int16_t) * num);
-    (*new)->revents = ap_palloc(cont, sizeof(ap_int16_t) * num);
+    (*new)->pollset = (struct pollfd *)ap_palloc(cont, 
+                                         sizeof(struct pollfd) * num);
 
+    (*new)->num = num;
     if ((*new) == NULL) {
         return APR_ENOMEM;
     }
@@ -116,14 +116,17 @@
 {
     int i = 0;
     
-    while (i < aprset->curpos && aprset->sock[i] != sock->socketdes)
{
+    while (i < aprset->curpos && aprset->pollset[i].fd != sock->socketdes)
{
         i++;
     }
     if (i >= aprset->curpos) {
+	if(aprset->curpos == aprset->num) {
+	    return APR_ENOMEM;
+	}
         aprset->curpos++;
     } 
-    aprset->sock[i] = sock->socketdes;
-    aprset->events[i] = get_event(event);
+    aprset->pollset[i].fd = sock->socketdes;
+    aprset->pollset[i].events = get_event(event);
 
     return APR_SUCCESS;
 }
@@ -132,28 +135,15 @@
 		    ap_interval_time_t timeout)
 {
     int i;
-    struct pollfd *pollset;
     int rv;
 
-    pollset = (struct pollfd *)ap_palloc(aprset->cntxt, 
-                                         sizeof(struct pollfd) * aprset->curpos);
-
-    for (i = 0; i < aprset->curpos; i++) {
-        pollset[i].fd = aprset->sock[i];
-        pollset[i].events = aprset->events[i];
-    }
-
     if (timeout > 0) {
         timeout /= 1000; /* convert microseconds to milliseconds */
     }
 
-    rv = poll(pollset, aprset->curpos, timeout);
+    rv = poll(aprset->pollset, aprset->curpos, timeout);
     (*nsds) = rv;
     
-    for (i = 0; i < aprset->curpos; i++) {
-        aprset->revents[i] = get_revent(pollset[i].revents);
-    }
-    
     if ((*nsds) < 0) {
         return errno;
     }
@@ -164,23 +154,23 @@
 {
     int i = 0;
     
-    while (i < aprset->curpos && aprset->sock[i] != sock->socketdes)
{
+    while (i < aprset->curpos && aprset->pollset[i].fd != sock->socketdes)
{
         i++;
     }
     if (i >= aprset->curpos) {
         return APR_EINVALSOCK;
     } 
-    (*event) = aprset->revents[i];
+    (*event) = aprset->pollset[i].revents;
     return APR_SUCCESS;
 }
 
-ap_status_t ap_remove_poll_socket(ap_pollfd_t *aprset, 
+ap_status_t ap_mask_poll_socket(ap_pollfd_t *aprset, 
                                   ap_socket_t *sock, ap_int16_t events)
 {
     ap_int16_t newevents;
     int i = 0;
     
-    while (i < aprset->curpos && aprset->sock[i] != sock->socketdes)
{
+    while (i < aprset->curpos && aprset->pollset[i].fd != sock->socketdes)
{
         i++;
     }
     if (i >= aprset->curpos) {
@@ -191,6 +181,23 @@
         aprset->events[i] ^= newevents;
     }
 
+    return APR_SUCCESS;
+}
+
+ap_status_t ap_remove_poll_socket(ap_pollfd_t *aprset, ap_socket_t *sock)
+{
+    int i = 0;
+    while(i < aprset->curpos && aprset->pollset[i].fd != sock->socketdes)
{
+	i++;
+    }
+    if(i >= aprset->curpos) {
+	return APR_NOTFOUND;
+    }
+    while(++i < aprset->curpos) {
+	aprset->pollset[i-1].fd = aprset->pollset[i].fd;
+	aprset->pollset[i-1].events = aprset->pollset[i].events;
+    }
+    --aprset->curpos;
     return APR_SUCCESS;
 }
 


Mime
View raw message