Include config.h before any system header to avoid problems with FILE_OFFSET_BITS...
[m6w6/libmemcached] / clients / memslap.c
index ee1920c73f5cda697c3ba60b051e3adb54634819..4e3d6813f05742cc6ba4a26a7f64446d306e62bf 100644 (file)
@@ -1,3 +1,4 @@
+#include "libmemcached/common.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -6,10 +7,10 @@
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <fcntl.h>
-#include <assert.h>
 #include <sys/time.h>
 #include <getopt.h>
 #include <pthread.h>
+#include <assert.h>
 
 #include <libmemcached/memcached.h>
 
@@ -68,6 +69,7 @@ pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
                            unsigned int *actual_loaded);
 void flush_all(memcached_st *memc);
 
+static int opt_binary= 0;
 static int opt_verbose= 0;
 static int opt_flush= 0;
 static int opt_non_blocking_io= 0;
@@ -77,6 +79,7 @@ static unsigned int opt_createial_load= 0;
 static unsigned int opt_concurrency= 0;
 static int opt_displayflag= 0;
 static char *opt_servers= NULL;
+static int opt_udp_io= 0;
 test_type opt_test= SET_TEST;
 
 int main(int argc, char *argv[])
@@ -86,7 +89,7 @@ int main(int argc, char *argv[])
 
   memset(&conclusion, 0, sizeof(conclusions_st));
 
-  srandom(time(NULL));
+  srandom((unsigned int)time(NULL));
   options_parse(argc, argv);
 
   if (!opt_servers)
@@ -139,8 +142,20 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
                               PTHREAD_CREATE_DETACHED);
 
   memc= memcached_create(NULL);
+
+  /* We need to set udp behavior before adding servers to the client */
+  if (opt_udp_io)
+  {
+    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,
+                           (uint64_t)opt_udp_io);
+    for(x= 0; x < servers[0].count; x++ )
+      servers[x].type= MEMCACHED_CONNECTION_UDP;
+  }
   memcached_server_push(memc, servers);
 
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
+                         (uint64_t)opt_binary);
+  
   if (opt_flush)
     flush_all(memc);
   if (opt_createial_load)
@@ -165,8 +180,7 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
   for (x= 0; x < opt_concurrency; x++)
   {
     thread_context_st *context;
-    context= (thread_context_st *)malloc(sizeof(thread_context_st));
-    memset(context, 0, sizeof(thread_context_st));
+    context= (thread_context_st *)calloc(1, sizeof(thread_context_st));
 
     context->memc= memcached_clone(NULL, memc);
     context->test= opt_test;
@@ -204,14 +218,7 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
   */
   pthread_mutex_lock(&counter_mutex);
   while (thread_counter)
-  {
-    struct timespec abstime;
-
-    memset(&abstime, 0, sizeof(struct timespec));
-    abstime.tv_sec= 1;
-
-    pthread_cond_timedwait(&count_threshhold, &counter_mutex, &abstime);
-  }
+    pthread_cond_wait(&count_threshhold, &counter_mutex);
   pthread_mutex_unlock(&counter_mutex);
 
   gettimeofday(&end_time, NULL);
@@ -219,6 +226,7 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
   conclusion->load_time= timedif(end_time, start_time);
   conclusion->read_time= timedif(end_time, start_time);
   pairs_free(pairs);
+  memcached_free(memc);
 }
 
 void options_parse(int argc, char *argv[])
@@ -230,19 +238,21 @@ void options_parse(int argc, char *argv[])
 
   static struct option long_options[]=
     {
-      {"concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY},
-      {"debug", no_argument, &opt_verbose, OPT_DEBUG},
-      {"execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER},
-      {"flag", no_argument, &opt_displayflag, OPT_FLAG},
-      {"flush", no_argument, &opt_flush, OPT_FLUSH},
-      {"help", no_argument, NULL, OPT_HELP},
-      {"initial-load", required_argument, NULL, OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */
-      {"non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK},
-      {"servers", required_argument, NULL, OPT_SERVERS},
-      {"tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY},
-      {"test", required_argument, NULL, OPT_SLAP_TEST},
-      {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
-      {"version", no_argument, NULL, OPT_VERSION},
+      {(OPTIONSTRING)"concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY},
+      {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
+      {(OPTIONSTRING)"execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER},
+      {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
+      {(OPTIONSTRING)"flush", no_argument, &opt_flush, OPT_FLUSH},
+      {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
+      {(OPTIONSTRING)"initial-load", required_argument, NULL, OPT_SLAP_INITIAL_LOAD}, /* Number to load initially */
+      {(OPTIONSTRING)"non-blocking", no_argument, &opt_non_blocking_io, OPT_SLAP_NON_BLOCK},
+      {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
+      {(OPTIONSTRING)"tcp-nodelay", no_argument, &opt_tcp_nodelay, OPT_SLAP_TCP_NODELAY},
+      {(OPTIONSTRING)"test", required_argument, NULL, OPT_SLAP_TEST},
+      {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
+      {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
+      {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
+      {(OPTIONSTRING)"udp", no_argument, NULL, OPT_UDP},
       {0, 0, 0, 0},
     };
 
@@ -257,6 +267,18 @@ void options_parse(int argc, char *argv[])
     {
     case 0:
       break;
+    case OPT_UDP:
+      if (opt_test == GET_TEST)
+      {
+        fprintf(stderr, "You can not run a get test in UDP mode. UDP mode "
+                  "does not currently support get ops.\n");
+        exit(1);
+      }
+      opt_udp_io= 1;
+      break;
+    case OPT_BINARY:
+      opt_binary = 1;
+      break;
     case OPT_VERBOSE: /* --verbose or -v */
       opt_verbose = OPT_VERBOSE;
       break;
@@ -274,7 +296,15 @@ void options_parse(int argc, char *argv[])
       break;
     case OPT_SLAP_TEST:
       if (!strcmp(optarg, "get"))
+      {
+        if (opt_udp_io == 1)
+        {
+          fprintf(stderr, "You can not run a get test in UDP mode. UDP mode "
+                  "does not currently support get ops.\n");
+          exit(1);
+        }
         opt_test= GET_TEST ;
+      }
       else if (!strcmp(optarg, "set"))
         opt_test= SET_TEST;
       else 
@@ -284,12 +314,13 @@ void options_parse(int argc, char *argv[])
       }
       break;
     case OPT_SLAP_CONCURRENCY:
-      opt_concurrency= strtol(optarg, (char **)NULL, 10);
+      opt_concurrency= (unsigned int)strtoul(optarg, (char **)NULL, 10);
+      break;
     case OPT_SLAP_EXECUTE_NUMBER:
-      opt_execute_number= strtol(optarg, (char **)NULL, 10);
+      opt_execute_number= (unsigned int)strtoul(optarg, (char **)NULL, 10);
       break;
     case OPT_SLAP_INITIAL_LOAD:
-      opt_createial_load= strtol(optarg, (char **)NULL, 10);
+      opt_createial_load= (unsigned int)strtoul(optarg, (char **)NULL, 10);
       break;
     case '?':
       /* getopt_long already printed an error message. */
@@ -342,17 +373,22 @@ void *run_task(void *p)
   switch (context->test)
   {
   case SET_TEST:
+    assert(context->execute_pairs);
     execute_set(memc, context->execute_pairs, context->execute_number);
     break;
   case GET_TEST:
     execute_get(memc, context->initial_pairs, context->initial_number);
     break;
+  default:
+    WATCHPOINT_ASSERT(context->test);
+    break;
   }
 
   memcached_free(memc);
 
   if (context->execute_pairs)
     pairs_free(context->execute_pairs);
+
   free(context);
 
   pthread_mutex_lock(&counter_mutex);
@@ -371,17 +407,17 @@ void flush_all(memcached_st *memc)
 pairs_st *load_create_data(memcached_st *memc, unsigned int number_of, 
                            unsigned int *actual_loaded)
 {
-  memcached_st *clone;
+  memcached_st *memc_clone;
   pairs_st *pairs;
 
-  clone= memcached_clone(NULL, memc);
+  memc_clone= memcached_clone(NULL, memc);
   /* We always used non-blocking IO for load since it is faster */
-  memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
+  memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
 
   pairs= pairs_generate(number_of, 400);
-  *actual_loaded= execute_set(clone, pairs, number_of);
+  *actual_loaded= execute_set(memc_clone, pairs, number_of);
 
-  memcached_free(clone);
+  memcached_free(memc_clone);
 
   return pairs;
 }