1.Fix bug that memslap can't start up(ms_thread=NULL). 2.ignore signal SIGPIPE. 3...
[m6w6/libmemcached] / clients / memslap.c
index 37e211a0dd4f2d8bb53ccf735a9319311c569cda..006d3c2b5d8488f97b249be37e7a3e6cf21250a3 100644 (file)
  *      Mingqiang Zhuang <mingqiangzhuang@hengtiansoft.com>
  *
  */
+#include "config.h"
+
 #include <stdlib.h>
 #include <getopt.h>
 #include <limits.h>
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 
 #include "ms_sigsegv.h"
 #include "ms_setting.h"
@@ -113,6 +126,10 @@ static void ms_sync_lock_init()
   pthread_mutex_init(&ms_global.init_lock.lock, NULL);
   pthread_cond_init(&ms_global.init_lock.cond, NULL);
 
+  ms_global.warmup_lock.count = 0;
+  pthread_mutex_init(&ms_global.warmup_lock.lock, NULL);
+  pthread_cond_init(&ms_global.warmup_lock.cond, NULL);
+
   ms_global.run_lock.count= 0;
   pthread_mutex_init(&ms_global.run_lock.lock, NULL);
   pthread_cond_init(&ms_global.run_lock.cond, NULL);
@@ -128,6 +145,9 @@ static void ms_sync_lock_destroy()
   pthread_mutex_destroy(&ms_global.init_lock.lock);
   pthread_cond_destroy(&ms_global.init_lock.cond);
 
+  pthread_mutex_destroy(&ms_global.warmup_lock.lock);
+  pthread_cond_destroy(&ms_global.warmup_lock.cond);
+
   pthread_mutex_destroy(&ms_global.run_lock.lock);
   pthread_cond_destroy(&ms_global.run_lock.cond);
 
@@ -298,6 +318,7 @@ void ms_help_command(const char *command_name, const char *description)
   {
     printf("    -%c, --%s%c\n", long_options[x].val, long_options[x].name,
            long_options[x].has_arg ? '=' : ' ');
+
     if ((help_message= (char *)ms_lookup_help(long_options[x].val)) != NULL)
     {
       printf("        %s\n", help_message);
@@ -417,7 +438,7 @@ static void ms_options_parse(int argc, char *argv[])
       break;
 
     case OPT_CONCURRENCY:       /* --concurrency or -c */
-      ms_setting.nconns= (int)strtol(optarg, (char **) NULL, 10);
+      ms_setting.nconns= (uint32_t)strtoul(optarg, (char **) NULL, 10);
       if (ms_setting.nconns <= 0)
       {
         fprintf(stderr, "Concurrency must be greater than 0.:-)\n");
@@ -613,11 +634,20 @@ static int ms_check_para()
 {
   if (ms_setting.srv_str == NULL)
   {
-    fprintf(stderr, "No Servers provided.\n\n");
-    return -1;
+    char *temp;
+
+    if ((temp= getenv("MEMCACHED_SERVERS")))
+    {
+      ms_setting.srv_str= strdup(temp);
+    }
+    else
+    {
+      fprintf(stderr, "No Servers provided\n\n");
+      return -1;
+    }
   }
 
-  if (ms_setting.nconns % ms_setting.nthreads != 0)
+  if (ms_setting.nconns % (uint32_t)ms_setting.nthreads != 0)
   {
     fprintf(stderr, "Concurrency must be the multiples of threads count.\n");
     return -1;
@@ -676,53 +706,45 @@ static void ms_print_memslap_stats(struct timeval *start_time,
   char buf[1024];
   char *pos= buf;
 
-  pos+= sprintf(pos,
-                "cmd_get: %llu\n",
-                (unsigned long long)ms_stats.cmd_get);
-  pos+= sprintf(pos,
-                "cmd_set: %llu\n",
-                (unsigned long long)ms_stats.cmd_set);
-  pos+= sprintf(pos,
-                "get_misses: %llu\n",
-                (unsigned long long)ms_stats.get_misses);
+  pos+= sprintf(pos, "cmd_get: %zu\n",
+                ms_stats.cmd_get);
+  pos+= sprintf(pos, "cmd_set: %zu\n",
+                ms_stats.cmd_set);
+  pos+= sprintf(pos, "get_misses: %zu\n",
+                ms_stats.get_misses);
 
   if (ms_setting.verify_percent > 0)
   {
-    pos+= sprintf(pos, "verify_misses: %llu\n",
-                  (unsigned long long)ms_stats.vef_miss);
-    pos+= sprintf(pos, "verify_failed: %llu\n",
-                  (unsigned long long)ms_stats.vef_failed);
+    pos+= sprintf(pos, "verify_misses: %zu\n",
+                  ms_stats.vef_miss);
+    pos+= sprintf(pos, "verify_failed: %zu\n",
+                  ms_stats.vef_failed);
   }
 
   if (ms_setting.exp_ver_per > 0)
   {
-    pos+= sprintf(pos, "expired_get: %llu\n",
-                  (unsigned long long)ms_stats.exp_get);
-    pos+= sprintf(pos, "unexpired_unget: %llu\n",
-                  (unsigned long long)ms_stats.unexp_unget);
+    pos+= sprintf(pos, "expired_get: %zu\n",
+                  ms_stats.exp_get);
+    pos+= sprintf(pos, "unexpired_unget: %zu\n",
+                  ms_stats.unexp_unget);
   }
 
   pos+= sprintf(pos,
-                "written_bytes: %llu\n",
-                (unsigned long long)ms_stats.bytes_written);
-  pos+= sprintf(pos,
-                "read_bytes: %llu\n",
-                (unsigned long long)ms_stats.bytes_read);
-  pos+= sprintf(pos,
-                "object_bytes: %llu\n",
-                (unsigned long long)ms_stats.obj_bytes);
+                "written_bytes: %zu\n",
+                ms_stats.bytes_written);
+  pos+= sprintf(pos, "read_bytes: %zu\n",
+                ms_stats.bytes_read);
+  pos+= sprintf(pos, "object_bytes: %zu\n",
+                ms_stats.obj_bytes);
 
   if (ms_setting.udp || ms_setting.facebook_test)
   {
-    pos+= sprintf(pos,
-                  "packet_disorder: %llu\n",
-                  (unsigned long long)ms_stats.pkt_disorder);
-    pos+= sprintf(pos,
-                  "packet_drop: %llu\n",
-                  (unsigned long long)ms_stats.pkt_drop);
-    pos+= sprintf(pos,
-                  "udp_timeout: %llu\n",
-                  (unsigned long long)ms_stats.udp_timeout);
+    pos+= sprintf(pos, "packet_disorder: %zu\n",
+                  ms_stats.pkt_disorder);
+    pos+= sprintf(pos, "packet_drop: %zu\n",
+                  ms_stats.pkt_drop);
+    pos+= sprintf(pos, "udp_timeout: %zu\n",
+                  ms_stats.udp_timeout);
   }
 
   if (ms_setting.stat_freq > 0)
@@ -756,19 +778,25 @@ static void ms_monitor_slap_mode()
   int second= 0;
   struct timeval start_time, end_time;
 
+  /* Wait all the threads complete initialization. */
+  pthread_mutex_lock(&ms_global.init_lock.lock);
+  while (ms_global.init_lock.count < ms_setting.nthreads)
+  {
+    pthread_cond_wait(&ms_global.init_lock.cond,
+                      &ms_global.init_lock.lock);
+  }
+  pthread_mutex_unlock(&ms_global.init_lock.lock);
+
   /* only when there is no set operation it need warm up */
   if (ms_setting.cmd_distr[CMD_SET].cmd_prop < PROP_ERROR)
   {
     /* Wait all the connects complete warm up. */
-    pthread_mutex_lock(&ms_global.init_lock.lock);
-    while (ms_global.init_lock.count < ms_setting.nconns)
-    {
-      pthread_cond_wait(&ms_global.init_lock.cond,
-                        &ms_global.init_lock.lock);
+    pthread_mutex_lock(&ms_global.warmup_lock.lock);
+    while (ms_global.warmup_lock.count < (int)ms_setting.nconns) {
+      pthread_cond_wait(&ms_global.warmup_lock.cond, &ms_global.warmup_lock.lock);
     }
-    pthread_mutex_unlock(&ms_global.init_lock.lock);
+    pthread_mutex_unlock(&ms_global.warmup_lock.lock);
   }
-
   ms_global.finish_warmup= true;
 
   /* running in "run time" mode, user specify run time */
@@ -811,7 +839,7 @@ static void ms_monitor_slap_mode()
      * We loop until we know that all connects have cleaned up.
      */
     pthread_mutex_lock(&ms_global.run_lock.lock);
-    while (ms_global.run_lock.count < ms_setting.nconns)
+    while (ms_global.run_lock.count < (int)ms_setting.nconns)
     {
       pthread_cond_wait(&ms_global.run_lock.cond, &ms_global.run_lock.lock);
     }