1 #include "libmemcached/common.h"
15 #include <libmemcached/memcached.h>
17 #include "client_options.h"
18 #include "utilities.h"
19 #include "generator.h"
22 #define DEFAULT_INITIAL_LOAD 10000
23 #define DEFAULT_EXECUTE_NUMBER 10000
24 #define DEFAULT_CONCURRENCY 1
26 #define PROGRAM_NAME "memslap"
27 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
29 /* Global Thread counter */
30 volatile unsigned int thread_counter
;
31 pthread_mutex_t counter_mutex
;
32 pthread_cond_t count_threshhold
;
33 volatile unsigned int master_wakeup
;
34 pthread_mutex_t sleeper_mutex
;
35 pthread_cond_t sleep_threshhold
;
37 void *run_task(void *p
);
40 typedef struct conclusions_st conclusions_st
;
41 typedef struct thread_context_st thread_context_st
;
48 struct thread_context_st
{
49 unsigned int key_count
;
50 pairs_st
*initial_pairs
;
51 unsigned int initial_number
;
52 pairs_st
*execute_pairs
;
53 unsigned int execute_number
;
60 struct conclusions_st
{
63 unsigned int rows_loaded
;
64 unsigned int rows_read
;
68 void options_parse(int argc
, char *argv
[]);
69 void conclusions_print(conclusions_st
*conclusion
);
70 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
);
71 pairs_st
*load_create_data(memcached_st
*memc
, unsigned int number_of
,
72 unsigned int *actual_loaded
);
73 void flush_all(memcached_st
*memc
);
75 static int opt_binary
= 0;
76 static int opt_verbose
= 0;
77 static int opt_flush
= 0;
78 static int opt_non_blocking_io
= 0;
79 static int opt_tcp_nodelay
= 0;
80 static unsigned int opt_execute_number
= 0;
81 static unsigned int opt_createial_load
= 0;
82 static unsigned int opt_concurrency
= 0;
83 static int opt_displayflag
= 0;
84 static char *opt_servers
= NULL
;
85 static int opt_udp_io
= 0;
86 test_type opt_test
= SET_TEST
;
88 int main(int argc
, char *argv
[])
90 conclusions_st conclusion
;
91 memcached_server_st
*servers
;
93 memset(&conclusion
, 0, sizeof(conclusions_st
));
95 srandom((unsigned int)time(NULL
));
96 options_parse(argc
, argv
);
102 if ((temp
= getenv("MEMCACHED_SERVERS")))
103 opt_servers
= strdup(temp
);
106 fprintf(stderr
, "No Servers provided\n");
111 servers
= memcached_servers_parse(opt_servers
);
113 pthread_mutex_init(&counter_mutex
, NULL
);
114 pthread_cond_init(&count_threshhold
, NULL
);
115 pthread_mutex_init(&sleeper_mutex
, NULL
);
116 pthread_cond_init(&sleep_threshhold
, NULL
);
118 scheduler(servers
, &conclusion
);
122 (void)pthread_mutex_destroy(&counter_mutex
);
123 (void)pthread_cond_destroy(&count_threshhold
);
124 (void)pthread_mutex_destroy(&sleeper_mutex
);
125 (void)pthread_cond_destroy(&sleep_threshhold
);
126 conclusions_print(&conclusion
);
127 memcached_server_list_free(servers
);
132 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
)
135 unsigned int actual_loaded
= 0; /* Fix warning */
138 struct timeval start_time
, end_time
;
139 pthread_t mainthread
; /* Thread descriptor */
140 pthread_attr_t attr
; /* Thread attributes */
141 pairs_st
*pairs
= NULL
;
143 pthread_attr_init(&attr
);
144 pthread_attr_setdetachstate(&attr
,
145 PTHREAD_CREATE_DETACHED
);
147 memc
= memcached_create(NULL
);
149 /* We need to set udp behavior before adding servers to the client */
152 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
,
153 (uint64_t)opt_udp_io
);
154 for(x
= 0; x
< servers
[0].count
; x
++ )
155 servers
[x
].type
= MEMCACHED_CONNECTION_UDP
;
157 memcached_server_push(memc
, servers
);
159 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
160 (uint64_t)opt_binary
);
164 if (opt_createial_load
)
165 pairs
= load_create_data(memc
, opt_createial_load
, &actual_loaded
);
167 char **keys
= calloc(actual_loaded
, sizeof(char*));
168 size_t *key_lengths
= calloc(actual_loaded
, sizeof(size_t));
170 if (keys
== NULL
|| key_lengths
== NULL
)
177 for (x
= 0; x
< actual_loaded
; ++x
)
179 keys
[x
]= pairs
[x
].key
;
180 key_lengths
[x
]= pairs
[x
].key_length
;
184 /* We set this after we have loaded */
186 if (opt_non_blocking_io
)
187 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
189 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
192 pthread_mutex_lock(&counter_mutex
);
195 pthread_mutex_lock(&sleeper_mutex
);
197 pthread_mutex_unlock(&sleeper_mutex
);
199 for (x
= 0; x
< opt_concurrency
; x
++)
201 thread_context_st
*context
;
202 context
= (thread_context_st
*)calloc(1, sizeof(thread_context_st
));
204 context
->memc
= memcached_clone(NULL
, memc
);
205 context
->test
= opt_test
;
207 context
->initial_pairs
= pairs
;
208 context
->initial_number
= actual_loaded
;
210 context
->key_lengths
= key_lengths
;
212 if (opt_test
== SET_TEST
)
214 context
->execute_pairs
= pairs_generate(opt_execute_number
, 400);
215 context
->execute_number
= opt_execute_number
;
218 /* now you create the thread */
219 if (pthread_create(&mainthread
, &attr
, run_task
,
220 (void *)context
) != 0)
222 fprintf(stderr
,"Could not create thread\n");
228 pthread_mutex_unlock(&counter_mutex
);
229 pthread_attr_destroy(&attr
);
231 pthread_mutex_lock(&sleeper_mutex
);
233 pthread_mutex_unlock(&sleeper_mutex
);
234 pthread_cond_broadcast(&sleep_threshhold
);
236 gettimeofday(&start_time
, NULL
);
238 We loop until we know that all children have cleaned up.
240 pthread_mutex_lock(&counter_mutex
);
241 while (thread_counter
)
242 pthread_cond_wait(&count_threshhold
, &counter_mutex
);
243 pthread_mutex_unlock(&counter_mutex
);
245 gettimeofday(&end_time
, NULL
);
247 conclusion
->load_time
= timedif(end_time
, start_time
);
248 conclusion
->read_time
= timedif(end_time
, start_time
);
252 memcached_free(memc
);
255 void options_parse(int argc
, char *argv
[])
257 memcached_programs_help_st help_options
[]=
262 static struct option long_options
[]=
264 {(OPTIONSTRING
)"concurrency", required_argument
, NULL
, OPT_SLAP_CONCURRENCY
},
265 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
266 {(OPTIONSTRING
)"execute-number", required_argument
, NULL
, OPT_SLAP_EXECUTE_NUMBER
},
267 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
268 {(OPTIONSTRING
)"flush", no_argument
, &opt_flush
, OPT_FLUSH
},
269 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
270 {(OPTIONSTRING
)"initial-load", required_argument
, NULL
, OPT_SLAP_INITIAL_LOAD
}, /* Number to load initially */
271 {(OPTIONSTRING
)"non-blocking", no_argument
, &opt_non_blocking_io
, OPT_SLAP_NON_BLOCK
},
272 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
273 {(OPTIONSTRING
)"tcp-nodelay", no_argument
, &opt_tcp_nodelay
, OPT_SLAP_TCP_NODELAY
},
274 {(OPTIONSTRING
)"test", required_argument
, NULL
, OPT_SLAP_TEST
},
275 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
276 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
277 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
278 {(OPTIONSTRING
)"udp", no_argument
, NULL
, OPT_UDP
},
287 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
288 if (option_rv
== -1) break;
294 if (opt_test
== GET_TEST
)
296 fprintf(stderr
, "You can not run a get test in UDP mode. UDP mode "
297 "does not currently support get ops.\n");
305 case OPT_VERBOSE
: /* --verbose or -v */
306 opt_verbose
= OPT_VERBOSE
;
308 case OPT_DEBUG
: /* --debug or -d */
309 opt_verbose
= OPT_DEBUG
;
311 case OPT_VERSION
: /* --version or -V */
312 version_command(PROGRAM_NAME
);
314 case OPT_HELP
: /* --help or -h */
315 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
317 case OPT_SERVERS
: /* --servers or -s */
318 opt_servers
= strdup(optarg
);
321 if (!strcmp(optarg
, "get"))
325 fprintf(stderr
, "You can not run a get test in UDP mode. UDP mode "
326 "does not currently support get ops.\n");
331 else if (!strcmp(optarg
, "set"))
333 else if (!strcmp(optarg
, "mget"))
339 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
343 case OPT_SLAP_CONCURRENCY
:
344 opt_concurrency
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
346 case OPT_SLAP_EXECUTE_NUMBER
:
347 opt_execute_number
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
349 case OPT_SLAP_INITIAL_LOAD
:
350 opt_createial_load
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
353 /* getopt_long already printed an error message. */
360 if ((opt_test
== GET_TEST
|| opt_test
== MGET_TEST
) && opt_createial_load
== 0)
361 opt_createial_load
= DEFAULT_INITIAL_LOAD
;
363 if (opt_execute_number
== 0)
364 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
366 if (opt_concurrency
== 0)
367 opt_concurrency
= DEFAULT_CONCURRENCY
;
370 void conclusions_print(conclusions_st
*conclusion
)
372 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
374 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
375 printf("\tRead %u rows\n", conclusion
->rows_read
);
377 if (opt_test
== SET_TEST
)
378 printf("\tTook %ld.%03ld seconds to load data\n", conclusion
->load_time
/ 1000,
379 conclusion
->load_time
% 1000);
381 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
382 conclusion
->read_time
% 1000);
385 void *run_task(void *p
)
387 thread_context_st
*context
= (thread_context_st
*)p
;
392 pthread_mutex_lock(&sleeper_mutex
);
393 while (master_wakeup
)
395 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
397 pthread_mutex_unlock(&sleeper_mutex
);
400 switch (context
->test
)
403 assert(context
->execute_pairs
);
404 execute_set(memc
, context
->execute_pairs
, context
->execute_number
);
407 execute_get(memc
, context
->initial_pairs
, context
->initial_number
);
410 execute_mget(memc
, (const char*const*)context
->keys
, context
->key_lengths
,
411 context
->initial_number
);
414 WATCHPOINT_ASSERT(context
->test
);
418 memcached_free(memc
);
420 if (context
->execute_pairs
)
421 pairs_free(context
->execute_pairs
);
425 pthread_mutex_lock(&counter_mutex
);
427 pthread_cond_signal(&count_threshhold
);
428 pthread_mutex_unlock(&counter_mutex
);
433 void flush_all(memcached_st
*memc
)
435 memcached_flush(memc
, 0);
438 pairs_st
*load_create_data(memcached_st
*memc
, unsigned int number_of
,
439 unsigned int *actual_loaded
)
441 memcached_st
*memc_clone
;
444 memc_clone
= memcached_clone(NULL
, memc
);
445 /* We always used non-blocking IO for load since it is faster */
446 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
448 pairs
= pairs_generate(number_of
, 400);
449 *actual_loaded
= execute_set(memc_clone
, pairs
, number_of
);
451 memcached_free(memc_clone
);