1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <mem_config.h>
52 #include <sys/types.h>
53 #include <sys/types.h>
58 #include <libmemcached-1.0/memcached.h>
60 #include "client_options.h"
61 #include "utilities.h"
62 #include "generator.h"
65 #define DEFAULT_INITIAL_LOAD 10000
66 #define DEFAULT_EXECUTE_NUMBER 10000
67 #define DEFAULT_CONCURRENCY 1
69 #define PROGRAM_NAME "memslap"
70 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
72 /* Global Thread counter */
73 volatile unsigned int master_wakeup
;
74 pthread_mutex_t sleeper_mutex
;
75 pthread_cond_t sleep_threshhold
;
84 struct thread_context_st
{
85 unsigned int key_count
;
86 pairs_st
*initial_pairs
;
87 unsigned int initial_number
;
88 pairs_st
*execute_pairs
;
89 unsigned int execute_number
;
94 const memcached_st
* root
;
96 thread_context_st(const memcached_st
* memc_arg
, test_t test_arg
) :
112 memc
= memcached_clone(NULL
, root
);
119 pairs_free(execute_pairs
);
121 memcached_free(memc
);
125 struct conclusions_st
{
128 unsigned int rows_loaded
;
129 unsigned int rows_read
;
140 void options_parse(int argc
, char *argv
[]);
141 void conclusions_print(conclusions_st
*conclusion
);
142 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
);
143 pairs_st
*load_create_data(memcached_st
*memc
, unsigned int number_of
,
144 unsigned int *actual_loaded
);
145 void flush_all(memcached_st
*memc
);
147 static bool opt_binary
= 0;
148 static int opt_verbose
= 0;
149 static int opt_flush
= 0;
150 static int opt_non_blocking_io
= 0;
151 static int opt_tcp_nodelay
= 0;
152 static unsigned int opt_execute_number
= 0;
153 static unsigned int opt_createial_load
= 0;
154 static unsigned int opt_concurrency
= 0;
155 static int opt_displayflag
= 0;
156 static char *opt_servers
= NULL
;
157 static bool opt_udp_io
= false;
158 test_t opt_test
= SET_TEST
;
162 static __attribute__((noreturn
)) void *run_task(void *p
)
164 thread_context_st
*context
= (thread_context_st
*)p
;
168 pthread_mutex_lock(&sleeper_mutex
);
169 while (master_wakeup
)
171 pthread_cond_wait(&sleep_threshhold
, &sleeper_mutex
);
173 pthread_mutex_unlock(&sleeper_mutex
);
176 switch (context
->test
)
179 assert(context
->execute_pairs
);
180 execute_set(context
->memc
, context
->execute_pairs
, context
->execute_number
);
184 execute_get(context
->memc
, context
->initial_pairs
, context
->initial_number
);
188 execute_mget(context
->memc
, (const char*const*)context
->keys
, context
->key_lengths
, context
->initial_number
);
200 int main(int argc
, char *argv
[])
202 conclusions_st conclusion
;
204 srandom((unsigned int)time(NULL
));
205 options_parse(argc
, argv
);
207 if (opt_servers
== NULL
)
211 if ((temp
= getenv("MEMCACHED_SERVERS")))
213 opt_servers
= strdup(temp
);
217 fprintf(stderr
, "No Servers provided\n");
222 memcached_server_st
*servers
= memcached_servers_parse(opt_servers
);
224 pthread_mutex_init(&sleeper_mutex
, NULL
);
225 pthread_cond_init(&sleep_threshhold
, NULL
);
227 int error_code
= EXIT_SUCCESS
;
229 scheduler(servers
, &conclusion
);
231 catch(std::exception
& e
)
233 std::cerr
<< "Died with exception: " << e
.what() << std::endl
;
234 error_code
= EXIT_FAILURE
;
239 (void)pthread_mutex_destroy(&sleeper_mutex
);
240 (void)pthread_cond_destroy(&sleep_threshhold
);
241 conclusions_print(&conclusion
);
242 memcached_server_list_free(servers
);
247 void scheduler(memcached_server_st
*servers
, conclusions_st
*conclusion
)
249 unsigned int actual_loaded
= 0; /* Fix warning */
251 struct timeval start_time
, end_time
;
252 pairs_st
*pairs
= NULL
;
254 memcached_st
*memc
= memcached_create(NULL
);
256 memcached_server_push(memc
, servers
);
258 /* We need to set udp behavior before adding servers to the client */
261 if (memcached_failed(memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_USE_UDP
, opt_udp_io
)))
263 std::cerr
<< "Failed to enable UDP." << std::endl
;
264 memcached_free(memc
);
269 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
270 (uint64_t)opt_binary
);
277 if (opt_createial_load
)
279 pairs
= load_create_data(memc
, opt_createial_load
, &actual_loaded
);
282 char **keys
= static_cast<char **>(calloc(actual_loaded
, sizeof(char*)));
283 size_t *key_lengths
= static_cast<size_t *>(calloc(actual_loaded
, sizeof(size_t)));
285 if (keys
== NULL
or key_lengths
== NULL
)
294 for (uint32_t x
= 0; x
< actual_loaded
; ++x
)
296 keys
[x
]= pairs
[x
].key
;
297 key_lengths
[x
]= pairs
[x
].key_length
;
301 /* We set this after we have loaded */
303 if (opt_non_blocking_io
)
304 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 1);
307 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_TCP_NODELAY
, 1);
310 pthread_mutex_lock(&sleeper_mutex
);
312 pthread_mutex_unlock(&sleeper_mutex
);
314 pthread_t
*threads
= new (std::nothrow
) pthread_t
[opt_concurrency
];
321 for (uint32_t x
= 0; x
< opt_concurrency
; x
++)
323 thread_context_st
*context
= new thread_context_st(memc
, opt_test
);
324 context
->test
= opt_test
;
326 context
->initial_pairs
= pairs
;
327 context
->initial_number
= actual_loaded
;
329 context
->key_lengths
= key_lengths
;
331 if (opt_test
== SET_TEST
)
333 context
->execute_pairs
= pairs_generate(opt_execute_number
, 400);
334 context
->execute_number
= opt_execute_number
;
337 /* now you create the thread */
338 if (pthread_create(threads
+x
, NULL
, run_task
, (void *)context
) != 0)
340 fprintf(stderr
,"Could not create thread\n");
345 pthread_mutex_lock(&sleeper_mutex
);
347 pthread_mutex_unlock(&sleeper_mutex
);
348 pthread_cond_broadcast(&sleep_threshhold
);
349 gettimeofday(&start_time
, NULL
);
351 for (uint32_t x
= 0; x
< opt_concurrency
; x
++)
354 pthread_join(threads
[x
], &retval
);
358 gettimeofday(&end_time
, NULL
);
360 conclusion
->load_time
= timedif(end_time
, start_time
);
361 conclusion
->read_time
= timedif(end_time
, start_time
);
365 memcached_free(memc
);
368 void options_parse(int argc
, char *argv
[])
370 memcached_programs_help_st help_options
[]=
375 static struct option long_options
[]=
377 {(OPTIONSTRING
)"concurrency", required_argument
, NULL
, OPT_SLAP_CONCURRENCY
},
378 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
379 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
380 {(OPTIONSTRING
)"execute-number", required_argument
, NULL
, OPT_SLAP_EXECUTE_NUMBER
},
381 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
382 {(OPTIONSTRING
)"flush", no_argument
, &opt_flush
, OPT_FLUSH
},
383 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
384 {(OPTIONSTRING
)"initial-load", required_argument
, NULL
, OPT_SLAP_INITIAL_LOAD
}, /* Number to load initially */
385 {(OPTIONSTRING
)"non-blocking", no_argument
, &opt_non_blocking_io
, OPT_SLAP_NON_BLOCK
},
386 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
387 {(OPTIONSTRING
)"tcp-nodelay", no_argument
, &opt_tcp_nodelay
, OPT_SLAP_TCP_NODELAY
},
388 {(OPTIONSTRING
)"test", required_argument
, NULL
, OPT_SLAP_TEST
},
389 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
390 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
391 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
392 {(OPTIONSTRING
)"udp", no_argument
, NULL
, OPT_UDP
},
396 bool opt_help
= false;
397 bool opt_version
= false;
401 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
403 if (option_rv
== -1) break;
411 if (opt_test
== GET_TEST
)
413 fprintf(stderr
, "You can not run a get test in UDP mode. UDP mode "
414 "does not currently support get ops.\n");
424 case OPT_VERBOSE
: /* --verbose or -v */
425 opt_verbose
= OPT_VERBOSE
;
428 case OPT_DEBUG
: /* --debug or -d */
429 opt_verbose
= OPT_DEBUG
;
432 case OPT_VERSION
: /* --version or -V */
436 case OPT_HELP
: /* --help or -h */
440 case OPT_SERVERS
: /* --servers or -s */
441 opt_servers
= strdup(optarg
);
445 if (strcmp(optarg
, "get") == 0)
449 fprintf(stderr
, "You can not run a get test in UDP mode. UDP mode "
450 "does not currently support get ops.\n");
455 else if (strcmp(optarg
, "set") == 0)
459 else if (strcmp(optarg
, "mget") == 0)
465 fprintf(stderr
, "Your test, %s, is not a known test\n", optarg
);
470 case OPT_SLAP_CONCURRENCY
:
471 opt_concurrency
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
474 case OPT_SLAP_EXECUTE_NUMBER
:
475 opt_execute_number
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
478 case OPT_SLAP_INITIAL_LOAD
:
479 opt_createial_load
= (unsigned int)strtoul(optarg
, (char **)NULL
, 10);
488 /* getopt_long already printed an error message. */
498 version_command(PROGRAM_NAME
);
504 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
508 if ((opt_test
== GET_TEST
or opt_test
== MGET_TEST
) and opt_createial_load
== 0)
509 opt_createial_load
= DEFAULT_INITIAL_LOAD
;
511 if (opt_execute_number
== 0)
512 opt_execute_number
= DEFAULT_EXECUTE_NUMBER
;
514 if (opt_concurrency
== 0)
515 opt_concurrency
= DEFAULT_CONCURRENCY
;
518 void conclusions_print(conclusions_st
*conclusion
)
520 printf("\tThreads connecting to servers %u\n", opt_concurrency
);
522 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
523 printf("\tRead %u rows\n", conclusion
->rows_read
);
525 if (opt_test
== SET_TEST
)
526 printf("\tTook %ld.%03ld seconds to load data\n", conclusion
->load_time
/ 1000,
527 conclusion
->load_time
% 1000);
529 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
530 conclusion
->read_time
% 1000);
533 void flush_all(memcached_st
*memc
)
535 memcached_flush(memc
, 0);
538 pairs_st
*load_create_data(memcached_st
*memc
, unsigned int number_of
,
539 unsigned int *actual_loaded
)
541 memcached_st
*memc_clone
= memcached_clone(NULL
, memc
);
542 /* We always used non-blocking IO for load since it is faster */
543 memcached_behavior_set(memc_clone
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
545 pairs_st
*pairs
= pairs_generate(number_of
, 400);
546 *actual_loaded
= execute_set(memc_clone
, pairs
, number_of
);
548 memcached_free(memc_clone
);