Fixed return length issues.
[m6w6/libmemcached] / src / memslap.c
index 2e83acdb7969d1f33a17a7f886102226bbdb2a1e..5815015905d90fa7547f21a2735ee3f12ddfefe7 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -62,12 +63,16 @@ struct conclusions_st {
 void options_parse(int argc, char *argv[]);
 void conclusions_print(conclusions_st *conclusion);
 void scheduler(memcached_server_st *servers, conclusions_st *conclusion);
-pairs_st *load_initial_data(memcached_server_st *servers, unsigned int number_of, 
+pairs_st *load_create_data(memcached_server_st *servers, unsigned int number_of, 
                             unsigned int *actual_loaded);
+void flush_all(memcached_server_st *servers);
 
 static int opt_verbose= 0;
+static int opt_flush= 0;
+static int opt_non_blocking_io= 0;
+static int opt_tcp_nodelay= 0;
 static unsigned int opt_execute_number= 0;
-static unsigned int opt_initial_load= 0;
+static unsigned int opt_createial_load= 0;
 static unsigned int opt_concurrency= 0;
 static int opt_displayflag= 0;
 static char *opt_servers= NULL;
@@ -84,9 +89,16 @@ int main(int argc, char *argv[])
   options_parse(argc, argv);
 
   if (!opt_servers)
-    exit(0);
+  {
+    char *temp;
+
+    if ((temp= getenv("MEMCACHED_SERVERS")))
+      opt_servers= strdup(temp);
+    else
+      exit(1);
+  }
 
-  servers= parse_opt_servers(opt_servers);
+  servers= memcached_servers_parse(opt_servers);
 
   pthread_mutex_init(&counter_mutex, NULL);
   pthread_cond_init(&count_threshhold, NULL);
@@ -97,10 +109,10 @@ int main(int argc, char *argv[])
 
   free(opt_servers);
 
-  (void)pthread_mutex_init(&counter_mutex, NULL);
-  (void)pthread_cond_init(&count_threshhold, NULL);
-  (void)pthread_mutex_init(&sleeper_mutex, NULL);
-  (void)pthread_cond_init(&sleep_threshhold, NULL);
+  (void)pthread_mutex_destroy(&counter_mutex);
+  (void)pthread_cond_destroy(&count_threshhold);
+  (void)pthread_mutex_destroy(&sleeper_mutex);
+  (void)pthread_cond_destroy(&sleep_threshhold);
   conclusions_print(&conclusion);
   memcached_server_list_free(servers);
 
@@ -121,8 +133,10 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
   pthread_attr_setdetachstate(&attr,
                               PTHREAD_CREATE_DETACHED);
 
-  if (opt_initial_load)
-    pairs= load_initial_data(servers, opt_initial_load, &actual_loaded);
+  if (opt_flush)
+    flush_all(servers);
+  if (opt_createial_load)
+    pairs= load_create_data(servers, opt_createial_load, &actual_loaded);
 
   pthread_mutex_lock(&counter_mutex);
   thread_counter= 0;
@@ -202,9 +216,12 @@ void options_parse(int argc, char *argv[])
       {"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},
@@ -254,7 +271,7 @@ void options_parse(int argc, char *argv[])
       opt_execute_number= strtol(optarg, (char **)NULL, 10);
       break;
     case OPT_SLAP_INITIAL_LOAD:
-      opt_initial_load= strtol(optarg, (char **)NULL, 10);
+      opt_createial_load= strtol(optarg, (char **)NULL, 10);
       break;
     case '?':
       /* getopt_long already printed an error message. */
@@ -264,8 +281,8 @@ void options_parse(int argc, char *argv[])
     }
   }
 
-  if (opt_test == GET_TEST && opt_initial_load == 0)
-    opt_initial_load= DEFAULT_INITIAL_LOAD;
+  if (opt_test == GET_TEST && opt_createial_load == 0)
+    opt_createial_load= DEFAULT_INITIAL_LOAD;
 
   if (opt_execute_number == 0)
     opt_execute_number= DEFAULT_EXECUTE_NUMBER;
@@ -293,8 +310,13 @@ void *run_task(void *p)
 {
   thread_context_st *context= (thread_context_st *)p;
   memcached_st *memc;
+  unsigned int value= 1;
 
-  memc= memcached_init(NULL);
+  memc= memcached_create(NULL);
+  if (opt_non_blocking_io)
+    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &value);
+  if (opt_tcp_nodelay)
+    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &value);
   
   memcached_server_push(memc, context->servers);
 
@@ -320,26 +342,43 @@ void *run_task(void *p)
   thread_counter--;
   pthread_cond_signal(&count_threshhold);
   pthread_mutex_unlock(&counter_mutex);
-  memcached_deinit(memc);
+  memcached_free(memc);
 
+  if (context->execute_pairs)
+    pairs_free(context->execute_pairs);
   free(context);
 
   return NULL;
 }
 
-pairs_st *load_initial_data(memcached_server_st *servers, unsigned int number_of, 
+void flush_all(memcached_server_st *servers)
+{
+  memcached_st *memc;
+
+  memc= memcached_create(NULL);
+
+  memcached_server_push(memc, servers);
+
+  memcached_flush(memc, 0);
+
+  memcached_free(memc);
+}
+
+pairs_st *load_create_data(memcached_server_st *servers, unsigned int number_of, 
                             unsigned int *actual_loaded)
 {
   memcached_st *memc;
   pairs_st *pairs;
 
-  memc= memcached_init(NULL);
+  memc= memcached_create(NULL);
+  /* We always used non-blocking IO for load since it is faster */
+  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL );
   memcached_server_push(memc, servers);
 
   pairs= pairs_generate(number_of);
   *actual_loaded= execute_set(memc, pairs, number_of);
 
-  memcached_deinit(memc);
+  memcached_free(memc);
 
   return pairs;
 }