Patch - Avoid extra system traps

Trond Norbye Trond.Norbye at Sun.COM
Thu Dec 20 11:16:15 UTC 2007


Hi.

Here is a patch for memcached fixing the following two issues:

1. Replace include of sys/signal.h with signal.h (according to C99  
this file contains the prototypes for signal etc).

2.  The previous implementation of try_read_network will try to read  
from the network until it read returns with an error code.  The new  
implementation will stop reading if read returns less bytes than we  
had room for in our buffer.  I assume that the OS will return all of  
the available data instead of forcing the program to issue multiple  
read operations. If the OS for some odd reason decide to return less  
than the data that is available, this socket will be signaled the next  
time we poll libevent for a new event.

Thanks,

Trond Norbye


Index: memcached.c
===================================================================
--- memcached.c	(revision 687)
+++ memcached.c	(working copy)
@@ -19,9 +19,9 @@
  #include <sys/stat.h>
  #include <sys/socket.h>
  #include <sys/un.h>
-#include <sys/signal.h>
  #include <sys/resource.h>
  #include <sys/uio.h>
+#include <signal.h>

  /* some POSIX systems need the following definition
   * to get mlockall flags out of sys/mman.h.  */
@@ -1918,14 +1918,19 @@
              c->request_addr_size = 0;
          }

-        res = read(c->sfd, c->rbuf + c->rbytes, c->rsize - c->rbytes);
+        int avail = c->rsize - c->rbytes;
+        res = read(c->sfd, c->rbuf + c->rbytes, avail);
          if (res > 0) {
              STATS_LOCK();
              stats.bytes_read += res;
              STATS_UNLOCK();
              gotdata = 1;
              c->rbytes += res;
-            continue;
+            if (res == avail) {
+                continue;
+            } else {
+                break;
+            }
          }
          if (res == 0) {
              /* connection closed */



More information about the memcached mailing list