[PATCH] Reduce mutex contention in get command for multiple keys.

Tomash Brechko tomash.brechko at gmail.com
Mon Nov 12 09:02:54 UTC 2007


In a concurrent environment no value is "current" for long, so we may
as well delay the update of the global statistics until we are done.
---
 trunk/server/memcached.c |   26 +++++++++++++++++---------
 1 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/trunk/server/memcached.c b/trunk/server/memcached.c
index e432cd6..999fcb6 100644
--- a/trunk/server/memcached.c
+++ b/trunk/server/memcached.c
@@ -1106,6 +1106,8 @@ static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens,
     token_t *key_token = &tokens[KEY_TOKEN];
     char suffix[255];
     uint32_t in_memory_ptr;
+    int stats_get_cmds = 0, stats_get_hits = 0, stats_get_misses = 0;
+
     assert(c != NULL);
 
     if (settings.managed) {
@@ -1129,12 +1131,15 @@ static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens,
 
             if(nkey > KEY_MAX_LENGTH) {
                 out_string(c, "CLIENT_ERROR bad command line format");
+                STATS_LOCK();
+                stats.get_cmds += stats_get_cmds;
+                stats.get_hits += stats_get_hits;
+                stats.get_misses += stats_get_misses;
+                STATS_UNLOCK();
                 return;
             }
 
-            STATS_LOCK();
-            stats.get_cmds++;
-            STATS_UNLOCK();
+            stats_get_cmds++;
             it = item_get(key, nkey);
             if (settings.detail_enabled) {
                 stats_prefix_record_get(key, NULL != it);
@@ -1183,17 +1188,13 @@ static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens,
                     fprintf(stderr, ">%d sending key %s\n", c->sfd, ITEM_key(it));
 
                 /* item_get() has incremented it->refcount for us */
-                STATS_LOCK();
-                stats.get_hits++;
-                STATS_UNLOCK();
+                stats_get_hits++;
                 item_update(it);
                 *(c->ilist + i) = it;
                 i++;
 
             } else {
-                STATS_LOCK();
-                stats.get_misses++;
-                STATS_UNLOCK();
+                stats_get_misses++;
             }
 
             key_token++;
@@ -1224,6 +1225,13 @@ static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens,
         conn_set_state(c, conn_mwrite);
         c->msgcurr = 0;
     }
+
+    STATS_LOCK();
+    stats.get_cmds += stats_get_cmds;
+    stats.get_hits += stats_get_hits;
+    stats.get_misses += stats_get_misses;
+    STATS_UNLOCK();
+
     return;
 }
 
-- 
1.5.3.5.529.ge3d6d


More information about the memcached mailing list