Removing memory issue/cleanup for threads in src/memslap.c
[awesomized/libmemcached] / src / memslap.c
index 5fe6d72edd21940f5ac1978f45fedaa08ec42f38..487b60230f11ddf7842b60346e75a41e0f2dfe29 100644 (file)
@@ -1,10 +1,12 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <fcntl.h>
+#include <assert.h>
 #include <sys/time.h>
 #include <getopt.h>
 #include <pthread.h>
@@ -62,10 +64,14 @@ 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_createial_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_createial_load= 0;
 static unsigned int opt_concurrency= 0;
@@ -84,9 +90,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);
@@ -121,8 +134,10 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
   pthread_attr_setdetachstate(&attr,
                               PTHREAD_CREATE_DETACHED);
 
+  if (opt_flush)
+    flush_all(servers);
   if (opt_createial_load)
-    pairs= load_createial_data(servers, opt_createial_load, &actual_loaded);
+    pairs= load_create_data(servers, opt_createial_load, &actual_loaded);
 
   pthread_mutex_lock(&counter_mutex);
   thread_counter= 0;
@@ -202,9 +217,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},
@@ -293,8 +311,14 @@ void *run_task(void *p)
 {
   thread_context_st *context= (thread_context_st *)p;
   memcached_st *memc;
+  unsigned int value= 1;
 
   memc= memcached_create(NULL);
+  WATCHPOINT_ASSERT(memc);
+  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);
 
@@ -316,24 +340,42 @@ void *run_task(void *p)
     break;
   }
 
+  memcached_free(memc);
+
+  if (context->execute_pairs)
+    pairs_free(context->execute_pairs);
+  free(context);
+
   pthread_mutex_lock(&counter_mutex);
   thread_counter--;
   pthread_cond_signal(&count_threshhold);
   pthread_mutex_unlock(&counter_mutex);
-  memcached_free(memc);
-
-  free(context);
 
   return NULL;
 }
 
-pairs_st *load_createial_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_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);