[memcached] kronuz, r443: Synced with 1.2.0 in the SVN

commits at code.sixapart.com commits at code.sixapart.com
Thu Nov 23 19:13:06 UTC 2006


Synced with 1.2.0 in the SVN

U   branches/server-win32/ChangeLog
U   branches/server-win32/Win32-Code/config.h
U   branches/server-win32/assoc.c
U   branches/server-win32/configure.ac
U   branches/server-win32/items.c
U   branches/server-win32/memcached.c
U   branches/server-win32/memcached.h
U   branches/server-win32/slabs.c
U   branches/server-win32/t/flush-all.t
U   branches/server-win32/t/stress-memcached.pl


Modified: branches/server-win32/ChangeLog
===================================================================
--- branches/server-win32/ChangeLog	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/ChangeLog	2006-11-23 19:12:58 UTC (rev 443)
@@ -1,3 +1,21 @@
+2006-11-13
+	* Iain Wade <iwade at optusnet.com.au>: Fix for UDP responses on non-"get"
+	 commands.
+
+2006-10-13
+	* Steven Grimm <sgrimm at facebook.com>: New faster hash function.
+
+2006-09-20
+
+	* don't listen on UDP by default; more clear message when UDP port in use
+
+2006-09-09
+	* release 1.2.0 (along with 1.1.13, which is the more tested branch)
+
+	nobody has run 1.2.0 in production, to my knowledge.  facebook has run
+	their pre-merge-with-trunk version, but bugs were discovered (and fixed)
+	after the merge.  there might be more.  you've been warned.  :)
+
 2006-09-04
 	* improved autoconf libevent detection, from the Tor project.
 

Modified: branches/server-win32/Win32-Code/config.h
===================================================================
--- branches/server-win32/Win32-Code/config.h	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/Win32-Code/config.h	2006-11-23 19:12:58 UTC (rev 443)
@@ -8,19 +8,19 @@
 #define PACKAGE_NAME "memcached Server"
 
 /* Define to the full name and version of this package. */
-#define PACKAGE_STRING "memcached 1.2.0-rc1"
+#define PACKAGE_STRING "memcached 1.2.0"
 
 /* Define to the full name and version of this package. */
-#define PACKAGE_DESCRIPTION "memcached 1.2.0-rc1 is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Win32 port by Kronuz."
+#define PACKAGE_DESCRIPTION "memcached 1.2.0 is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Win32 port by Kronuz."
 
 /* Define to the one symbol short name of this package. */
 #define PACKAGE_TARNAME "memcached"
 
 /* Define to the version of this package. */
-#define PACKAGE_VERSION "1.2.0-rc1"
+#define PACKAGE_VERSION "1.2.0"
 
 /* Define to 1 if you have the ANSI C header files. */
 #define STDC_HEADERS 1
 
 /* Version number of package */
-#define VERSION "1.2.0-rc1"
+#define VERSION "1.2.0"

Modified: branches/server-win32/assoc.c
===================================================================
--- branches/server-win32/assoc.c	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/assoc.c	2006-11-23 19:12:58 UTC (rev 443)
@@ -15,6 +15,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #ifndef WIN32
+#include "config.h"
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <sys/signal.h>
@@ -24,6 +25,7 @@
 #include <netinet/in.h>
 #include <errno.h>
 #else
+#include "Win32-Code/config.h"
 #include <Winsock2.h>
 #endif
 #include <stdlib.h>
@@ -43,91 +45,424 @@
 #define hashsize(n) ((ub4)1<<(n))
 #define hashmask(n) (hashsize(n)-1)
 
+/*
+ * Since the hash function does bit manipulation, it needs to know
+ * whether it's big or little-endian. ENDIAN_LITTLE and ENDIAN_BIG
+ * are set in the configure script.
+ */
+#if ENDIAN_BIG == 1
+# define HASH_LITTLE_ENDIAN 0
+# define HASH_BIG_ENDIAN 1
+#else
+# if ENDIAN_LITTLE == 1
+#  define HASH_LITTLE_ENDIAN 1
+#  define HASH_BIG_ENDIAN 0
+# else
+#  define HASH_LITTLE_ENDIAN 0
+#  define HASH_BIG_ENDIAN 0
+# endif
+#endif
+
+#define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
+
+/*
+-------------------------------------------------------------------------------
+mix -- mix 3 32-bit values reversibly.
+
+This is reversible, so any information in (a,b,c) before mix() is
+still in (a,b,c) after mix().
+
+If four pairs of (a,b,c) inputs are run through mix(), or through
+mix() in reverse, there are at least 32 bits of the output that
+are sometimes the same for one pair and different for another pair.
+This was tested for:
+* pairs that differed by one bit, by two bits, in any combination
+  of top bits of (a,b,c), or in any combination of bottom bits of
+  (a,b,c).
+* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
+  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
+  is commonly produced by subtraction) look like a single 1-bit
+  difference.
+* the base values were pseudorandom, all zero but one bit set, or 
+  all zero plus a counter that starts at zero.
+
+Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
+satisfy this are
+    4  6  8 16 19  4
+    9 15  3 18 27 15
+   14  9  3  7 17  3
+Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
+for "differ" defined as + with a one-bit base and a two-bit delta.  I
+used http://burtleburtle.net/bob/hash/avalanche.html to choose 
+the operations, constants, and arrangements of the variables.
+
+This does not achieve avalanche.  There are input bits of (a,b,c)
+that fail to affect some output bits of (a,b,c), especially of a.  The
+most thoroughly mixed value is c, but it doesn't really even achieve
+avalanche in c.
+
+This allows some parallelism.  Read-after-writes are good at doubling
+the number of bits affected, so the goal of mixing pulls in the opposite
+direction as the goal of parallelism.  I did what I could.  Rotates
+seem to cost as much as shifts on every machine I could lay my hands
+on, and rotates are much kinder to the top and bottom bits, so I used
+rotates.
+-------------------------------------------------------------------------------
+*/
 #define mix(a,b,c) \
 { \
-  a -= b; a -= c; a ^= (c>>13); \
-  b -= c; b -= a; b ^= (a<<8); \
-  c -= a; c -= b; c ^= (b>>13); \
-  a -= b; a -= c; a ^= (c>>12);  \
-  b -= c; b -= a; b ^= (a<<16); \
-  c -= a; c -= b; c ^= (b>>5); \
-  a -= b; a -= c; a ^= (c>>3);  \
-  b -= c; b -= a; b ^= (a<<10); \
-  c -= a; c -= b; c ^= (b>>15); \
+  a -= c;  a ^= rot(c, 4);  c += b; \
+  b -= a;  b ^= rot(a, 6);  a += c; \
+  c -= b;  c ^= rot(b, 8);  b += a; \
+  a -= c;  a ^= rot(c,16);  c += b; \
+  b -= a;  b ^= rot(a,19);  a += c; \
+  c -= b;  c ^= rot(b, 4);  b += a; \
 }
 
 /*
---------------------------------------------------------------------
-hash() -- hash a variable-length key into a 32-bit value
-  k       : the key (the unaligned variable-length array of bytes)
-  len     : the length of the key, counting by bytes
-  initval : can be any 4-byte value
-Returns a 32-bit value.  Every bit of the key affects every bit of
-the return value.  Every 1-bit and 2-bit delta achieves avalanche.
-About 6*len+35 instructions.
+-------------------------------------------------------------------------------
+final -- final mixing of 3 32-bit values (a,b,c) into c
 
-The best hash table sizes are powers of 2.  There is no need to do
-mod a prime (mod is sooo slow!).  If you need less than 32 bits,
-use a bitmask.  For example, if you need only 10 bits, do
-  h = (h & hashmask(10));
-In which case, the hash table should have hashsize(10) elements.
+Pairs of (a,b,c) values differing in only a few bits will usually
+produce values of c that look totally different.  This was tested for
+* pairs that differed by one bit, by two bits, in any combination
+  of top bits of (a,b,c), or in any combination of bottom bits of
+  (a,b,c).
+* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
+  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
+  is commonly produced by subtraction) look like a single 1-bit
+  difference.
+* the base values were pseudorandom, all zero but one bit set, or 
+  all zero plus a counter that starts at zero.
 
-If you are hashing n strings (ub1 **)k, do it like this:
-  for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
+These constants passed:
+ 14 11 25 16 4 14 24
+ 12 14 25 16 4 14 24
+and these came close:
+  4  8 15 26 3 22 24
+ 10  8 15 26 3 22 24
+ 11  8 15 26 3 22 24
+-------------------------------------------------------------------------------
+*/
+#define final(a,b,c) \
+{ \
+  c ^= b; c -= rot(b,14); \
+  a ^= c; a -= rot(c,11); \
+  b ^= a; b -= rot(a,25); \
+  c ^= b; c -= rot(b,16); \
+  a ^= c; a -= rot(c,4);  \
+  b ^= a; b -= rot(a,14); \
+  c ^= b; c -= rot(b,24); \
+}
 
-By Bob Jenkins, 1996.  bob_jenkins at burtleburtle.net.  You may use this
-code any way you wish, private, educational, or commercial.  It's free.
+#if HASH_LITTLE_ENDIAN == 1
+uint32_t hash( 
+  const void *key,       /* the key to hash */
+  size_t      length,    /* length of the key */
+  uint32_t    initval)   /* initval */
+{
+  uint32_t a,b,c;                                          /* internal state */
+  union { const void *ptr; size_t i; } u;     /* needed for Mac Powerbook G4 */
 
-See http://burtleburtle.net/bob/hash/evahash.html
-Use for hash table lookup, or anything where one collision in 2^^32 is
-acceptable.  Do NOT use for cryptographic purposes.
---------------------------------------------------------------------
+  /* Set up the internal state */  
+  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
+
+  u.ptr = key;
+  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
+    const uint32_t *k = key;                           /* read 32-bit chunks */
+#ifdef VALGRIND
+    const uint8_t  *k8;
+#endif // ifdef VALGRIND
+
+    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
+    while (length > 12)
+    {
+      a += k[0];
+      b += k[1];
+      c += k[2];
+      mix(a,b,c);
+      length -= 12;
+      k += 3;
+    }
+
+    /*----------------------------- handle the last (probably partial) block */
+    /* 
+     * "k[2]&0xffffff" actually reads beyond the end of the string, but
+     * then masks off the part it's not allowed to read.  Because the
+     * string is aligned, the masked-off tail is in the same word as the
+     * rest of the string.  Every machine with memory protection I've seen
+     * does it on word boundaries, so is OK with this.  But VALGRIND will
+     * still catch it and complain.  The masking trick does make the hash
+     * noticably faster for short strings (like English words).
 */
+#ifndef VALGRIND
 
-ub4 hash( k, length, initval)
-     register ub1 *k;        /* the key */
-     register ub4  length;   /* the length of the key */
-     register ub4  initval;  /* the previous hash, or an arbitrary value */
+    switch(length)
+    {
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
+    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
+    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
+    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
+    case 8 : b+=k[1]; a+=k[0]; break;
+    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
+    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
+    case 5 : b+=k[1]&0xff; a+=k[0]; break;
+    case 4 : a+=k[0]; break;
+    case 3 : a+=k[0]&0xffffff; break;
+    case 2 : a+=k[0]&0xffff; break;
+    case 1 : a+=k[0]&0xff; break;
+    case 0 : return c;  /* zero length strings require no mixing */
+    }
+
+#else /* make valgrind happy */
+
+    k8 = (const uint8_t *)k;
+    switch(length)
+    {
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
+    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
+    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
+    case 9 : c+=k8[8];                   /* fall through */
+    case 8 : b+=k[1]; a+=k[0]; break;
+    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
+    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
+    case 5 : b+=k8[4];                   /* fall through */
+    case 4 : a+=k[0]; break;
+    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
+    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
+    case 1 : a+=k8[0]; break;
+    case 0 : return c;  /* zero length strings require no mixing */
+    }
+
+#endif /* !valgrind */
+
+  } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
+    const uint16_t *k = key;                           /* read 16-bit chunks */
+    const uint8_t  *k8;
+
+    /*--------------- all but last block: aligned reads and different mixing */
+    while (length > 12)
+    {
+      a += k[0] + (((uint32_t)k[1])<<16);
+      b += k[2] + (((uint32_t)k[3])<<16);
+      c += k[4] + (((uint32_t)k[5])<<16);
+      mix(a,b,c);
+      length -= 12;
+      k += 6;
+    }
+
+    /*----------------------------- handle the last (probably partial) block */
+    k8 = (const uint8_t *)k;
+    switch(length)
 {
-    register ub4 a,b,c,len;
+    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
+             b+=k[2]+(((uint32_t)k[3])<<16);
+             a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
+    case 10: c+=k[4];
+             b+=k[2]+(((uint32_t)k[3])<<16);
+             a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 9 : c+=k8[8];                      /* fall through */
+    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
+             a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
+    case 6 : b+=k[2];
+             a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 5 : b+=k8[4];                      /* fall through */
+    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
+             break;
+    case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
+    case 2 : a+=k[0];
+             break;
+    case 1 : a+=k8[0];
+             break;
+    case 0 : return c;  /* zero length strings require no mixing */
+    }
 
-    /* Set up the internal state */
-    len = length;
-    a = b = 0x9e3779b9;  /* the golden ratio; an arbitrary value */
-    c = initval;         /* the previous hash value */
+  } else {                        /* need to read the key one byte at a time */
+    const uint8_t *k = key;
 
-    /*---------------------------------------- handle most of the key */
-    while (len >= 12)
+    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
+    while (length > 12)
         {
-            a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
-            b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
-            c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
+      a += k[0];
+      a += ((uint32_t)k[1])<<8;
+      a += ((uint32_t)k[2])<<16;
+      a += ((uint32_t)k[3])<<24;
+      b += k[4];
+      b += ((uint32_t)k[5])<<8;
+      b += ((uint32_t)k[6])<<16;
+      b += ((uint32_t)k[7])<<24;
+      c += k[8];
+      c += ((uint32_t)k[9])<<8;
+      c += ((uint32_t)k[10])<<16;
+      c += ((uint32_t)k[11])<<24;
             mix(a,b,c);
-            k += 12; len -= 12;
+      length -= 12;
+      k += 12;
         }
 
-    /*------------------------------------- handle the last 11 bytes */
-    c += length;
-    switch(len)              /* all the case statements fall through */
+    /*-------------------------------- last block: affect all 32 bits of (c) */
+    switch(length)                   /* all the case statements fall through */
         {
-        case 11: c+=((ub4)k[10]<<24);
-        case 10: c+=((ub4)k[9]<<16);
-        case 9 : c+=((ub4)k[8]<<8);
-            /* the first byte of c is reserved for the length */
-        case 8 : b+=((ub4)k[7]<<24);
-        case 7 : b+=((ub4)k[6]<<16);
-        case 6 : b+=((ub4)k[5]<<8);
+    case 12: c+=((uint32_t)k[11])<<24;
+    case 11: c+=((uint32_t)k[10])<<16;
+    case 10: c+=((uint32_t)k[9])<<8;
+    case 9 : c+=k[8];
+    case 8 : b+=((uint32_t)k[7])<<24;
+    case 7 : b+=((uint32_t)k[6])<<16;
+    case 6 : b+=((uint32_t)k[5])<<8;
         case 5 : b+=k[4];
-        case 4 : a+=((ub4)k[3]<<24);
-        case 3 : a+=((ub4)k[2]<<16);
-        case 2 : a+=((ub4)k[1]<<8);
+    case 4 : a+=((uint32_t)k[3])<<24;
+    case 3 : a+=((uint32_t)k[2])<<16;
+    case 2 : a+=((uint32_t)k[1])<<8;
         case 1 : a+=k[0];
-            /* case 0: nothing left to add */
+             break;
+    case 0 : return c;  /* zero length strings require no mixing */
+    }
         }
+
+  final(a,b,c);
+  return c;             /* zero length strings require no mixing */
+}
+
+#elif HASH_BIG_ENDIAN == 1
+/*
+ * hashbig():
+ * This is the same as hashword() on big-endian machines.  It is different
+ * from hashlittle() on all machines.  hashbig() takes advantage of
+ * big-endian byte ordering. 
+ */
+uint32_t hash( const void *key, size_t length, uint32_t initval)
+{
+  uint32_t a,b,c;
+  union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */
+
+  /* Set up the internal state */
+  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
+
+  u.ptr = key;
+  if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) {
+    const uint32_t *k = key;                           /* read 32-bit chunks */
+#ifdef VALGRIND
+    const uint8_t  *k8;
+#endif // ifdef VALGRIND
+
+    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
+    while (length > 12)
+    {
+      a += k[0];
+      b += k[1];
+      c += k[2];
+      mix(a,b,c);
+      length -= 12;
+      k += 3;
+    }
+
+    /*----------------------------- handle the last (probably partial) block */
+    /* 
+     * "k[2]<<8" actually reads beyond the end of the string, but
+     * then shifts out the part it's not allowed to read.  Because the
+     * string is aligned, the illegal read is in the same word as the
+     * rest of the string.  Every machine with memory protection I've seen
+     * does it on word boundaries, so is OK with this.  But VALGRIND will
+     * still catch it and complain.  The masking trick does make the hash
+     * noticably faster for short strings (like English words).
+     */
+#ifndef VALGRIND
+
+    switch(length)
+    {
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
+    case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break;
+    case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break;
+    case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break;
+    case 8 : b+=k[1]; a+=k[0]; break;
+    case 7 : b+=k[1]&0xffffff00; a+=k[0]; break;
+    case 6 : b+=k[1]&0xffff0000; a+=k[0]; break;
+    case 5 : b+=k[1]&0xff000000; a+=k[0]; break;
+    case 4 : a+=k[0]; break;
+    case 3 : a+=k[0]&0xffffff00; break;
+    case 2 : a+=k[0]&0xffff0000; break;
+    case 1 : a+=k[0]&0xff000000; break;
+    case 0 : return c;              /* zero length strings require no mixing */
+    }
+
+#else  /* make valgrind happy */
+
+    k8 = (const uint8_t *)k;
+    switch(length)                   /* all the case statements fall through */
+    {
+    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
+    case 11: c+=((uint32_t)k8[10])<<8;  /* fall through */
+    case 10: c+=((uint32_t)k8[9])<<16;  /* fall through */
+    case 9 : c+=((uint32_t)k8[8])<<24;  /* fall through */
+    case 8 : b+=k[1]; a+=k[0]; break;
+    case 7 : b+=((uint32_t)k8[6])<<8;   /* fall through */
+    case 6 : b+=((uint32_t)k8[5])<<16;  /* fall through */
+    case 5 : b+=((uint32_t)k8[4])<<24;  /* fall through */
+    case 4 : a+=k[0]; break;
+    case 3 : a+=((uint32_t)k8[2])<<8;   /* fall through */
+    case 2 : a+=((uint32_t)k8[1])<<16;  /* fall through */
+    case 1 : a+=((uint32_t)k8[0])<<24; break;
+    case 0 : return c;
+    }
+
+#endif /* !VALGRIND */
+
+  } else {                        /* need to read the key one byte at a time */
+    const uint8_t *k = key;
+
+    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
+    while (length > 12)
+    {
+      a += ((uint32_t)k[0])<<24;
+      a += ((uint32_t)k[1])<<16;
+      a += ((uint32_t)k[2])<<8;
+      a += ((uint32_t)k[3]);
+      b += ((uint32_t)k[4])<<24;
+      b += ((uint32_t)k[5])<<16;
+      b += ((uint32_t)k[6])<<8;
+      b += ((uint32_t)k[7]);
+      c += ((uint32_t)k[8])<<24;
+      c += ((uint32_t)k[9])<<16;
+      c += ((uint32_t)k[10])<<8;
+      c += ((uint32_t)k[11]);
     mix(a,b,c);
-    /*-------------------------------------------- report the result */
+      length -= 12;
+      k += 12;
+    }
+
+    /*-------------------------------- last block: affect all 32 bits of (c) */
+    switch(length)                   /* all the case statements fall through */
+    {
+    case 12: c+=k[11];
+    case 11: c+=((uint32_t)k[10])<<8;
+    case 10: c+=((uint32_t)k[9])<<16;
+    case 9 : c+=((uint32_t)k[8])<<24;
+    case 8 : b+=k[7];
+    case 7 : b+=((uint32_t)k[6])<<8;
+    case 6 : b+=((uint32_t)k[5])<<16;
+    case 5 : b+=((uint32_t)k[4])<<24;
+    case 4 : a+=k[3];
+    case 3 : a+=((uint32_t)k[2])<<8;
+    case 2 : a+=((uint32_t)k[1])<<16;
+    case 1 : a+=((uint32_t)k[0])<<24;
+             break;
+    case 0 : return c;
+    }
+  }
+
+  final(a,b,c);
     return c;
 }
+#else // HASH_XXX_ENDIAN == 1
+#error Must define HASH_BIG_ENDIAN or HASH_LITTLE_ENDIAN 
+#endif // hash_XXX_ENDIAN == 1
 
 static item** hashtable = 0;
 

Modified: branches/server-win32/configure.ac
===================================================================
--- branches/server-win32/configure.ac	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/configure.ac	2006-11-23 19:12:58 UTC (rev 443)
@@ -1,5 +1,5 @@
 AC_PREREQ(2.52)
-AC_INIT(memcached, 1.2.0-rc1, brad at danga.com)
+AC_INIT(memcached, 1.2.0, brad at danga.com)
 AC_CANONICAL_SYSTEM
 AC_CONFIG_SRCDIR(memcached.c)
 AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION)
@@ -129,6 +129,32 @@
 
 AC_C_SOCKLEN_T
 
+dnl Check if we're a little-endian or a big-endian system, needed by hash code
+AC_DEFUN(AC_C_ENDIAN,
+[AC_CACHE_CHECK(for endianness, ac_cv_c_endian,
+[
+  AC_RUN_IFELSE(
+    [AC_LANG_PROGRAM([], [dnl
+        long val = 1;
+        char *c = (char *) &val;
+        exit(*c == 1);
+    ])
+  ],[
+    ac_cv_c_endian=big
+  ],[
+    ac_cv_c_endian=little
+  ])
+])
+if test $ac_cv_c_endian = big; then
+  AC_DEFINE(ENDIAN_BIG, 1, [machine is bigendian])
+fi
+if test $ac_cv_c_endian = little; then
+  AC_DEFINE(ENDIAN_LITTLE, 1, [machine is littleendian])
+fi
+])
+
+AC_C_ENDIAN
+
 AC_CHECK_FUNCS(mlockall)
 
 AC_CONFIG_FILES(Makefile doc/Makefile)

Modified: branches/server-win32/items.c
===================================================================
--- branches/server-win32/items.c	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/items.c	2006-11-23 19:12:58 UTC (rev 443)
@@ -324,3 +324,29 @@
     free(histogram);
     return buf;
 }
+
+/* expires items that are more recent than the oldest_live setting. */
+void item_flush_expired() {
+    int i;
+    item *iter, *next;
+    if (! settings.oldest_live)
+        return;
+    for (i = 0; i < LARGEST_ID; i++) {
+        /* The LRU is sorted in decreasing time order, and an item's timestamp
+         * is never newer than its last access time, so we only need to walk
+         * back until we hit an item older than the oldest_live time.
+         * The oldest_live checking will auto-expire the remaining items.
+         */
+        for (iter = heads[i]; iter != NULL; iter = next) {
+            if (iter->time >= settings.oldest_live) {
+                next = iter->next;
+                if ((iter->it_flags & ITEM_SLABBED) == 0) {
+                    item_unlink(iter);
+                }
+            } else {
+                /* We've hit the first old item. Continue to the next queue. */
+                break;
+            }
+        }
+    }
+}

Modified: branches/server-win32/memcached.c
===================================================================
--- branches/server-win32/memcached.c	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/memcached.c	2006-11-23 19:12:58 UTC (rev 443)
@@ -15,16 +15,10 @@
  *
  *  $Id$
  */
-
-#ifndef WIN32
-#include "config.h"
-#else
-#include "Win32-Code/config.h"
-#endif
-
 #include <sys/types.h>
 #include <sys/stat.h>
 #ifndef WIN32
+#include "config.h"
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <sys/un.h>
@@ -50,6 +44,7 @@
 #include <arpa/inet.h>
 #include <errno.h>
 #else
+#include "Win32-Code/config.h"
 #include <Winsock2.h>
 #include <process.h>
 #include "Win32-Code/ntservice.h"
@@ -108,11 +103,11 @@
     stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = 0;
     stats.curr_bytes = stats.bytes_read = stats.bytes_written = 0;
 
-    /* make the time we started always be 1 second before we really
+    /* make the time we started always be 2 seconds before we really
        did, so time(0) - time.started is never zero.  if so, things
        like 'settings.oldest_live' which act as booleans as well as
        values are now false in boolean context... */
-    stats.started = time(0) - 1;
+    stats.started = time(0) - 2;
 }
 void stats_reset(void) {
     stats.total_items = stats.total_conns = 0;
@@ -122,7 +117,7 @@
 
 void settings_init(void) {
     settings.port = 11211;
-    settings.udpport = 11211;
+    settings.udpport = 0;
     settings.interf.s_addr = htonl(INADDR_ANY);
     settings.maxbytes = 64*1024*1024; /* default is 64MB */
     settings.maxconns = 1024;         /* to limit connections-related memory to about 5MB */
@@ -855,7 +850,7 @@
 
         char key[251];
         int flags;
-        rel_time_t expire;
+        time_t expire;
         long _expire = 0;
         int len, res;
         item *it;
@@ -1178,7 +1173,8 @@
         set_current_time();
 
         if (strcmp(command, "flush_all") == 0) {
-            settings.oldest_live = current_time;
+            settings.oldest_live = current_time - 1;
+            item_flush_expired();
             out_string(c, "OK");
             return;
         }
@@ -1190,7 +1186,8 @@
             return;
         }
 
-        settings.oldest_live = realtime(exptime);
+        settings.oldest_live = realtime(exptime) - 1;
+        item_flush_expired();
         out_string(c, "OK");
         return;
     }
@@ -1430,20 +1427,20 @@
 
 void drive_machine(conn *c) {
 
-    int exit = 0;
+    int stop = 0;
     int sfd, flags = 1;
     socklen_t addrlen;
     struct sockaddr addr;
     conn *newc;
     int res;
 
-    while (!exit) {
+    while (!stop) {
         switch(c->state) {
         case conn_listening:
             addrlen = sizeof(addr);
             if ((sfd = accept(c->sfd, &addr, &addrlen)) == -1) {
                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
-                    exit = 1;
+                    stop = 1;
                     break;
                 } else {
                     perror("accept()");
@@ -1481,7 +1478,7 @@
                 conn_set_state(c, conn_closing);
                 break;
             }
-            exit = 1;
+            stop = 1;
             break;
 
         case conn_nread:
@@ -1520,7 +1517,7 @@
                     conn_set_state(c, conn_closing);
                     break;
                 }
-                exit = 1;
+                stop = 1;
                 break;
             }
             /* otherwise we have a real error, on which we close the connection */
@@ -1563,7 +1560,7 @@
                     conn_set_state(c, conn_closing);
                     break;
                 }
-                exit = 1;
+                stop = 1;
                 break;
             }
             /* otherwise we have a real error, on which we close the connection */
@@ -1578,7 +1575,7 @@
              * assemble it into a msgbuf list (this will be a single-entry
              * list for TCP or a two-entry list for UDP).
              */
-            if (c->iovused == 0) {
+            if (c->iovused == 0 || (c->udp && c->iovused == 1)) {
                 if (add_iov(c, c->wcurr, c->wbytes) ||
                         c->udp && build_udp_headers(c)) {
                         if (settings.verbose > 0)
@@ -1620,7 +1617,7 @@
                 break;                   /* Continue in state machine. */
 
             case TRANSMIT_SOFT_ERROR:
-                exit = 1;
+                stop = 1;
                     break;
                 }
             break;
@@ -1630,7 +1627,7 @@
                 conn_cleanup(c);
             else
             conn_close(c);
-            exit = 1;
+            stop = 1;
             break;
         }
 
@@ -1728,9 +1725,9 @@
     if (is_udp) {
         maximize_sndbuf(sfd);
     } else {
-    setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags));
-    setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
-    setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
+        setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags));
+        setsockopt(sfd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling));
+        setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
     }
 
     /* 
@@ -1907,7 +1904,8 @@
         
 void usage(void) {
     printf(PACKAGE " " VERSION "\n");
-    printf("-p <num>          port number to listen on\n");
+    printf("-p <num>      TCP port number to listen on (default: 11211)\n");
+    printf("-U <num>      UDP port number to listen on (default: 0, off)\n");
     printf("-s <file>     unix socket path to listen on (disables network support)\n");
     printf("-l <ip_addr>      interface to listen on, default is INDRR_ANY\n");
 #ifdef WIN32
@@ -2152,6 +2150,9 @@
     struct passwd *pw;
     struct sigaction sa;
     struct rlimit rlim;
+
+    /* handle SIGINT */
+    signal(SIGINT, sig_handler);
 #else
     WSADATA wsaData;
     if(WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
@@ -2173,6 +2174,9 @@
     while ((c = getopt(argc, argv, "bp:s:U:m:Mc:khirvd:l:u:P:f:s:")) != -1) {
 #endif
         switch (c) {
+        case 'U':
+            settings.udpport = atoi(optarg);
+            break;
         case 'b':
             settings.managed = 1;
             break;
@@ -2322,7 +2326,7 @@
         /* create the UDP listening socket and bind it */
         u_socket = server_socket(settings.udpport, 1);
         if (u_socket == -1) {
-            fprintf(stderr, "failed to listen\n");
+            fprintf(stderr, "failed to listen on UDP port %d\n", settings.udpport);
             exit(1);
         }
     }
@@ -2343,6 +2347,7 @@
             return 1;
         }
     }
+
     /* create unix mode sockets after dropping privileges */
     if (settings.socketpath) {
         l_socket = server_socket_unix(settings.socketpath);

Modified: branches/server-win32/memcached.h
===================================================================
--- branches/server-win32/memcached.h	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/memcached.h	2006-11-23 19:12:58 UTC (rev 443)
@@ -275,6 +275,7 @@
 char *item_cachedump(unsigned int slabs_clsid, unsigned int limit, unsigned int *bytes);
 char *item_stats_sizes(int *bytes);
 void item_stats(char *buffer, int buflen);
+void item_flush_expired(void);
 
 /* time handling */
 void set_current_time ();  /* update the global variable holding

Modified: branches/server-win32/slabs.c
===================================================================
--- branches/server-win32/slabs.c	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/slabs.c	2006-11-23 19:12:58 UTC (rev 443)
@@ -129,6 +129,24 @@
 #endif
 }
 
+void slabs_preallocate (unsigned int maxslabs) {
+    int i;
+    unsigned int prealloc = 0;
+
+    /* pre-allocate a 1MB slab in every size class so people don't get
+       confused by non-intuitive "SERVER_ERROR out of memory"
+       messages.  this is the most common question on the mailing
+       list.  if you really don't want this, you can rebuild without
+       these three lines.  */
+
+    for(i=POWER_SMALLEST; i<=POWER_LARGEST; i++) {
+        if (++prealloc > maxslabs)
+            return;
+        slabs_newslab(i);
+    } 
+ 
+}
+
 static int grow_slab_list (unsigned int id) { 
     slabclass_t *p = &slabclass[id];
     if (p->slabs == p->list_size) {
@@ -167,24 +185,6 @@
     return 1;
 }
 
-void slabs_preallocate (unsigned int maxslabs) {
-    int i;
-    unsigned int prealloc = 0;
-
-    /* pre-allocate a 1MB slab in every size class so people don't get
-       confused by non-intuitive "SERVER_ERROR out of memory"
-       messages.  this is the most common question on the mailing
-       list.  if you really don't want this, you can rebuild without
-       these three lines.  */
-
-    for(i=POWER_SMALLEST; i<=POWER_LARGEST; i++) {
-        if (++prealloc > maxslabs)
-            return;
-        slabs_newslab(i);
-    } 
- 
-}
-
 void *slabs_alloc(size_t size) {
     slabclass_t *p;
 

Modified: branches/server-win32/t/flush-all.t
===================================================================
--- branches/server-win32/t/flush-all.t	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/t/flush-all.t	2006-11-23 19:12:58 UTC (rev 443)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 
 use strict;
-use Test::More tests => 11;
+use Test::More tests => 10;
 use FindBin qw($Bin);
 use lib "$Bin/lib";
 use MemcachedTest;
@@ -16,19 +16,13 @@
 mem_get_is($sock, "foo", "fooval");
 print $sock "flush_all\r\n";
 is(scalar <$sock>, "OK\r\n", "did flush_all");
-
 mem_get_is($sock, "foo", undef);
-SKIP: {
-    skip "flush_all is still only second-granularity.  need atomic counter on flush_all.", 2 unless 0;
 
+# check that flush_all doesn't blow away items that immediately get set
     print $sock "set foo 0 0 3\r\nnew\r\n";
     is(scalar <$sock>, "STORED\r\n", "stored foo = 'new'");
     mem_get_is($sock, "foo", 'new');
-}
 
-sleep 1;
-mem_get_is($sock, "foo", undef);
-
 # and the other form, specifying a flush_all time...
 my $expire = time() + 2;
 print $sock "flush_all $expire\r\n";

Modified: branches/server-win32/t/stress-memcached.pl
===================================================================
--- branches/server-win32/t/stress-memcached.pl	2006-11-23 07:38:27 UTC (rev 442)
+++ branches/server-win32/t/stress-memcached.pl	2006-11-23 19:12:58 UTC (rev 443)
@@ -2,8 +2,8 @@
 #
 
 use strict;
-use lib '../api/perl';
-use MemCachedClient;
+use lib '../../api/perl/lib';
+use Cache::Memcached;
 use Time::HiRes qw(time);
 
 unless (@ARGV == 2) {
@@ -13,7 +13,7 @@
 my $host = shift;
 my $threads = shift;
 
-my $memc = new MemCachedClient;
+my $memc = new Cache::Memcached;
 $memc->set_servers([$host]);
 
 unless ($memc->set("foo", "bar") &&
@@ -42,7 +42,7 @@
 
 sub stress {
     undef $memc;
-    $memc = new MemCachedClient;
+    $memc = new Cache::Memcached;
     $memc->set_servers([$host]);
 
     my ($t1, $t2);




More information about the memcached-commits mailing list