14 #include <memcached.h>
16 #include "client_options.h"
17 #include "utilities.h"
18 #include "generator.h"
21 #define DEFAULT_INITIAL_LOAD 10000
22 #define DEFAULT_EXECUTE_NUMBER 10000
23 #define DEFAULT_CONCURRENCY 1
25 #define PROGRAM_NAME "memslap"
26 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
28 /* Global Thread counter */
29 unsigned int thread_counter
;
30 pthread_mutex_t counter_mutex
;
31 pthread_cond_t count_threshhold
;
32 unsigned int master_wakeup
;
33 pthread_mutex_t sleeper_mutex
;
34 pthread_cond_t sleep_threshhold
;
36 void *run_task(void *p
);
39 typedef struct conclusions_st conclusions_st
;
40 typedef struct thread_context_st thread_context_st
;
46 struct thread_context_st
{
47 unsigned int key_count
;
48 pairs_st
*initial_pairs
;
49 unsigned int initial_number
;
50 pairs_st
*execute_pairs
;
51 unsigned int execute_number
;
53 memcached_server_st
*servers
;
56 struct conclusions_st
{
59 unsigned int rows_loaded
;
60 unsigned int rows_read
;
64 void options_parse(int argc
, char *argv
[]);
65 void conclusions_print(conclusions_st
*conclusion
);
66 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
);
67 pairs_st
*load_create_data(memcached_server_st
*servers
, unsigned int number_of
,
68 unsigned int *actual_loaded
);
69 void flush_all(memcached_server_st
*servers
);
71 static int opt_verbose
= 0;
72 static int opt_flush
= 0;
73 static int opt_non_blocking_io
= 0;
74 static int opt_tcp_nodelay
= 0;
75 static unsigned int opt_execute_number
= 0;
76 static unsigned int opt_createial_load
= 0;
77 static unsigned int opt_concurrency
= 0;
78 static int opt_displayflag
= 0;
79 static char *opt_servers
= NULL
;
80 test_type opt_test
= SET_TEST
;
82 int main(int argc
, char *argv
[])
84 conclusions_st conclusion
;
85 memcached_server_st
*servers
;
87 memset(&conclusion
, 0, sizeof(conclusions_st
));
90 options_parse(argc
, argv
);
96 if ((temp
= getenv("MEMCACHED_SERVERS")))
97 opt_servers
= strdup(temp
);
102 servers
= memcached_servers_parse(opt_servers
);
104 pthread_mutex_init(&counter_mutex
, NULL
);
105 pthread_cond_init(&count_threshhold
, NULL
);
106 pthread_mutex_init(&sleeper_mutex
, NULL
);
107 pthread_cond_init(&sleep_threshhold
, NULL
);
109 scheduler(servers
, &conclusion
);
113 (void)pthread_mutex_destroy(&counter_mutex
);
114 (void)pthread_cond_destroy(&count_threshhold
);
115 (void)pthread_mutex_destroy(&sleeper_mutex
);
116 (void)pthread_cond_destroy(&sleep_threshhold
);
117 conclusions_print(&conclusion
);
118 memcached_server_list_free(servers
);
123 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
)
126 unsigned int actual_loaded
;
128 struct timeval start_time
, end_time
;
129 pthread_t mainthread
; /* Thread descriptor */
130 pthread_attr_t attr
; /* Thread attributes */
131 pairs_st
*pairs
= NULL
;
133 pthread_attr_init(&attr
);
134 pthread_attr_setdetachstate(&attr
,
135 PTHREAD_CREATE_DETACHED
);
139 if (opt_createial_load
)
140 pairs
= load_create_data(servers
, opt_createial_load
, &actual_loaded
);
142 pthread_mutex_lock(&counter_mutex
);
145 pthread_mutex_lock(&sleeper_mutex
);
147 pthread_mutex_unlock(&sleeper_mutex
);
149 for (x
= 0; x
< opt_concurrency
; x
++)
151 thread_context_st
*context
;
152 context
= (thread_context_st
*)malloc(sizeof(thread_context_st
));
153 memset(context
, 0, sizeof(thread_context_st
));
155 context
->servers
= servers
;
156 context
->test
= opt_test
;
158 context
->initial_pairs
= pairs
;
159 context
->initial_number
= actual_loaded
;
161 if (opt_test
== SET_TEST
)
163 context
->execute_pairs
= pairs_generate(opt_execute_number
);
164 context
->execute_number
= opt_execute_number
;
167 /* now you create the thread */
168 if (pthread_create(&mainthread
, &attr
, run_task
,
169 (void *)context
) != 0)
171 fprintf(stderr
,"Could not create thread\n");
177 pthread_mutex_unlock(&counter_mutex
);
178 pthread_attr_destroy(&attr
);
180 pthread_mutex_lock(&sleeper_mutex
);
182 pthread_mutex_unlock(&sleeper_mutex
);
183 pthread_cond_broadcast(&sleep_threshhold
);
185 gettimeofday(&start_time
, NULL
);
187 We loop until we know that all children have cleaned up.
189 pthread_mutex_lock(&counter_mutex
);
190 while (thread_counter
)
192 struct timespec abstime
;
194 memset(&abstime
, 0, sizeof(struct timespec
));
197 pthread_cond_timedwait(&count_threshhold
, &counter_mutex
, &abstime
);
199 pthread_mutex_unlock(&counter_mutex
);
201 gettimeofday(&end_time
, NULL
);
203 conclusion
->load_time
= timedif(end_time
, start_time
);
204 conclusion
->read_time
= timedif(end_time
, start_time
);
208 void options_parse(int argc
, char *argv
[])
210 memcached_programs_help_st help_options
[]=
215 static struct option long_options
[]=
217 {"concurrency", required_argument
, NULL
, OPT_SLAP_CONCURRENCY
},
218 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
219 {"execute-number", required_argument
, NULL
, OPT_SLAP_EXECUTE_NUMBER
},
220 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
221 {"flush", no_argument
, &opt_flush
, OPT_FLUSH
},
222 {"help", no_argument
, NULL
, OPT_HELP
},
223 {"initial-load", required_argument
, NULL
, OPT_SLAP_INITIAL_LOAD
}, /* Number to load initially */
224 {"non-blocking", no_argument
, &opt_non_blocking_io
, OPT_SLAP_NON_BLOCK
},
225 {"servers", required_argument
, NULL
, OPT_SERVERS
},
226 {"tcp-nodelay", no_argument
, &opt_tcp_nodelay
, OPT_SLAP_TCP_NODELAY
},
227 {"test", required_argument
, NULL
, OPT_SLAP_TEST
},
228 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
229 {"version", no_argument
, NULL
, OPT_VERSION
},
238 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
239 if (option_rv
== -1) break;
244 case OPT_VERBOSE
: /* --verbose or -v */
245 opt_verbose
= OPT_VERBOSE
;
247 case OPT_DEBUG
: /* --debug or -d */
248 opt_verbose
= OPT_DEBUG
;
250 case OPT_VERSION
: /* --version or -V */
251 version_command(PROGRAM_NAME
);
253 case OPT_HELP
: /* --help or -h */
254 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
256 case OPT_SERVERS
: /* --servers or -s */
257 opt_servers
= strdup(optarg
);
260 if (!strcmp(optarg
, "get"))
262 else if (!strcmp(optarg
, "set"))
266 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
270 case OPT_SLAP_CONCURRENCY
:
271 opt_concurrency
= strtol(optarg
, (char **)NULL
, 10);
272 case OPT_SLAP_EXECUTE_NUMBER
:
273 opt_execute_number
= strtol(optarg
, (char **)NULL
, 10);
275 case OPT_SLAP_INITIAL_LOAD
:
276 opt_createial_load
= strtol(optarg
, (char **)NULL
, 10);
279 /* getopt_long already printed an error message. */
286 if (opt_test
== GET_TEST
&& opt_createial_load
== 0)
287 opt_createial_load
= DEFAULT_INITIAL_LOAD
;
289 if (opt_execute_number
== 0)
290 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
292 if (opt_concurrency
== 0)
293 opt_concurrency
= DEFAULT_CONCURRENCY
;
296 void conclusions_print(conclusions_st
*conclusion
)
298 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
300 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
301 printf("\tRead %u rows\n", conclusion
->rows_read
);
303 if (opt_test
== SET_TEST
)
304 printf("\tTook %ld.%03ld seconds to load data\n", conclusion
->load_time
/ 1000,
305 conclusion
->load_time
% 1000);
307 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
308 conclusion
->read_time
% 1000);
311 void *run_task(void *p
)
313 thread_context_st
*context
= (thread_context_st
*)p
;
315 unsigned int value
= 1;
317 memc
= memcached_create(NULL
);
318 WATCHPOINT_ASSERT(memc
);
319 if (opt_non_blocking_io
)
320 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, &value
);
322 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, &value
);
324 memcached_server_push(memc
, context
->servers
);
326 pthread_mutex_lock(&sleeper_mutex
);
327 while (master_wakeup
)
329 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
331 pthread_mutex_unlock(&sleeper_mutex
);
334 switch (context
->test
)
337 execute_set(memc
, context
->execute_pairs
, context
->execute_number
);
340 execute_get(memc
, context
->initial_pairs
, context
->initial_number
);
344 memcached_free(memc
);
346 if (context
->execute_pairs
)
347 pairs_free(context
->execute_pairs
);
350 pthread_mutex_lock(&counter_mutex
);
352 pthread_cond_signal(&count_threshhold
);
353 pthread_mutex_unlock(&counter_mutex
);
358 void flush_all(memcached_server_st
*servers
)
362 memc
= memcached_create(NULL
);
364 memcached_server_push(memc
, servers
);
366 memcached_flush(memc
, 0);
368 memcached_free(memc
);
371 pairs_st
*load_create_data(memcached_server_st
*servers
, unsigned int number_of
,
372 unsigned int *actual_loaded
)
377 memc
= memcached_create(NULL
);
378 /* We always used non-blocking IO for load since it is faster */
379 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, NULL
);
380 memcached_server_push(memc
, servers
);
382 pairs
= pairs_generate(number_of
);
383 *actual_loaded
= execute_set(memc
, pairs
, number_of
);
385 memcached_free(memc
);